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