xref: /linux/drivers/scsi/lpfc/lpfc_init.c (revision c39b9fd728d8173ecda993524089fbc38211a17f)
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2012 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/module.h>
28 #include <linux/kthread.h>
29 #include <linux/pci.h>
30 #include <linux/spinlock.h>
31 #include <linux/ctype.h>
32 #include <linux/aer.h>
33 #include <linux/slab.h>
34 #include <linux/firmware.h>
35 #include <linux/miscdevice.h>
36 
37 #include <scsi/scsi.h>
38 #include <scsi/scsi_device.h>
39 #include <scsi/scsi_host.h>
40 #include <scsi/scsi_transport_fc.h>
41 
42 #include "lpfc_hw4.h"
43 #include "lpfc_hw.h"
44 #include "lpfc_sli.h"
45 #include "lpfc_sli4.h"
46 #include "lpfc_nl.h"
47 #include "lpfc_disc.h"
48 #include "lpfc_scsi.h"
49 #include "lpfc.h"
50 #include "lpfc_logmsg.h"
51 #include "lpfc_crtn.h"
52 #include "lpfc_vport.h"
53 #include "lpfc_version.h"
54 
55 char *_dump_buf_data;
56 unsigned long _dump_buf_data_order;
57 char *_dump_buf_dif;
58 unsigned long _dump_buf_dif_order;
59 spinlock_t _dump_buf_lock;
60 
61 static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
62 static int lpfc_post_rcv_buf(struct lpfc_hba *);
63 static int lpfc_sli4_queue_verify(struct lpfc_hba *);
64 static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
65 static int lpfc_setup_endian_order(struct lpfc_hba *);
66 static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
67 static void lpfc_free_els_sgl_list(struct lpfc_hba *);
68 static void lpfc_init_sgl_list(struct lpfc_hba *);
69 static int lpfc_init_active_sgl_array(struct lpfc_hba *);
70 static void lpfc_free_active_sgl(struct lpfc_hba *);
71 static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
72 static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
73 static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
74 static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
75 static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
76 static void lpfc_sli4_disable_intr(struct lpfc_hba *);
77 static uint32_t lpfc_sli4_enable_intr(struct lpfc_hba *, uint32_t);
78 
79 static struct scsi_transport_template *lpfc_transport_template = NULL;
80 static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
81 static DEFINE_IDR(lpfc_hba_index);
82 
83 /**
84  * lpfc_config_port_prep - Perform lpfc initialization prior to config port
85  * @phba: pointer to lpfc hba data structure.
86  *
87  * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
88  * mailbox command. It retrieves the revision information from the HBA and
89  * collects the Vital Product Data (VPD) about the HBA for preparing the
90  * configuration of the HBA.
91  *
92  * Return codes:
93  *   0 - success.
94  *   -ERESTART - requests the SLI layer to reset the HBA and try again.
95  *   Any other value - indicates an error.
96  **/
97 int
98 lpfc_config_port_prep(struct lpfc_hba *phba)
99 {
100 	lpfc_vpd_t *vp = &phba->vpd;
101 	int i = 0, rc;
102 	LPFC_MBOXQ_t *pmb;
103 	MAILBOX_t *mb;
104 	char *lpfc_vpd_data = NULL;
105 	uint16_t offset = 0;
106 	static char licensed[56] =
107 		    "key unlock for use with gnu public licensed code only\0";
108 	static int init_key = 1;
109 
110 	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
111 	if (!pmb) {
112 		phba->link_state = LPFC_HBA_ERROR;
113 		return -ENOMEM;
114 	}
115 
116 	mb = &pmb->u.mb;
117 	phba->link_state = LPFC_INIT_MBX_CMDS;
118 
119 	if (lpfc_is_LC_HBA(phba->pcidev->device)) {
120 		if (init_key) {
121 			uint32_t *ptext = (uint32_t *) licensed;
122 
123 			for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
124 				*ptext = cpu_to_be32(*ptext);
125 			init_key = 0;
126 		}
127 
128 		lpfc_read_nv(phba, pmb);
129 		memset((char*)mb->un.varRDnvp.rsvd3, 0,
130 			sizeof (mb->un.varRDnvp.rsvd3));
131 		memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
132 			 sizeof (licensed));
133 
134 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
135 
136 		if (rc != MBX_SUCCESS) {
137 			lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
138 					"0324 Config Port initialization "
139 					"error, mbxCmd x%x READ_NVPARM, "
140 					"mbxStatus x%x\n",
141 					mb->mbxCommand, mb->mbxStatus);
142 			mempool_free(pmb, phba->mbox_mem_pool);
143 			return -ERESTART;
144 		}
145 		memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
146 		       sizeof(phba->wwnn));
147 		memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
148 		       sizeof(phba->wwpn));
149 	}
150 
151 	phba->sli3_options = 0x0;
152 
153 	/* Setup and issue mailbox READ REV command */
154 	lpfc_read_rev(phba, pmb);
155 	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
156 	if (rc != MBX_SUCCESS) {
157 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
158 				"0439 Adapter failed to init, mbxCmd x%x "
159 				"READ_REV, mbxStatus x%x\n",
160 				mb->mbxCommand, mb->mbxStatus);
161 		mempool_free( pmb, phba->mbox_mem_pool);
162 		return -ERESTART;
163 	}
164 
165 
166 	/*
167 	 * The value of rr must be 1 since the driver set the cv field to 1.
168 	 * This setting requires the FW to set all revision fields.
169 	 */
170 	if (mb->un.varRdRev.rr == 0) {
171 		vp->rev.rBit = 0;
172 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
173 				"0440 Adapter failed to init, READ_REV has "
174 				"missing revision information.\n");
175 		mempool_free(pmb, phba->mbox_mem_pool);
176 		return -ERESTART;
177 	}
178 
179 	if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
180 		mempool_free(pmb, phba->mbox_mem_pool);
181 		return -EINVAL;
182 	}
183 
184 	/* Save information as VPD data */
185 	vp->rev.rBit = 1;
186 	memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
187 	vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
188 	memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
189 	vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
190 	memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
191 	vp->rev.biuRev = mb->un.varRdRev.biuRev;
192 	vp->rev.smRev = mb->un.varRdRev.smRev;
193 	vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
194 	vp->rev.endecRev = mb->un.varRdRev.endecRev;
195 	vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
196 	vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
197 	vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
198 	vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
199 	vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
200 	vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
201 
202 	/* If the sli feature level is less then 9, we must
203 	 * tear down all RPIs and VPIs on link down if NPIV
204 	 * is enabled.
205 	 */
206 	if (vp->rev.feaLevelHigh < 9)
207 		phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
208 
209 	if (lpfc_is_LC_HBA(phba->pcidev->device))
210 		memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
211 						sizeof (phba->RandomData));
212 
213 	/* Get adapter VPD information */
214 	lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
215 	if (!lpfc_vpd_data)
216 		goto out_free_mbox;
217 	do {
218 		lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
219 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
220 
221 		if (rc != MBX_SUCCESS) {
222 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
223 					"0441 VPD not present on adapter, "
224 					"mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
225 					mb->mbxCommand, mb->mbxStatus);
226 			mb->un.varDmp.word_cnt = 0;
227 		}
228 		/* dump mem may return a zero when finished or we got a
229 		 * mailbox error, either way we are done.
230 		 */
231 		if (mb->un.varDmp.word_cnt == 0)
232 			break;
233 		if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
234 			mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
235 		lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
236 				      lpfc_vpd_data + offset,
237 				      mb->un.varDmp.word_cnt);
238 		offset += mb->un.varDmp.word_cnt;
239 	} while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
240 	lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
241 
242 	kfree(lpfc_vpd_data);
243 out_free_mbox:
244 	mempool_free(pmb, phba->mbox_mem_pool);
245 	return 0;
246 }
247 
248 /**
249  * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
250  * @phba: pointer to lpfc hba data structure.
251  * @pmboxq: pointer to the driver internal queue element for mailbox command.
252  *
253  * This is the completion handler for driver's configuring asynchronous event
254  * mailbox command to the device. If the mailbox command returns successfully,
255  * it will set internal async event support flag to 1; otherwise, it will
256  * set internal async event support flag to 0.
257  **/
258 static void
259 lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
260 {
261 	if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
262 		phba->temp_sensor_support = 1;
263 	else
264 		phba->temp_sensor_support = 0;
265 	mempool_free(pmboxq, phba->mbox_mem_pool);
266 	return;
267 }
268 
269 /**
270  * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
271  * @phba: pointer to lpfc hba data structure.
272  * @pmboxq: pointer to the driver internal queue element for mailbox command.
273  *
274  * This is the completion handler for dump mailbox command for getting
275  * wake up parameters. When this command complete, the response contain
276  * Option rom version of the HBA. This function translate the version number
277  * into a human readable string and store it in OptionROMVersion.
278  **/
279 static void
280 lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
281 {
282 	struct prog_id *prg;
283 	uint32_t prog_id_word;
284 	char dist = ' ';
285 	/* character array used for decoding dist type. */
286 	char dist_char[] = "nabx";
287 
288 	if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
289 		mempool_free(pmboxq, phba->mbox_mem_pool);
290 		return;
291 	}
292 
293 	prg = (struct prog_id *) &prog_id_word;
294 
295 	/* word 7 contain option rom version */
296 	prog_id_word = pmboxq->u.mb.un.varWords[7];
297 
298 	/* Decode the Option rom version word to a readable string */
299 	if (prg->dist < 4)
300 		dist = dist_char[prg->dist];
301 
302 	if ((prg->dist == 3) && (prg->num == 0))
303 		sprintf(phba->OptionROMVersion, "%d.%d%d",
304 			prg->ver, prg->rev, prg->lev);
305 	else
306 		sprintf(phba->OptionROMVersion, "%d.%d%d%c%d",
307 			prg->ver, prg->rev, prg->lev,
308 			dist, prg->num);
309 	mempool_free(pmboxq, phba->mbox_mem_pool);
310 	return;
311 }
312 
313 /**
314  * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
315  *	cfg_soft_wwnn, cfg_soft_wwpn
316  * @vport: pointer to lpfc vport data structure.
317  *
318  *
319  * Return codes
320  *   None.
321  **/
322 void
323 lpfc_update_vport_wwn(struct lpfc_vport *vport)
324 {
325 	/* If the soft name exists then update it using the service params */
326 	if (vport->phba->cfg_soft_wwnn)
327 		u64_to_wwn(vport->phba->cfg_soft_wwnn,
328 			   vport->fc_sparam.nodeName.u.wwn);
329 	if (vport->phba->cfg_soft_wwpn)
330 		u64_to_wwn(vport->phba->cfg_soft_wwpn,
331 			   vport->fc_sparam.portName.u.wwn);
332 
333 	/*
334 	 * If the name is empty or there exists a soft name
335 	 * then copy the service params name, otherwise use the fc name
336 	 */
337 	if (vport->fc_nodename.u.wwn[0] == 0 || vport->phba->cfg_soft_wwnn)
338 		memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
339 			sizeof(struct lpfc_name));
340 	else
341 		memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
342 			sizeof(struct lpfc_name));
343 
344 	if (vport->fc_portname.u.wwn[0] == 0 || vport->phba->cfg_soft_wwpn)
345 		memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
346 			sizeof(struct lpfc_name));
347 	else
348 		memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
349 			sizeof(struct lpfc_name));
350 }
351 
352 /**
353  * lpfc_config_port_post - Perform lpfc initialization after config port
354  * @phba: pointer to lpfc hba data structure.
355  *
356  * This routine will do LPFC initialization after the CONFIG_PORT mailbox
357  * command call. It performs all internal resource and state setups on the
358  * port: post IOCB buffers, enable appropriate host interrupt attentions,
359  * ELS ring timers, etc.
360  *
361  * Return codes
362  *   0 - success.
363  *   Any other value - error.
364  **/
365 int
366 lpfc_config_port_post(struct lpfc_hba *phba)
367 {
368 	struct lpfc_vport *vport = phba->pport;
369 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
370 	LPFC_MBOXQ_t *pmb;
371 	MAILBOX_t *mb;
372 	struct lpfc_dmabuf *mp;
373 	struct lpfc_sli *psli = &phba->sli;
374 	uint32_t status, timeout;
375 	int i, j;
376 	int rc;
377 
378 	spin_lock_irq(&phba->hbalock);
379 	/*
380 	 * If the Config port completed correctly the HBA is not
381 	 * over heated any more.
382 	 */
383 	if (phba->over_temp_state == HBA_OVER_TEMP)
384 		phba->over_temp_state = HBA_NORMAL_TEMP;
385 	spin_unlock_irq(&phba->hbalock);
386 
387 	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
388 	if (!pmb) {
389 		phba->link_state = LPFC_HBA_ERROR;
390 		return -ENOMEM;
391 	}
392 	mb = &pmb->u.mb;
393 
394 	/* Get login parameters for NID.  */
395 	rc = lpfc_read_sparam(phba, pmb, 0);
396 	if (rc) {
397 		mempool_free(pmb, phba->mbox_mem_pool);
398 		return -ENOMEM;
399 	}
400 
401 	pmb->vport = vport;
402 	if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
403 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
404 				"0448 Adapter failed init, mbxCmd x%x "
405 				"READ_SPARM mbxStatus x%x\n",
406 				mb->mbxCommand, mb->mbxStatus);
407 		phba->link_state = LPFC_HBA_ERROR;
408 		mp = (struct lpfc_dmabuf *) pmb->context1;
409 		mempool_free(pmb, phba->mbox_mem_pool);
410 		lpfc_mbuf_free(phba, mp->virt, mp->phys);
411 		kfree(mp);
412 		return -EIO;
413 	}
414 
415 	mp = (struct lpfc_dmabuf *) pmb->context1;
416 
417 	memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
418 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
419 	kfree(mp);
420 	pmb->context1 = NULL;
421 	lpfc_update_vport_wwn(vport);
422 
423 	/* Update the fc_host data structures with new wwn. */
424 	fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
425 	fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
426 	fc_host_max_npiv_vports(shost) = phba->max_vpi;
427 
428 	/* If no serial number in VPD data, use low 6 bytes of WWNN */
429 	/* This should be consolidated into parse_vpd ? - mr */
430 	if (phba->SerialNumber[0] == 0) {
431 		uint8_t *outptr;
432 
433 		outptr = &vport->fc_nodename.u.s.IEEE[0];
434 		for (i = 0; i < 12; i++) {
435 			status = *outptr++;
436 			j = ((status & 0xf0) >> 4);
437 			if (j <= 9)
438 				phba->SerialNumber[i] =
439 				    (char)((uint8_t) 0x30 + (uint8_t) j);
440 			else
441 				phba->SerialNumber[i] =
442 				    (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
443 			i++;
444 			j = (status & 0xf);
445 			if (j <= 9)
446 				phba->SerialNumber[i] =
447 				    (char)((uint8_t) 0x30 + (uint8_t) j);
448 			else
449 				phba->SerialNumber[i] =
450 				    (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
451 		}
452 	}
453 
454 	lpfc_read_config(phba, pmb);
455 	pmb->vport = vport;
456 	if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
457 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
458 				"0453 Adapter failed to init, mbxCmd x%x "
459 				"READ_CONFIG, mbxStatus x%x\n",
460 				mb->mbxCommand, mb->mbxStatus);
461 		phba->link_state = LPFC_HBA_ERROR;
462 		mempool_free( pmb, phba->mbox_mem_pool);
463 		return -EIO;
464 	}
465 
466 	/* Check if the port is disabled */
467 	lpfc_sli_read_link_ste(phba);
468 
469 	/* Reset the DFT_HBA_Q_DEPTH to the max xri  */
470 	if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
471 		phba->cfg_hba_queue_depth =
472 			(mb->un.varRdConfig.max_xri + 1) -
473 					lpfc_sli4_get_els_iocb_cnt(phba);
474 
475 	phba->lmt = mb->un.varRdConfig.lmt;
476 
477 	/* Get the default values for Model Name and Description */
478 	lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
479 
480 	phba->link_state = LPFC_LINK_DOWN;
481 
482 	/* Only process IOCBs on ELS ring till hba_state is READY */
483 	if (psli->ring[psli->extra_ring].sli.sli3.cmdringaddr)
484 		psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
485 	if (psli->ring[psli->fcp_ring].sli.sli3.cmdringaddr)
486 		psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
487 	if (psli->ring[psli->next_ring].sli.sli3.cmdringaddr)
488 		psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
489 
490 	/* Post receive buffers for desired rings */
491 	if (phba->sli_rev != 3)
492 		lpfc_post_rcv_buf(phba);
493 
494 	/*
495 	 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
496 	 */
497 	if (phba->intr_type == MSIX) {
498 		rc = lpfc_config_msi(phba, pmb);
499 		if (rc) {
500 			mempool_free(pmb, phba->mbox_mem_pool);
501 			return -EIO;
502 		}
503 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
504 		if (rc != MBX_SUCCESS) {
505 			lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
506 					"0352 Config MSI mailbox command "
507 					"failed, mbxCmd x%x, mbxStatus x%x\n",
508 					pmb->u.mb.mbxCommand,
509 					pmb->u.mb.mbxStatus);
510 			mempool_free(pmb, phba->mbox_mem_pool);
511 			return -EIO;
512 		}
513 	}
514 
515 	spin_lock_irq(&phba->hbalock);
516 	/* Initialize ERATT handling flag */
517 	phba->hba_flag &= ~HBA_ERATT_HANDLED;
518 
519 	/* Enable appropriate host interrupts */
520 	if (lpfc_readl(phba->HCregaddr, &status)) {
521 		spin_unlock_irq(&phba->hbalock);
522 		return -EIO;
523 	}
524 	status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
525 	if (psli->num_rings > 0)
526 		status |= HC_R0INT_ENA;
527 	if (psli->num_rings > 1)
528 		status |= HC_R1INT_ENA;
529 	if (psli->num_rings > 2)
530 		status |= HC_R2INT_ENA;
531 	if (psli->num_rings > 3)
532 		status |= HC_R3INT_ENA;
533 
534 	if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
535 	    (phba->cfg_poll & DISABLE_FCP_RING_INT))
536 		status &= ~(HC_R0INT_ENA);
537 
538 	writel(status, phba->HCregaddr);
539 	readl(phba->HCregaddr); /* flush */
540 	spin_unlock_irq(&phba->hbalock);
541 
542 	/* Set up ring-0 (ELS) timer */
543 	timeout = phba->fc_ratov * 2;
544 	mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
545 	/* Set up heart beat (HB) timer */
546 	mod_timer(&phba->hb_tmofunc, jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
547 	phba->hb_outstanding = 0;
548 	phba->last_completion_time = jiffies;
549 	/* Set up error attention (ERATT) polling timer */
550 	mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
551 
552 	if (phba->hba_flag & LINK_DISABLED) {
553 		lpfc_printf_log(phba,
554 			KERN_ERR, LOG_INIT,
555 			"2598 Adapter Link is disabled.\n");
556 		lpfc_down_link(phba, pmb);
557 		pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
558 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
559 		if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
560 			lpfc_printf_log(phba,
561 			KERN_ERR, LOG_INIT,
562 			"2599 Adapter failed to issue DOWN_LINK"
563 			" mbox command rc 0x%x\n", rc);
564 
565 			mempool_free(pmb, phba->mbox_mem_pool);
566 			return -EIO;
567 		}
568 	} else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
569 		mempool_free(pmb, phba->mbox_mem_pool);
570 		rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
571 		if (rc)
572 			return rc;
573 	}
574 	/* MBOX buffer will be freed in mbox compl */
575 	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
576 	if (!pmb) {
577 		phba->link_state = LPFC_HBA_ERROR;
578 		return -ENOMEM;
579 	}
580 
581 	lpfc_config_async(phba, pmb, LPFC_ELS_RING);
582 	pmb->mbox_cmpl = lpfc_config_async_cmpl;
583 	pmb->vport = phba->pport;
584 	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
585 
586 	if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
587 		lpfc_printf_log(phba,
588 				KERN_ERR,
589 				LOG_INIT,
590 				"0456 Adapter failed to issue "
591 				"ASYNCEVT_ENABLE mbox status x%x\n",
592 				rc);
593 		mempool_free(pmb, phba->mbox_mem_pool);
594 	}
595 
596 	/* Get Option rom version */
597 	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
598 	if (!pmb) {
599 		phba->link_state = LPFC_HBA_ERROR;
600 		return -ENOMEM;
601 	}
602 
603 	lpfc_dump_wakeup_param(phba, pmb);
604 	pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
605 	pmb->vport = phba->pport;
606 	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
607 
608 	if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
609 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
610 				"to get Option ROM version status x%x\n", rc);
611 		mempool_free(pmb, phba->mbox_mem_pool);
612 	}
613 
614 	return 0;
615 }
616 
617 /**
618  * lpfc_hba_init_link - Initialize the FC link
619  * @phba: pointer to lpfc hba data structure.
620  * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
621  *
622  * This routine will issue the INIT_LINK mailbox command call.
623  * It is available to other drivers through the lpfc_hba data
624  * structure for use as a delayed link up mechanism with the
625  * module parameter lpfc_suppress_link_up.
626  *
627  * Return code
628  *		0 - success
629  *		Any other value - error
630  **/
631 int
632 lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
633 {
634 	return lpfc_hba_init_link_fc_topology(phba, phba->cfg_topology, flag);
635 }
636 
637 /**
638  * lpfc_hba_init_link_fc_topology - Initialize FC link with desired topology
639  * @phba: pointer to lpfc hba data structure.
640  * @fc_topology: desired fc topology.
641  * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
642  *
643  * This routine will issue the INIT_LINK mailbox command call.
644  * It is available to other drivers through the lpfc_hba data
645  * structure for use as a delayed link up mechanism with the
646  * module parameter lpfc_suppress_link_up.
647  *
648  * Return code
649  *              0 - success
650  *              Any other value - error
651  **/
652 int
653 lpfc_hba_init_link_fc_topology(struct lpfc_hba *phba, uint32_t fc_topology,
654 			       uint32_t flag)
655 {
656 	struct lpfc_vport *vport = phba->pport;
657 	LPFC_MBOXQ_t *pmb;
658 	MAILBOX_t *mb;
659 	int rc;
660 
661 	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
662 	if (!pmb) {
663 		phba->link_state = LPFC_HBA_ERROR;
664 		return -ENOMEM;
665 	}
666 	mb = &pmb->u.mb;
667 	pmb->vport = vport;
668 
669 	if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_MAX) ||
670 	    ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G) &&
671 	     !(phba->lmt & LMT_1Gb)) ||
672 	    ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G) &&
673 	     !(phba->lmt & LMT_2Gb)) ||
674 	    ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G) &&
675 	     !(phba->lmt & LMT_4Gb)) ||
676 	    ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G) &&
677 	     !(phba->lmt & LMT_8Gb)) ||
678 	    ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G) &&
679 	     !(phba->lmt & LMT_10Gb)) ||
680 	    ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G) &&
681 	     !(phba->lmt & LMT_16Gb))) {
682 		/* Reset link speed to auto */
683 		lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
684 			"1302 Invalid speed for this board:%d "
685 			"Reset link speed to auto.\n",
686 			phba->cfg_link_speed);
687 			phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
688 	}
689 	lpfc_init_link(phba, pmb, fc_topology, phba->cfg_link_speed);
690 	pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
691 	if (phba->sli_rev < LPFC_SLI_REV4)
692 		lpfc_set_loopback_flag(phba);
693 	rc = lpfc_sli_issue_mbox(phba, pmb, flag);
694 	if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
695 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
696 			"0498 Adapter failed to init, mbxCmd x%x "
697 			"INIT_LINK, mbxStatus x%x\n",
698 			mb->mbxCommand, mb->mbxStatus);
699 		if (phba->sli_rev <= LPFC_SLI_REV3) {
700 			/* Clear all interrupt enable conditions */
701 			writel(0, phba->HCregaddr);
702 			readl(phba->HCregaddr); /* flush */
703 			/* Clear all pending interrupts */
704 			writel(0xffffffff, phba->HAregaddr);
705 			readl(phba->HAregaddr); /* flush */
706 		}
707 		phba->link_state = LPFC_HBA_ERROR;
708 		if (rc != MBX_BUSY || flag == MBX_POLL)
709 			mempool_free(pmb, phba->mbox_mem_pool);
710 		return -EIO;
711 	}
712 	phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
713 	if (flag == MBX_POLL)
714 		mempool_free(pmb, phba->mbox_mem_pool);
715 
716 	return 0;
717 }
718 
719 /**
720  * lpfc_hba_down_link - this routine downs the FC link
721  * @phba: pointer to lpfc hba data structure.
722  * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
723  *
724  * This routine will issue the DOWN_LINK mailbox command call.
725  * It is available to other drivers through the lpfc_hba data
726  * structure for use to stop the link.
727  *
728  * Return code
729  *		0 - success
730  *		Any other value - error
731  **/
732 int
733 lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
734 {
735 	LPFC_MBOXQ_t *pmb;
736 	int rc;
737 
738 	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
739 	if (!pmb) {
740 		phba->link_state = LPFC_HBA_ERROR;
741 		return -ENOMEM;
742 	}
743 
744 	lpfc_printf_log(phba,
745 		KERN_ERR, LOG_INIT,
746 		"0491 Adapter Link is disabled.\n");
747 	lpfc_down_link(phba, pmb);
748 	pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
749 	rc = lpfc_sli_issue_mbox(phba, pmb, flag);
750 	if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
751 		lpfc_printf_log(phba,
752 		KERN_ERR, LOG_INIT,
753 		"2522 Adapter failed to issue DOWN_LINK"
754 		" mbox command rc 0x%x\n", rc);
755 
756 		mempool_free(pmb, phba->mbox_mem_pool);
757 		return -EIO;
758 	}
759 	if (flag == MBX_POLL)
760 		mempool_free(pmb, phba->mbox_mem_pool);
761 
762 	return 0;
763 }
764 
765 /**
766  * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
767  * @phba: pointer to lpfc HBA data structure.
768  *
769  * This routine will do LPFC uninitialization before the HBA is reset when
770  * bringing down the SLI Layer.
771  *
772  * Return codes
773  *   0 - success.
774  *   Any other value - error.
775  **/
776 int
777 lpfc_hba_down_prep(struct lpfc_hba *phba)
778 {
779 	struct lpfc_vport **vports;
780 	int i;
781 
782 	if (phba->sli_rev <= LPFC_SLI_REV3) {
783 		/* Disable interrupts */
784 		writel(0, phba->HCregaddr);
785 		readl(phba->HCregaddr); /* flush */
786 	}
787 
788 	if (phba->pport->load_flag & FC_UNLOADING)
789 		lpfc_cleanup_discovery_resources(phba->pport);
790 	else {
791 		vports = lpfc_create_vport_work_array(phba);
792 		if (vports != NULL)
793 			for (i = 0; i <= phba->max_vports &&
794 				vports[i] != NULL; i++)
795 				lpfc_cleanup_discovery_resources(vports[i]);
796 		lpfc_destroy_vport_work_array(phba, vports);
797 	}
798 	return 0;
799 }
800 
801 /**
802  * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
803  * @phba: pointer to lpfc HBA data structure.
804  *
805  * This routine will do uninitialization after the HBA is reset when bring
806  * down the SLI Layer.
807  *
808  * Return codes
809  *   0 - success.
810  *   Any other value - error.
811  **/
812 static int
813 lpfc_hba_down_post_s3(struct lpfc_hba *phba)
814 {
815 	struct lpfc_sli *psli = &phba->sli;
816 	struct lpfc_sli_ring *pring;
817 	struct lpfc_dmabuf *mp, *next_mp;
818 	LIST_HEAD(completions);
819 	int i;
820 
821 	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
822 		lpfc_sli_hbqbuf_free_all(phba);
823 	else {
824 		/* Cleanup preposted buffers on the ELS ring */
825 		pring = &psli->ring[LPFC_ELS_RING];
826 		list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
827 			list_del(&mp->list);
828 			pring->postbufq_cnt--;
829 			lpfc_mbuf_free(phba, mp->virt, mp->phys);
830 			kfree(mp);
831 		}
832 	}
833 
834 	spin_lock_irq(&phba->hbalock);
835 	for (i = 0; i < psli->num_rings; i++) {
836 		pring = &psli->ring[i];
837 
838 		/* At this point in time the HBA is either reset or DOA. Either
839 		 * way, nothing should be on txcmplq as it will NEVER complete.
840 		 */
841 		list_splice_init(&pring->txcmplq, &completions);
842 		spin_unlock_irq(&phba->hbalock);
843 
844 		/* Cancel all the IOCBs from the completions list */
845 		lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
846 				      IOERR_SLI_ABORTED);
847 
848 		lpfc_sli_abort_iocb_ring(phba, pring);
849 		spin_lock_irq(&phba->hbalock);
850 	}
851 	spin_unlock_irq(&phba->hbalock);
852 
853 	return 0;
854 }
855 
856 /**
857  * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
858  * @phba: pointer to lpfc HBA data structure.
859  *
860  * This routine will do uninitialization after the HBA is reset when bring
861  * down the SLI Layer.
862  *
863  * Return codes
864  *   0 - success.
865  *   Any other value - error.
866  **/
867 static int
868 lpfc_hba_down_post_s4(struct lpfc_hba *phba)
869 {
870 	struct lpfc_scsi_buf *psb, *psb_next;
871 	LIST_HEAD(aborts);
872 	int ret;
873 	unsigned long iflag = 0;
874 	struct lpfc_sglq *sglq_entry = NULL;
875 
876 	ret = lpfc_hba_down_post_s3(phba);
877 	if (ret)
878 		return ret;
879 	/* At this point in time the HBA is either reset or DOA. Either
880 	 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
881 	 * on the lpfc_sgl_list so that it can either be freed if the
882 	 * driver is unloading or reposted if the driver is restarting
883 	 * the port.
884 	 */
885 	spin_lock_irq(&phba->hbalock);  /* required for lpfc_sgl_list and */
886 					/* scsl_buf_list */
887 	/* abts_sgl_list_lock required because worker thread uses this
888 	 * list.
889 	 */
890 	spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
891 	list_for_each_entry(sglq_entry,
892 		&phba->sli4_hba.lpfc_abts_els_sgl_list, list)
893 		sglq_entry->state = SGL_FREED;
894 
895 	list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
896 			&phba->sli4_hba.lpfc_sgl_list);
897 	spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
898 	/* abts_scsi_buf_list_lock required because worker thread uses this
899 	 * list.
900 	 */
901 	spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
902 	list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
903 			&aborts);
904 	spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
905 	spin_unlock_irq(&phba->hbalock);
906 
907 	list_for_each_entry_safe(psb, psb_next, &aborts, list) {
908 		psb->pCmd = NULL;
909 		psb->status = IOSTAT_SUCCESS;
910 	}
911 	spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
912 	list_splice(&aborts, &phba->lpfc_scsi_buf_list);
913 	spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
914 	return 0;
915 }
916 
917 /**
918  * lpfc_hba_down_post - Wrapper func for hba down post routine
919  * @phba: pointer to lpfc HBA data structure.
920  *
921  * This routine wraps the actual SLI3 or SLI4 routine for performing
922  * uninitialization after the HBA is reset when bring down the SLI Layer.
923  *
924  * Return codes
925  *   0 - success.
926  *   Any other value - error.
927  **/
928 int
929 lpfc_hba_down_post(struct lpfc_hba *phba)
930 {
931 	return (*phba->lpfc_hba_down_post)(phba);
932 }
933 
934 /**
935  * lpfc_hb_timeout - The HBA-timer timeout handler
936  * @ptr: unsigned long holds the pointer to lpfc hba data structure.
937  *
938  * This is the HBA-timer timeout handler registered to the lpfc driver. When
939  * this timer fires, a HBA timeout event shall be posted to the lpfc driver
940  * work-port-events bitmap and the worker thread is notified. This timeout
941  * event will be used by the worker thread to invoke the actual timeout
942  * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
943  * be performed in the timeout handler and the HBA timeout event bit shall
944  * be cleared by the worker thread after it has taken the event bitmap out.
945  **/
946 static void
947 lpfc_hb_timeout(unsigned long ptr)
948 {
949 	struct lpfc_hba *phba;
950 	uint32_t tmo_posted;
951 	unsigned long iflag;
952 
953 	phba = (struct lpfc_hba *)ptr;
954 
955 	/* Check for heart beat timeout conditions */
956 	spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
957 	tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
958 	if (!tmo_posted)
959 		phba->pport->work_port_events |= WORKER_HB_TMO;
960 	spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
961 
962 	/* Tell the worker thread there is work to do */
963 	if (!tmo_posted)
964 		lpfc_worker_wake_up(phba);
965 	return;
966 }
967 
968 /**
969  * lpfc_rrq_timeout - The RRQ-timer timeout handler
970  * @ptr: unsigned long holds the pointer to lpfc hba data structure.
971  *
972  * This is the RRQ-timer timeout handler registered to the lpfc driver. When
973  * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
974  * work-port-events bitmap and the worker thread is notified. This timeout
975  * event will be used by the worker thread to invoke the actual timeout
976  * handler routine, lpfc_rrq_handler. Any periodical operations will
977  * be performed in the timeout handler and the RRQ timeout event bit shall
978  * be cleared by the worker thread after it has taken the event bitmap out.
979  **/
980 static void
981 lpfc_rrq_timeout(unsigned long ptr)
982 {
983 	struct lpfc_hba *phba;
984 	unsigned long iflag;
985 
986 	phba = (struct lpfc_hba *)ptr;
987 	spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
988 	phba->hba_flag |= HBA_RRQ_ACTIVE;
989 	spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
990 	lpfc_worker_wake_up(phba);
991 }
992 
993 /**
994  * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
995  * @phba: pointer to lpfc hba data structure.
996  * @pmboxq: pointer to the driver internal queue element for mailbox command.
997  *
998  * This is the callback function to the lpfc heart-beat mailbox command.
999  * If configured, the lpfc driver issues the heart-beat mailbox command to
1000  * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
1001  * heart-beat mailbox command is issued, the driver shall set up heart-beat
1002  * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
1003  * heart-beat outstanding state. Once the mailbox command comes back and
1004  * no error conditions detected, the heart-beat mailbox command timer is
1005  * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1006  * state is cleared for the next heart-beat. If the timer expired with the
1007  * heart-beat outstanding state set, the driver will put the HBA offline.
1008  **/
1009 static void
1010 lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1011 {
1012 	unsigned long drvr_flag;
1013 
1014 	spin_lock_irqsave(&phba->hbalock, drvr_flag);
1015 	phba->hb_outstanding = 0;
1016 	spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1017 
1018 	/* Check and reset heart-beat timer is necessary */
1019 	mempool_free(pmboxq, phba->mbox_mem_pool);
1020 	if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1021 		!(phba->link_state == LPFC_HBA_ERROR) &&
1022 		!(phba->pport->load_flag & FC_UNLOADING))
1023 		mod_timer(&phba->hb_tmofunc,
1024 			jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
1025 	return;
1026 }
1027 
1028 /**
1029  * lpfc_hb_timeout_handler - The HBA-timer timeout handler
1030  * @phba: pointer to lpfc hba data structure.
1031  *
1032  * This is the actual HBA-timer timeout handler to be invoked by the worker
1033  * thread whenever the HBA timer fired and HBA-timeout event posted. This
1034  * handler performs any periodic operations needed for the device. If such
1035  * periodic event has already been attended to either in the interrupt handler
1036  * or by processing slow-ring or fast-ring events within the HBA-timer
1037  * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1038  * the timer for the next timeout period. If lpfc heart-beat mailbox command
1039  * is configured and there is no heart-beat mailbox command outstanding, a
1040  * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1041  * has been a heart-beat mailbox command outstanding, the HBA shall be put
1042  * to offline.
1043  **/
1044 void
1045 lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1046 {
1047 	struct lpfc_vport **vports;
1048 	LPFC_MBOXQ_t *pmboxq;
1049 	struct lpfc_dmabuf *buf_ptr;
1050 	int retval, i;
1051 	struct lpfc_sli *psli = &phba->sli;
1052 	LIST_HEAD(completions);
1053 
1054 	vports = lpfc_create_vport_work_array(phba);
1055 	if (vports != NULL)
1056 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
1057 			lpfc_rcv_seq_check_edtov(vports[i]);
1058 	lpfc_destroy_vport_work_array(phba, vports);
1059 
1060 	if ((phba->link_state == LPFC_HBA_ERROR) ||
1061 		(phba->pport->load_flag & FC_UNLOADING) ||
1062 		(phba->pport->fc_flag & FC_OFFLINE_MODE))
1063 		return;
1064 
1065 	spin_lock_irq(&phba->pport->work_port_lock);
1066 
1067 	if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
1068 		jiffies)) {
1069 		spin_unlock_irq(&phba->pport->work_port_lock);
1070 		if (!phba->hb_outstanding)
1071 			mod_timer(&phba->hb_tmofunc,
1072 				jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
1073 		else
1074 			mod_timer(&phba->hb_tmofunc,
1075 				jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
1076 		return;
1077 	}
1078 	spin_unlock_irq(&phba->pport->work_port_lock);
1079 
1080 	if (phba->elsbuf_cnt &&
1081 		(phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1082 		spin_lock_irq(&phba->hbalock);
1083 		list_splice_init(&phba->elsbuf, &completions);
1084 		phba->elsbuf_cnt = 0;
1085 		phba->elsbuf_prev_cnt = 0;
1086 		spin_unlock_irq(&phba->hbalock);
1087 
1088 		while (!list_empty(&completions)) {
1089 			list_remove_head(&completions, buf_ptr,
1090 				struct lpfc_dmabuf, list);
1091 			lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1092 			kfree(buf_ptr);
1093 		}
1094 	}
1095 	phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1096 
1097 	/* If there is no heart beat outstanding, issue a heartbeat command */
1098 	if (phba->cfg_enable_hba_heartbeat) {
1099 		if (!phba->hb_outstanding) {
1100 			if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1101 				(list_empty(&psli->mboxq))) {
1102 				pmboxq = mempool_alloc(phba->mbox_mem_pool,
1103 							GFP_KERNEL);
1104 				if (!pmboxq) {
1105 					mod_timer(&phba->hb_tmofunc,
1106 						 jiffies +
1107 						 HZ * LPFC_HB_MBOX_INTERVAL);
1108 					return;
1109 				}
1110 
1111 				lpfc_heart_beat(phba, pmboxq);
1112 				pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1113 				pmboxq->vport = phba->pport;
1114 				retval = lpfc_sli_issue_mbox(phba, pmboxq,
1115 						MBX_NOWAIT);
1116 
1117 				if (retval != MBX_BUSY &&
1118 					retval != MBX_SUCCESS) {
1119 					mempool_free(pmboxq,
1120 							phba->mbox_mem_pool);
1121 					mod_timer(&phba->hb_tmofunc,
1122 						jiffies +
1123 						HZ * LPFC_HB_MBOX_INTERVAL);
1124 					return;
1125 				}
1126 				phba->skipped_hb = 0;
1127 				phba->hb_outstanding = 1;
1128 			} else if (time_before_eq(phba->last_completion_time,
1129 					phba->skipped_hb)) {
1130 				lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1131 					"2857 Last completion time not "
1132 					" updated in %d ms\n",
1133 					jiffies_to_msecs(jiffies
1134 						 - phba->last_completion_time));
1135 			} else
1136 				phba->skipped_hb = jiffies;
1137 
1138 			mod_timer(&phba->hb_tmofunc,
1139 				  jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
1140 			return;
1141 		} else {
1142 			/*
1143 			* If heart beat timeout called with hb_outstanding set
1144 			* we need to give the hb mailbox cmd a chance to
1145 			* complete or TMO.
1146 			*/
1147 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1148 					"0459 Adapter heartbeat still out"
1149 					"standing:last compl time was %d ms.\n",
1150 					jiffies_to_msecs(jiffies
1151 						 - phba->last_completion_time));
1152 			mod_timer(&phba->hb_tmofunc,
1153 				  jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
1154 		}
1155 	}
1156 }
1157 
1158 /**
1159  * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
1160  * @phba: pointer to lpfc hba data structure.
1161  *
1162  * This routine is called to bring the HBA offline when HBA hardware error
1163  * other than Port Error 6 has been detected.
1164  **/
1165 static void
1166 lpfc_offline_eratt(struct lpfc_hba *phba)
1167 {
1168 	struct lpfc_sli   *psli = &phba->sli;
1169 
1170 	spin_lock_irq(&phba->hbalock);
1171 	psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1172 	spin_unlock_irq(&phba->hbalock);
1173 	lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
1174 
1175 	lpfc_offline(phba);
1176 	lpfc_reset_barrier(phba);
1177 	spin_lock_irq(&phba->hbalock);
1178 	lpfc_sli_brdreset(phba);
1179 	spin_unlock_irq(&phba->hbalock);
1180 	lpfc_hba_down_post(phba);
1181 	lpfc_sli_brdready(phba, HS_MBRDY);
1182 	lpfc_unblock_mgmt_io(phba);
1183 	phba->link_state = LPFC_HBA_ERROR;
1184 	return;
1185 }
1186 
1187 /**
1188  * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1189  * @phba: pointer to lpfc hba data structure.
1190  *
1191  * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1192  * other than Port Error 6 has been detected.
1193  **/
1194 static void
1195 lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1196 {
1197 	lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
1198 	lpfc_offline(phba);
1199 	lpfc_sli4_brdreset(phba);
1200 	lpfc_hba_down_post(phba);
1201 	lpfc_sli4_post_status_check(phba);
1202 	lpfc_unblock_mgmt_io(phba);
1203 	phba->link_state = LPFC_HBA_ERROR;
1204 }
1205 
1206 /**
1207  * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1208  * @phba: pointer to lpfc hba data structure.
1209  *
1210  * This routine is invoked to handle the deferred HBA hardware error
1211  * conditions. This type of error is indicated by HBA by setting ER1
1212  * and another ER bit in the host status register. The driver will
1213  * wait until the ER1 bit clears before handling the error condition.
1214  **/
1215 static void
1216 lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1217 {
1218 	uint32_t old_host_status = phba->work_hs;
1219 	struct lpfc_sli_ring  *pring;
1220 	struct lpfc_sli *psli = &phba->sli;
1221 
1222 	/* If the pci channel is offline, ignore possible errors,
1223 	 * since we cannot communicate with the pci card anyway.
1224 	 */
1225 	if (pci_channel_offline(phba->pcidev)) {
1226 		spin_lock_irq(&phba->hbalock);
1227 		phba->hba_flag &= ~DEFER_ERATT;
1228 		spin_unlock_irq(&phba->hbalock);
1229 		return;
1230 	}
1231 
1232 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1233 		"0479 Deferred Adapter Hardware Error "
1234 		"Data: x%x x%x x%x\n",
1235 		phba->work_hs,
1236 		phba->work_status[0], phba->work_status[1]);
1237 
1238 	spin_lock_irq(&phba->hbalock);
1239 	psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1240 	spin_unlock_irq(&phba->hbalock);
1241 
1242 
1243 	/*
1244 	 * Firmware stops when it triggred erratt. That could cause the I/Os
1245 	 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1246 	 * SCSI layer retry it after re-establishing link.
1247 	 */
1248 	pring = &psli->ring[psli->fcp_ring];
1249 	lpfc_sli_abort_iocb_ring(phba, pring);
1250 
1251 	/*
1252 	 * There was a firmware error. Take the hba offline and then
1253 	 * attempt to restart it.
1254 	 */
1255 	lpfc_offline_prep(phba, LPFC_MBX_WAIT);
1256 	lpfc_offline(phba);
1257 
1258 	/* Wait for the ER1 bit to clear.*/
1259 	while (phba->work_hs & HS_FFER1) {
1260 		msleep(100);
1261 		if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1262 			phba->work_hs = UNPLUG_ERR ;
1263 			break;
1264 		}
1265 		/* If driver is unloading let the worker thread continue */
1266 		if (phba->pport->load_flag & FC_UNLOADING) {
1267 			phba->work_hs = 0;
1268 			break;
1269 		}
1270 	}
1271 
1272 	/*
1273 	 * This is to ptrotect against a race condition in which
1274 	 * first write to the host attention register clear the
1275 	 * host status register.
1276 	 */
1277 	if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1278 		phba->work_hs = old_host_status & ~HS_FFER1;
1279 
1280 	spin_lock_irq(&phba->hbalock);
1281 	phba->hba_flag &= ~DEFER_ERATT;
1282 	spin_unlock_irq(&phba->hbalock);
1283 	phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1284 	phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1285 }
1286 
1287 static void
1288 lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1289 {
1290 	struct lpfc_board_event_header board_event;
1291 	struct Scsi_Host *shost;
1292 
1293 	board_event.event_type = FC_REG_BOARD_EVENT;
1294 	board_event.subcategory = LPFC_EVENT_PORTINTERR;
1295 	shost = lpfc_shost_from_vport(phba->pport);
1296 	fc_host_post_vendor_event(shost, fc_get_event_number(),
1297 				  sizeof(board_event),
1298 				  (char *) &board_event,
1299 				  LPFC_NL_VENDOR_ID);
1300 }
1301 
1302 /**
1303  * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
1304  * @phba: pointer to lpfc hba data structure.
1305  *
1306  * This routine is invoked to handle the following HBA hardware error
1307  * conditions:
1308  * 1 - HBA error attention interrupt
1309  * 2 - DMA ring index out of range
1310  * 3 - Mailbox command came back as unknown
1311  **/
1312 static void
1313 lpfc_handle_eratt_s3(struct lpfc_hba *phba)
1314 {
1315 	struct lpfc_vport *vport = phba->pport;
1316 	struct lpfc_sli   *psli = &phba->sli;
1317 	struct lpfc_sli_ring  *pring;
1318 	uint32_t event_data;
1319 	unsigned long temperature;
1320 	struct temp_event temp_event_data;
1321 	struct Scsi_Host  *shost;
1322 
1323 	/* If the pci channel is offline, ignore possible errors,
1324 	 * since we cannot communicate with the pci card anyway.
1325 	 */
1326 	if (pci_channel_offline(phba->pcidev)) {
1327 		spin_lock_irq(&phba->hbalock);
1328 		phba->hba_flag &= ~DEFER_ERATT;
1329 		spin_unlock_irq(&phba->hbalock);
1330 		return;
1331 	}
1332 
1333 	/* If resets are disabled then leave the HBA alone and return */
1334 	if (!phba->cfg_enable_hba_reset)
1335 		return;
1336 
1337 	/* Send an internal error event to mgmt application */
1338 	lpfc_board_errevt_to_mgmt(phba);
1339 
1340 	if (phba->hba_flag & DEFER_ERATT)
1341 		lpfc_handle_deferred_eratt(phba);
1342 
1343 	if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1344 		if (phba->work_hs & HS_FFER6)
1345 			/* Re-establishing Link */
1346 			lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1347 					"1301 Re-establishing Link "
1348 					"Data: x%x x%x x%x\n",
1349 					phba->work_hs, phba->work_status[0],
1350 					phba->work_status[1]);
1351 		if (phba->work_hs & HS_FFER8)
1352 			/* Device Zeroization */
1353 			lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1354 					"2861 Host Authentication device "
1355 					"zeroization Data:x%x x%x x%x\n",
1356 					phba->work_hs, phba->work_status[0],
1357 					phba->work_status[1]);
1358 
1359 		spin_lock_irq(&phba->hbalock);
1360 		psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1361 		spin_unlock_irq(&phba->hbalock);
1362 
1363 		/*
1364 		* Firmware stops when it triggled erratt with HS_FFER6.
1365 		* That could cause the I/Os dropped by the firmware.
1366 		* Error iocb (I/O) on txcmplq and let the SCSI layer
1367 		* retry it after re-establishing link.
1368 		*/
1369 		pring = &psli->ring[psli->fcp_ring];
1370 		lpfc_sli_abort_iocb_ring(phba, pring);
1371 
1372 		/*
1373 		 * There was a firmware error.  Take the hba offline and then
1374 		 * attempt to restart it.
1375 		 */
1376 		lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
1377 		lpfc_offline(phba);
1378 		lpfc_sli_brdrestart(phba);
1379 		if (lpfc_online(phba) == 0) {	/* Initialize the HBA */
1380 			lpfc_unblock_mgmt_io(phba);
1381 			return;
1382 		}
1383 		lpfc_unblock_mgmt_io(phba);
1384 	} else if (phba->work_hs & HS_CRIT_TEMP) {
1385 		temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1386 		temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1387 		temp_event_data.event_code = LPFC_CRIT_TEMP;
1388 		temp_event_data.data = (uint32_t)temperature;
1389 
1390 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1391 				"0406 Adapter maximum temperature exceeded "
1392 				"(%ld), taking this port offline "
1393 				"Data: x%x x%x x%x\n",
1394 				temperature, phba->work_hs,
1395 				phba->work_status[0], phba->work_status[1]);
1396 
1397 		shost = lpfc_shost_from_vport(phba->pport);
1398 		fc_host_post_vendor_event(shost, fc_get_event_number(),
1399 					  sizeof(temp_event_data),
1400 					  (char *) &temp_event_data,
1401 					  SCSI_NL_VID_TYPE_PCI
1402 					  | PCI_VENDOR_ID_EMULEX);
1403 
1404 		spin_lock_irq(&phba->hbalock);
1405 		phba->over_temp_state = HBA_OVER_TEMP;
1406 		spin_unlock_irq(&phba->hbalock);
1407 		lpfc_offline_eratt(phba);
1408 
1409 	} else {
1410 		/* The if clause above forces this code path when the status
1411 		 * failure is a value other than FFER6. Do not call the offline
1412 		 * twice. This is the adapter hardware error path.
1413 		 */
1414 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1415 				"0457 Adapter Hardware Error "
1416 				"Data: x%x x%x x%x\n",
1417 				phba->work_hs,
1418 				phba->work_status[0], phba->work_status[1]);
1419 
1420 		event_data = FC_REG_DUMP_EVENT;
1421 		shost = lpfc_shost_from_vport(vport);
1422 		fc_host_post_vendor_event(shost, fc_get_event_number(),
1423 				sizeof(event_data), (char *) &event_data,
1424 				SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1425 
1426 		lpfc_offline_eratt(phba);
1427 	}
1428 	return;
1429 }
1430 
1431 /**
1432  * lpfc_sli4_port_sta_fn_reset - The SLI4 function reset due to port status reg
1433  * @phba: pointer to lpfc hba data structure.
1434  * @mbx_action: flag for mailbox shutdown action.
1435  *
1436  * This routine is invoked to perform an SLI4 port PCI function reset in
1437  * response to port status register polling attention. It waits for port
1438  * status register (ERR, RDY, RN) bits before proceeding with function reset.
1439  * During this process, interrupt vectors are freed and later requested
1440  * for handling possible port resource change.
1441  **/
1442 static int
1443 lpfc_sli4_port_sta_fn_reset(struct lpfc_hba *phba, int mbx_action)
1444 {
1445 	int rc;
1446 	uint32_t intr_mode;
1447 
1448 	/*
1449 	 * On error status condition, driver need to wait for port
1450 	 * ready before performing reset.
1451 	 */
1452 	rc = lpfc_sli4_pdev_status_reg_wait(phba);
1453 	if (!rc) {
1454 		/* need reset: attempt for port recovery */
1455 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1456 				"2887 Reset Needed: Attempting Port "
1457 				"Recovery...\n");
1458 		lpfc_offline_prep(phba, mbx_action);
1459 		lpfc_offline(phba);
1460 		/* release interrupt for possible resource change */
1461 		lpfc_sli4_disable_intr(phba);
1462 		lpfc_sli_brdrestart(phba);
1463 		/* request and enable interrupt */
1464 		intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
1465 		if (intr_mode == LPFC_INTR_ERROR) {
1466 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1467 					"3175 Failed to enable interrupt\n");
1468 			return -EIO;
1469 		} else {
1470 			phba->intr_mode = intr_mode;
1471 		}
1472 		rc = lpfc_online(phba);
1473 		if (rc == 0)
1474 			lpfc_unblock_mgmt_io(phba);
1475 	}
1476 	return rc;
1477 }
1478 
1479 /**
1480  * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1481  * @phba: pointer to lpfc hba data structure.
1482  *
1483  * This routine is invoked to handle the SLI4 HBA hardware error attention
1484  * conditions.
1485  **/
1486 static void
1487 lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1488 {
1489 	struct lpfc_vport *vport = phba->pport;
1490 	uint32_t event_data;
1491 	struct Scsi_Host *shost;
1492 	uint32_t if_type;
1493 	struct lpfc_register portstat_reg = {0};
1494 	uint32_t reg_err1, reg_err2;
1495 	uint32_t uerrlo_reg, uemasklo_reg;
1496 	uint32_t pci_rd_rc1, pci_rd_rc2;
1497 	int rc;
1498 
1499 	/* If the pci channel is offline, ignore possible errors, since
1500 	 * we cannot communicate with the pci card anyway.
1501 	 */
1502 	if (pci_channel_offline(phba->pcidev))
1503 		return;
1504 	/* If resets are disabled then leave the HBA alone and return */
1505 	if (!phba->cfg_enable_hba_reset)
1506 		return;
1507 
1508 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1509 	switch (if_type) {
1510 	case LPFC_SLI_INTF_IF_TYPE_0:
1511 		pci_rd_rc1 = lpfc_readl(
1512 				phba->sli4_hba.u.if_type0.UERRLOregaddr,
1513 				&uerrlo_reg);
1514 		pci_rd_rc2 = lpfc_readl(
1515 				phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
1516 				&uemasklo_reg);
1517 		/* consider PCI bus read error as pci_channel_offline */
1518 		if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
1519 			return;
1520 		lpfc_sli4_offline_eratt(phba);
1521 		break;
1522 	case LPFC_SLI_INTF_IF_TYPE_2:
1523 		pci_rd_rc1 = lpfc_readl(
1524 				phba->sli4_hba.u.if_type2.STATUSregaddr,
1525 				&portstat_reg.word0);
1526 		/* consider PCI bus read error as pci_channel_offline */
1527 		if (pci_rd_rc1 == -EIO) {
1528 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1529 				"3151 PCI bus read access failure: x%x\n",
1530 				readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
1531 			return;
1532 		}
1533 		reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
1534 		reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
1535 		if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
1536 			/* TODO: Register for Overtemp async events. */
1537 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1538 				"2889 Port Overtemperature event, "
1539 				"taking port offline\n");
1540 			spin_lock_irq(&phba->hbalock);
1541 			phba->over_temp_state = HBA_OVER_TEMP;
1542 			spin_unlock_irq(&phba->hbalock);
1543 			lpfc_sli4_offline_eratt(phba);
1544 			break;
1545 		}
1546 		if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1547 		    reg_err2 == SLIPORT_ERR2_REG_FW_RESTART)
1548 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1549 					"3143 Port Down: Firmware Restarted\n");
1550 		else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1551 			 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1552 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1553 					"3144 Port Down: Debug Dump\n");
1554 		else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1555 			 reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
1556 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1557 					"3145 Port Down: Provisioning\n");
1558 
1559 		/* Check port status register for function reset */
1560 		rc = lpfc_sli4_port_sta_fn_reset(phba, LPFC_MBX_NO_WAIT);
1561 		if (rc == 0) {
1562 			/* don't report event on forced debug dump */
1563 			if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1564 			    reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1565 				return;
1566 			else
1567 				break;
1568 		}
1569 		/* fall through for not able to recover */
1570 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1571 				"3152 Unrecoverable error, bring the port "
1572 				"offline\n");
1573 		lpfc_sli4_offline_eratt(phba);
1574 		break;
1575 	case LPFC_SLI_INTF_IF_TYPE_1:
1576 	default:
1577 		break;
1578 	}
1579 	lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1580 			"3123 Report dump event to upper layer\n");
1581 	/* Send an internal error event to mgmt application */
1582 	lpfc_board_errevt_to_mgmt(phba);
1583 
1584 	event_data = FC_REG_DUMP_EVENT;
1585 	shost = lpfc_shost_from_vport(vport);
1586 	fc_host_post_vendor_event(shost, fc_get_event_number(),
1587 				  sizeof(event_data), (char *) &event_data,
1588 				  SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1589 }
1590 
1591 /**
1592  * lpfc_handle_eratt - Wrapper func for handling hba error attention
1593  * @phba: pointer to lpfc HBA data structure.
1594  *
1595  * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1596  * routine from the API jump table function pointer from the lpfc_hba struct.
1597  *
1598  * Return codes
1599  *   0 - success.
1600  *   Any other value - error.
1601  **/
1602 void
1603 lpfc_handle_eratt(struct lpfc_hba *phba)
1604 {
1605 	(*phba->lpfc_handle_eratt)(phba);
1606 }
1607 
1608 /**
1609  * lpfc_handle_latt - The HBA link event handler
1610  * @phba: pointer to lpfc hba data structure.
1611  *
1612  * This routine is invoked from the worker thread to handle a HBA host
1613  * attention link event.
1614  **/
1615 void
1616 lpfc_handle_latt(struct lpfc_hba *phba)
1617 {
1618 	struct lpfc_vport *vport = phba->pport;
1619 	struct lpfc_sli   *psli = &phba->sli;
1620 	LPFC_MBOXQ_t *pmb;
1621 	volatile uint32_t control;
1622 	struct lpfc_dmabuf *mp;
1623 	int rc = 0;
1624 
1625 	pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1626 	if (!pmb) {
1627 		rc = 1;
1628 		goto lpfc_handle_latt_err_exit;
1629 	}
1630 
1631 	mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1632 	if (!mp) {
1633 		rc = 2;
1634 		goto lpfc_handle_latt_free_pmb;
1635 	}
1636 
1637 	mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
1638 	if (!mp->virt) {
1639 		rc = 3;
1640 		goto lpfc_handle_latt_free_mp;
1641 	}
1642 
1643 	/* Cleanup any outstanding ELS commands */
1644 	lpfc_els_flush_all_cmd(phba);
1645 
1646 	psli->slistat.link_event++;
1647 	lpfc_read_topology(phba, pmb, mp);
1648 	pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
1649 	pmb->vport = vport;
1650 	/* Block ELS IOCBs until we have processed this mbox command */
1651 	phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
1652 	rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
1653 	if (rc == MBX_NOT_FINISHED) {
1654 		rc = 4;
1655 		goto lpfc_handle_latt_free_mbuf;
1656 	}
1657 
1658 	/* Clear Link Attention in HA REG */
1659 	spin_lock_irq(&phba->hbalock);
1660 	writel(HA_LATT, phba->HAregaddr);
1661 	readl(phba->HAregaddr); /* flush */
1662 	spin_unlock_irq(&phba->hbalock);
1663 
1664 	return;
1665 
1666 lpfc_handle_latt_free_mbuf:
1667 	phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
1668 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
1669 lpfc_handle_latt_free_mp:
1670 	kfree(mp);
1671 lpfc_handle_latt_free_pmb:
1672 	mempool_free(pmb, phba->mbox_mem_pool);
1673 lpfc_handle_latt_err_exit:
1674 	/* Enable Link attention interrupts */
1675 	spin_lock_irq(&phba->hbalock);
1676 	psli->sli_flag |= LPFC_PROCESS_LA;
1677 	control = readl(phba->HCregaddr);
1678 	control |= HC_LAINT_ENA;
1679 	writel(control, phba->HCregaddr);
1680 	readl(phba->HCregaddr); /* flush */
1681 
1682 	/* Clear Link Attention in HA REG */
1683 	writel(HA_LATT, phba->HAregaddr);
1684 	readl(phba->HAregaddr); /* flush */
1685 	spin_unlock_irq(&phba->hbalock);
1686 	lpfc_linkdown(phba);
1687 	phba->link_state = LPFC_HBA_ERROR;
1688 
1689 	lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1690 		     "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
1691 
1692 	return;
1693 }
1694 
1695 /**
1696  * lpfc_parse_vpd - Parse VPD (Vital Product Data)
1697  * @phba: pointer to lpfc hba data structure.
1698  * @vpd: pointer to the vital product data.
1699  * @len: length of the vital product data in bytes.
1700  *
1701  * This routine parses the Vital Product Data (VPD). The VPD is treated as
1702  * an array of characters. In this routine, the ModelName, ProgramType, and
1703  * ModelDesc, etc. fields of the phba data structure will be populated.
1704  *
1705  * Return codes
1706  *   0 - pointer to the VPD passed in is NULL
1707  *   1 - success
1708  **/
1709 int
1710 lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
1711 {
1712 	uint8_t lenlo, lenhi;
1713 	int Length;
1714 	int i, j;
1715 	int finished = 0;
1716 	int index = 0;
1717 
1718 	if (!vpd)
1719 		return 0;
1720 
1721 	/* Vital Product */
1722 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1723 			"0455 Vital Product Data: x%x x%x x%x x%x\n",
1724 			(uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1725 			(uint32_t) vpd[3]);
1726 	while (!finished && (index < (len - 4))) {
1727 		switch (vpd[index]) {
1728 		case 0x82:
1729 		case 0x91:
1730 			index += 1;
1731 			lenlo = vpd[index];
1732 			index += 1;
1733 			lenhi = vpd[index];
1734 			index += 1;
1735 			i = ((((unsigned short)lenhi) << 8) + lenlo);
1736 			index += i;
1737 			break;
1738 		case 0x90:
1739 			index += 1;
1740 			lenlo = vpd[index];
1741 			index += 1;
1742 			lenhi = vpd[index];
1743 			index += 1;
1744 			Length = ((((unsigned short)lenhi) << 8) + lenlo);
1745 			if (Length > len - index)
1746 				Length = len - index;
1747 			while (Length > 0) {
1748 			/* Look for Serial Number */
1749 			if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1750 				index += 2;
1751 				i = vpd[index];
1752 				index += 1;
1753 				j = 0;
1754 				Length -= (3+i);
1755 				while(i--) {
1756 					phba->SerialNumber[j++] = vpd[index++];
1757 					if (j == 31)
1758 						break;
1759 				}
1760 				phba->SerialNumber[j] = 0;
1761 				continue;
1762 			}
1763 			else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1764 				phba->vpd_flag |= VPD_MODEL_DESC;
1765 				index += 2;
1766 				i = vpd[index];
1767 				index += 1;
1768 				j = 0;
1769 				Length -= (3+i);
1770 				while(i--) {
1771 					phba->ModelDesc[j++] = vpd[index++];
1772 					if (j == 255)
1773 						break;
1774 				}
1775 				phba->ModelDesc[j] = 0;
1776 				continue;
1777 			}
1778 			else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1779 				phba->vpd_flag |= VPD_MODEL_NAME;
1780 				index += 2;
1781 				i = vpd[index];
1782 				index += 1;
1783 				j = 0;
1784 				Length -= (3+i);
1785 				while(i--) {
1786 					phba->ModelName[j++] = vpd[index++];
1787 					if (j == 79)
1788 						break;
1789 				}
1790 				phba->ModelName[j] = 0;
1791 				continue;
1792 			}
1793 			else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1794 				phba->vpd_flag |= VPD_PROGRAM_TYPE;
1795 				index += 2;
1796 				i = vpd[index];
1797 				index += 1;
1798 				j = 0;
1799 				Length -= (3+i);
1800 				while(i--) {
1801 					phba->ProgramType[j++] = vpd[index++];
1802 					if (j == 255)
1803 						break;
1804 				}
1805 				phba->ProgramType[j] = 0;
1806 				continue;
1807 			}
1808 			else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1809 				phba->vpd_flag |= VPD_PORT;
1810 				index += 2;
1811 				i = vpd[index];
1812 				index += 1;
1813 				j = 0;
1814 				Length -= (3+i);
1815 				while(i--) {
1816 					if ((phba->sli_rev == LPFC_SLI_REV4) &&
1817 					    (phba->sli4_hba.pport_name_sta ==
1818 					     LPFC_SLI4_PPNAME_GET)) {
1819 						j++;
1820 						index++;
1821 					} else
1822 						phba->Port[j++] = vpd[index++];
1823 					if (j == 19)
1824 						break;
1825 				}
1826 				if ((phba->sli_rev != LPFC_SLI_REV4) ||
1827 				    (phba->sli4_hba.pport_name_sta ==
1828 				     LPFC_SLI4_PPNAME_NON))
1829 					phba->Port[j] = 0;
1830 				continue;
1831 			}
1832 			else {
1833 				index += 2;
1834 				i = vpd[index];
1835 				index += 1;
1836 				index += i;
1837 				Length -= (3 + i);
1838 			}
1839 		}
1840 		finished = 0;
1841 		break;
1842 		case 0x78:
1843 			finished = 1;
1844 			break;
1845 		default:
1846 			index ++;
1847 			break;
1848 		}
1849 	}
1850 
1851 	return(1);
1852 }
1853 
1854 /**
1855  * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
1856  * @phba: pointer to lpfc hba data structure.
1857  * @mdp: pointer to the data structure to hold the derived model name.
1858  * @descp: pointer to the data structure to hold the derived description.
1859  *
1860  * This routine retrieves HBA's description based on its registered PCI device
1861  * ID. The @descp passed into this function points to an array of 256 chars. It
1862  * shall be returned with the model name, maximum speed, and the host bus type.
1863  * The @mdp passed into this function points to an array of 80 chars. When the
1864  * function returns, the @mdp will be filled with the model name.
1865  **/
1866 static void
1867 lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
1868 {
1869 	lpfc_vpd_t *vp;
1870 	uint16_t dev_id = phba->pcidev->device;
1871 	int max_speed;
1872 	int GE = 0;
1873 	int oneConnect = 0; /* default is not a oneConnect */
1874 	struct {
1875 		char *name;
1876 		char *bus;
1877 		char *function;
1878 	} m = {"<Unknown>", "", ""};
1879 
1880 	if (mdp && mdp[0] != '\0'
1881 		&& descp && descp[0] != '\0')
1882 		return;
1883 
1884 	if (phba->lmt & LMT_16Gb)
1885 		max_speed = 16;
1886 	else if (phba->lmt & LMT_10Gb)
1887 		max_speed = 10;
1888 	else if (phba->lmt & LMT_8Gb)
1889 		max_speed = 8;
1890 	else if (phba->lmt & LMT_4Gb)
1891 		max_speed = 4;
1892 	else if (phba->lmt & LMT_2Gb)
1893 		max_speed = 2;
1894 	else if (phba->lmt & LMT_1Gb)
1895 		max_speed = 1;
1896 	else
1897 		max_speed = 0;
1898 
1899 	vp = &phba->vpd;
1900 
1901 	switch (dev_id) {
1902 	case PCI_DEVICE_ID_FIREFLY:
1903 		m = (typeof(m)){"LP6000", "PCI", "Fibre Channel Adapter"};
1904 		break;
1905 	case PCI_DEVICE_ID_SUPERFLY:
1906 		if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
1907 			m = (typeof(m)){"LP7000", "PCI",
1908 					"Fibre Channel Adapter"};
1909 		else
1910 			m = (typeof(m)){"LP7000E", "PCI",
1911 					"Fibre Channel Adapter"};
1912 		break;
1913 	case PCI_DEVICE_ID_DRAGONFLY:
1914 		m = (typeof(m)){"LP8000", "PCI",
1915 				"Fibre Channel Adapter"};
1916 		break;
1917 	case PCI_DEVICE_ID_CENTAUR:
1918 		if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
1919 			m = (typeof(m)){"LP9002", "PCI",
1920 					"Fibre Channel Adapter"};
1921 		else
1922 			m = (typeof(m)){"LP9000", "PCI",
1923 					"Fibre Channel Adapter"};
1924 		break;
1925 	case PCI_DEVICE_ID_RFLY:
1926 		m = (typeof(m)){"LP952", "PCI",
1927 				"Fibre Channel Adapter"};
1928 		break;
1929 	case PCI_DEVICE_ID_PEGASUS:
1930 		m = (typeof(m)){"LP9802", "PCI-X",
1931 				"Fibre Channel Adapter"};
1932 		break;
1933 	case PCI_DEVICE_ID_THOR:
1934 		m = (typeof(m)){"LP10000", "PCI-X",
1935 				"Fibre Channel Adapter"};
1936 		break;
1937 	case PCI_DEVICE_ID_VIPER:
1938 		m = (typeof(m)){"LPX1000",  "PCI-X",
1939 				"Fibre Channel Adapter"};
1940 		break;
1941 	case PCI_DEVICE_ID_PFLY:
1942 		m = (typeof(m)){"LP982", "PCI-X",
1943 				"Fibre Channel Adapter"};
1944 		break;
1945 	case PCI_DEVICE_ID_TFLY:
1946 		m = (typeof(m)){"LP1050", "PCI-X",
1947 				"Fibre Channel Adapter"};
1948 		break;
1949 	case PCI_DEVICE_ID_HELIOS:
1950 		m = (typeof(m)){"LP11000", "PCI-X2",
1951 				"Fibre Channel Adapter"};
1952 		break;
1953 	case PCI_DEVICE_ID_HELIOS_SCSP:
1954 		m = (typeof(m)){"LP11000-SP", "PCI-X2",
1955 				"Fibre Channel Adapter"};
1956 		break;
1957 	case PCI_DEVICE_ID_HELIOS_DCSP:
1958 		m = (typeof(m)){"LP11002-SP",  "PCI-X2",
1959 				"Fibre Channel Adapter"};
1960 		break;
1961 	case PCI_DEVICE_ID_NEPTUNE:
1962 		m = (typeof(m)){"LPe1000", "PCIe", "Fibre Channel Adapter"};
1963 		break;
1964 	case PCI_DEVICE_ID_NEPTUNE_SCSP:
1965 		m = (typeof(m)){"LPe1000-SP", "PCIe", "Fibre Channel Adapter"};
1966 		break;
1967 	case PCI_DEVICE_ID_NEPTUNE_DCSP:
1968 		m = (typeof(m)){"LPe1002-SP", "PCIe", "Fibre Channel Adapter"};
1969 		break;
1970 	case PCI_DEVICE_ID_BMID:
1971 		m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
1972 		break;
1973 	case PCI_DEVICE_ID_BSMB:
1974 		m = (typeof(m)){"LP111", "PCI-X2", "Fibre Channel Adapter"};
1975 		break;
1976 	case PCI_DEVICE_ID_ZEPHYR:
1977 		m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
1978 		break;
1979 	case PCI_DEVICE_ID_ZEPHYR_SCSP:
1980 		m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
1981 		break;
1982 	case PCI_DEVICE_ID_ZEPHYR_DCSP:
1983 		m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
1984 		GE = 1;
1985 		break;
1986 	case PCI_DEVICE_ID_ZMID:
1987 		m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
1988 		break;
1989 	case PCI_DEVICE_ID_ZSMB:
1990 		m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
1991 		break;
1992 	case PCI_DEVICE_ID_LP101:
1993 		m = (typeof(m)){"LP101", "PCI-X", "Fibre Channel Adapter"};
1994 		break;
1995 	case PCI_DEVICE_ID_LP10000S:
1996 		m = (typeof(m)){"LP10000-S", "PCI", "Fibre Channel Adapter"};
1997 		break;
1998 	case PCI_DEVICE_ID_LP11000S:
1999 		m = (typeof(m)){"LP11000-S", "PCI-X2", "Fibre Channel Adapter"};
2000 		break;
2001 	case PCI_DEVICE_ID_LPE11000S:
2002 		m = (typeof(m)){"LPe11000-S", "PCIe", "Fibre Channel Adapter"};
2003 		break;
2004 	case PCI_DEVICE_ID_SAT:
2005 		m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
2006 		break;
2007 	case PCI_DEVICE_ID_SAT_MID:
2008 		m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
2009 		break;
2010 	case PCI_DEVICE_ID_SAT_SMB:
2011 		m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
2012 		break;
2013 	case PCI_DEVICE_ID_SAT_DCSP:
2014 		m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
2015 		break;
2016 	case PCI_DEVICE_ID_SAT_SCSP:
2017 		m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
2018 		break;
2019 	case PCI_DEVICE_ID_SAT_S:
2020 		m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
2021 		break;
2022 	case PCI_DEVICE_ID_HORNET:
2023 		m = (typeof(m)){"LP21000", "PCIe", "FCoE Adapter"};
2024 		GE = 1;
2025 		break;
2026 	case PCI_DEVICE_ID_PROTEUS_VF:
2027 		m = (typeof(m)){"LPev12000", "PCIe IOV",
2028 				"Fibre Channel Adapter"};
2029 		break;
2030 	case PCI_DEVICE_ID_PROTEUS_PF:
2031 		m = (typeof(m)){"LPev12000", "PCIe IOV",
2032 				"Fibre Channel Adapter"};
2033 		break;
2034 	case PCI_DEVICE_ID_PROTEUS_S:
2035 		m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
2036 				"Fibre Channel Adapter"};
2037 		break;
2038 	case PCI_DEVICE_ID_TIGERSHARK:
2039 		oneConnect = 1;
2040 		m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
2041 		break;
2042 	case PCI_DEVICE_ID_TOMCAT:
2043 		oneConnect = 1;
2044 		m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
2045 		break;
2046 	case PCI_DEVICE_ID_FALCON:
2047 		m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
2048 				"EmulexSecure Fibre"};
2049 		break;
2050 	case PCI_DEVICE_ID_BALIUS:
2051 		m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
2052 				"Fibre Channel Adapter"};
2053 		break;
2054 	case PCI_DEVICE_ID_LANCER_FC:
2055 	case PCI_DEVICE_ID_LANCER_FC_VF:
2056 		m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
2057 		break;
2058 	case PCI_DEVICE_ID_LANCER_FCOE:
2059 	case PCI_DEVICE_ID_LANCER_FCOE_VF:
2060 		oneConnect = 1;
2061 		m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
2062 		break;
2063 	case PCI_DEVICE_ID_SKYHAWK:
2064 	case PCI_DEVICE_ID_SKYHAWK_VF:
2065 		oneConnect = 1;
2066 		m = (typeof(m)){"OCe14000", "PCIe", "FCoE"};
2067 		break;
2068 	default:
2069 		m = (typeof(m)){"Unknown", "", ""};
2070 		break;
2071 	}
2072 
2073 	if (mdp && mdp[0] == '\0')
2074 		snprintf(mdp, 79,"%s", m.name);
2075 	/*
2076 	 * oneConnect hba requires special processing, they are all initiators
2077 	 * and we put the port number on the end
2078 	 */
2079 	if (descp && descp[0] == '\0') {
2080 		if (oneConnect)
2081 			snprintf(descp, 255,
2082 				"Emulex OneConnect %s, %s Initiator %s",
2083 				m.name, m.function,
2084 				phba->Port);
2085 		else if (max_speed == 0)
2086 			snprintf(descp, 255,
2087 				"Emulex %s %s %s ",
2088 				m.name, m.bus, m.function);
2089 		else
2090 			snprintf(descp, 255,
2091 				"Emulex %s %d%s %s %s",
2092 				m.name, max_speed, (GE) ? "GE" : "Gb",
2093 				m.bus, m.function);
2094 	}
2095 }
2096 
2097 /**
2098  * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
2099  * @phba: pointer to lpfc hba data structure.
2100  * @pring: pointer to a IOCB ring.
2101  * @cnt: the number of IOCBs to be posted to the IOCB ring.
2102  *
2103  * This routine posts a given number of IOCBs with the associated DMA buffer
2104  * descriptors specified by the cnt argument to the given IOCB ring.
2105  *
2106  * Return codes
2107  *   The number of IOCBs NOT able to be posted to the IOCB ring.
2108  **/
2109 int
2110 lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
2111 {
2112 	IOCB_t *icmd;
2113 	struct lpfc_iocbq *iocb;
2114 	struct lpfc_dmabuf *mp1, *mp2;
2115 
2116 	cnt += pring->missbufcnt;
2117 
2118 	/* While there are buffers to post */
2119 	while (cnt > 0) {
2120 		/* Allocate buffer for  command iocb */
2121 		iocb = lpfc_sli_get_iocbq(phba);
2122 		if (iocb == NULL) {
2123 			pring->missbufcnt = cnt;
2124 			return cnt;
2125 		}
2126 		icmd = &iocb->iocb;
2127 
2128 		/* 2 buffers can be posted per command */
2129 		/* Allocate buffer to post */
2130 		mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2131 		if (mp1)
2132 		    mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2133 		if (!mp1 || !mp1->virt) {
2134 			kfree(mp1);
2135 			lpfc_sli_release_iocbq(phba, iocb);
2136 			pring->missbufcnt = cnt;
2137 			return cnt;
2138 		}
2139 
2140 		INIT_LIST_HEAD(&mp1->list);
2141 		/* Allocate buffer to post */
2142 		if (cnt > 1) {
2143 			mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2144 			if (mp2)
2145 				mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2146 							    &mp2->phys);
2147 			if (!mp2 || !mp2->virt) {
2148 				kfree(mp2);
2149 				lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2150 				kfree(mp1);
2151 				lpfc_sli_release_iocbq(phba, iocb);
2152 				pring->missbufcnt = cnt;
2153 				return cnt;
2154 			}
2155 
2156 			INIT_LIST_HEAD(&mp2->list);
2157 		} else {
2158 			mp2 = NULL;
2159 		}
2160 
2161 		icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2162 		icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2163 		icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2164 		icmd->ulpBdeCount = 1;
2165 		cnt--;
2166 		if (mp2) {
2167 			icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2168 			icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2169 			icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2170 			cnt--;
2171 			icmd->ulpBdeCount = 2;
2172 		}
2173 
2174 		icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2175 		icmd->ulpLe = 1;
2176 
2177 		if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2178 		    IOCB_ERROR) {
2179 			lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2180 			kfree(mp1);
2181 			cnt++;
2182 			if (mp2) {
2183 				lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2184 				kfree(mp2);
2185 				cnt++;
2186 			}
2187 			lpfc_sli_release_iocbq(phba, iocb);
2188 			pring->missbufcnt = cnt;
2189 			return cnt;
2190 		}
2191 		lpfc_sli_ringpostbuf_put(phba, pring, mp1);
2192 		if (mp2)
2193 			lpfc_sli_ringpostbuf_put(phba, pring, mp2);
2194 	}
2195 	pring->missbufcnt = 0;
2196 	return 0;
2197 }
2198 
2199 /**
2200  * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
2201  * @phba: pointer to lpfc hba data structure.
2202  *
2203  * This routine posts initial receive IOCB buffers to the ELS ring. The
2204  * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
2205  * set to 64 IOCBs.
2206  *
2207  * Return codes
2208  *   0 - success (currently always success)
2209  **/
2210 static int
2211 lpfc_post_rcv_buf(struct lpfc_hba *phba)
2212 {
2213 	struct lpfc_sli *psli = &phba->sli;
2214 
2215 	/* Ring 0, ELS / CT buffers */
2216 	lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
2217 	/* Ring 2 - FCP no buffers needed */
2218 
2219 	return 0;
2220 }
2221 
2222 #define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2223 
2224 /**
2225  * lpfc_sha_init - Set up initial array of hash table entries
2226  * @HashResultPointer: pointer to an array as hash table.
2227  *
2228  * This routine sets up the initial values to the array of hash table entries
2229  * for the LC HBAs.
2230  **/
2231 static void
2232 lpfc_sha_init(uint32_t * HashResultPointer)
2233 {
2234 	HashResultPointer[0] = 0x67452301;
2235 	HashResultPointer[1] = 0xEFCDAB89;
2236 	HashResultPointer[2] = 0x98BADCFE;
2237 	HashResultPointer[3] = 0x10325476;
2238 	HashResultPointer[4] = 0xC3D2E1F0;
2239 }
2240 
2241 /**
2242  * lpfc_sha_iterate - Iterate initial hash table with the working hash table
2243  * @HashResultPointer: pointer to an initial/result hash table.
2244  * @HashWorkingPointer: pointer to an working hash table.
2245  *
2246  * This routine iterates an initial hash table pointed by @HashResultPointer
2247  * with the values from the working hash table pointeed by @HashWorkingPointer.
2248  * The results are putting back to the initial hash table, returned through
2249  * the @HashResultPointer as the result hash table.
2250  **/
2251 static void
2252 lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2253 {
2254 	int t;
2255 	uint32_t TEMP;
2256 	uint32_t A, B, C, D, E;
2257 	t = 16;
2258 	do {
2259 		HashWorkingPointer[t] =
2260 		    S(1,
2261 		      HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2262 								     8] ^
2263 		      HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2264 	} while (++t <= 79);
2265 	t = 0;
2266 	A = HashResultPointer[0];
2267 	B = HashResultPointer[1];
2268 	C = HashResultPointer[2];
2269 	D = HashResultPointer[3];
2270 	E = HashResultPointer[4];
2271 
2272 	do {
2273 		if (t < 20) {
2274 			TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2275 		} else if (t < 40) {
2276 			TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2277 		} else if (t < 60) {
2278 			TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2279 		} else {
2280 			TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2281 		}
2282 		TEMP += S(5, A) + E + HashWorkingPointer[t];
2283 		E = D;
2284 		D = C;
2285 		C = S(30, B);
2286 		B = A;
2287 		A = TEMP;
2288 	} while (++t <= 79);
2289 
2290 	HashResultPointer[0] += A;
2291 	HashResultPointer[1] += B;
2292 	HashResultPointer[2] += C;
2293 	HashResultPointer[3] += D;
2294 	HashResultPointer[4] += E;
2295 
2296 }
2297 
2298 /**
2299  * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
2300  * @RandomChallenge: pointer to the entry of host challenge random number array.
2301  * @HashWorking: pointer to the entry of the working hash array.
2302  *
2303  * This routine calculates the working hash array referred by @HashWorking
2304  * from the challenge random numbers associated with the host, referred by
2305  * @RandomChallenge. The result is put into the entry of the working hash
2306  * array and returned by reference through @HashWorking.
2307  **/
2308 static void
2309 lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2310 {
2311 	*HashWorking = (*RandomChallenge ^ *HashWorking);
2312 }
2313 
2314 /**
2315  * lpfc_hba_init - Perform special handling for LC HBA initialization
2316  * @phba: pointer to lpfc hba data structure.
2317  * @hbainit: pointer to an array of unsigned 32-bit integers.
2318  *
2319  * This routine performs the special handling for LC HBA initialization.
2320  **/
2321 void
2322 lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2323 {
2324 	int t;
2325 	uint32_t *HashWorking;
2326 	uint32_t *pwwnn = (uint32_t *) phba->wwnn;
2327 
2328 	HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
2329 	if (!HashWorking)
2330 		return;
2331 
2332 	HashWorking[0] = HashWorking[78] = *pwwnn++;
2333 	HashWorking[1] = HashWorking[79] = *pwwnn;
2334 
2335 	for (t = 0; t < 7; t++)
2336 		lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2337 
2338 	lpfc_sha_init(hbainit);
2339 	lpfc_sha_iterate(hbainit, HashWorking);
2340 	kfree(HashWorking);
2341 }
2342 
2343 /**
2344  * lpfc_cleanup - Performs vport cleanups before deleting a vport
2345  * @vport: pointer to a virtual N_Port data structure.
2346  *
2347  * This routine performs the necessary cleanups before deleting the @vport.
2348  * It invokes the discovery state machine to perform necessary state
2349  * transitions and to release the ndlps associated with the @vport. Note,
2350  * the physical port is treated as @vport 0.
2351  **/
2352 void
2353 lpfc_cleanup(struct lpfc_vport *vport)
2354 {
2355 	struct lpfc_hba   *phba = vport->phba;
2356 	struct lpfc_nodelist *ndlp, *next_ndlp;
2357 	int i = 0;
2358 
2359 	if (phba->link_state > LPFC_LINK_DOWN)
2360 		lpfc_port_link_failure(vport);
2361 
2362 	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
2363 		if (!NLP_CHK_NODE_ACT(ndlp)) {
2364 			ndlp = lpfc_enable_node(vport, ndlp,
2365 						NLP_STE_UNUSED_NODE);
2366 			if (!ndlp)
2367 				continue;
2368 			spin_lock_irq(&phba->ndlp_lock);
2369 			NLP_SET_FREE_REQ(ndlp);
2370 			spin_unlock_irq(&phba->ndlp_lock);
2371 			/* Trigger the release of the ndlp memory */
2372 			lpfc_nlp_put(ndlp);
2373 			continue;
2374 		}
2375 		spin_lock_irq(&phba->ndlp_lock);
2376 		if (NLP_CHK_FREE_REQ(ndlp)) {
2377 			/* The ndlp should not be in memory free mode already */
2378 			spin_unlock_irq(&phba->ndlp_lock);
2379 			continue;
2380 		} else
2381 			/* Indicate request for freeing ndlp memory */
2382 			NLP_SET_FREE_REQ(ndlp);
2383 		spin_unlock_irq(&phba->ndlp_lock);
2384 
2385 		if (vport->port_type != LPFC_PHYSICAL_PORT &&
2386 		    ndlp->nlp_DID == Fabric_DID) {
2387 			/* Just free up ndlp with Fabric_DID for vports */
2388 			lpfc_nlp_put(ndlp);
2389 			continue;
2390 		}
2391 
2392 		/* take care of nodes in unused state before the state
2393 		 * machine taking action.
2394 		 */
2395 		if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
2396 			lpfc_nlp_put(ndlp);
2397 			continue;
2398 		}
2399 
2400 		if (ndlp->nlp_type & NLP_FABRIC)
2401 			lpfc_disc_state_machine(vport, ndlp, NULL,
2402 					NLP_EVT_DEVICE_RECOVERY);
2403 
2404 		lpfc_disc_state_machine(vport, ndlp, NULL,
2405 					     NLP_EVT_DEVICE_RM);
2406 	}
2407 
2408 	/* At this point, ALL ndlp's should be gone
2409 	 * because of the previous NLP_EVT_DEVICE_RM.
2410 	 * Lets wait for this to happen, if needed.
2411 	 */
2412 	while (!list_empty(&vport->fc_nodes)) {
2413 		if (i++ > 3000) {
2414 			lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2415 				"0233 Nodelist not empty\n");
2416 			list_for_each_entry_safe(ndlp, next_ndlp,
2417 						&vport->fc_nodes, nlp_listp) {
2418 				lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2419 						LOG_NODE,
2420 						"0282 did:x%x ndlp:x%p "
2421 						"usgmap:x%x refcnt:%d\n",
2422 						ndlp->nlp_DID, (void *)ndlp,
2423 						ndlp->nlp_usg_map,
2424 						atomic_read(
2425 							&ndlp->kref.refcount));
2426 			}
2427 			break;
2428 		}
2429 
2430 		/* Wait for any activity on ndlps to settle */
2431 		msleep(10);
2432 	}
2433 	lpfc_cleanup_vports_rrqs(vport, NULL);
2434 }
2435 
2436 /**
2437  * lpfc_stop_vport_timers - Stop all the timers associated with a vport
2438  * @vport: pointer to a virtual N_Port data structure.
2439  *
2440  * This routine stops all the timers associated with a @vport. This function
2441  * is invoked before disabling or deleting a @vport. Note that the physical
2442  * port is treated as @vport 0.
2443  **/
2444 void
2445 lpfc_stop_vport_timers(struct lpfc_vport *vport)
2446 {
2447 	del_timer_sync(&vport->els_tmofunc);
2448 	del_timer_sync(&vport->fc_fdmitmo);
2449 	del_timer_sync(&vport->delayed_disc_tmo);
2450 	lpfc_can_disctmo(vport);
2451 	return;
2452 }
2453 
2454 /**
2455  * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2456  * @phba: pointer to lpfc hba data structure.
2457  *
2458  * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2459  * caller of this routine should already hold the host lock.
2460  **/
2461 void
2462 __lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2463 {
2464 	/* Clear pending FCF rediscovery wait flag */
2465 	phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2466 
2467 	/* Now, try to stop the timer */
2468 	del_timer(&phba->fcf.redisc_wait);
2469 }
2470 
2471 /**
2472  * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2473  * @phba: pointer to lpfc hba data structure.
2474  *
2475  * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2476  * checks whether the FCF rediscovery wait timer is pending with the host
2477  * lock held before proceeding with disabling the timer and clearing the
2478  * wait timer pendig flag.
2479  **/
2480 void
2481 lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2482 {
2483 	spin_lock_irq(&phba->hbalock);
2484 	if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2485 		/* FCF rediscovery timer already fired or stopped */
2486 		spin_unlock_irq(&phba->hbalock);
2487 		return;
2488 	}
2489 	__lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
2490 	/* Clear failover in progress flags */
2491 	phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
2492 	spin_unlock_irq(&phba->hbalock);
2493 }
2494 
2495 /**
2496  * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
2497  * @phba: pointer to lpfc hba data structure.
2498  *
2499  * This routine stops all the timers associated with a HBA. This function is
2500  * invoked before either putting a HBA offline or unloading the driver.
2501  **/
2502 void
2503 lpfc_stop_hba_timers(struct lpfc_hba *phba)
2504 {
2505 	lpfc_stop_vport_timers(phba->pport);
2506 	del_timer_sync(&phba->sli.mbox_tmo);
2507 	del_timer_sync(&phba->fabric_block_timer);
2508 	del_timer_sync(&phba->eratt_poll);
2509 	del_timer_sync(&phba->hb_tmofunc);
2510 	if (phba->sli_rev == LPFC_SLI_REV4) {
2511 		del_timer_sync(&phba->rrq_tmr);
2512 		phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2513 	}
2514 	phba->hb_outstanding = 0;
2515 
2516 	switch (phba->pci_dev_grp) {
2517 	case LPFC_PCI_DEV_LP:
2518 		/* Stop any LightPulse device specific driver timers */
2519 		del_timer_sync(&phba->fcp_poll_timer);
2520 		break;
2521 	case LPFC_PCI_DEV_OC:
2522 		/* Stop any OneConnect device sepcific driver timers */
2523 		lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
2524 		break;
2525 	default:
2526 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2527 				"0297 Invalid device group (x%x)\n",
2528 				phba->pci_dev_grp);
2529 		break;
2530 	}
2531 	return;
2532 }
2533 
2534 /**
2535  * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
2536  * @phba: pointer to lpfc hba data structure.
2537  *
2538  * This routine marks a HBA's management interface as blocked. Once the HBA's
2539  * management interface is marked as blocked, all the user space access to
2540  * the HBA, whether they are from sysfs interface or libdfc interface will
2541  * all be blocked. The HBA is set to block the management interface when the
2542  * driver prepares the HBA interface for online or offline.
2543  **/
2544 static void
2545 lpfc_block_mgmt_io(struct lpfc_hba *phba, int mbx_action)
2546 {
2547 	unsigned long iflag;
2548 	uint8_t actcmd = MBX_HEARTBEAT;
2549 	unsigned long timeout;
2550 
2551 	spin_lock_irqsave(&phba->hbalock, iflag);
2552 	phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
2553 	spin_unlock_irqrestore(&phba->hbalock, iflag);
2554 	if (mbx_action == LPFC_MBX_NO_WAIT)
2555 		return;
2556 	timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
2557 	spin_lock_irqsave(&phba->hbalock, iflag);
2558 	if (phba->sli.mbox_active) {
2559 		actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
2560 		/* Determine how long we might wait for the active mailbox
2561 		 * command to be gracefully completed by firmware.
2562 		 */
2563 		timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
2564 				phba->sli.mbox_active) * 1000) + jiffies;
2565 	}
2566 	spin_unlock_irqrestore(&phba->hbalock, iflag);
2567 
2568 	/* Wait for the outstnading mailbox command to complete */
2569 	while (phba->sli.mbox_active) {
2570 		/* Check active mailbox complete status every 2ms */
2571 		msleep(2);
2572 		if (time_after(jiffies, timeout)) {
2573 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2574 				"2813 Mgmt IO is Blocked %x "
2575 				"- mbox cmd %x still active\n",
2576 				phba->sli.sli_flag, actcmd);
2577 			break;
2578 		}
2579 	}
2580 }
2581 
2582 /**
2583  * lpfc_sli4_node_prep - Assign RPIs for active nodes.
2584  * @phba: pointer to lpfc hba data structure.
2585  *
2586  * Allocate RPIs for all active remote nodes. This is needed whenever
2587  * an SLI4 adapter is reset and the driver is not unloading. Its purpose
2588  * is to fixup the temporary rpi assignments.
2589  **/
2590 void
2591 lpfc_sli4_node_prep(struct lpfc_hba *phba)
2592 {
2593 	struct lpfc_nodelist  *ndlp, *next_ndlp;
2594 	struct lpfc_vport **vports;
2595 	int i;
2596 
2597 	if (phba->sli_rev != LPFC_SLI_REV4)
2598 		return;
2599 
2600 	vports = lpfc_create_vport_work_array(phba);
2601 	if (vports != NULL) {
2602 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2603 			if (vports[i]->load_flag & FC_UNLOADING)
2604 				continue;
2605 
2606 			list_for_each_entry_safe(ndlp, next_ndlp,
2607 						 &vports[i]->fc_nodes,
2608 						 nlp_listp) {
2609 				if (NLP_CHK_NODE_ACT(ndlp))
2610 					ndlp->nlp_rpi =
2611 						lpfc_sli4_alloc_rpi(phba);
2612 			}
2613 		}
2614 	}
2615 	lpfc_destroy_vport_work_array(phba, vports);
2616 }
2617 
2618 /**
2619  * lpfc_online - Initialize and bring a HBA online
2620  * @phba: pointer to lpfc hba data structure.
2621  *
2622  * This routine initializes the HBA and brings a HBA online. During this
2623  * process, the management interface is blocked to prevent user space access
2624  * to the HBA interfering with the driver initialization.
2625  *
2626  * Return codes
2627  *   0 - successful
2628  *   1 - failed
2629  **/
2630 int
2631 lpfc_online(struct lpfc_hba *phba)
2632 {
2633 	struct lpfc_vport *vport;
2634 	struct lpfc_vport **vports;
2635 	int i;
2636 
2637 	if (!phba)
2638 		return 0;
2639 	vport = phba->pport;
2640 
2641 	if (!(vport->fc_flag & FC_OFFLINE_MODE))
2642 		return 0;
2643 
2644 	lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2645 			"0458 Bring Adapter online\n");
2646 
2647 	lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
2648 
2649 	if (!lpfc_sli_queue_setup(phba)) {
2650 		lpfc_unblock_mgmt_io(phba);
2651 		return 1;
2652 	}
2653 
2654 	if (phba->sli_rev == LPFC_SLI_REV4) {
2655 		if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2656 			lpfc_unblock_mgmt_io(phba);
2657 			return 1;
2658 		}
2659 	} else {
2660 		if (lpfc_sli_hba_setup(phba)) {	/* Initialize SLI2/SLI3 HBA */
2661 			lpfc_unblock_mgmt_io(phba);
2662 			return 1;
2663 		}
2664 	}
2665 
2666 	vports = lpfc_create_vport_work_array(phba);
2667 	if (vports != NULL)
2668 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2669 			struct Scsi_Host *shost;
2670 			shost = lpfc_shost_from_vport(vports[i]);
2671 			spin_lock_irq(shost->host_lock);
2672 			vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2673 			if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2674 				vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2675 			if (phba->sli_rev == LPFC_SLI_REV4)
2676 				vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
2677 			spin_unlock_irq(shost->host_lock);
2678 		}
2679 		lpfc_destroy_vport_work_array(phba, vports);
2680 
2681 	lpfc_unblock_mgmt_io(phba);
2682 	return 0;
2683 }
2684 
2685 /**
2686  * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
2687  * @phba: pointer to lpfc hba data structure.
2688  *
2689  * This routine marks a HBA's management interface as not blocked. Once the
2690  * HBA's management interface is marked as not blocked, all the user space
2691  * access to the HBA, whether they are from sysfs interface or libdfc
2692  * interface will be allowed. The HBA is set to block the management interface
2693  * when the driver prepares the HBA interface for online or offline and then
2694  * set to unblock the management interface afterwards.
2695  **/
2696 void
2697 lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2698 {
2699 	unsigned long iflag;
2700 
2701 	spin_lock_irqsave(&phba->hbalock, iflag);
2702 	phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2703 	spin_unlock_irqrestore(&phba->hbalock, iflag);
2704 }
2705 
2706 /**
2707  * lpfc_offline_prep - Prepare a HBA to be brought offline
2708  * @phba: pointer to lpfc hba data structure.
2709  *
2710  * This routine is invoked to prepare a HBA to be brought offline. It performs
2711  * unregistration login to all the nodes on all vports and flushes the mailbox
2712  * queue to make it ready to be brought offline.
2713  **/
2714 void
2715 lpfc_offline_prep(struct lpfc_hba *phba, int mbx_action)
2716 {
2717 	struct lpfc_vport *vport = phba->pport;
2718 	struct lpfc_nodelist  *ndlp, *next_ndlp;
2719 	struct lpfc_vport **vports;
2720 	struct Scsi_Host *shost;
2721 	int i;
2722 
2723 	if (vport->fc_flag & FC_OFFLINE_MODE)
2724 		return;
2725 
2726 	lpfc_block_mgmt_io(phba, mbx_action);
2727 
2728 	lpfc_linkdown(phba);
2729 
2730 	/* Issue an unreg_login to all nodes on all vports */
2731 	vports = lpfc_create_vport_work_array(phba);
2732 	if (vports != NULL) {
2733 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2734 			if (vports[i]->load_flag & FC_UNLOADING)
2735 				continue;
2736 			shost = lpfc_shost_from_vport(vports[i]);
2737 			spin_lock_irq(shost->host_lock);
2738 			vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
2739 			vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2740 			vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
2741 			spin_unlock_irq(shost->host_lock);
2742 
2743 			shost =	lpfc_shost_from_vport(vports[i]);
2744 			list_for_each_entry_safe(ndlp, next_ndlp,
2745 						 &vports[i]->fc_nodes,
2746 						 nlp_listp) {
2747 				if (!NLP_CHK_NODE_ACT(ndlp))
2748 					continue;
2749 				if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2750 					continue;
2751 				if (ndlp->nlp_type & NLP_FABRIC) {
2752 					lpfc_disc_state_machine(vports[i], ndlp,
2753 						NULL, NLP_EVT_DEVICE_RECOVERY);
2754 					lpfc_disc_state_machine(vports[i], ndlp,
2755 						NULL, NLP_EVT_DEVICE_RM);
2756 				}
2757 				spin_lock_irq(shost->host_lock);
2758 				ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2759 				spin_unlock_irq(shost->host_lock);
2760 				/*
2761 				 * Whenever an SLI4 port goes offline, free the
2762 				 * RPI. Get a new RPI when the adapter port
2763 				 * comes back online.
2764 				 */
2765 				if (phba->sli_rev == LPFC_SLI_REV4)
2766 					lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
2767 				lpfc_unreg_rpi(vports[i], ndlp);
2768 			}
2769 		}
2770 	}
2771 	lpfc_destroy_vport_work_array(phba, vports);
2772 
2773 	lpfc_sli_mbox_sys_shutdown(phba, mbx_action);
2774 }
2775 
2776 /**
2777  * lpfc_offline - Bring a HBA offline
2778  * @phba: pointer to lpfc hba data structure.
2779  *
2780  * This routine actually brings a HBA offline. It stops all the timers
2781  * associated with the HBA, brings down the SLI layer, and eventually
2782  * marks the HBA as in offline state for the upper layer protocol.
2783  **/
2784 void
2785 lpfc_offline(struct lpfc_hba *phba)
2786 {
2787 	struct Scsi_Host  *shost;
2788 	struct lpfc_vport **vports;
2789 	int i;
2790 
2791 	if (phba->pport->fc_flag & FC_OFFLINE_MODE)
2792 		return;
2793 
2794 	/* stop port and all timers associated with this hba */
2795 	lpfc_stop_port(phba);
2796 	vports = lpfc_create_vport_work_array(phba);
2797 	if (vports != NULL)
2798 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
2799 			lpfc_stop_vport_timers(vports[i]);
2800 	lpfc_destroy_vport_work_array(phba, vports);
2801 	lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2802 			"0460 Bring Adapter offline\n");
2803 	/* Bring down the SLI Layer and cleanup.  The HBA is offline
2804 	   now.  */
2805 	lpfc_sli_hba_down(phba);
2806 	spin_lock_irq(&phba->hbalock);
2807 	phba->work_ha = 0;
2808 	spin_unlock_irq(&phba->hbalock);
2809 	vports = lpfc_create_vport_work_array(phba);
2810 	if (vports != NULL)
2811 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2812 			shost = lpfc_shost_from_vport(vports[i]);
2813 			spin_lock_irq(shost->host_lock);
2814 			vports[i]->work_port_events = 0;
2815 			vports[i]->fc_flag |= FC_OFFLINE_MODE;
2816 			spin_unlock_irq(shost->host_lock);
2817 		}
2818 	lpfc_destroy_vport_work_array(phba, vports);
2819 }
2820 
2821 /**
2822  * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
2823  * @phba: pointer to lpfc hba data structure.
2824  *
2825  * This routine is to free all the SCSI buffers and IOCBs from the driver
2826  * list back to kernel. It is called from lpfc_pci_remove_one to free
2827  * the internal resources before the device is removed from the system.
2828  **/
2829 static void
2830 lpfc_scsi_free(struct lpfc_hba *phba)
2831 {
2832 	struct lpfc_scsi_buf *sb, *sb_next;
2833 	struct lpfc_iocbq *io, *io_next;
2834 
2835 	spin_lock_irq(&phba->hbalock);
2836 	/* Release all the lpfc_scsi_bufs maintained by this host. */
2837 	spin_lock(&phba->scsi_buf_list_lock);
2838 	list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
2839 		list_del(&sb->list);
2840 		pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
2841 			      sb->dma_handle);
2842 		kfree(sb);
2843 		phba->total_scsi_bufs--;
2844 	}
2845 	spin_unlock(&phba->scsi_buf_list_lock);
2846 
2847 	/* Release all the lpfc_iocbq entries maintained by this host. */
2848 	list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2849 		list_del(&io->list);
2850 		kfree(io);
2851 		phba->total_iocbq_bufs--;
2852 	}
2853 
2854 	spin_unlock_irq(&phba->hbalock);
2855 }
2856 
2857 /**
2858  * lpfc_sli4_xri_sgl_update - update xri-sgl sizing and mapping
2859  * @phba: pointer to lpfc hba data structure.
2860  *
2861  * This routine first calculates the sizes of the current els and allocated
2862  * scsi sgl lists, and then goes through all sgls to updates the physical
2863  * XRIs assigned due to port function reset. During port initialization, the
2864  * current els and allocated scsi sgl lists are 0s.
2865  *
2866  * Return codes
2867  *   0 - successful (for now, it always returns 0)
2868  **/
2869 int
2870 lpfc_sli4_xri_sgl_update(struct lpfc_hba *phba)
2871 {
2872 	struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
2873 	struct lpfc_scsi_buf *psb = NULL, *psb_next = NULL;
2874 	uint16_t i, lxri, xri_cnt, els_xri_cnt, scsi_xri_cnt;
2875 	LIST_HEAD(els_sgl_list);
2876 	LIST_HEAD(scsi_sgl_list);
2877 	int rc;
2878 
2879 	/*
2880 	 * update on pci function's els xri-sgl list
2881 	 */
2882 	els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
2883 	if (els_xri_cnt > phba->sli4_hba.els_xri_cnt) {
2884 		/* els xri-sgl expanded */
2885 		xri_cnt = els_xri_cnt - phba->sli4_hba.els_xri_cnt;
2886 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2887 				"3157 ELS xri-sgl count increased from "
2888 				"%d to %d\n", phba->sli4_hba.els_xri_cnt,
2889 				els_xri_cnt);
2890 		/* allocate the additional els sgls */
2891 		for (i = 0; i < xri_cnt; i++) {
2892 			sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
2893 					     GFP_KERNEL);
2894 			if (sglq_entry == NULL) {
2895 				lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2896 						"2562 Failure to allocate an "
2897 						"ELS sgl entry:%d\n", i);
2898 				rc = -ENOMEM;
2899 				goto out_free_mem;
2900 			}
2901 			sglq_entry->buff_type = GEN_BUFF_TYPE;
2902 			sglq_entry->virt = lpfc_mbuf_alloc(phba, 0,
2903 							   &sglq_entry->phys);
2904 			if (sglq_entry->virt == NULL) {
2905 				kfree(sglq_entry);
2906 				lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2907 						"2563 Failure to allocate an "
2908 						"ELS mbuf:%d\n", i);
2909 				rc = -ENOMEM;
2910 				goto out_free_mem;
2911 			}
2912 			sglq_entry->sgl = sglq_entry->virt;
2913 			memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
2914 			sglq_entry->state = SGL_FREED;
2915 			list_add_tail(&sglq_entry->list, &els_sgl_list);
2916 		}
2917 		spin_lock_irq(&phba->hbalock);
2918 		list_splice_init(&els_sgl_list, &phba->sli4_hba.lpfc_sgl_list);
2919 		spin_unlock_irq(&phba->hbalock);
2920 	} else if (els_xri_cnt < phba->sli4_hba.els_xri_cnt) {
2921 		/* els xri-sgl shrinked */
2922 		xri_cnt = phba->sli4_hba.els_xri_cnt - els_xri_cnt;
2923 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2924 				"3158 ELS xri-sgl count decreased from "
2925 				"%d to %d\n", phba->sli4_hba.els_xri_cnt,
2926 				els_xri_cnt);
2927 		spin_lock_irq(&phba->hbalock);
2928 		list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &els_sgl_list);
2929 		spin_unlock_irq(&phba->hbalock);
2930 		/* release extra els sgls from list */
2931 		for (i = 0; i < xri_cnt; i++) {
2932 			list_remove_head(&els_sgl_list,
2933 					 sglq_entry, struct lpfc_sglq, list);
2934 			if (sglq_entry) {
2935 				lpfc_mbuf_free(phba, sglq_entry->virt,
2936 					       sglq_entry->phys);
2937 				kfree(sglq_entry);
2938 			}
2939 		}
2940 		spin_lock_irq(&phba->hbalock);
2941 		list_splice_init(&els_sgl_list, &phba->sli4_hba.lpfc_sgl_list);
2942 		spin_unlock_irq(&phba->hbalock);
2943 	} else
2944 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2945 				"3163 ELS xri-sgl count unchanged: %d\n",
2946 				els_xri_cnt);
2947 	phba->sli4_hba.els_xri_cnt = els_xri_cnt;
2948 
2949 	/* update xris to els sgls on the list */
2950 	sglq_entry = NULL;
2951 	sglq_entry_next = NULL;
2952 	list_for_each_entry_safe(sglq_entry, sglq_entry_next,
2953 				 &phba->sli4_hba.lpfc_sgl_list, list) {
2954 		lxri = lpfc_sli4_next_xritag(phba);
2955 		if (lxri == NO_XRI) {
2956 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2957 					"2400 Failed to allocate xri for "
2958 					"ELS sgl\n");
2959 			rc = -ENOMEM;
2960 			goto out_free_mem;
2961 		}
2962 		sglq_entry->sli4_lxritag = lxri;
2963 		sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
2964 	}
2965 
2966 	/*
2967 	 * update on pci function's allocated scsi xri-sgl list
2968 	 */
2969 	phba->total_scsi_bufs = 0;
2970 
2971 	/* maximum number of xris available for scsi buffers */
2972 	phba->sli4_hba.scsi_xri_max = phba->sli4_hba.max_cfg_param.max_xri -
2973 				      els_xri_cnt;
2974 
2975 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2976 			"2401 Current allocated SCSI xri-sgl count:%d, "
2977 			"maximum  SCSI xri count:%d\n",
2978 			phba->sli4_hba.scsi_xri_cnt,
2979 			phba->sli4_hba.scsi_xri_max);
2980 
2981 	spin_lock_irq(&phba->scsi_buf_list_lock);
2982 	list_splice_init(&phba->lpfc_scsi_buf_list, &scsi_sgl_list);
2983 	spin_unlock_irq(&phba->scsi_buf_list_lock);
2984 
2985 	if (phba->sli4_hba.scsi_xri_cnt > phba->sli4_hba.scsi_xri_max) {
2986 		/* max scsi xri shrinked below the allocated scsi buffers */
2987 		scsi_xri_cnt = phba->sli4_hba.scsi_xri_cnt -
2988 					phba->sli4_hba.scsi_xri_max;
2989 		/* release the extra allocated scsi buffers */
2990 		for (i = 0; i < scsi_xri_cnt; i++) {
2991 			list_remove_head(&scsi_sgl_list, psb,
2992 					 struct lpfc_scsi_buf, list);
2993 			pci_pool_free(phba->lpfc_scsi_dma_buf_pool, psb->data,
2994 				      psb->dma_handle);
2995 			kfree(psb);
2996 		}
2997 		spin_lock_irq(&phba->scsi_buf_list_lock);
2998 		phba->sli4_hba.scsi_xri_cnt -= scsi_xri_cnt;
2999 		spin_unlock_irq(&phba->scsi_buf_list_lock);
3000 	}
3001 
3002 	/* update xris associated to remaining allocated scsi buffers */
3003 	psb = NULL;
3004 	psb_next = NULL;
3005 	list_for_each_entry_safe(psb, psb_next, &scsi_sgl_list, list) {
3006 		lxri = lpfc_sli4_next_xritag(phba);
3007 		if (lxri == NO_XRI) {
3008 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3009 					"2560 Failed to allocate xri for "
3010 					"scsi buffer\n");
3011 			rc = -ENOMEM;
3012 			goto out_free_mem;
3013 		}
3014 		psb->cur_iocbq.sli4_lxritag = lxri;
3015 		psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3016 	}
3017 	spin_lock_irq(&phba->scsi_buf_list_lock);
3018 	list_splice_init(&scsi_sgl_list, &phba->lpfc_scsi_buf_list);
3019 	spin_unlock_irq(&phba->scsi_buf_list_lock);
3020 
3021 	return 0;
3022 
3023 out_free_mem:
3024 	lpfc_free_els_sgl_list(phba);
3025 	lpfc_scsi_free(phba);
3026 	return rc;
3027 }
3028 
3029 /**
3030  * lpfc_create_port - Create an FC port
3031  * @phba: pointer to lpfc hba data structure.
3032  * @instance: a unique integer ID to this FC port.
3033  * @dev: pointer to the device data structure.
3034  *
3035  * This routine creates a FC port for the upper layer protocol. The FC port
3036  * can be created on top of either a physical port or a virtual port provided
3037  * by the HBA. This routine also allocates a SCSI host data structure (shost)
3038  * and associates the FC port created before adding the shost into the SCSI
3039  * layer.
3040  *
3041  * Return codes
3042  *   @vport - pointer to the virtual N_Port data structure.
3043  *   NULL - port create failed.
3044  **/
3045 struct lpfc_vport *
3046 lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
3047 {
3048 	struct lpfc_vport *vport;
3049 	struct Scsi_Host  *shost;
3050 	int error = 0;
3051 
3052 	if (dev != &phba->pcidev->dev)
3053 		shost = scsi_host_alloc(&lpfc_vport_template,
3054 					sizeof(struct lpfc_vport));
3055 	else
3056 		shost = scsi_host_alloc(&lpfc_template,
3057 					sizeof(struct lpfc_vport));
3058 	if (!shost)
3059 		goto out;
3060 
3061 	vport = (struct lpfc_vport *) shost->hostdata;
3062 	vport->phba = phba;
3063 	vport->load_flag |= FC_LOADING;
3064 	vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
3065 	vport->fc_rscn_flush = 0;
3066 
3067 	lpfc_get_vport_cfgparam(vport);
3068 	shost->unique_id = instance;
3069 	shost->max_id = LPFC_MAX_TARGET;
3070 	shost->max_lun = vport->cfg_max_luns;
3071 	shost->this_id = -1;
3072 	shost->max_cmd_len = 16;
3073 	if (phba->sli_rev == LPFC_SLI_REV4) {
3074 		shost->dma_boundary =
3075 			phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
3076 		shost->sg_tablesize = phba->cfg_sg_seg_cnt;
3077 	}
3078 
3079 	/*
3080 	 * Set initial can_queue value since 0 is no longer supported and
3081 	 * scsi_add_host will fail. This will be adjusted later based on the
3082 	 * max xri value determined in hba setup.
3083 	 */
3084 	shost->can_queue = phba->cfg_hba_queue_depth - 10;
3085 	if (dev != &phba->pcidev->dev) {
3086 		shost->transportt = lpfc_vport_transport_template;
3087 		vport->port_type = LPFC_NPIV_PORT;
3088 	} else {
3089 		shost->transportt = lpfc_transport_template;
3090 		vport->port_type = LPFC_PHYSICAL_PORT;
3091 	}
3092 
3093 	/* Initialize all internally managed lists. */
3094 	INIT_LIST_HEAD(&vport->fc_nodes);
3095 	INIT_LIST_HEAD(&vport->rcv_buffer_list);
3096 	spin_lock_init(&vport->work_port_lock);
3097 
3098 	init_timer(&vport->fc_disctmo);
3099 	vport->fc_disctmo.function = lpfc_disc_timeout;
3100 	vport->fc_disctmo.data = (unsigned long)vport;
3101 
3102 	init_timer(&vport->fc_fdmitmo);
3103 	vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
3104 	vport->fc_fdmitmo.data = (unsigned long)vport;
3105 
3106 	init_timer(&vport->els_tmofunc);
3107 	vport->els_tmofunc.function = lpfc_els_timeout;
3108 	vport->els_tmofunc.data = (unsigned long)vport;
3109 
3110 	init_timer(&vport->delayed_disc_tmo);
3111 	vport->delayed_disc_tmo.function = lpfc_delayed_disc_tmo;
3112 	vport->delayed_disc_tmo.data = (unsigned long)vport;
3113 
3114 	error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
3115 	if (error)
3116 		goto out_put_shost;
3117 
3118 	spin_lock_irq(&phba->hbalock);
3119 	list_add_tail(&vport->listentry, &phba->port_list);
3120 	spin_unlock_irq(&phba->hbalock);
3121 	return vport;
3122 
3123 out_put_shost:
3124 	scsi_host_put(shost);
3125 out:
3126 	return NULL;
3127 }
3128 
3129 /**
3130  * destroy_port -  destroy an FC port
3131  * @vport: pointer to an lpfc virtual N_Port data structure.
3132  *
3133  * This routine destroys a FC port from the upper layer protocol. All the
3134  * resources associated with the port are released.
3135  **/
3136 void
3137 destroy_port(struct lpfc_vport *vport)
3138 {
3139 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3140 	struct lpfc_hba  *phba = vport->phba;
3141 
3142 	lpfc_debugfs_terminate(vport);
3143 	fc_remove_host(shost);
3144 	scsi_remove_host(shost);
3145 
3146 	spin_lock_irq(&phba->hbalock);
3147 	list_del_init(&vport->listentry);
3148 	spin_unlock_irq(&phba->hbalock);
3149 
3150 	lpfc_cleanup(vport);
3151 	return;
3152 }
3153 
3154 /**
3155  * lpfc_get_instance - Get a unique integer ID
3156  *
3157  * This routine allocates a unique integer ID from lpfc_hba_index pool. It
3158  * uses the kernel idr facility to perform the task.
3159  *
3160  * Return codes:
3161  *   instance - a unique integer ID allocated as the new instance.
3162  *   -1 - lpfc get instance failed.
3163  **/
3164 int
3165 lpfc_get_instance(void)
3166 {
3167 	int ret;
3168 
3169 	ret = idr_alloc(&lpfc_hba_index, NULL, 0, 0, GFP_KERNEL);
3170 	return ret < 0 ? -1 : ret;
3171 }
3172 
3173 /**
3174  * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
3175  * @shost: pointer to SCSI host data structure.
3176  * @time: elapsed time of the scan in jiffies.
3177  *
3178  * This routine is called by the SCSI layer with a SCSI host to determine
3179  * whether the scan host is finished.
3180  *
3181  * Note: there is no scan_start function as adapter initialization will have
3182  * asynchronously kicked off the link initialization.
3183  *
3184  * Return codes
3185  *   0 - SCSI host scan is not over yet.
3186  *   1 - SCSI host scan is over.
3187  **/
3188 int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
3189 {
3190 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3191 	struct lpfc_hba   *phba = vport->phba;
3192 	int stat = 0;
3193 
3194 	spin_lock_irq(shost->host_lock);
3195 
3196 	if (vport->load_flag & FC_UNLOADING) {
3197 		stat = 1;
3198 		goto finished;
3199 	}
3200 	if (time >= 30 * HZ) {
3201 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3202 				"0461 Scanning longer than 30 "
3203 				"seconds.  Continuing initialization\n");
3204 		stat = 1;
3205 		goto finished;
3206 	}
3207 	if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
3208 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3209 				"0465 Link down longer than 15 "
3210 				"seconds.  Continuing initialization\n");
3211 		stat = 1;
3212 		goto finished;
3213 	}
3214 
3215 	if (vport->port_state != LPFC_VPORT_READY)
3216 		goto finished;
3217 	if (vport->num_disc_nodes || vport->fc_prli_sent)
3218 		goto finished;
3219 	if (vport->fc_map_cnt == 0 && time < 2 * HZ)
3220 		goto finished;
3221 	if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
3222 		goto finished;
3223 
3224 	stat = 1;
3225 
3226 finished:
3227 	spin_unlock_irq(shost->host_lock);
3228 	return stat;
3229 }
3230 
3231 /**
3232  * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
3233  * @shost: pointer to SCSI host data structure.
3234  *
3235  * This routine initializes a given SCSI host attributes on a FC port. The
3236  * SCSI host can be either on top of a physical port or a virtual port.
3237  **/
3238 void lpfc_host_attrib_init(struct Scsi_Host *shost)
3239 {
3240 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3241 	struct lpfc_hba   *phba = vport->phba;
3242 	/*
3243 	 * Set fixed host attributes.  Must done after lpfc_sli_hba_setup().
3244 	 */
3245 
3246 	fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
3247 	fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
3248 	fc_host_supported_classes(shost) = FC_COS_CLASS3;
3249 
3250 	memset(fc_host_supported_fc4s(shost), 0,
3251 	       sizeof(fc_host_supported_fc4s(shost)));
3252 	fc_host_supported_fc4s(shost)[2] = 1;
3253 	fc_host_supported_fc4s(shost)[7] = 1;
3254 
3255 	lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
3256 				 sizeof fc_host_symbolic_name(shost));
3257 
3258 	fc_host_supported_speeds(shost) = 0;
3259 	if (phba->lmt & LMT_16Gb)
3260 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
3261 	if (phba->lmt & LMT_10Gb)
3262 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
3263 	if (phba->lmt & LMT_8Gb)
3264 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
3265 	if (phba->lmt & LMT_4Gb)
3266 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
3267 	if (phba->lmt & LMT_2Gb)
3268 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
3269 	if (phba->lmt & LMT_1Gb)
3270 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
3271 
3272 	fc_host_maxframe_size(shost) =
3273 		(((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
3274 		(uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
3275 
3276 	fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
3277 
3278 	/* This value is also unchanging */
3279 	memset(fc_host_active_fc4s(shost), 0,
3280 	       sizeof(fc_host_active_fc4s(shost)));
3281 	fc_host_active_fc4s(shost)[2] = 1;
3282 	fc_host_active_fc4s(shost)[7] = 1;
3283 
3284 	fc_host_max_npiv_vports(shost) = phba->max_vpi;
3285 	spin_lock_irq(shost->host_lock);
3286 	vport->load_flag &= ~FC_LOADING;
3287 	spin_unlock_irq(shost->host_lock);
3288 }
3289 
3290 /**
3291  * lpfc_stop_port_s3 - Stop SLI3 device port
3292  * @phba: pointer to lpfc hba data structure.
3293  *
3294  * This routine is invoked to stop an SLI3 device port, it stops the device
3295  * from generating interrupts and stops the device driver's timers for the
3296  * device.
3297  **/
3298 static void
3299 lpfc_stop_port_s3(struct lpfc_hba *phba)
3300 {
3301 	/* Clear all interrupt enable conditions */
3302 	writel(0, phba->HCregaddr);
3303 	readl(phba->HCregaddr); /* flush */
3304 	/* Clear all pending interrupts */
3305 	writel(0xffffffff, phba->HAregaddr);
3306 	readl(phba->HAregaddr); /* flush */
3307 
3308 	/* Reset some HBA SLI setup states */
3309 	lpfc_stop_hba_timers(phba);
3310 	phba->pport->work_port_events = 0;
3311 }
3312 
3313 /**
3314  * lpfc_stop_port_s4 - Stop SLI4 device port
3315  * @phba: pointer to lpfc hba data structure.
3316  *
3317  * This routine is invoked to stop an SLI4 device port, it stops the device
3318  * from generating interrupts and stops the device driver's timers for the
3319  * device.
3320  **/
3321 static void
3322 lpfc_stop_port_s4(struct lpfc_hba *phba)
3323 {
3324 	/* Reset some HBA SLI4 setup states */
3325 	lpfc_stop_hba_timers(phba);
3326 	phba->pport->work_port_events = 0;
3327 	phba->sli4_hba.intr_enable = 0;
3328 }
3329 
3330 /**
3331  * lpfc_stop_port - Wrapper function for stopping hba port
3332  * @phba: Pointer to HBA context object.
3333  *
3334  * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
3335  * the API jump table function pointer from the lpfc_hba struct.
3336  **/
3337 void
3338 lpfc_stop_port(struct lpfc_hba *phba)
3339 {
3340 	phba->lpfc_stop_port(phba);
3341 }
3342 
3343 /**
3344  * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
3345  * @phba: Pointer to hba for which this call is being executed.
3346  *
3347  * This routine starts the timer waiting for the FCF rediscovery to complete.
3348  **/
3349 void
3350 lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
3351 {
3352 	unsigned long fcf_redisc_wait_tmo =
3353 		(jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
3354 	/* Start fcf rediscovery wait period timer */
3355 	mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
3356 	spin_lock_irq(&phba->hbalock);
3357 	/* Allow action to new fcf asynchronous event */
3358 	phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
3359 	/* Mark the FCF rediscovery pending state */
3360 	phba->fcf.fcf_flag |= FCF_REDISC_PEND;
3361 	spin_unlock_irq(&phba->hbalock);
3362 }
3363 
3364 /**
3365  * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
3366  * @ptr: Map to lpfc_hba data structure pointer.
3367  *
3368  * This routine is invoked when waiting for FCF table rediscover has been
3369  * timed out. If new FCF record(s) has (have) been discovered during the
3370  * wait period, a new FCF event shall be added to the FCOE async event
3371  * list, and then worker thread shall be waked up for processing from the
3372  * worker thread context.
3373  **/
3374 void
3375 lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
3376 {
3377 	struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
3378 
3379 	/* Don't send FCF rediscovery event if timer cancelled */
3380 	spin_lock_irq(&phba->hbalock);
3381 	if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
3382 		spin_unlock_irq(&phba->hbalock);
3383 		return;
3384 	}
3385 	/* Clear FCF rediscovery timer pending flag */
3386 	phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
3387 	/* FCF rediscovery event to worker thread */
3388 	phba->fcf.fcf_flag |= FCF_REDISC_EVT;
3389 	spin_unlock_irq(&phba->hbalock);
3390 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
3391 			"2776 FCF rediscover quiescent timer expired\n");
3392 	/* wake up worker thread */
3393 	lpfc_worker_wake_up(phba);
3394 }
3395 
3396 /**
3397  * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
3398  * @phba: pointer to lpfc hba data structure.
3399  * @acqe_link: pointer to the async link completion queue entry.
3400  *
3401  * This routine is to parse the SLI4 link-attention link fault code and
3402  * translate it into the base driver's read link attention mailbox command
3403  * status.
3404  *
3405  * Return: Link-attention status in terms of base driver's coding.
3406  **/
3407 static uint16_t
3408 lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
3409 			   struct lpfc_acqe_link *acqe_link)
3410 {
3411 	uint16_t latt_fault;
3412 
3413 	switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
3414 	case LPFC_ASYNC_LINK_FAULT_NONE:
3415 	case LPFC_ASYNC_LINK_FAULT_LOCAL:
3416 	case LPFC_ASYNC_LINK_FAULT_REMOTE:
3417 		latt_fault = 0;
3418 		break;
3419 	default:
3420 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3421 				"0398 Invalid link fault code: x%x\n",
3422 				bf_get(lpfc_acqe_link_fault, acqe_link));
3423 		latt_fault = MBXERR_ERROR;
3424 		break;
3425 	}
3426 	return latt_fault;
3427 }
3428 
3429 /**
3430  * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
3431  * @phba: pointer to lpfc hba data structure.
3432  * @acqe_link: pointer to the async link completion queue entry.
3433  *
3434  * This routine is to parse the SLI4 link attention type and translate it
3435  * into the base driver's link attention type coding.
3436  *
3437  * Return: Link attention type in terms of base driver's coding.
3438  **/
3439 static uint8_t
3440 lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
3441 			  struct lpfc_acqe_link *acqe_link)
3442 {
3443 	uint8_t att_type;
3444 
3445 	switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
3446 	case LPFC_ASYNC_LINK_STATUS_DOWN:
3447 	case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
3448 		att_type = LPFC_ATT_LINK_DOWN;
3449 		break;
3450 	case LPFC_ASYNC_LINK_STATUS_UP:
3451 		/* Ignore physical link up events - wait for logical link up */
3452 		att_type = LPFC_ATT_RESERVED;
3453 		break;
3454 	case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
3455 		att_type = LPFC_ATT_LINK_UP;
3456 		break;
3457 	default:
3458 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3459 				"0399 Invalid link attention type: x%x\n",
3460 				bf_get(lpfc_acqe_link_status, acqe_link));
3461 		att_type = LPFC_ATT_RESERVED;
3462 		break;
3463 	}
3464 	return att_type;
3465 }
3466 
3467 /**
3468  * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
3469  * @phba: pointer to lpfc hba data structure.
3470  * @acqe_link: pointer to the async link completion queue entry.
3471  *
3472  * This routine is to parse the SLI4 link-attention link speed and translate
3473  * it into the base driver's link-attention link speed coding.
3474  *
3475  * Return: Link-attention link speed in terms of base driver's coding.
3476  **/
3477 static uint8_t
3478 lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
3479 				struct lpfc_acqe_link *acqe_link)
3480 {
3481 	uint8_t link_speed;
3482 
3483 	switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
3484 	case LPFC_ASYNC_LINK_SPEED_ZERO:
3485 	case LPFC_ASYNC_LINK_SPEED_10MBPS:
3486 	case LPFC_ASYNC_LINK_SPEED_100MBPS:
3487 		link_speed = LPFC_LINK_SPEED_UNKNOWN;
3488 		break;
3489 	case LPFC_ASYNC_LINK_SPEED_1GBPS:
3490 		link_speed = LPFC_LINK_SPEED_1GHZ;
3491 		break;
3492 	case LPFC_ASYNC_LINK_SPEED_10GBPS:
3493 		link_speed = LPFC_LINK_SPEED_10GHZ;
3494 		break;
3495 	default:
3496 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3497 				"0483 Invalid link-attention link speed: x%x\n",
3498 				bf_get(lpfc_acqe_link_speed, acqe_link));
3499 		link_speed = LPFC_LINK_SPEED_UNKNOWN;
3500 		break;
3501 	}
3502 	return link_speed;
3503 }
3504 
3505 /**
3506  * lpfc_sli_port_speed_get - Get sli3 link speed code to link speed
3507  * @phba: pointer to lpfc hba data structure.
3508  *
3509  * This routine is to get an SLI3 FC port's link speed in Mbps.
3510  *
3511  * Return: link speed in terms of Mbps.
3512  **/
3513 uint32_t
3514 lpfc_sli_port_speed_get(struct lpfc_hba *phba)
3515 {
3516 	uint32_t link_speed;
3517 
3518 	if (!lpfc_is_link_up(phba))
3519 		return 0;
3520 
3521 	switch (phba->fc_linkspeed) {
3522 	case LPFC_LINK_SPEED_1GHZ:
3523 		link_speed = 1000;
3524 		break;
3525 	case LPFC_LINK_SPEED_2GHZ:
3526 		link_speed = 2000;
3527 		break;
3528 	case LPFC_LINK_SPEED_4GHZ:
3529 		link_speed = 4000;
3530 		break;
3531 	case LPFC_LINK_SPEED_8GHZ:
3532 		link_speed = 8000;
3533 		break;
3534 	case LPFC_LINK_SPEED_10GHZ:
3535 		link_speed = 10000;
3536 		break;
3537 	case LPFC_LINK_SPEED_16GHZ:
3538 		link_speed = 16000;
3539 		break;
3540 	default:
3541 		link_speed = 0;
3542 	}
3543 	return link_speed;
3544 }
3545 
3546 /**
3547  * lpfc_sli4_port_speed_parse - Parse async evt link speed code to link speed
3548  * @phba: pointer to lpfc hba data structure.
3549  * @evt_code: asynchronous event code.
3550  * @speed_code: asynchronous event link speed code.
3551  *
3552  * This routine is to parse the giving SLI4 async event link speed code into
3553  * value of Mbps for the link speed.
3554  *
3555  * Return: link speed in terms of Mbps.
3556  **/
3557 static uint32_t
3558 lpfc_sli4_port_speed_parse(struct lpfc_hba *phba, uint32_t evt_code,
3559 			   uint8_t speed_code)
3560 {
3561 	uint32_t port_speed;
3562 
3563 	switch (evt_code) {
3564 	case LPFC_TRAILER_CODE_LINK:
3565 		switch (speed_code) {
3566 		case LPFC_EVT_CODE_LINK_NO_LINK:
3567 			port_speed = 0;
3568 			break;
3569 		case LPFC_EVT_CODE_LINK_10_MBIT:
3570 			port_speed = 10;
3571 			break;
3572 		case LPFC_EVT_CODE_LINK_100_MBIT:
3573 			port_speed = 100;
3574 			break;
3575 		case LPFC_EVT_CODE_LINK_1_GBIT:
3576 			port_speed = 1000;
3577 			break;
3578 		case LPFC_EVT_CODE_LINK_10_GBIT:
3579 			port_speed = 10000;
3580 			break;
3581 		default:
3582 			port_speed = 0;
3583 		}
3584 		break;
3585 	case LPFC_TRAILER_CODE_FC:
3586 		switch (speed_code) {
3587 		case LPFC_EVT_CODE_FC_NO_LINK:
3588 			port_speed = 0;
3589 			break;
3590 		case LPFC_EVT_CODE_FC_1_GBAUD:
3591 			port_speed = 1000;
3592 			break;
3593 		case LPFC_EVT_CODE_FC_2_GBAUD:
3594 			port_speed = 2000;
3595 			break;
3596 		case LPFC_EVT_CODE_FC_4_GBAUD:
3597 			port_speed = 4000;
3598 			break;
3599 		case LPFC_EVT_CODE_FC_8_GBAUD:
3600 			port_speed = 8000;
3601 			break;
3602 		case LPFC_EVT_CODE_FC_10_GBAUD:
3603 			port_speed = 10000;
3604 			break;
3605 		case LPFC_EVT_CODE_FC_16_GBAUD:
3606 			port_speed = 16000;
3607 			break;
3608 		default:
3609 			port_speed = 0;
3610 		}
3611 		break;
3612 	default:
3613 		port_speed = 0;
3614 	}
3615 	return port_speed;
3616 }
3617 
3618 /**
3619  * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
3620  * @phba: pointer to lpfc hba data structure.
3621  * @acqe_link: pointer to the async link completion queue entry.
3622  *
3623  * This routine is to handle the SLI4 asynchronous FCoE link event.
3624  **/
3625 static void
3626 lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3627 			 struct lpfc_acqe_link *acqe_link)
3628 {
3629 	struct lpfc_dmabuf *mp;
3630 	LPFC_MBOXQ_t *pmb;
3631 	MAILBOX_t *mb;
3632 	struct lpfc_mbx_read_top *la;
3633 	uint8_t att_type;
3634 	int rc;
3635 
3636 	att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
3637 	if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
3638 		return;
3639 	phba->fcoe_eventtag = acqe_link->event_tag;
3640 	pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3641 	if (!pmb) {
3642 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3643 				"0395 The mboxq allocation failed\n");
3644 		return;
3645 	}
3646 	mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3647 	if (!mp) {
3648 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3649 				"0396 The lpfc_dmabuf allocation failed\n");
3650 		goto out_free_pmb;
3651 	}
3652 	mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3653 	if (!mp->virt) {
3654 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3655 				"0397 The mbuf allocation failed\n");
3656 		goto out_free_dmabuf;
3657 	}
3658 
3659 	/* Cleanup any outstanding ELS commands */
3660 	lpfc_els_flush_all_cmd(phba);
3661 
3662 	/* Block ELS IOCBs until we have done process link event */
3663 	phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3664 
3665 	/* Update link event statistics */
3666 	phba->sli.slistat.link_event++;
3667 
3668 	/* Create lpfc_handle_latt mailbox command from link ACQE */
3669 	lpfc_read_topology(phba, pmb, mp);
3670 	pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
3671 	pmb->vport = phba->pport;
3672 
3673 	/* Keep the link status for extra SLI4 state machine reference */
3674 	phba->sli4_hba.link_state.speed =
3675 			lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_LINK,
3676 				bf_get(lpfc_acqe_link_speed, acqe_link));
3677 	phba->sli4_hba.link_state.duplex =
3678 				bf_get(lpfc_acqe_link_duplex, acqe_link);
3679 	phba->sli4_hba.link_state.status =
3680 				bf_get(lpfc_acqe_link_status, acqe_link);
3681 	phba->sli4_hba.link_state.type =
3682 				bf_get(lpfc_acqe_link_type, acqe_link);
3683 	phba->sli4_hba.link_state.number =
3684 				bf_get(lpfc_acqe_link_number, acqe_link);
3685 	phba->sli4_hba.link_state.fault =
3686 				bf_get(lpfc_acqe_link_fault, acqe_link);
3687 	phba->sli4_hba.link_state.logical_speed =
3688 			bf_get(lpfc_acqe_logical_link_speed, acqe_link) * 10;
3689 
3690 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3691 			"2900 Async FC/FCoE Link event - Speed:%dGBit "
3692 			"duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
3693 			"Logical speed:%dMbps Fault:%d\n",
3694 			phba->sli4_hba.link_state.speed,
3695 			phba->sli4_hba.link_state.topology,
3696 			phba->sli4_hba.link_state.status,
3697 			phba->sli4_hba.link_state.type,
3698 			phba->sli4_hba.link_state.number,
3699 			phba->sli4_hba.link_state.logical_speed,
3700 			phba->sli4_hba.link_state.fault);
3701 	/*
3702 	 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
3703 	 * topology info. Note: Optional for non FC-AL ports.
3704 	 */
3705 	if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3706 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3707 		if (rc == MBX_NOT_FINISHED)
3708 			goto out_free_dmabuf;
3709 		return;
3710 	}
3711 	/*
3712 	 * For FCoE Mode: fill in all the topology information we need and call
3713 	 * the READ_TOPOLOGY completion routine to continue without actually
3714 	 * sending the READ_TOPOLOGY mailbox command to the port.
3715 	 */
3716 	/* Parse and translate status field */
3717 	mb = &pmb->u.mb;
3718 	mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
3719 
3720 	/* Parse and translate link attention fields */
3721 	la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
3722 	la->eventTag = acqe_link->event_tag;
3723 	bf_set(lpfc_mbx_read_top_att_type, la, att_type);
3724 	bf_set(lpfc_mbx_read_top_link_spd, la,
3725 	       lpfc_sli4_parse_latt_link_speed(phba, acqe_link));
3726 
3727 	/* Fake the the following irrelvant fields */
3728 	bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
3729 	bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
3730 	bf_set(lpfc_mbx_read_top_il, la, 0);
3731 	bf_set(lpfc_mbx_read_top_pb, la, 0);
3732 	bf_set(lpfc_mbx_read_top_fa, la, 0);
3733 	bf_set(lpfc_mbx_read_top_mm, la, 0);
3734 
3735 	/* Invoke the lpfc_handle_latt mailbox command callback function */
3736 	lpfc_mbx_cmpl_read_topology(phba, pmb);
3737 
3738 	return;
3739 
3740 out_free_dmabuf:
3741 	kfree(mp);
3742 out_free_pmb:
3743 	mempool_free(pmb, phba->mbox_mem_pool);
3744 }
3745 
3746 /**
3747  * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
3748  * @phba: pointer to lpfc hba data structure.
3749  * @acqe_fc: pointer to the async fc completion queue entry.
3750  *
3751  * This routine is to handle the SLI4 asynchronous FC event. It will simply log
3752  * that the event was received and then issue a read_topology mailbox command so
3753  * that the rest of the driver will treat it the same as SLI3.
3754  **/
3755 static void
3756 lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
3757 {
3758 	struct lpfc_dmabuf *mp;
3759 	LPFC_MBOXQ_t *pmb;
3760 	int rc;
3761 
3762 	if (bf_get(lpfc_trailer_type, acqe_fc) !=
3763 	    LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
3764 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3765 				"2895 Non FC link Event detected.(%d)\n",
3766 				bf_get(lpfc_trailer_type, acqe_fc));
3767 		return;
3768 	}
3769 	/* Keep the link status for extra SLI4 state machine reference */
3770 	phba->sli4_hba.link_state.speed =
3771 			lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
3772 				bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
3773 	phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
3774 	phba->sli4_hba.link_state.topology =
3775 				bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
3776 	phba->sli4_hba.link_state.status =
3777 				bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
3778 	phba->sli4_hba.link_state.type =
3779 				bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
3780 	phba->sli4_hba.link_state.number =
3781 				bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
3782 	phba->sli4_hba.link_state.fault =
3783 				bf_get(lpfc_acqe_link_fault, acqe_fc);
3784 	phba->sli4_hba.link_state.logical_speed =
3785 				bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
3786 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3787 			"2896 Async FC event - Speed:%dGBaud Topology:x%x "
3788 			"LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
3789 			"%dMbps Fault:%d\n",
3790 			phba->sli4_hba.link_state.speed,
3791 			phba->sli4_hba.link_state.topology,
3792 			phba->sli4_hba.link_state.status,
3793 			phba->sli4_hba.link_state.type,
3794 			phba->sli4_hba.link_state.number,
3795 			phba->sli4_hba.link_state.logical_speed,
3796 			phba->sli4_hba.link_state.fault);
3797 	pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3798 	if (!pmb) {
3799 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3800 				"2897 The mboxq allocation failed\n");
3801 		return;
3802 	}
3803 	mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3804 	if (!mp) {
3805 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3806 				"2898 The lpfc_dmabuf allocation failed\n");
3807 		goto out_free_pmb;
3808 	}
3809 	mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3810 	if (!mp->virt) {
3811 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3812 				"2899 The mbuf allocation failed\n");
3813 		goto out_free_dmabuf;
3814 	}
3815 
3816 	/* Cleanup any outstanding ELS commands */
3817 	lpfc_els_flush_all_cmd(phba);
3818 
3819 	/* Block ELS IOCBs until we have done process link event */
3820 	phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3821 
3822 	/* Update link event statistics */
3823 	phba->sli.slistat.link_event++;
3824 
3825 	/* Create lpfc_handle_latt mailbox command from link ACQE */
3826 	lpfc_read_topology(phba, pmb, mp);
3827 	pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
3828 	pmb->vport = phba->pport;
3829 
3830 	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3831 	if (rc == MBX_NOT_FINISHED)
3832 		goto out_free_dmabuf;
3833 	return;
3834 
3835 out_free_dmabuf:
3836 	kfree(mp);
3837 out_free_pmb:
3838 	mempool_free(pmb, phba->mbox_mem_pool);
3839 }
3840 
3841 /**
3842  * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
3843  * @phba: pointer to lpfc hba data structure.
3844  * @acqe_fc: pointer to the async SLI completion queue entry.
3845  *
3846  * This routine is to handle the SLI4 asynchronous SLI events.
3847  **/
3848 static void
3849 lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
3850 {
3851 	char port_name;
3852 	char message[128];
3853 	uint8_t status;
3854 	struct lpfc_acqe_misconfigured_event *misconfigured;
3855 
3856 	/* special case misconfigured event as it contains data for all ports */
3857 	if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
3858 		 LPFC_SLI_INTF_IF_TYPE_2) ||
3859 		(bf_get(lpfc_trailer_type, acqe_sli) !=
3860 			LPFC_SLI_EVENT_TYPE_MISCONFIGURED)) {
3861 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3862 				"2901 Async SLI event - Event Data1:x%08x Event Data2:"
3863 				"x%08x SLI Event Type:%d\n",
3864 				acqe_sli->event_data1, acqe_sli->event_data2,
3865 				bf_get(lpfc_trailer_type, acqe_sli));
3866 		return;
3867 	}
3868 
3869 	port_name = phba->Port[0];
3870 	if (port_name == 0x00)
3871 		port_name = '?'; /* get port name is empty */
3872 
3873 	misconfigured = (struct lpfc_acqe_misconfigured_event *)
3874 					&acqe_sli->event_data1;
3875 
3876 	/* fetch the status for this port */
3877 	switch (phba->sli4_hba.lnk_info.lnk_no) {
3878 	case LPFC_LINK_NUMBER_0:
3879 		status = bf_get(lpfc_sli_misconfigured_port0,
3880 					&misconfigured->theEvent);
3881 		break;
3882 	case LPFC_LINK_NUMBER_1:
3883 		status = bf_get(lpfc_sli_misconfigured_port1,
3884 					&misconfigured->theEvent);
3885 		break;
3886 	case LPFC_LINK_NUMBER_2:
3887 		status = bf_get(lpfc_sli_misconfigured_port2,
3888 					&misconfigured->theEvent);
3889 		break;
3890 	case LPFC_LINK_NUMBER_3:
3891 		status = bf_get(lpfc_sli_misconfigured_port3,
3892 					&misconfigured->theEvent);
3893 		break;
3894 	default:
3895 		status = ~LPFC_SLI_EVENT_STATUS_VALID;
3896 		break;
3897 	}
3898 
3899 	switch (status) {
3900 	case LPFC_SLI_EVENT_STATUS_VALID:
3901 		return; /* no message if the sfp is okay */
3902 	case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
3903 		sprintf(message, "Optics faulted/incorrectly installed/not " \
3904 				"installed - Reseat optics, if issue not "
3905 				"resolved, replace.");
3906 		break;
3907 	case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
3908 		sprintf(message,
3909 			"Optics of two types installed - Remove one optic or " \
3910 			"install matching pair of optics.");
3911 		break;
3912 	case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
3913 		sprintf(message, "Incompatible optics - Replace with " \
3914 				"compatible optics for card to function.");
3915 		break;
3916 	default:
3917 		/* firmware is reporting a status we don't know about */
3918 		sprintf(message, "Unknown event status x%02x", status);
3919 		break;
3920 	}
3921 
3922 	lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3923 			"3176 Misconfigured Physical Port - "
3924 			"Port Name %c %s\n", port_name, message);
3925 }
3926 
3927 /**
3928  * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
3929  * @vport: pointer to vport data structure.
3930  *
3931  * This routine is to perform Clear Virtual Link (CVL) on a vport in
3932  * response to a CVL event.
3933  *
3934  * Return the pointer to the ndlp with the vport if successful, otherwise
3935  * return NULL.
3936  **/
3937 static struct lpfc_nodelist *
3938 lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
3939 {
3940 	struct lpfc_nodelist *ndlp;
3941 	struct Scsi_Host *shost;
3942 	struct lpfc_hba *phba;
3943 
3944 	if (!vport)
3945 		return NULL;
3946 	phba = vport->phba;
3947 	if (!phba)
3948 		return NULL;
3949 	ndlp = lpfc_findnode_did(vport, Fabric_DID);
3950 	if (!ndlp) {
3951 		/* Cannot find existing Fabric ndlp, so allocate a new one */
3952 		ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
3953 		if (!ndlp)
3954 			return 0;
3955 		lpfc_nlp_init(vport, ndlp, Fabric_DID);
3956 		/* Set the node type */
3957 		ndlp->nlp_type |= NLP_FABRIC;
3958 		/* Put ndlp onto node list */
3959 		lpfc_enqueue_node(vport, ndlp);
3960 	} else if (!NLP_CHK_NODE_ACT(ndlp)) {
3961 		/* re-setup ndlp without removing from node list */
3962 		ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
3963 		if (!ndlp)
3964 			return 0;
3965 	}
3966 	if ((phba->pport->port_state < LPFC_FLOGI) &&
3967 		(phba->pport->port_state != LPFC_VPORT_FAILED))
3968 		return NULL;
3969 	/* If virtual link is not yet instantiated ignore CVL */
3970 	if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
3971 		&& (vport->port_state != LPFC_VPORT_FAILED))
3972 		return NULL;
3973 	shost = lpfc_shost_from_vport(vport);
3974 	if (!shost)
3975 		return NULL;
3976 	lpfc_linkdown_port(vport);
3977 	lpfc_cleanup_pending_mbox(vport);
3978 	spin_lock_irq(shost->host_lock);
3979 	vport->fc_flag |= FC_VPORT_CVL_RCVD;
3980 	spin_unlock_irq(shost->host_lock);
3981 
3982 	return ndlp;
3983 }
3984 
3985 /**
3986  * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
3987  * @vport: pointer to lpfc hba data structure.
3988  *
3989  * This routine is to perform Clear Virtual Link (CVL) on all vports in
3990  * response to a FCF dead event.
3991  **/
3992 static void
3993 lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
3994 {
3995 	struct lpfc_vport **vports;
3996 	int i;
3997 
3998 	vports = lpfc_create_vport_work_array(phba);
3999 	if (vports)
4000 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
4001 			lpfc_sli4_perform_vport_cvl(vports[i]);
4002 	lpfc_destroy_vport_work_array(phba, vports);
4003 }
4004 
4005 /**
4006  * lpfc_sli4_perform_inuse_fcf_recovery - Perform inuse fcf recovery
4007  * @vport: pointer to lpfc hba data structure.
4008  *
4009  * This routine is to perform FCF recovery when the in-use FCF either dead or
4010  * got modified.
4011  **/
4012 static void
4013 lpfc_sli4_perform_inuse_fcf_recovery(struct lpfc_hba *phba,
4014 				     struct lpfc_acqe_fip *acqe_fip)
4015 {
4016 	int rc;
4017 
4018 	spin_lock_irq(&phba->hbalock);
4019 	/* Mark the fast failover process in progress */
4020 	phba->fcf.fcf_flag |= FCF_DEAD_DISC;
4021 	spin_unlock_irq(&phba->hbalock);
4022 
4023 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
4024 			"2771 Start FCF fast failover process due to in-use "
4025 			"FCF DEAD/MODIFIED event: evt_tag:x%x, index:x%x\n",
4026 			acqe_fip->event_tag, acqe_fip->index);
4027 	rc = lpfc_sli4_redisc_fcf_table(phba);
4028 	if (rc) {
4029 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4030 				"2772 Issue FCF rediscover mabilbox command "
4031 				"failed, fail through to FCF dead event\n");
4032 		spin_lock_irq(&phba->hbalock);
4033 		phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
4034 		spin_unlock_irq(&phba->hbalock);
4035 		/*
4036 		 * Last resort will fail over by treating this as a link
4037 		 * down to FCF registration.
4038 		 */
4039 		lpfc_sli4_fcf_dead_failthrough(phba);
4040 	} else {
4041 		/* Reset FCF roundrobin bmask for new discovery */
4042 		lpfc_sli4_clear_fcf_rr_bmask(phba);
4043 		/*
4044 		 * Handling fast FCF failover to a DEAD FCF event is
4045 		 * considered equalivant to receiving CVL to all vports.
4046 		 */
4047 		lpfc_sli4_perform_all_vport_cvl(phba);
4048 	}
4049 }
4050 
4051 /**
4052  * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
4053  * @phba: pointer to lpfc hba data structure.
4054  * @acqe_link: pointer to the async fcoe completion queue entry.
4055  *
4056  * This routine is to handle the SLI4 asynchronous fcoe event.
4057  **/
4058 static void
4059 lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
4060 			struct lpfc_acqe_fip *acqe_fip)
4061 {
4062 	uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
4063 	int rc;
4064 	struct lpfc_vport *vport;
4065 	struct lpfc_nodelist *ndlp;
4066 	struct Scsi_Host  *shost;
4067 	int active_vlink_present;
4068 	struct lpfc_vport **vports;
4069 	int i;
4070 
4071 	phba->fc_eventTag = acqe_fip->event_tag;
4072 	phba->fcoe_eventtag = acqe_fip->event_tag;
4073 	switch (event_type) {
4074 	case LPFC_FIP_EVENT_TYPE_NEW_FCF:
4075 	case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
4076 		if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
4077 			lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4078 					LOG_DISCOVERY,
4079 					"2546 New FCF event, evt_tag:x%x, "
4080 					"index:x%x\n",
4081 					acqe_fip->event_tag,
4082 					acqe_fip->index);
4083 		else
4084 			lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
4085 					LOG_DISCOVERY,
4086 					"2788 FCF param modified event, "
4087 					"evt_tag:x%x, index:x%x\n",
4088 					acqe_fip->event_tag,
4089 					acqe_fip->index);
4090 		if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
4091 			/*
4092 			 * During period of FCF discovery, read the FCF
4093 			 * table record indexed by the event to update
4094 			 * FCF roundrobin failover eligible FCF bmask.
4095 			 */
4096 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4097 					LOG_DISCOVERY,
4098 					"2779 Read FCF (x%x) for updating "
4099 					"roundrobin FCF failover bmask\n",
4100 					acqe_fip->index);
4101 			rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
4102 		}
4103 
4104 		/* If the FCF discovery is in progress, do nothing. */
4105 		spin_lock_irq(&phba->hbalock);
4106 		if (phba->hba_flag & FCF_TS_INPROG) {
4107 			spin_unlock_irq(&phba->hbalock);
4108 			break;
4109 		}
4110 		/* If fast FCF failover rescan event is pending, do nothing */
4111 		if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
4112 			spin_unlock_irq(&phba->hbalock);
4113 			break;
4114 		}
4115 
4116 		/* If FCF has been in discovered state, perform rediscovery
4117 		 * only if the FCF with the same index of the in-use FCF got
4118 		 * modified during normal operation. Otherwise, do nothing.
4119 		 */
4120 		if (phba->pport->port_state > LPFC_FLOGI) {
4121 			spin_unlock_irq(&phba->hbalock);
4122 			if (phba->fcf.current_rec.fcf_indx ==
4123 			    acqe_fip->index) {
4124 				lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
4125 						"3300 In-use FCF (%d) "
4126 						"modified, perform FCF "
4127 						"rediscovery\n",
4128 						acqe_fip->index);
4129 				lpfc_sli4_perform_inuse_fcf_recovery(phba,
4130 								     acqe_fip);
4131 			}
4132 			break;
4133 		}
4134 		spin_unlock_irq(&phba->hbalock);
4135 
4136 		/* Otherwise, scan the entire FCF table and re-discover SAN */
4137 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
4138 				"2770 Start FCF table scan per async FCF "
4139 				"event, evt_tag:x%x, index:x%x\n",
4140 				acqe_fip->event_tag, acqe_fip->index);
4141 		rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
4142 						     LPFC_FCOE_FCF_GET_FIRST);
4143 		if (rc)
4144 			lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4145 					"2547 Issue FCF scan read FCF mailbox "
4146 					"command failed (x%x)\n", rc);
4147 		break;
4148 
4149 	case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
4150 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4151 			"2548 FCF Table full count 0x%x tag 0x%x\n",
4152 			bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
4153 			acqe_fip->event_tag);
4154 		break;
4155 
4156 	case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
4157 		phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
4158 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4159 			"2549 FCF (x%x) disconnected from network, "
4160 			"tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
4161 		/*
4162 		 * If we are in the middle of FCF failover process, clear
4163 		 * the corresponding FCF bit in the roundrobin bitmap.
4164 		 */
4165 		spin_lock_irq(&phba->hbalock);
4166 		if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
4167 			spin_unlock_irq(&phba->hbalock);
4168 			/* Update FLOGI FCF failover eligible FCF bmask */
4169 			lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
4170 			break;
4171 		}
4172 		spin_unlock_irq(&phba->hbalock);
4173 
4174 		/* If the event is not for currently used fcf do nothing */
4175 		if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
4176 			break;
4177 
4178 		/*
4179 		 * Otherwise, request the port to rediscover the entire FCF
4180 		 * table for a fast recovery from case that the current FCF
4181 		 * is no longer valid as we are not in the middle of FCF
4182 		 * failover process already.
4183 		 */
4184 		lpfc_sli4_perform_inuse_fcf_recovery(phba, acqe_fip);
4185 		break;
4186 	case LPFC_FIP_EVENT_TYPE_CVL:
4187 		phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
4188 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4189 			"2718 Clear Virtual Link Received for VPI 0x%x"
4190 			" tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
4191 
4192 		vport = lpfc_find_vport_by_vpid(phba,
4193 						acqe_fip->index);
4194 		ndlp = lpfc_sli4_perform_vport_cvl(vport);
4195 		if (!ndlp)
4196 			break;
4197 		active_vlink_present = 0;
4198 
4199 		vports = lpfc_create_vport_work_array(phba);
4200 		if (vports) {
4201 			for (i = 0; i <= phba->max_vports && vports[i] != NULL;
4202 					i++) {
4203 				if ((!(vports[i]->fc_flag &
4204 					FC_VPORT_CVL_RCVD)) &&
4205 					(vports[i]->port_state > LPFC_FDISC)) {
4206 					active_vlink_present = 1;
4207 					break;
4208 				}
4209 			}
4210 			lpfc_destroy_vport_work_array(phba, vports);
4211 		}
4212 
4213 		if (active_vlink_present) {
4214 			/*
4215 			 * If there are other active VLinks present,
4216 			 * re-instantiate the Vlink using FDISC.
4217 			 */
4218 			mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
4219 			shost = lpfc_shost_from_vport(vport);
4220 			spin_lock_irq(shost->host_lock);
4221 			ndlp->nlp_flag |= NLP_DELAY_TMO;
4222 			spin_unlock_irq(shost->host_lock);
4223 			ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
4224 			vport->port_state = LPFC_FDISC;
4225 		} else {
4226 			/*
4227 			 * Otherwise, we request port to rediscover
4228 			 * the entire FCF table for a fast recovery
4229 			 * from possible case that the current FCF
4230 			 * is no longer valid if we are not already
4231 			 * in the FCF failover process.
4232 			 */
4233 			spin_lock_irq(&phba->hbalock);
4234 			if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
4235 				spin_unlock_irq(&phba->hbalock);
4236 				break;
4237 			}
4238 			/* Mark the fast failover process in progress */
4239 			phba->fcf.fcf_flag |= FCF_ACVL_DISC;
4240 			spin_unlock_irq(&phba->hbalock);
4241 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4242 					LOG_DISCOVERY,
4243 					"2773 Start FCF failover per CVL, "
4244 					"evt_tag:x%x\n", acqe_fip->event_tag);
4245 			rc = lpfc_sli4_redisc_fcf_table(phba);
4246 			if (rc) {
4247 				lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4248 						LOG_DISCOVERY,
4249 						"2774 Issue FCF rediscover "
4250 						"mabilbox command failed, "
4251 						"through to CVL event\n");
4252 				spin_lock_irq(&phba->hbalock);
4253 				phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
4254 				spin_unlock_irq(&phba->hbalock);
4255 				/*
4256 				 * Last resort will be re-try on the
4257 				 * the current registered FCF entry.
4258 				 */
4259 				lpfc_retry_pport_discovery(phba);
4260 			} else
4261 				/*
4262 				 * Reset FCF roundrobin bmask for new
4263 				 * discovery.
4264 				 */
4265 				lpfc_sli4_clear_fcf_rr_bmask(phba);
4266 		}
4267 		break;
4268 	default:
4269 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4270 			"0288 Unknown FCoE event type 0x%x event tag "
4271 			"0x%x\n", event_type, acqe_fip->event_tag);
4272 		break;
4273 	}
4274 }
4275 
4276 /**
4277  * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
4278  * @phba: pointer to lpfc hba data structure.
4279  * @acqe_link: pointer to the async dcbx completion queue entry.
4280  *
4281  * This routine is to handle the SLI4 asynchronous dcbx event.
4282  **/
4283 static void
4284 lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
4285 			 struct lpfc_acqe_dcbx *acqe_dcbx)
4286 {
4287 	phba->fc_eventTag = acqe_dcbx->event_tag;
4288 	lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4289 			"0290 The SLI4 DCBX asynchronous event is not "
4290 			"handled yet\n");
4291 }
4292 
4293 /**
4294  * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
4295  * @phba: pointer to lpfc hba data structure.
4296  * @acqe_link: pointer to the async grp5 completion queue entry.
4297  *
4298  * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
4299  * is an asynchronous notified of a logical link speed change.  The Port
4300  * reports the logical link speed in units of 10Mbps.
4301  **/
4302 static void
4303 lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
4304 			 struct lpfc_acqe_grp5 *acqe_grp5)
4305 {
4306 	uint16_t prev_ll_spd;
4307 
4308 	phba->fc_eventTag = acqe_grp5->event_tag;
4309 	phba->fcoe_eventtag = acqe_grp5->event_tag;
4310 	prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
4311 	phba->sli4_hba.link_state.logical_speed =
4312 		(bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5)) * 10;
4313 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4314 			"2789 GRP5 Async Event: Updating logical link speed "
4315 			"from %dMbps to %dMbps\n", prev_ll_spd,
4316 			phba->sli4_hba.link_state.logical_speed);
4317 }
4318 
4319 /**
4320  * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
4321  * @phba: pointer to lpfc hba data structure.
4322  *
4323  * This routine is invoked by the worker thread to process all the pending
4324  * SLI4 asynchronous events.
4325  **/
4326 void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
4327 {
4328 	struct lpfc_cq_event *cq_event;
4329 
4330 	/* First, declare the async event has been handled */
4331 	spin_lock_irq(&phba->hbalock);
4332 	phba->hba_flag &= ~ASYNC_EVENT;
4333 	spin_unlock_irq(&phba->hbalock);
4334 	/* Now, handle all the async events */
4335 	while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
4336 		/* Get the first event from the head of the event queue */
4337 		spin_lock_irq(&phba->hbalock);
4338 		list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
4339 				 cq_event, struct lpfc_cq_event, list);
4340 		spin_unlock_irq(&phba->hbalock);
4341 		/* Process the asynchronous event */
4342 		switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
4343 		case LPFC_TRAILER_CODE_LINK:
4344 			lpfc_sli4_async_link_evt(phba,
4345 						 &cq_event->cqe.acqe_link);
4346 			break;
4347 		case LPFC_TRAILER_CODE_FCOE:
4348 			lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
4349 			break;
4350 		case LPFC_TRAILER_CODE_DCBX:
4351 			lpfc_sli4_async_dcbx_evt(phba,
4352 						 &cq_event->cqe.acqe_dcbx);
4353 			break;
4354 		case LPFC_TRAILER_CODE_GRP5:
4355 			lpfc_sli4_async_grp5_evt(phba,
4356 						 &cq_event->cqe.acqe_grp5);
4357 			break;
4358 		case LPFC_TRAILER_CODE_FC:
4359 			lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
4360 			break;
4361 		case LPFC_TRAILER_CODE_SLI:
4362 			lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
4363 			break;
4364 		default:
4365 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4366 					"1804 Invalid asynchrous event code: "
4367 					"x%x\n", bf_get(lpfc_trailer_code,
4368 					&cq_event->cqe.mcqe_cmpl));
4369 			break;
4370 		}
4371 		/* Free the completion event processed to the free pool */
4372 		lpfc_sli4_cq_event_release(phba, cq_event);
4373 	}
4374 }
4375 
4376 /**
4377  * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
4378  * @phba: pointer to lpfc hba data structure.
4379  *
4380  * This routine is invoked by the worker thread to process FCF table
4381  * rediscovery pending completion event.
4382  **/
4383 void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
4384 {
4385 	int rc;
4386 
4387 	spin_lock_irq(&phba->hbalock);
4388 	/* Clear FCF rediscovery timeout event */
4389 	phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
4390 	/* Clear driver fast failover FCF record flag */
4391 	phba->fcf.failover_rec.flag = 0;
4392 	/* Set state for FCF fast failover */
4393 	phba->fcf.fcf_flag |= FCF_REDISC_FOV;
4394 	spin_unlock_irq(&phba->hbalock);
4395 
4396 	/* Scan FCF table from the first entry to re-discover SAN */
4397 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
4398 			"2777 Start post-quiescent FCF table scan\n");
4399 	rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
4400 	if (rc)
4401 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4402 				"2747 Issue FCF scan read FCF mailbox "
4403 				"command failed 0x%x\n", rc);
4404 }
4405 
4406 /**
4407  * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
4408  * @phba: pointer to lpfc hba data structure.
4409  * @dev_grp: The HBA PCI-Device group number.
4410  *
4411  * This routine is invoked to set up the per HBA PCI-Device group function
4412  * API jump table entries.
4413  *
4414  * Return: 0 if success, otherwise -ENODEV
4415  **/
4416 int
4417 lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4418 {
4419 	int rc;
4420 
4421 	/* Set up lpfc PCI-device group */
4422 	phba->pci_dev_grp = dev_grp;
4423 
4424 	/* The LPFC_PCI_DEV_OC uses SLI4 */
4425 	if (dev_grp == LPFC_PCI_DEV_OC)
4426 		phba->sli_rev = LPFC_SLI_REV4;
4427 
4428 	/* Set up device INIT API function jump table */
4429 	rc = lpfc_init_api_table_setup(phba, dev_grp);
4430 	if (rc)
4431 		return -ENODEV;
4432 	/* Set up SCSI API function jump table */
4433 	rc = lpfc_scsi_api_table_setup(phba, dev_grp);
4434 	if (rc)
4435 		return -ENODEV;
4436 	/* Set up SLI API function jump table */
4437 	rc = lpfc_sli_api_table_setup(phba, dev_grp);
4438 	if (rc)
4439 		return -ENODEV;
4440 	/* Set up MBOX API function jump table */
4441 	rc = lpfc_mbox_api_table_setup(phba, dev_grp);
4442 	if (rc)
4443 		return -ENODEV;
4444 
4445 	return 0;
4446 }
4447 
4448 /**
4449  * lpfc_log_intr_mode - Log the active interrupt mode
4450  * @phba: pointer to lpfc hba data structure.
4451  * @intr_mode: active interrupt mode adopted.
4452  *
4453  * This routine it invoked to log the currently used active interrupt mode
4454  * to the device.
4455  **/
4456 static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
4457 {
4458 	switch (intr_mode) {
4459 	case 0:
4460 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4461 				"0470 Enable INTx interrupt mode.\n");
4462 		break;
4463 	case 1:
4464 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4465 				"0481 Enabled MSI interrupt mode.\n");
4466 		break;
4467 	case 2:
4468 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4469 				"0480 Enabled MSI-X interrupt mode.\n");
4470 		break;
4471 	default:
4472 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4473 				"0482 Illegal interrupt mode.\n");
4474 		break;
4475 	}
4476 	return;
4477 }
4478 
4479 /**
4480  * lpfc_enable_pci_dev - Enable a generic PCI device.
4481  * @phba: pointer to lpfc hba data structure.
4482  *
4483  * This routine is invoked to enable the PCI device that is common to all
4484  * PCI devices.
4485  *
4486  * Return codes
4487  * 	0 - successful
4488  * 	other values - error
4489  **/
4490 static int
4491 lpfc_enable_pci_dev(struct lpfc_hba *phba)
4492 {
4493 	struct pci_dev *pdev;
4494 	int bars = 0;
4495 
4496 	/* Obtain PCI device reference */
4497 	if (!phba->pcidev)
4498 		goto out_error;
4499 	else
4500 		pdev = phba->pcidev;
4501 	/* Select PCI BARs */
4502 	bars = pci_select_bars(pdev, IORESOURCE_MEM);
4503 	/* Enable PCI device */
4504 	if (pci_enable_device_mem(pdev))
4505 		goto out_error;
4506 	/* Request PCI resource for the device */
4507 	if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
4508 		goto out_disable_device;
4509 	/* Set up device as PCI master and save state for EEH */
4510 	pci_set_master(pdev);
4511 	pci_try_set_mwi(pdev);
4512 	pci_save_state(pdev);
4513 
4514 	/* PCIe EEH recovery on powerpc platforms needs fundamental reset */
4515 	if (pci_find_capability(pdev, PCI_CAP_ID_EXP))
4516 		pdev->needs_freset = 1;
4517 
4518 	return 0;
4519 
4520 out_disable_device:
4521 	pci_disable_device(pdev);
4522 out_error:
4523 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4524 			"1401 Failed to enable pci device, bars:x%x\n", bars);
4525 	return -ENODEV;
4526 }
4527 
4528 /**
4529  * lpfc_disable_pci_dev - Disable a generic PCI device.
4530  * @phba: pointer to lpfc hba data structure.
4531  *
4532  * This routine is invoked to disable the PCI device that is common to all
4533  * PCI devices.
4534  **/
4535 static void
4536 lpfc_disable_pci_dev(struct lpfc_hba *phba)
4537 {
4538 	struct pci_dev *pdev;
4539 	int bars;
4540 
4541 	/* Obtain PCI device reference */
4542 	if (!phba->pcidev)
4543 		return;
4544 	else
4545 		pdev = phba->pcidev;
4546 	/* Select PCI BARs */
4547 	bars = pci_select_bars(pdev, IORESOURCE_MEM);
4548 	/* Release PCI resource and disable PCI device */
4549 	pci_release_selected_regions(pdev, bars);
4550 	pci_disable_device(pdev);
4551 	/* Null out PCI private reference to driver */
4552 	pci_set_drvdata(pdev, NULL);
4553 
4554 	return;
4555 }
4556 
4557 /**
4558  * lpfc_reset_hba - Reset a hba
4559  * @phba: pointer to lpfc hba data structure.
4560  *
4561  * This routine is invoked to reset a hba device. It brings the HBA
4562  * offline, performs a board restart, and then brings the board back
4563  * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
4564  * on outstanding mailbox commands.
4565  **/
4566 void
4567 lpfc_reset_hba(struct lpfc_hba *phba)
4568 {
4569 	/* If resets are disabled then set error state and return. */
4570 	if (!phba->cfg_enable_hba_reset) {
4571 		phba->link_state = LPFC_HBA_ERROR;
4572 		return;
4573 	}
4574 	lpfc_offline_prep(phba, LPFC_MBX_WAIT);
4575 	lpfc_offline(phba);
4576 	lpfc_sli_brdrestart(phba);
4577 	lpfc_online(phba);
4578 	lpfc_unblock_mgmt_io(phba);
4579 }
4580 
4581 /**
4582  * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
4583  * @phba: pointer to lpfc hba data structure.
4584  *
4585  * This function enables the PCI SR-IOV virtual functions to a physical
4586  * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4587  * enable the number of virtual functions to the physical function. As
4588  * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4589  * API call does not considered as an error condition for most of the device.
4590  **/
4591 uint16_t
4592 lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
4593 {
4594 	struct pci_dev *pdev = phba->pcidev;
4595 	uint16_t nr_virtfn;
4596 	int pos;
4597 
4598 	pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
4599 	if (pos == 0)
4600 		return 0;
4601 
4602 	pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
4603 	return nr_virtfn;
4604 }
4605 
4606 /**
4607  * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
4608  * @phba: pointer to lpfc hba data structure.
4609  * @nr_vfn: number of virtual functions to be enabled.
4610  *
4611  * This function enables the PCI SR-IOV virtual functions to a physical
4612  * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4613  * enable the number of virtual functions to the physical function. As
4614  * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4615  * API call does not considered as an error condition for most of the device.
4616  **/
4617 int
4618 lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
4619 {
4620 	struct pci_dev *pdev = phba->pcidev;
4621 	uint16_t max_nr_vfn;
4622 	int rc;
4623 
4624 	max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
4625 	if (nr_vfn > max_nr_vfn) {
4626 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4627 				"3057 Requested vfs (%d) greater than "
4628 				"supported vfs (%d)", nr_vfn, max_nr_vfn);
4629 		return -EINVAL;
4630 	}
4631 
4632 	rc = pci_enable_sriov(pdev, nr_vfn);
4633 	if (rc) {
4634 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4635 				"2806 Failed to enable sriov on this device "
4636 				"with vfn number nr_vf:%d, rc:%d\n",
4637 				nr_vfn, rc);
4638 	} else
4639 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4640 				"2807 Successful enable sriov on this device "
4641 				"with vfn number nr_vf:%d\n", nr_vfn);
4642 	return rc;
4643 }
4644 
4645 /**
4646  * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
4647  * @phba: pointer to lpfc hba data structure.
4648  *
4649  * This routine is invoked to set up the driver internal resources specific to
4650  * support the SLI-3 HBA device it attached to.
4651  *
4652  * Return codes
4653  * 	0 - successful
4654  * 	other values - error
4655  **/
4656 static int
4657 lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
4658 {
4659 	struct lpfc_sli *psli;
4660 	int rc;
4661 
4662 	/*
4663 	 * Initialize timers used by driver
4664 	 */
4665 
4666 	/* Heartbeat timer */
4667 	init_timer(&phba->hb_tmofunc);
4668 	phba->hb_tmofunc.function = lpfc_hb_timeout;
4669 	phba->hb_tmofunc.data = (unsigned long)phba;
4670 
4671 	psli = &phba->sli;
4672 	/* MBOX heartbeat timer */
4673 	init_timer(&psli->mbox_tmo);
4674 	psli->mbox_tmo.function = lpfc_mbox_timeout;
4675 	psli->mbox_tmo.data = (unsigned long) phba;
4676 	/* FCP polling mode timer */
4677 	init_timer(&phba->fcp_poll_timer);
4678 	phba->fcp_poll_timer.function = lpfc_poll_timeout;
4679 	phba->fcp_poll_timer.data = (unsigned long) phba;
4680 	/* Fabric block timer */
4681 	init_timer(&phba->fabric_block_timer);
4682 	phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4683 	phba->fabric_block_timer.data = (unsigned long) phba;
4684 	/* EA polling mode timer */
4685 	init_timer(&phba->eratt_poll);
4686 	phba->eratt_poll.function = lpfc_poll_eratt;
4687 	phba->eratt_poll.data = (unsigned long) phba;
4688 
4689 	/* Host attention work mask setup */
4690 	phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
4691 	phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
4692 
4693 	/* Get all the module params for configuring this host */
4694 	lpfc_get_cfgparam(phba);
4695 	if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
4696 		phba->menlo_flag |= HBA_MENLO_SUPPORT;
4697 		/* check for menlo minimum sg count */
4698 		if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
4699 			phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
4700 	}
4701 
4702 	if (!phba->sli.ring)
4703 		phba->sli.ring = (struct lpfc_sli_ring *)
4704 			kzalloc(LPFC_SLI3_MAX_RING *
4705 			sizeof(struct lpfc_sli_ring), GFP_KERNEL);
4706 	if (!phba->sli.ring)
4707 		return -ENOMEM;
4708 
4709 	/*
4710 	 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
4711 	 * used to create the sg_dma_buf_pool must be dynamically calculated.
4712 	 * 2 segments are added since the IOCB needs a command and response bde.
4713 	 */
4714 	phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4715 		sizeof(struct fcp_rsp) +
4716 			((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
4717 
4718 	if (phba->cfg_enable_bg) {
4719 		phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT;
4720 		phba->cfg_sg_dma_buf_size +=
4721 			phba->cfg_prot_sg_seg_cnt * sizeof(struct ulp_bde64);
4722 	}
4723 
4724 	/* Also reinitialize the host templates with new values. */
4725 	lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4726 	lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4727 
4728 	phba->max_vpi = LPFC_MAX_VPI;
4729 	/* This will be set to correct value after config_port mbox */
4730 	phba->max_vports = 0;
4731 
4732 	/*
4733 	 * Initialize the SLI Layer to run with lpfc HBAs.
4734 	 */
4735 	lpfc_sli_setup(phba);
4736 	lpfc_sli_queue_setup(phba);
4737 
4738 	/* Allocate device driver memory */
4739 	if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
4740 		return -ENOMEM;
4741 
4742 	/*
4743 	 * Enable sr-iov virtual functions if supported and configured
4744 	 * through the module parameter.
4745 	 */
4746 	if (phba->cfg_sriov_nr_virtfn > 0) {
4747 		rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
4748 						 phba->cfg_sriov_nr_virtfn);
4749 		if (rc) {
4750 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4751 					"2808 Requested number of SR-IOV "
4752 					"virtual functions (%d) is not "
4753 					"supported\n",
4754 					phba->cfg_sriov_nr_virtfn);
4755 			phba->cfg_sriov_nr_virtfn = 0;
4756 		}
4757 	}
4758 
4759 	return 0;
4760 }
4761 
4762 /**
4763  * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
4764  * @phba: pointer to lpfc hba data structure.
4765  *
4766  * This routine is invoked to unset the driver internal resources set up
4767  * specific for supporting the SLI-3 HBA device it attached to.
4768  **/
4769 static void
4770 lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
4771 {
4772 	/* Free device driver memory allocated */
4773 	lpfc_mem_free_all(phba);
4774 
4775 	return;
4776 }
4777 
4778 /**
4779  * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
4780  * @phba: pointer to lpfc hba data structure.
4781  *
4782  * This routine is invoked to set up the driver internal resources specific to
4783  * support the SLI-4 HBA device it attached to.
4784  *
4785  * Return codes
4786  * 	0 - successful
4787  * 	other values - error
4788  **/
4789 static int
4790 lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
4791 {
4792 	struct lpfc_sli *psli;
4793 	LPFC_MBOXQ_t *mboxq;
4794 	int rc, i, hbq_count, buf_size, dma_buf_size, max_buf_size;
4795 	uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
4796 	struct lpfc_mqe *mqe;
4797 	int longs, sli_family;
4798 	int sges_per_segment;
4799 
4800 	/* Before proceed, wait for POST done and device ready */
4801 	rc = lpfc_sli4_post_status_check(phba);
4802 	if (rc)
4803 		return -ENODEV;
4804 
4805 	/*
4806 	 * Initialize timers used by driver
4807 	 */
4808 
4809 	/* Heartbeat timer */
4810 	init_timer(&phba->hb_tmofunc);
4811 	phba->hb_tmofunc.function = lpfc_hb_timeout;
4812 	phba->hb_tmofunc.data = (unsigned long)phba;
4813 	init_timer(&phba->rrq_tmr);
4814 	phba->rrq_tmr.function = lpfc_rrq_timeout;
4815 	phba->rrq_tmr.data = (unsigned long)phba;
4816 
4817 	psli = &phba->sli;
4818 	/* MBOX heartbeat timer */
4819 	init_timer(&psli->mbox_tmo);
4820 	psli->mbox_tmo.function = lpfc_mbox_timeout;
4821 	psli->mbox_tmo.data = (unsigned long) phba;
4822 	/* Fabric block timer */
4823 	init_timer(&phba->fabric_block_timer);
4824 	phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4825 	phba->fabric_block_timer.data = (unsigned long) phba;
4826 	/* EA polling mode timer */
4827 	init_timer(&phba->eratt_poll);
4828 	phba->eratt_poll.function = lpfc_poll_eratt;
4829 	phba->eratt_poll.data = (unsigned long) phba;
4830 	/* FCF rediscover timer */
4831 	init_timer(&phba->fcf.redisc_wait);
4832 	phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
4833 	phba->fcf.redisc_wait.data = (unsigned long)phba;
4834 
4835 	/*
4836 	 * Control structure for handling external multi-buffer mailbox
4837 	 * command pass-through.
4838 	 */
4839 	memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
4840 		sizeof(struct lpfc_mbox_ext_buf_ctx));
4841 	INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
4842 
4843 	/*
4844 	 * We need to do a READ_CONFIG mailbox command here before
4845 	 * calling lpfc_get_cfgparam. For VFs this will report the
4846 	 * MAX_XRI, MAX_VPI, MAX_RPI, MAX_IOCB, and MAX_VFI settings.
4847 	 * All of the resources allocated
4848 	 * for this Port are tied to these values.
4849 	 */
4850 	/* Get all the module params for configuring this host */
4851 	lpfc_get_cfgparam(phba);
4852 	phba->max_vpi = LPFC_MAX_VPI;
4853 
4854 	/* Eventually cfg_fcp_eq_count / cfg_fcp_wq_count will be depricated */
4855 	phba->cfg_fcp_io_channel = phba->cfg_fcp_eq_count;
4856 
4857 	/* This will be set to correct value after the read_config mbox */
4858 	phba->max_vports = 0;
4859 
4860 	/* Program the default value of vlan_id and fc_map */
4861 	phba->valid_vlan = 0;
4862 	phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4863 	phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4864 	phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4865 
4866 	/* With BlockGuard we can have multiple SGEs per Data Segemnt */
4867 	sges_per_segment = 1;
4868 	if (phba->cfg_enable_bg)
4869 		sges_per_segment = 2;
4870 
4871 	/*
4872 	 * For SLI4, instead of using ring 0 (LPFC_FCP_RING) for FCP commands
4873 	 * we will associate a new ring, for each FCP fastpath EQ/CQ/WQ tuple.
4874 	 */
4875 	if (!phba->sli.ring)
4876 		phba->sli.ring = kzalloc(
4877 			(LPFC_SLI3_MAX_RING + phba->cfg_fcp_io_channel) *
4878 			sizeof(struct lpfc_sli_ring), GFP_KERNEL);
4879 	if (!phba->sli.ring)
4880 		return -ENOMEM;
4881 	/*
4882 	 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
4883 	 * used to create the sg_dma_buf_pool must be dynamically calculated.
4884 	 * 2 segments are added since the IOCB needs a command and response bde.
4885 	 * To insure that the scsi sgl does not cross a 4k page boundary only
4886 	 * sgl sizes of must be a power of 2.
4887 	 */
4888 	buf_size = (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp) +
4889 		    (((phba->cfg_sg_seg_cnt * sges_per_segment) + 2) *
4890 		    sizeof(struct sli4_sge)));
4891 
4892 	sli_family = bf_get(lpfc_sli_intf_sli_family, &phba->sli4_hba.sli_intf);
4893 	max_buf_size = LPFC_SLI4_MAX_BUF_SIZE;
4894 	switch (sli_family) {
4895 	case LPFC_SLI_INTF_FAMILY_BE2:
4896 	case LPFC_SLI_INTF_FAMILY_BE3:
4897 		/* There is a single hint for BE - 2 pages per BPL. */
4898 		if (bf_get(lpfc_sli_intf_sli_hint1, &phba->sli4_hba.sli_intf) ==
4899 		    LPFC_SLI_INTF_SLI_HINT1_1)
4900 			max_buf_size = LPFC_SLI4_FL1_MAX_BUF_SIZE;
4901 		break;
4902 	case LPFC_SLI_INTF_FAMILY_LNCR_A0:
4903 	case LPFC_SLI_INTF_FAMILY_LNCR_B0:
4904 	default:
4905 		break;
4906 	}
4907 
4908 	for (dma_buf_size = LPFC_SLI4_MIN_BUF_SIZE;
4909 	     dma_buf_size < max_buf_size && buf_size > dma_buf_size;
4910 	     dma_buf_size = dma_buf_size << 1)
4911 		;
4912 	if (dma_buf_size == max_buf_size)
4913 		phba->cfg_sg_seg_cnt = (dma_buf_size -
4914 			sizeof(struct fcp_cmnd) - sizeof(struct fcp_rsp) -
4915 			(2 * sizeof(struct sli4_sge))) /
4916 				sizeof(struct sli4_sge);
4917 	phba->cfg_sg_dma_buf_size = dma_buf_size;
4918 
4919 	/* Initialize buffer queue management fields */
4920 	hbq_count = lpfc_sli_hbq_count();
4921 	for (i = 0; i < hbq_count; ++i)
4922 		INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
4923 	INIT_LIST_HEAD(&phba->rb_pend_list);
4924 	phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
4925 	phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
4926 
4927 	/*
4928 	 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
4929 	 */
4930 	/* Initialize the Abort scsi buffer list used by driver */
4931 	spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
4932 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
4933 	/* This abort list used by worker thread */
4934 	spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
4935 
4936 	/*
4937 	 * Initialize driver internal slow-path work queues
4938 	 */
4939 
4940 	/* Driver internel slow-path CQ Event pool */
4941 	INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
4942 	/* Response IOCB work queue list */
4943 	INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
4944 	/* Asynchronous event CQ Event work queue list */
4945 	INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
4946 	/* Fast-path XRI aborted CQ Event work queue list */
4947 	INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
4948 	/* Slow-path XRI aborted CQ Event work queue list */
4949 	INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
4950 	/* Receive queue CQ Event work queue list */
4951 	INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
4952 
4953 	/* Initialize extent block lists. */
4954 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
4955 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
4956 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
4957 	INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
4958 
4959 	/* Initialize the driver internal SLI layer lists. */
4960 	lpfc_sli_setup(phba);
4961 	lpfc_sli_queue_setup(phba);
4962 
4963 	/* Allocate device driver memory */
4964 	rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
4965 	if (rc)
4966 		return -ENOMEM;
4967 
4968 	/* IF Type 2 ports get initialized now. */
4969 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
4970 	    LPFC_SLI_INTF_IF_TYPE_2) {
4971 		rc = lpfc_pci_function_reset(phba);
4972 		if (unlikely(rc))
4973 			return -ENODEV;
4974 	}
4975 
4976 	/* Create the bootstrap mailbox command */
4977 	rc = lpfc_create_bootstrap_mbox(phba);
4978 	if (unlikely(rc))
4979 		goto out_free_mem;
4980 
4981 	/* Set up the host's endian order with the device. */
4982 	rc = lpfc_setup_endian_order(phba);
4983 	if (unlikely(rc))
4984 		goto out_free_bsmbx;
4985 
4986 	/* Set up the hba's configuration parameters. */
4987 	rc = lpfc_sli4_read_config(phba);
4988 	if (unlikely(rc))
4989 		goto out_free_bsmbx;
4990 
4991 	/* IF Type 0 ports get initialized now. */
4992 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
4993 	    LPFC_SLI_INTF_IF_TYPE_0) {
4994 		rc = lpfc_pci_function_reset(phba);
4995 		if (unlikely(rc))
4996 			goto out_free_bsmbx;
4997 	}
4998 
4999 	mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
5000 						       GFP_KERNEL);
5001 	if (!mboxq) {
5002 		rc = -ENOMEM;
5003 		goto out_free_bsmbx;
5004 	}
5005 
5006 	/* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
5007 	lpfc_supported_pages(mboxq);
5008 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5009 	if (!rc) {
5010 		mqe = &mboxq->u.mqe;
5011 		memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
5012 		       LPFC_MAX_SUPPORTED_PAGES);
5013 		for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
5014 			switch (pn_page[i]) {
5015 			case LPFC_SLI4_PARAMETERS:
5016 				phba->sli4_hba.pc_sli4_params.supported = 1;
5017 				break;
5018 			default:
5019 				break;
5020 			}
5021 		}
5022 		/* Read the port's SLI4 Parameters capabilities if supported. */
5023 		if (phba->sli4_hba.pc_sli4_params.supported)
5024 			rc = lpfc_pc_sli4_params_get(phba, mboxq);
5025 		if (rc) {
5026 			mempool_free(mboxq, phba->mbox_mem_pool);
5027 			rc = -EIO;
5028 			goto out_free_bsmbx;
5029 		}
5030 	}
5031 	/*
5032 	 * Get sli4 parameters that override parameters from Port capabilities.
5033 	 * If this call fails, it isn't critical unless the SLI4 parameters come
5034 	 * back in conflict.
5035 	 */
5036 	rc = lpfc_get_sli4_parameters(phba, mboxq);
5037 	if (rc) {
5038 		if (phba->sli4_hba.extents_in_use &&
5039 		    phba->sli4_hba.rpi_hdrs_in_use) {
5040 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5041 				"2999 Unsupported SLI4 Parameters "
5042 				"Extents and RPI headers enabled.\n");
5043 			goto out_free_bsmbx;
5044 		}
5045 	}
5046 	mempool_free(mboxq, phba->mbox_mem_pool);
5047 	/* Verify all the SLI4 queues */
5048 	rc = lpfc_sli4_queue_verify(phba);
5049 	if (rc)
5050 		goto out_free_bsmbx;
5051 
5052 	/* Create driver internal CQE event pool */
5053 	rc = lpfc_sli4_cq_event_pool_create(phba);
5054 	if (rc)
5055 		goto out_free_bsmbx;
5056 
5057 	/* Initialize sgl lists per host */
5058 	lpfc_init_sgl_list(phba);
5059 
5060 	/* Allocate and initialize active sgl array */
5061 	rc = lpfc_init_active_sgl_array(phba);
5062 	if (rc) {
5063 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5064 				"1430 Failed to initialize sgl list.\n");
5065 		goto out_destroy_cq_event_pool;
5066 	}
5067 	rc = lpfc_sli4_init_rpi_hdrs(phba);
5068 	if (rc) {
5069 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5070 				"1432 Failed to initialize rpi headers.\n");
5071 		goto out_free_active_sgl;
5072 	}
5073 
5074 	/* Allocate eligible FCF bmask memory for FCF roundrobin failover */
5075 	longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
5076 	phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
5077 					 GFP_KERNEL);
5078 	if (!phba->fcf.fcf_rr_bmask) {
5079 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5080 				"2759 Failed allocate memory for FCF round "
5081 				"robin failover bmask\n");
5082 		rc = -ENOMEM;
5083 		goto out_remove_rpi_hdrs;
5084 	}
5085 
5086 	phba->sli4_hba.fcp_eq_hdl =
5087 			kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
5088 			    phba->cfg_fcp_io_channel), GFP_KERNEL);
5089 	if (!phba->sli4_hba.fcp_eq_hdl) {
5090 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5091 				"2572 Failed allocate memory for "
5092 				"fast-path per-EQ handle array\n");
5093 		rc = -ENOMEM;
5094 		goto out_free_fcf_rr_bmask;
5095 	}
5096 
5097 	phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
5098 				      phba->cfg_fcp_io_channel), GFP_KERNEL);
5099 	if (!phba->sli4_hba.msix_entries) {
5100 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5101 				"2573 Failed allocate memory for msi-x "
5102 				"interrupt vector entries\n");
5103 		rc = -ENOMEM;
5104 		goto out_free_fcp_eq_hdl;
5105 	}
5106 
5107 	/*
5108 	 * Enable sr-iov virtual functions if supported and configured
5109 	 * through the module parameter.
5110 	 */
5111 	if (phba->cfg_sriov_nr_virtfn > 0) {
5112 		rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
5113 						 phba->cfg_sriov_nr_virtfn);
5114 		if (rc) {
5115 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5116 					"3020 Requested number of SR-IOV "
5117 					"virtual functions (%d) is not "
5118 					"supported\n",
5119 					phba->cfg_sriov_nr_virtfn);
5120 			phba->cfg_sriov_nr_virtfn = 0;
5121 		}
5122 	}
5123 
5124 	return 0;
5125 
5126 out_free_fcp_eq_hdl:
5127 	kfree(phba->sli4_hba.fcp_eq_hdl);
5128 out_free_fcf_rr_bmask:
5129 	kfree(phba->fcf.fcf_rr_bmask);
5130 out_remove_rpi_hdrs:
5131 	lpfc_sli4_remove_rpi_hdrs(phba);
5132 out_free_active_sgl:
5133 	lpfc_free_active_sgl(phba);
5134 out_destroy_cq_event_pool:
5135 	lpfc_sli4_cq_event_pool_destroy(phba);
5136 out_free_bsmbx:
5137 	lpfc_destroy_bootstrap_mbox(phba);
5138 out_free_mem:
5139 	lpfc_mem_free(phba);
5140 	return rc;
5141 }
5142 
5143 /**
5144  * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
5145  * @phba: pointer to lpfc hba data structure.
5146  *
5147  * This routine is invoked to unset the driver internal resources set up
5148  * specific for supporting the SLI-4 HBA device it attached to.
5149  **/
5150 static void
5151 lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
5152 {
5153 	struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
5154 
5155 	/* Free memory allocated for msi-x interrupt vector entries */
5156 	kfree(phba->sli4_hba.msix_entries);
5157 
5158 	/* Free memory allocated for fast-path work queue handles */
5159 	kfree(phba->sli4_hba.fcp_eq_hdl);
5160 
5161 	/* Free the allocated rpi headers. */
5162 	lpfc_sli4_remove_rpi_hdrs(phba);
5163 	lpfc_sli4_remove_rpis(phba);
5164 
5165 	/* Free eligible FCF index bmask */
5166 	kfree(phba->fcf.fcf_rr_bmask);
5167 
5168 	/* Free the ELS sgl list */
5169 	lpfc_free_active_sgl(phba);
5170 	lpfc_free_els_sgl_list(phba);
5171 
5172 	/* Free the completion queue EQ event pool */
5173 	lpfc_sli4_cq_event_release_all(phba);
5174 	lpfc_sli4_cq_event_pool_destroy(phba);
5175 
5176 	/* Release resource identifiers. */
5177 	lpfc_sli4_dealloc_resource_identifiers(phba);
5178 
5179 	/* Free the bsmbx region. */
5180 	lpfc_destroy_bootstrap_mbox(phba);
5181 
5182 	/* Free the SLI Layer memory with SLI4 HBAs */
5183 	lpfc_mem_free_all(phba);
5184 
5185 	/* Free the current connect table */
5186 	list_for_each_entry_safe(conn_entry, next_conn_entry,
5187 		&phba->fcf_conn_rec_list, list) {
5188 		list_del_init(&conn_entry->list);
5189 		kfree(conn_entry);
5190 	}
5191 
5192 	return;
5193 }
5194 
5195 /**
5196  * lpfc_init_api_table_setup - Set up init api function jump table
5197  * @phba: The hba struct for which this call is being executed.
5198  * @dev_grp: The HBA PCI-Device group number.
5199  *
5200  * This routine sets up the device INIT interface API function jump table
5201  * in @phba struct.
5202  *
5203  * Returns: 0 - success, -ENODEV - failure.
5204  **/
5205 int
5206 lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5207 {
5208 	phba->lpfc_hba_init_link = lpfc_hba_init_link;
5209 	phba->lpfc_hba_down_link = lpfc_hba_down_link;
5210 	phba->lpfc_selective_reset = lpfc_selective_reset;
5211 	switch (dev_grp) {
5212 	case LPFC_PCI_DEV_LP:
5213 		phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
5214 		phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
5215 		phba->lpfc_stop_port = lpfc_stop_port_s3;
5216 		break;
5217 	case LPFC_PCI_DEV_OC:
5218 		phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
5219 		phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
5220 		phba->lpfc_stop_port = lpfc_stop_port_s4;
5221 		break;
5222 	default:
5223 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5224 				"1431 Invalid HBA PCI-device group: 0x%x\n",
5225 				dev_grp);
5226 		return -ENODEV;
5227 		break;
5228 	}
5229 	return 0;
5230 }
5231 
5232 /**
5233  * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
5234  * @phba: pointer to lpfc hba data structure.
5235  *
5236  * This routine is invoked to set up the driver internal resources before the
5237  * device specific resource setup to support the HBA device it attached to.
5238  *
5239  * Return codes
5240  *	0 - successful
5241  *	other values - error
5242  **/
5243 static int
5244 lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
5245 {
5246 	/*
5247 	 * Driver resources common to all SLI revisions
5248 	 */
5249 	atomic_set(&phba->fast_event_count, 0);
5250 	spin_lock_init(&phba->hbalock);
5251 
5252 	/* Initialize ndlp management spinlock */
5253 	spin_lock_init(&phba->ndlp_lock);
5254 
5255 	INIT_LIST_HEAD(&phba->port_list);
5256 	INIT_LIST_HEAD(&phba->work_list);
5257 	init_waitqueue_head(&phba->wait_4_mlo_m_q);
5258 
5259 	/* Initialize the wait queue head for the kernel thread */
5260 	init_waitqueue_head(&phba->work_waitq);
5261 
5262 	/* Initialize the scsi buffer list used by driver for scsi IO */
5263 	spin_lock_init(&phba->scsi_buf_list_lock);
5264 	INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
5265 
5266 	/* Initialize the fabric iocb list */
5267 	INIT_LIST_HEAD(&phba->fabric_iocb_list);
5268 
5269 	/* Initialize list to save ELS buffers */
5270 	INIT_LIST_HEAD(&phba->elsbuf);
5271 
5272 	/* Initialize FCF connection rec list */
5273 	INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
5274 
5275 	return 0;
5276 }
5277 
5278 /**
5279  * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
5280  * @phba: pointer to lpfc hba data structure.
5281  *
5282  * This routine is invoked to set up the driver internal resources after the
5283  * device specific resource setup to support the HBA device it attached to.
5284  *
5285  * Return codes
5286  * 	0 - successful
5287  * 	other values - error
5288  **/
5289 static int
5290 lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
5291 {
5292 	int error;
5293 
5294 	/* Startup the kernel thread for this host adapter. */
5295 	phba->worker_thread = kthread_run(lpfc_do_work, phba,
5296 					  "lpfc_worker_%d", phba->brd_no);
5297 	if (IS_ERR(phba->worker_thread)) {
5298 		error = PTR_ERR(phba->worker_thread);
5299 		return error;
5300 	}
5301 
5302 	return 0;
5303 }
5304 
5305 /**
5306  * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
5307  * @phba: pointer to lpfc hba data structure.
5308  *
5309  * This routine is invoked to unset the driver internal resources set up after
5310  * the device specific resource setup for supporting the HBA device it
5311  * attached to.
5312  **/
5313 static void
5314 lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
5315 {
5316 	/* Stop kernel worker thread */
5317 	kthread_stop(phba->worker_thread);
5318 }
5319 
5320 /**
5321  * lpfc_free_iocb_list - Free iocb list.
5322  * @phba: pointer to lpfc hba data structure.
5323  *
5324  * This routine is invoked to free the driver's IOCB list and memory.
5325  **/
5326 static void
5327 lpfc_free_iocb_list(struct lpfc_hba *phba)
5328 {
5329 	struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
5330 
5331 	spin_lock_irq(&phba->hbalock);
5332 	list_for_each_entry_safe(iocbq_entry, iocbq_next,
5333 				 &phba->lpfc_iocb_list, list) {
5334 		list_del(&iocbq_entry->list);
5335 		kfree(iocbq_entry);
5336 		phba->total_iocbq_bufs--;
5337 	}
5338 	spin_unlock_irq(&phba->hbalock);
5339 
5340 	return;
5341 }
5342 
5343 /**
5344  * lpfc_init_iocb_list - Allocate and initialize iocb list.
5345  * @phba: pointer to lpfc hba data structure.
5346  *
5347  * This routine is invoked to allocate and initizlize the driver's IOCB
5348  * list and set up the IOCB tag array accordingly.
5349  *
5350  * Return codes
5351  *	0 - successful
5352  *	other values - error
5353  **/
5354 static int
5355 lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
5356 {
5357 	struct lpfc_iocbq *iocbq_entry = NULL;
5358 	uint16_t iotag;
5359 	int i;
5360 
5361 	/* Initialize and populate the iocb list per host.  */
5362 	INIT_LIST_HEAD(&phba->lpfc_iocb_list);
5363 	for (i = 0; i < iocb_count; i++) {
5364 		iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
5365 		if (iocbq_entry == NULL) {
5366 			printk(KERN_ERR "%s: only allocated %d iocbs of "
5367 				"expected %d count. Unloading driver.\n",
5368 				__func__, i, LPFC_IOCB_LIST_CNT);
5369 			goto out_free_iocbq;
5370 		}
5371 
5372 		iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
5373 		if (iotag == 0) {
5374 			kfree(iocbq_entry);
5375 			printk(KERN_ERR "%s: failed to allocate IOTAG. "
5376 				"Unloading driver.\n", __func__);
5377 			goto out_free_iocbq;
5378 		}
5379 		iocbq_entry->sli4_lxritag = NO_XRI;
5380 		iocbq_entry->sli4_xritag = NO_XRI;
5381 
5382 		spin_lock_irq(&phba->hbalock);
5383 		list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
5384 		phba->total_iocbq_bufs++;
5385 		spin_unlock_irq(&phba->hbalock);
5386 	}
5387 
5388 	return 0;
5389 
5390 out_free_iocbq:
5391 	lpfc_free_iocb_list(phba);
5392 
5393 	return -ENOMEM;
5394 }
5395 
5396 /**
5397  * lpfc_free_sgl_list - Free a given sgl list.
5398  * @phba: pointer to lpfc hba data structure.
5399  * @sglq_list: pointer to the head of sgl list.
5400  *
5401  * This routine is invoked to free a give sgl list and memory.
5402  **/
5403 void
5404 lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
5405 {
5406 	struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
5407 
5408 	list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
5409 		list_del(&sglq_entry->list);
5410 		lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
5411 		kfree(sglq_entry);
5412 	}
5413 }
5414 
5415 /**
5416  * lpfc_free_els_sgl_list - Free els sgl list.
5417  * @phba: pointer to lpfc hba data structure.
5418  *
5419  * This routine is invoked to free the driver's els sgl list and memory.
5420  **/
5421 static void
5422 lpfc_free_els_sgl_list(struct lpfc_hba *phba)
5423 {
5424 	LIST_HEAD(sglq_list);
5425 
5426 	/* Retrieve all els sgls from driver list */
5427 	spin_lock_irq(&phba->hbalock);
5428 	list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
5429 	spin_unlock_irq(&phba->hbalock);
5430 
5431 	/* Now free the sgl list */
5432 	lpfc_free_sgl_list(phba, &sglq_list);
5433 }
5434 
5435 /**
5436  * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
5437  * @phba: pointer to lpfc hba data structure.
5438  *
5439  * This routine is invoked to allocate the driver's active sgl memory.
5440  * This array will hold the sglq_entry's for active IOs.
5441  **/
5442 static int
5443 lpfc_init_active_sgl_array(struct lpfc_hba *phba)
5444 {
5445 	int size;
5446 	size = sizeof(struct lpfc_sglq *);
5447 	size *= phba->sli4_hba.max_cfg_param.max_xri;
5448 
5449 	phba->sli4_hba.lpfc_sglq_active_list =
5450 		kzalloc(size, GFP_KERNEL);
5451 	if (!phba->sli4_hba.lpfc_sglq_active_list)
5452 		return -ENOMEM;
5453 	return 0;
5454 }
5455 
5456 /**
5457  * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
5458  * @phba: pointer to lpfc hba data structure.
5459  *
5460  * This routine is invoked to walk through the array of active sglq entries
5461  * and free all of the resources.
5462  * This is just a place holder for now.
5463  **/
5464 static void
5465 lpfc_free_active_sgl(struct lpfc_hba *phba)
5466 {
5467 	kfree(phba->sli4_hba.lpfc_sglq_active_list);
5468 }
5469 
5470 /**
5471  * lpfc_init_sgl_list - Allocate and initialize sgl list.
5472  * @phba: pointer to lpfc hba data structure.
5473  *
5474  * This routine is invoked to allocate and initizlize the driver's sgl
5475  * list and set up the sgl xritag tag array accordingly.
5476  *
5477  **/
5478 static void
5479 lpfc_init_sgl_list(struct lpfc_hba *phba)
5480 {
5481 	/* Initialize and populate the sglq list per host/VF. */
5482 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
5483 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
5484 
5485 	/* els xri-sgl book keeping */
5486 	phba->sli4_hba.els_xri_cnt = 0;
5487 
5488 	/* scsi xri-buffer book keeping */
5489 	phba->sli4_hba.scsi_xri_cnt = 0;
5490 }
5491 
5492 /**
5493  * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
5494  * @phba: pointer to lpfc hba data structure.
5495  *
5496  * This routine is invoked to post rpi header templates to the
5497  * port for those SLI4 ports that do not support extents.  This routine
5498  * posts a PAGE_SIZE memory region to the port to hold up to
5499  * PAGE_SIZE modulo 64 rpi context headers.  This is an initialization routine
5500  * and should be called only when interrupts are disabled.
5501  *
5502  * Return codes
5503  * 	0 - successful
5504  *	-ERROR - otherwise.
5505  **/
5506 int
5507 lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
5508 {
5509 	int rc = 0;
5510 	struct lpfc_rpi_hdr *rpi_hdr;
5511 
5512 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
5513 	if (!phba->sli4_hba.rpi_hdrs_in_use)
5514 		return rc;
5515 	if (phba->sli4_hba.extents_in_use)
5516 		return -EIO;
5517 
5518 	rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
5519 	if (!rpi_hdr) {
5520 		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5521 				"0391 Error during rpi post operation\n");
5522 		lpfc_sli4_remove_rpis(phba);
5523 		rc = -ENODEV;
5524 	}
5525 
5526 	return rc;
5527 }
5528 
5529 /**
5530  * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
5531  * @phba: pointer to lpfc hba data structure.
5532  *
5533  * This routine is invoked to allocate a single 4KB memory region to
5534  * support rpis and stores them in the phba.  This single region
5535  * provides support for up to 64 rpis.  The region is used globally
5536  * by the device.
5537  *
5538  * Returns:
5539  *   A valid rpi hdr on success.
5540  *   A NULL pointer on any failure.
5541  **/
5542 struct lpfc_rpi_hdr *
5543 lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
5544 {
5545 	uint16_t rpi_limit, curr_rpi_range;
5546 	struct lpfc_dmabuf *dmabuf;
5547 	struct lpfc_rpi_hdr *rpi_hdr;
5548 	uint32_t rpi_count;
5549 
5550 	/*
5551 	 * If the SLI4 port supports extents, posting the rpi header isn't
5552 	 * required.  Set the expected maximum count and let the actual value
5553 	 * get set when extents are fully allocated.
5554 	 */
5555 	if (!phba->sli4_hba.rpi_hdrs_in_use)
5556 		return NULL;
5557 	if (phba->sli4_hba.extents_in_use)
5558 		return NULL;
5559 
5560 	/* The limit on the logical index is just the max_rpi count. */
5561 	rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
5562 	phba->sli4_hba.max_cfg_param.max_rpi - 1;
5563 
5564 	spin_lock_irq(&phba->hbalock);
5565 	/*
5566 	 * Establish the starting RPI in this header block.  The starting
5567 	 * rpi is normalized to a zero base because the physical rpi is
5568 	 * port based.
5569 	 */
5570 	curr_rpi_range = phba->sli4_hba.next_rpi;
5571 	spin_unlock_irq(&phba->hbalock);
5572 
5573 	/*
5574 	 * The port has a limited number of rpis. The increment here
5575 	 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
5576 	 * and to allow the full max_rpi range per port.
5577 	 */
5578 	if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
5579 		rpi_count = rpi_limit - curr_rpi_range;
5580 	else
5581 		rpi_count = LPFC_RPI_HDR_COUNT;
5582 
5583 	if (!rpi_count)
5584 		return NULL;
5585 	/*
5586 	 * First allocate the protocol header region for the port.  The
5587 	 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
5588 	 */
5589 	dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5590 	if (!dmabuf)
5591 		return NULL;
5592 
5593 	dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5594 					  LPFC_HDR_TEMPLATE_SIZE,
5595 					  &dmabuf->phys,
5596 					  GFP_KERNEL);
5597 	if (!dmabuf->virt) {
5598 		rpi_hdr = NULL;
5599 		goto err_free_dmabuf;
5600 	}
5601 
5602 	memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
5603 	if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
5604 		rpi_hdr = NULL;
5605 		goto err_free_coherent;
5606 	}
5607 
5608 	/* Save the rpi header data for cleanup later. */
5609 	rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
5610 	if (!rpi_hdr)
5611 		goto err_free_coherent;
5612 
5613 	rpi_hdr->dmabuf = dmabuf;
5614 	rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
5615 	rpi_hdr->page_count = 1;
5616 	spin_lock_irq(&phba->hbalock);
5617 
5618 	/* The rpi_hdr stores the logical index only. */
5619 	rpi_hdr->start_rpi = curr_rpi_range;
5620 	list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
5621 
5622 	/*
5623 	 * The next_rpi stores the next logical module-64 rpi value used
5624 	 * to post physical rpis in subsequent rpi postings.
5625 	 */
5626 	phba->sli4_hba.next_rpi += rpi_count;
5627 	spin_unlock_irq(&phba->hbalock);
5628 	return rpi_hdr;
5629 
5630  err_free_coherent:
5631 	dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
5632 			  dmabuf->virt, dmabuf->phys);
5633  err_free_dmabuf:
5634 	kfree(dmabuf);
5635 	return NULL;
5636 }
5637 
5638 /**
5639  * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
5640  * @phba: pointer to lpfc hba data structure.
5641  *
5642  * This routine is invoked to remove all memory resources allocated
5643  * to support rpis for SLI4 ports not supporting extents. This routine
5644  * presumes the caller has released all rpis consumed by fabric or port
5645  * logins and is prepared to have the header pages removed.
5646  **/
5647 void
5648 lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
5649 {
5650 	struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
5651 
5652 	if (!phba->sli4_hba.rpi_hdrs_in_use)
5653 		goto exit;
5654 
5655 	list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
5656 				 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
5657 		list_del(&rpi_hdr->list);
5658 		dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
5659 				  rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
5660 		kfree(rpi_hdr->dmabuf);
5661 		kfree(rpi_hdr);
5662 	}
5663  exit:
5664 	/* There are no rpis available to the port now. */
5665 	phba->sli4_hba.next_rpi = 0;
5666 }
5667 
5668 /**
5669  * lpfc_hba_alloc - Allocate driver hba data structure for a device.
5670  * @pdev: pointer to pci device data structure.
5671  *
5672  * This routine is invoked to allocate the driver hba data structure for an
5673  * HBA device. If the allocation is successful, the phba reference to the
5674  * PCI device data structure is set.
5675  *
5676  * Return codes
5677  *      pointer to @phba - successful
5678  *      NULL - error
5679  **/
5680 static struct lpfc_hba *
5681 lpfc_hba_alloc(struct pci_dev *pdev)
5682 {
5683 	struct lpfc_hba *phba;
5684 
5685 	/* Allocate memory for HBA structure */
5686 	phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
5687 	if (!phba) {
5688 		dev_err(&pdev->dev, "failed to allocate hba struct\n");
5689 		return NULL;
5690 	}
5691 
5692 	/* Set reference to PCI device in HBA structure */
5693 	phba->pcidev = pdev;
5694 
5695 	/* Assign an unused board number */
5696 	phba->brd_no = lpfc_get_instance();
5697 	if (phba->brd_no < 0) {
5698 		kfree(phba);
5699 		return NULL;
5700 	}
5701 
5702 	spin_lock_init(&phba->ct_ev_lock);
5703 	INIT_LIST_HEAD(&phba->ct_ev_waiters);
5704 
5705 	return phba;
5706 }
5707 
5708 /**
5709  * lpfc_hba_free - Free driver hba data structure with a device.
5710  * @phba: pointer to lpfc hba data structure.
5711  *
5712  * This routine is invoked to free the driver hba data structure with an
5713  * HBA device.
5714  **/
5715 static void
5716 lpfc_hba_free(struct lpfc_hba *phba)
5717 {
5718 	/* Release the driver assigned board number */
5719 	idr_remove(&lpfc_hba_index, phba->brd_no);
5720 
5721 	/* Free memory allocated with sli rings */
5722 	kfree(phba->sli.ring);
5723 	phba->sli.ring = NULL;
5724 
5725 	kfree(phba);
5726 	return;
5727 }
5728 
5729 /**
5730  * lpfc_create_shost - Create hba physical port with associated scsi host.
5731  * @phba: pointer to lpfc hba data structure.
5732  *
5733  * This routine is invoked to create HBA physical port and associate a SCSI
5734  * host with it.
5735  *
5736  * Return codes
5737  *      0 - successful
5738  *      other values - error
5739  **/
5740 static int
5741 lpfc_create_shost(struct lpfc_hba *phba)
5742 {
5743 	struct lpfc_vport *vport;
5744 	struct Scsi_Host  *shost;
5745 
5746 	/* Initialize HBA FC structure */
5747 	phba->fc_edtov = FF_DEF_EDTOV;
5748 	phba->fc_ratov = FF_DEF_RATOV;
5749 	phba->fc_altov = FF_DEF_ALTOV;
5750 	phba->fc_arbtov = FF_DEF_ARBTOV;
5751 
5752 	atomic_set(&phba->sdev_cnt, 0);
5753 	vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
5754 	if (!vport)
5755 		return -ENODEV;
5756 
5757 	shost = lpfc_shost_from_vport(vport);
5758 	phba->pport = vport;
5759 	lpfc_debugfs_initialize(vport);
5760 	/* Put reference to SCSI host to driver's device private data */
5761 	pci_set_drvdata(phba->pcidev, shost);
5762 
5763 	return 0;
5764 }
5765 
5766 /**
5767  * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
5768  * @phba: pointer to lpfc hba data structure.
5769  *
5770  * This routine is invoked to destroy HBA physical port and the associated
5771  * SCSI host.
5772  **/
5773 static void
5774 lpfc_destroy_shost(struct lpfc_hba *phba)
5775 {
5776 	struct lpfc_vport *vport = phba->pport;
5777 
5778 	/* Destroy physical port that associated with the SCSI host */
5779 	destroy_port(vport);
5780 
5781 	return;
5782 }
5783 
5784 /**
5785  * lpfc_setup_bg - Setup Block guard structures and debug areas.
5786  * @phba: pointer to lpfc hba data structure.
5787  * @shost: the shost to be used to detect Block guard settings.
5788  *
5789  * This routine sets up the local Block guard protocol settings for @shost.
5790  * This routine also allocates memory for debugging bg buffers.
5791  **/
5792 static void
5793 lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
5794 {
5795 	uint32_t old_mask;
5796 	uint32_t old_guard;
5797 
5798 	int pagecnt = 10;
5799 	if (lpfc_prot_mask && lpfc_prot_guard) {
5800 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5801 				"1478 Registering BlockGuard with the "
5802 				"SCSI layer\n");
5803 
5804 		old_mask = lpfc_prot_mask;
5805 		old_guard = lpfc_prot_guard;
5806 
5807 		/* Only allow supported values */
5808 		lpfc_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
5809 			SHOST_DIX_TYPE0_PROTECTION |
5810 			SHOST_DIX_TYPE1_PROTECTION);
5811 		lpfc_prot_guard &= (SHOST_DIX_GUARD_IP | SHOST_DIX_GUARD_CRC);
5812 
5813 		/* DIF Type 1 protection for profiles AST1/C1 is end to end */
5814 		if (lpfc_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
5815 			lpfc_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
5816 
5817 		if (lpfc_prot_mask && lpfc_prot_guard) {
5818 			if ((old_mask != lpfc_prot_mask) ||
5819 				(old_guard != lpfc_prot_guard))
5820 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5821 					"1475 Registering BlockGuard with the "
5822 					"SCSI layer: mask %d  guard %d\n",
5823 					lpfc_prot_mask, lpfc_prot_guard);
5824 
5825 			scsi_host_set_prot(shost, lpfc_prot_mask);
5826 			scsi_host_set_guard(shost, lpfc_prot_guard);
5827 		} else
5828 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5829 				"1479 Not Registering BlockGuard with the SCSI "
5830 				"layer, Bad protection parameters: %d %d\n",
5831 				old_mask, old_guard);
5832 	}
5833 
5834 	if (!_dump_buf_data) {
5835 		while (pagecnt) {
5836 			spin_lock_init(&_dump_buf_lock);
5837 			_dump_buf_data =
5838 				(char *) __get_free_pages(GFP_KERNEL, pagecnt);
5839 			if (_dump_buf_data) {
5840 				lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5841 					"9043 BLKGRD: allocated %d pages for "
5842 				       "_dump_buf_data at 0x%p\n",
5843 				       (1 << pagecnt), _dump_buf_data);
5844 				_dump_buf_data_order = pagecnt;
5845 				memset(_dump_buf_data, 0,
5846 				       ((1 << PAGE_SHIFT) << pagecnt));
5847 				break;
5848 			} else
5849 				--pagecnt;
5850 		}
5851 		if (!_dump_buf_data_order)
5852 			lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5853 				"9044 BLKGRD: ERROR unable to allocate "
5854 			       "memory for hexdump\n");
5855 	} else
5856 		lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5857 			"9045 BLKGRD: already allocated _dump_buf_data=0x%p"
5858 		       "\n", _dump_buf_data);
5859 	if (!_dump_buf_dif) {
5860 		while (pagecnt) {
5861 			_dump_buf_dif =
5862 				(char *) __get_free_pages(GFP_KERNEL, pagecnt);
5863 			if (_dump_buf_dif) {
5864 				lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5865 					"9046 BLKGRD: allocated %d pages for "
5866 				       "_dump_buf_dif at 0x%p\n",
5867 				       (1 << pagecnt), _dump_buf_dif);
5868 				_dump_buf_dif_order = pagecnt;
5869 				memset(_dump_buf_dif, 0,
5870 				       ((1 << PAGE_SHIFT) << pagecnt));
5871 				break;
5872 			} else
5873 				--pagecnt;
5874 		}
5875 		if (!_dump_buf_dif_order)
5876 			lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5877 			"9047 BLKGRD: ERROR unable to allocate "
5878 			       "memory for hexdump\n");
5879 	} else
5880 		lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5881 			"9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
5882 		       _dump_buf_dif);
5883 }
5884 
5885 /**
5886  * lpfc_post_init_setup - Perform necessary device post initialization setup.
5887  * @phba: pointer to lpfc hba data structure.
5888  *
5889  * This routine is invoked to perform all the necessary post initialization
5890  * setup for the device.
5891  **/
5892 static void
5893 lpfc_post_init_setup(struct lpfc_hba *phba)
5894 {
5895 	struct Scsi_Host  *shost;
5896 	struct lpfc_adapter_event_header adapter_event;
5897 
5898 	/* Get the default values for Model Name and Description */
5899 	lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
5900 
5901 	/*
5902 	 * hba setup may have changed the hba_queue_depth so we need to
5903 	 * adjust the value of can_queue.
5904 	 */
5905 	shost = pci_get_drvdata(phba->pcidev);
5906 	shost->can_queue = phba->cfg_hba_queue_depth - 10;
5907 	if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
5908 		lpfc_setup_bg(phba, shost);
5909 
5910 	lpfc_host_attrib_init(shost);
5911 
5912 	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
5913 		spin_lock_irq(shost->host_lock);
5914 		lpfc_poll_start_timer(phba);
5915 		spin_unlock_irq(shost->host_lock);
5916 	}
5917 
5918 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5919 			"0428 Perform SCSI scan\n");
5920 	/* Send board arrival event to upper layer */
5921 	adapter_event.event_type = FC_REG_ADAPTER_EVENT;
5922 	adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
5923 	fc_host_post_vendor_event(shost, fc_get_event_number(),
5924 				  sizeof(adapter_event),
5925 				  (char *) &adapter_event,
5926 				  LPFC_NL_VENDOR_ID);
5927 	return;
5928 }
5929 
5930 /**
5931  * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
5932  * @phba: pointer to lpfc hba data structure.
5933  *
5934  * This routine is invoked to set up the PCI device memory space for device
5935  * with SLI-3 interface spec.
5936  *
5937  * Return codes
5938  * 	0 - successful
5939  * 	other values - error
5940  **/
5941 static int
5942 lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
5943 {
5944 	struct pci_dev *pdev;
5945 	unsigned long bar0map_len, bar2map_len;
5946 	int i, hbq_count;
5947 	void *ptr;
5948 	int error = -ENODEV;
5949 
5950 	/* Obtain PCI device reference */
5951 	if (!phba->pcidev)
5952 		return error;
5953 	else
5954 		pdev = phba->pcidev;
5955 
5956 	/* Set the device DMA mask size */
5957 	if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
5958 	 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
5959 		if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
5960 		 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
5961 			return error;
5962 		}
5963 	}
5964 
5965 	/* Get the bus address of Bar0 and Bar2 and the number of bytes
5966 	 * required by each mapping.
5967 	 */
5968 	phba->pci_bar0_map = pci_resource_start(pdev, 0);
5969 	bar0map_len = pci_resource_len(pdev, 0);
5970 
5971 	phba->pci_bar2_map = pci_resource_start(pdev, 2);
5972 	bar2map_len = pci_resource_len(pdev, 2);
5973 
5974 	/* Map HBA SLIM to a kernel virtual address. */
5975 	phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
5976 	if (!phba->slim_memmap_p) {
5977 		dev_printk(KERN_ERR, &pdev->dev,
5978 			   "ioremap failed for SLIM memory.\n");
5979 		goto out;
5980 	}
5981 
5982 	/* Map HBA Control Registers to a kernel virtual address. */
5983 	phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
5984 	if (!phba->ctrl_regs_memmap_p) {
5985 		dev_printk(KERN_ERR, &pdev->dev,
5986 			   "ioremap failed for HBA control registers.\n");
5987 		goto out_iounmap_slim;
5988 	}
5989 
5990 	/* Allocate memory for SLI-2 structures */
5991 	phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
5992 					       SLI2_SLIM_SIZE,
5993 					       &phba->slim2p.phys,
5994 					       GFP_KERNEL);
5995 	if (!phba->slim2p.virt)
5996 		goto out_iounmap;
5997 
5998 	memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
5999 	phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
6000 	phba->mbox_ext = (phba->slim2p.virt +
6001 		offsetof(struct lpfc_sli2_slim, mbx_ext_words));
6002 	phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
6003 	phba->IOCBs = (phba->slim2p.virt +
6004 		       offsetof(struct lpfc_sli2_slim, IOCBs));
6005 
6006 	phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
6007 						 lpfc_sli_hbq_size(),
6008 						 &phba->hbqslimp.phys,
6009 						 GFP_KERNEL);
6010 	if (!phba->hbqslimp.virt)
6011 		goto out_free_slim;
6012 
6013 	hbq_count = lpfc_sli_hbq_count();
6014 	ptr = phba->hbqslimp.virt;
6015 	for (i = 0; i < hbq_count; ++i) {
6016 		phba->hbqs[i].hbq_virt = ptr;
6017 		INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
6018 		ptr += (lpfc_hbq_defs[i]->entry_count *
6019 			sizeof(struct lpfc_hbq_entry));
6020 	}
6021 	phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
6022 	phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
6023 
6024 	memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
6025 
6026 	INIT_LIST_HEAD(&phba->rb_pend_list);
6027 
6028 	phba->MBslimaddr = phba->slim_memmap_p;
6029 	phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
6030 	phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
6031 	phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
6032 	phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
6033 
6034 	return 0;
6035 
6036 out_free_slim:
6037 	dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6038 			  phba->slim2p.virt, phba->slim2p.phys);
6039 out_iounmap:
6040 	iounmap(phba->ctrl_regs_memmap_p);
6041 out_iounmap_slim:
6042 	iounmap(phba->slim_memmap_p);
6043 out:
6044 	return error;
6045 }
6046 
6047 /**
6048  * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
6049  * @phba: pointer to lpfc hba data structure.
6050  *
6051  * This routine is invoked to unset the PCI device memory space for device
6052  * with SLI-3 interface spec.
6053  **/
6054 static void
6055 lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
6056 {
6057 	struct pci_dev *pdev;
6058 
6059 	/* Obtain PCI device reference */
6060 	if (!phba->pcidev)
6061 		return;
6062 	else
6063 		pdev = phba->pcidev;
6064 
6065 	/* Free coherent DMA memory allocated */
6066 	dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
6067 			  phba->hbqslimp.virt, phba->hbqslimp.phys);
6068 	dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6069 			  phba->slim2p.virt, phba->slim2p.phys);
6070 
6071 	/* I/O memory unmap */
6072 	iounmap(phba->ctrl_regs_memmap_p);
6073 	iounmap(phba->slim_memmap_p);
6074 
6075 	return;
6076 }
6077 
6078 /**
6079  * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
6080  * @phba: pointer to lpfc hba data structure.
6081  *
6082  * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
6083  * done and check status.
6084  *
6085  * Return 0 if successful, otherwise -ENODEV.
6086  **/
6087 int
6088 lpfc_sli4_post_status_check(struct lpfc_hba *phba)
6089 {
6090 	struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
6091 	struct lpfc_register reg_data;
6092 	int i, port_error = 0;
6093 	uint32_t if_type;
6094 
6095 	memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
6096 	memset(&reg_data, 0, sizeof(reg_data));
6097 	if (!phba->sli4_hba.PSMPHRregaddr)
6098 		return -ENODEV;
6099 
6100 	/* Wait up to 30 seconds for the SLI Port POST done and ready */
6101 	for (i = 0; i < 3000; i++) {
6102 		if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
6103 			&portsmphr_reg.word0) ||
6104 			(bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
6105 			/* Port has a fatal POST error, break out */
6106 			port_error = -ENODEV;
6107 			break;
6108 		}
6109 		if (LPFC_POST_STAGE_PORT_READY ==
6110 		    bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
6111 			break;
6112 		msleep(10);
6113 	}
6114 
6115 	/*
6116 	 * If there was a port error during POST, then don't proceed with
6117 	 * other register reads as the data may not be valid.  Just exit.
6118 	 */
6119 	if (port_error) {
6120 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6121 			"1408 Port Failed POST - portsmphr=0x%x, "
6122 			"perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
6123 			"scr2=x%x, hscratch=x%x, pstatus=x%x\n",
6124 			portsmphr_reg.word0,
6125 			bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
6126 			bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
6127 			bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
6128 			bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
6129 			bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
6130 			bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
6131 			bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
6132 			bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
6133 	} else {
6134 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6135 				"2534 Device Info: SLIFamily=0x%x, "
6136 				"SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
6137 				"SLIHint_2=0x%x, FT=0x%x\n",
6138 				bf_get(lpfc_sli_intf_sli_family,
6139 				       &phba->sli4_hba.sli_intf),
6140 				bf_get(lpfc_sli_intf_slirev,
6141 				       &phba->sli4_hba.sli_intf),
6142 				bf_get(lpfc_sli_intf_if_type,
6143 				       &phba->sli4_hba.sli_intf),
6144 				bf_get(lpfc_sli_intf_sli_hint1,
6145 				       &phba->sli4_hba.sli_intf),
6146 				bf_get(lpfc_sli_intf_sli_hint2,
6147 				       &phba->sli4_hba.sli_intf),
6148 				bf_get(lpfc_sli_intf_func_type,
6149 				       &phba->sli4_hba.sli_intf));
6150 		/*
6151 		 * Check for other Port errors during the initialization
6152 		 * process.  Fail the load if the port did not come up
6153 		 * correctly.
6154 		 */
6155 		if_type = bf_get(lpfc_sli_intf_if_type,
6156 				 &phba->sli4_hba.sli_intf);
6157 		switch (if_type) {
6158 		case LPFC_SLI_INTF_IF_TYPE_0:
6159 			phba->sli4_hba.ue_mask_lo =
6160 			      readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
6161 			phba->sli4_hba.ue_mask_hi =
6162 			      readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
6163 			uerrlo_reg.word0 =
6164 			      readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
6165 			uerrhi_reg.word0 =
6166 				readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
6167 			if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
6168 			    (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
6169 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6170 						"1422 Unrecoverable Error "
6171 						"Detected during POST "
6172 						"uerr_lo_reg=0x%x, "
6173 						"uerr_hi_reg=0x%x, "
6174 						"ue_mask_lo_reg=0x%x, "
6175 						"ue_mask_hi_reg=0x%x\n",
6176 						uerrlo_reg.word0,
6177 						uerrhi_reg.word0,
6178 						phba->sli4_hba.ue_mask_lo,
6179 						phba->sli4_hba.ue_mask_hi);
6180 				port_error = -ENODEV;
6181 			}
6182 			break;
6183 		case LPFC_SLI_INTF_IF_TYPE_2:
6184 			/* Final checks.  The port status should be clean. */
6185 			if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
6186 				&reg_data.word0) ||
6187 				(bf_get(lpfc_sliport_status_err, &reg_data) &&
6188 				 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
6189 				phba->work_status[0] =
6190 					readl(phba->sli4_hba.u.if_type2.
6191 					      ERR1regaddr);
6192 				phba->work_status[1] =
6193 					readl(phba->sli4_hba.u.if_type2.
6194 					      ERR2regaddr);
6195 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6196 					"2888 Unrecoverable port error "
6197 					"following POST: port status reg "
6198 					"0x%x, port_smphr reg 0x%x, "
6199 					"error 1=0x%x, error 2=0x%x\n",
6200 					reg_data.word0,
6201 					portsmphr_reg.word0,
6202 					phba->work_status[0],
6203 					phba->work_status[1]);
6204 				port_error = -ENODEV;
6205 			}
6206 			break;
6207 		case LPFC_SLI_INTF_IF_TYPE_1:
6208 		default:
6209 			break;
6210 		}
6211 	}
6212 	return port_error;
6213 }
6214 
6215 /**
6216  * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
6217  * @phba: pointer to lpfc hba data structure.
6218  * @if_type:  The SLI4 interface type getting configured.
6219  *
6220  * This routine is invoked to set up SLI4 BAR0 PCI config space register
6221  * memory map.
6222  **/
6223 static void
6224 lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
6225 {
6226 	switch (if_type) {
6227 	case LPFC_SLI_INTF_IF_TYPE_0:
6228 		phba->sli4_hba.u.if_type0.UERRLOregaddr =
6229 			phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
6230 		phba->sli4_hba.u.if_type0.UERRHIregaddr =
6231 			phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
6232 		phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
6233 			phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
6234 		phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
6235 			phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
6236 		phba->sli4_hba.SLIINTFregaddr =
6237 			phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
6238 		break;
6239 	case LPFC_SLI_INTF_IF_TYPE_2:
6240 		phba->sli4_hba.u.if_type2.ERR1regaddr =
6241 			phba->sli4_hba.conf_regs_memmap_p +
6242 						LPFC_CTL_PORT_ER1_OFFSET;
6243 		phba->sli4_hba.u.if_type2.ERR2regaddr =
6244 			phba->sli4_hba.conf_regs_memmap_p +
6245 						LPFC_CTL_PORT_ER2_OFFSET;
6246 		phba->sli4_hba.u.if_type2.CTRLregaddr =
6247 			phba->sli4_hba.conf_regs_memmap_p +
6248 						LPFC_CTL_PORT_CTL_OFFSET;
6249 		phba->sli4_hba.u.if_type2.STATUSregaddr =
6250 			phba->sli4_hba.conf_regs_memmap_p +
6251 						LPFC_CTL_PORT_STA_OFFSET;
6252 		phba->sli4_hba.SLIINTFregaddr =
6253 			phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
6254 		phba->sli4_hba.PSMPHRregaddr =
6255 			phba->sli4_hba.conf_regs_memmap_p +
6256 						LPFC_CTL_PORT_SEM_OFFSET;
6257 		phba->sli4_hba.RQDBregaddr =
6258 			phba->sli4_hba.conf_regs_memmap_p +
6259 						LPFC_ULP0_RQ_DOORBELL;
6260 		phba->sli4_hba.WQDBregaddr =
6261 			phba->sli4_hba.conf_regs_memmap_p +
6262 						LPFC_ULP0_WQ_DOORBELL;
6263 		phba->sli4_hba.EQCQDBregaddr =
6264 			phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
6265 		phba->sli4_hba.MQDBregaddr =
6266 			phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
6267 		phba->sli4_hba.BMBXregaddr =
6268 			phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
6269 		break;
6270 	case LPFC_SLI_INTF_IF_TYPE_1:
6271 	default:
6272 		dev_printk(KERN_ERR, &phba->pcidev->dev,
6273 			   "FATAL - unsupported SLI4 interface type - %d\n",
6274 			   if_type);
6275 		break;
6276 	}
6277 }
6278 
6279 /**
6280  * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
6281  * @phba: pointer to lpfc hba data structure.
6282  *
6283  * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
6284  * memory map.
6285  **/
6286 static void
6287 lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
6288 {
6289 	phba->sli4_hba.PSMPHRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
6290 		LPFC_SLIPORT_IF0_SMPHR;
6291 	phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
6292 		LPFC_HST_ISR0;
6293 	phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
6294 		LPFC_HST_IMR0;
6295 	phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
6296 		LPFC_HST_ISCR0;
6297 }
6298 
6299 /**
6300  * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
6301  * @phba: pointer to lpfc hba data structure.
6302  * @vf: virtual function number
6303  *
6304  * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
6305  * based on the given viftual function number, @vf.
6306  *
6307  * Return 0 if successful, otherwise -ENODEV.
6308  **/
6309 static int
6310 lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
6311 {
6312 	if (vf > LPFC_VIR_FUNC_MAX)
6313 		return -ENODEV;
6314 
6315 	phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6316 				vf * LPFC_VFR_PAGE_SIZE +
6317 					LPFC_ULP0_RQ_DOORBELL);
6318 	phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6319 				vf * LPFC_VFR_PAGE_SIZE +
6320 					LPFC_ULP0_WQ_DOORBELL);
6321 	phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6322 				vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
6323 	phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6324 				vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
6325 	phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6326 				vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
6327 	return 0;
6328 }
6329 
6330 /**
6331  * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
6332  * @phba: pointer to lpfc hba data structure.
6333  *
6334  * This routine is invoked to create the bootstrap mailbox
6335  * region consistent with the SLI-4 interface spec.  This
6336  * routine allocates all memory necessary to communicate
6337  * mailbox commands to the port and sets up all alignment
6338  * needs.  No locks are expected to be held when calling
6339  * this routine.
6340  *
6341  * Return codes
6342  * 	0 - successful
6343  * 	-ENOMEM - could not allocated memory.
6344  **/
6345 static int
6346 lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
6347 {
6348 	uint32_t bmbx_size;
6349 	struct lpfc_dmabuf *dmabuf;
6350 	struct dma_address *dma_address;
6351 	uint32_t pa_addr;
6352 	uint64_t phys_addr;
6353 
6354 	dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
6355 	if (!dmabuf)
6356 		return -ENOMEM;
6357 
6358 	/*
6359 	 * The bootstrap mailbox region is comprised of 2 parts
6360 	 * plus an alignment restriction of 16 bytes.
6361 	 */
6362 	bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
6363 	dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
6364 					  bmbx_size,
6365 					  &dmabuf->phys,
6366 					  GFP_KERNEL);
6367 	if (!dmabuf->virt) {
6368 		kfree(dmabuf);
6369 		return -ENOMEM;
6370 	}
6371 	memset(dmabuf->virt, 0, bmbx_size);
6372 
6373 	/*
6374 	 * Initialize the bootstrap mailbox pointers now so that the register
6375 	 * operations are simple later.  The mailbox dma address is required
6376 	 * to be 16-byte aligned.  Also align the virtual memory as each
6377 	 * maibox is copied into the bmbx mailbox region before issuing the
6378 	 * command to the port.
6379 	 */
6380 	phba->sli4_hba.bmbx.dmabuf = dmabuf;
6381 	phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
6382 
6383 	phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
6384 					      LPFC_ALIGN_16_BYTE);
6385 	phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
6386 					      LPFC_ALIGN_16_BYTE);
6387 
6388 	/*
6389 	 * Set the high and low physical addresses now.  The SLI4 alignment
6390 	 * requirement is 16 bytes and the mailbox is posted to the port
6391 	 * as two 30-bit addresses.  The other data is a bit marking whether
6392 	 * the 30-bit address is the high or low address.
6393 	 * Upcast bmbx aphys to 64bits so shift instruction compiles
6394 	 * clean on 32 bit machines.
6395 	 */
6396 	dma_address = &phba->sli4_hba.bmbx.dma_address;
6397 	phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
6398 	pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
6399 	dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
6400 					   LPFC_BMBX_BIT1_ADDR_HI);
6401 
6402 	pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
6403 	dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
6404 					   LPFC_BMBX_BIT1_ADDR_LO);
6405 	return 0;
6406 }
6407 
6408 /**
6409  * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
6410  * @phba: pointer to lpfc hba data structure.
6411  *
6412  * This routine is invoked to teardown the bootstrap mailbox
6413  * region and release all host resources. This routine requires
6414  * the caller to ensure all mailbox commands recovered, no
6415  * additional mailbox comands are sent, and interrupts are disabled
6416  * before calling this routine.
6417  *
6418  **/
6419 static void
6420 lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
6421 {
6422 	dma_free_coherent(&phba->pcidev->dev,
6423 			  phba->sli4_hba.bmbx.bmbx_size,
6424 			  phba->sli4_hba.bmbx.dmabuf->virt,
6425 			  phba->sli4_hba.bmbx.dmabuf->phys);
6426 
6427 	kfree(phba->sli4_hba.bmbx.dmabuf);
6428 	memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
6429 }
6430 
6431 /**
6432  * lpfc_sli4_read_config - Get the config parameters.
6433  * @phba: pointer to lpfc hba data structure.
6434  *
6435  * This routine is invoked to read the configuration parameters from the HBA.
6436  * The configuration parameters are used to set the base and maximum values
6437  * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
6438  * allocation for the port.
6439  *
6440  * Return codes
6441  * 	0 - successful
6442  * 	-ENOMEM - No available memory
6443  *      -EIO - The mailbox failed to complete successfully.
6444  **/
6445 int
6446 lpfc_sli4_read_config(struct lpfc_hba *phba)
6447 {
6448 	LPFC_MBOXQ_t *pmb;
6449 	struct lpfc_mbx_read_config *rd_config;
6450 	union  lpfc_sli4_cfg_shdr *shdr;
6451 	uint32_t shdr_status, shdr_add_status;
6452 	struct lpfc_mbx_get_func_cfg *get_func_cfg;
6453 	struct lpfc_rsrc_desc_fcfcoe *desc;
6454 	char *pdesc_0;
6455 	uint32_t desc_count;
6456 	int length, i, rc = 0, rc2;
6457 
6458 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6459 	if (!pmb) {
6460 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6461 				"2011 Unable to allocate memory for issuing "
6462 				"SLI_CONFIG_SPECIAL mailbox command\n");
6463 		return -ENOMEM;
6464 	}
6465 
6466 	lpfc_read_config(phba, pmb);
6467 
6468 	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6469 	if (rc != MBX_SUCCESS) {
6470 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6471 			"2012 Mailbox failed , mbxCmd x%x "
6472 			"READ_CONFIG, mbxStatus x%x\n",
6473 			bf_get(lpfc_mqe_command, &pmb->u.mqe),
6474 			bf_get(lpfc_mqe_status, &pmb->u.mqe));
6475 		rc = -EIO;
6476 	} else {
6477 		rd_config = &pmb->u.mqe.un.rd_config;
6478 		if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
6479 			phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
6480 			phba->sli4_hba.lnk_info.lnk_tp =
6481 				bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
6482 			phba->sli4_hba.lnk_info.lnk_no =
6483 				bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
6484 			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6485 					"3081 lnk_type:%d, lnk_numb:%d\n",
6486 					phba->sli4_hba.lnk_info.lnk_tp,
6487 					phba->sli4_hba.lnk_info.lnk_no);
6488 		} else
6489 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6490 					"3082 Mailbox (x%x) returned ldv:x0\n",
6491 					bf_get(lpfc_mqe_command, &pmb->u.mqe));
6492 		phba->sli4_hba.extents_in_use =
6493 			bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
6494 		phba->sli4_hba.max_cfg_param.max_xri =
6495 			bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
6496 		phba->sli4_hba.max_cfg_param.xri_base =
6497 			bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
6498 		phba->sli4_hba.max_cfg_param.max_vpi =
6499 			bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
6500 		phba->sli4_hba.max_cfg_param.vpi_base =
6501 			bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
6502 		phba->sli4_hba.max_cfg_param.max_rpi =
6503 			bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
6504 		phba->sli4_hba.max_cfg_param.rpi_base =
6505 			bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
6506 		phba->sli4_hba.max_cfg_param.max_vfi =
6507 			bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
6508 		phba->sli4_hba.max_cfg_param.vfi_base =
6509 			bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
6510 		phba->sli4_hba.max_cfg_param.max_fcfi =
6511 			bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
6512 		phba->sli4_hba.max_cfg_param.max_eq =
6513 			bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
6514 		phba->sli4_hba.max_cfg_param.max_rq =
6515 			bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
6516 		phba->sli4_hba.max_cfg_param.max_wq =
6517 			bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
6518 		phba->sli4_hba.max_cfg_param.max_cq =
6519 			bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
6520 		phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
6521 		phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
6522 		phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
6523 		phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
6524 		phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
6525 				(phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
6526 		phba->max_vports = phba->max_vpi;
6527 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6528 				"2003 cfg params Extents? %d "
6529 				"XRI(B:%d M:%d), "
6530 				"VPI(B:%d M:%d) "
6531 				"VFI(B:%d M:%d) "
6532 				"RPI(B:%d M:%d) "
6533 				"FCFI(Count:%d)\n",
6534 				phba->sli4_hba.extents_in_use,
6535 				phba->sli4_hba.max_cfg_param.xri_base,
6536 				phba->sli4_hba.max_cfg_param.max_xri,
6537 				phba->sli4_hba.max_cfg_param.vpi_base,
6538 				phba->sli4_hba.max_cfg_param.max_vpi,
6539 				phba->sli4_hba.max_cfg_param.vfi_base,
6540 				phba->sli4_hba.max_cfg_param.max_vfi,
6541 				phba->sli4_hba.max_cfg_param.rpi_base,
6542 				phba->sli4_hba.max_cfg_param.max_rpi,
6543 				phba->sli4_hba.max_cfg_param.max_fcfi);
6544 	}
6545 
6546 	if (rc)
6547 		goto read_cfg_out;
6548 
6549 	/* Reset the DFT_HBA_Q_DEPTH to the max xri  */
6550 	if (phba->cfg_hba_queue_depth >
6551 		(phba->sli4_hba.max_cfg_param.max_xri -
6552 			lpfc_sli4_get_els_iocb_cnt(phba)))
6553 		phba->cfg_hba_queue_depth =
6554 			phba->sli4_hba.max_cfg_param.max_xri -
6555 				lpfc_sli4_get_els_iocb_cnt(phba);
6556 
6557 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
6558 	    LPFC_SLI_INTF_IF_TYPE_2)
6559 		goto read_cfg_out;
6560 
6561 	/* get the pf# and vf# for SLI4 if_type 2 port */
6562 	length = (sizeof(struct lpfc_mbx_get_func_cfg) -
6563 		  sizeof(struct lpfc_sli4_cfg_mhdr));
6564 	lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
6565 			 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
6566 			 length, LPFC_SLI4_MBX_EMBED);
6567 
6568 	rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6569 	shdr = (union lpfc_sli4_cfg_shdr *)
6570 				&pmb->u.mqe.un.sli4_config.header.cfg_shdr;
6571 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6572 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6573 	if (rc2 || shdr_status || shdr_add_status) {
6574 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6575 				"3026 Mailbox failed , mbxCmd x%x "
6576 				"GET_FUNCTION_CONFIG, mbxStatus x%x\n",
6577 				bf_get(lpfc_mqe_command, &pmb->u.mqe),
6578 				bf_get(lpfc_mqe_status, &pmb->u.mqe));
6579 		goto read_cfg_out;
6580 	}
6581 
6582 	/* search for fc_fcoe resrouce descriptor */
6583 	get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
6584 	desc_count = get_func_cfg->func_cfg.rsrc_desc_count;
6585 
6586 	pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
6587 	desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
6588 	length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
6589 	if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
6590 		length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
6591 	else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
6592 		goto read_cfg_out;
6593 
6594 	for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
6595 		desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
6596 		if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
6597 		    bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
6598 			phba->sli4_hba.iov.pf_number =
6599 				bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
6600 			phba->sli4_hba.iov.vf_number =
6601 				bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
6602 			break;
6603 		}
6604 	}
6605 
6606 	if (i < LPFC_RSRC_DESC_MAX_NUM)
6607 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6608 				"3027 GET_FUNCTION_CONFIG: pf_number:%d, "
6609 				"vf_number:%d\n", phba->sli4_hba.iov.pf_number,
6610 				phba->sli4_hba.iov.vf_number);
6611 	else
6612 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6613 				"3028 GET_FUNCTION_CONFIG: failed to find "
6614 				"Resrouce Descriptor:x%x\n",
6615 				LPFC_RSRC_DESC_TYPE_FCFCOE);
6616 
6617 read_cfg_out:
6618 	mempool_free(pmb, phba->mbox_mem_pool);
6619 	return rc;
6620 }
6621 
6622 /**
6623  * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
6624  * @phba: pointer to lpfc hba data structure.
6625  *
6626  * This routine is invoked to setup the port-side endian order when
6627  * the port if_type is 0.  This routine has no function for other
6628  * if_types.
6629  *
6630  * Return codes
6631  * 	0 - successful
6632  * 	-ENOMEM - No available memory
6633  *      -EIO - The mailbox failed to complete successfully.
6634  **/
6635 static int
6636 lpfc_setup_endian_order(struct lpfc_hba *phba)
6637 {
6638 	LPFC_MBOXQ_t *mboxq;
6639 	uint32_t if_type, rc = 0;
6640 	uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
6641 				      HOST_ENDIAN_HIGH_WORD1};
6642 
6643 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
6644 	switch (if_type) {
6645 	case LPFC_SLI_INTF_IF_TYPE_0:
6646 		mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6647 						       GFP_KERNEL);
6648 		if (!mboxq) {
6649 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6650 					"0492 Unable to allocate memory for "
6651 					"issuing SLI_CONFIG_SPECIAL mailbox "
6652 					"command\n");
6653 			return -ENOMEM;
6654 		}
6655 
6656 		/*
6657 		 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
6658 		 * two words to contain special data values and no other data.
6659 		 */
6660 		memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
6661 		memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
6662 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6663 		if (rc != MBX_SUCCESS) {
6664 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6665 					"0493 SLI_CONFIG_SPECIAL mailbox "
6666 					"failed with status x%x\n",
6667 					rc);
6668 			rc = -EIO;
6669 		}
6670 		mempool_free(mboxq, phba->mbox_mem_pool);
6671 		break;
6672 	case LPFC_SLI_INTF_IF_TYPE_2:
6673 	case LPFC_SLI_INTF_IF_TYPE_1:
6674 	default:
6675 		break;
6676 	}
6677 	return rc;
6678 }
6679 
6680 /**
6681  * lpfc_sli4_queue_verify - Verify and update EQ and CQ counts
6682  * @phba: pointer to lpfc hba data structure.
6683  *
6684  * This routine is invoked to check the user settable queue counts for EQs and
6685  * CQs. after this routine is called the counts will be set to valid values that
6686  * adhere to the constraints of the system's interrupt vectors and the port's
6687  * queue resources.
6688  *
6689  * Return codes
6690  *      0 - successful
6691  *      -ENOMEM - No available memory
6692  **/
6693 static int
6694 lpfc_sli4_queue_verify(struct lpfc_hba *phba)
6695 {
6696 	int cfg_fcp_io_channel;
6697 	uint32_t cpu;
6698 	uint32_t i = 0;
6699 
6700 
6701 	/*
6702 	 * Sanity check for configured queue parameters against the run-time
6703 	 * device parameters
6704 	 */
6705 
6706 	/* Sanity check on HBA EQ parameters */
6707 	cfg_fcp_io_channel = phba->cfg_fcp_io_channel;
6708 
6709 	/* It doesn't make sense to have more io channels then CPUs */
6710 	for_each_online_cpu(cpu) {
6711 		i++;
6712 	}
6713 	if (i < cfg_fcp_io_channel) {
6714 		lpfc_printf_log(phba,
6715 				KERN_ERR, LOG_INIT,
6716 				"3188 Reducing IO channels to match number of "
6717 				"CPUs: from %d to %d\n", cfg_fcp_io_channel, i);
6718 		cfg_fcp_io_channel = i;
6719 	}
6720 
6721 	if (cfg_fcp_io_channel >
6722 	    phba->sli4_hba.max_cfg_param.max_eq) {
6723 		if (phba->sli4_hba.max_cfg_param.max_eq <
6724 		    LPFC_FCP_IO_CHAN_MIN) {
6725 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6726 					"2574 Not enough EQs (%d) from the "
6727 					"pci function for supporting FCP "
6728 					"EQs (%d)\n",
6729 					phba->sli4_hba.max_cfg_param.max_eq,
6730 					phba->cfg_fcp_io_channel);
6731 			goto out_error;
6732 		}
6733 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6734 				"2575 Reducing IO channels to match number of "
6735 				"available EQs: from %d to %d\n",
6736 				cfg_fcp_io_channel,
6737 				phba->sli4_hba.max_cfg_param.max_eq);
6738 		cfg_fcp_io_channel = phba->sli4_hba.max_cfg_param.max_eq;
6739 	}
6740 
6741 	/* Eventually cfg_fcp_eq_count / cfg_fcp_wq_count will be depricated */
6742 
6743 	/* The actual number of FCP event queues adopted */
6744 	phba->cfg_fcp_eq_count = cfg_fcp_io_channel;
6745 	phba->cfg_fcp_wq_count = cfg_fcp_io_channel;
6746 	phba->cfg_fcp_io_channel = cfg_fcp_io_channel;
6747 
6748 	/* Get EQ depth from module parameter, fake the default for now */
6749 	phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
6750 	phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
6751 
6752 	/* Get CQ depth from module parameter, fake the default for now */
6753 	phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
6754 	phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
6755 
6756 	return 0;
6757 out_error:
6758 	return -ENOMEM;
6759 }
6760 
6761 /**
6762  * lpfc_sli4_queue_create - Create all the SLI4 queues
6763  * @phba: pointer to lpfc hba data structure.
6764  *
6765  * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
6766  * operation. For each SLI4 queue type, the parameters such as queue entry
6767  * count (queue depth) shall be taken from the module parameter. For now,
6768  * we just use some constant number as place holder.
6769  *
6770  * Return codes
6771  *      0 - successful
6772  *      -ENOMEM - No availble memory
6773  *      -EIO - The mailbox failed to complete successfully.
6774  **/
6775 int
6776 lpfc_sli4_queue_create(struct lpfc_hba *phba)
6777 {
6778 	struct lpfc_queue *qdesc;
6779 	int idx;
6780 
6781 	/*
6782 	 * Create HBA Record arrays.
6783 	 */
6784 	if (!phba->cfg_fcp_io_channel)
6785 		return -ERANGE;
6786 
6787 	phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
6788 	phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
6789 	phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
6790 	phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
6791 	phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
6792 	phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
6793 
6794 	phba->sli4_hba.hba_eq =  kzalloc((sizeof(struct lpfc_queue *) *
6795 				phba->cfg_fcp_io_channel), GFP_KERNEL);
6796 	if (!phba->sli4_hba.hba_eq) {
6797 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6798 			"2576 Failed allocate memory for "
6799 			"fast-path EQ record array\n");
6800 		goto out_error;
6801 	}
6802 
6803 	phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
6804 				phba->cfg_fcp_io_channel), GFP_KERNEL);
6805 	if (!phba->sli4_hba.fcp_cq) {
6806 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6807 				"2577 Failed allocate memory for fast-path "
6808 				"CQ record array\n");
6809 		goto out_error;
6810 	}
6811 
6812 	phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
6813 				phba->cfg_fcp_io_channel), GFP_KERNEL);
6814 	if (!phba->sli4_hba.fcp_wq) {
6815 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6816 				"2578 Failed allocate memory for fast-path "
6817 				"WQ record array\n");
6818 		goto out_error;
6819 	}
6820 
6821 	/*
6822 	 * Since the first EQ can have multiple CQs associated with it,
6823 	 * this array is used to quickly see if we have a FCP fast-path
6824 	 * CQ match.
6825 	 */
6826 	phba->sli4_hba.fcp_cq_map = kzalloc((sizeof(uint16_t) *
6827 					 phba->cfg_fcp_io_channel), GFP_KERNEL);
6828 	if (!phba->sli4_hba.fcp_cq_map) {
6829 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6830 				"2545 Failed allocate memory for fast-path "
6831 				"CQ map\n");
6832 		goto out_error;
6833 	}
6834 
6835 	/*
6836 	 * Create HBA Event Queues (EQs).  The cfg_fcp_io_channel specifies
6837 	 * how many EQs to create.
6838 	 */
6839 	for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
6840 
6841 		/* Create EQs */
6842 		qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
6843 					      phba->sli4_hba.eq_ecount);
6844 		if (!qdesc) {
6845 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6846 					"0497 Failed allocate EQ (%d)\n", idx);
6847 			goto out_error;
6848 		}
6849 		phba->sli4_hba.hba_eq[idx] = qdesc;
6850 
6851 		/* Create Fast Path FCP CQs */
6852 		qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6853 					      phba->sli4_hba.cq_ecount);
6854 		if (!qdesc) {
6855 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6856 					"0499 Failed allocate fast-path FCP "
6857 					"CQ (%d)\n", idx);
6858 			goto out_error;
6859 		}
6860 		phba->sli4_hba.fcp_cq[idx] = qdesc;
6861 
6862 		/* Create Fast Path FCP WQs */
6863 		qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
6864 					      phba->sli4_hba.wq_ecount);
6865 		if (!qdesc) {
6866 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6867 					"0503 Failed allocate fast-path FCP "
6868 					"WQ (%d)\n", idx);
6869 			goto out_error;
6870 		}
6871 		phba->sli4_hba.fcp_wq[idx] = qdesc;
6872 	}
6873 
6874 
6875 	/*
6876 	 * Create Slow Path Completion Queues (CQs)
6877 	 */
6878 
6879 	/* Create slow-path Mailbox Command Complete Queue */
6880 	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6881 				      phba->sli4_hba.cq_ecount);
6882 	if (!qdesc) {
6883 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6884 				"0500 Failed allocate slow-path mailbox CQ\n");
6885 		goto out_error;
6886 	}
6887 	phba->sli4_hba.mbx_cq = qdesc;
6888 
6889 	/* Create slow-path ELS Complete Queue */
6890 	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6891 				      phba->sli4_hba.cq_ecount);
6892 	if (!qdesc) {
6893 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6894 				"0501 Failed allocate slow-path ELS CQ\n");
6895 		goto out_error;
6896 	}
6897 	phba->sli4_hba.els_cq = qdesc;
6898 
6899 
6900 	/*
6901 	 * Create Slow Path Work Queues (WQs)
6902 	 */
6903 
6904 	/* Create Mailbox Command Queue */
6905 
6906 	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
6907 				      phba->sli4_hba.mq_ecount);
6908 	if (!qdesc) {
6909 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6910 				"0505 Failed allocate slow-path MQ\n");
6911 		goto out_error;
6912 	}
6913 	phba->sli4_hba.mbx_wq = qdesc;
6914 
6915 	/*
6916 	 * Create ELS Work Queues
6917 	 */
6918 
6919 	/* Create slow-path ELS Work Queue */
6920 	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
6921 				      phba->sli4_hba.wq_ecount);
6922 	if (!qdesc) {
6923 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6924 				"0504 Failed allocate slow-path ELS WQ\n");
6925 		goto out_error;
6926 	}
6927 	phba->sli4_hba.els_wq = qdesc;
6928 
6929 	/*
6930 	 * Create Receive Queue (RQ)
6931 	 */
6932 
6933 	/* Create Receive Queue for header */
6934 	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
6935 				      phba->sli4_hba.rq_ecount);
6936 	if (!qdesc) {
6937 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6938 				"0506 Failed allocate receive HRQ\n");
6939 		goto out_error;
6940 	}
6941 	phba->sli4_hba.hdr_rq = qdesc;
6942 
6943 	/* Create Receive Queue for data */
6944 	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
6945 				      phba->sli4_hba.rq_ecount);
6946 	if (!qdesc) {
6947 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6948 				"0507 Failed allocate receive DRQ\n");
6949 		goto out_error;
6950 	}
6951 	phba->sli4_hba.dat_rq = qdesc;
6952 
6953 	return 0;
6954 
6955 out_error:
6956 	lpfc_sli4_queue_destroy(phba);
6957 	return -ENOMEM;
6958 }
6959 
6960 /**
6961  * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
6962  * @phba: pointer to lpfc hba data structure.
6963  *
6964  * This routine is invoked to release all the SLI4 queues with the FCoE HBA
6965  * operation.
6966  *
6967  * Return codes
6968  *      0 - successful
6969  *      -ENOMEM - No available memory
6970  *      -EIO - The mailbox failed to complete successfully.
6971  **/
6972 void
6973 lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
6974 {
6975 	int idx;
6976 
6977 	if (phba->sli4_hba.hba_eq != NULL) {
6978 		/* Release HBA event queue */
6979 		for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
6980 			if (phba->sli4_hba.hba_eq[idx] != NULL) {
6981 				lpfc_sli4_queue_free(
6982 					phba->sli4_hba.hba_eq[idx]);
6983 				phba->sli4_hba.hba_eq[idx] = NULL;
6984 			}
6985 		}
6986 		kfree(phba->sli4_hba.hba_eq);
6987 		phba->sli4_hba.hba_eq = NULL;
6988 	}
6989 
6990 	if (phba->sli4_hba.fcp_cq != NULL) {
6991 		/* Release FCP completion queue */
6992 		for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
6993 			if (phba->sli4_hba.fcp_cq[idx] != NULL) {
6994 				lpfc_sli4_queue_free(
6995 					phba->sli4_hba.fcp_cq[idx]);
6996 				phba->sli4_hba.fcp_cq[idx] = NULL;
6997 			}
6998 		}
6999 		kfree(phba->sli4_hba.fcp_cq);
7000 		phba->sli4_hba.fcp_cq = NULL;
7001 	}
7002 
7003 	if (phba->sli4_hba.fcp_wq != NULL) {
7004 		/* Release FCP work queue */
7005 		for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7006 			if (phba->sli4_hba.fcp_wq[idx] != NULL) {
7007 				lpfc_sli4_queue_free(
7008 					phba->sli4_hba.fcp_wq[idx]);
7009 				phba->sli4_hba.fcp_wq[idx] = NULL;
7010 			}
7011 		}
7012 		kfree(phba->sli4_hba.fcp_wq);
7013 		phba->sli4_hba.fcp_wq = NULL;
7014 	}
7015 
7016 	if (phba->pci_bar0_memmap_p) {
7017 		iounmap(phba->pci_bar0_memmap_p);
7018 		phba->pci_bar0_memmap_p = NULL;
7019 	}
7020 	if (phba->pci_bar2_memmap_p) {
7021 		iounmap(phba->pci_bar2_memmap_p);
7022 		phba->pci_bar2_memmap_p = NULL;
7023 	}
7024 	if (phba->pci_bar4_memmap_p) {
7025 		iounmap(phba->pci_bar4_memmap_p);
7026 		phba->pci_bar4_memmap_p = NULL;
7027 	}
7028 
7029 	/* Release FCP CQ mapping array */
7030 	if (phba->sli4_hba.fcp_cq_map != NULL) {
7031 		kfree(phba->sli4_hba.fcp_cq_map);
7032 		phba->sli4_hba.fcp_cq_map = NULL;
7033 	}
7034 
7035 	/* Release mailbox command work queue */
7036 	if (phba->sli4_hba.mbx_wq != NULL) {
7037 		lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
7038 		phba->sli4_hba.mbx_wq = NULL;
7039 	}
7040 
7041 	/* Release ELS work queue */
7042 	if (phba->sli4_hba.els_wq != NULL) {
7043 		lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
7044 		phba->sli4_hba.els_wq = NULL;
7045 	}
7046 
7047 	/* Release unsolicited receive queue */
7048 	if (phba->sli4_hba.hdr_rq != NULL) {
7049 		lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
7050 		phba->sli4_hba.hdr_rq = NULL;
7051 	}
7052 	if (phba->sli4_hba.dat_rq != NULL) {
7053 		lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
7054 		phba->sli4_hba.dat_rq = NULL;
7055 	}
7056 
7057 	/* Release ELS complete queue */
7058 	if (phba->sli4_hba.els_cq != NULL) {
7059 		lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
7060 		phba->sli4_hba.els_cq = NULL;
7061 	}
7062 
7063 	/* Release mailbox command complete queue */
7064 	if (phba->sli4_hba.mbx_cq != NULL) {
7065 		lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
7066 		phba->sli4_hba.mbx_cq = NULL;
7067 	}
7068 
7069 	return;
7070 }
7071 
7072 /**
7073  * lpfc_sli4_queue_setup - Set up all the SLI4 queues
7074  * @phba: pointer to lpfc hba data structure.
7075  *
7076  * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
7077  * operation.
7078  *
7079  * Return codes
7080  *      0 - successful
7081  *      -ENOMEM - No available memory
7082  *      -EIO - The mailbox failed to complete successfully.
7083  **/
7084 int
7085 lpfc_sli4_queue_setup(struct lpfc_hba *phba)
7086 {
7087 	struct lpfc_sli *psli = &phba->sli;
7088 	struct lpfc_sli_ring *pring;
7089 	int rc = -ENOMEM;
7090 	int fcp_eqidx, fcp_cqidx, fcp_wqidx;
7091 	int fcp_cq_index = 0;
7092 	uint32_t shdr_status, shdr_add_status;
7093 	union lpfc_sli4_cfg_shdr *shdr;
7094 	LPFC_MBOXQ_t *mboxq;
7095 	uint32_t length;
7096 
7097 	/* Check for dual-ULP support */
7098 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7099 	if (!mboxq) {
7100 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7101 				"3249 Unable to allocate memory for "
7102 				"QUERY_FW_CFG mailbox command\n");
7103 		return -ENOMEM;
7104 	}
7105 	length = (sizeof(struct lpfc_mbx_query_fw_config) -
7106 		  sizeof(struct lpfc_sli4_cfg_mhdr));
7107 	lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7108 			 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
7109 			 length, LPFC_SLI4_MBX_EMBED);
7110 
7111 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7112 
7113 	shdr = (union lpfc_sli4_cfg_shdr *)
7114 			&mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7115 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7116 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7117 	if (shdr_status || shdr_add_status || rc) {
7118 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7119 				"3250 QUERY_FW_CFG mailbox failed with status "
7120 				"x%x add_status x%x, mbx status x%x\n",
7121 				shdr_status, shdr_add_status, rc);
7122 		if (rc != MBX_TIMEOUT)
7123 			mempool_free(mboxq, phba->mbox_mem_pool);
7124 		rc = -ENXIO;
7125 		goto out_error;
7126 	}
7127 
7128 	phba->sli4_hba.fw_func_mode =
7129 			mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
7130 	phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
7131 	phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
7132 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7133 			"3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
7134 			"ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
7135 			phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
7136 
7137 	if (rc != MBX_TIMEOUT)
7138 		mempool_free(mboxq, phba->mbox_mem_pool);
7139 
7140 	/*
7141 	 * Set up HBA Event Queues (EQs)
7142 	 */
7143 
7144 	/* Set up HBA event queue */
7145 	if (phba->cfg_fcp_io_channel && !phba->sli4_hba.hba_eq) {
7146 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7147 				"3147 Fast-path EQs not allocated\n");
7148 		rc = -ENOMEM;
7149 		goto out_error;
7150 	}
7151 	for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
7152 		if (!phba->sli4_hba.hba_eq[fcp_eqidx]) {
7153 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7154 					"0522 Fast-path EQ (%d) not "
7155 					"allocated\n", fcp_eqidx);
7156 			rc = -ENOMEM;
7157 			goto out_destroy_hba_eq;
7158 		}
7159 		rc = lpfc_eq_create(phba, phba->sli4_hba.hba_eq[fcp_eqidx],
7160 			 (phba->cfg_fcp_imax / phba->cfg_fcp_io_channel));
7161 		if (rc) {
7162 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7163 					"0523 Failed setup of fast-path EQ "
7164 					"(%d), rc = 0x%x\n", fcp_eqidx, rc);
7165 			goto out_destroy_hba_eq;
7166 		}
7167 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7168 				"2584 HBA EQ setup: "
7169 				"queue[%d]-id=%d\n", fcp_eqidx,
7170 				phba->sli4_hba.hba_eq[fcp_eqidx]->queue_id);
7171 	}
7172 
7173 	/* Set up fast-path FCP Response Complete Queue */
7174 	if (!phba->sli4_hba.fcp_cq) {
7175 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7176 				"3148 Fast-path FCP CQ array not "
7177 				"allocated\n");
7178 		rc = -ENOMEM;
7179 		goto out_destroy_hba_eq;
7180 	}
7181 
7182 	for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_io_channel; fcp_cqidx++) {
7183 		if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
7184 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7185 					"0526 Fast-path FCP CQ (%d) not "
7186 					"allocated\n", fcp_cqidx);
7187 			rc = -ENOMEM;
7188 			goto out_destroy_fcp_cq;
7189 		}
7190 		rc = lpfc_cq_create(phba, phba->sli4_hba.fcp_cq[fcp_cqidx],
7191 			phba->sli4_hba.hba_eq[fcp_cqidx], LPFC_WCQ, LPFC_FCP);
7192 		if (rc) {
7193 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7194 					"0527 Failed setup of fast-path FCP "
7195 					"CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
7196 			goto out_destroy_fcp_cq;
7197 		}
7198 
7199 		/* Setup fcp_cq_map for fast lookup */
7200 		phba->sli4_hba.fcp_cq_map[fcp_cqidx] =
7201 				phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id;
7202 
7203 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7204 				"2588 FCP CQ setup: cq[%d]-id=%d, "
7205 				"parent seq[%d]-id=%d\n",
7206 				fcp_cqidx,
7207 				phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
7208 				fcp_cqidx,
7209 				phba->sli4_hba.hba_eq[fcp_cqidx]->queue_id);
7210 	}
7211 
7212 	/* Set up fast-path FCP Work Queue */
7213 	if (!phba->sli4_hba.fcp_wq) {
7214 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7215 				"3149 Fast-path FCP WQ array not "
7216 				"allocated\n");
7217 		rc = -ENOMEM;
7218 		goto out_destroy_fcp_cq;
7219 	}
7220 
7221 	for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_io_channel; fcp_wqidx++) {
7222 		if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
7223 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7224 					"0534 Fast-path FCP WQ (%d) not "
7225 					"allocated\n", fcp_wqidx);
7226 			rc = -ENOMEM;
7227 			goto out_destroy_fcp_wq;
7228 		}
7229 		rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
7230 				    phba->sli4_hba.fcp_cq[fcp_wqidx],
7231 				    LPFC_FCP);
7232 		if (rc) {
7233 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7234 					"0535 Failed setup of fast-path FCP "
7235 					"WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
7236 			goto out_destroy_fcp_wq;
7237 		}
7238 
7239 		/* Bind this WQ to the next FCP ring */
7240 		pring = &psli->ring[MAX_SLI3_CONFIGURED_RINGS + fcp_wqidx];
7241 		pring->sli.sli4.wqp = (void *)phba->sli4_hba.fcp_wq[fcp_wqidx];
7242 		phba->sli4_hba.fcp_cq[fcp_wqidx]->pring = pring;
7243 
7244 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7245 				"2591 FCP WQ setup: wq[%d]-id=%d, "
7246 				"parent cq[%d]-id=%d\n",
7247 				fcp_wqidx,
7248 				phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
7249 				fcp_cq_index,
7250 				phba->sli4_hba.fcp_cq[fcp_wqidx]->queue_id);
7251 	}
7252 	/*
7253 	 * Set up Complete Queues (CQs)
7254 	 */
7255 
7256 	/* Set up slow-path MBOX Complete Queue as the first CQ */
7257 	if (!phba->sli4_hba.mbx_cq) {
7258 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7259 				"0528 Mailbox CQ not allocated\n");
7260 		rc = -ENOMEM;
7261 		goto out_destroy_fcp_wq;
7262 	}
7263 	rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq,
7264 			phba->sli4_hba.hba_eq[0], LPFC_MCQ, LPFC_MBOX);
7265 	if (rc) {
7266 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7267 				"0529 Failed setup of slow-path mailbox CQ: "
7268 				"rc = 0x%x\n", rc);
7269 		goto out_destroy_fcp_wq;
7270 	}
7271 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7272 			"2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
7273 			phba->sli4_hba.mbx_cq->queue_id,
7274 			phba->sli4_hba.hba_eq[0]->queue_id);
7275 
7276 	/* Set up slow-path ELS Complete Queue */
7277 	if (!phba->sli4_hba.els_cq) {
7278 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7279 				"0530 ELS CQ not allocated\n");
7280 		rc = -ENOMEM;
7281 		goto out_destroy_mbx_cq;
7282 	}
7283 	rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq,
7284 			phba->sli4_hba.hba_eq[0], LPFC_WCQ, LPFC_ELS);
7285 	if (rc) {
7286 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7287 				"0531 Failed setup of slow-path ELS CQ: "
7288 				"rc = 0x%x\n", rc);
7289 		goto out_destroy_mbx_cq;
7290 	}
7291 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7292 			"2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
7293 			phba->sli4_hba.els_cq->queue_id,
7294 			phba->sli4_hba.hba_eq[0]->queue_id);
7295 
7296 	/*
7297 	 * Set up all the Work Queues (WQs)
7298 	 */
7299 
7300 	/* Set up Mailbox Command Queue */
7301 	if (!phba->sli4_hba.mbx_wq) {
7302 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7303 				"0538 Slow-path MQ not allocated\n");
7304 		rc = -ENOMEM;
7305 		goto out_destroy_els_cq;
7306 	}
7307 	rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
7308 			    phba->sli4_hba.mbx_cq, LPFC_MBOX);
7309 	if (rc) {
7310 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7311 				"0539 Failed setup of slow-path MQ: "
7312 				"rc = 0x%x\n", rc);
7313 		goto out_destroy_els_cq;
7314 	}
7315 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7316 			"2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
7317 			phba->sli4_hba.mbx_wq->queue_id,
7318 			phba->sli4_hba.mbx_cq->queue_id);
7319 
7320 	/* Set up slow-path ELS Work Queue */
7321 	if (!phba->sli4_hba.els_wq) {
7322 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7323 				"0536 Slow-path ELS WQ not allocated\n");
7324 		rc = -ENOMEM;
7325 		goto out_destroy_mbx_wq;
7326 	}
7327 	rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
7328 			    phba->sli4_hba.els_cq, LPFC_ELS);
7329 	if (rc) {
7330 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7331 				"0537 Failed setup of slow-path ELS WQ: "
7332 				"rc = 0x%x\n", rc);
7333 		goto out_destroy_mbx_wq;
7334 	}
7335 
7336 	/* Bind this WQ to the ELS ring */
7337 	pring = &psli->ring[LPFC_ELS_RING];
7338 	pring->sli.sli4.wqp = (void *)phba->sli4_hba.els_wq;
7339 	phba->sli4_hba.els_cq->pring = pring;
7340 
7341 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7342 			"2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
7343 			phba->sli4_hba.els_wq->queue_id,
7344 			phba->sli4_hba.els_cq->queue_id);
7345 
7346 	/*
7347 	 * Create Receive Queue (RQ)
7348 	 */
7349 	if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
7350 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7351 				"0540 Receive Queue not allocated\n");
7352 		rc = -ENOMEM;
7353 		goto out_destroy_els_wq;
7354 	}
7355 
7356 	lpfc_rq_adjust_repost(phba, phba->sli4_hba.hdr_rq, LPFC_ELS_HBQ);
7357 	lpfc_rq_adjust_repost(phba, phba->sli4_hba.dat_rq, LPFC_ELS_HBQ);
7358 
7359 	rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
7360 			    phba->sli4_hba.els_cq, LPFC_USOL);
7361 	if (rc) {
7362 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7363 				"0541 Failed setup of Receive Queue: "
7364 				"rc = 0x%x\n", rc);
7365 		goto out_destroy_fcp_wq;
7366 	}
7367 
7368 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7369 			"2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
7370 			"parent cq-id=%d\n",
7371 			phba->sli4_hba.hdr_rq->queue_id,
7372 			phba->sli4_hba.dat_rq->queue_id,
7373 			phba->sli4_hba.els_cq->queue_id);
7374 	return 0;
7375 
7376 out_destroy_els_wq:
7377 	lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7378 out_destroy_mbx_wq:
7379 	lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
7380 out_destroy_els_cq:
7381 	lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
7382 out_destroy_mbx_cq:
7383 	lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
7384 out_destroy_fcp_wq:
7385 	for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
7386 		lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
7387 out_destroy_fcp_cq:
7388 	for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
7389 		lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
7390 out_destroy_hba_eq:
7391 	for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
7392 		lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[fcp_eqidx]);
7393 out_error:
7394 	return rc;
7395 }
7396 
7397 /**
7398  * lpfc_sli4_queue_unset - Unset all the SLI4 queues
7399  * @phba: pointer to lpfc hba data structure.
7400  *
7401  * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
7402  * operation.
7403  *
7404  * Return codes
7405  *      0 - successful
7406  *      -ENOMEM - No available memory
7407  *      -EIO - The mailbox failed to complete successfully.
7408  **/
7409 void
7410 lpfc_sli4_queue_unset(struct lpfc_hba *phba)
7411 {
7412 	int fcp_qidx;
7413 
7414 	/* Unset mailbox command work queue */
7415 	lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
7416 	/* Unset ELS work queue */
7417 	lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7418 	/* Unset unsolicited receive queue */
7419 	lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
7420 	/* Unset FCP work queue */
7421 	if (phba->sli4_hba.fcp_wq) {
7422 		for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
7423 		     fcp_qidx++)
7424 			lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
7425 	}
7426 	/* Unset mailbox command complete queue */
7427 	lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
7428 	/* Unset ELS complete queue */
7429 	lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
7430 	/* Unset FCP response complete queue */
7431 	if (phba->sli4_hba.fcp_cq) {
7432 		for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
7433 		     fcp_qidx++)
7434 			lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
7435 	}
7436 	/* Unset fast-path event queue */
7437 	if (phba->sli4_hba.hba_eq) {
7438 		for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
7439 		     fcp_qidx++)
7440 			lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[fcp_qidx]);
7441 	}
7442 }
7443 
7444 /**
7445  * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
7446  * @phba: pointer to lpfc hba data structure.
7447  *
7448  * This routine is invoked to allocate and set up a pool of completion queue
7449  * events. The body of the completion queue event is a completion queue entry
7450  * CQE. For now, this pool is used for the interrupt service routine to queue
7451  * the following HBA completion queue events for the worker thread to process:
7452  *   - Mailbox asynchronous events
7453  *   - Receive queue completion unsolicited events
7454  * Later, this can be used for all the slow-path events.
7455  *
7456  * Return codes
7457  *      0 - successful
7458  *      -ENOMEM - No available memory
7459  **/
7460 static int
7461 lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
7462 {
7463 	struct lpfc_cq_event *cq_event;
7464 	int i;
7465 
7466 	for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
7467 		cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
7468 		if (!cq_event)
7469 			goto out_pool_create_fail;
7470 		list_add_tail(&cq_event->list,
7471 			      &phba->sli4_hba.sp_cqe_event_pool);
7472 	}
7473 	return 0;
7474 
7475 out_pool_create_fail:
7476 	lpfc_sli4_cq_event_pool_destroy(phba);
7477 	return -ENOMEM;
7478 }
7479 
7480 /**
7481  * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
7482  * @phba: pointer to lpfc hba data structure.
7483  *
7484  * This routine is invoked to free the pool of completion queue events at
7485  * driver unload time. Note that, it is the responsibility of the driver
7486  * cleanup routine to free all the outstanding completion-queue events
7487  * allocated from this pool back into the pool before invoking this routine
7488  * to destroy the pool.
7489  **/
7490 static void
7491 lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
7492 {
7493 	struct lpfc_cq_event *cq_event, *next_cq_event;
7494 
7495 	list_for_each_entry_safe(cq_event, next_cq_event,
7496 				 &phba->sli4_hba.sp_cqe_event_pool, list) {
7497 		list_del(&cq_event->list);
7498 		kfree(cq_event);
7499 	}
7500 }
7501 
7502 /**
7503  * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
7504  * @phba: pointer to lpfc hba data structure.
7505  *
7506  * This routine is the lock free version of the API invoked to allocate a
7507  * completion-queue event from the free pool.
7508  *
7509  * Return: Pointer to the newly allocated completion-queue event if successful
7510  *         NULL otherwise.
7511  **/
7512 struct lpfc_cq_event *
7513 __lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
7514 {
7515 	struct lpfc_cq_event *cq_event = NULL;
7516 
7517 	list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
7518 			 struct lpfc_cq_event, list);
7519 	return cq_event;
7520 }
7521 
7522 /**
7523  * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
7524  * @phba: pointer to lpfc hba data structure.
7525  *
7526  * This routine is the lock version of the API invoked to allocate a
7527  * completion-queue event from the free pool.
7528  *
7529  * Return: Pointer to the newly allocated completion-queue event if successful
7530  *         NULL otherwise.
7531  **/
7532 struct lpfc_cq_event *
7533 lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
7534 {
7535 	struct lpfc_cq_event *cq_event;
7536 	unsigned long iflags;
7537 
7538 	spin_lock_irqsave(&phba->hbalock, iflags);
7539 	cq_event = __lpfc_sli4_cq_event_alloc(phba);
7540 	spin_unlock_irqrestore(&phba->hbalock, iflags);
7541 	return cq_event;
7542 }
7543 
7544 /**
7545  * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
7546  * @phba: pointer to lpfc hba data structure.
7547  * @cq_event: pointer to the completion queue event to be freed.
7548  *
7549  * This routine is the lock free version of the API invoked to release a
7550  * completion-queue event back into the free pool.
7551  **/
7552 void
7553 __lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
7554 			     struct lpfc_cq_event *cq_event)
7555 {
7556 	list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
7557 }
7558 
7559 /**
7560  * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
7561  * @phba: pointer to lpfc hba data structure.
7562  * @cq_event: pointer to the completion queue event to be freed.
7563  *
7564  * This routine is the lock version of the API invoked to release a
7565  * completion-queue event back into the free pool.
7566  **/
7567 void
7568 lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
7569 			   struct lpfc_cq_event *cq_event)
7570 {
7571 	unsigned long iflags;
7572 	spin_lock_irqsave(&phba->hbalock, iflags);
7573 	__lpfc_sli4_cq_event_release(phba, cq_event);
7574 	spin_unlock_irqrestore(&phba->hbalock, iflags);
7575 }
7576 
7577 /**
7578  * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
7579  * @phba: pointer to lpfc hba data structure.
7580  *
7581  * This routine is to free all the pending completion-queue events to the
7582  * back into the free pool for device reset.
7583  **/
7584 static void
7585 lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
7586 {
7587 	LIST_HEAD(cqelist);
7588 	struct lpfc_cq_event *cqe;
7589 	unsigned long iflags;
7590 
7591 	/* Retrieve all the pending WCQEs from pending WCQE lists */
7592 	spin_lock_irqsave(&phba->hbalock, iflags);
7593 	/* Pending FCP XRI abort events */
7594 	list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
7595 			 &cqelist);
7596 	/* Pending ELS XRI abort events */
7597 	list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
7598 			 &cqelist);
7599 	/* Pending asynnc events */
7600 	list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
7601 			 &cqelist);
7602 	spin_unlock_irqrestore(&phba->hbalock, iflags);
7603 
7604 	while (!list_empty(&cqelist)) {
7605 		list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
7606 		lpfc_sli4_cq_event_release(phba, cqe);
7607 	}
7608 }
7609 
7610 /**
7611  * lpfc_pci_function_reset - Reset pci function.
7612  * @phba: pointer to lpfc hba data structure.
7613  *
7614  * This routine is invoked to request a PCI function reset. It will destroys
7615  * all resources assigned to the PCI function which originates this request.
7616  *
7617  * Return codes
7618  *      0 - successful
7619  *      -ENOMEM - No available memory
7620  *      -EIO - The mailbox failed to complete successfully.
7621  **/
7622 int
7623 lpfc_pci_function_reset(struct lpfc_hba *phba)
7624 {
7625 	LPFC_MBOXQ_t *mboxq;
7626 	uint32_t rc = 0, if_type;
7627 	uint32_t shdr_status, shdr_add_status;
7628 	uint32_t rdy_chk, num_resets = 0, reset_again = 0;
7629 	union lpfc_sli4_cfg_shdr *shdr;
7630 	struct lpfc_register reg_data;
7631 	uint16_t devid;
7632 
7633 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7634 	switch (if_type) {
7635 	case LPFC_SLI_INTF_IF_TYPE_0:
7636 		mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
7637 						       GFP_KERNEL);
7638 		if (!mboxq) {
7639 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7640 					"0494 Unable to allocate memory for "
7641 					"issuing SLI_FUNCTION_RESET mailbox "
7642 					"command\n");
7643 			return -ENOMEM;
7644 		}
7645 
7646 		/* Setup PCI function reset mailbox-ioctl command */
7647 		lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7648 				 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
7649 				 LPFC_SLI4_MBX_EMBED);
7650 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7651 		shdr = (union lpfc_sli4_cfg_shdr *)
7652 			&mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7653 		shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7654 		shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
7655 					 &shdr->response);
7656 		if (rc != MBX_TIMEOUT)
7657 			mempool_free(mboxq, phba->mbox_mem_pool);
7658 		if (shdr_status || shdr_add_status || rc) {
7659 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7660 					"0495 SLI_FUNCTION_RESET mailbox "
7661 					"failed with status x%x add_status x%x,"
7662 					" mbx status x%x\n",
7663 					shdr_status, shdr_add_status, rc);
7664 			rc = -ENXIO;
7665 		}
7666 		break;
7667 	case LPFC_SLI_INTF_IF_TYPE_2:
7668 		for (num_resets = 0;
7669 		     num_resets < MAX_IF_TYPE_2_RESETS;
7670 		     num_resets++) {
7671 			reg_data.word0 = 0;
7672 			bf_set(lpfc_sliport_ctrl_end, &reg_data,
7673 			       LPFC_SLIPORT_LITTLE_ENDIAN);
7674 			bf_set(lpfc_sliport_ctrl_ip, &reg_data,
7675 			       LPFC_SLIPORT_INIT_PORT);
7676 			writel(reg_data.word0, phba->sli4_hba.u.if_type2.
7677 			       CTRLregaddr);
7678 			/* flush */
7679 			pci_read_config_word(phba->pcidev,
7680 					     PCI_DEVICE_ID, &devid);
7681 			/*
7682 			 * Poll the Port Status Register and wait for RDY for
7683 			 * up to 10 seconds.  If the port doesn't respond, treat
7684 			 * it as an error.  If the port responds with RN, start
7685 			 * the loop again.
7686 			 */
7687 			for (rdy_chk = 0; rdy_chk < 1000; rdy_chk++) {
7688 				msleep(10);
7689 				if (lpfc_readl(phba->sli4_hba.u.if_type2.
7690 					      STATUSregaddr, &reg_data.word0)) {
7691 					rc = -ENODEV;
7692 					goto out;
7693 				}
7694 				if (bf_get(lpfc_sliport_status_rn, &reg_data))
7695 					reset_again++;
7696 				if (bf_get(lpfc_sliport_status_rdy, &reg_data))
7697 					break;
7698 			}
7699 
7700 			/*
7701 			 * If the port responds to the init request with
7702 			 * reset needed, delay for a bit and restart the loop.
7703 			 */
7704 			if (reset_again && (rdy_chk < 1000)) {
7705 				msleep(10);
7706 				reset_again = 0;
7707 				continue;
7708 			}
7709 
7710 			/* Detect any port errors. */
7711 			if ((bf_get(lpfc_sliport_status_err, &reg_data)) ||
7712 			    (rdy_chk >= 1000)) {
7713 				phba->work_status[0] = readl(
7714 					phba->sli4_hba.u.if_type2.ERR1regaddr);
7715 				phba->work_status[1] = readl(
7716 					phba->sli4_hba.u.if_type2.ERR2regaddr);
7717 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7718 					"2890 Port error detected during port "
7719 					"reset(%d): wait_tmo:%d ms, "
7720 					"port status reg 0x%x, "
7721 					"error 1=0x%x, error 2=0x%x\n",
7722 					num_resets, rdy_chk*10,
7723 					reg_data.word0,
7724 					phba->work_status[0],
7725 					phba->work_status[1]);
7726 				rc = -ENODEV;
7727 			}
7728 
7729 			/*
7730 			 * Terminate the outer loop provided the Port indicated
7731 			 * ready within 10 seconds.
7732 			 */
7733 			if (rdy_chk < 1000)
7734 				break;
7735 		}
7736 		/* delay driver action following IF_TYPE_2 function reset */
7737 		msleep(100);
7738 		break;
7739 	case LPFC_SLI_INTF_IF_TYPE_1:
7740 	default:
7741 		break;
7742 	}
7743 
7744 out:
7745 	/* Catch the not-ready port failure after a port reset. */
7746 	if (num_resets >= MAX_IF_TYPE_2_RESETS)
7747 		rc = -ENODEV;
7748 
7749 	return rc;
7750 }
7751 
7752 /**
7753  * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
7754  * @phba: pointer to lpfc hba data structure.
7755  *
7756  * This routine is invoked to set up the PCI device memory space for device
7757  * with SLI-4 interface spec.
7758  *
7759  * Return codes
7760  * 	0 - successful
7761  * 	other values - error
7762  **/
7763 static int
7764 lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
7765 {
7766 	struct pci_dev *pdev;
7767 	unsigned long bar0map_len, bar1map_len, bar2map_len;
7768 	int error = -ENODEV;
7769 	uint32_t if_type;
7770 
7771 	/* Obtain PCI device reference */
7772 	if (!phba->pcidev)
7773 		return error;
7774 	else
7775 		pdev = phba->pcidev;
7776 
7777 	/* Set the device DMA mask size */
7778 	if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
7779 	 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
7780 		if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
7781 		 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
7782 			return error;
7783 		}
7784 	}
7785 
7786 	/*
7787 	 * The BARs and register set definitions and offset locations are
7788 	 * dependent on the if_type.
7789 	 */
7790 	if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
7791 				  &phba->sli4_hba.sli_intf.word0)) {
7792 		return error;
7793 	}
7794 
7795 	/* There is no SLI3 failback for SLI4 devices. */
7796 	if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
7797 	    LPFC_SLI_INTF_VALID) {
7798 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7799 				"2894 SLI_INTF reg contents invalid "
7800 				"sli_intf reg 0x%x\n",
7801 				phba->sli4_hba.sli_intf.word0);
7802 		return error;
7803 	}
7804 
7805 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7806 	/*
7807 	 * Get the bus address of SLI4 device Bar regions and the
7808 	 * number of bytes required by each mapping. The mapping of the
7809 	 * particular PCI BARs regions is dependent on the type of
7810 	 * SLI4 device.
7811 	 */
7812 	if (pci_resource_start(pdev, 0)) {
7813 		phba->pci_bar0_map = pci_resource_start(pdev, 0);
7814 		bar0map_len = pci_resource_len(pdev, 0);
7815 
7816 		/*
7817 		 * Map SLI4 PCI Config Space Register base to a kernel virtual
7818 		 * addr
7819 		 */
7820 		phba->sli4_hba.conf_regs_memmap_p =
7821 			ioremap(phba->pci_bar0_map, bar0map_len);
7822 		if (!phba->sli4_hba.conf_regs_memmap_p) {
7823 			dev_printk(KERN_ERR, &pdev->dev,
7824 				   "ioremap failed for SLI4 PCI config "
7825 				   "registers.\n");
7826 			goto out;
7827 		}
7828 		/* Set up BAR0 PCI config space register memory map */
7829 		lpfc_sli4_bar0_register_memmap(phba, if_type);
7830 	} else {
7831 		phba->pci_bar0_map = pci_resource_start(pdev, 1);
7832 		bar0map_len = pci_resource_len(pdev, 1);
7833 		if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
7834 			dev_printk(KERN_ERR, &pdev->dev,
7835 			   "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
7836 			goto out;
7837 		}
7838 		phba->sli4_hba.conf_regs_memmap_p =
7839 				ioremap(phba->pci_bar0_map, bar0map_len);
7840 		if (!phba->sli4_hba.conf_regs_memmap_p) {
7841 			dev_printk(KERN_ERR, &pdev->dev,
7842 				"ioremap failed for SLI4 PCI config "
7843 				"registers.\n");
7844 				goto out;
7845 		}
7846 		lpfc_sli4_bar0_register_memmap(phba, if_type);
7847 	}
7848 
7849 	if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
7850 	    (pci_resource_start(pdev, 2))) {
7851 		/*
7852 		 * Map SLI4 if type 0 HBA Control Register base to a kernel
7853 		 * virtual address and setup the registers.
7854 		 */
7855 		phba->pci_bar1_map = pci_resource_start(pdev, 2);
7856 		bar1map_len = pci_resource_len(pdev, 2);
7857 		phba->sli4_hba.ctrl_regs_memmap_p =
7858 				ioremap(phba->pci_bar1_map, bar1map_len);
7859 		if (!phba->sli4_hba.ctrl_regs_memmap_p) {
7860 			dev_printk(KERN_ERR, &pdev->dev,
7861 			   "ioremap failed for SLI4 HBA control registers.\n");
7862 			goto out_iounmap_conf;
7863 		}
7864 		lpfc_sli4_bar1_register_memmap(phba);
7865 	}
7866 
7867 	if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
7868 	    (pci_resource_start(pdev, 4))) {
7869 		/*
7870 		 * Map SLI4 if type 0 HBA Doorbell Register base to a kernel
7871 		 * virtual address and setup the registers.
7872 		 */
7873 		phba->pci_bar2_map = pci_resource_start(pdev, 4);
7874 		bar2map_len = pci_resource_len(pdev, 4);
7875 		phba->sli4_hba.drbl_regs_memmap_p =
7876 				ioremap(phba->pci_bar2_map, bar2map_len);
7877 		if (!phba->sli4_hba.drbl_regs_memmap_p) {
7878 			dev_printk(KERN_ERR, &pdev->dev,
7879 			   "ioremap failed for SLI4 HBA doorbell registers.\n");
7880 			goto out_iounmap_ctrl;
7881 		}
7882 		error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
7883 		if (error)
7884 			goto out_iounmap_all;
7885 	}
7886 
7887 	return 0;
7888 
7889 out_iounmap_all:
7890 	iounmap(phba->sli4_hba.drbl_regs_memmap_p);
7891 out_iounmap_ctrl:
7892 	iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
7893 out_iounmap_conf:
7894 	iounmap(phba->sli4_hba.conf_regs_memmap_p);
7895 out:
7896 	return error;
7897 }
7898 
7899 /**
7900  * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
7901  * @phba: pointer to lpfc hba data structure.
7902  *
7903  * This routine is invoked to unset the PCI device memory space for device
7904  * with SLI-4 interface spec.
7905  **/
7906 static void
7907 lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
7908 {
7909 	uint32_t if_type;
7910 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7911 
7912 	switch (if_type) {
7913 	case LPFC_SLI_INTF_IF_TYPE_0:
7914 		iounmap(phba->sli4_hba.drbl_regs_memmap_p);
7915 		iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
7916 		iounmap(phba->sli4_hba.conf_regs_memmap_p);
7917 		break;
7918 	case LPFC_SLI_INTF_IF_TYPE_2:
7919 		iounmap(phba->sli4_hba.conf_regs_memmap_p);
7920 		break;
7921 	case LPFC_SLI_INTF_IF_TYPE_1:
7922 	default:
7923 		dev_printk(KERN_ERR, &phba->pcidev->dev,
7924 			   "FATAL - unsupported SLI4 interface type - %d\n",
7925 			   if_type);
7926 		break;
7927 	}
7928 }
7929 
7930 /**
7931  * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
7932  * @phba: pointer to lpfc hba data structure.
7933  *
7934  * This routine is invoked to enable the MSI-X interrupt vectors to device
7935  * with SLI-3 interface specs. The kernel function pci_enable_msix() is
7936  * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
7937  * invoked, enables either all or nothing, depending on the current
7938  * availability of PCI vector resources. The device driver is responsible
7939  * for calling the individual request_irq() to register each MSI-X vector
7940  * with a interrupt handler, which is done in this function. Note that
7941  * later when device is unloading, the driver should always call free_irq()
7942  * on all MSI-X vectors it has done request_irq() on before calling
7943  * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
7944  * will be left with MSI-X enabled and leaks its vectors.
7945  *
7946  * Return codes
7947  *   0 - successful
7948  *   other values - error
7949  **/
7950 static int
7951 lpfc_sli_enable_msix(struct lpfc_hba *phba)
7952 {
7953 	int rc, i;
7954 	LPFC_MBOXQ_t *pmb;
7955 
7956 	/* Set up MSI-X multi-message vectors */
7957 	for (i = 0; i < LPFC_MSIX_VECTORS; i++)
7958 		phba->msix_entries[i].entry = i;
7959 
7960 	/* Configure MSI-X capability structure */
7961 	rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
7962 				ARRAY_SIZE(phba->msix_entries));
7963 	if (rc) {
7964 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7965 				"0420 PCI enable MSI-X failed (%d)\n", rc);
7966 		goto msi_fail_out;
7967 	}
7968 	for (i = 0; i < LPFC_MSIX_VECTORS; i++)
7969 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7970 				"0477 MSI-X entry[%d]: vector=x%x "
7971 				"message=%d\n", i,
7972 				phba->msix_entries[i].vector,
7973 				phba->msix_entries[i].entry);
7974 	/*
7975 	 * Assign MSI-X vectors to interrupt handlers
7976 	 */
7977 
7978 	/* vector-0 is associated to slow-path handler */
7979 	rc = request_irq(phba->msix_entries[0].vector,
7980 			 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
7981 			 LPFC_SP_DRIVER_HANDLER_NAME, phba);
7982 	if (rc) {
7983 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7984 				"0421 MSI-X slow-path request_irq failed "
7985 				"(%d)\n", rc);
7986 		goto msi_fail_out;
7987 	}
7988 
7989 	/* vector-1 is associated to fast-path handler */
7990 	rc = request_irq(phba->msix_entries[1].vector,
7991 			 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
7992 			 LPFC_FP_DRIVER_HANDLER_NAME, phba);
7993 
7994 	if (rc) {
7995 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7996 				"0429 MSI-X fast-path request_irq failed "
7997 				"(%d)\n", rc);
7998 		goto irq_fail_out;
7999 	}
8000 
8001 	/*
8002 	 * Configure HBA MSI-X attention conditions to messages
8003 	 */
8004 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8005 
8006 	if (!pmb) {
8007 		rc = -ENOMEM;
8008 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8009 				"0474 Unable to allocate memory for issuing "
8010 				"MBOX_CONFIG_MSI command\n");
8011 		goto mem_fail_out;
8012 	}
8013 	rc = lpfc_config_msi(phba, pmb);
8014 	if (rc)
8015 		goto mbx_fail_out;
8016 	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
8017 	if (rc != MBX_SUCCESS) {
8018 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
8019 				"0351 Config MSI mailbox command failed, "
8020 				"mbxCmd x%x, mbxStatus x%x\n",
8021 				pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
8022 		goto mbx_fail_out;
8023 	}
8024 
8025 	/* Free memory allocated for mailbox command */
8026 	mempool_free(pmb, phba->mbox_mem_pool);
8027 	return rc;
8028 
8029 mbx_fail_out:
8030 	/* Free memory allocated for mailbox command */
8031 	mempool_free(pmb, phba->mbox_mem_pool);
8032 
8033 mem_fail_out:
8034 	/* free the irq already requested */
8035 	free_irq(phba->msix_entries[1].vector, phba);
8036 
8037 irq_fail_out:
8038 	/* free the irq already requested */
8039 	free_irq(phba->msix_entries[0].vector, phba);
8040 
8041 msi_fail_out:
8042 	/* Unconfigure MSI-X capability structure */
8043 	pci_disable_msix(phba->pcidev);
8044 	return rc;
8045 }
8046 
8047 /**
8048  * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
8049  * @phba: pointer to lpfc hba data structure.
8050  *
8051  * This routine is invoked to release the MSI-X vectors and then disable the
8052  * MSI-X interrupt mode to device with SLI-3 interface spec.
8053  **/
8054 static void
8055 lpfc_sli_disable_msix(struct lpfc_hba *phba)
8056 {
8057 	int i;
8058 
8059 	/* Free up MSI-X multi-message vectors */
8060 	for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8061 		free_irq(phba->msix_entries[i].vector, phba);
8062 	/* Disable MSI-X */
8063 	pci_disable_msix(phba->pcidev);
8064 
8065 	return;
8066 }
8067 
8068 /**
8069  * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
8070  * @phba: pointer to lpfc hba data structure.
8071  *
8072  * This routine is invoked to enable the MSI interrupt mode to device with
8073  * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
8074  * enable the MSI vector. The device driver is responsible for calling the
8075  * request_irq() to register MSI vector with a interrupt the handler, which
8076  * is done in this function.
8077  *
8078  * Return codes
8079  * 	0 - successful
8080  * 	other values - error
8081  */
8082 static int
8083 lpfc_sli_enable_msi(struct lpfc_hba *phba)
8084 {
8085 	int rc;
8086 
8087 	rc = pci_enable_msi(phba->pcidev);
8088 	if (!rc)
8089 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8090 				"0462 PCI enable MSI mode success.\n");
8091 	else {
8092 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8093 				"0471 PCI enable MSI mode failed (%d)\n", rc);
8094 		return rc;
8095 	}
8096 
8097 	rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
8098 			 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8099 	if (rc) {
8100 		pci_disable_msi(phba->pcidev);
8101 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8102 				"0478 MSI request_irq failed (%d)\n", rc);
8103 	}
8104 	return rc;
8105 }
8106 
8107 /**
8108  * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
8109  * @phba: pointer to lpfc hba data structure.
8110  *
8111  * This routine is invoked to disable the MSI interrupt mode to device with
8112  * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
8113  * done request_irq() on before calling pci_disable_msi(). Failure to do so
8114  * results in a BUG_ON() and a device will be left with MSI enabled and leaks
8115  * its vector.
8116  */
8117 static void
8118 lpfc_sli_disable_msi(struct lpfc_hba *phba)
8119 {
8120 	free_irq(phba->pcidev->irq, phba);
8121 	pci_disable_msi(phba->pcidev);
8122 	return;
8123 }
8124 
8125 /**
8126  * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
8127  * @phba: pointer to lpfc hba data structure.
8128  *
8129  * This routine is invoked to enable device interrupt and associate driver's
8130  * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
8131  * spec. Depends on the interrupt mode configured to the driver, the driver
8132  * will try to fallback from the configured interrupt mode to an interrupt
8133  * mode which is supported by the platform, kernel, and device in the order
8134  * of:
8135  * MSI-X -> MSI -> IRQ.
8136  *
8137  * Return codes
8138  *   0 - successful
8139  *   other values - error
8140  **/
8141 static uint32_t
8142 lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
8143 {
8144 	uint32_t intr_mode = LPFC_INTR_ERROR;
8145 	int retval;
8146 
8147 	if (cfg_mode == 2) {
8148 		/* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
8149 		retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
8150 		if (!retval) {
8151 			/* Now, try to enable MSI-X interrupt mode */
8152 			retval = lpfc_sli_enable_msix(phba);
8153 			if (!retval) {
8154 				/* Indicate initialization to MSI-X mode */
8155 				phba->intr_type = MSIX;
8156 				intr_mode = 2;
8157 			}
8158 		}
8159 	}
8160 
8161 	/* Fallback to MSI if MSI-X initialization failed */
8162 	if (cfg_mode >= 1 && phba->intr_type == NONE) {
8163 		retval = lpfc_sli_enable_msi(phba);
8164 		if (!retval) {
8165 			/* Indicate initialization to MSI mode */
8166 			phba->intr_type = MSI;
8167 			intr_mode = 1;
8168 		}
8169 	}
8170 
8171 	/* Fallback to INTx if both MSI-X/MSI initalization failed */
8172 	if (phba->intr_type == NONE) {
8173 		retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
8174 				     IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8175 		if (!retval) {
8176 			/* Indicate initialization to INTx mode */
8177 			phba->intr_type = INTx;
8178 			intr_mode = 0;
8179 		}
8180 	}
8181 	return intr_mode;
8182 }
8183 
8184 /**
8185  * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
8186  * @phba: pointer to lpfc hba data structure.
8187  *
8188  * This routine is invoked to disable device interrupt and disassociate the
8189  * driver's interrupt handler(s) from interrupt vector(s) to device with
8190  * SLI-3 interface spec. Depending on the interrupt mode, the driver will
8191  * release the interrupt vector(s) for the message signaled interrupt.
8192  **/
8193 static void
8194 lpfc_sli_disable_intr(struct lpfc_hba *phba)
8195 {
8196 	/* Disable the currently initialized interrupt mode */
8197 	if (phba->intr_type == MSIX)
8198 		lpfc_sli_disable_msix(phba);
8199 	else if (phba->intr_type == MSI)
8200 		lpfc_sli_disable_msi(phba);
8201 	else if (phba->intr_type == INTx)
8202 		free_irq(phba->pcidev->irq, phba);
8203 
8204 	/* Reset interrupt management states */
8205 	phba->intr_type = NONE;
8206 	phba->sli.slistat.sli_intr = 0;
8207 
8208 	return;
8209 }
8210 
8211 /**
8212  * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
8213  * @phba: pointer to lpfc hba data structure.
8214  *
8215  * This routine is invoked to enable the MSI-X interrupt vectors to device
8216  * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
8217  * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
8218  * enables either all or nothing, depending on the current availability of
8219  * PCI vector resources. The device driver is responsible for calling the
8220  * individual request_irq() to register each MSI-X vector with a interrupt
8221  * handler, which is done in this function. Note that later when device is
8222  * unloading, the driver should always call free_irq() on all MSI-X vectors
8223  * it has done request_irq() on before calling pci_disable_msix(). Failure
8224  * to do so results in a BUG_ON() and a device will be left with MSI-X
8225  * enabled and leaks its vectors.
8226  *
8227  * Return codes
8228  * 0 - successful
8229  * other values - error
8230  **/
8231 static int
8232 lpfc_sli4_enable_msix(struct lpfc_hba *phba)
8233 {
8234 	int vectors, rc, index;
8235 
8236 	/* Set up MSI-X multi-message vectors */
8237 	for (index = 0; index < phba->cfg_fcp_io_channel; index++)
8238 		phba->sli4_hba.msix_entries[index].entry = index;
8239 
8240 	/* Configure MSI-X capability structure */
8241 	vectors = phba->cfg_fcp_io_channel;
8242 enable_msix_vectors:
8243 	rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
8244 			     vectors);
8245 	if (rc > 1) {
8246 		vectors = rc;
8247 		goto enable_msix_vectors;
8248 	} else if (rc) {
8249 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8250 				"0484 PCI enable MSI-X failed (%d)\n", rc);
8251 		goto msi_fail_out;
8252 	}
8253 
8254 	/* Log MSI-X vector assignment */
8255 	for (index = 0; index < vectors; index++)
8256 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8257 				"0489 MSI-X entry[%d]: vector=x%x "
8258 				"message=%d\n", index,
8259 				phba->sli4_hba.msix_entries[index].vector,
8260 				phba->sli4_hba.msix_entries[index].entry);
8261 
8262 	/*
8263 	 * Assign MSI-X vectors to interrupt handlers
8264 	 */
8265 	for (index = 0; index < vectors; index++) {
8266 		memset(&phba->sli4_hba.handler_name[index], 0, 16);
8267 		sprintf((char *)&phba->sli4_hba.handler_name[index],
8268 			 LPFC_DRIVER_HANDLER_NAME"%d", index);
8269 
8270 		phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8271 		phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
8272 		atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].fcp_eq_in_use, 1);
8273 		rc = request_irq(phba->sli4_hba.msix_entries[index].vector,
8274 				 &lpfc_sli4_hba_intr_handler, IRQF_SHARED,
8275 				 (char *)&phba->sli4_hba.handler_name[index],
8276 				 &phba->sli4_hba.fcp_eq_hdl[index]);
8277 		if (rc) {
8278 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8279 					"0486 MSI-X fast-path (%d) "
8280 					"request_irq failed (%d)\n", index, rc);
8281 			goto cfg_fail_out;
8282 		}
8283 	}
8284 
8285 	if (vectors != phba->cfg_fcp_io_channel) {
8286 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8287 				"3238 Reducing IO channels to match number of "
8288 				"MSI-X vectors, requested %d got %d\n",
8289 				phba->cfg_fcp_io_channel, vectors);
8290 		phba->cfg_fcp_io_channel = vectors;
8291 	}
8292 	return rc;
8293 
8294 cfg_fail_out:
8295 	/* free the irq already requested */
8296 	for (--index; index >= 0; index--)
8297 		free_irq(phba->sli4_hba.msix_entries[index].vector,
8298 			 &phba->sli4_hba.fcp_eq_hdl[index]);
8299 
8300 msi_fail_out:
8301 	/* Unconfigure MSI-X capability structure */
8302 	pci_disable_msix(phba->pcidev);
8303 	return rc;
8304 }
8305 
8306 /**
8307  * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
8308  * @phba: pointer to lpfc hba data structure.
8309  *
8310  * This routine is invoked to release the MSI-X vectors and then disable the
8311  * MSI-X interrupt mode to device with SLI-4 interface spec.
8312  **/
8313 static void
8314 lpfc_sli4_disable_msix(struct lpfc_hba *phba)
8315 {
8316 	int index;
8317 
8318 	/* Free up MSI-X multi-message vectors */
8319 	for (index = 0; index < phba->cfg_fcp_io_channel; index++)
8320 		free_irq(phba->sli4_hba.msix_entries[index].vector,
8321 			 &phba->sli4_hba.fcp_eq_hdl[index]);
8322 
8323 	/* Disable MSI-X */
8324 	pci_disable_msix(phba->pcidev);
8325 
8326 	return;
8327 }
8328 
8329 /**
8330  * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
8331  * @phba: pointer to lpfc hba data structure.
8332  *
8333  * This routine is invoked to enable the MSI interrupt mode to device with
8334  * SLI-4 interface spec. The kernel function pci_enable_msi() is called
8335  * to enable the MSI vector. The device driver is responsible for calling
8336  * the request_irq() to register MSI vector with a interrupt the handler,
8337  * which is done in this function.
8338  *
8339  * Return codes
8340  * 	0 - successful
8341  * 	other values - error
8342  **/
8343 static int
8344 lpfc_sli4_enable_msi(struct lpfc_hba *phba)
8345 {
8346 	int rc, index;
8347 
8348 	rc = pci_enable_msi(phba->pcidev);
8349 	if (!rc)
8350 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8351 				"0487 PCI enable MSI mode success.\n");
8352 	else {
8353 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8354 				"0488 PCI enable MSI mode failed (%d)\n", rc);
8355 		return rc;
8356 	}
8357 
8358 	rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
8359 			 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8360 	if (rc) {
8361 		pci_disable_msi(phba->pcidev);
8362 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8363 				"0490 MSI request_irq failed (%d)\n", rc);
8364 		return rc;
8365 	}
8366 
8367 	for (index = 0; index < phba->cfg_fcp_io_channel; index++) {
8368 		phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8369 		phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
8370 	}
8371 
8372 	return 0;
8373 }
8374 
8375 /**
8376  * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
8377  * @phba: pointer to lpfc hba data structure.
8378  *
8379  * This routine is invoked to disable the MSI interrupt mode to device with
8380  * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
8381  * done request_irq() on before calling pci_disable_msi(). Failure to do so
8382  * results in a BUG_ON() and a device will be left with MSI enabled and leaks
8383  * its vector.
8384  **/
8385 static void
8386 lpfc_sli4_disable_msi(struct lpfc_hba *phba)
8387 {
8388 	free_irq(phba->pcidev->irq, phba);
8389 	pci_disable_msi(phba->pcidev);
8390 	return;
8391 }
8392 
8393 /**
8394  * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
8395  * @phba: pointer to lpfc hba data structure.
8396  *
8397  * This routine is invoked to enable device interrupt and associate driver's
8398  * interrupt handler(s) to interrupt vector(s) to device with SLI-4
8399  * interface spec. Depends on the interrupt mode configured to the driver,
8400  * the driver will try to fallback from the configured interrupt mode to an
8401  * interrupt mode which is supported by the platform, kernel, and device in
8402  * the order of:
8403  * MSI-X -> MSI -> IRQ.
8404  *
8405  * Return codes
8406  * 	0 - successful
8407  * 	other values - error
8408  **/
8409 static uint32_t
8410 lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
8411 {
8412 	uint32_t intr_mode = LPFC_INTR_ERROR;
8413 	int retval, index;
8414 
8415 	if (cfg_mode == 2) {
8416 		/* Preparation before conf_msi mbox cmd */
8417 		retval = 0;
8418 		if (!retval) {
8419 			/* Now, try to enable MSI-X interrupt mode */
8420 			retval = lpfc_sli4_enable_msix(phba);
8421 			if (!retval) {
8422 				/* Indicate initialization to MSI-X mode */
8423 				phba->intr_type = MSIX;
8424 				intr_mode = 2;
8425 			}
8426 		}
8427 	}
8428 
8429 	/* Fallback to MSI if MSI-X initialization failed */
8430 	if (cfg_mode >= 1 && phba->intr_type == NONE) {
8431 		retval = lpfc_sli4_enable_msi(phba);
8432 		if (!retval) {
8433 			/* Indicate initialization to MSI mode */
8434 			phba->intr_type = MSI;
8435 			intr_mode = 1;
8436 		}
8437 	}
8438 
8439 	/* Fallback to INTx if both MSI-X/MSI initalization failed */
8440 	if (phba->intr_type == NONE) {
8441 		retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
8442 				     IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8443 		if (!retval) {
8444 			/* Indicate initialization to INTx mode */
8445 			phba->intr_type = INTx;
8446 			intr_mode = 0;
8447 			for (index = 0; index < phba->cfg_fcp_io_channel;
8448 			     index++) {
8449 				phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8450 				phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
8451 				atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].
8452 					fcp_eq_in_use, 1);
8453 			}
8454 		}
8455 	}
8456 	return intr_mode;
8457 }
8458 
8459 /**
8460  * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
8461  * @phba: pointer to lpfc hba data structure.
8462  *
8463  * This routine is invoked to disable device interrupt and disassociate
8464  * the driver's interrupt handler(s) from interrupt vector(s) to device
8465  * with SLI-4 interface spec. Depending on the interrupt mode, the driver
8466  * will release the interrupt vector(s) for the message signaled interrupt.
8467  **/
8468 static void
8469 lpfc_sli4_disable_intr(struct lpfc_hba *phba)
8470 {
8471 	/* Disable the currently initialized interrupt mode */
8472 	if (phba->intr_type == MSIX)
8473 		lpfc_sli4_disable_msix(phba);
8474 	else if (phba->intr_type == MSI)
8475 		lpfc_sli4_disable_msi(phba);
8476 	else if (phba->intr_type == INTx)
8477 		free_irq(phba->pcidev->irq, phba);
8478 
8479 	/* Reset interrupt management states */
8480 	phba->intr_type = NONE;
8481 	phba->sli.slistat.sli_intr = 0;
8482 
8483 	return;
8484 }
8485 
8486 /**
8487  * lpfc_unset_hba - Unset SLI3 hba device initialization
8488  * @phba: pointer to lpfc hba data structure.
8489  *
8490  * This routine is invoked to unset the HBA device initialization steps to
8491  * a device with SLI-3 interface spec.
8492  **/
8493 static void
8494 lpfc_unset_hba(struct lpfc_hba *phba)
8495 {
8496 	struct lpfc_vport *vport = phba->pport;
8497 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
8498 
8499 	spin_lock_irq(shost->host_lock);
8500 	vport->load_flag |= FC_UNLOADING;
8501 	spin_unlock_irq(shost->host_lock);
8502 
8503 	kfree(phba->vpi_bmask);
8504 	kfree(phba->vpi_ids);
8505 
8506 	lpfc_stop_hba_timers(phba);
8507 
8508 	phba->pport->work_port_events = 0;
8509 
8510 	lpfc_sli_hba_down(phba);
8511 
8512 	lpfc_sli_brdrestart(phba);
8513 
8514 	lpfc_sli_disable_intr(phba);
8515 
8516 	return;
8517 }
8518 
8519 /**
8520  * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
8521  * @phba: Pointer to HBA context object.
8522  *
8523  * This function is called in the SLI4 code path to wait for completion
8524  * of device's XRIs exchange busy. It will check the XRI exchange busy
8525  * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
8526  * that, it will check the XRI exchange busy on outstanding FCP and ELS
8527  * I/Os every 30 seconds, log error message, and wait forever. Only when
8528  * all XRI exchange busy complete, the driver unload shall proceed with
8529  * invoking the function reset ioctl mailbox command to the CNA and the
8530  * the rest of the driver unload resource release.
8531  **/
8532 static void
8533 lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
8534 {
8535 	int wait_time = 0;
8536 	int fcp_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
8537 	int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
8538 
8539 	while (!fcp_xri_cmpl || !els_xri_cmpl) {
8540 		if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
8541 			if (!fcp_xri_cmpl)
8542 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8543 						"2877 FCP XRI exchange busy "
8544 						"wait time: %d seconds.\n",
8545 						wait_time/1000);
8546 			if (!els_xri_cmpl)
8547 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8548 						"2878 ELS XRI exchange busy "
8549 						"wait time: %d seconds.\n",
8550 						wait_time/1000);
8551 			msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
8552 			wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
8553 		} else {
8554 			msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
8555 			wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
8556 		}
8557 		fcp_xri_cmpl =
8558 			list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
8559 		els_xri_cmpl =
8560 			list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
8561 	}
8562 }
8563 
8564 /**
8565  * lpfc_sli4_hba_unset - Unset the fcoe hba
8566  * @phba: Pointer to HBA context object.
8567  *
8568  * This function is called in the SLI4 code path to reset the HBA's FCoE
8569  * function. The caller is not required to hold any lock. This routine
8570  * issues PCI function reset mailbox command to reset the FCoE function.
8571  * At the end of the function, it calls lpfc_hba_down_post function to
8572  * free any pending commands.
8573  **/
8574 static void
8575 lpfc_sli4_hba_unset(struct lpfc_hba *phba)
8576 {
8577 	int wait_cnt = 0;
8578 	LPFC_MBOXQ_t *mboxq;
8579 	struct pci_dev *pdev = phba->pcidev;
8580 
8581 	lpfc_stop_hba_timers(phba);
8582 	phba->sli4_hba.intr_enable = 0;
8583 
8584 	/*
8585 	 * Gracefully wait out the potential current outstanding asynchronous
8586 	 * mailbox command.
8587 	 */
8588 
8589 	/* First, block any pending async mailbox command from posted */
8590 	spin_lock_irq(&phba->hbalock);
8591 	phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
8592 	spin_unlock_irq(&phba->hbalock);
8593 	/* Now, trying to wait it out if we can */
8594 	while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8595 		msleep(10);
8596 		if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
8597 			break;
8598 	}
8599 	/* Forcefully release the outstanding mailbox command if timed out */
8600 	if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8601 		spin_lock_irq(&phba->hbalock);
8602 		mboxq = phba->sli.mbox_active;
8603 		mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8604 		__lpfc_mbox_cmpl_put(phba, mboxq);
8605 		phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8606 		phba->sli.mbox_active = NULL;
8607 		spin_unlock_irq(&phba->hbalock);
8608 	}
8609 
8610 	/* Abort all iocbs associated with the hba */
8611 	lpfc_sli_hba_iocb_abort(phba);
8612 
8613 	/* Wait for completion of device XRI exchange busy */
8614 	lpfc_sli4_xri_exchange_busy_wait(phba);
8615 
8616 	/* Disable PCI subsystem interrupt */
8617 	lpfc_sli4_disable_intr(phba);
8618 
8619 	/* Disable SR-IOV if enabled */
8620 	if (phba->cfg_sriov_nr_virtfn)
8621 		pci_disable_sriov(pdev);
8622 
8623 	/* Stop kthread signal shall trigger work_done one more time */
8624 	kthread_stop(phba->worker_thread);
8625 
8626 	/* Reset SLI4 HBA FCoE function */
8627 	lpfc_pci_function_reset(phba);
8628 	lpfc_sli4_queue_destroy(phba);
8629 
8630 	/* Stop the SLI4 device port */
8631 	phba->pport->work_port_events = 0;
8632 }
8633 
8634  /**
8635  * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
8636  * @phba: Pointer to HBA context object.
8637  * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
8638  *
8639  * This function is called in the SLI4 code path to read the port's
8640  * sli4 capabilities.
8641  *
8642  * This function may be be called from any context that can block-wait
8643  * for the completion.  The expectation is that this routine is called
8644  * typically from probe_one or from the online routine.
8645  **/
8646 int
8647 lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8648 {
8649 	int rc;
8650 	struct lpfc_mqe *mqe;
8651 	struct lpfc_pc_sli4_params *sli4_params;
8652 	uint32_t mbox_tmo;
8653 
8654 	rc = 0;
8655 	mqe = &mboxq->u.mqe;
8656 
8657 	/* Read the port's SLI4 Parameters port capabilities */
8658 	lpfc_pc_sli4_params(mboxq);
8659 	if (!phba->sli4_hba.intr_enable)
8660 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8661 	else {
8662 		mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
8663 		rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
8664 	}
8665 
8666 	if (unlikely(rc))
8667 		return 1;
8668 
8669 	sli4_params = &phba->sli4_hba.pc_sli4_params;
8670 	sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
8671 	sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
8672 	sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
8673 	sli4_params->featurelevel_1 = bf_get(featurelevel_1,
8674 					     &mqe->un.sli4_params);
8675 	sli4_params->featurelevel_2 = bf_get(featurelevel_2,
8676 					     &mqe->un.sli4_params);
8677 	sli4_params->proto_types = mqe->un.sli4_params.word3;
8678 	sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
8679 	sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
8680 	sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
8681 	sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
8682 	sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
8683 	sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
8684 	sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
8685 	sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
8686 	sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
8687 	sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
8688 	sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
8689 	sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
8690 	sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
8691 	sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
8692 	sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
8693 	sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
8694 	sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
8695 	sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
8696 	sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
8697 	sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
8698 
8699 	/* Make sure that sge_supp_len can be handled by the driver */
8700 	if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
8701 		sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
8702 
8703 	return rc;
8704 }
8705 
8706 /**
8707  * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
8708  * @phba: Pointer to HBA context object.
8709  * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
8710  *
8711  * This function is called in the SLI4 code path to read the port's
8712  * sli4 capabilities.
8713  *
8714  * This function may be be called from any context that can block-wait
8715  * for the completion.  The expectation is that this routine is called
8716  * typically from probe_one or from the online routine.
8717  **/
8718 int
8719 lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8720 {
8721 	int rc;
8722 	struct lpfc_mqe *mqe = &mboxq->u.mqe;
8723 	struct lpfc_pc_sli4_params *sli4_params;
8724 	uint32_t mbox_tmo;
8725 	int length;
8726 	struct lpfc_sli4_parameters *mbx_sli4_parameters;
8727 
8728 	/*
8729 	 * By default, the driver assumes the SLI4 port requires RPI
8730 	 * header postings.  The SLI4_PARAM response will correct this
8731 	 * assumption.
8732 	 */
8733 	phba->sli4_hba.rpi_hdrs_in_use = 1;
8734 
8735 	/* Read the port's SLI4 Config Parameters */
8736 	length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
8737 		  sizeof(struct lpfc_sli4_cfg_mhdr));
8738 	lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
8739 			 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
8740 			 length, LPFC_SLI4_MBX_EMBED);
8741 	if (!phba->sli4_hba.intr_enable)
8742 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8743 	else {
8744 		mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
8745 		rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
8746 	}
8747 	if (unlikely(rc))
8748 		return rc;
8749 	sli4_params = &phba->sli4_hba.pc_sli4_params;
8750 	mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
8751 	sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
8752 	sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
8753 	sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
8754 	sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
8755 					     mbx_sli4_parameters);
8756 	sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
8757 					     mbx_sli4_parameters);
8758 	if (bf_get(cfg_phwq, mbx_sli4_parameters))
8759 		phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
8760 	else
8761 		phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
8762 	sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
8763 	sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
8764 	sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
8765 	sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
8766 	sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
8767 	sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
8768 	sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
8769 					    mbx_sli4_parameters);
8770 	sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
8771 					   mbx_sli4_parameters);
8772 	phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
8773 	phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
8774 
8775 	/* Make sure that sge_supp_len can be handled by the driver */
8776 	if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
8777 		sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
8778 
8779 	return 0;
8780 }
8781 
8782 /**
8783  * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
8784  * @pdev: pointer to PCI device
8785  * @pid: pointer to PCI device identifier
8786  *
8787  * This routine is to be called to attach a device with SLI-3 interface spec
8788  * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
8789  * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
8790  * information of the device and driver to see if the driver state that it can
8791  * support this kind of device. If the match is successful, the driver core
8792  * invokes this routine. If this routine determines it can claim the HBA, it
8793  * does all the initialization that it needs to do to handle the HBA properly.
8794  *
8795  * Return code
8796  * 	0 - driver can claim the device
8797  * 	negative value - driver can not claim the device
8798  **/
8799 static int
8800 lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
8801 {
8802 	struct lpfc_hba   *phba;
8803 	struct lpfc_vport *vport = NULL;
8804 	struct Scsi_Host  *shost = NULL;
8805 	int error;
8806 	uint32_t cfg_mode, intr_mode;
8807 
8808 	/* Allocate memory for HBA structure */
8809 	phba = lpfc_hba_alloc(pdev);
8810 	if (!phba)
8811 		return -ENOMEM;
8812 
8813 	/* Perform generic PCI device enabling operation */
8814 	error = lpfc_enable_pci_dev(phba);
8815 	if (error)
8816 		goto out_free_phba;
8817 
8818 	/* Set up SLI API function jump table for PCI-device group-0 HBAs */
8819 	error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
8820 	if (error)
8821 		goto out_disable_pci_dev;
8822 
8823 	/* Set up SLI-3 specific device PCI memory space */
8824 	error = lpfc_sli_pci_mem_setup(phba);
8825 	if (error) {
8826 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8827 				"1402 Failed to set up pci memory space.\n");
8828 		goto out_disable_pci_dev;
8829 	}
8830 
8831 	/* Set up phase-1 common device driver resources */
8832 	error = lpfc_setup_driver_resource_phase1(phba);
8833 	if (error) {
8834 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8835 				"1403 Failed to set up driver resource.\n");
8836 		goto out_unset_pci_mem_s3;
8837 	}
8838 
8839 	/* Set up SLI-3 specific device driver resources */
8840 	error = lpfc_sli_driver_resource_setup(phba);
8841 	if (error) {
8842 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8843 				"1404 Failed to set up driver resource.\n");
8844 		goto out_unset_pci_mem_s3;
8845 	}
8846 
8847 	/* Initialize and populate the iocb list per host */
8848 	error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
8849 	if (error) {
8850 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8851 				"1405 Failed to initialize iocb list.\n");
8852 		goto out_unset_driver_resource_s3;
8853 	}
8854 
8855 	/* Set up common device driver resources */
8856 	error = lpfc_setup_driver_resource_phase2(phba);
8857 	if (error) {
8858 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8859 				"1406 Failed to set up driver resource.\n");
8860 		goto out_free_iocb_list;
8861 	}
8862 
8863 	/* Get the default values for Model Name and Description */
8864 	lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
8865 
8866 	/* Create SCSI host to the physical port */
8867 	error = lpfc_create_shost(phba);
8868 	if (error) {
8869 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8870 				"1407 Failed to create scsi host.\n");
8871 		goto out_unset_driver_resource;
8872 	}
8873 
8874 	/* Configure sysfs attributes */
8875 	vport = phba->pport;
8876 	error = lpfc_alloc_sysfs_attr(vport);
8877 	if (error) {
8878 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8879 				"1476 Failed to allocate sysfs attr\n");
8880 		goto out_destroy_shost;
8881 	}
8882 
8883 	shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
8884 	/* Now, trying to enable interrupt and bring up the device */
8885 	cfg_mode = phba->cfg_use_msi;
8886 	while (true) {
8887 		/* Put device to a known state before enabling interrupt */
8888 		lpfc_stop_port(phba);
8889 		/* Configure and enable interrupt */
8890 		intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
8891 		if (intr_mode == LPFC_INTR_ERROR) {
8892 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8893 					"0431 Failed to enable interrupt.\n");
8894 			error = -ENODEV;
8895 			goto out_free_sysfs_attr;
8896 		}
8897 		/* SLI-3 HBA setup */
8898 		if (lpfc_sli_hba_setup(phba)) {
8899 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8900 					"1477 Failed to set up hba\n");
8901 			error = -ENODEV;
8902 			goto out_remove_device;
8903 		}
8904 
8905 		/* Wait 50ms for the interrupts of previous mailbox commands */
8906 		msleep(50);
8907 		/* Check active interrupts on message signaled interrupts */
8908 		if (intr_mode == 0 ||
8909 		    phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
8910 			/* Log the current active interrupt mode */
8911 			phba->intr_mode = intr_mode;
8912 			lpfc_log_intr_mode(phba, intr_mode);
8913 			break;
8914 		} else {
8915 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8916 					"0447 Configure interrupt mode (%d) "
8917 					"failed active interrupt test.\n",
8918 					intr_mode);
8919 			/* Disable the current interrupt mode */
8920 			lpfc_sli_disable_intr(phba);
8921 			/* Try next level of interrupt mode */
8922 			cfg_mode = --intr_mode;
8923 		}
8924 	}
8925 
8926 	/* Perform post initialization setup */
8927 	lpfc_post_init_setup(phba);
8928 
8929 	/* Check if there are static vports to be created. */
8930 	lpfc_create_static_vport(phba);
8931 
8932 	return 0;
8933 
8934 out_remove_device:
8935 	lpfc_unset_hba(phba);
8936 out_free_sysfs_attr:
8937 	lpfc_free_sysfs_attr(vport);
8938 out_destroy_shost:
8939 	lpfc_destroy_shost(phba);
8940 out_unset_driver_resource:
8941 	lpfc_unset_driver_resource_phase2(phba);
8942 out_free_iocb_list:
8943 	lpfc_free_iocb_list(phba);
8944 out_unset_driver_resource_s3:
8945 	lpfc_sli_driver_resource_unset(phba);
8946 out_unset_pci_mem_s3:
8947 	lpfc_sli_pci_mem_unset(phba);
8948 out_disable_pci_dev:
8949 	lpfc_disable_pci_dev(phba);
8950 	if (shost)
8951 		scsi_host_put(shost);
8952 out_free_phba:
8953 	lpfc_hba_free(phba);
8954 	return error;
8955 }
8956 
8957 /**
8958  * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
8959  * @pdev: pointer to PCI device
8960  *
8961  * This routine is to be called to disattach a device with SLI-3 interface
8962  * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
8963  * removed from PCI bus, it performs all the necessary cleanup for the HBA
8964  * device to be removed from the PCI subsystem properly.
8965  **/
8966 static void
8967 lpfc_pci_remove_one_s3(struct pci_dev *pdev)
8968 {
8969 	struct Scsi_Host  *shost = pci_get_drvdata(pdev);
8970 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
8971 	struct lpfc_vport **vports;
8972 	struct lpfc_hba   *phba = vport->phba;
8973 	int i;
8974 	int bars = pci_select_bars(pdev, IORESOURCE_MEM);
8975 
8976 	spin_lock_irq(&phba->hbalock);
8977 	vport->load_flag |= FC_UNLOADING;
8978 	spin_unlock_irq(&phba->hbalock);
8979 
8980 	lpfc_free_sysfs_attr(vport);
8981 
8982 	/* Release all the vports against this physical port */
8983 	vports = lpfc_create_vport_work_array(phba);
8984 	if (vports != NULL)
8985 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
8986 			if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
8987 				continue;
8988 			fc_vport_terminate(vports[i]->fc_vport);
8989 		}
8990 	lpfc_destroy_vport_work_array(phba, vports);
8991 
8992 	/* Remove FC host and then SCSI host with the physical port */
8993 	fc_remove_host(shost);
8994 	scsi_remove_host(shost);
8995 	lpfc_cleanup(vport);
8996 
8997 	/*
8998 	 * Bring down the SLI Layer. This step disable all interrupts,
8999 	 * clears the rings, discards all mailbox commands, and resets
9000 	 * the HBA.
9001 	 */
9002 
9003 	/* HBA interrupt will be disabled after this call */
9004 	lpfc_sli_hba_down(phba);
9005 	/* Stop kthread signal shall trigger work_done one more time */
9006 	kthread_stop(phba->worker_thread);
9007 	/* Final cleanup of txcmplq and reset the HBA */
9008 	lpfc_sli_brdrestart(phba);
9009 
9010 	kfree(phba->vpi_bmask);
9011 	kfree(phba->vpi_ids);
9012 
9013 	lpfc_stop_hba_timers(phba);
9014 	spin_lock_irq(&phba->hbalock);
9015 	list_del_init(&vport->listentry);
9016 	spin_unlock_irq(&phba->hbalock);
9017 
9018 	lpfc_debugfs_terminate(vport);
9019 
9020 	/* Disable SR-IOV if enabled */
9021 	if (phba->cfg_sriov_nr_virtfn)
9022 		pci_disable_sriov(pdev);
9023 
9024 	/* Disable interrupt */
9025 	lpfc_sli_disable_intr(phba);
9026 
9027 	pci_set_drvdata(pdev, NULL);
9028 	scsi_host_put(shost);
9029 
9030 	/*
9031 	 * Call scsi_free before mem_free since scsi bufs are released to their
9032 	 * corresponding pools here.
9033 	 */
9034 	lpfc_scsi_free(phba);
9035 	lpfc_mem_free_all(phba);
9036 
9037 	dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
9038 			  phba->hbqslimp.virt, phba->hbqslimp.phys);
9039 
9040 	/* Free resources associated with SLI2 interface */
9041 	dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
9042 			  phba->slim2p.virt, phba->slim2p.phys);
9043 
9044 	/* unmap adapter SLIM and Control Registers */
9045 	iounmap(phba->ctrl_regs_memmap_p);
9046 	iounmap(phba->slim_memmap_p);
9047 
9048 	lpfc_hba_free(phba);
9049 
9050 	pci_release_selected_regions(pdev, bars);
9051 	pci_disable_device(pdev);
9052 }
9053 
9054 /**
9055  * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
9056  * @pdev: pointer to PCI device
9057  * @msg: power management message
9058  *
9059  * This routine is to be called from the kernel's PCI subsystem to support
9060  * system Power Management (PM) to device with SLI-3 interface spec. When
9061  * PM invokes this method, it quiesces the device by stopping the driver's
9062  * worker thread for the device, turning off device's interrupt and DMA,
9063  * and bring the device offline. Note that as the driver implements the
9064  * minimum PM requirements to a power-aware driver's PM support for the
9065  * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
9066  * to the suspend() method call will be treated as SUSPEND and the driver will
9067  * fully reinitialize its device during resume() method call, the driver will
9068  * set device to PCI_D3hot state in PCI config space instead of setting it
9069  * according to the @msg provided by the PM.
9070  *
9071  * Return code
9072  * 	0 - driver suspended the device
9073  * 	Error otherwise
9074  **/
9075 static int
9076 lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
9077 {
9078 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
9079 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9080 
9081 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9082 			"0473 PCI device Power Management suspend.\n");
9083 
9084 	/* Bring down the device */
9085 	lpfc_offline_prep(phba, LPFC_MBX_WAIT);
9086 	lpfc_offline(phba);
9087 	kthread_stop(phba->worker_thread);
9088 
9089 	/* Disable interrupt from device */
9090 	lpfc_sli_disable_intr(phba);
9091 
9092 	/* Save device state to PCI config space */
9093 	pci_save_state(pdev);
9094 	pci_set_power_state(pdev, PCI_D3hot);
9095 
9096 	return 0;
9097 }
9098 
9099 /**
9100  * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
9101  * @pdev: pointer to PCI device
9102  *
9103  * This routine is to be called from the kernel's PCI subsystem to support
9104  * system Power Management (PM) to device with SLI-3 interface spec. When PM
9105  * invokes this method, it restores the device's PCI config space state and
9106  * fully reinitializes the device and brings it online. Note that as the
9107  * driver implements the minimum PM requirements to a power-aware driver's
9108  * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
9109  * FREEZE) to the suspend() method call will be treated as SUSPEND and the
9110  * driver will fully reinitialize its device during resume() method call,
9111  * the device will be set to PCI_D0 directly in PCI config space before
9112  * restoring the state.
9113  *
9114  * Return code
9115  * 	0 - driver suspended the device
9116  * 	Error otherwise
9117  **/
9118 static int
9119 lpfc_pci_resume_one_s3(struct pci_dev *pdev)
9120 {
9121 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
9122 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9123 	uint32_t intr_mode;
9124 	int error;
9125 
9126 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9127 			"0452 PCI device Power Management resume.\n");
9128 
9129 	/* Restore device state from PCI config space */
9130 	pci_set_power_state(pdev, PCI_D0);
9131 	pci_restore_state(pdev);
9132 
9133 	/*
9134 	 * As the new kernel behavior of pci_restore_state() API call clears
9135 	 * device saved_state flag, need to save the restored state again.
9136 	 */
9137 	pci_save_state(pdev);
9138 
9139 	if (pdev->is_busmaster)
9140 		pci_set_master(pdev);
9141 
9142 	/* Startup the kernel thread for this host adapter. */
9143 	phba->worker_thread = kthread_run(lpfc_do_work, phba,
9144 					"lpfc_worker_%d", phba->brd_no);
9145 	if (IS_ERR(phba->worker_thread)) {
9146 		error = PTR_ERR(phba->worker_thread);
9147 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9148 				"0434 PM resume failed to start worker "
9149 				"thread: error=x%x.\n", error);
9150 		return error;
9151 	}
9152 
9153 	/* Configure and enable interrupt */
9154 	intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
9155 	if (intr_mode == LPFC_INTR_ERROR) {
9156 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9157 				"0430 PM resume Failed to enable interrupt\n");
9158 		return -EIO;
9159 	} else
9160 		phba->intr_mode = intr_mode;
9161 
9162 	/* Restart HBA and bring it online */
9163 	lpfc_sli_brdrestart(phba);
9164 	lpfc_online(phba);
9165 
9166 	/* Log the current active interrupt mode */
9167 	lpfc_log_intr_mode(phba, phba->intr_mode);
9168 
9169 	return 0;
9170 }
9171 
9172 /**
9173  * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
9174  * @phba: pointer to lpfc hba data structure.
9175  *
9176  * This routine is called to prepare the SLI3 device for PCI slot recover. It
9177  * aborts all the outstanding SCSI I/Os to the pci device.
9178  **/
9179 static void
9180 lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
9181 {
9182 	struct lpfc_sli *psli = &phba->sli;
9183 	struct lpfc_sli_ring  *pring;
9184 
9185 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9186 			"2723 PCI channel I/O abort preparing for recovery\n");
9187 
9188 	/*
9189 	 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
9190 	 * and let the SCSI mid-layer to retry them to recover.
9191 	 */
9192 	pring = &psli->ring[psli->fcp_ring];
9193 	lpfc_sli_abort_iocb_ring(phba, pring);
9194 }
9195 
9196 /**
9197  * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
9198  * @phba: pointer to lpfc hba data structure.
9199  *
9200  * This routine is called to prepare the SLI3 device for PCI slot reset. It
9201  * disables the device interrupt and pci device, and aborts the internal FCP
9202  * pending I/Os.
9203  **/
9204 static void
9205 lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
9206 {
9207 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9208 			"2710 PCI channel disable preparing for reset\n");
9209 
9210 	/* Block any management I/Os to the device */
9211 	lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
9212 
9213 	/* Block all SCSI devices' I/Os on the host */
9214 	lpfc_scsi_dev_block(phba);
9215 
9216 	/* stop all timers */
9217 	lpfc_stop_hba_timers(phba);
9218 
9219 	/* Disable interrupt and pci device */
9220 	lpfc_sli_disable_intr(phba);
9221 	pci_disable_device(phba->pcidev);
9222 
9223 	/* Flush all driver's outstanding SCSI I/Os as we are to reset */
9224 	lpfc_sli_flush_fcp_rings(phba);
9225 }
9226 
9227 /**
9228  * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
9229  * @phba: pointer to lpfc hba data structure.
9230  *
9231  * This routine is called to prepare the SLI3 device for PCI slot permanently
9232  * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
9233  * pending I/Os.
9234  **/
9235 static void
9236 lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
9237 {
9238 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9239 			"2711 PCI channel permanent disable for failure\n");
9240 	/* Block all SCSI devices' I/Os on the host */
9241 	lpfc_scsi_dev_block(phba);
9242 
9243 	/* stop all timers */
9244 	lpfc_stop_hba_timers(phba);
9245 
9246 	/* Clean up all driver's outstanding SCSI I/Os */
9247 	lpfc_sli_flush_fcp_rings(phba);
9248 }
9249 
9250 /**
9251  * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
9252  * @pdev: pointer to PCI device.
9253  * @state: the current PCI connection state.
9254  *
9255  * This routine is called from the PCI subsystem for I/O error handling to
9256  * device with SLI-3 interface spec. This function is called by the PCI
9257  * subsystem after a PCI bus error affecting this device has been detected.
9258  * When this function is invoked, it will need to stop all the I/Os and
9259  * interrupt(s) to the device. Once that is done, it will return
9260  * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
9261  * as desired.
9262  *
9263  * Return codes
9264  * 	PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
9265  * 	PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
9266  * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
9267  **/
9268 static pci_ers_result_t
9269 lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
9270 {
9271 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
9272 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9273 
9274 	switch (state) {
9275 	case pci_channel_io_normal:
9276 		/* Non-fatal error, prepare for recovery */
9277 		lpfc_sli_prep_dev_for_recover(phba);
9278 		return PCI_ERS_RESULT_CAN_RECOVER;
9279 	case pci_channel_io_frozen:
9280 		/* Fatal error, prepare for slot reset */
9281 		lpfc_sli_prep_dev_for_reset(phba);
9282 		return PCI_ERS_RESULT_NEED_RESET;
9283 	case pci_channel_io_perm_failure:
9284 		/* Permanent failure, prepare for device down */
9285 		lpfc_sli_prep_dev_for_perm_failure(phba);
9286 		return PCI_ERS_RESULT_DISCONNECT;
9287 	default:
9288 		/* Unknown state, prepare and request slot reset */
9289 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9290 				"0472 Unknown PCI error state: x%x\n", state);
9291 		lpfc_sli_prep_dev_for_reset(phba);
9292 		return PCI_ERS_RESULT_NEED_RESET;
9293 	}
9294 }
9295 
9296 /**
9297  * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
9298  * @pdev: pointer to PCI device.
9299  *
9300  * This routine is called from the PCI subsystem for error handling to
9301  * device with SLI-3 interface spec. This is called after PCI bus has been
9302  * reset to restart the PCI card from scratch, as if from a cold-boot.
9303  * During the PCI subsystem error recovery, after driver returns
9304  * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
9305  * recovery and then call this routine before calling the .resume method
9306  * to recover the device. This function will initialize the HBA device,
9307  * enable the interrupt, but it will just put the HBA to offline state
9308  * without passing any I/O traffic.
9309  *
9310  * Return codes
9311  * 	PCI_ERS_RESULT_RECOVERED - the device has been recovered
9312  * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
9313  */
9314 static pci_ers_result_t
9315 lpfc_io_slot_reset_s3(struct pci_dev *pdev)
9316 {
9317 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
9318 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9319 	struct lpfc_sli *psli = &phba->sli;
9320 	uint32_t intr_mode;
9321 
9322 	dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
9323 	if (pci_enable_device_mem(pdev)) {
9324 		printk(KERN_ERR "lpfc: Cannot re-enable "
9325 			"PCI device after reset.\n");
9326 		return PCI_ERS_RESULT_DISCONNECT;
9327 	}
9328 
9329 	pci_restore_state(pdev);
9330 
9331 	/*
9332 	 * As the new kernel behavior of pci_restore_state() API call clears
9333 	 * device saved_state flag, need to save the restored state again.
9334 	 */
9335 	pci_save_state(pdev);
9336 
9337 	if (pdev->is_busmaster)
9338 		pci_set_master(pdev);
9339 
9340 	spin_lock_irq(&phba->hbalock);
9341 	psli->sli_flag &= ~LPFC_SLI_ACTIVE;
9342 	spin_unlock_irq(&phba->hbalock);
9343 
9344 	/* Configure and enable interrupt */
9345 	intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
9346 	if (intr_mode == LPFC_INTR_ERROR) {
9347 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9348 				"0427 Cannot re-enable interrupt after "
9349 				"slot reset.\n");
9350 		return PCI_ERS_RESULT_DISCONNECT;
9351 	} else
9352 		phba->intr_mode = intr_mode;
9353 
9354 	/* Take device offline, it will perform cleanup */
9355 	lpfc_offline_prep(phba, LPFC_MBX_WAIT);
9356 	lpfc_offline(phba);
9357 	lpfc_sli_brdrestart(phba);
9358 
9359 	/* Log the current active interrupt mode */
9360 	lpfc_log_intr_mode(phba, phba->intr_mode);
9361 
9362 	return PCI_ERS_RESULT_RECOVERED;
9363 }
9364 
9365 /**
9366  * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
9367  * @pdev: pointer to PCI device
9368  *
9369  * This routine is called from the PCI subsystem for error handling to device
9370  * with SLI-3 interface spec. It is called when kernel error recovery tells
9371  * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
9372  * error recovery. After this call, traffic can start to flow from this device
9373  * again.
9374  */
9375 static void
9376 lpfc_io_resume_s3(struct pci_dev *pdev)
9377 {
9378 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
9379 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9380 
9381 	/* Bring device online, it will be no-op for non-fatal error resume */
9382 	lpfc_online(phba);
9383 
9384 	/* Clean up Advanced Error Reporting (AER) if needed */
9385 	if (phba->hba_flag & HBA_AER_ENABLED)
9386 		pci_cleanup_aer_uncorrect_error_status(pdev);
9387 }
9388 
9389 /**
9390  * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
9391  * @phba: pointer to lpfc hba data structure.
9392  *
9393  * returns the number of ELS/CT IOCBs to reserve
9394  **/
9395 int
9396 lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
9397 {
9398 	int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
9399 
9400 	if (phba->sli_rev == LPFC_SLI_REV4) {
9401 		if (max_xri <= 100)
9402 			return 10;
9403 		else if (max_xri <= 256)
9404 			return 25;
9405 		else if (max_xri <= 512)
9406 			return 50;
9407 		else if (max_xri <= 1024)
9408 			return 100;
9409 		else if (max_xri <= 1536)
9410 			return 150;
9411 		else if (max_xri <= 2048)
9412 			return 200;
9413 		else
9414 			return 250;
9415 	} else
9416 		return 0;
9417 }
9418 
9419 /**
9420  * lpfc_write_firmware - attempt to write a firmware image to the port
9421  * @fw: pointer to firmware image returned from request_firmware.
9422  * @phba: pointer to lpfc hba data structure.
9423  *
9424  **/
9425 static void
9426 lpfc_write_firmware(const struct firmware *fw, void *context)
9427 {
9428 	struct lpfc_hba *phba = (struct lpfc_hba *)context;
9429 	char fwrev[FW_REV_STR_SIZE];
9430 	struct lpfc_grp_hdr *image;
9431 	struct list_head dma_buffer_list;
9432 	int i, rc = 0;
9433 	struct lpfc_dmabuf *dmabuf, *next;
9434 	uint32_t offset = 0, temp_offset = 0;
9435 
9436 	/* It can be null in no-wait mode, sanity check */
9437 	if (!fw) {
9438 		rc = -ENXIO;
9439 		goto out;
9440 	}
9441 	image = (struct lpfc_grp_hdr *)fw->data;
9442 
9443 	INIT_LIST_HEAD(&dma_buffer_list);
9444 	if ((be32_to_cpu(image->magic_number) != LPFC_GROUP_OJECT_MAGIC_NUM) ||
9445 	    (bf_get_be32(lpfc_grp_hdr_file_type, image) !=
9446 	     LPFC_FILE_TYPE_GROUP) ||
9447 	    (bf_get_be32(lpfc_grp_hdr_id, image) != LPFC_FILE_ID_GROUP) ||
9448 	    (be32_to_cpu(image->size) != fw->size)) {
9449 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9450 				"3022 Invalid FW image found. "
9451 				"Magic:%x Type:%x ID:%x\n",
9452 				be32_to_cpu(image->magic_number),
9453 				bf_get_be32(lpfc_grp_hdr_file_type, image),
9454 				bf_get_be32(lpfc_grp_hdr_id, image));
9455 		rc = -EINVAL;
9456 		goto release_out;
9457 	}
9458 	lpfc_decode_firmware_rev(phba, fwrev, 1);
9459 	if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
9460 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9461 				"3023 Updating Firmware, Current Version:%s "
9462 				"New Version:%s\n",
9463 				fwrev, image->revision);
9464 		for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
9465 			dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
9466 					 GFP_KERNEL);
9467 			if (!dmabuf) {
9468 				rc = -ENOMEM;
9469 				goto release_out;
9470 			}
9471 			dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
9472 							  SLI4_PAGE_SIZE,
9473 							  &dmabuf->phys,
9474 							  GFP_KERNEL);
9475 			if (!dmabuf->virt) {
9476 				kfree(dmabuf);
9477 				rc = -ENOMEM;
9478 				goto release_out;
9479 			}
9480 			list_add_tail(&dmabuf->list, &dma_buffer_list);
9481 		}
9482 		while (offset < fw->size) {
9483 			temp_offset = offset;
9484 			list_for_each_entry(dmabuf, &dma_buffer_list, list) {
9485 				if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
9486 					memcpy(dmabuf->virt,
9487 					       fw->data + temp_offset,
9488 					       fw->size - temp_offset);
9489 					temp_offset = fw->size;
9490 					break;
9491 				}
9492 				memcpy(dmabuf->virt, fw->data + temp_offset,
9493 				       SLI4_PAGE_SIZE);
9494 				temp_offset += SLI4_PAGE_SIZE;
9495 			}
9496 			rc = lpfc_wr_object(phba, &dma_buffer_list,
9497 				    (fw->size - offset), &offset);
9498 			if (rc)
9499 				goto release_out;
9500 		}
9501 		rc = offset;
9502 	}
9503 
9504 release_out:
9505 	list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
9506 		list_del(&dmabuf->list);
9507 		dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
9508 				  dmabuf->virt, dmabuf->phys);
9509 		kfree(dmabuf);
9510 	}
9511 	release_firmware(fw);
9512 out:
9513 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9514 			"3024 Firmware update done: %d.\n", rc);
9515 	return;
9516 }
9517 
9518 /**
9519  * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
9520  * @phba: pointer to lpfc hba data structure.
9521  *
9522  * This routine is called to perform Linux generic firmware upgrade on device
9523  * that supports such feature.
9524  **/
9525 int
9526 lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
9527 {
9528 	uint8_t file_name[ELX_MODEL_NAME_SIZE];
9529 	int ret;
9530 	const struct firmware *fw;
9531 
9532 	/* Only supported on SLI4 interface type 2 for now */
9533 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
9534 	    LPFC_SLI_INTF_IF_TYPE_2)
9535 		return -EPERM;
9536 
9537 	snprintf(file_name, ELX_MODEL_NAME_SIZE, "%s.grp", phba->ModelName);
9538 
9539 	if (fw_upgrade == INT_FW_UPGRADE) {
9540 		ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
9541 					file_name, &phba->pcidev->dev,
9542 					GFP_KERNEL, (void *)phba,
9543 					lpfc_write_firmware);
9544 	} else if (fw_upgrade == RUN_FW_UPGRADE) {
9545 		ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
9546 		if (!ret)
9547 			lpfc_write_firmware(fw, (void *)phba);
9548 	} else {
9549 		ret = -EINVAL;
9550 	}
9551 
9552 	return ret;
9553 }
9554 
9555 /**
9556  * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
9557  * @pdev: pointer to PCI device
9558  * @pid: pointer to PCI device identifier
9559  *
9560  * This routine is called from the kernel's PCI subsystem to device with
9561  * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
9562  * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
9563  * information of the device and driver to see if the driver state that it
9564  * can support this kind of device. If the match is successful, the driver
9565  * core invokes this routine. If this routine determines it can claim the HBA,
9566  * it does all the initialization that it needs to do to handle the HBA
9567  * properly.
9568  *
9569  * Return code
9570  * 	0 - driver can claim the device
9571  * 	negative value - driver can not claim the device
9572  **/
9573 static int
9574 lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
9575 {
9576 	struct lpfc_hba   *phba;
9577 	struct lpfc_vport *vport = NULL;
9578 	struct Scsi_Host  *shost = NULL;
9579 	int error, ret;
9580 	uint32_t cfg_mode, intr_mode;
9581 	int adjusted_fcp_io_channel;
9582 
9583 	/* Allocate memory for HBA structure */
9584 	phba = lpfc_hba_alloc(pdev);
9585 	if (!phba)
9586 		return -ENOMEM;
9587 
9588 	/* Perform generic PCI device enabling operation */
9589 	error = lpfc_enable_pci_dev(phba);
9590 	if (error)
9591 		goto out_free_phba;
9592 
9593 	/* Set up SLI API function jump table for PCI-device group-1 HBAs */
9594 	error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
9595 	if (error)
9596 		goto out_disable_pci_dev;
9597 
9598 	/* Set up SLI-4 specific device PCI memory space */
9599 	error = lpfc_sli4_pci_mem_setup(phba);
9600 	if (error) {
9601 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9602 				"1410 Failed to set up pci memory space.\n");
9603 		goto out_disable_pci_dev;
9604 	}
9605 
9606 	/* Set up phase-1 common device driver resources */
9607 	error = lpfc_setup_driver_resource_phase1(phba);
9608 	if (error) {
9609 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9610 				"1411 Failed to set up driver resource.\n");
9611 		goto out_unset_pci_mem_s4;
9612 	}
9613 
9614 	/* Set up SLI-4 Specific device driver resources */
9615 	error = lpfc_sli4_driver_resource_setup(phba);
9616 	if (error) {
9617 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9618 				"1412 Failed to set up driver resource.\n");
9619 		goto out_unset_pci_mem_s4;
9620 	}
9621 
9622 	/* Initialize and populate the iocb list per host */
9623 
9624 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9625 			"2821 initialize iocb list %d.\n",
9626 			phba->cfg_iocb_cnt*1024);
9627 	error = lpfc_init_iocb_list(phba, phba->cfg_iocb_cnt*1024);
9628 
9629 	if (error) {
9630 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9631 				"1413 Failed to initialize iocb list.\n");
9632 		goto out_unset_driver_resource_s4;
9633 	}
9634 
9635 	INIT_LIST_HEAD(&phba->active_rrq_list);
9636 	INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
9637 
9638 	/* Set up common device driver resources */
9639 	error = lpfc_setup_driver_resource_phase2(phba);
9640 	if (error) {
9641 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9642 				"1414 Failed to set up driver resource.\n");
9643 		goto out_free_iocb_list;
9644 	}
9645 
9646 	/* Get the default values for Model Name and Description */
9647 	lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
9648 
9649 	/* Create SCSI host to the physical port */
9650 	error = lpfc_create_shost(phba);
9651 	if (error) {
9652 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9653 				"1415 Failed to create scsi host.\n");
9654 		goto out_unset_driver_resource;
9655 	}
9656 
9657 	/* Configure sysfs attributes */
9658 	vport = phba->pport;
9659 	error = lpfc_alloc_sysfs_attr(vport);
9660 	if (error) {
9661 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9662 				"1416 Failed to allocate sysfs attr\n");
9663 		goto out_destroy_shost;
9664 	}
9665 
9666 	shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
9667 	/* Now, trying to enable interrupt and bring up the device */
9668 	cfg_mode = phba->cfg_use_msi;
9669 
9670 	/* Put device to a known state before enabling interrupt */
9671 	lpfc_stop_port(phba);
9672 	/* Configure and enable interrupt */
9673 	intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
9674 	if (intr_mode == LPFC_INTR_ERROR) {
9675 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9676 				"0426 Failed to enable interrupt.\n");
9677 		error = -ENODEV;
9678 		goto out_free_sysfs_attr;
9679 	}
9680 	/* Default to single EQ for non-MSI-X */
9681 	if (phba->intr_type != MSIX)
9682 		adjusted_fcp_io_channel = 1;
9683 	else
9684 		adjusted_fcp_io_channel = phba->cfg_fcp_io_channel;
9685 	phba->cfg_fcp_io_channel = adjusted_fcp_io_channel;
9686 	/* Set up SLI-4 HBA */
9687 	if (lpfc_sli4_hba_setup(phba)) {
9688 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9689 				"1421 Failed to set up hba\n");
9690 		error = -ENODEV;
9691 		goto out_disable_intr;
9692 	}
9693 
9694 	/* Log the current active interrupt mode */
9695 	phba->intr_mode = intr_mode;
9696 	lpfc_log_intr_mode(phba, intr_mode);
9697 
9698 	/* Perform post initialization setup */
9699 	lpfc_post_init_setup(phba);
9700 
9701 	/* check for firmware upgrade or downgrade */
9702 	if (phba->cfg_request_firmware_upgrade)
9703 		ret = lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
9704 
9705 	/* Check if there are static vports to be created. */
9706 	lpfc_create_static_vport(phba);
9707 	return 0;
9708 
9709 out_disable_intr:
9710 	lpfc_sli4_disable_intr(phba);
9711 out_free_sysfs_attr:
9712 	lpfc_free_sysfs_attr(vport);
9713 out_destroy_shost:
9714 	lpfc_destroy_shost(phba);
9715 out_unset_driver_resource:
9716 	lpfc_unset_driver_resource_phase2(phba);
9717 out_free_iocb_list:
9718 	lpfc_free_iocb_list(phba);
9719 out_unset_driver_resource_s4:
9720 	lpfc_sli4_driver_resource_unset(phba);
9721 out_unset_pci_mem_s4:
9722 	lpfc_sli4_pci_mem_unset(phba);
9723 out_disable_pci_dev:
9724 	lpfc_disable_pci_dev(phba);
9725 	if (shost)
9726 		scsi_host_put(shost);
9727 out_free_phba:
9728 	lpfc_hba_free(phba);
9729 	return error;
9730 }
9731 
9732 /**
9733  * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
9734  * @pdev: pointer to PCI device
9735  *
9736  * This routine is called from the kernel's PCI subsystem to device with
9737  * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
9738  * removed from PCI bus, it performs all the necessary cleanup for the HBA
9739  * device to be removed from the PCI subsystem properly.
9740  **/
9741 static void
9742 lpfc_pci_remove_one_s4(struct pci_dev *pdev)
9743 {
9744 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
9745 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
9746 	struct lpfc_vport **vports;
9747 	struct lpfc_hba *phba = vport->phba;
9748 	int i;
9749 
9750 	/* Mark the device unloading flag */
9751 	spin_lock_irq(&phba->hbalock);
9752 	vport->load_flag |= FC_UNLOADING;
9753 	spin_unlock_irq(&phba->hbalock);
9754 
9755 	/* Free the HBA sysfs attributes */
9756 	lpfc_free_sysfs_attr(vport);
9757 
9758 	/* Release all the vports against this physical port */
9759 	vports = lpfc_create_vport_work_array(phba);
9760 	if (vports != NULL)
9761 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
9762 			if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
9763 				continue;
9764 			fc_vport_terminate(vports[i]->fc_vport);
9765 		}
9766 	lpfc_destroy_vport_work_array(phba, vports);
9767 
9768 	/* Remove FC host and then SCSI host with the physical port */
9769 	fc_remove_host(shost);
9770 	scsi_remove_host(shost);
9771 
9772 	/* Perform cleanup on the physical port */
9773 	lpfc_cleanup(vport);
9774 
9775 	/*
9776 	 * Bring down the SLI Layer. This step disables all interrupts,
9777 	 * clears the rings, discards all mailbox commands, and resets
9778 	 * the HBA FCoE function.
9779 	 */
9780 	lpfc_debugfs_terminate(vport);
9781 	lpfc_sli4_hba_unset(phba);
9782 
9783 	spin_lock_irq(&phba->hbalock);
9784 	list_del_init(&vport->listentry);
9785 	spin_unlock_irq(&phba->hbalock);
9786 
9787 	/* Perform scsi free before driver resource_unset since scsi
9788 	 * buffers are released to their corresponding pools here.
9789 	 */
9790 	lpfc_scsi_free(phba);
9791 
9792 	lpfc_sli4_driver_resource_unset(phba);
9793 
9794 	/* Unmap adapter Control and Doorbell registers */
9795 	lpfc_sli4_pci_mem_unset(phba);
9796 
9797 	/* Release PCI resources and disable device's PCI function */
9798 	scsi_host_put(shost);
9799 	lpfc_disable_pci_dev(phba);
9800 
9801 	/* Finally, free the driver's device data structure */
9802 	lpfc_hba_free(phba);
9803 
9804 	return;
9805 }
9806 
9807 /**
9808  * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
9809  * @pdev: pointer to PCI device
9810  * @msg: power management message
9811  *
9812  * This routine is called from the kernel's PCI subsystem to support system
9813  * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
9814  * this method, it quiesces the device by stopping the driver's worker
9815  * thread for the device, turning off device's interrupt and DMA, and bring
9816  * the device offline. Note that as the driver implements the minimum PM
9817  * requirements to a power-aware driver's PM support for suspend/resume -- all
9818  * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
9819  * method call will be treated as SUSPEND and the driver will fully
9820  * reinitialize its device during resume() method call, the driver will set
9821  * device to PCI_D3hot state in PCI config space instead of setting it
9822  * according to the @msg provided by the PM.
9823  *
9824  * Return code
9825  * 	0 - driver suspended the device
9826  * 	Error otherwise
9827  **/
9828 static int
9829 lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
9830 {
9831 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
9832 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9833 
9834 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9835 			"2843 PCI device Power Management suspend.\n");
9836 
9837 	/* Bring down the device */
9838 	lpfc_offline_prep(phba, LPFC_MBX_WAIT);
9839 	lpfc_offline(phba);
9840 	kthread_stop(phba->worker_thread);
9841 
9842 	/* Disable interrupt from device */
9843 	lpfc_sli4_disable_intr(phba);
9844 	lpfc_sli4_queue_destroy(phba);
9845 
9846 	/* Save device state to PCI config space */
9847 	pci_save_state(pdev);
9848 	pci_set_power_state(pdev, PCI_D3hot);
9849 
9850 	return 0;
9851 }
9852 
9853 /**
9854  * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
9855  * @pdev: pointer to PCI device
9856  *
9857  * This routine is called from the kernel's PCI subsystem to support system
9858  * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
9859  * this method, it restores the device's PCI config space state and fully
9860  * reinitializes the device and brings it online. Note that as the driver
9861  * implements the minimum PM requirements to a power-aware driver's PM for
9862  * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
9863  * to the suspend() method call will be treated as SUSPEND and the driver
9864  * will fully reinitialize its device during resume() method call, the device
9865  * will be set to PCI_D0 directly in PCI config space before restoring the
9866  * state.
9867  *
9868  * Return code
9869  * 	0 - driver suspended the device
9870  * 	Error otherwise
9871  **/
9872 static int
9873 lpfc_pci_resume_one_s4(struct pci_dev *pdev)
9874 {
9875 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
9876 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9877 	uint32_t intr_mode;
9878 	int error;
9879 
9880 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9881 			"0292 PCI device Power Management resume.\n");
9882 
9883 	/* Restore device state from PCI config space */
9884 	pci_set_power_state(pdev, PCI_D0);
9885 	pci_restore_state(pdev);
9886 
9887 	/*
9888 	 * As the new kernel behavior of pci_restore_state() API call clears
9889 	 * device saved_state flag, need to save the restored state again.
9890 	 */
9891 	pci_save_state(pdev);
9892 
9893 	if (pdev->is_busmaster)
9894 		pci_set_master(pdev);
9895 
9896 	 /* Startup the kernel thread for this host adapter. */
9897 	phba->worker_thread = kthread_run(lpfc_do_work, phba,
9898 					"lpfc_worker_%d", phba->brd_no);
9899 	if (IS_ERR(phba->worker_thread)) {
9900 		error = PTR_ERR(phba->worker_thread);
9901 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9902 				"0293 PM resume failed to start worker "
9903 				"thread: error=x%x.\n", error);
9904 		return error;
9905 	}
9906 
9907 	/* Configure and enable interrupt */
9908 	intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
9909 	if (intr_mode == LPFC_INTR_ERROR) {
9910 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9911 				"0294 PM resume Failed to enable interrupt\n");
9912 		return -EIO;
9913 	} else
9914 		phba->intr_mode = intr_mode;
9915 
9916 	/* Restart HBA and bring it online */
9917 	lpfc_sli_brdrestart(phba);
9918 	lpfc_online(phba);
9919 
9920 	/* Log the current active interrupt mode */
9921 	lpfc_log_intr_mode(phba, phba->intr_mode);
9922 
9923 	return 0;
9924 }
9925 
9926 /**
9927  * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
9928  * @phba: pointer to lpfc hba data structure.
9929  *
9930  * This routine is called to prepare the SLI4 device for PCI slot recover. It
9931  * aborts all the outstanding SCSI I/Os to the pci device.
9932  **/
9933 static void
9934 lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
9935 {
9936 	struct lpfc_sli *psli = &phba->sli;
9937 	struct lpfc_sli_ring  *pring;
9938 
9939 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9940 			"2828 PCI channel I/O abort preparing for recovery\n");
9941 	/*
9942 	 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
9943 	 * and let the SCSI mid-layer to retry them to recover.
9944 	 */
9945 	pring = &psli->ring[psli->fcp_ring];
9946 	lpfc_sli_abort_iocb_ring(phba, pring);
9947 }
9948 
9949 /**
9950  * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
9951  * @phba: pointer to lpfc hba data structure.
9952  *
9953  * This routine is called to prepare the SLI4 device for PCI slot reset. It
9954  * disables the device interrupt and pci device, and aborts the internal FCP
9955  * pending I/Os.
9956  **/
9957 static void
9958 lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
9959 {
9960 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9961 			"2826 PCI channel disable preparing for reset\n");
9962 
9963 	/* Block any management I/Os to the device */
9964 	lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
9965 
9966 	/* Block all SCSI devices' I/Os on the host */
9967 	lpfc_scsi_dev_block(phba);
9968 
9969 	/* stop all timers */
9970 	lpfc_stop_hba_timers(phba);
9971 
9972 	/* Disable interrupt and pci device */
9973 	lpfc_sli4_disable_intr(phba);
9974 	lpfc_sli4_queue_destroy(phba);
9975 	pci_disable_device(phba->pcidev);
9976 
9977 	/* Flush all driver's outstanding SCSI I/Os as we are to reset */
9978 	lpfc_sli_flush_fcp_rings(phba);
9979 }
9980 
9981 /**
9982  * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
9983  * @phba: pointer to lpfc hba data structure.
9984  *
9985  * This routine is called to prepare the SLI4 device for PCI slot permanently
9986  * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
9987  * pending I/Os.
9988  **/
9989 static void
9990 lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
9991 {
9992 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9993 			"2827 PCI channel permanent disable for failure\n");
9994 
9995 	/* Block all SCSI devices' I/Os on the host */
9996 	lpfc_scsi_dev_block(phba);
9997 
9998 	/* stop all timers */
9999 	lpfc_stop_hba_timers(phba);
10000 
10001 	/* Clean up all driver's outstanding SCSI I/Os */
10002 	lpfc_sli_flush_fcp_rings(phba);
10003 }
10004 
10005 /**
10006  * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
10007  * @pdev: pointer to PCI device.
10008  * @state: the current PCI connection state.
10009  *
10010  * This routine is called from the PCI subsystem for error handling to device
10011  * with SLI-4 interface spec. This function is called by the PCI subsystem
10012  * after a PCI bus error affecting this device has been detected. When this
10013  * function is invoked, it will need to stop all the I/Os and interrupt(s)
10014  * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
10015  * for the PCI subsystem to perform proper recovery as desired.
10016  *
10017  * Return codes
10018  * 	PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10019  * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10020  **/
10021 static pci_ers_result_t
10022 lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
10023 {
10024 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
10025 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10026 
10027 	switch (state) {
10028 	case pci_channel_io_normal:
10029 		/* Non-fatal error, prepare for recovery */
10030 		lpfc_sli4_prep_dev_for_recover(phba);
10031 		return PCI_ERS_RESULT_CAN_RECOVER;
10032 	case pci_channel_io_frozen:
10033 		/* Fatal error, prepare for slot reset */
10034 		lpfc_sli4_prep_dev_for_reset(phba);
10035 		return PCI_ERS_RESULT_NEED_RESET;
10036 	case pci_channel_io_perm_failure:
10037 		/* Permanent failure, prepare for device down */
10038 		lpfc_sli4_prep_dev_for_perm_failure(phba);
10039 		return PCI_ERS_RESULT_DISCONNECT;
10040 	default:
10041 		/* Unknown state, prepare and request slot reset */
10042 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10043 				"2825 Unknown PCI error state: x%x\n", state);
10044 		lpfc_sli4_prep_dev_for_reset(phba);
10045 		return PCI_ERS_RESULT_NEED_RESET;
10046 	}
10047 }
10048 
10049 /**
10050  * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
10051  * @pdev: pointer to PCI device.
10052  *
10053  * This routine is called from the PCI subsystem for error handling to device
10054  * with SLI-4 interface spec. It is called after PCI bus has been reset to
10055  * restart the PCI card from scratch, as if from a cold-boot. During the
10056  * PCI subsystem error recovery, after the driver returns
10057  * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
10058  * recovery and then call this routine before calling the .resume method to
10059  * recover the device. This function will initialize the HBA device, enable
10060  * the interrupt, but it will just put the HBA to offline state without
10061  * passing any I/O traffic.
10062  *
10063  * Return codes
10064  * 	PCI_ERS_RESULT_RECOVERED - the device has been recovered
10065  * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10066  */
10067 static pci_ers_result_t
10068 lpfc_io_slot_reset_s4(struct pci_dev *pdev)
10069 {
10070 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
10071 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10072 	struct lpfc_sli *psli = &phba->sli;
10073 	uint32_t intr_mode;
10074 
10075 	dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
10076 	if (pci_enable_device_mem(pdev)) {
10077 		printk(KERN_ERR "lpfc: Cannot re-enable "
10078 			"PCI device after reset.\n");
10079 		return PCI_ERS_RESULT_DISCONNECT;
10080 	}
10081 
10082 	pci_restore_state(pdev);
10083 
10084 	/*
10085 	 * As the new kernel behavior of pci_restore_state() API call clears
10086 	 * device saved_state flag, need to save the restored state again.
10087 	 */
10088 	pci_save_state(pdev);
10089 
10090 	if (pdev->is_busmaster)
10091 		pci_set_master(pdev);
10092 
10093 	spin_lock_irq(&phba->hbalock);
10094 	psli->sli_flag &= ~LPFC_SLI_ACTIVE;
10095 	spin_unlock_irq(&phba->hbalock);
10096 
10097 	/* Configure and enable interrupt */
10098 	intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
10099 	if (intr_mode == LPFC_INTR_ERROR) {
10100 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10101 				"2824 Cannot re-enable interrupt after "
10102 				"slot reset.\n");
10103 		return PCI_ERS_RESULT_DISCONNECT;
10104 	} else
10105 		phba->intr_mode = intr_mode;
10106 
10107 	/* Log the current active interrupt mode */
10108 	lpfc_log_intr_mode(phba, phba->intr_mode);
10109 
10110 	return PCI_ERS_RESULT_RECOVERED;
10111 }
10112 
10113 /**
10114  * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
10115  * @pdev: pointer to PCI device
10116  *
10117  * This routine is called from the PCI subsystem for error handling to device
10118  * with SLI-4 interface spec. It is called when kernel error recovery tells
10119  * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
10120  * error recovery. After this call, traffic can start to flow from this device
10121  * again.
10122  **/
10123 static void
10124 lpfc_io_resume_s4(struct pci_dev *pdev)
10125 {
10126 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
10127 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10128 
10129 	/*
10130 	 * In case of slot reset, as function reset is performed through
10131 	 * mailbox command which needs DMA to be enabled, this operation
10132 	 * has to be moved to the io resume phase. Taking device offline
10133 	 * will perform the necessary cleanup.
10134 	 */
10135 	if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
10136 		/* Perform device reset */
10137 		lpfc_offline_prep(phba, LPFC_MBX_WAIT);
10138 		lpfc_offline(phba);
10139 		lpfc_sli_brdrestart(phba);
10140 		/* Bring the device back online */
10141 		lpfc_online(phba);
10142 	}
10143 
10144 	/* Clean up Advanced Error Reporting (AER) if needed */
10145 	if (phba->hba_flag & HBA_AER_ENABLED)
10146 		pci_cleanup_aer_uncorrect_error_status(pdev);
10147 }
10148 
10149 /**
10150  * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
10151  * @pdev: pointer to PCI device
10152  * @pid: pointer to PCI device identifier
10153  *
10154  * This routine is to be registered to the kernel's PCI subsystem. When an
10155  * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
10156  * at PCI device-specific information of the device and driver to see if the
10157  * driver state that it can support this kind of device. If the match is
10158  * successful, the driver core invokes this routine. This routine dispatches
10159  * the action to the proper SLI-3 or SLI-4 device probing routine, which will
10160  * do all the initialization that it needs to do to handle the HBA device
10161  * properly.
10162  *
10163  * Return code
10164  * 	0 - driver can claim the device
10165  * 	negative value - driver can not claim the device
10166  **/
10167 static int
10168 lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
10169 {
10170 	int rc;
10171 	struct lpfc_sli_intf intf;
10172 
10173 	if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
10174 		return -ENODEV;
10175 
10176 	if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
10177 	    (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
10178 		rc = lpfc_pci_probe_one_s4(pdev, pid);
10179 	else
10180 		rc = lpfc_pci_probe_one_s3(pdev, pid);
10181 
10182 	return rc;
10183 }
10184 
10185 /**
10186  * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
10187  * @pdev: pointer to PCI device
10188  *
10189  * This routine is to be registered to the kernel's PCI subsystem. When an
10190  * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
10191  * This routine dispatches the action to the proper SLI-3 or SLI-4 device
10192  * remove routine, which will perform all the necessary cleanup for the
10193  * device to be removed from the PCI subsystem properly.
10194  **/
10195 static void
10196 lpfc_pci_remove_one(struct pci_dev *pdev)
10197 {
10198 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
10199 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10200 
10201 	switch (phba->pci_dev_grp) {
10202 	case LPFC_PCI_DEV_LP:
10203 		lpfc_pci_remove_one_s3(pdev);
10204 		break;
10205 	case LPFC_PCI_DEV_OC:
10206 		lpfc_pci_remove_one_s4(pdev);
10207 		break;
10208 	default:
10209 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10210 				"1424 Invalid PCI device group: 0x%x\n",
10211 				phba->pci_dev_grp);
10212 		break;
10213 	}
10214 	return;
10215 }
10216 
10217 /**
10218  * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
10219  * @pdev: pointer to PCI device
10220  * @msg: power management message
10221  *
10222  * This routine is to be registered to the kernel's PCI subsystem to support
10223  * system Power Management (PM). When PM invokes this method, it dispatches
10224  * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
10225  * suspend the device.
10226  *
10227  * Return code
10228  * 	0 - driver suspended the device
10229  * 	Error otherwise
10230  **/
10231 static int
10232 lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
10233 {
10234 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
10235 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10236 	int rc = -ENODEV;
10237 
10238 	switch (phba->pci_dev_grp) {
10239 	case LPFC_PCI_DEV_LP:
10240 		rc = lpfc_pci_suspend_one_s3(pdev, msg);
10241 		break;
10242 	case LPFC_PCI_DEV_OC:
10243 		rc = lpfc_pci_suspend_one_s4(pdev, msg);
10244 		break;
10245 	default:
10246 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10247 				"1425 Invalid PCI device group: 0x%x\n",
10248 				phba->pci_dev_grp);
10249 		break;
10250 	}
10251 	return rc;
10252 }
10253 
10254 /**
10255  * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
10256  * @pdev: pointer to PCI device
10257  *
10258  * This routine is to be registered to the kernel's PCI subsystem to support
10259  * system Power Management (PM). When PM invokes this method, it dispatches
10260  * the action to the proper SLI-3 or SLI-4 device resume routine, which will
10261  * resume the device.
10262  *
10263  * Return code
10264  * 	0 - driver suspended the device
10265  * 	Error otherwise
10266  **/
10267 static int
10268 lpfc_pci_resume_one(struct pci_dev *pdev)
10269 {
10270 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
10271 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10272 	int rc = -ENODEV;
10273 
10274 	switch (phba->pci_dev_grp) {
10275 	case LPFC_PCI_DEV_LP:
10276 		rc = lpfc_pci_resume_one_s3(pdev);
10277 		break;
10278 	case LPFC_PCI_DEV_OC:
10279 		rc = lpfc_pci_resume_one_s4(pdev);
10280 		break;
10281 	default:
10282 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10283 				"1426 Invalid PCI device group: 0x%x\n",
10284 				phba->pci_dev_grp);
10285 		break;
10286 	}
10287 	return rc;
10288 }
10289 
10290 /**
10291  * lpfc_io_error_detected - lpfc method for handling PCI I/O error
10292  * @pdev: pointer to PCI device.
10293  * @state: the current PCI connection state.
10294  *
10295  * This routine is registered to the PCI subsystem for error handling. This
10296  * function is called by the PCI subsystem after a PCI bus error affecting
10297  * this device has been detected. When this routine is invoked, it dispatches
10298  * the action to the proper SLI-3 or SLI-4 device error detected handling
10299  * routine, which will perform the proper error detected operation.
10300  *
10301  * Return codes
10302  * 	PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10303  * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10304  **/
10305 static pci_ers_result_t
10306 lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
10307 {
10308 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
10309 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10310 	pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
10311 
10312 	switch (phba->pci_dev_grp) {
10313 	case LPFC_PCI_DEV_LP:
10314 		rc = lpfc_io_error_detected_s3(pdev, state);
10315 		break;
10316 	case LPFC_PCI_DEV_OC:
10317 		rc = lpfc_io_error_detected_s4(pdev, state);
10318 		break;
10319 	default:
10320 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10321 				"1427 Invalid PCI device group: 0x%x\n",
10322 				phba->pci_dev_grp);
10323 		break;
10324 	}
10325 	return rc;
10326 }
10327 
10328 /**
10329  * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
10330  * @pdev: pointer to PCI device.
10331  *
10332  * This routine is registered to the PCI subsystem for error handling. This
10333  * function is called after PCI bus has been reset to restart the PCI card
10334  * from scratch, as if from a cold-boot. When this routine is invoked, it
10335  * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
10336  * routine, which will perform the proper device reset.
10337  *
10338  * Return codes
10339  * 	PCI_ERS_RESULT_RECOVERED - the device has been recovered
10340  * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10341  **/
10342 static pci_ers_result_t
10343 lpfc_io_slot_reset(struct pci_dev *pdev)
10344 {
10345 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
10346 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10347 	pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
10348 
10349 	switch (phba->pci_dev_grp) {
10350 	case LPFC_PCI_DEV_LP:
10351 		rc = lpfc_io_slot_reset_s3(pdev);
10352 		break;
10353 	case LPFC_PCI_DEV_OC:
10354 		rc = lpfc_io_slot_reset_s4(pdev);
10355 		break;
10356 	default:
10357 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10358 				"1428 Invalid PCI device group: 0x%x\n",
10359 				phba->pci_dev_grp);
10360 		break;
10361 	}
10362 	return rc;
10363 }
10364 
10365 /**
10366  * lpfc_io_resume - lpfc method for resuming PCI I/O operation
10367  * @pdev: pointer to PCI device
10368  *
10369  * This routine is registered to the PCI subsystem for error handling. It
10370  * is called when kernel error recovery tells the lpfc driver that it is
10371  * OK to resume normal PCI operation after PCI bus error recovery. When
10372  * this routine is invoked, it dispatches the action to the proper SLI-3
10373  * or SLI-4 device io_resume routine, which will resume the device operation.
10374  **/
10375 static void
10376 lpfc_io_resume(struct pci_dev *pdev)
10377 {
10378 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
10379 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10380 
10381 	switch (phba->pci_dev_grp) {
10382 	case LPFC_PCI_DEV_LP:
10383 		lpfc_io_resume_s3(pdev);
10384 		break;
10385 	case LPFC_PCI_DEV_OC:
10386 		lpfc_io_resume_s4(pdev);
10387 		break;
10388 	default:
10389 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10390 				"1429 Invalid PCI device group: 0x%x\n",
10391 				phba->pci_dev_grp);
10392 		break;
10393 	}
10394 	return;
10395 }
10396 
10397 static struct pci_device_id lpfc_id_table[] = {
10398 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
10399 		PCI_ANY_ID, PCI_ANY_ID, },
10400 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
10401 		PCI_ANY_ID, PCI_ANY_ID, },
10402 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
10403 		PCI_ANY_ID, PCI_ANY_ID, },
10404 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
10405 		PCI_ANY_ID, PCI_ANY_ID, },
10406 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
10407 		PCI_ANY_ID, PCI_ANY_ID, },
10408 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
10409 		PCI_ANY_ID, PCI_ANY_ID, },
10410 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
10411 		PCI_ANY_ID, PCI_ANY_ID, },
10412 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
10413 		PCI_ANY_ID, PCI_ANY_ID, },
10414 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
10415 		PCI_ANY_ID, PCI_ANY_ID, },
10416 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
10417 		PCI_ANY_ID, PCI_ANY_ID, },
10418 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
10419 		PCI_ANY_ID, PCI_ANY_ID, },
10420 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
10421 		PCI_ANY_ID, PCI_ANY_ID, },
10422 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
10423 		PCI_ANY_ID, PCI_ANY_ID, },
10424 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
10425 		PCI_ANY_ID, PCI_ANY_ID, },
10426 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
10427 		PCI_ANY_ID, PCI_ANY_ID, },
10428 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
10429 		PCI_ANY_ID, PCI_ANY_ID, },
10430 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
10431 		PCI_ANY_ID, PCI_ANY_ID, },
10432 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
10433 		PCI_ANY_ID, PCI_ANY_ID, },
10434 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
10435 		PCI_ANY_ID, PCI_ANY_ID, },
10436 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
10437 		PCI_ANY_ID, PCI_ANY_ID, },
10438 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
10439 		PCI_ANY_ID, PCI_ANY_ID, },
10440 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
10441 		PCI_ANY_ID, PCI_ANY_ID, },
10442 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
10443 		PCI_ANY_ID, PCI_ANY_ID, },
10444 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
10445 		PCI_ANY_ID, PCI_ANY_ID, },
10446 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
10447 		PCI_ANY_ID, PCI_ANY_ID, },
10448 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
10449 		PCI_ANY_ID, PCI_ANY_ID, },
10450 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
10451 		PCI_ANY_ID, PCI_ANY_ID, },
10452 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
10453 		PCI_ANY_ID, PCI_ANY_ID, },
10454 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
10455 		PCI_ANY_ID, PCI_ANY_ID, },
10456 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
10457 		PCI_ANY_ID, PCI_ANY_ID, },
10458 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
10459 		PCI_ANY_ID, PCI_ANY_ID, },
10460 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
10461 		PCI_ANY_ID, PCI_ANY_ID, },
10462 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
10463 		PCI_ANY_ID, PCI_ANY_ID, },
10464 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
10465 		PCI_ANY_ID, PCI_ANY_ID, },
10466 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
10467 		PCI_ANY_ID, PCI_ANY_ID, },
10468 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
10469 		PCI_ANY_ID, PCI_ANY_ID, },
10470 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
10471 		PCI_ANY_ID, PCI_ANY_ID, },
10472 	{PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
10473 		PCI_ANY_ID, PCI_ANY_ID, },
10474 	{PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TOMCAT,
10475 		PCI_ANY_ID, PCI_ANY_ID, },
10476 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FALCON,
10477 		PCI_ANY_ID, PCI_ANY_ID, },
10478 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BALIUS,
10479 		PCI_ANY_ID, PCI_ANY_ID, },
10480 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC,
10481 		PCI_ANY_ID, PCI_ANY_ID, },
10482 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE,
10483 		PCI_ANY_ID, PCI_ANY_ID, },
10484 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC_VF,
10485 		PCI_ANY_ID, PCI_ANY_ID, },
10486 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE_VF,
10487 		PCI_ANY_ID, PCI_ANY_ID, },
10488 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SKYHAWK,
10489 		PCI_ANY_ID, PCI_ANY_ID, },
10490 	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SKYHAWK_VF,
10491 		PCI_ANY_ID, PCI_ANY_ID, },
10492 	{ 0 }
10493 };
10494 
10495 MODULE_DEVICE_TABLE(pci, lpfc_id_table);
10496 
10497 static const struct pci_error_handlers lpfc_err_handler = {
10498 	.error_detected = lpfc_io_error_detected,
10499 	.slot_reset = lpfc_io_slot_reset,
10500 	.resume = lpfc_io_resume,
10501 };
10502 
10503 static struct pci_driver lpfc_driver = {
10504 	.name		= LPFC_DRIVER_NAME,
10505 	.id_table	= lpfc_id_table,
10506 	.probe		= lpfc_pci_probe_one,
10507 	.remove		= lpfc_pci_remove_one,
10508 	.suspend        = lpfc_pci_suspend_one,
10509 	.resume		= lpfc_pci_resume_one,
10510 	.err_handler    = &lpfc_err_handler,
10511 };
10512 
10513 static const struct file_operations lpfc_mgmt_fop = {
10514 	.owner = THIS_MODULE,
10515 };
10516 
10517 static struct miscdevice lpfc_mgmt_dev = {
10518 	.minor = MISC_DYNAMIC_MINOR,
10519 	.name = "lpfcmgmt",
10520 	.fops = &lpfc_mgmt_fop,
10521 };
10522 
10523 /**
10524  * lpfc_init - lpfc module initialization routine
10525  *
10526  * This routine is to be invoked when the lpfc module is loaded into the
10527  * kernel. The special kernel macro module_init() is used to indicate the
10528  * role of this routine to the kernel as lpfc module entry point.
10529  *
10530  * Return codes
10531  *   0 - successful
10532  *   -ENOMEM - FC attach transport failed
10533  *   all others - failed
10534  */
10535 static int __init
10536 lpfc_init(void)
10537 {
10538 	int error = 0;
10539 
10540 	printk(LPFC_MODULE_DESC "\n");
10541 	printk(LPFC_COPYRIGHT "\n");
10542 
10543 	error = misc_register(&lpfc_mgmt_dev);
10544 	if (error)
10545 		printk(KERN_ERR "Could not register lpfcmgmt device, "
10546 			"misc_register returned with status %d", error);
10547 
10548 	if (lpfc_enable_npiv) {
10549 		lpfc_transport_functions.vport_create = lpfc_vport_create;
10550 		lpfc_transport_functions.vport_delete = lpfc_vport_delete;
10551 	}
10552 	lpfc_transport_template =
10553 				fc_attach_transport(&lpfc_transport_functions);
10554 	if (lpfc_transport_template == NULL)
10555 		return -ENOMEM;
10556 	if (lpfc_enable_npiv) {
10557 		lpfc_vport_transport_template =
10558 			fc_attach_transport(&lpfc_vport_transport_functions);
10559 		if (lpfc_vport_transport_template == NULL) {
10560 			fc_release_transport(lpfc_transport_template);
10561 			return -ENOMEM;
10562 		}
10563 	}
10564 	error = pci_register_driver(&lpfc_driver);
10565 	if (error) {
10566 		fc_release_transport(lpfc_transport_template);
10567 		if (lpfc_enable_npiv)
10568 			fc_release_transport(lpfc_vport_transport_template);
10569 	}
10570 
10571 	return error;
10572 }
10573 
10574 /**
10575  * lpfc_exit - lpfc module removal routine
10576  *
10577  * This routine is invoked when the lpfc module is removed from the kernel.
10578  * The special kernel macro module_exit() is used to indicate the role of
10579  * this routine to the kernel as lpfc module exit point.
10580  */
10581 static void __exit
10582 lpfc_exit(void)
10583 {
10584 	misc_deregister(&lpfc_mgmt_dev);
10585 	pci_unregister_driver(&lpfc_driver);
10586 	fc_release_transport(lpfc_transport_template);
10587 	if (lpfc_enable_npiv)
10588 		fc_release_transport(lpfc_vport_transport_template);
10589 	if (_dump_buf_data) {
10590 		printk(KERN_ERR	"9062 BLKGRD: freeing %lu pages for "
10591 				"_dump_buf_data at 0x%p\n",
10592 				(1L << _dump_buf_data_order), _dump_buf_data);
10593 		free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
10594 	}
10595 
10596 	if (_dump_buf_dif) {
10597 		printk(KERN_ERR	"9049 BLKGRD: freeing %lu pages for "
10598 				"_dump_buf_dif at 0x%p\n",
10599 				(1L << _dump_buf_dif_order), _dump_buf_dif);
10600 		free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
10601 	}
10602 }
10603 
10604 module_init(lpfc_init);
10605 module_exit(lpfc_exit);
10606 MODULE_LICENSE("GPL");
10607 MODULE_DESCRIPTION(LPFC_MODULE_DESC);
10608 MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
10609 MODULE_VERSION("0:" LPFC_DRIVER_VERSION);
10610