lpfc_init.c revision 4ef58d4e2ad1fa2a3e5bbf41af2284671fca8cf8
1/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for         *
3 * Fibre Channel Host Bus Adapters.                                *
4 * Copyright (C) 2004-2009 Emulex.  All rights reserved.           *
5 * EMULEX and SLI are trademarks of Emulex.                        *
6 * www.emulex.com                                                  *
7 * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
8 *                                                                 *
9 * This program is free software; you can redistribute it and/or   *
10 * modify it under the terms of version 2 of the GNU General       *
11 * Public License as published by the Free Software Foundation.    *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
18 * more details, a copy of which can be found in the file COPYING  *
19 * included with this package.                                     *
20 *******************************************************************/
21
22#include <linux/blkdev.h>
23#include <linux/delay.h>
24#include <linux/dma-mapping.h>
25#include <linux/idr.h>
26#include <linux/interrupt.h>
27#include <linux/kthread.h>
28#include <linux/pci.h>
29#include <linux/spinlock.h>
30#include <linux/ctype.h>
31#include <linux/aer.h>
32
33#include <scsi/scsi.h>
34#include <scsi/scsi_device.h>
35#include <scsi/scsi_host.h>
36#include <scsi/scsi_transport_fc.h>
37
38#include "lpfc_hw4.h"
39#include "lpfc_hw.h"
40#include "lpfc_sli.h"
41#include "lpfc_sli4.h"
42#include "lpfc_nl.h"
43#include "lpfc_disc.h"
44#include "lpfc_scsi.h"
45#include "lpfc.h"
46#include "lpfc_logmsg.h"
47#include "lpfc_crtn.h"
48#include "lpfc_vport.h"
49#include "lpfc_version.h"
50
51char *_dump_buf_data;
52unsigned long _dump_buf_data_order;
53char *_dump_buf_dif;
54unsigned long _dump_buf_dif_order;
55spinlock_t _dump_buf_lock;
56
57static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
58static int lpfc_post_rcv_buf(struct lpfc_hba *);
59static int lpfc_sli4_queue_create(struct lpfc_hba *);
60static void lpfc_sli4_queue_destroy(struct lpfc_hba *);
61static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
62static int lpfc_setup_endian_order(struct lpfc_hba *);
63static int lpfc_sli4_read_config(struct lpfc_hba *);
64static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
65static void lpfc_free_sgl_list(struct lpfc_hba *);
66static int lpfc_init_sgl_list(struct lpfc_hba *);
67static int lpfc_init_active_sgl_array(struct lpfc_hba *);
68static void lpfc_free_active_sgl(struct lpfc_hba *);
69static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
70static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
71static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
72static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
73static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
74
75static struct scsi_transport_template *lpfc_transport_template = NULL;
76static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
77static DEFINE_IDR(lpfc_hba_index);
78
79/**
80 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
81 * @phba: pointer to lpfc hba data structure.
82 *
83 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
84 * mailbox command. It retrieves the revision information from the HBA and
85 * collects the Vital Product Data (VPD) about the HBA for preparing the
86 * configuration of the HBA.
87 *
88 * Return codes:
89 *   0 - success.
90 *   -ERESTART - requests the SLI layer to reset the HBA and try again.
91 *   Any other value - indicates an error.
92 **/
93int
94lpfc_config_port_prep(struct lpfc_hba *phba)
95{
96	lpfc_vpd_t *vp = &phba->vpd;
97	int i = 0, rc;
98	LPFC_MBOXQ_t *pmb;
99	MAILBOX_t *mb;
100	char *lpfc_vpd_data = NULL;
101	uint16_t offset = 0;
102	static char licensed[56] =
103		    "key unlock for use with gnu public licensed code only\0";
104	static int init_key = 1;
105
106	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
107	if (!pmb) {
108		phba->link_state = LPFC_HBA_ERROR;
109		return -ENOMEM;
110	}
111
112	mb = &pmb->u.mb;
113	phba->link_state = LPFC_INIT_MBX_CMDS;
114
115	if (lpfc_is_LC_HBA(phba->pcidev->device)) {
116		if (init_key) {
117			uint32_t *ptext = (uint32_t *) licensed;
118
119			for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
120				*ptext = cpu_to_be32(*ptext);
121			init_key = 0;
122		}
123
124		lpfc_read_nv(phba, pmb);
125		memset((char*)mb->un.varRDnvp.rsvd3, 0,
126			sizeof (mb->un.varRDnvp.rsvd3));
127		memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
128			 sizeof (licensed));
129
130		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
131
132		if (rc != MBX_SUCCESS) {
133			lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
134					"0324 Config Port initialization "
135					"error, mbxCmd x%x READ_NVPARM, "
136					"mbxStatus x%x\n",
137					mb->mbxCommand, mb->mbxStatus);
138			mempool_free(pmb, phba->mbox_mem_pool);
139			return -ERESTART;
140		}
141		memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
142		       sizeof(phba->wwnn));
143		memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
144		       sizeof(phba->wwpn));
145	}
146
147	phba->sli3_options = 0x0;
148
149	/* Setup and issue mailbox READ REV command */
150	lpfc_read_rev(phba, pmb);
151	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
152	if (rc != MBX_SUCCESS) {
153		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
154				"0439 Adapter failed to init, mbxCmd x%x "
155				"READ_REV, mbxStatus x%x\n",
156				mb->mbxCommand, mb->mbxStatus);
157		mempool_free( pmb, phba->mbox_mem_pool);
158		return -ERESTART;
159	}
160
161
162	/*
163	 * The value of rr must be 1 since the driver set the cv field to 1.
164	 * This setting requires the FW to set all revision fields.
165	 */
166	if (mb->un.varRdRev.rr == 0) {
167		vp->rev.rBit = 0;
168		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
169				"0440 Adapter failed to init, READ_REV has "
170				"missing revision information.\n");
171		mempool_free(pmb, phba->mbox_mem_pool);
172		return -ERESTART;
173	}
174
175	if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
176		mempool_free(pmb, phba->mbox_mem_pool);
177		return -EINVAL;
178	}
179
180	/* Save information as VPD data */
181	vp->rev.rBit = 1;
182	memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
183	vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
184	memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
185	vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
186	memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
187	vp->rev.biuRev = mb->un.varRdRev.biuRev;
188	vp->rev.smRev = mb->un.varRdRev.smRev;
189	vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
190	vp->rev.endecRev = mb->un.varRdRev.endecRev;
191	vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
192	vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
193	vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
194	vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
195	vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
196	vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
197
198	/* If the sli feature level is less then 9, we must
199	 * tear down all RPIs and VPIs on link down if NPIV
200	 * is enabled.
201	 */
202	if (vp->rev.feaLevelHigh < 9)
203		phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
204
205	if (lpfc_is_LC_HBA(phba->pcidev->device))
206		memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
207						sizeof (phba->RandomData));
208
209	/* Get adapter VPD information */
210	lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
211	if (!lpfc_vpd_data)
212		goto out_free_mbox;
213
214	do {
215		lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
216		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
217
218		if (rc != MBX_SUCCESS) {
219			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
220					"0441 VPD not present on adapter, "
221					"mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
222					mb->mbxCommand, mb->mbxStatus);
223			mb->un.varDmp.word_cnt = 0;
224		}
225		/* dump mem may return a zero when finished or we got a
226		 * mailbox error, either way we are done.
227		 */
228		if (mb->un.varDmp.word_cnt == 0)
229			break;
230		if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
231			mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
232		lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
233				      lpfc_vpd_data + offset,
234				      mb->un.varDmp.word_cnt);
235		offset += mb->un.varDmp.word_cnt;
236	} while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
237	lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
238
239	kfree(lpfc_vpd_data);
240out_free_mbox:
241	mempool_free(pmb, phba->mbox_mem_pool);
242	return 0;
243}
244
245/**
246 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
247 * @phba: pointer to lpfc hba data structure.
248 * @pmboxq: pointer to the driver internal queue element for mailbox command.
249 *
250 * This is the completion handler for driver's configuring asynchronous event
251 * mailbox command to the device. If the mailbox command returns successfully,
252 * it will set internal async event support flag to 1; otherwise, it will
253 * set internal async event support flag to 0.
254 **/
255static void
256lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
257{
258	if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
259		phba->temp_sensor_support = 1;
260	else
261		phba->temp_sensor_support = 0;
262	mempool_free(pmboxq, phba->mbox_mem_pool);
263	return;
264}
265
266/**
267 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
268 * @phba: pointer to lpfc hba data structure.
269 * @pmboxq: pointer to the driver internal queue element for mailbox command.
270 *
271 * This is the completion handler for dump mailbox command for getting
272 * wake up parameters. When this command complete, the response contain
273 * Option rom version of the HBA. This function translate the version number
274 * into a human readable string and store it in OptionROMVersion.
275 **/
276static void
277lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
278{
279	struct prog_id *prg;
280	uint32_t prog_id_word;
281	char dist = ' ';
282	/* character array used for decoding dist type. */
283	char dist_char[] = "nabx";
284
285	if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
286		mempool_free(pmboxq, phba->mbox_mem_pool);
287		return;
288	}
289
290	prg = (struct prog_id *) &prog_id_word;
291
292	/* word 7 contain option rom version */
293	prog_id_word = pmboxq->u.mb.un.varWords[7];
294
295	/* Decode the Option rom version word to a readable string */
296	if (prg->dist < 4)
297		dist = dist_char[prg->dist];
298
299	if ((prg->dist == 3) && (prg->num == 0))
300		sprintf(phba->OptionROMVersion, "%d.%d%d",
301			prg->ver, prg->rev, prg->lev);
302	else
303		sprintf(phba->OptionROMVersion, "%d.%d%d%c%d",
304			prg->ver, prg->rev, prg->lev,
305			dist, prg->num);
306	mempool_free(pmboxq, phba->mbox_mem_pool);
307	return;
308}
309
310/**
311 * lpfc_config_port_post - Perform lpfc initialization after config port
312 * @phba: pointer to lpfc hba data structure.
313 *
314 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
315 * command call. It performs all internal resource and state setups on the
316 * port: post IOCB buffers, enable appropriate host interrupt attentions,
317 * ELS ring timers, etc.
318 *
319 * Return codes
320 *   0 - success.
321 *   Any other value - error.
322 **/
323int
324lpfc_config_port_post(struct lpfc_hba *phba)
325{
326	struct lpfc_vport *vport = phba->pport;
327	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
328	LPFC_MBOXQ_t *pmb;
329	MAILBOX_t *mb;
330	struct lpfc_dmabuf *mp;
331	struct lpfc_sli *psli = &phba->sli;
332	uint32_t status, timeout;
333	int i, j;
334	int rc;
335
336	spin_lock_irq(&phba->hbalock);
337	/*
338	 * If the Config port completed correctly the HBA is not
339	 * over heated any more.
340	 */
341	if (phba->over_temp_state == HBA_OVER_TEMP)
342		phba->over_temp_state = HBA_NORMAL_TEMP;
343	spin_unlock_irq(&phba->hbalock);
344
345	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
346	if (!pmb) {
347		phba->link_state = LPFC_HBA_ERROR;
348		return -ENOMEM;
349	}
350	mb = &pmb->u.mb;
351
352	/* Get login parameters for NID.  */
353	lpfc_read_sparam(phba, pmb, 0);
354	pmb->vport = vport;
355	if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
356		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
357				"0448 Adapter failed init, mbxCmd x%x "
358				"READ_SPARM mbxStatus x%x\n",
359				mb->mbxCommand, mb->mbxStatus);
360		phba->link_state = LPFC_HBA_ERROR;
361		mp = (struct lpfc_dmabuf *) pmb->context1;
362		mempool_free( pmb, phba->mbox_mem_pool);
363		lpfc_mbuf_free(phba, mp->virt, mp->phys);
364		kfree(mp);
365		return -EIO;
366	}
367
368	mp = (struct lpfc_dmabuf *) pmb->context1;
369
370	memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
371	lpfc_mbuf_free(phba, mp->virt, mp->phys);
372	kfree(mp);
373	pmb->context1 = NULL;
374
375	if (phba->cfg_soft_wwnn)
376		u64_to_wwn(phba->cfg_soft_wwnn,
377			   vport->fc_sparam.nodeName.u.wwn);
378	if (phba->cfg_soft_wwpn)
379		u64_to_wwn(phba->cfg_soft_wwpn,
380			   vport->fc_sparam.portName.u.wwn);
381	memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
382	       sizeof (struct lpfc_name));
383	memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
384	       sizeof (struct lpfc_name));
385
386	/* Update the fc_host data structures with new wwn. */
387	fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
388	fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
389	fc_host_max_npiv_vports(shost) = phba->max_vpi;
390
391	/* If no serial number in VPD data, use low 6 bytes of WWNN */
392	/* This should be consolidated into parse_vpd ? - mr */
393	if (phba->SerialNumber[0] == 0) {
394		uint8_t *outptr;
395
396		outptr = &vport->fc_nodename.u.s.IEEE[0];
397		for (i = 0; i < 12; i++) {
398			status = *outptr++;
399			j = ((status & 0xf0) >> 4);
400			if (j <= 9)
401				phba->SerialNumber[i] =
402				    (char)((uint8_t) 0x30 + (uint8_t) j);
403			else
404				phba->SerialNumber[i] =
405				    (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
406			i++;
407			j = (status & 0xf);
408			if (j <= 9)
409				phba->SerialNumber[i] =
410				    (char)((uint8_t) 0x30 + (uint8_t) j);
411			else
412				phba->SerialNumber[i] =
413				    (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
414		}
415	}
416
417	lpfc_read_config(phba, pmb);
418	pmb->vport = vport;
419	if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
420		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
421				"0453 Adapter failed to init, mbxCmd x%x "
422				"READ_CONFIG, mbxStatus x%x\n",
423				mb->mbxCommand, mb->mbxStatus);
424		phba->link_state = LPFC_HBA_ERROR;
425		mempool_free( pmb, phba->mbox_mem_pool);
426		return -EIO;
427	}
428
429	/* Check if the port is disabled */
430	lpfc_sli_read_link_ste(phba);
431
432	/* Reset the DFT_HBA_Q_DEPTH to the max xri  */
433	if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
434		phba->cfg_hba_queue_depth =
435			(mb->un.varRdConfig.max_xri + 1) -
436					lpfc_sli4_get_els_iocb_cnt(phba);
437
438	phba->lmt = mb->un.varRdConfig.lmt;
439
440	/* Get the default values for Model Name and Description */
441	lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
442
443	if ((phba->cfg_link_speed > LINK_SPEED_10G)
444	    || ((phba->cfg_link_speed == LINK_SPEED_1G)
445		&& !(phba->lmt & LMT_1Gb))
446	    || ((phba->cfg_link_speed == LINK_SPEED_2G)
447		&& !(phba->lmt & LMT_2Gb))
448	    || ((phba->cfg_link_speed == LINK_SPEED_4G)
449		&& !(phba->lmt & LMT_4Gb))
450	    || ((phba->cfg_link_speed == LINK_SPEED_8G)
451		&& !(phba->lmt & LMT_8Gb))
452	    || ((phba->cfg_link_speed == LINK_SPEED_10G)
453		&& !(phba->lmt & LMT_10Gb))) {
454		/* Reset link speed to auto */
455		lpfc_printf_log(phba, KERN_WARNING, LOG_LINK_EVENT,
456			"1302 Invalid speed for this board: "
457			"Reset link speed to auto: x%x\n",
458			phba->cfg_link_speed);
459			phba->cfg_link_speed = LINK_SPEED_AUTO;
460	}
461
462	phba->link_state = LPFC_LINK_DOWN;
463
464	/* Only process IOCBs on ELS ring till hba_state is READY */
465	if (psli->ring[psli->extra_ring].cmdringaddr)
466		psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
467	if (psli->ring[psli->fcp_ring].cmdringaddr)
468		psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
469	if (psli->ring[psli->next_ring].cmdringaddr)
470		psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
471
472	/* Post receive buffers for desired rings */
473	if (phba->sli_rev != 3)
474		lpfc_post_rcv_buf(phba);
475
476	/*
477	 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
478	 */
479	if (phba->intr_type == MSIX) {
480		rc = lpfc_config_msi(phba, pmb);
481		if (rc) {
482			mempool_free(pmb, phba->mbox_mem_pool);
483			return -EIO;
484		}
485		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
486		if (rc != MBX_SUCCESS) {
487			lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
488					"0352 Config MSI mailbox command "
489					"failed, mbxCmd x%x, mbxStatus x%x\n",
490					pmb->u.mb.mbxCommand,
491					pmb->u.mb.mbxStatus);
492			mempool_free(pmb, phba->mbox_mem_pool);
493			return -EIO;
494		}
495	}
496
497	spin_lock_irq(&phba->hbalock);
498	/* Initialize ERATT handling flag */
499	phba->hba_flag &= ~HBA_ERATT_HANDLED;
500
501	/* Enable appropriate host interrupts */
502	status = readl(phba->HCregaddr);
503	status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
504	if (psli->num_rings > 0)
505		status |= HC_R0INT_ENA;
506	if (psli->num_rings > 1)
507		status |= HC_R1INT_ENA;
508	if (psli->num_rings > 2)
509		status |= HC_R2INT_ENA;
510	if (psli->num_rings > 3)
511		status |= HC_R3INT_ENA;
512
513	if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
514	    (phba->cfg_poll & DISABLE_FCP_RING_INT))
515		status &= ~(HC_R0INT_ENA);
516
517	writel(status, phba->HCregaddr);
518	readl(phba->HCregaddr); /* flush */
519	spin_unlock_irq(&phba->hbalock);
520
521	/* Set up ring-0 (ELS) timer */
522	timeout = phba->fc_ratov * 2;
523	mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
524	/* Set up heart beat (HB) timer */
525	mod_timer(&phba->hb_tmofunc, jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
526	phba->hb_outstanding = 0;
527	phba->last_completion_time = jiffies;
528	/* Set up error attention (ERATT) polling timer */
529	mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
530
531	if (phba->hba_flag & LINK_DISABLED) {
532		lpfc_printf_log(phba,
533			KERN_ERR, LOG_INIT,
534			"2598 Adapter Link is disabled.\n");
535		lpfc_down_link(phba, pmb);
536		pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
537		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
538		if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
539			lpfc_printf_log(phba,
540			KERN_ERR, LOG_INIT,
541			"2599 Adapter failed to issue DOWN_LINK"
542			" mbox command rc 0x%x\n", rc);
543
544			mempool_free(pmb, phba->mbox_mem_pool);
545			return -EIO;
546		}
547	} else {
548		lpfc_init_link(phba, pmb, phba->cfg_topology,
549			phba->cfg_link_speed);
550		pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
551		lpfc_set_loopback_flag(phba);
552		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
553		if (rc != MBX_SUCCESS) {
554			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
555				"0454 Adapter failed to init, mbxCmd x%x "
556				"INIT_LINK, mbxStatus x%x\n",
557				mb->mbxCommand, mb->mbxStatus);
558
559			/* Clear all interrupt enable conditions */
560			writel(0, phba->HCregaddr);
561			readl(phba->HCregaddr); /* flush */
562			/* Clear all pending interrupts */
563			writel(0xffffffff, phba->HAregaddr);
564			readl(phba->HAregaddr); /* flush */
565
566			phba->link_state = LPFC_HBA_ERROR;
567			if (rc != MBX_BUSY)
568				mempool_free(pmb, phba->mbox_mem_pool);
569			return -EIO;
570		}
571	}
572	/* MBOX buffer will be freed in mbox compl */
573	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
574	lpfc_config_async(phba, pmb, LPFC_ELS_RING);
575	pmb->mbox_cmpl = lpfc_config_async_cmpl;
576	pmb->vport = phba->pport;
577	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
578
579	if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
580		lpfc_printf_log(phba,
581				KERN_ERR,
582				LOG_INIT,
583				"0456 Adapter failed to issue "
584				"ASYNCEVT_ENABLE mbox status x%x\n",
585				rc);
586		mempool_free(pmb, phba->mbox_mem_pool);
587	}
588
589	/* Get Option rom version */
590	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
591	lpfc_dump_wakeup_param(phba, pmb);
592	pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
593	pmb->vport = phba->pport;
594	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
595
596	if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
597		lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
598				"to get Option ROM version status x%x\n", rc);
599		mempool_free(pmb, phba->mbox_mem_pool);
600	}
601
602	return 0;
603}
604
605/**
606 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
607 * @phba: pointer to lpfc HBA data structure.
608 *
609 * This routine will do LPFC uninitialization before the HBA is reset when
610 * bringing down the SLI Layer.
611 *
612 * Return codes
613 *   0 - success.
614 *   Any other value - error.
615 **/
616int
617lpfc_hba_down_prep(struct lpfc_hba *phba)
618{
619	struct lpfc_vport **vports;
620	int i;
621
622	if (phba->sli_rev <= LPFC_SLI_REV3) {
623		/* Disable interrupts */
624		writel(0, phba->HCregaddr);
625		readl(phba->HCregaddr); /* flush */
626	}
627
628	if (phba->pport->load_flag & FC_UNLOADING)
629		lpfc_cleanup_discovery_resources(phba->pport);
630	else {
631		vports = lpfc_create_vport_work_array(phba);
632		if (vports != NULL)
633			for (i = 0; i <= phba->max_vports &&
634				vports[i] != NULL; i++)
635				lpfc_cleanup_discovery_resources(vports[i]);
636		lpfc_destroy_vport_work_array(phba, vports);
637	}
638	return 0;
639}
640
641/**
642 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
643 * @phba: pointer to lpfc HBA data structure.
644 *
645 * This routine will do uninitialization after the HBA is reset when bring
646 * down the SLI Layer.
647 *
648 * Return codes
649 *   0 - success.
650 *   Any other value - error.
651 **/
652static int
653lpfc_hba_down_post_s3(struct lpfc_hba *phba)
654{
655	struct lpfc_sli *psli = &phba->sli;
656	struct lpfc_sli_ring *pring;
657	struct lpfc_dmabuf *mp, *next_mp;
658	LIST_HEAD(completions);
659	int i;
660
661	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
662		lpfc_sli_hbqbuf_free_all(phba);
663	else {
664		/* Cleanup preposted buffers on the ELS ring */
665		pring = &psli->ring[LPFC_ELS_RING];
666		list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
667			list_del(&mp->list);
668			pring->postbufq_cnt--;
669			lpfc_mbuf_free(phba, mp->virt, mp->phys);
670			kfree(mp);
671		}
672	}
673
674	spin_lock_irq(&phba->hbalock);
675	for (i = 0; i < psli->num_rings; i++) {
676		pring = &psli->ring[i];
677
678		/* At this point in time the HBA is either reset or DOA. Either
679		 * way, nothing should be on txcmplq as it will NEVER complete.
680		 */
681		list_splice_init(&pring->txcmplq, &completions);
682		pring->txcmplq_cnt = 0;
683		spin_unlock_irq(&phba->hbalock);
684
685		/* Cancel all the IOCBs from the completions list */
686		lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
687				      IOERR_SLI_ABORTED);
688
689		lpfc_sli_abort_iocb_ring(phba, pring);
690		spin_lock_irq(&phba->hbalock);
691	}
692	spin_unlock_irq(&phba->hbalock);
693
694	return 0;
695}
696/**
697 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
698 * @phba: pointer to lpfc HBA data structure.
699 *
700 * This routine will do uninitialization after the HBA is reset when bring
701 * down the SLI Layer.
702 *
703 * Return codes
704 *   0 - success.
705 *   Any other value - error.
706 **/
707static int
708lpfc_hba_down_post_s4(struct lpfc_hba *phba)
709{
710	struct lpfc_scsi_buf *psb, *psb_next;
711	LIST_HEAD(aborts);
712	int ret;
713	unsigned long iflag = 0;
714	ret = lpfc_hba_down_post_s3(phba);
715	if (ret)
716		return ret;
717	/* At this point in time the HBA is either reset or DOA. Either
718	 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
719	 * on the lpfc_sgl_list so that it can either be freed if the
720	 * driver is unloading or reposted if the driver is restarting
721	 * the port.
722	 */
723	spin_lock_irq(&phba->hbalock);  /* required for lpfc_sgl_list and */
724					/* scsl_buf_list */
725	/* abts_sgl_list_lock required because worker thread uses this
726	 * list.
727	 */
728	spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
729	list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
730			&phba->sli4_hba.lpfc_sgl_list);
731	spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
732	/* abts_scsi_buf_list_lock required because worker thread uses this
733	 * list.
734	 */
735	spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
736	list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
737			&aborts);
738	spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
739	spin_unlock_irq(&phba->hbalock);
740
741	list_for_each_entry_safe(psb, psb_next, &aborts, list) {
742		psb->pCmd = NULL;
743		psb->status = IOSTAT_SUCCESS;
744	}
745	spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
746	list_splice(&aborts, &phba->lpfc_scsi_buf_list);
747	spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
748	return 0;
749}
750
751/**
752 * lpfc_hba_down_post - Wrapper func for hba down post routine
753 * @phba: pointer to lpfc HBA data structure.
754 *
755 * This routine wraps the actual SLI3 or SLI4 routine for performing
756 * uninitialization after the HBA is reset when bring down the SLI Layer.
757 *
758 * Return codes
759 *   0 - success.
760 *   Any other value - error.
761 **/
762int
763lpfc_hba_down_post(struct lpfc_hba *phba)
764{
765	return (*phba->lpfc_hba_down_post)(phba);
766}
767
768/**
769 * lpfc_hb_timeout - The HBA-timer timeout handler
770 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
771 *
772 * This is the HBA-timer timeout handler registered to the lpfc driver. When
773 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
774 * work-port-events bitmap and the worker thread is notified. This timeout
775 * event will be used by the worker thread to invoke the actual timeout
776 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
777 * be performed in the timeout handler and the HBA timeout event bit shall
778 * be cleared by the worker thread after it has taken the event bitmap out.
779 **/
780static void
781lpfc_hb_timeout(unsigned long ptr)
782{
783	struct lpfc_hba *phba;
784	uint32_t tmo_posted;
785	unsigned long iflag;
786
787	phba = (struct lpfc_hba *)ptr;
788
789	/* Check for heart beat timeout conditions */
790	spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
791	tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
792	if (!tmo_posted)
793		phba->pport->work_port_events |= WORKER_HB_TMO;
794	spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
795
796	/* Tell the worker thread there is work to do */
797	if (!tmo_posted)
798		lpfc_worker_wake_up(phba);
799	return;
800}
801
802/**
803 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
804 * @phba: pointer to lpfc hba data structure.
805 * @pmboxq: pointer to the driver internal queue element for mailbox command.
806 *
807 * This is the callback function to the lpfc heart-beat mailbox command.
808 * If configured, the lpfc driver issues the heart-beat mailbox command to
809 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
810 * heart-beat mailbox command is issued, the driver shall set up heart-beat
811 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
812 * heart-beat outstanding state. Once the mailbox command comes back and
813 * no error conditions detected, the heart-beat mailbox command timer is
814 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
815 * state is cleared for the next heart-beat. If the timer expired with the
816 * heart-beat outstanding state set, the driver will put the HBA offline.
817 **/
818static void
819lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
820{
821	unsigned long drvr_flag;
822
823	spin_lock_irqsave(&phba->hbalock, drvr_flag);
824	phba->hb_outstanding = 0;
825	spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
826
827	/* Check and reset heart-beat timer is necessary */
828	mempool_free(pmboxq, phba->mbox_mem_pool);
829	if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
830		!(phba->link_state == LPFC_HBA_ERROR) &&
831		!(phba->pport->load_flag & FC_UNLOADING))
832		mod_timer(&phba->hb_tmofunc,
833			jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
834	return;
835}
836
837/**
838 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
839 * @phba: pointer to lpfc hba data structure.
840 *
841 * This is the actual HBA-timer timeout handler to be invoked by the worker
842 * thread whenever the HBA timer fired and HBA-timeout event posted. This
843 * handler performs any periodic operations needed for the device. If such
844 * periodic event has already been attended to either in the interrupt handler
845 * or by processing slow-ring or fast-ring events within the HBA-timer
846 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
847 * the timer for the next timeout period. If lpfc heart-beat mailbox command
848 * is configured and there is no heart-beat mailbox command outstanding, a
849 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
850 * has been a heart-beat mailbox command outstanding, the HBA shall be put
851 * to offline.
852 **/
853void
854lpfc_hb_timeout_handler(struct lpfc_hba *phba)
855{
856	struct lpfc_vport **vports;
857	LPFC_MBOXQ_t *pmboxq;
858	struct lpfc_dmabuf *buf_ptr;
859	int retval, i;
860	struct lpfc_sli *psli = &phba->sli;
861	LIST_HEAD(completions);
862
863	vports = lpfc_create_vport_work_array(phba);
864	if (vports != NULL)
865		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
866			lpfc_rcv_seq_check_edtov(vports[i]);
867	lpfc_destroy_vport_work_array(phba, vports);
868
869	if ((phba->link_state == LPFC_HBA_ERROR) ||
870		(phba->pport->load_flag & FC_UNLOADING) ||
871		(phba->pport->fc_flag & FC_OFFLINE_MODE))
872		return;
873
874	spin_lock_irq(&phba->pport->work_port_lock);
875
876	if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
877		jiffies)) {
878		spin_unlock_irq(&phba->pport->work_port_lock);
879		if (!phba->hb_outstanding)
880			mod_timer(&phba->hb_tmofunc,
881				jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
882		else
883			mod_timer(&phba->hb_tmofunc,
884				jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
885		return;
886	}
887	spin_unlock_irq(&phba->pport->work_port_lock);
888
889	if (phba->elsbuf_cnt &&
890		(phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
891		spin_lock_irq(&phba->hbalock);
892		list_splice_init(&phba->elsbuf, &completions);
893		phba->elsbuf_cnt = 0;
894		phba->elsbuf_prev_cnt = 0;
895		spin_unlock_irq(&phba->hbalock);
896
897		while (!list_empty(&completions)) {
898			list_remove_head(&completions, buf_ptr,
899				struct lpfc_dmabuf, list);
900			lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
901			kfree(buf_ptr);
902		}
903	}
904	phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
905
906	/* If there is no heart beat outstanding, issue a heartbeat command */
907	if (phba->cfg_enable_hba_heartbeat) {
908		if (!phba->hb_outstanding) {
909			pmboxq = mempool_alloc(phba->mbox_mem_pool,GFP_KERNEL);
910			if (!pmboxq) {
911				mod_timer(&phba->hb_tmofunc,
912					  jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
913				return;
914			}
915
916			lpfc_heart_beat(phba, pmboxq);
917			pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
918			pmboxq->vport = phba->pport;
919			retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
920
921			if (retval != MBX_BUSY && retval != MBX_SUCCESS) {
922				mempool_free(pmboxq, phba->mbox_mem_pool);
923				mod_timer(&phba->hb_tmofunc,
924					  jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
925				return;
926			}
927			mod_timer(&phba->hb_tmofunc,
928				  jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
929			phba->hb_outstanding = 1;
930			return;
931		} else {
932			/*
933			* If heart beat timeout called with hb_outstanding set
934			* we need to take the HBA offline.
935			*/
936			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
937					"0459 Adapter heartbeat failure, "
938					"taking this port offline.\n");
939
940			spin_lock_irq(&phba->hbalock);
941			psli->sli_flag &= ~LPFC_SLI_ACTIVE;
942			spin_unlock_irq(&phba->hbalock);
943
944			lpfc_offline_prep(phba);
945			lpfc_offline(phba);
946			lpfc_unblock_mgmt_io(phba);
947			phba->link_state = LPFC_HBA_ERROR;
948			lpfc_hba_down_post(phba);
949		}
950	}
951}
952
953/**
954 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
955 * @phba: pointer to lpfc hba data structure.
956 *
957 * This routine is called to bring the HBA offline when HBA hardware error
958 * other than Port Error 6 has been detected.
959 **/
960static void
961lpfc_offline_eratt(struct lpfc_hba *phba)
962{
963	struct lpfc_sli   *psli = &phba->sli;
964
965	spin_lock_irq(&phba->hbalock);
966	psli->sli_flag &= ~LPFC_SLI_ACTIVE;
967	spin_unlock_irq(&phba->hbalock);
968	lpfc_offline_prep(phba);
969
970	lpfc_offline(phba);
971	lpfc_reset_barrier(phba);
972	spin_lock_irq(&phba->hbalock);
973	lpfc_sli_brdreset(phba);
974	spin_unlock_irq(&phba->hbalock);
975	lpfc_hba_down_post(phba);
976	lpfc_sli_brdready(phba, HS_MBRDY);
977	lpfc_unblock_mgmt_io(phba);
978	phba->link_state = LPFC_HBA_ERROR;
979	return;
980}
981
982/**
983 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
984 * @phba: pointer to lpfc hba data structure.
985 *
986 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
987 * other than Port Error 6 has been detected.
988 **/
989static void
990lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
991{
992	lpfc_offline_prep(phba);
993	lpfc_offline(phba);
994	lpfc_sli4_brdreset(phba);
995	lpfc_hba_down_post(phba);
996	lpfc_sli4_post_status_check(phba);
997	lpfc_unblock_mgmt_io(phba);
998	phba->link_state = LPFC_HBA_ERROR;
999}
1000
1001/**
1002 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1003 * @phba: pointer to lpfc hba data structure.
1004 *
1005 * This routine is invoked to handle the deferred HBA hardware error
1006 * conditions. This type of error is indicated by HBA by setting ER1
1007 * and another ER bit in the host status register. The driver will
1008 * wait until the ER1 bit clears before handling the error condition.
1009 **/
1010static void
1011lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1012{
1013	uint32_t old_host_status = phba->work_hs;
1014	struct lpfc_sli_ring  *pring;
1015	struct lpfc_sli *psli = &phba->sli;
1016
1017	/* If the pci channel is offline, ignore possible errors,
1018	 * since we cannot communicate with the pci card anyway.
1019	 */
1020	if (pci_channel_offline(phba->pcidev)) {
1021		spin_lock_irq(&phba->hbalock);
1022		phba->hba_flag &= ~DEFER_ERATT;
1023		spin_unlock_irq(&phba->hbalock);
1024		return;
1025	}
1026
1027	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1028		"0479 Deferred Adapter Hardware Error "
1029		"Data: x%x x%x x%x\n",
1030		phba->work_hs,
1031		phba->work_status[0], phba->work_status[1]);
1032
1033	spin_lock_irq(&phba->hbalock);
1034	psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1035	spin_unlock_irq(&phba->hbalock);
1036
1037
1038	/*
1039	 * Firmware stops when it triggred erratt. That could cause the I/Os
1040	 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1041	 * SCSI layer retry it after re-establishing link.
1042	 */
1043	pring = &psli->ring[psli->fcp_ring];
1044	lpfc_sli_abort_iocb_ring(phba, pring);
1045
1046	/*
1047	 * There was a firmware error. Take the hba offline and then
1048	 * attempt to restart it.
1049	 */
1050	lpfc_offline_prep(phba);
1051	lpfc_offline(phba);
1052
1053	/* Wait for the ER1 bit to clear.*/
1054	while (phba->work_hs & HS_FFER1) {
1055		msleep(100);
1056		phba->work_hs = readl(phba->HSregaddr);
1057		/* If driver is unloading let the worker thread continue */
1058		if (phba->pport->load_flag & FC_UNLOADING) {
1059			phba->work_hs = 0;
1060			break;
1061		}
1062	}
1063
1064	/*
1065	 * This is to ptrotect against a race condition in which
1066	 * first write to the host attention register clear the
1067	 * host status register.
1068	 */
1069	if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1070		phba->work_hs = old_host_status & ~HS_FFER1;
1071
1072	spin_lock_irq(&phba->hbalock);
1073	phba->hba_flag &= ~DEFER_ERATT;
1074	spin_unlock_irq(&phba->hbalock);
1075	phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1076	phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1077}
1078
1079static void
1080lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1081{
1082	struct lpfc_board_event_header board_event;
1083	struct Scsi_Host *shost;
1084
1085	board_event.event_type = FC_REG_BOARD_EVENT;
1086	board_event.subcategory = LPFC_EVENT_PORTINTERR;
1087	shost = lpfc_shost_from_vport(phba->pport);
1088	fc_host_post_vendor_event(shost, fc_get_event_number(),
1089				  sizeof(board_event),
1090				  (char *) &board_event,
1091				  LPFC_NL_VENDOR_ID);
1092}
1093
1094/**
1095 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
1096 * @phba: pointer to lpfc hba data structure.
1097 *
1098 * This routine is invoked to handle the following HBA hardware error
1099 * conditions:
1100 * 1 - HBA error attention interrupt
1101 * 2 - DMA ring index out of range
1102 * 3 - Mailbox command came back as unknown
1103 **/
1104static void
1105lpfc_handle_eratt_s3(struct lpfc_hba *phba)
1106{
1107	struct lpfc_vport *vport = phba->pport;
1108	struct lpfc_sli   *psli = &phba->sli;
1109	struct lpfc_sli_ring  *pring;
1110	uint32_t event_data;
1111	unsigned long temperature;
1112	struct temp_event temp_event_data;
1113	struct Scsi_Host  *shost;
1114
1115	/* If the pci channel is offline, ignore possible errors,
1116	 * since we cannot communicate with the pci card anyway.
1117	 */
1118	if (pci_channel_offline(phba->pcidev)) {
1119		spin_lock_irq(&phba->hbalock);
1120		phba->hba_flag &= ~DEFER_ERATT;
1121		spin_unlock_irq(&phba->hbalock);
1122		return;
1123	}
1124
1125	/* If resets are disabled then leave the HBA alone and return */
1126	if (!phba->cfg_enable_hba_reset)
1127		return;
1128
1129	/* Send an internal error event to mgmt application */
1130	lpfc_board_errevt_to_mgmt(phba);
1131
1132	if (phba->hba_flag & DEFER_ERATT)
1133		lpfc_handle_deferred_eratt(phba);
1134
1135	if (phba->work_hs & HS_FFER6) {
1136		/* Re-establishing Link */
1137		lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1138				"1301 Re-establishing Link "
1139				"Data: x%x x%x x%x\n",
1140				phba->work_hs,
1141				phba->work_status[0], phba->work_status[1]);
1142
1143		spin_lock_irq(&phba->hbalock);
1144		psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1145		spin_unlock_irq(&phba->hbalock);
1146
1147		/*
1148		* Firmware stops when it triggled erratt with HS_FFER6.
1149		* That could cause the I/Os dropped by the firmware.
1150		* Error iocb (I/O) on txcmplq and let the SCSI layer
1151		* retry it after re-establishing link.
1152		*/
1153		pring = &psli->ring[psli->fcp_ring];
1154		lpfc_sli_abort_iocb_ring(phba, pring);
1155
1156		/*
1157		 * There was a firmware error.  Take the hba offline and then
1158		 * attempt to restart it.
1159		 */
1160		lpfc_offline_prep(phba);
1161		lpfc_offline(phba);
1162		lpfc_sli_brdrestart(phba);
1163		if (lpfc_online(phba) == 0) {	/* Initialize the HBA */
1164			lpfc_unblock_mgmt_io(phba);
1165			return;
1166		}
1167		lpfc_unblock_mgmt_io(phba);
1168	} else if (phba->work_hs & HS_CRIT_TEMP) {
1169		temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1170		temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1171		temp_event_data.event_code = LPFC_CRIT_TEMP;
1172		temp_event_data.data = (uint32_t)temperature;
1173
1174		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1175				"0406 Adapter maximum temperature exceeded "
1176				"(%ld), taking this port offline "
1177				"Data: x%x x%x x%x\n",
1178				temperature, phba->work_hs,
1179				phba->work_status[0], phba->work_status[1]);
1180
1181		shost = lpfc_shost_from_vport(phba->pport);
1182		fc_host_post_vendor_event(shost, fc_get_event_number(),
1183					  sizeof(temp_event_data),
1184					  (char *) &temp_event_data,
1185					  SCSI_NL_VID_TYPE_PCI
1186					  | PCI_VENDOR_ID_EMULEX);
1187
1188		spin_lock_irq(&phba->hbalock);
1189		phba->over_temp_state = HBA_OVER_TEMP;
1190		spin_unlock_irq(&phba->hbalock);
1191		lpfc_offline_eratt(phba);
1192
1193	} else {
1194		/* The if clause above forces this code path when the status
1195		 * failure is a value other than FFER6. Do not call the offline
1196		 * twice. This is the adapter hardware error path.
1197		 */
1198		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1199				"0457 Adapter Hardware Error "
1200				"Data: x%x x%x x%x\n",
1201				phba->work_hs,
1202				phba->work_status[0], phba->work_status[1]);
1203
1204		event_data = FC_REG_DUMP_EVENT;
1205		shost = lpfc_shost_from_vport(vport);
1206		fc_host_post_vendor_event(shost, fc_get_event_number(),
1207				sizeof(event_data), (char *) &event_data,
1208				SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1209
1210		lpfc_offline_eratt(phba);
1211	}
1212	return;
1213}
1214
1215/**
1216 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1217 * @phba: pointer to lpfc hba data structure.
1218 *
1219 * This routine is invoked to handle the SLI4 HBA hardware error attention
1220 * conditions.
1221 **/
1222static void
1223lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1224{
1225	struct lpfc_vport *vport = phba->pport;
1226	uint32_t event_data;
1227	struct Scsi_Host *shost;
1228
1229	/* If the pci channel is offline, ignore possible errors, since
1230	 * we cannot communicate with the pci card anyway.
1231	 */
1232	if (pci_channel_offline(phba->pcidev))
1233		return;
1234	/* If resets are disabled then leave the HBA alone and return */
1235	if (!phba->cfg_enable_hba_reset)
1236		return;
1237
1238	/* Send an internal error event to mgmt application */
1239	lpfc_board_errevt_to_mgmt(phba);
1240
1241	/* For now, the actual action for SLI4 device handling is not
1242	 * specified yet, just treated it as adaptor hardware failure
1243	 */
1244	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1245			"0143 SLI4 Adapter Hardware Error Data: x%x x%x\n",
1246			phba->work_status[0], phba->work_status[1]);
1247
1248	event_data = FC_REG_DUMP_EVENT;
1249	shost = lpfc_shost_from_vport(vport);
1250	fc_host_post_vendor_event(shost, fc_get_event_number(),
1251				  sizeof(event_data), (char *) &event_data,
1252				  SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1253
1254	lpfc_sli4_offline_eratt(phba);
1255}
1256
1257/**
1258 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1259 * @phba: pointer to lpfc HBA data structure.
1260 *
1261 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1262 * routine from the API jump table function pointer from the lpfc_hba struct.
1263 *
1264 * Return codes
1265 *   0 - success.
1266 *   Any other value - error.
1267 **/
1268void
1269lpfc_handle_eratt(struct lpfc_hba *phba)
1270{
1271	(*phba->lpfc_handle_eratt)(phba);
1272}
1273
1274/**
1275 * lpfc_handle_latt - The HBA link event handler
1276 * @phba: pointer to lpfc hba data structure.
1277 *
1278 * This routine is invoked from the worker thread to handle a HBA host
1279 * attention link event.
1280 **/
1281void
1282lpfc_handle_latt(struct lpfc_hba *phba)
1283{
1284	struct lpfc_vport *vport = phba->pport;
1285	struct lpfc_sli   *psli = &phba->sli;
1286	LPFC_MBOXQ_t *pmb;
1287	volatile uint32_t control;
1288	struct lpfc_dmabuf *mp;
1289	int rc = 0;
1290
1291	pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1292	if (!pmb) {
1293		rc = 1;
1294		goto lpfc_handle_latt_err_exit;
1295	}
1296
1297	mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1298	if (!mp) {
1299		rc = 2;
1300		goto lpfc_handle_latt_free_pmb;
1301	}
1302
1303	mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
1304	if (!mp->virt) {
1305		rc = 3;
1306		goto lpfc_handle_latt_free_mp;
1307	}
1308
1309	/* Cleanup any outstanding ELS commands */
1310	lpfc_els_flush_all_cmd(phba);
1311
1312	psli->slistat.link_event++;
1313	lpfc_read_la(phba, pmb, mp);
1314	pmb->mbox_cmpl = lpfc_mbx_cmpl_read_la;
1315	pmb->vport = vport;
1316	/* Block ELS IOCBs until we have processed this mbox command */
1317	phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
1318	rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
1319	if (rc == MBX_NOT_FINISHED) {
1320		rc = 4;
1321		goto lpfc_handle_latt_free_mbuf;
1322	}
1323
1324	/* Clear Link Attention in HA REG */
1325	spin_lock_irq(&phba->hbalock);
1326	writel(HA_LATT, phba->HAregaddr);
1327	readl(phba->HAregaddr); /* flush */
1328	spin_unlock_irq(&phba->hbalock);
1329
1330	return;
1331
1332lpfc_handle_latt_free_mbuf:
1333	phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
1334	lpfc_mbuf_free(phba, mp->virt, mp->phys);
1335lpfc_handle_latt_free_mp:
1336	kfree(mp);
1337lpfc_handle_latt_free_pmb:
1338	mempool_free(pmb, phba->mbox_mem_pool);
1339lpfc_handle_latt_err_exit:
1340	/* Enable Link attention interrupts */
1341	spin_lock_irq(&phba->hbalock);
1342	psli->sli_flag |= LPFC_PROCESS_LA;
1343	control = readl(phba->HCregaddr);
1344	control |= HC_LAINT_ENA;
1345	writel(control, phba->HCregaddr);
1346	readl(phba->HCregaddr); /* flush */
1347
1348	/* Clear Link Attention in HA REG */
1349	writel(HA_LATT, phba->HAregaddr);
1350	readl(phba->HAregaddr); /* flush */
1351	spin_unlock_irq(&phba->hbalock);
1352	lpfc_linkdown(phba);
1353	phba->link_state = LPFC_HBA_ERROR;
1354
1355	lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1356		     "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
1357
1358	return;
1359}
1360
1361/**
1362 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
1363 * @phba: pointer to lpfc hba data structure.
1364 * @vpd: pointer to the vital product data.
1365 * @len: length of the vital product data in bytes.
1366 *
1367 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1368 * an array of characters. In this routine, the ModelName, ProgramType, and
1369 * ModelDesc, etc. fields of the phba data structure will be populated.
1370 *
1371 * Return codes
1372 *   0 - pointer to the VPD passed in is NULL
1373 *   1 - success
1374 **/
1375int
1376lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
1377{
1378	uint8_t lenlo, lenhi;
1379	int Length;
1380	int i, j;
1381	int finished = 0;
1382	int index = 0;
1383
1384	if (!vpd)
1385		return 0;
1386
1387	/* Vital Product */
1388	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1389			"0455 Vital Product Data: x%x x%x x%x x%x\n",
1390			(uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1391			(uint32_t) vpd[3]);
1392	while (!finished && (index < (len - 4))) {
1393		switch (vpd[index]) {
1394		case 0x82:
1395		case 0x91:
1396			index += 1;
1397			lenlo = vpd[index];
1398			index += 1;
1399			lenhi = vpd[index];
1400			index += 1;
1401			i = ((((unsigned short)lenhi) << 8) + lenlo);
1402			index += i;
1403			break;
1404		case 0x90:
1405			index += 1;
1406			lenlo = vpd[index];
1407			index += 1;
1408			lenhi = vpd[index];
1409			index += 1;
1410			Length = ((((unsigned short)lenhi) << 8) + lenlo);
1411			if (Length > len - index)
1412				Length = len - index;
1413			while (Length > 0) {
1414			/* Look for Serial Number */
1415			if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1416				index += 2;
1417				i = vpd[index];
1418				index += 1;
1419				j = 0;
1420				Length -= (3+i);
1421				while(i--) {
1422					phba->SerialNumber[j++] = vpd[index++];
1423					if (j == 31)
1424						break;
1425				}
1426				phba->SerialNumber[j] = 0;
1427				continue;
1428			}
1429			else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1430				phba->vpd_flag |= VPD_MODEL_DESC;
1431				index += 2;
1432				i = vpd[index];
1433				index += 1;
1434				j = 0;
1435				Length -= (3+i);
1436				while(i--) {
1437					phba->ModelDesc[j++] = vpd[index++];
1438					if (j == 255)
1439						break;
1440				}
1441				phba->ModelDesc[j] = 0;
1442				continue;
1443			}
1444			else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1445				phba->vpd_flag |= VPD_MODEL_NAME;
1446				index += 2;
1447				i = vpd[index];
1448				index += 1;
1449				j = 0;
1450				Length -= (3+i);
1451				while(i--) {
1452					phba->ModelName[j++] = vpd[index++];
1453					if (j == 79)
1454						break;
1455				}
1456				phba->ModelName[j] = 0;
1457				continue;
1458			}
1459			else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1460				phba->vpd_flag |= VPD_PROGRAM_TYPE;
1461				index += 2;
1462				i = vpd[index];
1463				index += 1;
1464				j = 0;
1465				Length -= (3+i);
1466				while(i--) {
1467					phba->ProgramType[j++] = vpd[index++];
1468					if (j == 255)
1469						break;
1470				}
1471				phba->ProgramType[j] = 0;
1472				continue;
1473			}
1474			else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1475				phba->vpd_flag |= VPD_PORT;
1476				index += 2;
1477				i = vpd[index];
1478				index += 1;
1479				j = 0;
1480				Length -= (3+i);
1481				while(i--) {
1482				phba->Port[j++] = vpd[index++];
1483				if (j == 19)
1484					break;
1485				}
1486				phba->Port[j] = 0;
1487				continue;
1488			}
1489			else {
1490				index += 2;
1491				i = vpd[index];
1492				index += 1;
1493				index += i;
1494				Length -= (3 + i);
1495			}
1496		}
1497		finished = 0;
1498		break;
1499		case 0x78:
1500			finished = 1;
1501			break;
1502		default:
1503			index ++;
1504			break;
1505		}
1506	}
1507
1508	return(1);
1509}
1510
1511/**
1512 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
1513 * @phba: pointer to lpfc hba data structure.
1514 * @mdp: pointer to the data structure to hold the derived model name.
1515 * @descp: pointer to the data structure to hold the derived description.
1516 *
1517 * This routine retrieves HBA's description based on its registered PCI device
1518 * ID. The @descp passed into this function points to an array of 256 chars. It
1519 * shall be returned with the model name, maximum speed, and the host bus type.
1520 * The @mdp passed into this function points to an array of 80 chars. When the
1521 * function returns, the @mdp will be filled with the model name.
1522 **/
1523static void
1524lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
1525{
1526	lpfc_vpd_t *vp;
1527	uint16_t dev_id = phba->pcidev->device;
1528	int max_speed;
1529	int GE = 0;
1530	int oneConnect = 0; /* default is not a oneConnect */
1531	struct {
1532		char *name;
1533		char *bus;
1534		char *function;
1535	} m = {"<Unknown>", "", ""};
1536
1537	if (mdp && mdp[0] != '\0'
1538		&& descp && descp[0] != '\0')
1539		return;
1540
1541	if (phba->lmt & LMT_10Gb)
1542		max_speed = 10;
1543	else if (phba->lmt & LMT_8Gb)
1544		max_speed = 8;
1545	else if (phba->lmt & LMT_4Gb)
1546		max_speed = 4;
1547	else if (phba->lmt & LMT_2Gb)
1548		max_speed = 2;
1549	else
1550		max_speed = 1;
1551
1552	vp = &phba->vpd;
1553
1554	switch (dev_id) {
1555	case PCI_DEVICE_ID_FIREFLY:
1556		m = (typeof(m)){"LP6000", "PCI", "Fibre Channel Adapter"};
1557		break;
1558	case PCI_DEVICE_ID_SUPERFLY:
1559		if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
1560			m = (typeof(m)){"LP7000", "PCI",
1561					"Fibre Channel Adapter"};
1562		else
1563			m = (typeof(m)){"LP7000E", "PCI",
1564					"Fibre Channel Adapter"};
1565		break;
1566	case PCI_DEVICE_ID_DRAGONFLY:
1567		m = (typeof(m)){"LP8000", "PCI",
1568				"Fibre Channel Adapter"};
1569		break;
1570	case PCI_DEVICE_ID_CENTAUR:
1571		if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
1572			m = (typeof(m)){"LP9002", "PCI",
1573					"Fibre Channel Adapter"};
1574		else
1575			m = (typeof(m)){"LP9000", "PCI",
1576					"Fibre Channel Adapter"};
1577		break;
1578	case PCI_DEVICE_ID_RFLY:
1579		m = (typeof(m)){"LP952", "PCI",
1580				"Fibre Channel Adapter"};
1581		break;
1582	case PCI_DEVICE_ID_PEGASUS:
1583		m = (typeof(m)){"LP9802", "PCI-X",
1584				"Fibre Channel Adapter"};
1585		break;
1586	case PCI_DEVICE_ID_THOR:
1587		m = (typeof(m)){"LP10000", "PCI-X",
1588				"Fibre Channel Adapter"};
1589		break;
1590	case PCI_DEVICE_ID_VIPER:
1591		m = (typeof(m)){"LPX1000",  "PCI-X",
1592				"Fibre Channel Adapter"};
1593		break;
1594	case PCI_DEVICE_ID_PFLY:
1595		m = (typeof(m)){"LP982", "PCI-X",
1596				"Fibre Channel Adapter"};
1597		break;
1598	case PCI_DEVICE_ID_TFLY:
1599		m = (typeof(m)){"LP1050", "PCI-X",
1600				"Fibre Channel Adapter"};
1601		break;
1602	case PCI_DEVICE_ID_HELIOS:
1603		m = (typeof(m)){"LP11000", "PCI-X2",
1604				"Fibre Channel Adapter"};
1605		break;
1606	case PCI_DEVICE_ID_HELIOS_SCSP:
1607		m = (typeof(m)){"LP11000-SP", "PCI-X2",
1608				"Fibre Channel Adapter"};
1609		break;
1610	case PCI_DEVICE_ID_HELIOS_DCSP:
1611		m = (typeof(m)){"LP11002-SP",  "PCI-X2",
1612				"Fibre Channel Adapter"};
1613		break;
1614	case PCI_DEVICE_ID_NEPTUNE:
1615		m = (typeof(m)){"LPe1000", "PCIe", "Fibre Channel Adapter"};
1616		break;
1617	case PCI_DEVICE_ID_NEPTUNE_SCSP:
1618		m = (typeof(m)){"LPe1000-SP", "PCIe", "Fibre Channel Adapter"};
1619		break;
1620	case PCI_DEVICE_ID_NEPTUNE_DCSP:
1621		m = (typeof(m)){"LPe1002-SP", "PCIe", "Fibre Channel Adapter"};
1622		break;
1623	case PCI_DEVICE_ID_BMID:
1624		m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
1625		break;
1626	case PCI_DEVICE_ID_BSMB:
1627		m = (typeof(m)){"LP111", "PCI-X2", "Fibre Channel Adapter"};
1628		break;
1629	case PCI_DEVICE_ID_ZEPHYR:
1630		m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
1631		break;
1632	case PCI_DEVICE_ID_ZEPHYR_SCSP:
1633		m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
1634		break;
1635	case PCI_DEVICE_ID_ZEPHYR_DCSP:
1636		m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
1637		GE = 1;
1638		break;
1639	case PCI_DEVICE_ID_ZMID:
1640		m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
1641		break;
1642	case PCI_DEVICE_ID_ZSMB:
1643		m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
1644		break;
1645	case PCI_DEVICE_ID_LP101:
1646		m = (typeof(m)){"LP101", "PCI-X", "Fibre Channel Adapter"};
1647		break;
1648	case PCI_DEVICE_ID_LP10000S:
1649		m = (typeof(m)){"LP10000-S", "PCI", "Fibre Channel Adapter"};
1650		break;
1651	case PCI_DEVICE_ID_LP11000S:
1652		m = (typeof(m)){"LP11000-S", "PCI-X2", "Fibre Channel Adapter"};
1653		break;
1654	case PCI_DEVICE_ID_LPE11000S:
1655		m = (typeof(m)){"LPe11000-S", "PCIe", "Fibre Channel Adapter"};
1656		break;
1657	case PCI_DEVICE_ID_SAT:
1658		m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
1659		break;
1660	case PCI_DEVICE_ID_SAT_MID:
1661		m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
1662		break;
1663	case PCI_DEVICE_ID_SAT_SMB:
1664		m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
1665		break;
1666	case PCI_DEVICE_ID_SAT_DCSP:
1667		m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
1668		break;
1669	case PCI_DEVICE_ID_SAT_SCSP:
1670		m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
1671		break;
1672	case PCI_DEVICE_ID_SAT_S:
1673		m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
1674		break;
1675	case PCI_DEVICE_ID_HORNET:
1676		m = (typeof(m)){"LP21000", "PCIe", "FCoE Adapter"};
1677		GE = 1;
1678		break;
1679	case PCI_DEVICE_ID_PROTEUS_VF:
1680		m = (typeof(m)){"LPev12000", "PCIe IOV",
1681				"Fibre Channel Adapter"};
1682		break;
1683	case PCI_DEVICE_ID_PROTEUS_PF:
1684		m = (typeof(m)){"LPev12000", "PCIe IOV",
1685				"Fibre Channel Adapter"};
1686		break;
1687	case PCI_DEVICE_ID_PROTEUS_S:
1688		m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
1689				"Fibre Channel Adapter"};
1690		break;
1691	case PCI_DEVICE_ID_TIGERSHARK:
1692		oneConnect = 1;
1693		m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
1694		break;
1695	case PCI_DEVICE_ID_TOMCAT:
1696		oneConnect = 1;
1697		m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
1698		break;
1699	case PCI_DEVICE_ID_FALCON:
1700		m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
1701				"EmulexSecure Fibre"};
1702		break;
1703	default:
1704		m = (typeof(m)){"Unknown", "", ""};
1705		break;
1706	}
1707
1708	if (mdp && mdp[0] == '\0')
1709		snprintf(mdp, 79,"%s", m.name);
1710	/* oneConnect hba requires special processing, they are all initiators
1711	 * and we put the port number on the end
1712	 */
1713	if (descp && descp[0] == '\0') {
1714		if (oneConnect)
1715			snprintf(descp, 255,
1716				"Emulex OneConnect %s, %s Initiator, Port %s",
1717				m.name, m.function,
1718				phba->Port);
1719		else
1720			snprintf(descp, 255,
1721				"Emulex %s %d%s %s %s",
1722				m.name, max_speed, (GE) ? "GE" : "Gb",
1723				m.bus, m.function);
1724	}
1725}
1726
1727/**
1728 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
1729 * @phba: pointer to lpfc hba data structure.
1730 * @pring: pointer to a IOCB ring.
1731 * @cnt: the number of IOCBs to be posted to the IOCB ring.
1732 *
1733 * This routine posts a given number of IOCBs with the associated DMA buffer
1734 * descriptors specified by the cnt argument to the given IOCB ring.
1735 *
1736 * Return codes
1737 *   The number of IOCBs NOT able to be posted to the IOCB ring.
1738 **/
1739int
1740lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
1741{
1742	IOCB_t *icmd;
1743	struct lpfc_iocbq *iocb;
1744	struct lpfc_dmabuf *mp1, *mp2;
1745
1746	cnt += pring->missbufcnt;
1747
1748	/* While there are buffers to post */
1749	while (cnt > 0) {
1750		/* Allocate buffer for  command iocb */
1751		iocb = lpfc_sli_get_iocbq(phba);
1752		if (iocb == NULL) {
1753			pring->missbufcnt = cnt;
1754			return cnt;
1755		}
1756		icmd = &iocb->iocb;
1757
1758		/* 2 buffers can be posted per command */
1759		/* Allocate buffer to post */
1760		mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1761		if (mp1)
1762		    mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
1763		if (!mp1 || !mp1->virt) {
1764			kfree(mp1);
1765			lpfc_sli_release_iocbq(phba, iocb);
1766			pring->missbufcnt = cnt;
1767			return cnt;
1768		}
1769
1770		INIT_LIST_HEAD(&mp1->list);
1771		/* Allocate buffer to post */
1772		if (cnt > 1) {
1773			mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1774			if (mp2)
1775				mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
1776							    &mp2->phys);
1777			if (!mp2 || !mp2->virt) {
1778				kfree(mp2);
1779				lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1780				kfree(mp1);
1781				lpfc_sli_release_iocbq(phba, iocb);
1782				pring->missbufcnt = cnt;
1783				return cnt;
1784			}
1785
1786			INIT_LIST_HEAD(&mp2->list);
1787		} else {
1788			mp2 = NULL;
1789		}
1790
1791		icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
1792		icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
1793		icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
1794		icmd->ulpBdeCount = 1;
1795		cnt--;
1796		if (mp2) {
1797			icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
1798			icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
1799			icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
1800			cnt--;
1801			icmd->ulpBdeCount = 2;
1802		}
1803
1804		icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
1805		icmd->ulpLe = 1;
1806
1807		if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
1808		    IOCB_ERROR) {
1809			lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1810			kfree(mp1);
1811			cnt++;
1812			if (mp2) {
1813				lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
1814				kfree(mp2);
1815				cnt++;
1816			}
1817			lpfc_sli_release_iocbq(phba, iocb);
1818			pring->missbufcnt = cnt;
1819			return cnt;
1820		}
1821		lpfc_sli_ringpostbuf_put(phba, pring, mp1);
1822		if (mp2)
1823			lpfc_sli_ringpostbuf_put(phba, pring, mp2);
1824	}
1825	pring->missbufcnt = 0;
1826	return 0;
1827}
1828
1829/**
1830 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
1831 * @phba: pointer to lpfc hba data structure.
1832 *
1833 * This routine posts initial receive IOCB buffers to the ELS ring. The
1834 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
1835 * set to 64 IOCBs.
1836 *
1837 * Return codes
1838 *   0 - success (currently always success)
1839 **/
1840static int
1841lpfc_post_rcv_buf(struct lpfc_hba *phba)
1842{
1843	struct lpfc_sli *psli = &phba->sli;
1844
1845	/* Ring 0, ELS / CT buffers */
1846	lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
1847	/* Ring 2 - FCP no buffers needed */
1848
1849	return 0;
1850}
1851
1852#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
1853
1854/**
1855 * lpfc_sha_init - Set up initial array of hash table entries
1856 * @HashResultPointer: pointer to an array as hash table.
1857 *
1858 * This routine sets up the initial values to the array of hash table entries
1859 * for the LC HBAs.
1860 **/
1861static void
1862lpfc_sha_init(uint32_t * HashResultPointer)
1863{
1864	HashResultPointer[0] = 0x67452301;
1865	HashResultPointer[1] = 0xEFCDAB89;
1866	HashResultPointer[2] = 0x98BADCFE;
1867	HashResultPointer[3] = 0x10325476;
1868	HashResultPointer[4] = 0xC3D2E1F0;
1869}
1870
1871/**
1872 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
1873 * @HashResultPointer: pointer to an initial/result hash table.
1874 * @HashWorkingPointer: pointer to an working hash table.
1875 *
1876 * This routine iterates an initial hash table pointed by @HashResultPointer
1877 * with the values from the working hash table pointeed by @HashWorkingPointer.
1878 * The results are putting back to the initial hash table, returned through
1879 * the @HashResultPointer as the result hash table.
1880 **/
1881static void
1882lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
1883{
1884	int t;
1885	uint32_t TEMP;
1886	uint32_t A, B, C, D, E;
1887	t = 16;
1888	do {
1889		HashWorkingPointer[t] =
1890		    S(1,
1891		      HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
1892								     8] ^
1893		      HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
1894	} while (++t <= 79);
1895	t = 0;
1896	A = HashResultPointer[0];
1897	B = HashResultPointer[1];
1898	C = HashResultPointer[2];
1899	D = HashResultPointer[3];
1900	E = HashResultPointer[4];
1901
1902	do {
1903		if (t < 20) {
1904			TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
1905		} else if (t < 40) {
1906			TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
1907		} else if (t < 60) {
1908			TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
1909		} else {
1910			TEMP = (B ^ C ^ D) + 0xCA62C1D6;
1911		}
1912		TEMP += S(5, A) + E + HashWorkingPointer[t];
1913		E = D;
1914		D = C;
1915		C = S(30, B);
1916		B = A;
1917		A = TEMP;
1918	} while (++t <= 79);
1919
1920	HashResultPointer[0] += A;
1921	HashResultPointer[1] += B;
1922	HashResultPointer[2] += C;
1923	HashResultPointer[3] += D;
1924	HashResultPointer[4] += E;
1925
1926}
1927
1928/**
1929 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
1930 * @RandomChallenge: pointer to the entry of host challenge random number array.
1931 * @HashWorking: pointer to the entry of the working hash array.
1932 *
1933 * This routine calculates the working hash array referred by @HashWorking
1934 * from the challenge random numbers associated with the host, referred by
1935 * @RandomChallenge. The result is put into the entry of the working hash
1936 * array and returned by reference through @HashWorking.
1937 **/
1938static void
1939lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
1940{
1941	*HashWorking = (*RandomChallenge ^ *HashWorking);
1942}
1943
1944/**
1945 * lpfc_hba_init - Perform special handling for LC HBA initialization
1946 * @phba: pointer to lpfc hba data structure.
1947 * @hbainit: pointer to an array of unsigned 32-bit integers.
1948 *
1949 * This routine performs the special handling for LC HBA initialization.
1950 **/
1951void
1952lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
1953{
1954	int t;
1955	uint32_t *HashWorking;
1956	uint32_t *pwwnn = (uint32_t *) phba->wwnn;
1957
1958	HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
1959	if (!HashWorking)
1960		return;
1961
1962	HashWorking[0] = HashWorking[78] = *pwwnn++;
1963	HashWorking[1] = HashWorking[79] = *pwwnn;
1964
1965	for (t = 0; t < 7; t++)
1966		lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
1967
1968	lpfc_sha_init(hbainit);
1969	lpfc_sha_iterate(hbainit, HashWorking);
1970	kfree(HashWorking);
1971}
1972
1973/**
1974 * lpfc_cleanup - Performs vport cleanups before deleting a vport
1975 * @vport: pointer to a virtual N_Port data structure.
1976 *
1977 * This routine performs the necessary cleanups before deleting the @vport.
1978 * It invokes the discovery state machine to perform necessary state
1979 * transitions and to release the ndlps associated with the @vport. Note,
1980 * the physical port is treated as @vport 0.
1981 **/
1982void
1983lpfc_cleanup(struct lpfc_vport *vport)
1984{
1985	struct lpfc_hba   *phba = vport->phba;
1986	struct lpfc_nodelist *ndlp, *next_ndlp;
1987	int i = 0;
1988
1989	if (phba->link_state > LPFC_LINK_DOWN)
1990		lpfc_port_link_failure(vport);
1991
1992	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
1993		if (!NLP_CHK_NODE_ACT(ndlp)) {
1994			ndlp = lpfc_enable_node(vport, ndlp,
1995						NLP_STE_UNUSED_NODE);
1996			if (!ndlp)
1997				continue;
1998			spin_lock_irq(&phba->ndlp_lock);
1999			NLP_SET_FREE_REQ(ndlp);
2000			spin_unlock_irq(&phba->ndlp_lock);
2001			/* Trigger the release of the ndlp memory */
2002			lpfc_nlp_put(ndlp);
2003			continue;
2004		}
2005		spin_lock_irq(&phba->ndlp_lock);
2006		if (NLP_CHK_FREE_REQ(ndlp)) {
2007			/* The ndlp should not be in memory free mode already */
2008			spin_unlock_irq(&phba->ndlp_lock);
2009			continue;
2010		} else
2011			/* Indicate request for freeing ndlp memory */
2012			NLP_SET_FREE_REQ(ndlp);
2013		spin_unlock_irq(&phba->ndlp_lock);
2014
2015		if (vport->port_type != LPFC_PHYSICAL_PORT &&
2016		    ndlp->nlp_DID == Fabric_DID) {
2017			/* Just free up ndlp with Fabric_DID for vports */
2018			lpfc_nlp_put(ndlp);
2019			continue;
2020		}
2021
2022		if (ndlp->nlp_type & NLP_FABRIC)
2023			lpfc_disc_state_machine(vport, ndlp, NULL,
2024					NLP_EVT_DEVICE_RECOVERY);
2025
2026		lpfc_disc_state_machine(vport, ndlp, NULL,
2027					     NLP_EVT_DEVICE_RM);
2028
2029	}
2030
2031	/* At this point, ALL ndlp's should be gone
2032	 * because of the previous NLP_EVT_DEVICE_RM.
2033	 * Lets wait for this to happen, if needed.
2034	 */
2035	while (!list_empty(&vport->fc_nodes)) {
2036		if (i++ > 3000) {
2037			lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2038				"0233 Nodelist not empty\n");
2039			list_for_each_entry_safe(ndlp, next_ndlp,
2040						&vport->fc_nodes, nlp_listp) {
2041				lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2042						LOG_NODE,
2043						"0282 did:x%x ndlp:x%p "
2044						"usgmap:x%x refcnt:%d\n",
2045						ndlp->nlp_DID, (void *)ndlp,
2046						ndlp->nlp_usg_map,
2047						atomic_read(
2048							&ndlp->kref.refcount));
2049			}
2050			break;
2051		}
2052
2053		/* Wait for any activity on ndlps to settle */
2054		msleep(10);
2055	}
2056}
2057
2058/**
2059 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
2060 * @vport: pointer to a virtual N_Port data structure.
2061 *
2062 * This routine stops all the timers associated with a @vport. This function
2063 * is invoked before disabling or deleting a @vport. Note that the physical
2064 * port is treated as @vport 0.
2065 **/
2066void
2067lpfc_stop_vport_timers(struct lpfc_vport *vport)
2068{
2069	del_timer_sync(&vport->els_tmofunc);
2070	del_timer_sync(&vport->fc_fdmitmo);
2071	lpfc_can_disctmo(vport);
2072	return;
2073}
2074
2075/**
2076 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
2077 * @phba: pointer to lpfc hba data structure.
2078 *
2079 * This routine stops all the timers associated with a HBA. This function is
2080 * invoked before either putting a HBA offline or unloading the driver.
2081 **/
2082void
2083lpfc_stop_hba_timers(struct lpfc_hba *phba)
2084{
2085	lpfc_stop_vport_timers(phba->pport);
2086	del_timer_sync(&phba->sli.mbox_tmo);
2087	del_timer_sync(&phba->fabric_block_timer);
2088	del_timer_sync(&phba->eratt_poll);
2089	del_timer_sync(&phba->hb_tmofunc);
2090	phba->hb_outstanding = 0;
2091
2092	switch (phba->pci_dev_grp) {
2093	case LPFC_PCI_DEV_LP:
2094		/* Stop any LightPulse device specific driver timers */
2095		del_timer_sync(&phba->fcp_poll_timer);
2096		break;
2097	case LPFC_PCI_DEV_OC:
2098		/* Stop any OneConnect device sepcific driver timers */
2099		break;
2100	default:
2101		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2102				"0297 Invalid device group (x%x)\n",
2103				phba->pci_dev_grp);
2104		break;
2105	}
2106	return;
2107}
2108
2109/**
2110 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
2111 * @phba: pointer to lpfc hba data structure.
2112 *
2113 * This routine marks a HBA's management interface as blocked. Once the HBA's
2114 * management interface is marked as blocked, all the user space access to
2115 * the HBA, whether they are from sysfs interface or libdfc interface will
2116 * all be blocked. The HBA is set to block the management interface when the
2117 * driver prepares the HBA interface for online or offline.
2118 **/
2119static void
2120lpfc_block_mgmt_io(struct lpfc_hba * phba)
2121{
2122	unsigned long iflag;
2123
2124	spin_lock_irqsave(&phba->hbalock, iflag);
2125	phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
2126	spin_unlock_irqrestore(&phba->hbalock, iflag);
2127}
2128
2129/**
2130 * lpfc_online - Initialize and bring a HBA online
2131 * @phba: pointer to lpfc hba data structure.
2132 *
2133 * This routine initializes the HBA and brings a HBA online. During this
2134 * process, the management interface is blocked to prevent user space access
2135 * to the HBA interfering with the driver initialization.
2136 *
2137 * Return codes
2138 *   0 - successful
2139 *   1 - failed
2140 **/
2141int
2142lpfc_online(struct lpfc_hba *phba)
2143{
2144	struct lpfc_vport *vport;
2145	struct lpfc_vport **vports;
2146	int i;
2147
2148	if (!phba)
2149		return 0;
2150	vport = phba->pport;
2151
2152	if (!(vport->fc_flag & FC_OFFLINE_MODE))
2153		return 0;
2154
2155	lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2156			"0458 Bring Adapter online\n");
2157
2158	lpfc_block_mgmt_io(phba);
2159
2160	if (!lpfc_sli_queue_setup(phba)) {
2161		lpfc_unblock_mgmt_io(phba);
2162		return 1;
2163	}
2164
2165	if (phba->sli_rev == LPFC_SLI_REV4) {
2166		if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2167			lpfc_unblock_mgmt_io(phba);
2168			return 1;
2169		}
2170	} else {
2171		if (lpfc_sli_hba_setup(phba)) {	/* Initialize SLI2/SLI3 HBA */
2172			lpfc_unblock_mgmt_io(phba);
2173			return 1;
2174		}
2175	}
2176
2177	vports = lpfc_create_vport_work_array(phba);
2178	if (vports != NULL)
2179		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2180			struct Scsi_Host *shost;
2181			shost = lpfc_shost_from_vport(vports[i]);
2182			spin_lock_irq(shost->host_lock);
2183			vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2184			if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2185				vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2186			if (phba->sli_rev == LPFC_SLI_REV4)
2187				vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
2188			spin_unlock_irq(shost->host_lock);
2189		}
2190		lpfc_destroy_vport_work_array(phba, vports);
2191
2192	lpfc_unblock_mgmt_io(phba);
2193	return 0;
2194}
2195
2196/**
2197 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
2198 * @phba: pointer to lpfc hba data structure.
2199 *
2200 * This routine marks a HBA's management interface as not blocked. Once the
2201 * HBA's management interface is marked as not blocked, all the user space
2202 * access to the HBA, whether they are from sysfs interface or libdfc
2203 * interface will be allowed. The HBA is set to block the management interface
2204 * when the driver prepares the HBA interface for online or offline and then
2205 * set to unblock the management interface afterwards.
2206 **/
2207void
2208lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2209{
2210	unsigned long iflag;
2211
2212	spin_lock_irqsave(&phba->hbalock, iflag);
2213	phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2214	spin_unlock_irqrestore(&phba->hbalock, iflag);
2215}
2216
2217/**
2218 * lpfc_offline_prep - Prepare a HBA to be brought offline
2219 * @phba: pointer to lpfc hba data structure.
2220 *
2221 * This routine is invoked to prepare a HBA to be brought offline. It performs
2222 * unregistration login to all the nodes on all vports and flushes the mailbox
2223 * queue to make it ready to be brought offline.
2224 **/
2225void
2226lpfc_offline_prep(struct lpfc_hba * phba)
2227{
2228	struct lpfc_vport *vport = phba->pport;
2229	struct lpfc_nodelist  *ndlp, *next_ndlp;
2230	struct lpfc_vport **vports;
2231	int i;
2232
2233	if (vport->fc_flag & FC_OFFLINE_MODE)
2234		return;
2235
2236	lpfc_block_mgmt_io(phba);
2237
2238	lpfc_linkdown(phba);
2239
2240	/* Issue an unreg_login to all nodes on all vports */
2241	vports = lpfc_create_vport_work_array(phba);
2242	if (vports != NULL) {
2243		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2244			struct Scsi_Host *shost;
2245
2246			if (vports[i]->load_flag & FC_UNLOADING)
2247				continue;
2248			vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
2249			shost =	lpfc_shost_from_vport(vports[i]);
2250			list_for_each_entry_safe(ndlp, next_ndlp,
2251						 &vports[i]->fc_nodes,
2252						 nlp_listp) {
2253				if (!NLP_CHK_NODE_ACT(ndlp))
2254					continue;
2255				if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2256					continue;
2257				if (ndlp->nlp_type & NLP_FABRIC) {
2258					lpfc_disc_state_machine(vports[i], ndlp,
2259						NULL, NLP_EVT_DEVICE_RECOVERY);
2260					lpfc_disc_state_machine(vports[i], ndlp,
2261						NULL, NLP_EVT_DEVICE_RM);
2262				}
2263				spin_lock_irq(shost->host_lock);
2264				ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2265				spin_unlock_irq(shost->host_lock);
2266				lpfc_unreg_rpi(vports[i], ndlp);
2267			}
2268		}
2269	}
2270	lpfc_destroy_vport_work_array(phba, vports);
2271
2272	lpfc_sli_mbox_sys_shutdown(phba);
2273}
2274
2275/**
2276 * lpfc_offline - Bring a HBA offline
2277 * @phba: pointer to lpfc hba data structure.
2278 *
2279 * This routine actually brings a HBA offline. It stops all the timers
2280 * associated with the HBA, brings down the SLI layer, and eventually
2281 * marks the HBA as in offline state for the upper layer protocol.
2282 **/
2283void
2284lpfc_offline(struct lpfc_hba *phba)
2285{
2286	struct Scsi_Host  *shost;
2287	struct lpfc_vport **vports;
2288	int i;
2289
2290	if (phba->pport->fc_flag & FC_OFFLINE_MODE)
2291		return;
2292
2293	/* stop port and all timers associated with this hba */
2294	lpfc_stop_port(phba);
2295	vports = lpfc_create_vport_work_array(phba);
2296	if (vports != NULL)
2297		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
2298			lpfc_stop_vport_timers(vports[i]);
2299	lpfc_destroy_vport_work_array(phba, vports);
2300	lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2301			"0460 Bring Adapter offline\n");
2302	/* Bring down the SLI Layer and cleanup.  The HBA is offline
2303	   now.  */
2304	lpfc_sli_hba_down(phba);
2305	spin_lock_irq(&phba->hbalock);
2306	phba->work_ha = 0;
2307	spin_unlock_irq(&phba->hbalock);
2308	vports = lpfc_create_vport_work_array(phba);
2309	if (vports != NULL)
2310		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2311			shost = lpfc_shost_from_vport(vports[i]);
2312			spin_lock_irq(shost->host_lock);
2313			vports[i]->work_port_events = 0;
2314			vports[i]->fc_flag |= FC_OFFLINE_MODE;
2315			spin_unlock_irq(shost->host_lock);
2316		}
2317	lpfc_destroy_vport_work_array(phba, vports);
2318}
2319
2320/**
2321 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
2322 * @phba: pointer to lpfc hba data structure.
2323 *
2324 * This routine is to free all the SCSI buffers and IOCBs from the driver
2325 * list back to kernel. It is called from lpfc_pci_remove_one to free
2326 * the internal resources before the device is removed from the system.
2327 *
2328 * Return codes
2329 *   0 - successful (for now, it always returns 0)
2330 **/
2331static int
2332lpfc_scsi_free(struct lpfc_hba *phba)
2333{
2334	struct lpfc_scsi_buf *sb, *sb_next;
2335	struct lpfc_iocbq *io, *io_next;
2336
2337	spin_lock_irq(&phba->hbalock);
2338	/* Release all the lpfc_scsi_bufs maintained by this host. */
2339	spin_lock(&phba->scsi_buf_list_lock);
2340	list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
2341		list_del(&sb->list);
2342		pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
2343			      sb->dma_handle);
2344		kfree(sb);
2345		phba->total_scsi_bufs--;
2346	}
2347	spin_unlock(&phba->scsi_buf_list_lock);
2348
2349	/* Release all the lpfc_iocbq entries maintained by this host. */
2350	list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2351		list_del(&io->list);
2352		kfree(io);
2353		phba->total_iocbq_bufs--;
2354	}
2355	spin_unlock_irq(&phba->hbalock);
2356	return 0;
2357}
2358
2359/**
2360 * lpfc_create_port - Create an FC port
2361 * @phba: pointer to lpfc hba data structure.
2362 * @instance: a unique integer ID to this FC port.
2363 * @dev: pointer to the device data structure.
2364 *
2365 * This routine creates a FC port for the upper layer protocol. The FC port
2366 * can be created on top of either a physical port or a virtual port provided
2367 * by the HBA. This routine also allocates a SCSI host data structure (shost)
2368 * and associates the FC port created before adding the shost into the SCSI
2369 * layer.
2370 *
2371 * Return codes
2372 *   @vport - pointer to the virtual N_Port data structure.
2373 *   NULL - port create failed.
2374 **/
2375struct lpfc_vport *
2376lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
2377{
2378	struct lpfc_vport *vport;
2379	struct Scsi_Host  *shost;
2380	int error = 0;
2381
2382	if (dev != &phba->pcidev->dev)
2383		shost = scsi_host_alloc(&lpfc_vport_template,
2384					sizeof(struct lpfc_vport));
2385	else
2386		shost = scsi_host_alloc(&lpfc_template,
2387					sizeof(struct lpfc_vport));
2388	if (!shost)
2389		goto out;
2390
2391	vport = (struct lpfc_vport *) shost->hostdata;
2392	vport->phba = phba;
2393	vport->load_flag |= FC_LOADING;
2394	vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2395	vport->fc_rscn_flush = 0;
2396
2397	lpfc_get_vport_cfgparam(vport);
2398	shost->unique_id = instance;
2399	shost->max_id = LPFC_MAX_TARGET;
2400	shost->max_lun = vport->cfg_max_luns;
2401	shost->this_id = -1;
2402	shost->max_cmd_len = 16;
2403	if (phba->sli_rev == LPFC_SLI_REV4) {
2404		shost->dma_boundary = LPFC_SLI4_MAX_SEGMENT_SIZE;
2405		shost->sg_tablesize = phba->cfg_sg_seg_cnt;
2406	}
2407
2408	/*
2409	 * Set initial can_queue value since 0 is no longer supported and
2410	 * scsi_add_host will fail. This will be adjusted later based on the
2411	 * max xri value determined in hba setup.
2412	 */
2413	shost->can_queue = phba->cfg_hba_queue_depth - 10;
2414	if (dev != &phba->pcidev->dev) {
2415		shost->transportt = lpfc_vport_transport_template;
2416		vport->port_type = LPFC_NPIV_PORT;
2417	} else {
2418		shost->transportt = lpfc_transport_template;
2419		vport->port_type = LPFC_PHYSICAL_PORT;
2420	}
2421
2422	/* Initialize all internally managed lists. */
2423	INIT_LIST_HEAD(&vport->fc_nodes);
2424	INIT_LIST_HEAD(&vport->rcv_buffer_list);
2425	spin_lock_init(&vport->work_port_lock);
2426
2427	init_timer(&vport->fc_disctmo);
2428	vport->fc_disctmo.function = lpfc_disc_timeout;
2429	vport->fc_disctmo.data = (unsigned long)vport;
2430
2431	init_timer(&vport->fc_fdmitmo);
2432	vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
2433	vport->fc_fdmitmo.data = (unsigned long)vport;
2434
2435	init_timer(&vport->els_tmofunc);
2436	vport->els_tmofunc.function = lpfc_els_timeout;
2437	vport->els_tmofunc.data = (unsigned long)vport;
2438
2439	error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
2440	if (error)
2441		goto out_put_shost;
2442
2443	spin_lock_irq(&phba->hbalock);
2444	list_add_tail(&vport->listentry, &phba->port_list);
2445	spin_unlock_irq(&phba->hbalock);
2446	return vport;
2447
2448out_put_shost:
2449	scsi_host_put(shost);
2450out:
2451	return NULL;
2452}
2453
2454/**
2455 * destroy_port -  destroy an FC port
2456 * @vport: pointer to an lpfc virtual N_Port data structure.
2457 *
2458 * This routine destroys a FC port from the upper layer protocol. All the
2459 * resources associated with the port are released.
2460 **/
2461void
2462destroy_port(struct lpfc_vport *vport)
2463{
2464	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2465	struct lpfc_hba  *phba = vport->phba;
2466
2467	lpfc_debugfs_terminate(vport);
2468	fc_remove_host(shost);
2469	scsi_remove_host(shost);
2470
2471	spin_lock_irq(&phba->hbalock);
2472	list_del_init(&vport->listentry);
2473	spin_unlock_irq(&phba->hbalock);
2474
2475	lpfc_cleanup(vport);
2476	return;
2477}
2478
2479/**
2480 * lpfc_get_instance - Get a unique integer ID
2481 *
2482 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
2483 * uses the kernel idr facility to perform the task.
2484 *
2485 * Return codes:
2486 *   instance - a unique integer ID allocated as the new instance.
2487 *   -1 - lpfc get instance failed.
2488 **/
2489int
2490lpfc_get_instance(void)
2491{
2492	int instance = 0;
2493
2494	/* Assign an unused number */
2495	if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
2496		return -1;
2497	if (idr_get_new(&lpfc_hba_index, NULL, &instance))
2498		return -1;
2499	return instance;
2500}
2501
2502/**
2503 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
2504 * @shost: pointer to SCSI host data structure.
2505 * @time: elapsed time of the scan in jiffies.
2506 *
2507 * This routine is called by the SCSI layer with a SCSI host to determine
2508 * whether the scan host is finished.
2509 *
2510 * Note: there is no scan_start function as adapter initialization will have
2511 * asynchronously kicked off the link initialization.
2512 *
2513 * Return codes
2514 *   0 - SCSI host scan is not over yet.
2515 *   1 - SCSI host scan is over.
2516 **/
2517int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2518{
2519	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2520	struct lpfc_hba   *phba = vport->phba;
2521	int stat = 0;
2522
2523	spin_lock_irq(shost->host_lock);
2524
2525	if (vport->load_flag & FC_UNLOADING) {
2526		stat = 1;
2527		goto finished;
2528	}
2529	if (time >= 30 * HZ) {
2530		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2531				"0461 Scanning longer than 30 "
2532				"seconds.  Continuing initialization\n");
2533		stat = 1;
2534		goto finished;
2535	}
2536	if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
2537		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2538				"0465 Link down longer than 15 "
2539				"seconds.  Continuing initialization\n");
2540		stat = 1;
2541		goto finished;
2542	}
2543
2544	if (vport->port_state != LPFC_VPORT_READY)
2545		goto finished;
2546	if (vport->num_disc_nodes || vport->fc_prli_sent)
2547		goto finished;
2548	if (vport->fc_map_cnt == 0 && time < 2 * HZ)
2549		goto finished;
2550	if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
2551		goto finished;
2552
2553	stat = 1;
2554
2555finished:
2556	spin_unlock_irq(shost->host_lock);
2557	return stat;
2558}
2559
2560/**
2561 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
2562 * @shost: pointer to SCSI host data structure.
2563 *
2564 * This routine initializes a given SCSI host attributes on a FC port. The
2565 * SCSI host can be either on top of a physical port or a virtual port.
2566 **/
2567void lpfc_host_attrib_init(struct Scsi_Host *shost)
2568{
2569	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2570	struct lpfc_hba   *phba = vport->phba;
2571	/*
2572	 * Set fixed host attributes.  Must done after lpfc_sli_hba_setup().
2573	 */
2574
2575	fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
2576	fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
2577	fc_host_supported_classes(shost) = FC_COS_CLASS3;
2578
2579	memset(fc_host_supported_fc4s(shost), 0,
2580	       sizeof(fc_host_supported_fc4s(shost)));
2581	fc_host_supported_fc4s(shost)[2] = 1;
2582	fc_host_supported_fc4s(shost)[7] = 1;
2583
2584	lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
2585				 sizeof fc_host_symbolic_name(shost));
2586
2587	fc_host_supported_speeds(shost) = 0;
2588	if (phba->lmt & LMT_10Gb)
2589		fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
2590	if (phba->lmt & LMT_8Gb)
2591		fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
2592	if (phba->lmt & LMT_4Gb)
2593		fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
2594	if (phba->lmt & LMT_2Gb)
2595		fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
2596	if (phba->lmt & LMT_1Gb)
2597		fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
2598
2599	fc_host_maxframe_size(shost) =
2600		(((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
2601		(uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
2602
2603	/* This value is also unchanging */
2604	memset(fc_host_active_fc4s(shost), 0,
2605	       sizeof(fc_host_active_fc4s(shost)));
2606	fc_host_active_fc4s(shost)[2] = 1;
2607	fc_host_active_fc4s(shost)[7] = 1;
2608
2609	fc_host_max_npiv_vports(shost) = phba->max_vpi;
2610	spin_lock_irq(shost->host_lock);
2611	vport->load_flag &= ~FC_LOADING;
2612	spin_unlock_irq(shost->host_lock);
2613}
2614
2615/**
2616 * lpfc_stop_port_s3 - Stop SLI3 device port
2617 * @phba: pointer to lpfc hba data structure.
2618 *
2619 * This routine is invoked to stop an SLI3 device port, it stops the device
2620 * from generating interrupts and stops the device driver's timers for the
2621 * device.
2622 **/
2623static void
2624lpfc_stop_port_s3(struct lpfc_hba *phba)
2625{
2626	/* Clear all interrupt enable conditions */
2627	writel(0, phba->HCregaddr);
2628	readl(phba->HCregaddr); /* flush */
2629	/* Clear all pending interrupts */
2630	writel(0xffffffff, phba->HAregaddr);
2631	readl(phba->HAregaddr); /* flush */
2632
2633	/* Reset some HBA SLI setup states */
2634	lpfc_stop_hba_timers(phba);
2635	phba->pport->work_port_events = 0;
2636}
2637
2638/**
2639 * lpfc_stop_port_s4 - Stop SLI4 device port
2640 * @phba: pointer to lpfc hba data structure.
2641 *
2642 * This routine is invoked to stop an SLI4 device port, it stops the device
2643 * from generating interrupts and stops the device driver's timers for the
2644 * device.
2645 **/
2646static void
2647lpfc_stop_port_s4(struct lpfc_hba *phba)
2648{
2649	/* Reset some HBA SLI4 setup states */
2650	lpfc_stop_hba_timers(phba);
2651	phba->pport->work_port_events = 0;
2652	phba->sli4_hba.intr_enable = 0;
2653	/* Hard clear it for now, shall have more graceful way to wait later */
2654	phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2655}
2656
2657/**
2658 * lpfc_stop_port - Wrapper function for stopping hba port
2659 * @phba: Pointer to HBA context object.
2660 *
2661 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
2662 * the API jump table function pointer from the lpfc_hba struct.
2663 **/
2664void
2665lpfc_stop_port(struct lpfc_hba *phba)
2666{
2667	phba->lpfc_stop_port(phba);
2668}
2669
2670/**
2671 * lpfc_sli4_remove_dflt_fcf - Remove the driver default fcf record from the port.
2672 * @phba: pointer to lpfc hba data structure.
2673 *
2674 * This routine is invoked to remove the driver default fcf record from
2675 * the port.  This routine currently acts on FCF Index 0.
2676 *
2677 **/
2678void
2679lpfc_sli_remove_dflt_fcf(struct lpfc_hba *phba)
2680{
2681	int rc = 0;
2682	LPFC_MBOXQ_t *mboxq;
2683	struct lpfc_mbx_del_fcf_tbl_entry *del_fcf_record;
2684	uint32_t mbox_tmo, req_len;
2685	uint32_t shdr_status, shdr_add_status;
2686
2687	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2688	if (!mboxq) {
2689		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2690			"2020 Failed to allocate mbox for ADD_FCF cmd\n");
2691		return;
2692	}
2693
2694	req_len = sizeof(struct lpfc_mbx_del_fcf_tbl_entry) -
2695		  sizeof(struct lpfc_sli4_cfg_mhdr);
2696	rc = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
2697			      LPFC_MBOX_OPCODE_FCOE_DELETE_FCF,
2698			      req_len, LPFC_SLI4_MBX_EMBED);
2699	/*
2700	 * In phase 1, there is a single FCF index, 0.  In phase2, the driver
2701	 * supports multiple FCF indices.
2702	 */
2703	del_fcf_record = &mboxq->u.mqe.un.del_fcf_entry;
2704	bf_set(lpfc_mbx_del_fcf_tbl_count, del_fcf_record, 1);
2705	bf_set(lpfc_mbx_del_fcf_tbl_index, del_fcf_record,
2706	       phba->fcf.fcf_indx);
2707
2708	if (!phba->sli4_hba.intr_enable)
2709		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
2710	else {
2711		mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
2712		rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
2713	}
2714	/* The IOCTL status is embedded in the mailbox subheader. */
2715	shdr_status = bf_get(lpfc_mbox_hdr_status,
2716			     &del_fcf_record->header.cfg_shdr.response);
2717	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
2718				 &del_fcf_record->header.cfg_shdr.response);
2719	if (shdr_status || shdr_add_status || rc != MBX_SUCCESS) {
2720		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2721				"2516 DEL FCF of default FCF Index failed "
2722				"mbx status x%x, status x%x add_status x%x\n",
2723				rc, shdr_status, shdr_add_status);
2724	}
2725	if (rc != MBX_TIMEOUT)
2726		mempool_free(mboxq, phba->mbox_mem_pool);
2727}
2728
2729/**
2730 * lpfc_sli4_fw_cfg_check - Read the firmware config and verify FCoE support
2731 * @phba: pointer to lpfc hba data structure.
2732 *
2733 * This function uses the QUERY_FW_CFG mailbox command to determine if the
2734 * firmware loaded supports FCoE. A return of zero indicates that the mailbox
2735 * was successful and the firmware supports FCoE. Any other return indicates
2736 * a error. It is assumed that this function will be called before interrupts
2737 * are enabled.
2738 **/
2739static int
2740lpfc_sli4_fw_cfg_check(struct lpfc_hba *phba)
2741{
2742	int rc = 0;
2743	LPFC_MBOXQ_t *mboxq;
2744	struct lpfc_mbx_query_fw_cfg *query_fw_cfg;
2745	uint32_t length;
2746	uint32_t shdr_status, shdr_add_status;
2747
2748	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2749	if (!mboxq) {
2750		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2751				"2621 Failed to allocate mbox for "
2752				"query firmware config cmd\n");
2753		return -ENOMEM;
2754	}
2755	query_fw_cfg = &mboxq->u.mqe.un.query_fw_cfg;
2756	length = (sizeof(struct lpfc_mbx_query_fw_cfg) -
2757		  sizeof(struct lpfc_sli4_cfg_mhdr));
2758	lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
2759			 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
2760			 length, LPFC_SLI4_MBX_EMBED);
2761	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
2762	/* The IOCTL status is embedded in the mailbox subheader. */
2763	shdr_status = bf_get(lpfc_mbox_hdr_status,
2764			     &query_fw_cfg->header.cfg_shdr.response);
2765	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
2766				 &query_fw_cfg->header.cfg_shdr.response);
2767	if (shdr_status || shdr_add_status || rc != MBX_SUCCESS) {
2768		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2769				"2622 Query Firmware Config failed "
2770				"mbx status x%x, status x%x add_status x%x\n",
2771				rc, shdr_status, shdr_add_status);
2772		return -EINVAL;
2773	}
2774	if (!bf_get(lpfc_function_mode_fcoe_i, query_fw_cfg)) {
2775		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2776				"2623 FCoE Function not supported by firmware. "
2777				"Function mode = %08x\n",
2778				query_fw_cfg->function_mode);
2779		return -EINVAL;
2780	}
2781	if (rc != MBX_TIMEOUT)
2782		mempool_free(mboxq, phba->mbox_mem_pool);
2783	return 0;
2784}
2785
2786/**
2787 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
2788 * @phba: pointer to lpfc hba data structure.
2789 * @acqe_link: pointer to the async link completion queue entry.
2790 *
2791 * This routine is to parse the SLI4 link-attention link fault code and
2792 * translate it into the base driver's read link attention mailbox command
2793 * status.
2794 *
2795 * Return: Link-attention status in terms of base driver's coding.
2796 **/
2797static uint16_t
2798lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
2799			   struct lpfc_acqe_link *acqe_link)
2800{
2801	uint16_t latt_fault;
2802
2803	switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
2804	case LPFC_ASYNC_LINK_FAULT_NONE:
2805	case LPFC_ASYNC_LINK_FAULT_LOCAL:
2806	case LPFC_ASYNC_LINK_FAULT_REMOTE:
2807		latt_fault = 0;
2808		break;
2809	default:
2810		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2811				"0398 Invalid link fault code: x%x\n",
2812				bf_get(lpfc_acqe_link_fault, acqe_link));
2813		latt_fault = MBXERR_ERROR;
2814		break;
2815	}
2816	return latt_fault;
2817}
2818
2819/**
2820 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
2821 * @phba: pointer to lpfc hba data structure.
2822 * @acqe_link: pointer to the async link completion queue entry.
2823 *
2824 * This routine is to parse the SLI4 link attention type and translate it
2825 * into the base driver's link attention type coding.
2826 *
2827 * Return: Link attention type in terms of base driver's coding.
2828 **/
2829static uint8_t
2830lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
2831			  struct lpfc_acqe_link *acqe_link)
2832{
2833	uint8_t att_type;
2834
2835	switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
2836	case LPFC_ASYNC_LINK_STATUS_DOWN:
2837	case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
2838		att_type = AT_LINK_DOWN;
2839		break;
2840	case LPFC_ASYNC_LINK_STATUS_UP:
2841		/* Ignore physical link up events - wait for logical link up */
2842		att_type = AT_RESERVED;
2843		break;
2844	case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
2845		att_type = AT_LINK_UP;
2846		break;
2847	default:
2848		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2849				"0399 Invalid link attention type: x%x\n",
2850				bf_get(lpfc_acqe_link_status, acqe_link));
2851		att_type = AT_RESERVED;
2852		break;
2853	}
2854	return att_type;
2855}
2856
2857/**
2858 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
2859 * @phba: pointer to lpfc hba data structure.
2860 * @acqe_link: pointer to the async link completion queue entry.
2861 *
2862 * This routine is to parse the SLI4 link-attention link speed and translate
2863 * it into the base driver's link-attention link speed coding.
2864 *
2865 * Return: Link-attention link speed in terms of base driver's coding.
2866 **/
2867static uint8_t
2868lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
2869				struct lpfc_acqe_link *acqe_link)
2870{
2871	uint8_t link_speed;
2872
2873	switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
2874	case LPFC_ASYNC_LINK_SPEED_ZERO:
2875		link_speed = LA_UNKNW_LINK;
2876		break;
2877	case LPFC_ASYNC_LINK_SPEED_10MBPS:
2878		link_speed = LA_UNKNW_LINK;
2879		break;
2880	case LPFC_ASYNC_LINK_SPEED_100MBPS:
2881		link_speed = LA_UNKNW_LINK;
2882		break;
2883	case LPFC_ASYNC_LINK_SPEED_1GBPS:
2884		link_speed = LA_1GHZ_LINK;
2885		break;
2886	case LPFC_ASYNC_LINK_SPEED_10GBPS:
2887		link_speed = LA_10GHZ_LINK;
2888		break;
2889	default:
2890		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2891				"0483 Invalid link-attention link speed: x%x\n",
2892				bf_get(lpfc_acqe_link_speed, acqe_link));
2893		link_speed = LA_UNKNW_LINK;
2894		break;
2895	}
2896	return link_speed;
2897}
2898
2899/**
2900 * lpfc_sli4_async_link_evt - Process the asynchronous link event
2901 * @phba: pointer to lpfc hba data structure.
2902 * @acqe_link: pointer to the async link completion queue entry.
2903 *
2904 * This routine is to handle the SLI4 asynchronous link event.
2905 **/
2906static void
2907lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
2908			 struct lpfc_acqe_link *acqe_link)
2909{
2910	struct lpfc_dmabuf *mp;
2911	LPFC_MBOXQ_t *pmb;
2912	MAILBOX_t *mb;
2913	READ_LA_VAR *la;
2914	uint8_t att_type;
2915
2916	att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
2917	if (att_type != AT_LINK_DOWN && att_type != AT_LINK_UP)
2918		return;
2919	phba->fcoe_eventtag = acqe_link->event_tag;
2920	pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2921	if (!pmb) {
2922		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2923				"0395 The mboxq allocation failed\n");
2924		return;
2925	}
2926	mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2927	if (!mp) {
2928		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2929				"0396 The lpfc_dmabuf allocation failed\n");
2930		goto out_free_pmb;
2931	}
2932	mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
2933	if (!mp->virt) {
2934		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2935				"0397 The mbuf allocation failed\n");
2936		goto out_free_dmabuf;
2937	}
2938
2939	/* Cleanup any outstanding ELS commands */
2940	lpfc_els_flush_all_cmd(phba);
2941
2942	/* Block ELS IOCBs until we have done process link event */
2943	phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
2944
2945	/* Update link event statistics */
2946	phba->sli.slistat.link_event++;
2947
2948	/* Create pseudo lpfc_handle_latt mailbox command from link ACQE */
2949	lpfc_read_la(phba, pmb, mp);
2950	pmb->vport = phba->pport;
2951
2952	/* Parse and translate status field */
2953	mb = &pmb->u.mb;
2954	mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
2955
2956	/* Parse and translate link attention fields */
2957	la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
2958	la->eventTag = acqe_link->event_tag;
2959	la->attType = att_type;
2960	la->UlnkSpeed = lpfc_sli4_parse_latt_link_speed(phba, acqe_link);
2961
2962	/* Fake the the following irrelvant fields */
2963	la->topology = TOPOLOGY_PT_PT;
2964	la->granted_AL_PA = 0;
2965	la->il = 0;
2966	la->pb = 0;
2967	la->fa = 0;
2968	la->mm = 0;
2969
2970	/* Keep the link status for extra SLI4 state machine reference */
2971	phba->sli4_hba.link_state.speed =
2972				bf_get(lpfc_acqe_link_speed, acqe_link);
2973	phba->sli4_hba.link_state.duplex =
2974				bf_get(lpfc_acqe_link_duplex, acqe_link);
2975	phba->sli4_hba.link_state.status =
2976				bf_get(lpfc_acqe_link_status, acqe_link);
2977	phba->sli4_hba.link_state.physical =
2978				bf_get(lpfc_acqe_link_physical, acqe_link);
2979	phba->sli4_hba.link_state.fault =
2980				bf_get(lpfc_acqe_link_fault, acqe_link);
2981
2982	/* Invoke the lpfc_handle_latt mailbox command callback function */
2983	lpfc_mbx_cmpl_read_la(phba, pmb);
2984
2985	return;
2986
2987out_free_dmabuf:
2988	kfree(mp);
2989out_free_pmb:
2990	mempool_free(pmb, phba->mbox_mem_pool);
2991}
2992
2993/**
2994 * lpfc_sli4_async_fcoe_evt - Process the asynchronous fcoe event
2995 * @phba: pointer to lpfc hba data structure.
2996 * @acqe_link: pointer to the async fcoe completion queue entry.
2997 *
2998 * This routine is to handle the SLI4 asynchronous fcoe event.
2999 **/
3000static void
3001lpfc_sli4_async_fcoe_evt(struct lpfc_hba *phba,
3002			 struct lpfc_acqe_fcoe *acqe_fcoe)
3003{
3004	uint8_t event_type = bf_get(lpfc_acqe_fcoe_event_type, acqe_fcoe);
3005	int rc;
3006	struct lpfc_vport *vport;
3007	struct lpfc_nodelist *ndlp;
3008	struct Scsi_Host  *shost;
3009
3010	phba->fc_eventTag = acqe_fcoe->event_tag;
3011	phba->fcoe_eventtag = acqe_fcoe->event_tag;
3012	switch (event_type) {
3013	case LPFC_FCOE_EVENT_TYPE_NEW_FCF:
3014		lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
3015			"2546 New FCF found index 0x%x tag 0x%x\n",
3016			acqe_fcoe->index,
3017			acqe_fcoe->event_tag);
3018		/*
3019		 * If the current FCF is in discovered state, or
3020		 * FCF discovery is in progress do nothing.
3021		 */
3022		spin_lock_irq(&phba->hbalock);
3023		if ((phba->fcf.fcf_flag & FCF_DISCOVERED) ||
3024		   (phba->hba_flag & FCF_DISC_INPROGRESS)) {
3025			spin_unlock_irq(&phba->hbalock);
3026			break;
3027		}
3028		spin_unlock_irq(&phba->hbalock);
3029
3030		/* Read the FCF table and re-discover SAN. */
3031		rc = lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
3032		if (rc)
3033			lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
3034					"2547 Read FCF record failed 0x%x\n",
3035					rc);
3036		break;
3037
3038	case LPFC_FCOE_EVENT_TYPE_FCF_TABLE_FULL:
3039		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3040			"2548 FCF Table full count 0x%x tag 0x%x\n",
3041			bf_get(lpfc_acqe_fcoe_fcf_count, acqe_fcoe),
3042			acqe_fcoe->event_tag);
3043		break;
3044
3045	case LPFC_FCOE_EVENT_TYPE_FCF_DEAD:
3046		lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
3047			"2549 FCF disconnected from network index 0x%x"
3048			" tag 0x%x\n", acqe_fcoe->index,
3049			acqe_fcoe->event_tag);
3050		/* If the event is not for currently used fcf do nothing */
3051		if (phba->fcf.fcf_indx != acqe_fcoe->index)
3052			break;
3053		/*
3054		 * Currently, driver support only one FCF - so treat this as
3055		 * a link down.
3056		 */
3057		lpfc_linkdown(phba);
3058		/* Unregister FCF if no devices connected to it */
3059		lpfc_unregister_unused_fcf(phba);
3060		break;
3061	case LPFC_FCOE_EVENT_TYPE_CVL:
3062		lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
3063			"2718 Clear Virtual Link Received for VPI 0x%x"
3064			" tag 0x%x\n", acqe_fcoe->index, acqe_fcoe->event_tag);
3065		vport = lpfc_find_vport_by_vpid(phba,
3066				acqe_fcoe->index - phba->vpi_base);
3067		if (!vport)
3068			break;
3069		ndlp = lpfc_findnode_did(vport, Fabric_DID);
3070		if (!ndlp)
3071			break;
3072		shost = lpfc_shost_from_vport(vport);
3073		lpfc_linkdown_port(vport);
3074		if (vport->port_type != LPFC_NPIV_PORT) {
3075			mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
3076			spin_lock_irq(shost->host_lock);
3077			ndlp->nlp_flag |= NLP_DELAY_TMO;
3078			spin_unlock_irq(shost->host_lock);
3079			ndlp->nlp_last_elscmd = ELS_CMD_FLOGI;
3080			vport->port_state = LPFC_FLOGI;
3081		}
3082		break;
3083	default:
3084		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3085			"0288 Unknown FCoE event type 0x%x event tag "
3086			"0x%x\n", event_type, acqe_fcoe->event_tag);
3087		break;
3088	}
3089}
3090
3091/**
3092 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
3093 * @phba: pointer to lpfc hba data structure.
3094 * @acqe_link: pointer to the async dcbx completion queue entry.
3095 *
3096 * This routine is to handle the SLI4 asynchronous dcbx event.
3097 **/
3098static void
3099lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
3100			 struct lpfc_acqe_dcbx *acqe_dcbx)
3101{
3102	phba->fc_eventTag = acqe_dcbx->event_tag;
3103	lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3104			"0290 The SLI4 DCBX asynchronous event is not "
3105			"handled yet\n");
3106}
3107
3108/**
3109 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
3110 * @phba: pointer to lpfc hba data structure.
3111 *
3112 * This routine is invoked by the worker thread to process all the pending
3113 * SLI4 asynchronous events.
3114 **/
3115void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
3116{
3117	struct lpfc_cq_event *cq_event;
3118
3119	/* First, declare the async event has been handled */
3120	spin_lock_irq(&phba->hbalock);
3121	phba->hba_flag &= ~ASYNC_EVENT;
3122	spin_unlock_irq(&phba->hbalock);
3123	/* Now, handle all the async events */
3124	while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
3125		/* Get the first event from the head of the event queue */
3126		spin_lock_irq(&phba->hbalock);
3127		list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
3128				 cq_event, struct lpfc_cq_event, list);
3129		spin_unlock_irq(&phba->hbalock);
3130		/* Process the asynchronous event */
3131		switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
3132		case LPFC_TRAILER_CODE_LINK:
3133			lpfc_sli4_async_link_evt(phba,
3134						 &cq_event->cqe.acqe_link);
3135			break;
3136		case LPFC_TRAILER_CODE_FCOE:
3137			lpfc_sli4_async_fcoe_evt(phba,
3138						 &cq_event->cqe.acqe_fcoe);
3139			break;
3140		case LPFC_TRAILER_CODE_DCBX:
3141			lpfc_sli4_async_dcbx_evt(phba,
3142						 &cq_event->cqe.acqe_dcbx);
3143			break;
3144		default:
3145			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3146					"1804 Invalid asynchrous event code: "
3147					"x%x\n", bf_get(lpfc_trailer_code,
3148					&cq_event->cqe.mcqe_cmpl));
3149			break;
3150		}
3151		/* Free the completion event processed to the free pool */
3152		lpfc_sli4_cq_event_release(phba, cq_event);
3153	}
3154}
3155
3156/**
3157 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
3158 * @phba: pointer to lpfc hba data structure.
3159 * @dev_grp: The HBA PCI-Device group number.
3160 *
3161 * This routine is invoked to set up the per HBA PCI-Device group function
3162 * API jump table entries.
3163 *
3164 * Return: 0 if success, otherwise -ENODEV
3165 **/
3166int
3167lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
3168{
3169	int rc;
3170
3171	/* Set up lpfc PCI-device group */
3172	phba->pci_dev_grp = dev_grp;
3173
3174	/* The LPFC_PCI_DEV_OC uses SLI4 */
3175	if (dev_grp == LPFC_PCI_DEV_OC)
3176		phba->sli_rev = LPFC_SLI_REV4;
3177
3178	/* Set up device INIT API function jump table */
3179	rc = lpfc_init_api_table_setup(phba, dev_grp);
3180	if (rc)
3181		return -ENODEV;
3182	/* Set up SCSI API function jump table */
3183	rc = lpfc_scsi_api_table_setup(phba, dev_grp);
3184	if (rc)
3185		return -ENODEV;
3186	/* Set up SLI API function jump table */
3187	rc = lpfc_sli_api_table_setup(phba, dev_grp);
3188	if (rc)
3189		return -ENODEV;
3190	/* Set up MBOX API function jump table */
3191	rc = lpfc_mbox_api_table_setup(phba, dev_grp);
3192	if (rc)
3193		return -ENODEV;
3194
3195	return 0;
3196}
3197
3198/**
3199 * lpfc_log_intr_mode - Log the active interrupt mode
3200 * @phba: pointer to lpfc hba data structure.
3201 * @intr_mode: active interrupt mode adopted.
3202 *
3203 * This routine it invoked to log the currently used active interrupt mode
3204 * to the device.
3205 **/
3206static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
3207{
3208	switch (intr_mode) {
3209	case 0:
3210		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3211				"0470 Enable INTx interrupt mode.\n");
3212		break;
3213	case 1:
3214		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3215				"0481 Enabled MSI interrupt mode.\n");
3216		break;
3217	case 2:
3218		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3219				"0480 Enabled MSI-X interrupt mode.\n");
3220		break;
3221	default:
3222		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3223				"0482 Illegal interrupt mode.\n");
3224		break;
3225	}
3226	return;
3227}
3228
3229/**
3230 * lpfc_enable_pci_dev - Enable a generic PCI device.
3231 * @phba: pointer to lpfc hba data structure.
3232 *
3233 * This routine is invoked to enable the PCI device that is common to all
3234 * PCI devices.
3235 *
3236 * Return codes
3237 * 	0 - successful
3238 * 	other values - error
3239 **/
3240static int
3241lpfc_enable_pci_dev(struct lpfc_hba *phba)
3242{
3243	struct pci_dev *pdev;
3244	int bars;
3245
3246	/* Obtain PCI device reference */
3247	if (!phba->pcidev)
3248		goto out_error;
3249	else
3250		pdev = phba->pcidev;
3251	/* Select PCI BARs */
3252	bars = pci_select_bars(pdev, IORESOURCE_MEM);
3253	/* Enable PCI device */
3254	if (pci_enable_device_mem(pdev))
3255		goto out_error;
3256	/* Request PCI resource for the device */
3257	if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
3258		goto out_disable_device;
3259	/* Set up device as PCI master and save state for EEH */
3260	pci_set_master(pdev);
3261	pci_try_set_mwi(pdev);
3262	pci_save_state(pdev);
3263
3264	return 0;
3265
3266out_disable_device:
3267	pci_disable_device(pdev);
3268out_error:
3269	return -ENODEV;
3270}
3271
3272/**
3273 * lpfc_disable_pci_dev - Disable a generic PCI device.
3274 * @phba: pointer to lpfc hba data structure.
3275 *
3276 * This routine is invoked to disable the PCI device that is common to all
3277 * PCI devices.
3278 **/
3279static void
3280lpfc_disable_pci_dev(struct lpfc_hba *phba)
3281{
3282	struct pci_dev *pdev;
3283	int bars;
3284
3285	/* Obtain PCI device reference */
3286	if (!phba->pcidev)
3287		return;
3288	else
3289		pdev = phba->pcidev;
3290	/* Select PCI BARs */
3291	bars = pci_select_bars(pdev, IORESOURCE_MEM);
3292	/* Release PCI resource and disable PCI device */
3293	pci_release_selected_regions(pdev, bars);
3294	pci_disable_device(pdev);
3295	/* Null out PCI private reference to driver */
3296	pci_set_drvdata(pdev, NULL);
3297
3298	return;
3299}
3300
3301/**
3302 * lpfc_reset_hba - Reset a hba
3303 * @phba: pointer to lpfc hba data structure.
3304 *
3305 * This routine is invoked to reset a hba device. It brings the HBA
3306 * offline, performs a board restart, and then brings the board back
3307 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
3308 * on outstanding mailbox commands.
3309 **/
3310void
3311lpfc_reset_hba(struct lpfc_hba *phba)
3312{
3313	/* If resets are disabled then set error state and return. */
3314	if (!phba->cfg_enable_hba_reset) {
3315		phba->link_state = LPFC_HBA_ERROR;
3316		return;
3317	}
3318	lpfc_offline_prep(phba);
3319	lpfc_offline(phba);
3320	lpfc_sli_brdrestart(phba);
3321	lpfc_online(phba);
3322	lpfc_unblock_mgmt_io(phba);
3323}
3324
3325/**
3326 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
3327 * @phba: pointer to lpfc hba data structure.
3328 *
3329 * This routine is invoked to set up the driver internal resources specific to
3330 * support the SLI-3 HBA device it attached to.
3331 *
3332 * Return codes
3333 * 	0 - successful
3334 * 	other values - error
3335 **/
3336static int
3337lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
3338{
3339	struct lpfc_sli *psli;
3340
3341	/*
3342	 * Initialize timers used by driver
3343	 */
3344
3345	/* Heartbeat timer */
3346	init_timer(&phba->hb_tmofunc);
3347	phba->hb_tmofunc.function = lpfc_hb_timeout;
3348	phba->hb_tmofunc.data = (unsigned long)phba;
3349
3350	psli = &phba->sli;
3351	/* MBOX heartbeat timer */
3352	init_timer(&psli->mbox_tmo);
3353	psli->mbox_tmo.function = lpfc_mbox_timeout;
3354	psli->mbox_tmo.data = (unsigned long) phba;
3355	/* FCP polling mode timer */
3356	init_timer(&phba->fcp_poll_timer);
3357	phba->fcp_poll_timer.function = lpfc_poll_timeout;
3358	phba->fcp_poll_timer.data = (unsigned long) phba;
3359	/* Fabric block timer */
3360	init_timer(&phba->fabric_block_timer);
3361	phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
3362	phba->fabric_block_timer.data = (unsigned long) phba;
3363	/* EA polling mode timer */
3364	init_timer(&phba->eratt_poll);
3365	phba->eratt_poll.function = lpfc_poll_eratt;
3366	phba->eratt_poll.data = (unsigned long) phba;
3367
3368	/* Host attention work mask setup */
3369	phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
3370	phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
3371
3372	/* Get all the module params for configuring this host */
3373	lpfc_get_cfgparam(phba);
3374	/*
3375	 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
3376	 * used to create the sg_dma_buf_pool must be dynamically calculated.
3377	 * 2 segments are added since the IOCB needs a command and response bde.
3378	 */
3379	phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
3380		sizeof(struct fcp_rsp) +
3381			((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
3382
3383	if (phba->cfg_enable_bg) {
3384		phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT;
3385		phba->cfg_sg_dma_buf_size +=
3386			phba->cfg_prot_sg_seg_cnt * sizeof(struct ulp_bde64);
3387	}
3388
3389	/* Also reinitialize the host templates with new values. */
3390	lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
3391	lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
3392
3393	phba->max_vpi = LPFC_MAX_VPI;
3394	/* This will be set to correct value after config_port mbox */
3395	phba->max_vports = 0;
3396
3397	/*
3398	 * Initialize the SLI Layer to run with lpfc HBAs.
3399	 */
3400	lpfc_sli_setup(phba);
3401	lpfc_sli_queue_setup(phba);
3402
3403	/* Allocate device driver memory */
3404	if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
3405		return -ENOMEM;
3406
3407	return 0;
3408}
3409
3410/**
3411 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
3412 * @phba: pointer to lpfc hba data structure.
3413 *
3414 * This routine is invoked to unset the driver internal resources set up
3415 * specific for supporting the SLI-3 HBA device it attached to.
3416 **/
3417static void
3418lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
3419{
3420	/* Free device driver memory allocated */
3421	lpfc_mem_free_all(phba);
3422
3423	return;
3424}
3425
3426/**
3427 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
3428 * @phba: pointer to lpfc hba data structure.
3429 *
3430 * This routine is invoked to set up the driver internal resources specific to
3431 * support the SLI-4 HBA device it attached to.
3432 *
3433 * Return codes
3434 * 	0 - successful
3435 * 	other values - error
3436 **/
3437static int
3438lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
3439{
3440	struct lpfc_sli *psli;
3441	int rc;
3442	int i, hbq_count;
3443
3444	/* Before proceed, wait for POST done and device ready */
3445	rc = lpfc_sli4_post_status_check(phba);
3446	if (rc)
3447		return -ENODEV;
3448
3449	/*
3450	 * Initialize timers used by driver
3451	 */
3452
3453	/* Heartbeat timer */
3454	init_timer(&phba->hb_tmofunc);
3455	phba->hb_tmofunc.function = lpfc_hb_timeout;
3456	phba->hb_tmofunc.data = (unsigned long)phba;
3457
3458	psli = &phba->sli;
3459	/* MBOX heartbeat timer */
3460	init_timer(&psli->mbox_tmo);
3461	psli->mbox_tmo.function = lpfc_mbox_timeout;
3462	psli->mbox_tmo.data = (unsigned long) phba;
3463	/* Fabric block timer */
3464	init_timer(&phba->fabric_block_timer);
3465	phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
3466	phba->fabric_block_timer.data = (unsigned long) phba;
3467	/* EA polling mode timer */
3468	init_timer(&phba->eratt_poll);
3469	phba->eratt_poll.function = lpfc_poll_eratt;
3470	phba->eratt_poll.data = (unsigned long) phba;
3471	/*
3472	 * We need to do a READ_CONFIG mailbox command here before
3473	 * calling lpfc_get_cfgparam. For VFs this will report the
3474	 * MAX_XRI, MAX_VPI, MAX_RPI, MAX_IOCB, and MAX_VFI settings.
3475	 * All of the resources allocated
3476	 * for this Port are tied to these values.
3477	 */
3478	/* Get all the module params for configuring this host */
3479	lpfc_get_cfgparam(phba);
3480	phba->max_vpi = LPFC_MAX_VPI;
3481	/* This will be set to correct value after the read_config mbox */
3482	phba->max_vports = 0;
3483
3484	/* Program the default value of vlan_id and fc_map */
3485	phba->valid_vlan = 0;
3486	phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
3487	phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
3488	phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
3489
3490	/*
3491	 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
3492	 * used to create the sg_dma_buf_pool must be dynamically calculated.
3493	 * 2 segments are added since the IOCB needs a command and response bde.
3494	 * To insure that the scsi sgl does not cross a 4k page boundary only
3495	 * sgl sizes of 1k, 2k, 4k, and 8k are supported.
3496	 * Table of sgl sizes and seg_cnt:
3497	 * sgl size, 	sg_seg_cnt	total seg
3498	 * 1k		50		52
3499	 * 2k		114		116
3500	 * 4k		242		244
3501	 * 8k		498		500
3502	 * cmd(32) + rsp(160) + (52 * sizeof(sli4_sge)) = 1024
3503	 * cmd(32) + rsp(160) + (116 * sizeof(sli4_sge)) = 2048
3504	 * cmd(32) + rsp(160) + (244 * sizeof(sli4_sge)) = 4096
3505	 * cmd(32) + rsp(160) + (500 * sizeof(sli4_sge)) = 8192
3506	 */
3507	if (phba->cfg_sg_seg_cnt <= LPFC_DEFAULT_SG_SEG_CNT)
3508		phba->cfg_sg_seg_cnt = 50;
3509	else if (phba->cfg_sg_seg_cnt <= 114)
3510		phba->cfg_sg_seg_cnt = 114;
3511	else if (phba->cfg_sg_seg_cnt <= 242)
3512		phba->cfg_sg_seg_cnt = 242;
3513	else
3514		phba->cfg_sg_seg_cnt = 498;
3515
3516	phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd)
3517					+ sizeof(struct fcp_rsp);
3518	phba->cfg_sg_dma_buf_size +=
3519		((phba->cfg_sg_seg_cnt + 2) * sizeof(struct sli4_sge));
3520
3521	/* Initialize buffer queue management fields */
3522	hbq_count = lpfc_sli_hbq_count();
3523	for (i = 0; i < hbq_count; ++i)
3524		INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
3525	INIT_LIST_HEAD(&phba->rb_pend_list);
3526	phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
3527	phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
3528
3529	/*
3530	 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
3531	 */
3532	/* Initialize the Abort scsi buffer list used by driver */
3533	spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
3534	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
3535	/* This abort list used by worker thread */
3536	spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
3537
3538	/*
3539	 * Initialize dirver internal slow-path work queues
3540	 */
3541
3542	/* Driver internel slow-path CQ Event pool */
3543	INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
3544	/* Response IOCB work queue list */
3545	INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
3546	/* Asynchronous event CQ Event work queue list */
3547	INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
3548	/* Fast-path XRI aborted CQ Event work queue list */
3549	INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
3550	/* Slow-path XRI aborted CQ Event work queue list */
3551	INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
3552	/* Receive queue CQ Event work queue list */
3553	INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
3554
3555	/* Initialize the driver internal SLI layer lists. */
3556	lpfc_sli_setup(phba);
3557	lpfc_sli_queue_setup(phba);
3558
3559	/* Allocate device driver memory */
3560	rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
3561	if (rc)
3562		return -ENOMEM;
3563
3564	/* Create the bootstrap mailbox command */
3565	rc = lpfc_create_bootstrap_mbox(phba);
3566	if (unlikely(rc))
3567		goto out_free_mem;
3568
3569	/* Set up the host's endian order with the device. */
3570	rc = lpfc_setup_endian_order(phba);
3571	if (unlikely(rc))
3572		goto out_free_bsmbx;
3573
3574	rc = lpfc_sli4_fw_cfg_check(phba);
3575	if (unlikely(rc))
3576		goto out_free_bsmbx;
3577
3578	/* Set up the hba's configuration parameters. */
3579	rc = lpfc_sli4_read_config(phba);
3580	if (unlikely(rc))
3581		goto out_free_bsmbx;
3582
3583	/* Perform a function reset */
3584	rc = lpfc_pci_function_reset(phba);
3585	if (unlikely(rc))
3586		goto out_free_bsmbx;
3587
3588	/* Create all the SLI4 queues */
3589	rc = lpfc_sli4_queue_create(phba);
3590	if (rc)
3591		goto out_free_bsmbx;
3592
3593	/* Create driver internal CQE event pool */
3594	rc = lpfc_sli4_cq_event_pool_create(phba);
3595	if (rc)
3596		goto out_destroy_queue;
3597
3598	/* Initialize and populate the iocb list per host */
3599	rc = lpfc_init_sgl_list(phba);
3600	if (rc) {
3601		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3602				"1400 Failed to initialize sgl list.\n");
3603		goto out_destroy_cq_event_pool;
3604	}
3605	rc = lpfc_init_active_sgl_array(phba);
3606	if (rc) {
3607		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3608				"1430 Failed to initialize sgl list.\n");
3609		goto out_free_sgl_list;
3610	}
3611
3612	rc = lpfc_sli4_init_rpi_hdrs(phba);
3613	if (rc) {
3614		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3615				"1432 Failed to initialize rpi headers.\n");
3616		goto out_free_active_sgl;
3617	}
3618
3619	phba->sli4_hba.fcp_eq_hdl = kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
3620				    phba->cfg_fcp_eq_count), GFP_KERNEL);
3621	if (!phba->sli4_hba.fcp_eq_hdl) {
3622		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3623				"2572 Failed allocate memory for fast-path "
3624				"per-EQ handle array\n");
3625		goto out_remove_rpi_hdrs;
3626	}
3627
3628	phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
3629				      phba->sli4_hba.cfg_eqn), GFP_KERNEL);
3630	if (!phba->sli4_hba.msix_entries) {
3631		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3632				"2573 Failed allocate memory for msi-x "
3633				"interrupt vector entries\n");
3634		goto out_free_fcp_eq_hdl;
3635	}
3636
3637	return rc;
3638
3639out_free_fcp_eq_hdl:
3640	kfree(phba->sli4_hba.fcp_eq_hdl);
3641out_remove_rpi_hdrs:
3642	lpfc_sli4_remove_rpi_hdrs(phba);
3643out_free_active_sgl:
3644	lpfc_free_active_sgl(phba);
3645out_free_sgl_list:
3646	lpfc_free_sgl_list(phba);
3647out_destroy_cq_event_pool:
3648	lpfc_sli4_cq_event_pool_destroy(phba);
3649out_destroy_queue:
3650	lpfc_sli4_queue_destroy(phba);
3651out_free_bsmbx:
3652	lpfc_destroy_bootstrap_mbox(phba);
3653out_free_mem:
3654	lpfc_mem_free(phba);
3655	return rc;
3656}
3657
3658/**
3659 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
3660 * @phba: pointer to lpfc hba data structure.
3661 *
3662 * This routine is invoked to unset the driver internal resources set up
3663 * specific for supporting the SLI-4 HBA device it attached to.
3664 **/
3665static void
3666lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
3667{
3668	struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
3669
3670	/* unregister default FCFI from the HBA */
3671	lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
3672
3673	/* Free the default FCR table */
3674	lpfc_sli_remove_dflt_fcf(phba);
3675
3676	/* Free memory allocated for msi-x interrupt vector entries */
3677	kfree(phba->sli4_hba.msix_entries);
3678
3679	/* Free memory allocated for fast-path work queue handles */
3680	kfree(phba->sli4_hba.fcp_eq_hdl);
3681
3682	/* Free the allocated rpi headers. */
3683	lpfc_sli4_remove_rpi_hdrs(phba);
3684	lpfc_sli4_remove_rpis(phba);
3685
3686	/* Free the ELS sgl list */
3687	lpfc_free_active_sgl(phba);
3688	lpfc_free_sgl_list(phba);
3689
3690	/* Free the SCSI sgl management array */
3691	kfree(phba->sli4_hba.lpfc_scsi_psb_array);
3692
3693	/* Free the SLI4 queues */
3694	lpfc_sli4_queue_destroy(phba);
3695
3696	/* Free the completion queue EQ event pool */
3697	lpfc_sli4_cq_event_release_all(phba);
3698	lpfc_sli4_cq_event_pool_destroy(phba);
3699
3700	/* Reset SLI4 HBA FCoE function */
3701	lpfc_pci_function_reset(phba);
3702
3703	/* Free the bsmbx region. */
3704	lpfc_destroy_bootstrap_mbox(phba);
3705
3706	/* Free the SLI Layer memory with SLI4 HBAs */
3707	lpfc_mem_free_all(phba);
3708
3709	/* Free the current connect table */
3710	list_for_each_entry_safe(conn_entry, next_conn_entry,
3711		&phba->fcf_conn_rec_list, list) {
3712		list_del_init(&conn_entry->list);
3713		kfree(conn_entry);
3714	}
3715
3716	return;
3717}
3718
3719/**
3720 * lpfc_init_api_table_setup - Set up init api fucntion jump table
3721 * @phba: The hba struct for which this call is being executed.
3722 * @dev_grp: The HBA PCI-Device group number.
3723 *
3724 * This routine sets up the device INIT interface API function jump table
3725 * in @phba struct.
3726 *
3727 * Returns: 0 - success, -ENODEV - failure.
3728 **/
3729int
3730lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
3731{
3732	switch (dev_grp) {
3733	case LPFC_PCI_DEV_LP:
3734		phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
3735		phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
3736		phba->lpfc_stop_port = lpfc_stop_port_s3;
3737		break;
3738	case LPFC_PCI_DEV_OC:
3739		phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
3740		phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
3741		phba->lpfc_stop_port = lpfc_stop_port_s4;
3742		break;
3743	default:
3744		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3745				"1431 Invalid HBA PCI-device group: 0x%x\n",
3746				dev_grp);
3747		return -ENODEV;
3748		break;
3749	}
3750	return 0;
3751}
3752
3753/**
3754 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
3755 * @phba: pointer to lpfc hba data structure.
3756 *
3757 * This routine is invoked to set up the driver internal resources before the
3758 * device specific resource setup to support the HBA device it attached to.
3759 *
3760 * Return codes
3761 *	0 - successful
3762 *	other values - error
3763 **/
3764static int
3765lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
3766{
3767	/*
3768	 * Driver resources common to all SLI revisions
3769	 */
3770	atomic_set(&phba->fast_event_count, 0);
3771	spin_lock_init(&phba->hbalock);
3772
3773	/* Initialize ndlp management spinlock */
3774	spin_lock_init(&phba->ndlp_lock);
3775
3776	INIT_LIST_HEAD(&phba->port_list);
3777	INIT_LIST_HEAD(&phba->work_list);
3778	init_waitqueue_head(&phba->wait_4_mlo_m_q);
3779
3780	/* Initialize the wait queue head for the kernel thread */
3781	init_waitqueue_head(&phba->work_waitq);
3782
3783	/* Initialize the scsi buffer list used by driver for scsi IO */
3784	spin_lock_init(&phba->scsi_buf_list_lock);
3785	INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
3786
3787	/* Initialize the fabric iocb list */
3788	INIT_LIST_HEAD(&phba->fabric_iocb_list);
3789
3790	/* Initialize list to save ELS buffers */
3791	INIT_LIST_HEAD(&phba->elsbuf);
3792
3793	/* Initialize FCF connection rec list */
3794	INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
3795
3796	return 0;
3797}
3798
3799/**
3800 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
3801 * @phba: pointer to lpfc hba data structure.
3802 *
3803 * This routine is invoked to set up the driver internal resources after the
3804 * device specific resource setup to support the HBA device it attached to.
3805 *
3806 * Return codes
3807 * 	0 - successful
3808 * 	other values - error
3809 **/
3810static int
3811lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
3812{
3813	int error;
3814
3815	/* Startup the kernel thread for this host adapter. */
3816	phba->worker_thread = kthread_run(lpfc_do_work, phba,
3817					  "lpfc_worker_%d", phba->brd_no);
3818	if (IS_ERR(phba->worker_thread)) {
3819		error = PTR_ERR(phba->worker_thread);
3820		return error;
3821	}
3822
3823	return 0;
3824}
3825
3826/**
3827 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
3828 * @phba: pointer to lpfc hba data structure.
3829 *
3830 * This routine is invoked to unset the driver internal resources set up after
3831 * the device specific resource setup for supporting the HBA device it
3832 * attached to.
3833 **/
3834static void
3835lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
3836{
3837	/* Stop kernel worker thread */
3838	kthread_stop(phba->worker_thread);
3839}
3840
3841/**
3842 * lpfc_free_iocb_list - Free iocb list.
3843 * @phba: pointer to lpfc hba data structure.
3844 *
3845 * This routine is invoked to free the driver's IOCB list and memory.
3846 **/
3847static void
3848lpfc_free_iocb_list(struct lpfc_hba *phba)
3849{
3850	struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
3851
3852	spin_lock_irq(&phba->hbalock);
3853	list_for_each_entry_safe(iocbq_entry, iocbq_next,
3854				 &phba->lpfc_iocb_list, list) {
3855		list_del(&iocbq_entry->list);
3856		kfree(iocbq_entry);
3857		phba->total_iocbq_bufs--;
3858	}
3859	spin_unlock_irq(&phba->hbalock);
3860
3861	return;
3862}
3863
3864/**
3865 * lpfc_init_iocb_list - Allocate and initialize iocb list.
3866 * @phba: pointer to lpfc hba data structure.
3867 *
3868 * This routine is invoked to allocate and initizlize the driver's IOCB
3869 * list and set up the IOCB tag array accordingly.
3870 *
3871 * Return codes
3872 *	0 - successful
3873 *	other values - error
3874 **/
3875static int
3876lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
3877{
3878	struct lpfc_iocbq *iocbq_entry = NULL;
3879	uint16_t iotag;
3880	int i;
3881
3882	/* Initialize and populate the iocb list per host.  */
3883	INIT_LIST_HEAD(&phba->lpfc_iocb_list);
3884	for (i = 0; i < iocb_count; i++) {
3885		iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
3886		if (iocbq_entry == NULL) {
3887			printk(KERN_ERR "%s: only allocated %d iocbs of "
3888				"expected %d count. Unloading driver.\n",
3889				__func__, i, LPFC_IOCB_LIST_CNT);
3890			goto out_free_iocbq;
3891		}
3892
3893		iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
3894		if (iotag == 0) {
3895			kfree(iocbq_entry);
3896			printk(KERN_ERR "%s: failed to allocate IOTAG. "
3897				"Unloading driver.\n", __func__);
3898			goto out_free_iocbq;
3899		}
3900		iocbq_entry->sli4_xritag = NO_XRI;
3901
3902		spin_lock_irq(&phba->hbalock);
3903		list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
3904		phba->total_iocbq_bufs++;
3905		spin_unlock_irq(&phba->hbalock);
3906	}
3907
3908	return 0;
3909
3910out_free_iocbq:
3911	lpfc_free_iocb_list(phba);
3912
3913	return -ENOMEM;
3914}
3915
3916/**
3917 * lpfc_free_sgl_list - Free sgl list.
3918 * @phba: pointer to lpfc hba data structure.
3919 *
3920 * This routine is invoked to free the driver's sgl list and memory.
3921 **/
3922static void
3923lpfc_free_sgl_list(struct lpfc_hba *phba)
3924{
3925	struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
3926	LIST_HEAD(sglq_list);
3927	int rc = 0;
3928
3929	spin_lock_irq(&phba->hbalock);
3930	list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
3931	spin_unlock_irq(&phba->hbalock);
3932
3933	list_for_each_entry_safe(sglq_entry, sglq_next,
3934				 &sglq_list, list) {
3935		list_del(&sglq_entry->list);
3936		lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
3937		kfree(sglq_entry);
3938		phba->sli4_hba.total_sglq_bufs--;
3939	}
3940	rc = lpfc_sli4_remove_all_sgl_pages(phba);
3941	if (rc) {
3942		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3943			"2005 Unable to deregister pages from HBA: %x\n", rc);
3944	}
3945	kfree(phba->sli4_hba.lpfc_els_sgl_array);
3946}
3947
3948/**
3949 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
3950 * @phba: pointer to lpfc hba data structure.
3951 *
3952 * This routine is invoked to allocate the driver's active sgl memory.
3953 * This array will hold the sglq_entry's for active IOs.
3954 **/
3955static int
3956lpfc_init_active_sgl_array(struct lpfc_hba *phba)
3957{
3958	int size;
3959	size = sizeof(struct lpfc_sglq *);
3960	size *= phba->sli4_hba.max_cfg_param.max_xri;
3961
3962	phba->sli4_hba.lpfc_sglq_active_list =
3963		kzalloc(size, GFP_KERNEL);
3964	if (!phba->sli4_hba.lpfc_sglq_active_list)
3965		return -ENOMEM;
3966	return 0;
3967}
3968
3969/**
3970 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
3971 * @phba: pointer to lpfc hba data structure.
3972 *
3973 * This routine is invoked to walk through the array of active sglq entries
3974 * and free all of the resources.
3975 * This is just a place holder for now.
3976 **/
3977static void
3978lpfc_free_active_sgl(struct lpfc_hba *phba)
3979{
3980	kfree(phba->sli4_hba.lpfc_sglq_active_list);
3981}
3982
3983/**
3984 * lpfc_init_sgl_list - Allocate and initialize sgl list.
3985 * @phba: pointer to lpfc hba data structure.
3986 *
3987 * This routine is invoked to allocate and initizlize the driver's sgl
3988 * list and set up the sgl xritag tag array accordingly.
3989 *
3990 * Return codes
3991 *	0 - successful
3992 *	other values - error
3993 **/
3994static int
3995lpfc_init_sgl_list(struct lpfc_hba *phba)
3996{
3997	struct lpfc_sglq *sglq_entry = NULL;
3998	int i;
3999	int els_xri_cnt;
4000
4001	els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
4002	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4003				"2400 lpfc_init_sgl_list els %d.\n",
4004				els_xri_cnt);
4005	/* Initialize and populate the sglq list per host/VF. */
4006	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
4007	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
4008
4009	/* Sanity check on XRI management */
4010	if (phba->sli4_hba.max_cfg_param.max_xri <= els_xri_cnt) {
4011		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4012				"2562 No room left for SCSI XRI allocation: "
4013				"max_xri=%d, els_xri=%d\n",
4014				phba->sli4_hba.max_cfg_param.max_xri,
4015				els_xri_cnt);
4016		return -ENOMEM;
4017	}
4018
4019	/* Allocate memory for the ELS XRI management array */
4020	phba->sli4_hba.lpfc_els_sgl_array =
4021			kzalloc((sizeof(struct lpfc_sglq *) * els_xri_cnt),
4022			GFP_KERNEL);
4023
4024	if (!phba->sli4_hba.lpfc_els_sgl_array) {
4025		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4026				"2401 Failed to allocate memory for ELS "
4027				"XRI management array of size %d.\n",
4028				els_xri_cnt);
4029		return -ENOMEM;
4030	}
4031
4032	/* Keep the SCSI XRI into the XRI management array */
4033	phba->sli4_hba.scsi_xri_max =
4034			phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
4035	phba->sli4_hba.scsi_xri_cnt = 0;
4036
4037	phba->sli4_hba.lpfc_scsi_psb_array =
4038			kzalloc((sizeof(struct lpfc_scsi_buf *) *
4039			phba->sli4_hba.scsi_xri_max), GFP_KERNEL);
4040
4041	if (!phba->sli4_hba.lpfc_scsi_psb_array) {
4042		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4043				"2563 Failed to allocate memory for SCSI "
4044				"XRI management array of size %d.\n",
4045				phba->sli4_hba.scsi_xri_max);
4046		kfree(phba->sli4_hba.lpfc_els_sgl_array);
4047		return -ENOMEM;
4048	}
4049
4050	for (i = 0; i < els_xri_cnt; i++) {
4051		sglq_entry = kzalloc(sizeof(struct lpfc_sglq), GFP_KERNEL);
4052		if (sglq_entry == NULL) {
4053			printk(KERN_ERR "%s: only allocated %d sgls of "
4054				"expected %d count. Unloading driver.\n",
4055				__func__, i, els_xri_cnt);
4056			goto out_free_mem;
4057		}
4058
4059		sglq_entry->sli4_xritag = lpfc_sli4_next_xritag(phba);
4060		if (sglq_entry->sli4_xritag == NO_XRI) {
4061			kfree(sglq_entry);
4062			printk(KERN_ERR "%s: failed to allocate XRI.\n"
4063				"Unloading driver.\n", __func__);
4064			goto out_free_mem;
4065		}
4066		sglq_entry->buff_type = GEN_BUFF_TYPE;
4067		sglq_entry->virt = lpfc_mbuf_alloc(phba, 0, &sglq_entry->phys);
4068		if (sglq_entry->virt == NULL) {
4069			kfree(sglq_entry);
4070			printk(KERN_ERR "%s: failed to allocate mbuf.\n"
4071				"Unloading driver.\n", __func__);
4072			goto out_free_mem;
4073		}
4074		sglq_entry->sgl = sglq_entry->virt;
4075		memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
4076
4077		/* The list order is used by later block SGL registraton */
4078		spin_lock_irq(&phba->hbalock);
4079		list_add_tail(&sglq_entry->list, &phba->sli4_hba.lpfc_sgl_list);
4080		phba->sli4_hba.lpfc_els_sgl_array[i] = sglq_entry;
4081		phba->sli4_hba.total_sglq_bufs++;
4082		spin_unlock_irq(&phba->hbalock);
4083	}
4084	return 0;
4085
4086out_free_mem:
4087	kfree(phba->sli4_hba.lpfc_scsi_psb_array);
4088	lpfc_free_sgl_list(phba);
4089	return -ENOMEM;
4090}
4091
4092/**
4093 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
4094 * @phba: pointer to lpfc hba data structure.
4095 *
4096 * This routine is invoked to post rpi header templates to the
4097 * HBA consistent with the SLI-4 interface spec.  This routine
4098 * posts a PAGE_SIZE memory region to the port to hold up to
4099 * PAGE_SIZE modulo 64 rpi context headers.
4100 * No locks are held here because this is an initialization routine
4101 * called only from probe or lpfc_online when interrupts are not
4102 * enabled and the driver is reinitializing the device.
4103 *
4104 * Return codes
4105 * 	0 - successful
4106 * 	ENOMEM - No availble memory
4107 *      EIO - The mailbox failed to complete successfully.
4108 **/
4109int
4110lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
4111{
4112	int rc = 0;
4113	int longs;
4114	uint16_t rpi_count;
4115	struct lpfc_rpi_hdr *rpi_hdr;
4116
4117	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
4118
4119	/*
4120	 * Provision an rpi bitmask range for discovery. The total count
4121	 * is the difference between max and base + 1.
4122	 */
4123	rpi_count = phba->sli4_hba.max_cfg_param.rpi_base +
4124		    phba->sli4_hba.max_cfg_param.max_rpi - 1;
4125
4126	longs = ((rpi_count) + BITS_PER_LONG - 1) / BITS_PER_LONG;
4127	phba->sli4_hba.rpi_bmask = kzalloc(longs * sizeof(unsigned long),
4128					   GFP_KERNEL);
4129	if (!phba->sli4_hba.rpi_bmask)
4130		return -ENOMEM;
4131
4132	rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
4133	if (!rpi_hdr) {
4134		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4135				"0391 Error during rpi post operation\n");
4136		lpfc_sli4_remove_rpis(phba);
4137		rc = -ENODEV;
4138	}
4139
4140	return rc;
4141}
4142
4143/**
4144 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
4145 * @phba: pointer to lpfc hba data structure.
4146 *
4147 * This routine is invoked to allocate a single 4KB memory region to
4148 * support rpis and stores them in the phba.  This single region
4149 * provides support for up to 64 rpis.  The region is used globally
4150 * by the device.
4151 *
4152 * Returns:
4153 *   A valid rpi hdr on success.
4154 *   A NULL pointer on any failure.
4155 **/
4156struct lpfc_rpi_hdr *
4157lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
4158{
4159	uint16_t rpi_limit, curr_rpi_range;
4160	struct lpfc_dmabuf *dmabuf;
4161	struct lpfc_rpi_hdr *rpi_hdr;
4162
4163	rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
4164		    phba->sli4_hba.max_cfg_param.max_rpi - 1;
4165
4166	spin_lock_irq(&phba->hbalock);
4167	curr_rpi_range = phba->sli4_hba.next_rpi;
4168	spin_unlock_irq(&phba->hbalock);
4169
4170	/*
4171	 * The port has a limited number of rpis. The increment here
4172	 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
4173	 * and to allow the full max_rpi range per port.
4174	 */
4175	if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
4176		return NULL;
4177
4178	/*
4179	 * First allocate the protocol header region for the port.  The
4180	 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
4181	 */
4182	dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4183	if (!dmabuf)
4184		return NULL;
4185
4186	dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4187					  LPFC_HDR_TEMPLATE_SIZE,
4188					  &dmabuf->phys,
4189					  GFP_KERNEL);
4190	if (!dmabuf->virt) {
4191		rpi_hdr = NULL;
4192		goto err_free_dmabuf;
4193	}
4194
4195	memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
4196	if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
4197		rpi_hdr = NULL;
4198		goto err_free_coherent;
4199	}
4200
4201	/* Save the rpi header data for cleanup later. */
4202	rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
4203	if (!rpi_hdr)
4204		goto err_free_coherent;
4205
4206	rpi_hdr->dmabuf = dmabuf;
4207	rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
4208	rpi_hdr->page_count = 1;
4209	spin_lock_irq(&phba->hbalock);
4210	rpi_hdr->start_rpi = phba->sli4_hba.next_rpi;
4211	list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
4212
4213	/*
4214	 * The next_rpi stores the next module-64 rpi value to post
4215	 * in any subsequent rpi memory region postings.
4216	 */
4217	phba->sli4_hba.next_rpi += LPFC_RPI_HDR_COUNT;
4218	spin_unlock_irq(&phba->hbalock);
4219	return rpi_hdr;
4220
4221 err_free_coherent:
4222	dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
4223			  dmabuf->virt, dmabuf->phys);
4224 err_free_dmabuf:
4225	kfree(dmabuf);
4226	return NULL;
4227}
4228
4229/**
4230 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
4231 * @phba: pointer to lpfc hba data structure.
4232 *
4233 * This routine is invoked to remove all memory resources allocated
4234 * to support rpis. This routine presumes the caller has released all
4235 * rpis consumed by fabric or port logins and is prepared to have
4236 * the header pages removed.
4237 **/
4238void
4239lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
4240{
4241	struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
4242
4243	list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
4244				 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
4245		list_del(&rpi_hdr->list);
4246		dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
4247				  rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
4248		kfree(rpi_hdr->dmabuf);
4249		kfree(rpi_hdr);
4250	}
4251
4252	phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
4253	memset(phba->sli4_hba.rpi_bmask, 0, sizeof(*phba->sli4_hba.rpi_bmask));
4254}
4255
4256/**
4257 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
4258 * @pdev: pointer to pci device data structure.
4259 *
4260 * This routine is invoked to allocate the driver hba data structure for an
4261 * HBA device. If the allocation is successful, the phba reference to the
4262 * PCI device data structure is set.
4263 *
4264 * Return codes
4265 *      pointer to @phba - successful
4266 *      NULL - error
4267 **/
4268static struct lpfc_hba *
4269lpfc_hba_alloc(struct pci_dev *pdev)
4270{
4271	struct lpfc_hba *phba;
4272
4273	/* Allocate memory for HBA structure */
4274	phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
4275	if (!phba) {
4276		dev_err(&pdev->dev, "failed to allocate hba struct\n");
4277		return NULL;
4278	}
4279
4280	/* Set reference to PCI device in HBA structure */
4281	phba->pcidev = pdev;
4282
4283	/* Assign an unused board number */
4284	phba->brd_no = lpfc_get_instance();
4285	if (phba->brd_no < 0) {
4286		kfree(phba);
4287		return NULL;
4288	}
4289
4290	mutex_init(&phba->ct_event_mutex);
4291	INIT_LIST_HEAD(&phba->ct_ev_waiters);
4292
4293	return phba;
4294}
4295
4296/**
4297 * lpfc_hba_free - Free driver hba data structure with a device.
4298 * @phba: pointer to lpfc hba data structure.
4299 *
4300 * This routine is invoked to free the driver hba data structure with an
4301 * HBA device.
4302 **/
4303static void
4304lpfc_hba_free(struct lpfc_hba *phba)
4305{
4306	/* Release the driver assigned board number */
4307	idr_remove(&lpfc_hba_index, phba->brd_no);
4308
4309	kfree(phba);
4310	return;
4311}
4312
4313/**
4314 * lpfc_create_shost - Create hba physical port with associated scsi host.
4315 * @phba: pointer to lpfc hba data structure.
4316 *
4317 * This routine is invoked to create HBA physical port and associate a SCSI
4318 * host with it.
4319 *
4320 * Return codes
4321 *      0 - successful
4322 *      other values - error
4323 **/
4324static int
4325lpfc_create_shost(struct lpfc_hba *phba)
4326{
4327	struct lpfc_vport *vport;
4328	struct Scsi_Host  *shost;
4329
4330	/* Initialize HBA FC structure */
4331	phba->fc_edtov = FF_DEF_EDTOV;
4332	phba->fc_ratov = FF_DEF_RATOV;
4333	phba->fc_altov = FF_DEF_ALTOV;
4334	phba->fc_arbtov = FF_DEF_ARBTOV;
4335
4336	vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
4337	if (!vport)
4338		return -ENODEV;
4339
4340	shost = lpfc_shost_from_vport(vport);
4341	phba->pport = vport;
4342	lpfc_debugfs_initialize(vport);
4343	/* Put reference to SCSI host to driver's device private data */
4344	pci_set_drvdata(phba->pcidev, shost);
4345
4346	return 0;
4347}
4348
4349/**
4350 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
4351 * @phba: pointer to lpfc hba data structure.
4352 *
4353 * This routine is invoked to destroy HBA physical port and the associated
4354 * SCSI host.
4355 **/
4356static void
4357lpfc_destroy_shost(struct lpfc_hba *phba)
4358{
4359	struct lpfc_vport *vport = phba->pport;
4360
4361	/* Destroy physical port that associated with the SCSI host */
4362	destroy_port(vport);
4363
4364	return;
4365}
4366
4367/**
4368 * lpfc_setup_bg - Setup Block guard structures and debug areas.
4369 * @phba: pointer to lpfc hba data structure.
4370 * @shost: the shost to be used to detect Block guard settings.
4371 *
4372 * This routine sets up the local Block guard protocol settings for @shost.
4373 * This routine also allocates memory for debugging bg buffers.
4374 **/
4375static void
4376lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
4377{
4378	int pagecnt = 10;
4379	if (lpfc_prot_mask && lpfc_prot_guard) {
4380		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4381				"1478 Registering BlockGuard with the "
4382				"SCSI layer\n");
4383		scsi_host_set_prot(shost, lpfc_prot_mask);
4384		scsi_host_set_guard(shost, lpfc_prot_guard);
4385	}
4386	if (!_dump_buf_data) {
4387		while (pagecnt) {
4388			spin_lock_init(&_dump_buf_lock);
4389			_dump_buf_data =
4390				(char *) __get_free_pages(GFP_KERNEL, pagecnt);
4391			if (_dump_buf_data) {
4392				lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4393					"9043 BLKGRD: allocated %d pages for "
4394				       "_dump_buf_data at 0x%p\n",
4395				       (1 << pagecnt), _dump_buf_data);
4396				_dump_buf_data_order = pagecnt;
4397				memset(_dump_buf_data, 0,
4398				       ((1 << PAGE_SHIFT) << pagecnt));
4399				break;
4400			} else
4401				--pagecnt;
4402		}
4403		if (!_dump_buf_data_order)
4404			lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4405				"9044 BLKGRD: ERROR unable to allocate "
4406			       "memory for hexdump\n");
4407	} else
4408		lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4409			"9045 BLKGRD: already allocated _dump_buf_data=0x%p"
4410		       "\n", _dump_buf_data);
4411	if (!_dump_buf_dif) {
4412		while (pagecnt) {
4413			_dump_buf_dif =
4414				(char *) __get_free_pages(GFP_KERNEL, pagecnt);
4415			if (_dump_buf_dif) {
4416				lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4417					"9046 BLKGRD: allocated %d pages for "
4418				       "_dump_buf_dif at 0x%p\n",
4419				       (1 << pagecnt), _dump_buf_dif);
4420				_dump_buf_dif_order = pagecnt;
4421				memset(_dump_buf_dif, 0,
4422				       ((1 << PAGE_SHIFT) << pagecnt));
4423				break;
4424			} else
4425				--pagecnt;
4426		}
4427		if (!_dump_buf_dif_order)
4428			lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4429			"9047 BLKGRD: ERROR unable to allocate "
4430			       "memory for hexdump\n");
4431	} else
4432		lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4433			"9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
4434		       _dump_buf_dif);
4435}
4436
4437/**
4438 * lpfc_post_init_setup - Perform necessary device post initialization setup.
4439 * @phba: pointer to lpfc hba data structure.
4440 *
4441 * This routine is invoked to perform all the necessary post initialization
4442 * setup for the device.
4443 **/
4444static void
4445lpfc_post_init_setup(struct lpfc_hba *phba)
4446{
4447	struct Scsi_Host  *shost;
4448	struct lpfc_adapter_event_header adapter_event;
4449
4450	/* Get the default values for Model Name and Description */
4451	lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
4452
4453	/*
4454	 * hba setup may have changed the hba_queue_depth so we need to
4455	 * adjust the value of can_queue.
4456	 */
4457	shost = pci_get_drvdata(phba->pcidev);
4458	shost->can_queue = phba->cfg_hba_queue_depth - 10;
4459	if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
4460		lpfc_setup_bg(phba, shost);
4461
4462	lpfc_host_attrib_init(shost);
4463
4464	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
4465		spin_lock_irq(shost->host_lock);
4466		lpfc_poll_start_timer(phba);
4467		spin_unlock_irq(shost->host_lock);
4468	}
4469
4470	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4471			"0428 Perform SCSI scan\n");
4472	/* Send board arrival event to upper layer */
4473	adapter_event.event_type = FC_REG_ADAPTER_EVENT;
4474	adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
4475	fc_host_post_vendor_event(shost, fc_get_event_number(),
4476				  sizeof(adapter_event),
4477				  (char *) &adapter_event,
4478				  LPFC_NL_VENDOR_ID);
4479	return;
4480}
4481
4482/**
4483 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
4484 * @phba: pointer to lpfc hba data structure.
4485 *
4486 * This routine is invoked to set up the PCI device memory space for device
4487 * with SLI-3 interface spec.
4488 *
4489 * Return codes
4490 * 	0 - successful
4491 * 	other values - error
4492 **/
4493static int
4494lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
4495{
4496	struct pci_dev *pdev;
4497	unsigned long bar0map_len, bar2map_len;
4498	int i, hbq_count;
4499	void *ptr;
4500	int error = -ENODEV;
4501
4502	/* Obtain PCI device reference */
4503	if (!phba->pcidev)
4504		return error;
4505	else
4506		pdev = phba->pcidev;
4507
4508	/* Set the device DMA mask size */
4509	if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0)
4510		if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
4511			return error;
4512
4513	/* Get the bus address of Bar0 and Bar2 and the number of bytes
4514	 * required by each mapping.
4515	 */
4516	phba->pci_bar0_map = pci_resource_start(pdev, 0);
4517	bar0map_len = pci_resource_len(pdev, 0);
4518
4519	phba->pci_bar2_map = pci_resource_start(pdev, 2);
4520	bar2map_len = pci_resource_len(pdev, 2);
4521
4522	/* Map HBA SLIM to a kernel virtual address. */
4523	phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
4524	if (!phba->slim_memmap_p) {
4525		dev_printk(KERN_ERR, &pdev->dev,
4526			   "ioremap failed for SLIM memory.\n");
4527		goto out;
4528	}
4529
4530	/* Map HBA Control Registers to a kernel virtual address. */
4531	phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
4532	if (!phba->ctrl_regs_memmap_p) {
4533		dev_printk(KERN_ERR, &pdev->dev,
4534			   "ioremap failed for HBA control registers.\n");
4535		goto out_iounmap_slim;
4536	}
4537
4538	/* Allocate memory for SLI-2 structures */
4539	phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
4540					       SLI2_SLIM_SIZE,
4541					       &phba->slim2p.phys,
4542					       GFP_KERNEL);
4543	if (!phba->slim2p.virt)
4544		goto out_iounmap;
4545
4546	memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
4547	phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
4548	phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
4549	phba->IOCBs = (phba->slim2p.virt +
4550		       offsetof(struct lpfc_sli2_slim, IOCBs));
4551
4552	phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
4553						 lpfc_sli_hbq_size(),
4554						 &phba->hbqslimp.phys,
4555						 GFP_KERNEL);
4556	if (!phba->hbqslimp.virt)
4557		goto out_free_slim;
4558
4559	hbq_count = lpfc_sli_hbq_count();
4560	ptr = phba->hbqslimp.virt;
4561	for (i = 0; i < hbq_count; ++i) {
4562		phba->hbqs[i].hbq_virt = ptr;
4563		INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
4564		ptr += (lpfc_hbq_defs[i]->entry_count *
4565			sizeof(struct lpfc_hbq_entry));
4566	}
4567	phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
4568	phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
4569
4570	memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
4571
4572	INIT_LIST_HEAD(&phba->rb_pend_list);
4573
4574	phba->MBslimaddr = phba->slim_memmap_p;
4575	phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
4576	phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
4577	phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
4578	phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
4579
4580	return 0;
4581
4582out_free_slim:
4583	dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
4584			  phba->slim2p.virt, phba->slim2p.phys);
4585out_iounmap:
4586	iounmap(phba->ctrl_regs_memmap_p);
4587out_iounmap_slim:
4588	iounmap(phba->slim_memmap_p);
4589out:
4590	return error;
4591}
4592
4593/**
4594 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
4595 * @phba: pointer to lpfc hba data structure.
4596 *
4597 * This routine is invoked to unset the PCI device memory space for device
4598 * with SLI-3 interface spec.
4599 **/
4600static void
4601lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
4602{
4603	struct pci_dev *pdev;
4604
4605	/* Obtain PCI device reference */
4606	if (!phba->pcidev)
4607		return;
4608	else
4609		pdev = phba->pcidev;
4610
4611	/* Free coherent DMA memory allocated */
4612	dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
4613			  phba->hbqslimp.virt, phba->hbqslimp.phys);
4614	dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
4615			  phba->slim2p.virt, phba->slim2p.phys);
4616
4617	/* I/O memory unmap */
4618	iounmap(phba->ctrl_regs_memmap_p);
4619	iounmap(phba->slim_memmap_p);
4620
4621	return;
4622}
4623
4624/**
4625 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
4626 * @phba: pointer to lpfc hba data structure.
4627 *
4628 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
4629 * done and check status.
4630 *
4631 * Return 0 if successful, otherwise -ENODEV.
4632 **/
4633int
4634lpfc_sli4_post_status_check(struct lpfc_hba *phba)
4635{
4636	struct lpfc_register sta_reg, uerrlo_reg, uerrhi_reg, scratchpad;
4637	int i, port_error = -ENODEV;
4638
4639	if (!phba->sli4_hba.STAregaddr)
4640		return -ENODEV;
4641
4642	/* Wait up to 30 seconds for the SLI Port POST done and ready */
4643	for (i = 0; i < 3000; i++) {
4644		sta_reg.word0 = readl(phba->sli4_hba.STAregaddr);
4645		/* Encounter fatal POST error, break out */
4646		if (bf_get(lpfc_hst_state_perr, &sta_reg)) {
4647			port_error = -ENODEV;
4648			break;
4649		}
4650		if (LPFC_POST_STAGE_ARMFW_READY ==
4651		    bf_get(lpfc_hst_state_port_status, &sta_reg)) {
4652			port_error = 0;
4653			break;
4654		}
4655		msleep(10);
4656	}
4657
4658	if (port_error)
4659		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4660			"1408 Failure HBA POST Status: sta_reg=0x%x, "
4661			"perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, xrom=x%x, "
4662			"dl=x%x, pstatus=x%x\n", sta_reg.word0,
4663			bf_get(lpfc_hst_state_perr, &sta_reg),
4664			bf_get(lpfc_hst_state_sfi, &sta_reg),
4665			bf_get(lpfc_hst_state_nip, &sta_reg),
4666			bf_get(lpfc_hst_state_ipc, &sta_reg),
4667			bf_get(lpfc_hst_state_xrom, &sta_reg),
4668			bf_get(lpfc_hst_state_dl, &sta_reg),
4669			bf_get(lpfc_hst_state_port_status, &sta_reg));
4670
4671	/* Log device information */
4672	scratchpad.word0 =  readl(phba->sli4_hba.SCRATCHPADregaddr);
4673	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4674			"2534 Device Info: ChipType=0x%x, SliRev=0x%x, "
4675			"FeatureL1=0x%x, FeatureL2=0x%x\n",
4676			bf_get(lpfc_scratchpad_chiptype, &scratchpad),
4677			bf_get(lpfc_scratchpad_slirev, &scratchpad),
4678			bf_get(lpfc_scratchpad_featurelevel1, &scratchpad),
4679			bf_get(lpfc_scratchpad_featurelevel2, &scratchpad));
4680	phba->sli4_hba.ue_mask_lo = readl(phba->sli4_hba.UEMASKLOregaddr);
4681	phba->sli4_hba.ue_mask_hi = readl(phba->sli4_hba.UEMASKHIregaddr);
4682	/* With uncoverable error, log the error message and return error */
4683	uerrlo_reg.word0 = readl(phba->sli4_hba.UERRLOregaddr);
4684	uerrhi_reg.word0 = readl(phba->sli4_hba.UERRHIregaddr);
4685	if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
4686	    (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
4687		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4688				"1422 HBA Unrecoverable error: "
4689				"uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
4690				"ue_mask_lo_reg=0x%x, ue_mask_hi_reg=0x%x\n",
4691				uerrlo_reg.word0, uerrhi_reg.word0,
4692				phba->sli4_hba.ue_mask_lo,
4693				phba->sli4_hba.ue_mask_hi);
4694		return -ENODEV;
4695	}
4696
4697	return port_error;
4698}
4699
4700/**
4701 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
4702 * @phba: pointer to lpfc hba data structure.
4703 *
4704 * This routine is invoked to set up SLI4 BAR0 PCI config space register
4705 * memory map.
4706 **/
4707static void
4708lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba)
4709{
4710	phba->sli4_hba.UERRLOregaddr = phba->sli4_hba.conf_regs_memmap_p +
4711					LPFC_UERR_STATUS_LO;
4712	phba->sli4_hba.UERRHIregaddr = phba->sli4_hba.conf_regs_memmap_p +
4713					LPFC_UERR_STATUS_HI;
4714	phba->sli4_hba.UEMASKLOregaddr = phba->sli4_hba.conf_regs_memmap_p +
4715					LPFC_UE_MASK_LO;
4716	phba->sli4_hba.UEMASKHIregaddr = phba->sli4_hba.conf_regs_memmap_p +
4717					LPFC_UE_MASK_HI;
4718	phba->sli4_hba.SCRATCHPADregaddr = phba->sli4_hba.conf_regs_memmap_p +
4719					LPFC_SCRATCHPAD;
4720}
4721
4722/**
4723 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
4724 * @phba: pointer to lpfc hba data structure.
4725 *
4726 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
4727 * memory map.
4728 **/
4729static void
4730lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
4731{
4732
4733	phba->sli4_hba.STAregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4734				    LPFC_HST_STATE;
4735	phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4736				    LPFC_HST_ISR0;
4737	phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4738				    LPFC_HST_IMR0;
4739	phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4740				     LPFC_HST_ISCR0;
4741	return;
4742}
4743
4744/**
4745 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
4746 * @phba: pointer to lpfc hba data structure.
4747 * @vf: virtual function number
4748 *
4749 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
4750 * based on the given viftual function number, @vf.
4751 *
4752 * Return 0 if successful, otherwise -ENODEV.
4753 **/
4754static int
4755lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
4756{
4757	if (vf > LPFC_VIR_FUNC_MAX)
4758		return -ENODEV;
4759
4760	phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4761				vf * LPFC_VFR_PAGE_SIZE + LPFC_RQ_DOORBELL);
4762	phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4763				vf * LPFC_VFR_PAGE_SIZE + LPFC_WQ_DOORBELL);
4764	phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4765				vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
4766	phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4767				vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
4768	phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4769				vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
4770	return 0;
4771}
4772
4773/**
4774 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
4775 * @phba: pointer to lpfc hba data structure.
4776 *
4777 * This routine is invoked to create the bootstrap mailbox
4778 * region consistent with the SLI-4 interface spec.  This
4779 * routine allocates all memory necessary to communicate
4780 * mailbox commands to the port and sets up all alignment
4781 * needs.  No locks are expected to be held when calling
4782 * this routine.
4783 *
4784 * Return codes
4785 * 	0 - successful
4786 * 	ENOMEM - could not allocated memory.
4787 **/
4788static int
4789lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
4790{
4791	uint32_t bmbx_size;
4792	struct lpfc_dmabuf *dmabuf;
4793	struct dma_address *dma_address;
4794	uint32_t pa_addr;
4795	uint64_t phys_addr;
4796
4797	dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4798	if (!dmabuf)
4799		return -ENOMEM;
4800
4801	/*
4802	 * The bootstrap mailbox region is comprised of 2 parts
4803	 * plus an alignment restriction of 16 bytes.
4804	 */
4805	bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
4806	dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4807					  bmbx_size,
4808					  &dmabuf->phys,
4809					  GFP_KERNEL);
4810	if (!dmabuf->virt) {
4811		kfree(dmabuf);
4812		return -ENOMEM;
4813	}
4814	memset(dmabuf->virt, 0, bmbx_size);
4815
4816	/*
4817	 * Initialize the bootstrap mailbox pointers now so that the register
4818	 * operations are simple later.  The mailbox dma address is required
4819	 * to be 16-byte aligned.  Also align the virtual memory as each
4820	 * maibox is copied into the bmbx mailbox region before issuing the
4821	 * command to the port.
4822	 */
4823	phba->sli4_hba.bmbx.dmabuf = dmabuf;
4824	phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
4825
4826	phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
4827					      LPFC_ALIGN_16_BYTE);
4828	phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
4829					      LPFC_ALIGN_16_BYTE);
4830
4831	/*
4832	 * Set the high and low physical addresses now.  The SLI4 alignment
4833	 * requirement is 16 bytes and the mailbox is posted to the port
4834	 * as two 30-bit addresses.  The other data is a bit marking whether
4835	 * the 30-bit address is the high or low address.
4836	 * Upcast bmbx aphys to 64bits so shift instruction compiles
4837	 * clean on 32 bit machines.
4838	 */
4839	dma_address = &phba->sli4_hba.bmbx.dma_address;
4840	phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
4841	pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
4842	dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
4843					   LPFC_BMBX_BIT1_ADDR_HI);
4844
4845	pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
4846	dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
4847					   LPFC_BMBX_BIT1_ADDR_LO);
4848	return 0;
4849}
4850
4851/**
4852 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
4853 * @phba: pointer to lpfc hba data structure.
4854 *
4855 * This routine is invoked to teardown the bootstrap mailbox
4856 * region and release all host resources. This routine requires
4857 * the caller to ensure all mailbox commands recovered, no
4858 * additional mailbox comands are sent, and interrupts are disabled
4859 * before calling this routine.
4860 *
4861 **/
4862static void
4863lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
4864{
4865	dma_free_coherent(&phba->pcidev->dev,
4866			  phba->sli4_hba.bmbx.bmbx_size,
4867			  phba->sli4_hba.bmbx.dmabuf->virt,
4868			  phba->sli4_hba.bmbx.dmabuf->phys);
4869
4870	kfree(phba->sli4_hba.bmbx.dmabuf);
4871	memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
4872}
4873
4874/**
4875 * lpfc_sli4_read_config - Get the config parameters.
4876 * @phba: pointer to lpfc hba data structure.
4877 *
4878 * This routine is invoked to read the configuration parameters from the HBA.
4879 * The configuration parameters are used to set the base and maximum values
4880 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
4881 * allocation for the port.
4882 *
4883 * Return codes
4884 * 	0 - successful
4885 * 	ENOMEM - No availble memory
4886 *      EIO - The mailbox failed to complete successfully.
4887 **/
4888static int
4889lpfc_sli4_read_config(struct lpfc_hba *phba)
4890{
4891	LPFC_MBOXQ_t *pmb;
4892	struct lpfc_mbx_read_config *rd_config;
4893	uint32_t rc = 0;
4894
4895	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4896	if (!pmb) {
4897		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4898				"2011 Unable to allocate memory for issuing "
4899				"SLI_CONFIG_SPECIAL mailbox command\n");
4900		return -ENOMEM;
4901	}
4902
4903	lpfc_read_config(phba, pmb);
4904
4905	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
4906	if (rc != MBX_SUCCESS) {
4907		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4908			"2012 Mailbox failed , mbxCmd x%x "
4909			"READ_CONFIG, mbxStatus x%x\n",
4910			bf_get(lpfc_mqe_command, &pmb->u.mqe),
4911			bf_get(lpfc_mqe_status, &pmb->u.mqe));
4912		rc = -EIO;
4913	} else {
4914		rd_config = &pmb->u.mqe.un.rd_config;
4915		phba->sli4_hba.max_cfg_param.max_xri =
4916			bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
4917		phba->sli4_hba.max_cfg_param.xri_base =
4918			bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
4919		phba->sli4_hba.max_cfg_param.max_vpi =
4920			bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
4921		phba->sli4_hba.max_cfg_param.vpi_base =
4922			bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
4923		phba->sli4_hba.max_cfg_param.max_rpi =
4924			bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
4925		phba->sli4_hba.max_cfg_param.rpi_base =
4926			bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
4927		phba->sli4_hba.max_cfg_param.max_vfi =
4928			bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
4929		phba->sli4_hba.max_cfg_param.vfi_base =
4930			bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
4931		phba->sli4_hba.max_cfg_param.max_fcfi =
4932			bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
4933		phba->sli4_hba.max_cfg_param.fcfi_base =
4934			bf_get(lpfc_mbx_rd_conf_fcfi_base, rd_config);
4935		phba->sli4_hba.max_cfg_param.max_eq =
4936			bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
4937		phba->sli4_hba.max_cfg_param.max_rq =
4938			bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
4939		phba->sli4_hba.max_cfg_param.max_wq =
4940			bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
4941		phba->sli4_hba.max_cfg_param.max_cq =
4942			bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
4943		phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
4944		phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
4945		phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
4946		phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
4947		phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
4948		phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
4949				(phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
4950		phba->max_vports = phba->max_vpi;
4951		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4952				"2003 cfg params XRI(B:%d M:%d), "
4953				"VPI(B:%d M:%d) "
4954				"VFI(B:%d M:%d) "
4955				"RPI(B:%d M:%d) "
4956				"FCFI(B:%d M:%d)\n",
4957				phba->sli4_hba.max_cfg_param.xri_base,
4958				phba->sli4_hba.max_cfg_param.max_xri,
4959				phba->sli4_hba.max_cfg_param.vpi_base,
4960				phba->sli4_hba.max_cfg_param.max_vpi,
4961				phba->sli4_hba.max_cfg_param.vfi_base,
4962				phba->sli4_hba.max_cfg_param.max_vfi,
4963				phba->sli4_hba.max_cfg_param.rpi_base,
4964				phba->sli4_hba.max_cfg_param.max_rpi,
4965				phba->sli4_hba.max_cfg_param.fcfi_base,
4966				phba->sli4_hba.max_cfg_param.max_fcfi);
4967	}
4968	mempool_free(pmb, phba->mbox_mem_pool);
4969
4970	/* Reset the DFT_HBA_Q_DEPTH to the max xri  */
4971	if (phba->cfg_hba_queue_depth > (phba->sli4_hba.max_cfg_param.max_xri))
4972		phba->cfg_hba_queue_depth =
4973				phba->sli4_hba.max_cfg_param.max_xri;
4974	return rc;
4975}
4976
4977/**
4978 * lpfc_dev_endian_order_setup - Notify the port of the host's endian order.
4979 * @phba: pointer to lpfc hba data structure.
4980 *
4981 * This routine is invoked to setup the host-side endian order to the
4982 * HBA consistent with the SLI-4 interface spec.
4983 *
4984 * Return codes
4985 * 	0 - successful
4986 * 	ENOMEM - No availble memory
4987 *      EIO - The mailbox failed to complete successfully.
4988 **/
4989static int
4990lpfc_setup_endian_order(struct lpfc_hba *phba)
4991{
4992	LPFC_MBOXQ_t *mboxq;
4993	uint32_t rc = 0;
4994	uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
4995				      HOST_ENDIAN_HIGH_WORD1};
4996
4997	mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4998	if (!mboxq) {
4999		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5000				"0492 Unable to allocate memory for issuing "
5001				"SLI_CONFIG_SPECIAL mailbox command\n");
5002		return -ENOMEM;
5003	}
5004
5005	/*
5006	 * The SLI4_CONFIG_SPECIAL mailbox command requires the first two
5007	 * words to contain special data values and no other data.
5008	 */
5009	memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
5010	memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
5011	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5012	if (rc != MBX_SUCCESS) {
5013		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5014				"0493 SLI_CONFIG_SPECIAL mailbox failed with "
5015				"status x%x\n",
5016				rc);
5017		rc = -EIO;
5018	}
5019
5020	mempool_free(mboxq, phba->mbox_mem_pool);
5021	return rc;
5022}
5023
5024/**
5025 * lpfc_sli4_queue_create - Create all the SLI4 queues
5026 * @phba: pointer to lpfc hba data structure.
5027 *
5028 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
5029 * operation. For each SLI4 queue type, the parameters such as queue entry
5030 * count (queue depth) shall be taken from the module parameter. For now,
5031 * we just use some constant number as place holder.
5032 *
5033 * Return codes
5034 *      0 - successful
5035 *      ENOMEM - No availble memory
5036 *      EIO - The mailbox failed to complete successfully.
5037 **/
5038static int
5039lpfc_sli4_queue_create(struct lpfc_hba *phba)
5040{
5041	struct lpfc_queue *qdesc;
5042	int fcp_eqidx, fcp_cqidx, fcp_wqidx;
5043	int cfg_fcp_wq_count;
5044	int cfg_fcp_eq_count;
5045
5046	/*
5047	 * Sanity check for confiugred queue parameters against the run-time
5048	 * device parameters
5049	 */
5050
5051	/* Sanity check on FCP fast-path WQ parameters */
5052	cfg_fcp_wq_count = phba->cfg_fcp_wq_count;
5053	if (cfg_fcp_wq_count >
5054	    (phba->sli4_hba.max_cfg_param.max_wq - LPFC_SP_WQN_DEF)) {
5055		cfg_fcp_wq_count = phba->sli4_hba.max_cfg_param.max_wq -
5056				   LPFC_SP_WQN_DEF;
5057		if (cfg_fcp_wq_count < LPFC_FP_WQN_MIN) {
5058			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5059					"2581 Not enough WQs (%d) from "
5060					"the pci function for supporting "
5061					"FCP WQs (%d)\n",
5062					phba->sli4_hba.max_cfg_param.max_wq,
5063					phba->cfg_fcp_wq_count);
5064			goto out_error;
5065		}
5066		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5067				"2582 Not enough WQs (%d) from the pci "
5068				"function for supporting the requested "
5069				"FCP WQs (%d), the actual FCP WQs can "
5070				"be supported: %d\n",
5071				phba->sli4_hba.max_cfg_param.max_wq,
5072				phba->cfg_fcp_wq_count, cfg_fcp_wq_count);
5073	}
5074	/* The actual number of FCP work queues adopted */
5075	phba->cfg_fcp_wq_count = cfg_fcp_wq_count;
5076
5077	/* Sanity check on FCP fast-path EQ parameters */
5078	cfg_fcp_eq_count = phba->cfg_fcp_eq_count;
5079	if (cfg_fcp_eq_count >
5080	    (phba->sli4_hba.max_cfg_param.max_eq - LPFC_SP_EQN_DEF)) {
5081		cfg_fcp_eq_count = phba->sli4_hba.max_cfg_param.max_eq -
5082				   LPFC_SP_EQN_DEF;
5083		if (cfg_fcp_eq_count < LPFC_FP_EQN_MIN) {
5084			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5085					"2574 Not enough EQs (%d) from the "
5086					"pci function for supporting FCP "
5087					"EQs (%d)\n",
5088					phba->sli4_hba.max_cfg_param.max_eq,
5089					phba->cfg_fcp_eq_count);
5090			goto out_error;
5091		}
5092		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5093				"2575 Not enough EQs (%d) from the pci "
5094				"function for supporting the requested "
5095				"FCP EQs (%d), the actual FCP EQs can "
5096				"be supported: %d\n",
5097				phba->sli4_hba.max_cfg_param.max_eq,
5098				phba->cfg_fcp_eq_count, cfg_fcp_eq_count);
5099	}
5100	/* It does not make sense to have more EQs than WQs */
5101	if (cfg_fcp_eq_count > phba->cfg_fcp_wq_count) {
5102		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5103				"2593 The FCP EQ count(%d) cannot be greater "
5104				"than the FCP WQ count(%d), limiting the "
5105				"FCP EQ count to %d\n", cfg_fcp_eq_count,
5106				phba->cfg_fcp_wq_count,
5107				phba->cfg_fcp_wq_count);
5108		cfg_fcp_eq_count = phba->cfg_fcp_wq_count;
5109	}
5110	/* The actual number of FCP event queues adopted */
5111	phba->cfg_fcp_eq_count = cfg_fcp_eq_count;
5112	/* The overall number of event queues used */
5113	phba->sli4_hba.cfg_eqn = phba->cfg_fcp_eq_count + LPFC_SP_EQN_DEF;
5114
5115	/*
5116	 * Create Event Queues (EQs)
5117	 */
5118
5119	/* Get EQ depth from module parameter, fake the default for now */
5120	phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
5121	phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
5122
5123	/* Create slow path event queue */
5124	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5125				      phba->sli4_hba.eq_ecount);
5126	if (!qdesc) {
5127		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5128				"0496 Failed allocate slow-path EQ\n");
5129		goto out_error;
5130	}
5131	phba->sli4_hba.sp_eq = qdesc;
5132
5133	/* Create fast-path FCP Event Queue(s) */
5134	phba->sli4_hba.fp_eq = kzalloc((sizeof(struct lpfc_queue *) *
5135			       phba->cfg_fcp_eq_count), GFP_KERNEL);
5136	if (!phba->sli4_hba.fp_eq) {
5137		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5138				"2576 Failed allocate memory for fast-path "
5139				"EQ record array\n");
5140		goto out_free_sp_eq;
5141	}
5142	for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
5143		qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5144					      phba->sli4_hba.eq_ecount);
5145		if (!qdesc) {
5146			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5147					"0497 Failed allocate fast-path EQ\n");
5148			goto out_free_fp_eq;
5149		}
5150		phba->sli4_hba.fp_eq[fcp_eqidx] = qdesc;
5151	}
5152
5153	/*
5154	 * Create Complete Queues (CQs)
5155	 */
5156
5157	/* Get CQ depth from module parameter, fake the default for now */
5158	phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
5159	phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
5160
5161	/* Create slow-path Mailbox Command Complete Queue */
5162	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5163				      phba->sli4_hba.cq_ecount);
5164	if (!qdesc) {
5165		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5166				"0500 Failed allocate slow-path mailbox CQ\n");
5167		goto out_free_fp_eq;
5168	}
5169	phba->sli4_hba.mbx_cq = qdesc;
5170
5171	/* Create slow-path ELS Complete Queue */
5172	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5173				      phba->sli4_hba.cq_ecount);
5174	if (!qdesc) {
5175		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5176				"0501 Failed allocate slow-path ELS CQ\n");
5177		goto out_free_mbx_cq;
5178	}
5179	phba->sli4_hba.els_cq = qdesc;
5180
5181
5182	/* Create fast-path FCP Completion Queue(s), one-to-one with EQs */
5183	phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
5184				phba->cfg_fcp_eq_count), GFP_KERNEL);
5185	if (!phba->sli4_hba.fcp_cq) {
5186		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5187				"2577 Failed allocate memory for fast-path "
5188				"CQ record array\n");
5189		goto out_free_els_cq;
5190	}
5191	for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
5192		qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5193					      phba->sli4_hba.cq_ecount);
5194		if (!qdesc) {
5195			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5196					"0499 Failed allocate fast-path FCP "
5197					"CQ (%d)\n", fcp_cqidx);
5198			goto out_free_fcp_cq;
5199		}
5200		phba->sli4_hba.fcp_cq[fcp_cqidx] = qdesc;
5201	}
5202
5203	/* Create Mailbox Command Queue */
5204	phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
5205	phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
5206
5207	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
5208				      phba->sli4_hba.mq_ecount);
5209	if (!qdesc) {
5210		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5211				"0505 Failed allocate slow-path MQ\n");
5212		goto out_free_fcp_cq;
5213	}
5214	phba->sli4_hba.mbx_wq = qdesc;
5215
5216	/*
5217	 * Create all the Work Queues (WQs)
5218	 */
5219	phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
5220	phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
5221
5222	/* Create slow-path ELS Work Queue */
5223	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
5224				      phba->sli4_hba.wq_ecount);
5225	if (!qdesc) {
5226		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5227				"0504 Failed allocate slow-path ELS WQ\n");
5228		goto out_free_mbx_wq;
5229	}
5230	phba->sli4_hba.els_wq = qdesc;
5231
5232	/* Create fast-path FCP Work Queue(s) */
5233	phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
5234				phba->cfg_fcp_wq_count), GFP_KERNEL);
5235	if (!phba->sli4_hba.fcp_wq) {
5236		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5237				"2578 Failed allocate memory for fast-path "
5238				"WQ record array\n");
5239		goto out_free_els_wq;
5240	}
5241	for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
5242		qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
5243					      phba->sli4_hba.wq_ecount);
5244		if (!qdesc) {
5245			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5246					"0503 Failed allocate fast-path FCP "
5247					"WQ (%d)\n", fcp_wqidx);
5248			goto out_free_fcp_wq;
5249		}
5250		phba->sli4_hba.fcp_wq[fcp_wqidx] = qdesc;
5251	}
5252
5253	/*
5254	 * Create Receive Queue (RQ)
5255	 */
5256	phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
5257	phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
5258
5259	/* Create Receive Queue for header */
5260	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
5261				      phba->sli4_hba.rq_ecount);
5262	if (!qdesc) {
5263		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5264				"0506 Failed allocate receive HRQ\n");
5265		goto out_free_fcp_wq;
5266	}
5267	phba->sli4_hba.hdr_rq = qdesc;
5268
5269	/* Create Receive Queue for data */
5270	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
5271				      phba->sli4_hba.rq_ecount);
5272	if (!qdesc) {
5273		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5274				"0507 Failed allocate receive DRQ\n");
5275		goto out_free_hdr_rq;
5276	}
5277	phba->sli4_hba.dat_rq = qdesc;
5278
5279	return 0;
5280
5281out_free_hdr_rq:
5282	lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
5283	phba->sli4_hba.hdr_rq = NULL;
5284out_free_fcp_wq:
5285	for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--) {
5286		lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_wqidx]);
5287		phba->sli4_hba.fcp_wq[fcp_wqidx] = NULL;
5288	}
5289	kfree(phba->sli4_hba.fcp_wq);
5290out_free_els_wq:
5291	lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
5292	phba->sli4_hba.els_wq = NULL;
5293out_free_mbx_wq:
5294	lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
5295	phba->sli4_hba.mbx_wq = NULL;
5296out_free_fcp_cq:
5297	for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--) {
5298		lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_cqidx]);
5299		phba->sli4_hba.fcp_cq[fcp_cqidx] = NULL;
5300	}
5301	kfree(phba->sli4_hba.fcp_cq);
5302out_free_els_cq:
5303	lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
5304	phba->sli4_hba.els_cq = NULL;
5305out_free_mbx_cq:
5306	lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
5307	phba->sli4_hba.mbx_cq = NULL;
5308out_free_fp_eq:
5309	for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--) {
5310		lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_eqidx]);
5311		phba->sli4_hba.fp_eq[fcp_eqidx] = NULL;
5312	}
5313	kfree(phba->sli4_hba.fp_eq);
5314out_free_sp_eq:
5315	lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
5316	phba->sli4_hba.sp_eq = NULL;
5317out_error:
5318	return -ENOMEM;
5319}
5320
5321/**
5322 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
5323 * @phba: pointer to lpfc hba data structure.
5324 *
5325 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
5326 * operation.
5327 *
5328 * Return codes
5329 *      0 - successful
5330 *      ENOMEM - No availble memory
5331 *      EIO - The mailbox failed to complete successfully.
5332 **/
5333static void
5334lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
5335{
5336	int fcp_qidx;
5337
5338	/* Release mailbox command work queue */
5339	lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
5340	phba->sli4_hba.mbx_wq = NULL;
5341
5342	/* Release ELS work queue */
5343	lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
5344	phba->sli4_hba.els_wq = NULL;
5345
5346	/* Release FCP work queue */
5347	for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
5348		lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_qidx]);
5349	kfree(phba->sli4_hba.fcp_wq);
5350	phba->sli4_hba.fcp_wq = NULL;
5351
5352	/* Release unsolicited receive queue */
5353	lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
5354	phba->sli4_hba.hdr_rq = NULL;
5355	lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
5356	phba->sli4_hba.dat_rq = NULL;
5357
5358	/* Release ELS complete queue */
5359	lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
5360	phba->sli4_hba.els_cq = NULL;
5361
5362	/* Release mailbox command complete queue */
5363	lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
5364	phba->sli4_hba.mbx_cq = NULL;
5365
5366	/* Release FCP response complete queue */
5367	for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5368		lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_qidx]);
5369	kfree(phba->sli4_hba.fcp_cq);
5370	phba->sli4_hba.fcp_cq = NULL;
5371
5372	/* Release fast-path event queue */
5373	for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5374		lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_qidx]);
5375	kfree(phba->sli4_hba.fp_eq);
5376	phba->sli4_hba.fp_eq = NULL;
5377
5378	/* Release slow-path event queue */
5379	lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
5380	phba->sli4_hba.sp_eq = NULL;
5381
5382	return;
5383}
5384
5385/**
5386 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
5387 * @phba: pointer to lpfc hba data structure.
5388 *
5389 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
5390 * operation.
5391 *
5392 * Return codes
5393 *      0 - successful
5394 *      ENOMEM - No availble memory
5395 *      EIO - The mailbox failed to complete successfully.
5396 **/
5397int
5398lpfc_sli4_queue_setup(struct lpfc_hba *phba)
5399{
5400	int rc = -ENOMEM;
5401	int fcp_eqidx, fcp_cqidx, fcp_wqidx;
5402	int fcp_cq_index = 0;
5403
5404	/*
5405	 * Set up Event Queues (EQs)
5406	 */
5407
5408	/* Set up slow-path event queue */
5409	if (!phba->sli4_hba.sp_eq) {
5410		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5411				"0520 Slow-path EQ not allocated\n");
5412		goto out_error;
5413	}
5414	rc = lpfc_eq_create(phba, phba->sli4_hba.sp_eq,
5415			    LPFC_SP_DEF_IMAX);
5416	if (rc) {
5417		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5418				"0521 Failed setup of slow-path EQ: "
5419				"rc = 0x%x\n", rc);
5420		goto out_error;
5421	}
5422	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5423			"2583 Slow-path EQ setup: queue-id=%d\n",
5424			phba->sli4_hba.sp_eq->queue_id);
5425
5426	/* Set up fast-path event queue */
5427	for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
5428		if (!phba->sli4_hba.fp_eq[fcp_eqidx]) {
5429			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5430					"0522 Fast-path EQ (%d) not "
5431					"allocated\n", fcp_eqidx);
5432			goto out_destroy_fp_eq;
5433		}
5434		rc = lpfc_eq_create(phba, phba->sli4_hba.fp_eq[fcp_eqidx],
5435				    phba->cfg_fcp_imax);
5436		if (rc) {
5437			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5438					"0523 Failed setup of fast-path EQ "
5439					"(%d), rc = 0x%x\n", fcp_eqidx, rc);
5440			goto out_destroy_fp_eq;
5441		}
5442		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5443				"2584 Fast-path EQ setup: "
5444				"queue[%d]-id=%d\n", fcp_eqidx,
5445				phba->sli4_hba.fp_eq[fcp_eqidx]->queue_id);
5446	}
5447
5448	/*
5449	 * Set up Complete Queues (CQs)
5450	 */
5451
5452	/* Set up slow-path MBOX Complete Queue as the first CQ */
5453	if (!phba->sli4_hba.mbx_cq) {
5454		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5455				"0528 Mailbox CQ not allocated\n");
5456		goto out_destroy_fp_eq;
5457	}
5458	rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq, phba->sli4_hba.sp_eq,
5459			    LPFC_MCQ, LPFC_MBOX);
5460	if (rc) {
5461		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5462				"0529 Failed setup of slow-path mailbox CQ: "
5463				"rc = 0x%x\n", rc);
5464		goto out_destroy_fp_eq;
5465	}
5466	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5467			"2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
5468			phba->sli4_hba.mbx_cq->queue_id,
5469			phba->sli4_hba.sp_eq->queue_id);
5470
5471	/* Set up slow-path ELS Complete Queue */
5472	if (!phba->sli4_hba.els_cq) {
5473		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5474				"0530 ELS CQ not allocated\n");
5475		goto out_destroy_mbx_cq;
5476	}
5477	rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq, phba->sli4_hba.sp_eq,
5478			    LPFC_WCQ, LPFC_ELS);
5479	if (rc) {
5480		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5481				"0531 Failed setup of slow-path ELS CQ: "
5482				"rc = 0x%x\n", rc);
5483		goto out_destroy_mbx_cq;
5484	}
5485	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5486			"2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
5487			phba->sli4_hba.els_cq->queue_id,
5488			phba->sli4_hba.sp_eq->queue_id);
5489
5490	/* Set up fast-path FCP Response Complete Queue */
5491	for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
5492		if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
5493			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5494					"0526 Fast-path FCP CQ (%d) not "
5495					"allocated\n", fcp_cqidx);
5496			goto out_destroy_fcp_cq;
5497		}
5498		rc = lpfc_cq_create(phba, phba->sli4_hba.fcp_cq[fcp_cqidx],
5499				    phba->sli4_hba.fp_eq[fcp_cqidx],
5500				    LPFC_WCQ, LPFC_FCP);
5501		if (rc) {
5502			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5503					"0527 Failed setup of fast-path FCP "
5504					"CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
5505			goto out_destroy_fcp_cq;
5506		}
5507		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5508				"2588 FCP CQ setup: cq[%d]-id=%d, "
5509				"parent eq[%d]-id=%d\n",
5510				fcp_cqidx,
5511				phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
5512				fcp_cqidx,
5513				phba->sli4_hba.fp_eq[fcp_cqidx]->queue_id);
5514	}
5515
5516	/*
5517	 * Set up all the Work Queues (WQs)
5518	 */
5519
5520	/* Set up Mailbox Command Queue */
5521	if (!phba->sli4_hba.mbx_wq) {
5522		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5523				"0538 Slow-path MQ not allocated\n");
5524		goto out_destroy_fcp_cq;
5525	}
5526	rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
5527			    phba->sli4_hba.mbx_cq, LPFC_MBOX);
5528	if (rc) {
5529		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5530				"0539 Failed setup of slow-path MQ: "
5531				"rc = 0x%x\n", rc);
5532		goto out_destroy_fcp_cq;
5533	}
5534	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5535			"2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
5536			phba->sli4_hba.mbx_wq->queue_id,
5537			phba->sli4_hba.mbx_cq->queue_id);
5538
5539	/* Set up slow-path ELS Work Queue */
5540	if (!phba->sli4_hba.els_wq) {
5541		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5542				"0536 Slow-path ELS WQ not allocated\n");
5543		goto out_destroy_mbx_wq;
5544	}
5545	rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
5546			    phba->sli4_hba.els_cq, LPFC_ELS);
5547	if (rc) {
5548		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5549				"0537 Failed setup of slow-path ELS WQ: "
5550				"rc = 0x%x\n", rc);
5551		goto out_destroy_mbx_wq;
5552	}
5553	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5554			"2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
5555			phba->sli4_hba.els_wq->queue_id,
5556			phba->sli4_hba.els_cq->queue_id);
5557
5558	/* Set up fast-path FCP Work Queue */
5559	for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
5560		if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
5561			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5562					"0534 Fast-path FCP WQ (%d) not "
5563					"allocated\n", fcp_wqidx);
5564			goto out_destroy_fcp_wq;
5565		}
5566		rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
5567				    phba->sli4_hba.fcp_cq[fcp_cq_index],
5568				    LPFC_FCP);
5569		if (rc) {
5570			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5571					"0535 Failed setup of fast-path FCP "
5572					"WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
5573			goto out_destroy_fcp_wq;
5574		}
5575		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5576				"2591 FCP WQ setup: wq[%d]-id=%d, "
5577				"parent cq[%d]-id=%d\n",
5578				fcp_wqidx,
5579				phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
5580				fcp_cq_index,
5581				phba->sli4_hba.fcp_cq[fcp_cq_index]->queue_id);
5582		/* Round robin FCP Work Queue's Completion Queue assignment */
5583		fcp_cq_index = ((fcp_cq_index + 1) % phba->cfg_fcp_eq_count);
5584	}
5585
5586	/*
5587	 * Create Receive Queue (RQ)
5588	 */
5589	if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
5590		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5591				"0540 Receive Queue not allocated\n");
5592		goto out_destroy_fcp_wq;
5593	}
5594	rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
5595			    phba->sli4_hba.els_cq, LPFC_USOL);
5596	if (rc) {
5597		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5598				"0541 Failed setup of Receive Queue: "
5599				"rc = 0x%x\n", rc);
5600		goto out_destroy_fcp_wq;
5601	}
5602	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5603			"2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
5604			"parent cq-id=%d\n",
5605			phba->sli4_hba.hdr_rq->queue_id,
5606			phba->sli4_hba.dat_rq->queue_id,
5607			phba->sli4_hba.els_cq->queue_id);
5608	return 0;
5609
5610out_destroy_fcp_wq:
5611	for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
5612		lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
5613	lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
5614out_destroy_mbx_wq:
5615	lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
5616out_destroy_fcp_cq:
5617	for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
5618		lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
5619	lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
5620out_destroy_mbx_cq:
5621	lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
5622out_destroy_fp_eq:
5623	for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
5624		lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_eqidx]);
5625	lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
5626out_error:
5627	return rc;
5628}
5629
5630/**
5631 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
5632 * @phba: pointer to lpfc hba data structure.
5633 *
5634 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
5635 * operation.
5636 *
5637 * Return codes
5638 *      0 - successful
5639 *      ENOMEM - No availble memory
5640 *      EIO - The mailbox failed to complete successfully.
5641 **/
5642void
5643lpfc_sli4_queue_unset(struct lpfc_hba *phba)
5644{
5645	int fcp_qidx;
5646
5647	/* Unset mailbox command work queue */
5648	lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
5649	/* Unset ELS work queue */
5650	lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
5651	/* Unset unsolicited receive queue */
5652	lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
5653	/* Unset FCP work queue */
5654	for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
5655		lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
5656	/* Unset mailbox command complete queue */
5657	lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
5658	/* Unset ELS complete queue */
5659	lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
5660	/* Unset FCP response complete queue */
5661	for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5662		lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
5663	/* Unset fast-path event queue */
5664	for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5665		lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_qidx]);
5666	/* Unset slow-path event queue */
5667	lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
5668}
5669
5670/**
5671 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
5672 * @phba: pointer to lpfc hba data structure.
5673 *
5674 * This routine is invoked to allocate and set up a pool of completion queue
5675 * events. The body of the completion queue event is a completion queue entry
5676 * CQE. For now, this pool is used for the interrupt service routine to queue
5677 * the following HBA completion queue events for the worker thread to process:
5678 *   - Mailbox asynchronous events
5679 *   - Receive queue completion unsolicited events
5680 * Later, this can be used for all the slow-path events.
5681 *
5682 * Return codes
5683 *      0 - successful
5684 *      -ENOMEM - No availble memory
5685 **/
5686static int
5687lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
5688{
5689	struct lpfc_cq_event *cq_event;
5690	int i;
5691
5692	for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
5693		cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
5694		if (!cq_event)
5695			goto out_pool_create_fail;
5696		list_add_tail(&cq_event->list,
5697			      &phba->sli4_hba.sp_cqe_event_pool);
5698	}
5699	return 0;
5700
5701out_pool_create_fail:
5702	lpfc_sli4_cq_event_pool_destroy(phba);
5703	return -ENOMEM;
5704}
5705
5706/**
5707 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
5708 * @phba: pointer to lpfc hba data structure.
5709 *
5710 * This routine is invoked to free the pool of completion queue events at
5711 * driver unload time. Note that, it is the responsibility of the driver
5712 * cleanup routine to free all the outstanding completion-queue events
5713 * allocated from this pool back into the pool before invoking this routine
5714 * to destroy the pool.
5715 **/
5716static void
5717lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
5718{
5719	struct lpfc_cq_event *cq_event, *next_cq_event;
5720
5721	list_for_each_entry_safe(cq_event, next_cq_event,
5722				 &phba->sli4_hba.sp_cqe_event_pool, list) {
5723		list_del(&cq_event->list);
5724		kfree(cq_event);
5725	}
5726}
5727
5728/**
5729 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
5730 * @phba: pointer to lpfc hba data structure.
5731 *
5732 * This routine is the lock free version of the API invoked to allocate a
5733 * completion-queue event from the free pool.
5734 *
5735 * Return: Pointer to the newly allocated completion-queue event if successful
5736 *         NULL otherwise.
5737 **/
5738struct lpfc_cq_event *
5739__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
5740{
5741	struct lpfc_cq_event *cq_event = NULL;
5742
5743	list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
5744			 struct lpfc_cq_event, list);
5745	return cq_event;
5746}
5747
5748/**
5749 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
5750 * @phba: pointer to lpfc hba data structure.
5751 *
5752 * This routine is the lock version of the API invoked to allocate a
5753 * completion-queue event from the free pool.
5754 *
5755 * Return: Pointer to the newly allocated completion-queue event if successful
5756 *         NULL otherwise.
5757 **/
5758struct lpfc_cq_event *
5759lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
5760{
5761	struct lpfc_cq_event *cq_event;
5762	unsigned long iflags;
5763
5764	spin_lock_irqsave(&phba->hbalock, iflags);
5765	cq_event = __lpfc_sli4_cq_event_alloc(phba);
5766	spin_unlock_irqrestore(&phba->hbalock, iflags);
5767	return cq_event;
5768}
5769
5770/**
5771 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
5772 * @phba: pointer to lpfc hba data structure.
5773 * @cq_event: pointer to the completion queue event to be freed.
5774 *
5775 * This routine is the lock free version of the API invoked to release a
5776 * completion-queue event back into the free pool.
5777 **/
5778void
5779__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
5780			     struct lpfc_cq_event *cq_event)
5781{
5782	list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
5783}
5784
5785/**
5786 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
5787 * @phba: pointer to lpfc hba data structure.
5788 * @cq_event: pointer to the completion queue event to be freed.
5789 *
5790 * This routine is the lock version of the API invoked to release a
5791 * completion-queue event back into the free pool.
5792 **/
5793void
5794lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
5795			   struct lpfc_cq_event *cq_event)
5796{
5797	unsigned long iflags;
5798	spin_lock_irqsave(&phba->hbalock, iflags);
5799	__lpfc_sli4_cq_event_release(phba, cq_event);
5800	spin_unlock_irqrestore(&phba->hbalock, iflags);
5801}
5802
5803/**
5804 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
5805 * @phba: pointer to lpfc hba data structure.
5806 *
5807 * This routine is to free all the pending completion-queue events to the
5808 * back into the free pool for device reset.
5809 **/
5810static void
5811lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
5812{
5813	LIST_HEAD(cqelist);
5814	struct lpfc_cq_event *cqe;
5815	unsigned long iflags;
5816
5817	/* Retrieve all the pending WCQEs from pending WCQE lists */
5818	spin_lock_irqsave(&phba->hbalock, iflags);
5819	/* Pending FCP XRI abort events */
5820	list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
5821			 &cqelist);
5822	/* Pending ELS XRI abort events */
5823	list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
5824			 &cqelist);
5825	/* Pending asynnc events */
5826	list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
5827			 &cqelist);
5828	spin_unlock_irqrestore(&phba->hbalock, iflags);
5829
5830	while (!list_empty(&cqelist)) {
5831		list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
5832		lpfc_sli4_cq_event_release(phba, cqe);
5833	}
5834}
5835
5836/**
5837 * lpfc_pci_function_reset - Reset pci function.
5838 * @phba: pointer to lpfc hba data structure.
5839 *
5840 * This routine is invoked to request a PCI function reset. It will destroys
5841 * all resources assigned to the PCI function which originates this request.
5842 *
5843 * Return codes
5844 *      0 - successful
5845 *      ENOMEM - No availble memory
5846 *      EIO - The mailbox failed to complete successfully.
5847 **/
5848int
5849lpfc_pci_function_reset(struct lpfc_hba *phba)
5850{
5851	LPFC_MBOXQ_t *mboxq;
5852	uint32_t rc = 0;
5853	uint32_t shdr_status, shdr_add_status;
5854	union lpfc_sli4_cfg_shdr *shdr;
5855
5856	mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5857	if (!mboxq) {
5858		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5859				"0494 Unable to allocate memory for issuing "
5860				"SLI_FUNCTION_RESET mailbox command\n");
5861		return -ENOMEM;
5862	}
5863
5864	/* Set up PCI function reset SLI4_CONFIG mailbox-ioctl command */
5865	lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5866			 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
5867			 LPFC_SLI4_MBX_EMBED);
5868	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5869	shdr = (union lpfc_sli4_cfg_shdr *)
5870		&mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
5871	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5872	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5873	if (rc != MBX_TIMEOUT)
5874		mempool_free(mboxq, phba->mbox_mem_pool);
5875	if (shdr_status || shdr_add_status || rc) {
5876		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5877				"0495 SLI_FUNCTION_RESET mailbox failed with "
5878				"status x%x add_status x%x, mbx status x%x\n",
5879				shdr_status, shdr_add_status, rc);
5880		rc = -ENXIO;
5881	}
5882	return rc;
5883}
5884
5885/**
5886 * lpfc_sli4_send_nop_mbox_cmds - Send sli-4 nop mailbox commands
5887 * @phba: pointer to lpfc hba data structure.
5888 * @cnt: number of nop mailbox commands to send.
5889 *
5890 * This routine is invoked to send a number @cnt of NOP mailbox command and
5891 * wait for each command to complete.
5892 *
5893 * Return: the number of NOP mailbox command completed.
5894 **/
5895static int
5896lpfc_sli4_send_nop_mbox_cmds(struct lpfc_hba *phba, uint32_t cnt)
5897{
5898	LPFC_MBOXQ_t *mboxq;
5899	int length, cmdsent;
5900	uint32_t mbox_tmo;
5901	uint32_t rc = 0;
5902	uint32_t shdr_status, shdr_add_status;
5903	union lpfc_sli4_cfg_shdr *shdr;
5904
5905	if (cnt == 0) {
5906		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5907				"2518 Requested to send 0 NOP mailbox cmd\n");
5908		return cnt;
5909	}
5910
5911	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5912	if (!mboxq) {
5913		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5914				"2519 Unable to allocate memory for issuing "
5915				"NOP mailbox command\n");
5916		return 0;
5917	}
5918
5919	/* Set up NOP SLI4_CONFIG mailbox-ioctl command */
5920	length = (sizeof(struct lpfc_mbx_nop) -
5921		  sizeof(struct lpfc_sli4_cfg_mhdr));
5922	lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5923			 LPFC_MBOX_OPCODE_NOP, length, LPFC_SLI4_MBX_EMBED);
5924
5925	mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
5926	for (cmdsent = 0; cmdsent < cnt; cmdsent++) {
5927		if (!phba->sli4_hba.intr_enable)
5928			rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5929		else
5930			rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
5931		if (rc == MBX_TIMEOUT)
5932			break;
5933		/* Check return status */
5934		shdr = (union lpfc_sli4_cfg_shdr *)
5935			&mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
5936		shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5937		shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
5938					 &shdr->response);
5939		if (shdr_status || shdr_add_status || rc) {
5940			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5941					"2520 NOP mailbox command failed "
5942					"status x%x add_status x%x mbx "
5943					"status x%x\n", shdr_status,
5944					shdr_add_status, rc);
5945			break;
5946		}
5947	}
5948
5949	if (rc != MBX_TIMEOUT)
5950		mempool_free(mboxq, phba->mbox_mem_pool);
5951
5952	return cmdsent;
5953}
5954
5955/**
5956 * lpfc_sli4_fcfi_unreg - Unregister fcfi to device
5957 * @phba: pointer to lpfc hba data structure.
5958 * @fcfi: fcf index.
5959 *
5960 * This routine is invoked to unregister a FCFI from device.
5961 **/
5962void
5963lpfc_sli4_fcfi_unreg(struct lpfc_hba *phba, uint16_t fcfi)
5964{
5965	LPFC_MBOXQ_t *mbox;
5966	uint32_t mbox_tmo;
5967	int rc;
5968	unsigned long flags;
5969
5970	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5971
5972	if (!mbox)
5973		return;
5974
5975	lpfc_unreg_fcfi(mbox, fcfi);
5976
5977	if (!phba->sli4_hba.intr_enable)
5978		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5979	else {
5980		mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
5981		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5982	}
5983	if (rc != MBX_TIMEOUT)
5984		mempool_free(mbox, phba->mbox_mem_pool);
5985	if (rc != MBX_SUCCESS)
5986		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5987				"2517 Unregister FCFI command failed "
5988				"status %d, mbxStatus x%x\n", rc,
5989				bf_get(lpfc_mqe_status, &mbox->u.mqe));
5990	else {
5991		spin_lock_irqsave(&phba->hbalock, flags);
5992		/* Mark the FCFI is no longer registered */
5993		phba->fcf.fcf_flag &=
5994			~(FCF_AVAILABLE | FCF_REGISTERED | FCF_DISCOVERED);
5995		spin_unlock_irqrestore(&phba->hbalock, flags);
5996	}
5997}
5998
5999/**
6000 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
6001 * @phba: pointer to lpfc hba data structure.
6002 *
6003 * This routine is invoked to set up the PCI device memory space for device
6004 * with SLI-4 interface spec.
6005 *
6006 * Return codes
6007 * 	0 - successful
6008 * 	other values - error
6009 **/
6010static int
6011lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
6012{
6013	struct pci_dev *pdev;
6014	unsigned long bar0map_len, bar1map_len, bar2map_len;
6015	int error = -ENODEV;
6016
6017	/* Obtain PCI device reference */
6018	if (!phba->pcidev)
6019		return error;
6020	else
6021		pdev = phba->pcidev;
6022
6023	/* Set the device DMA mask size */
6024	if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0)
6025		if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
6026			return error;
6027
6028	/* Get the bus address of SLI4 device Bar0, Bar1, and Bar2 and the
6029	 * number of bytes required by each mapping. They are actually
6030	 * mapping to the PCI BAR regions 1, 2, and 4 by the SLI4 device.
6031	 */
6032	phba->pci_bar0_map = pci_resource_start(pdev, LPFC_SLI4_BAR0);
6033	bar0map_len = pci_resource_len(pdev, LPFC_SLI4_BAR0);
6034
6035	phba->pci_bar1_map = pci_resource_start(pdev, LPFC_SLI4_BAR1);
6036	bar1map_len = pci_resource_len(pdev, LPFC_SLI4_BAR1);
6037
6038	phba->pci_bar2_map = pci_resource_start(pdev, LPFC_SLI4_BAR2);
6039	bar2map_len = pci_resource_len(pdev, LPFC_SLI4_BAR2);
6040
6041	/* Map SLI4 PCI Config Space Register base to a kernel virtual addr */
6042	phba->sli4_hba.conf_regs_memmap_p =
6043				ioremap(phba->pci_bar0_map, bar0map_len);
6044	if (!phba->sli4_hba.conf_regs_memmap_p) {
6045		dev_printk(KERN_ERR, &pdev->dev,
6046			   "ioremap failed for SLI4 PCI config registers.\n");
6047		goto out;
6048	}
6049
6050	/* Map SLI4 HBA Control Register base to a kernel virtual address. */
6051	phba->sli4_hba.ctrl_regs_memmap_p =
6052				ioremap(phba->pci_bar1_map, bar1map_len);
6053	if (!phba->sli4_hba.ctrl_regs_memmap_p) {
6054		dev_printk(KERN_ERR, &pdev->dev,
6055			   "ioremap failed for SLI4 HBA control registers.\n");
6056		goto out_iounmap_conf;
6057	}
6058
6059	/* Map SLI4 HBA Doorbell Register base to a kernel virtual address. */
6060	phba->sli4_hba.drbl_regs_memmap_p =
6061				ioremap(phba->pci_bar2_map, bar2map_len);
6062	if (!phba->sli4_hba.drbl_regs_memmap_p) {
6063		dev_printk(KERN_ERR, &pdev->dev,
6064			   "ioremap failed for SLI4 HBA doorbell registers.\n");
6065		goto out_iounmap_ctrl;
6066	}
6067
6068	/* Set up BAR0 PCI config space register memory map */
6069	lpfc_sli4_bar0_register_memmap(phba);
6070
6071	/* Set up BAR1 register memory map */
6072	lpfc_sli4_bar1_register_memmap(phba);
6073
6074	/* Set up BAR2 register memory map */
6075	error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
6076	if (error)
6077		goto out_iounmap_all;
6078
6079	return 0;
6080
6081out_iounmap_all:
6082	iounmap(phba->sli4_hba.drbl_regs_memmap_p);
6083out_iounmap_ctrl:
6084	iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
6085out_iounmap_conf:
6086	iounmap(phba->sli4_hba.conf_regs_memmap_p);
6087out:
6088	return error;
6089}
6090
6091/**
6092 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
6093 * @phba: pointer to lpfc hba data structure.
6094 *
6095 * This routine is invoked to unset the PCI device memory space for device
6096 * with SLI-4 interface spec.
6097 **/
6098static void
6099lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
6100{
6101	struct pci_dev *pdev;
6102
6103	/* Obtain PCI device reference */
6104	if (!phba->pcidev)
6105		return;
6106	else
6107		pdev = phba->pcidev;
6108
6109	/* Free coherent DMA memory allocated */
6110
6111	/* Unmap I/O memory space */
6112	iounmap(phba->sli4_hba.drbl_regs_memmap_p);
6113	iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
6114	iounmap(phba->sli4_hba.conf_regs_memmap_p);
6115
6116	return;
6117}
6118
6119/**
6120 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
6121 * @phba: pointer to lpfc hba data structure.
6122 *
6123 * This routine is invoked to enable the MSI-X interrupt vectors to device
6124 * with SLI-3 interface specs. The kernel function pci_enable_msix() is
6125 * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
6126 * invoked, enables either all or nothing, depending on the current
6127 * availability of PCI vector resources. The device driver is responsible
6128 * for calling the individual request_irq() to register each MSI-X vector
6129 * with a interrupt handler, which is done in this function. Note that
6130 * later when device is unloading, the driver should always call free_irq()
6131 * on all MSI-X vectors it has done request_irq() on before calling
6132 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
6133 * will be left with MSI-X enabled and leaks its vectors.
6134 *
6135 * Return codes
6136 *   0 - successful
6137 *   other values - error
6138 **/
6139static int
6140lpfc_sli_enable_msix(struct lpfc_hba *phba)
6141{
6142	int rc, i;
6143	LPFC_MBOXQ_t *pmb;
6144
6145	/* Set up MSI-X multi-message vectors */
6146	for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6147		phba->msix_entries[i].entry = i;
6148
6149	/* Configure MSI-X capability structure */
6150	rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
6151				ARRAY_SIZE(phba->msix_entries));
6152	if (rc) {
6153		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6154				"0420 PCI enable MSI-X failed (%d)\n", rc);
6155		goto msi_fail_out;
6156	}
6157	for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6158		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6159				"0477 MSI-X entry[%d]: vector=x%x "
6160				"message=%d\n", i,
6161				phba->msix_entries[i].vector,
6162				phba->msix_entries[i].entry);
6163	/*
6164	 * Assign MSI-X vectors to interrupt handlers
6165	 */
6166
6167	/* vector-0 is associated to slow-path handler */
6168	rc = request_irq(phba->msix_entries[0].vector,
6169			 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
6170			 LPFC_SP_DRIVER_HANDLER_NAME, phba);
6171	if (rc) {
6172		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6173				"0421 MSI-X slow-path request_irq failed "
6174				"(%d)\n", rc);
6175		goto msi_fail_out;
6176	}
6177
6178	/* vector-1 is associated to fast-path handler */
6179	rc = request_irq(phba->msix_entries[1].vector,
6180			 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
6181			 LPFC_FP_DRIVER_HANDLER_NAME, phba);
6182
6183	if (rc) {
6184		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6185				"0429 MSI-X fast-path request_irq failed "
6186				"(%d)\n", rc);
6187		goto irq_fail_out;
6188	}
6189
6190	/*
6191	 * Configure HBA MSI-X attention conditions to messages
6192	 */
6193	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6194
6195	if (!pmb) {
6196		rc = -ENOMEM;
6197		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6198				"0474 Unable to allocate memory for issuing "
6199				"MBOX_CONFIG_MSI command\n");
6200		goto mem_fail_out;
6201	}
6202	rc = lpfc_config_msi(phba, pmb);
6203	if (rc)
6204		goto mbx_fail_out;
6205	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6206	if (rc != MBX_SUCCESS) {
6207		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
6208				"0351 Config MSI mailbox command failed, "
6209				"mbxCmd x%x, mbxStatus x%x\n",
6210				pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
6211		goto mbx_fail_out;
6212	}
6213
6214	/* Free memory allocated for mailbox command */
6215	mempool_free(pmb, phba->mbox_mem_pool);
6216	return rc;
6217
6218mbx_fail_out:
6219	/* Free memory allocated for mailbox command */
6220	mempool_free(pmb, phba->mbox_mem_pool);
6221
6222mem_fail_out:
6223	/* free the irq already requested */
6224	free_irq(phba->msix_entries[1].vector, phba);
6225
6226irq_fail_out:
6227	/* free the irq already requested */
6228	free_irq(phba->msix_entries[0].vector, phba);
6229
6230msi_fail_out:
6231	/* Unconfigure MSI-X capability structure */
6232	pci_disable_msix(phba->pcidev);
6233	return rc;
6234}
6235
6236/**
6237 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
6238 * @phba: pointer to lpfc hba data structure.
6239 *
6240 * This routine is invoked to release the MSI-X vectors and then disable the
6241 * MSI-X interrupt mode to device with SLI-3 interface spec.
6242 **/
6243static void
6244lpfc_sli_disable_msix(struct lpfc_hba *phba)
6245{
6246	int i;
6247
6248	/* Free up MSI-X multi-message vectors */
6249	for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6250		free_irq(phba->msix_entries[i].vector, phba);
6251	/* Disable MSI-X */
6252	pci_disable_msix(phba->pcidev);
6253
6254	return;
6255}
6256
6257/**
6258 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
6259 * @phba: pointer to lpfc hba data structure.
6260 *
6261 * This routine is invoked to enable the MSI interrupt mode to device with
6262 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
6263 * enable the MSI vector. The device driver is responsible for calling the
6264 * request_irq() to register MSI vector with a interrupt the handler, which
6265 * is done in this function.
6266 *
6267 * Return codes
6268 * 	0 - successful
6269 * 	other values - error
6270 */
6271static int
6272lpfc_sli_enable_msi(struct lpfc_hba *phba)
6273{
6274	int rc;
6275
6276	rc = pci_enable_msi(phba->pcidev);
6277	if (!rc)
6278		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6279				"0462 PCI enable MSI mode success.\n");
6280	else {
6281		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6282				"0471 PCI enable MSI mode failed (%d)\n", rc);
6283		return rc;
6284	}
6285
6286	rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
6287			 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6288	if (rc) {
6289		pci_disable_msi(phba->pcidev);
6290		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6291				"0478 MSI request_irq failed (%d)\n", rc);
6292	}
6293	return rc;
6294}
6295
6296/**
6297 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
6298 * @phba: pointer to lpfc hba data structure.
6299 *
6300 * This routine is invoked to disable the MSI interrupt mode to device with
6301 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
6302 * done request_irq() on before calling pci_disable_msi(). Failure to do so
6303 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
6304 * its vector.
6305 */
6306static void
6307lpfc_sli_disable_msi(struct lpfc_hba *phba)
6308{
6309	free_irq(phba->pcidev->irq, phba);
6310	pci_disable_msi(phba->pcidev);
6311	return;
6312}
6313
6314/**
6315 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
6316 * @phba: pointer to lpfc hba data structure.
6317 *
6318 * This routine is invoked to enable device interrupt and associate driver's
6319 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
6320 * spec. Depends on the interrupt mode configured to the driver, the driver
6321 * will try to fallback from the configured interrupt mode to an interrupt
6322 * mode which is supported by the platform, kernel, and device in the order
6323 * of:
6324 * MSI-X -> MSI -> IRQ.
6325 *
6326 * Return codes
6327 *   0 - successful
6328 *   other values - error
6329 **/
6330static uint32_t
6331lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
6332{
6333	uint32_t intr_mode = LPFC_INTR_ERROR;
6334	int retval;
6335
6336	if (cfg_mode == 2) {
6337		/* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
6338		retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
6339		if (!retval) {
6340			/* Now, try to enable MSI-X interrupt mode */
6341			retval = lpfc_sli_enable_msix(phba);
6342			if (!retval) {
6343				/* Indicate initialization to MSI-X mode */
6344				phba->intr_type = MSIX;
6345				intr_mode = 2;
6346			}
6347		}
6348	}
6349
6350	/* Fallback to MSI if MSI-X initialization failed */
6351	if (cfg_mode >= 1 && phba->intr_type == NONE) {
6352		retval = lpfc_sli_enable_msi(phba);
6353		if (!retval) {
6354			/* Indicate initialization to MSI mode */
6355			phba->intr_type = MSI;
6356			intr_mode = 1;
6357		}
6358	}
6359
6360	/* Fallback to INTx if both MSI-X/MSI initalization failed */
6361	if (phba->intr_type == NONE) {
6362		retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
6363				     IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6364		if (!retval) {
6365			/* Indicate initialization to INTx mode */
6366			phba->intr_type = INTx;
6367			intr_mode = 0;
6368		}
6369	}
6370	return intr_mode;
6371}
6372
6373/**
6374 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
6375 * @phba: pointer to lpfc hba data structure.
6376 *
6377 * This routine is invoked to disable device interrupt and disassociate the
6378 * driver's interrupt handler(s) from interrupt vector(s) to device with
6379 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
6380 * release the interrupt vector(s) for the message signaled interrupt.
6381 **/
6382static void
6383lpfc_sli_disable_intr(struct lpfc_hba *phba)
6384{
6385	/* Disable the currently initialized interrupt mode */
6386	if (phba->intr_type == MSIX)
6387		lpfc_sli_disable_msix(phba);
6388	else if (phba->intr_type == MSI)
6389		lpfc_sli_disable_msi(phba);
6390	else if (phba->intr_type == INTx)
6391		free_irq(phba->pcidev->irq, phba);
6392
6393	/* Reset interrupt management states */
6394	phba->intr_type = NONE;
6395	phba->sli.slistat.sli_intr = 0;
6396
6397	return;
6398}
6399
6400/**
6401 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
6402 * @phba: pointer to lpfc hba data structure.
6403 *
6404 * This routine is invoked to enable the MSI-X interrupt vectors to device
6405 * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
6406 * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
6407 * enables either all or nothing, depending on the current availability of
6408 * PCI vector resources. The device driver is responsible for calling the
6409 * individual request_irq() to register each MSI-X vector with a interrupt
6410 * handler, which is done in this function. Note that later when device is
6411 * unloading, the driver should always call free_irq() on all MSI-X vectors
6412 * it has done request_irq() on before calling pci_disable_msix(). Failure
6413 * to do so results in a BUG_ON() and a device will be left with MSI-X
6414 * enabled and leaks its vectors.
6415 *
6416 * Return codes
6417 * 0 - successful
6418 * other values - error
6419 **/
6420static int
6421lpfc_sli4_enable_msix(struct lpfc_hba *phba)
6422{
6423	int rc, index;
6424
6425	/* Set up MSI-X multi-message vectors */
6426	for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
6427		phba->sli4_hba.msix_entries[index].entry = index;
6428
6429	/* Configure MSI-X capability structure */
6430	rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
6431			     phba->sli4_hba.cfg_eqn);
6432	if (rc) {
6433		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6434				"0484 PCI enable MSI-X failed (%d)\n", rc);
6435		goto msi_fail_out;
6436	}
6437	/* Log MSI-X vector assignment */
6438	for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
6439		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6440				"0489 MSI-X entry[%d]: vector=x%x "
6441				"message=%d\n", index,
6442				phba->sli4_hba.msix_entries[index].vector,
6443				phba->sli4_hba.msix_entries[index].entry);
6444	/*
6445	 * Assign MSI-X vectors to interrupt handlers
6446	 */
6447
6448	/* The first vector must associated to slow-path handler for MQ */
6449	rc = request_irq(phba->sli4_hba.msix_entries[0].vector,
6450			 &lpfc_sli4_sp_intr_handler, IRQF_SHARED,
6451			 LPFC_SP_DRIVER_HANDLER_NAME, phba);
6452	if (rc) {
6453		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6454				"0485 MSI-X slow-path request_irq failed "
6455				"(%d)\n", rc);
6456		goto msi_fail_out;
6457	}
6458
6459	/* The rest of the vector(s) are associated to fast-path handler(s) */
6460	for (index = 1; index < phba->sli4_hba.cfg_eqn; index++) {
6461		phba->sli4_hba.fcp_eq_hdl[index - 1].idx = index - 1;
6462		phba->sli4_hba.fcp_eq_hdl[index - 1].phba = phba;
6463		rc = request_irq(phba->sli4_hba.msix_entries[index].vector,
6464				 &lpfc_sli4_fp_intr_handler, IRQF_SHARED,
6465				 LPFC_FP_DRIVER_HANDLER_NAME,
6466				 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6467		if (rc) {
6468			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6469					"0486 MSI-X fast-path (%d) "
6470					"request_irq failed (%d)\n", index, rc);
6471			goto cfg_fail_out;
6472		}
6473	}
6474
6475	return rc;
6476
6477cfg_fail_out:
6478	/* free the irq already requested */
6479	for (--index; index >= 1; index--)
6480		free_irq(phba->sli4_hba.msix_entries[index - 1].vector,
6481			 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6482
6483	/* free the irq already requested */
6484	free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
6485
6486msi_fail_out:
6487	/* Unconfigure MSI-X capability structure */
6488	pci_disable_msix(phba->pcidev);
6489	return rc;
6490}
6491
6492/**
6493 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
6494 * @phba: pointer to lpfc hba data structure.
6495 *
6496 * This routine is invoked to release the MSI-X vectors and then disable the
6497 * MSI-X interrupt mode to device with SLI-4 interface spec.
6498 **/
6499static void
6500lpfc_sli4_disable_msix(struct lpfc_hba *phba)
6501{
6502	int index;
6503
6504	/* Free up MSI-X multi-message vectors */
6505	free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
6506
6507	for (index = 1; index < phba->sli4_hba.cfg_eqn; index++)
6508		free_irq(phba->sli4_hba.msix_entries[index].vector,
6509			 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6510	/* Disable MSI-X */
6511	pci_disable_msix(phba->pcidev);
6512
6513	return;
6514}
6515
6516/**
6517 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
6518 * @phba: pointer to lpfc hba data structure.
6519 *
6520 * This routine is invoked to enable the MSI interrupt mode to device with
6521 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
6522 * to enable the MSI vector. The device driver is responsible for calling
6523 * the request_irq() to register MSI vector with a interrupt the handler,
6524 * which is done in this function.
6525 *
6526 * Return codes
6527 * 	0 - successful
6528 * 	other values - error
6529 **/
6530static int
6531lpfc_sli4_enable_msi(struct lpfc_hba *phba)
6532{
6533	int rc, index;
6534
6535	rc = pci_enable_msi(phba->pcidev);
6536	if (!rc)
6537		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6538				"0487 PCI enable MSI mode success.\n");
6539	else {
6540		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6541				"0488 PCI enable MSI mode failed (%d)\n", rc);
6542		return rc;
6543	}
6544
6545	rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
6546			 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6547	if (rc) {
6548		pci_disable_msi(phba->pcidev);
6549		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6550				"0490 MSI request_irq failed (%d)\n", rc);
6551	}
6552
6553	for (index = 0; index < phba->cfg_fcp_eq_count; index++) {
6554		phba->sli4_hba.fcp_eq_hdl[index].idx = index;
6555		phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
6556	}
6557
6558	return rc;
6559}
6560
6561/**
6562 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
6563 * @phba: pointer to lpfc hba data structure.
6564 *
6565 * This routine is invoked to disable the MSI interrupt mode to device with
6566 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
6567 * done request_irq() on before calling pci_disable_msi(). Failure to do so
6568 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
6569 * its vector.
6570 **/
6571static void
6572lpfc_sli4_disable_msi(struct lpfc_hba *phba)
6573{
6574	free_irq(phba->pcidev->irq, phba);
6575	pci_disable_msi(phba->pcidev);
6576	return;
6577}
6578
6579/**
6580 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
6581 * @phba: pointer to lpfc hba data structure.
6582 *
6583 * This routine is invoked to enable device interrupt and associate driver's
6584 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
6585 * interface spec. Depends on the interrupt mode configured to the driver,
6586 * the driver will try to fallback from the configured interrupt mode to an
6587 * interrupt mode which is supported by the platform, kernel, and device in
6588 * the order of:
6589 * MSI-X -> MSI -> IRQ.
6590 *
6591 * Return codes
6592 * 	0 - successful
6593 * 	other values - error
6594 **/
6595static uint32_t
6596lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
6597{
6598	uint32_t intr_mode = LPFC_INTR_ERROR;
6599	int retval, index;
6600
6601	if (cfg_mode == 2) {
6602		/* Preparation before conf_msi mbox cmd */
6603		retval = 0;
6604		if (!retval) {
6605			/* Now, try to enable MSI-X interrupt mode */
6606			retval = lpfc_sli4_enable_msix(phba);
6607			if (!retval) {
6608				/* Indicate initialization to MSI-X mode */
6609				phba->intr_type = MSIX;
6610				intr_mode = 2;
6611			}
6612		}
6613	}
6614
6615	/* Fallback to MSI if MSI-X initialization failed */
6616	if (cfg_mode >= 1 && phba->intr_type == NONE) {
6617		retval = lpfc_sli4_enable_msi(phba);
6618		if (!retval) {
6619			/* Indicate initialization to MSI mode */
6620			phba->intr_type = MSI;
6621			intr_mode = 1;
6622		}
6623	}
6624
6625	/* Fallback to INTx if both MSI-X/MSI initalization failed */
6626	if (phba->intr_type == NONE) {
6627		retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
6628				     IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6629		if (!retval) {
6630			/* Indicate initialization to INTx mode */
6631			phba->intr_type = INTx;
6632			intr_mode = 0;
6633			for (index = 0; index < phba->cfg_fcp_eq_count;
6634			     index++) {
6635				phba->sli4_hba.fcp_eq_hdl[index].idx = index;
6636				phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
6637			}
6638		}
6639	}
6640	return intr_mode;
6641}
6642
6643/**
6644 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
6645 * @phba: pointer to lpfc hba data structure.
6646 *
6647 * This routine is invoked to disable device interrupt and disassociate
6648 * the driver's interrupt handler(s) from interrupt vector(s) to device
6649 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
6650 * will release the interrupt vector(s) for the message signaled interrupt.
6651 **/
6652static void
6653lpfc_sli4_disable_intr(struct lpfc_hba *phba)
6654{
6655	/* Disable the currently initialized interrupt mode */
6656	if (phba->intr_type == MSIX)
6657		lpfc_sli4_disable_msix(phba);
6658	else if (phba->intr_type == MSI)
6659		lpfc_sli4_disable_msi(phba);
6660	else if (phba->intr_type == INTx)
6661		free_irq(phba->pcidev->irq, phba);
6662
6663	/* Reset interrupt management states */
6664	phba->intr_type = NONE;
6665	phba->sli.slistat.sli_intr = 0;
6666
6667	return;
6668}
6669
6670/**
6671 * lpfc_unset_hba - Unset SLI3 hba device initialization
6672 * @phba: pointer to lpfc hba data structure.
6673 *
6674 * This routine is invoked to unset the HBA device initialization steps to
6675 * a device with SLI-3 interface spec.
6676 **/
6677static void
6678lpfc_unset_hba(struct lpfc_hba *phba)
6679{
6680	struct lpfc_vport *vport = phba->pport;
6681	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
6682
6683	spin_lock_irq(shost->host_lock);
6684	vport->load_flag |= FC_UNLOADING;
6685	spin_unlock_irq(shost->host_lock);
6686
6687	lpfc_stop_hba_timers(phba);
6688
6689	phba->pport->work_port_events = 0;
6690
6691	lpfc_sli_hba_down(phba);
6692
6693	lpfc_sli_brdrestart(phba);
6694
6695	lpfc_sli_disable_intr(phba);
6696
6697	return;
6698}
6699
6700/**
6701 * lpfc_sli4_unset_hba - Unset SLI4 hba device initialization.
6702 * @phba: pointer to lpfc hba data structure.
6703 *
6704 * This routine is invoked to unset the HBA device initialization steps to
6705 * a device with SLI-4 interface spec.
6706 **/
6707static void
6708lpfc_sli4_unset_hba(struct lpfc_hba *phba)
6709{
6710	struct lpfc_vport *vport = phba->pport;
6711	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
6712
6713	spin_lock_irq(shost->host_lock);
6714	vport->load_flag |= FC_UNLOADING;
6715	spin_unlock_irq(shost->host_lock);
6716
6717	phba->pport->work_port_events = 0;
6718
6719	lpfc_sli4_hba_down(phba);
6720
6721	lpfc_sli4_disable_intr(phba);
6722
6723	return;
6724}
6725
6726/**
6727 * lpfc_sli4_hba_unset - Unset the fcoe hba
6728 * @phba: Pointer to HBA context object.
6729 *
6730 * This function is called in the SLI4 code path to reset the HBA's FCoE
6731 * function. The caller is not required to hold any lock. This routine
6732 * issues PCI function reset mailbox command to reset the FCoE function.
6733 * At the end of the function, it calls lpfc_hba_down_post function to
6734 * free any pending commands.
6735 **/
6736static void
6737lpfc_sli4_hba_unset(struct lpfc_hba *phba)
6738{
6739	int wait_cnt = 0;
6740	LPFC_MBOXQ_t *mboxq;
6741
6742	lpfc_stop_hba_timers(phba);
6743	phba->sli4_hba.intr_enable = 0;
6744
6745	/*
6746	 * Gracefully wait out the potential current outstanding asynchronous
6747	 * mailbox command.
6748	 */
6749
6750	/* First, block any pending async mailbox command from posted */
6751	spin_lock_irq(&phba->hbalock);
6752	phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
6753	spin_unlock_irq(&phba->hbalock);
6754	/* Now, trying to wait it out if we can */
6755	while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6756		msleep(10);
6757		if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
6758			break;
6759	}
6760	/* Forcefully release the outstanding mailbox command if timed out */
6761	if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6762		spin_lock_irq(&phba->hbalock);
6763		mboxq = phba->sli.mbox_active;
6764		mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
6765		__lpfc_mbox_cmpl_put(phba, mboxq);
6766		phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6767		phba->sli.mbox_active = NULL;
6768		spin_unlock_irq(&phba->hbalock);
6769	}
6770
6771	/* Tear down the queues in the HBA */
6772	lpfc_sli4_queue_unset(phba);
6773
6774	/* Disable PCI subsystem interrupt */
6775	lpfc_sli4_disable_intr(phba);
6776
6777	/* Stop kthread signal shall trigger work_done one more time */
6778	kthread_stop(phba->worker_thread);
6779
6780	/* Stop the SLI4 device port */
6781	phba->pport->work_port_events = 0;
6782}
6783
6784/**
6785 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
6786 * @pdev: pointer to PCI device
6787 * @pid: pointer to PCI device identifier
6788 *
6789 * This routine is to be called to attach a device with SLI-3 interface spec
6790 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
6791 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
6792 * information of the device and driver to see if the driver state that it can
6793 * support this kind of device. If the match is successful, the driver core
6794 * invokes this routine. If this routine determines it can claim the HBA, it
6795 * does all the initialization that it needs to do to handle the HBA properly.
6796 *
6797 * Return code
6798 * 	0 - driver can claim the device
6799 * 	negative value - driver can not claim the device
6800 **/
6801static int __devinit
6802lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
6803{
6804	struct lpfc_hba   *phba;
6805	struct lpfc_vport *vport = NULL;
6806	struct Scsi_Host  *shost = NULL;
6807	int error;
6808	uint32_t cfg_mode, intr_mode;
6809
6810	/* Allocate memory for HBA structure */
6811	phba = lpfc_hba_alloc(pdev);
6812	if (!phba)
6813		return -ENOMEM;
6814
6815	/* Perform generic PCI device enabling operation */
6816	error = lpfc_enable_pci_dev(phba);
6817	if (error) {
6818		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6819				"1401 Failed to enable pci device.\n");
6820		goto out_free_phba;
6821	}
6822
6823	/* Set up SLI API function jump table for PCI-device group-0 HBAs */
6824	error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
6825	if (error)
6826		goto out_disable_pci_dev;
6827
6828	/* Set up SLI-3 specific device PCI memory space */
6829	error = lpfc_sli_pci_mem_setup(phba);
6830	if (error) {
6831		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6832				"1402 Failed to set up pci memory space.\n");
6833		goto out_disable_pci_dev;
6834	}
6835
6836	/* Set up phase-1 common device driver resources */
6837	error = lpfc_setup_driver_resource_phase1(phba);
6838	if (error) {
6839		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6840				"1403 Failed to set up driver resource.\n");
6841		goto out_unset_pci_mem_s3;
6842	}
6843
6844	/* Set up SLI-3 specific device driver resources */
6845	error = lpfc_sli_driver_resource_setup(phba);
6846	if (error) {
6847		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6848				"1404 Failed to set up driver resource.\n");
6849		goto out_unset_pci_mem_s3;
6850	}
6851
6852	/* Initialize and populate the iocb list per host */
6853	error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
6854	if (error) {
6855		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6856				"1405 Failed to initialize iocb list.\n");
6857		goto out_unset_driver_resource_s3;
6858	}
6859
6860	/* Set up common device driver resources */
6861	error = lpfc_setup_driver_resource_phase2(phba);
6862	if (error) {
6863		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6864				"1406 Failed to set up driver resource.\n");
6865		goto out_free_iocb_list;
6866	}
6867
6868	/* Create SCSI host to the physical port */
6869	error = lpfc_create_shost(phba);
6870	if (error) {
6871		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6872				"1407 Failed to create scsi host.\n");
6873		goto out_unset_driver_resource;
6874	}
6875
6876	/* Configure sysfs attributes */
6877	vport = phba->pport;
6878	error = lpfc_alloc_sysfs_attr(vport);
6879	if (error) {
6880		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6881				"1476 Failed to allocate sysfs attr\n");
6882		goto out_destroy_shost;
6883	}
6884
6885	shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
6886	/* Now, trying to enable interrupt and bring up the device */
6887	cfg_mode = phba->cfg_use_msi;
6888	while (true) {
6889		/* Put device to a known state before enabling interrupt */
6890		lpfc_stop_port(phba);
6891		/* Configure and enable interrupt */
6892		intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
6893		if (intr_mode == LPFC_INTR_ERROR) {
6894			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6895					"0431 Failed to enable interrupt.\n");
6896			error = -ENODEV;
6897			goto out_free_sysfs_attr;
6898		}
6899		/* SLI-3 HBA setup */
6900		if (lpfc_sli_hba_setup(phba)) {
6901			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6902					"1477 Failed to set up hba\n");
6903			error = -ENODEV;
6904			goto out_remove_device;
6905		}
6906
6907		/* Wait 50ms for the interrupts of previous mailbox commands */
6908		msleep(50);
6909		/* Check active interrupts on message signaled interrupts */
6910		if (intr_mode == 0 ||
6911		    phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
6912			/* Log the current active interrupt mode */
6913			phba->intr_mode = intr_mode;
6914			lpfc_log_intr_mode(phba, intr_mode);
6915			break;
6916		} else {
6917			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6918					"0447 Configure interrupt mode (%d) "
6919					"failed active interrupt test.\n",
6920					intr_mode);
6921			/* Disable the current interrupt mode */
6922			lpfc_sli_disable_intr(phba);
6923			/* Try next level of interrupt mode */
6924			cfg_mode = --intr_mode;
6925		}
6926	}
6927
6928	/* Perform post initialization setup */
6929	lpfc_post_init_setup(phba);
6930
6931	/* Check if there are static vports to be created. */
6932	lpfc_create_static_vport(phba);
6933
6934	return 0;
6935
6936out_remove_device:
6937	lpfc_unset_hba(phba);
6938out_free_sysfs_attr:
6939	lpfc_free_sysfs_attr(vport);
6940out_destroy_shost:
6941	lpfc_destroy_shost(phba);
6942out_unset_driver_resource:
6943	lpfc_unset_driver_resource_phase2(phba);
6944out_free_iocb_list:
6945	lpfc_free_iocb_list(phba);
6946out_unset_driver_resource_s3:
6947	lpfc_sli_driver_resource_unset(phba);
6948out_unset_pci_mem_s3:
6949	lpfc_sli_pci_mem_unset(phba);
6950out_disable_pci_dev:
6951	lpfc_disable_pci_dev(phba);
6952	if (shost)
6953		scsi_host_put(shost);
6954out_free_phba:
6955	lpfc_hba_free(phba);
6956	return error;
6957}
6958
6959/**
6960 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
6961 * @pdev: pointer to PCI device
6962 *
6963 * This routine is to be called to disattach a device with SLI-3 interface
6964 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
6965 * removed from PCI bus, it performs all the necessary cleanup for the HBA
6966 * device to be removed from the PCI subsystem properly.
6967 **/
6968static void __devexit
6969lpfc_pci_remove_one_s3(struct pci_dev *pdev)
6970{
6971	struct Scsi_Host  *shost = pci_get_drvdata(pdev);
6972	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
6973	struct lpfc_vport **vports;
6974	struct lpfc_hba   *phba = vport->phba;
6975	int i;
6976	int bars = pci_select_bars(pdev, IORESOURCE_MEM);
6977
6978	spin_lock_irq(&phba->hbalock);
6979	vport->load_flag |= FC_UNLOADING;
6980	spin_unlock_irq(&phba->hbalock);
6981
6982	lpfc_free_sysfs_attr(vport);
6983
6984	/* Release all the vports against this physical port */
6985	vports = lpfc_create_vport_work_array(phba);
6986	if (vports != NULL)
6987		for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
6988			fc_vport_terminate(vports[i]->fc_vport);
6989	lpfc_destroy_vport_work_array(phba, vports);
6990
6991	/* Remove FC host and then SCSI host with the physical port */
6992	fc_remove_host(shost);
6993	scsi_remove_host(shost);
6994	lpfc_cleanup(vport);
6995
6996	/*
6997	 * Bring down the SLI Layer. This step disable all interrupts,
6998	 * clears the rings, discards all mailbox commands, and resets
6999	 * the HBA.
7000	 */
7001
7002	/* HBA interrupt will be diabled after this call */
7003	lpfc_sli_hba_down(phba);
7004	/* Stop kthread signal shall trigger work_done one more time */
7005	kthread_stop(phba->worker_thread);
7006	/* Final cleanup of txcmplq and reset the HBA */
7007	lpfc_sli_brdrestart(phba);
7008
7009	lpfc_stop_hba_timers(phba);
7010	spin_lock_irq(&phba->hbalock);
7011	list_del_init(&vport->listentry);
7012	spin_unlock_irq(&phba->hbalock);
7013
7014	lpfc_debugfs_terminate(vport);
7015
7016	/* Disable interrupt */
7017	lpfc_sli_disable_intr(phba);
7018
7019	pci_set_drvdata(pdev, NULL);
7020	scsi_host_put(shost);
7021
7022	/*
7023	 * Call scsi_free before mem_free since scsi bufs are released to their
7024	 * corresponding pools here.
7025	 */
7026	lpfc_scsi_free(phba);
7027	lpfc_mem_free_all(phba);
7028
7029	dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
7030			  phba->hbqslimp.virt, phba->hbqslimp.phys);
7031
7032	/* Free resources associated with SLI2 interface */
7033	dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7034			  phba->slim2p.virt, phba->slim2p.phys);
7035
7036	/* unmap adapter SLIM and Control Registers */
7037	iounmap(phba->ctrl_regs_memmap_p);
7038	iounmap(phba->slim_memmap_p);
7039
7040	lpfc_hba_free(phba);
7041
7042	pci_release_selected_regions(pdev, bars);
7043	pci_disable_device(pdev);
7044}
7045
7046/**
7047 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
7048 * @pdev: pointer to PCI device
7049 * @msg: power management message
7050 *
7051 * This routine is to be called from the kernel's PCI subsystem to support
7052 * system Power Management (PM) to device with SLI-3 interface spec. When
7053 * PM invokes this method, it quiesces the device by stopping the driver's
7054 * worker thread for the device, turning off device's interrupt and DMA,
7055 * and bring the device offline. Note that as the driver implements the
7056 * minimum PM requirements to a power-aware driver's PM support for the
7057 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
7058 * to the suspend() method call will be treated as SUSPEND and the driver will
7059 * fully reinitialize its device during resume() method call, the driver will
7060 * set device to PCI_D3hot state in PCI config space instead of setting it
7061 * according to the @msg provided by the PM.
7062 *
7063 * Return code
7064 * 	0 - driver suspended the device
7065 * 	Error otherwise
7066 **/
7067static int
7068lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
7069{
7070	struct Scsi_Host *shost = pci_get_drvdata(pdev);
7071	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7072
7073	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7074			"0473 PCI device Power Management suspend.\n");
7075
7076	/* Bring down the device */
7077	lpfc_offline_prep(phba);
7078	lpfc_offline(phba);
7079	kthread_stop(phba->worker_thread);
7080
7081	/* Disable interrupt from device */
7082	lpfc_sli_disable_intr(phba);
7083
7084	/* Save device state to PCI config space */
7085	pci_save_state(pdev);
7086	pci_set_power_state(pdev, PCI_D3hot);
7087
7088	return 0;
7089}
7090
7091/**
7092 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
7093 * @pdev: pointer to PCI device
7094 *
7095 * This routine is to be called from the kernel's PCI subsystem to support
7096 * system Power Management (PM) to device with SLI-3 interface spec. When PM
7097 * invokes this method, it restores the device's PCI config space state and
7098 * fully reinitializes the device and brings it online. Note that as the
7099 * driver implements the minimum PM requirements to a power-aware driver's
7100 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
7101 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
7102 * driver will fully reinitialize its device during resume() method call,
7103 * the device will be set to PCI_D0 directly in PCI config space before
7104 * restoring the state.
7105 *
7106 * Return code
7107 * 	0 - driver suspended the device
7108 * 	Error otherwise
7109 **/
7110static int
7111lpfc_pci_resume_one_s3(struct pci_dev *pdev)
7112{
7113	struct Scsi_Host *shost = pci_get_drvdata(pdev);
7114	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7115	uint32_t intr_mode;
7116	int error;
7117
7118	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7119			"0452 PCI device Power Management resume.\n");
7120
7121	/* Restore device state from PCI config space */
7122	pci_set_power_state(pdev, PCI_D0);
7123	pci_restore_state(pdev);
7124
7125	if (pdev->is_busmaster)
7126		pci_set_master(pdev);
7127
7128	/* Startup the kernel thread for this host adapter. */
7129	phba->worker_thread = kthread_run(lpfc_do_work, phba,
7130					"lpfc_worker_%d", phba->brd_no);
7131	if (IS_ERR(phba->worker_thread)) {
7132		error = PTR_ERR(phba->worker_thread);
7133		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7134				"0434 PM resume failed to start worker "
7135				"thread: error=x%x.\n", error);
7136		return error;
7137	}
7138
7139	/* Configure and enable interrupt */
7140	intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
7141	if (intr_mode == LPFC_INTR_ERROR) {
7142		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7143				"0430 PM resume Failed to enable interrupt\n");
7144		return -EIO;
7145	} else
7146		phba->intr_mode = intr_mode;
7147
7148	/* Restart HBA and bring it online */
7149	lpfc_sli_brdrestart(phba);
7150	lpfc_online(phba);
7151
7152	/* Log the current active interrupt mode */
7153	lpfc_log_intr_mode(phba, phba->intr_mode);
7154
7155	return 0;
7156}
7157
7158/**
7159 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
7160 * @phba: pointer to lpfc hba data structure.
7161 *
7162 * This routine is called to prepare the SLI3 device for PCI slot recover. It
7163 * aborts and stops all the on-going I/Os on the pci device.
7164 **/
7165static void
7166lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
7167{
7168	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7169			"2723 PCI channel I/O abort preparing for recovery\n");
7170	/* Prepare for bringing HBA offline */
7171	lpfc_offline_prep(phba);
7172	/* Clear sli active flag to prevent sysfs access to HBA */
7173	spin_lock_irq(&phba->hbalock);
7174	phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
7175	spin_unlock_irq(&phba->hbalock);
7176	/* Stop and flush all I/Os and bring HBA offline */
7177	lpfc_offline(phba);
7178}
7179
7180/**
7181 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
7182 * @phba: pointer to lpfc hba data structure.
7183 *
7184 * This routine is called to prepare the SLI3 device for PCI slot reset. It
7185 * disables the device interrupt and pci device, and aborts the internal FCP
7186 * pending I/Os.
7187 **/
7188static void
7189lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
7190{
7191	struct lpfc_sli *psli = &phba->sli;
7192	struct lpfc_sli_ring  *pring;
7193
7194	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7195			"2710 PCI channel disable preparing for reset\n");
7196	/* Disable interrupt and pci device */
7197	lpfc_sli_disable_intr(phba);
7198	pci_disable_device(phba->pcidev);
7199	/*
7200	 * There may be I/Os dropped by the firmware.
7201	 * Error iocb (I/O) on txcmplq and let the SCSI layer
7202	 * retry it after re-establishing link.
7203	 */
7204	pring = &psli->ring[psli->fcp_ring];
7205	lpfc_sli_abort_iocb_ring(phba, pring);
7206}
7207
7208/**
7209 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
7210 * @phba: pointer to lpfc hba data structure.
7211 *
7212 * This routine is called to prepare the SLI3 device for PCI slot permanently
7213 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
7214 * pending I/Os.
7215 **/
7216static void
7217lpfc_prep_dev_for_perm_failure(struct lpfc_hba *phba)
7218{
7219	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7220			"2711 PCI channel permanent disable for failure\n");
7221	/* Block all SCSI devices' I/Os on the host */
7222	lpfc_scsi_dev_block(phba);
7223	/* Clean up all driver's outstanding SCSI I/Os */
7224	lpfc_sli_flush_fcp_rings(phba);
7225}
7226
7227/**
7228 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
7229 * @pdev: pointer to PCI device.
7230 * @state: the current PCI connection state.
7231 *
7232 * This routine is called from the PCI subsystem for I/O error handling to
7233 * device with SLI-3 interface spec. This function is called by the PCI
7234 * subsystem after a PCI bus error affecting this device has been detected.
7235 * When this function is invoked, it will need to stop all the I/Os and
7236 * interrupt(s) to the device. Once that is done, it will return
7237 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
7238 * as desired.
7239 *
7240 * Return codes
7241 * 	PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
7242 * 	PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
7243 * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
7244 **/
7245static pci_ers_result_t
7246lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
7247{
7248	struct Scsi_Host *shost = pci_get_drvdata(pdev);
7249	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7250
7251	switch (state) {
7252	case pci_channel_io_normal:
7253		/* Non-fatal error, prepare for recovery */
7254		lpfc_sli_prep_dev_for_recover(phba);
7255		return PCI_ERS_RESULT_CAN_RECOVER;
7256	case pci_channel_io_frozen:
7257		/* Fatal error, prepare for slot reset */
7258		lpfc_sli_prep_dev_for_reset(phba);
7259		return PCI_ERS_RESULT_NEED_RESET;
7260	case pci_channel_io_perm_failure:
7261		/* Permanent failure, prepare for device down */
7262		lpfc_prep_dev_for_perm_failure(phba);
7263		return PCI_ERS_RESULT_DISCONNECT;
7264	default:
7265		/* Unknown state, prepare and request slot reset */
7266		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7267				"0472 Unknown PCI error state: x%x\n", state);
7268		lpfc_sli_prep_dev_for_reset(phba);
7269		return PCI_ERS_RESULT_NEED_RESET;
7270	}
7271}
7272
7273/**
7274 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
7275 * @pdev: pointer to PCI device.
7276 *
7277 * This routine is called from the PCI subsystem for error handling to
7278 * device with SLI-3 interface spec. This is called after PCI bus has been
7279 * reset to restart the PCI card from scratch, as if from a cold-boot.
7280 * During the PCI subsystem error recovery, after driver returns
7281 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
7282 * recovery and then call this routine before calling the .resume method
7283 * to recover the device. This function will initialize the HBA device,
7284 * enable the interrupt, but it will just put the HBA to offline state
7285 * without passing any I/O traffic.
7286 *
7287 * Return codes
7288 * 	PCI_ERS_RESULT_RECOVERED - the device has been recovered
7289 * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
7290 */
7291static pci_ers_result_t
7292lpfc_io_slot_reset_s3(struct pci_dev *pdev)
7293{
7294	struct Scsi_Host *shost = pci_get_drvdata(pdev);
7295	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7296	struct lpfc_sli *psli = &phba->sli;
7297	uint32_t intr_mode;
7298
7299	dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
7300	if (pci_enable_device_mem(pdev)) {
7301		printk(KERN_ERR "lpfc: Cannot re-enable "
7302			"PCI device after reset.\n");
7303		return PCI_ERS_RESULT_DISCONNECT;
7304	}
7305
7306	pci_restore_state(pdev);
7307	if (pdev->is_busmaster)
7308		pci_set_master(pdev);
7309
7310	spin_lock_irq(&phba->hbalock);
7311	psli->sli_flag &= ~LPFC_SLI_ACTIVE;
7312	spin_unlock_irq(&phba->hbalock);
7313
7314	/* Configure and enable interrupt */
7315	intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
7316	if (intr_mode == LPFC_INTR_ERROR) {
7317		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7318				"0427 Cannot re-enable interrupt after "
7319				"slot reset.\n");
7320		return PCI_ERS_RESULT_DISCONNECT;
7321	} else
7322		phba->intr_mode = intr_mode;
7323
7324	/* Take device offline; this will perform cleanup */
7325	lpfc_offline(phba);
7326	lpfc_sli_brdrestart(phba);
7327
7328	/* Log the current active interrupt mode */
7329	lpfc_log_intr_mode(phba, phba->intr_mode);
7330
7331	return PCI_ERS_RESULT_RECOVERED;
7332}
7333
7334/**
7335 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
7336 * @pdev: pointer to PCI device
7337 *
7338 * This routine is called from the PCI subsystem for error handling to device
7339 * with SLI-3 interface spec. It is called when kernel error recovery tells
7340 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
7341 * error recovery. After this call, traffic can start to flow from this device
7342 * again.
7343 */
7344static void
7345lpfc_io_resume_s3(struct pci_dev *pdev)
7346{
7347	struct Scsi_Host *shost = pci_get_drvdata(pdev);
7348	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7349
7350	/* Bring the device online */
7351	lpfc_online(phba);
7352
7353	/* Clean up Advanced Error Reporting (AER) if needed */
7354	if (phba->hba_flag & HBA_AER_ENABLED)
7355		pci_cleanup_aer_uncorrect_error_status(pdev);
7356}
7357
7358/**
7359 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
7360 * @phba: pointer to lpfc hba data structure.
7361 *
7362 * returns the number of ELS/CT IOCBs to reserve
7363 **/
7364int
7365lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
7366{
7367	int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
7368
7369	if (phba->sli_rev == LPFC_SLI_REV4) {
7370		if (max_xri <= 100)
7371			return 10;
7372		else if (max_xri <= 256)
7373			return 25;
7374		else if (max_xri <= 512)
7375			return 50;
7376		else if (max_xri <= 1024)
7377			return 100;
7378		else
7379			return 150;
7380	} else
7381		return 0;
7382}
7383
7384/**
7385 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
7386 * @pdev: pointer to PCI device
7387 * @pid: pointer to PCI device identifier
7388 *
7389 * This routine is called from the kernel's PCI subsystem to device with
7390 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
7391 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
7392 * information of the device and driver to see if the driver state that it
7393 * can support this kind of device. If the match is successful, the driver
7394 * core invokes this routine. If this routine determines it can claim the HBA,
7395 * it does all the initialization that it needs to do to handle the HBA
7396 * properly.
7397 *
7398 * Return code
7399 * 	0 - driver can claim the device
7400 * 	negative value - driver can not claim the device
7401 **/
7402static int __devinit
7403lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
7404{
7405	struct lpfc_hba   *phba;
7406	struct lpfc_vport *vport = NULL;
7407	struct Scsi_Host  *shost = NULL;
7408	int error;
7409	uint32_t cfg_mode, intr_mode;
7410	int mcnt;
7411
7412	/* Allocate memory for HBA structure */
7413	phba = lpfc_hba_alloc(pdev);
7414	if (!phba)
7415		return -ENOMEM;
7416
7417	/* Perform generic PCI device enabling operation */
7418	error = lpfc_enable_pci_dev(phba);
7419	if (error) {
7420		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7421				"1409 Failed to enable pci device.\n");
7422		goto out_free_phba;
7423	}
7424
7425	/* Set up SLI API function jump table for PCI-device group-1 HBAs */
7426	error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
7427	if (error)
7428		goto out_disable_pci_dev;
7429
7430	/* Set up SLI-4 specific device PCI memory space */
7431	error = lpfc_sli4_pci_mem_setup(phba);
7432	if (error) {
7433		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7434				"1410 Failed to set up pci memory space.\n");
7435		goto out_disable_pci_dev;
7436	}
7437
7438	/* Set up phase-1 common device driver resources */
7439	error = lpfc_setup_driver_resource_phase1(phba);
7440	if (error) {
7441		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7442				"1411 Failed to set up driver resource.\n");
7443		goto out_unset_pci_mem_s4;
7444	}
7445
7446	/* Set up SLI-4 Specific device driver resources */
7447	error = lpfc_sli4_driver_resource_setup(phba);
7448	if (error) {
7449		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7450				"1412 Failed to set up driver resource.\n");
7451		goto out_unset_pci_mem_s4;
7452	}
7453
7454	/* Initialize and populate the iocb list per host */
7455	error = lpfc_init_iocb_list(phba,
7456			phba->sli4_hba.max_cfg_param.max_xri);
7457	if (error) {
7458		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7459				"1413 Failed to initialize iocb list.\n");
7460		goto out_unset_driver_resource_s4;
7461	}
7462
7463	/* Set up common device driver resources */
7464	error = lpfc_setup_driver_resource_phase2(phba);
7465	if (error) {
7466		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7467				"1414 Failed to set up driver resource.\n");
7468		goto out_free_iocb_list;
7469	}
7470
7471	/* Create SCSI host to the physical port */
7472	error = lpfc_create_shost(phba);
7473	if (error) {
7474		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7475				"1415 Failed to create scsi host.\n");
7476		goto out_unset_driver_resource;
7477	}
7478
7479	/* Configure sysfs attributes */
7480	vport = phba->pport;
7481	error = lpfc_alloc_sysfs_attr(vport);
7482	if (error) {
7483		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7484				"1416 Failed to allocate sysfs attr\n");
7485		goto out_destroy_shost;
7486	}
7487
7488	shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
7489	/* Now, trying to enable interrupt and bring up the device */
7490	cfg_mode = phba->cfg_use_msi;
7491	while (true) {
7492		/* Put device to a known state before enabling interrupt */
7493		lpfc_stop_port(phba);
7494		/* Configure and enable interrupt */
7495		intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
7496		if (intr_mode == LPFC_INTR_ERROR) {
7497			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7498					"0426 Failed to enable interrupt.\n");
7499			error = -ENODEV;
7500			goto out_free_sysfs_attr;
7501		}
7502		/* Set up SLI-4 HBA */
7503		if (lpfc_sli4_hba_setup(phba)) {
7504			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7505					"1421 Failed to set up hba\n");
7506			error = -ENODEV;
7507			goto out_disable_intr;
7508		}
7509
7510		/* Send NOP mbx cmds for non-INTx mode active interrupt test */
7511		if (intr_mode != 0)
7512			mcnt = lpfc_sli4_send_nop_mbox_cmds(phba,
7513							    LPFC_ACT_INTR_CNT);
7514
7515		/* Check active interrupts received only for MSI/MSI-X */
7516		if (intr_mode == 0 ||
7517		    phba->sli.slistat.sli_intr >= LPFC_ACT_INTR_CNT) {
7518			/* Log the current active interrupt mode */
7519			phba->intr_mode = intr_mode;
7520			lpfc_log_intr_mode(phba, intr_mode);
7521			break;
7522		}
7523		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7524				"0451 Configure interrupt mode (%d) "
7525				"failed active interrupt test.\n",
7526				intr_mode);
7527		/* Unset the preivous SLI-4 HBA setup */
7528		lpfc_sli4_unset_hba(phba);
7529		/* Try next level of interrupt mode */
7530		cfg_mode = --intr_mode;
7531	}
7532
7533	/* Perform post initialization setup */
7534	lpfc_post_init_setup(phba);
7535
7536	/* Check if there are static vports to be created. */
7537	lpfc_create_static_vport(phba);
7538
7539	return 0;
7540
7541out_disable_intr:
7542	lpfc_sli4_disable_intr(phba);
7543out_free_sysfs_attr:
7544	lpfc_free_sysfs_attr(vport);
7545out_destroy_shost:
7546	lpfc_destroy_shost(phba);
7547out_unset_driver_resource:
7548	lpfc_unset_driver_resource_phase2(phba);
7549out_free_iocb_list:
7550	lpfc_free_iocb_list(phba);
7551out_unset_driver_resource_s4:
7552	lpfc_sli4_driver_resource_unset(phba);
7553out_unset_pci_mem_s4:
7554	lpfc_sli4_pci_mem_unset(phba);
7555out_disable_pci_dev:
7556	lpfc_disable_pci_dev(phba);
7557	if (shost)
7558		scsi_host_put(shost);
7559out_free_phba:
7560	lpfc_hba_free(phba);
7561	return error;
7562}
7563
7564/**
7565 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
7566 * @pdev: pointer to PCI device
7567 *
7568 * This routine is called from the kernel's PCI subsystem to device with
7569 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
7570 * removed from PCI bus, it performs all the necessary cleanup for the HBA
7571 * device to be removed from the PCI subsystem properly.
7572 **/
7573static void __devexit
7574lpfc_pci_remove_one_s4(struct pci_dev *pdev)
7575{
7576	struct Scsi_Host *shost = pci_get_drvdata(pdev);
7577	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
7578	struct lpfc_vport **vports;
7579	struct lpfc_hba *phba = vport->phba;
7580	int i;
7581
7582	/* Mark the device unloading flag */
7583	spin_lock_irq(&phba->hbalock);
7584	vport->load_flag |= FC_UNLOADING;
7585	spin_unlock_irq(&phba->hbalock);
7586
7587	/* Free the HBA sysfs attributes */
7588	lpfc_free_sysfs_attr(vport);
7589
7590	/* Release all the vports against this physical port */
7591	vports = lpfc_create_vport_work_array(phba);
7592	if (vports != NULL)
7593		for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
7594			fc_vport_terminate(vports[i]->fc_vport);
7595	lpfc_destroy_vport_work_array(phba, vports);
7596
7597	/* Remove FC host and then SCSI host with the physical port */
7598	fc_remove_host(shost);
7599	scsi_remove_host(shost);
7600
7601	/* Perform cleanup on the physical port */
7602	lpfc_cleanup(vport);
7603
7604	/*
7605	 * Bring down the SLI Layer. This step disables all interrupts,
7606	 * clears the rings, discards all mailbox commands, and resets
7607	 * the HBA FCoE function.
7608	 */
7609	lpfc_debugfs_terminate(vport);
7610	lpfc_sli4_hba_unset(phba);
7611
7612	spin_lock_irq(&phba->hbalock);
7613	list_del_init(&vport->listentry);
7614	spin_unlock_irq(&phba->hbalock);
7615
7616	/* Call scsi_free before lpfc_sli4_driver_resource_unset since scsi
7617	 * buffers are released to their corresponding pools here.
7618	 */
7619	lpfc_scsi_free(phba);
7620	lpfc_sli4_driver_resource_unset(phba);
7621
7622	/* Unmap adapter Control and Doorbell registers */
7623	lpfc_sli4_pci_mem_unset(phba);
7624
7625	/* Release PCI resources and disable device's PCI function */
7626	scsi_host_put(shost);
7627	lpfc_disable_pci_dev(phba);
7628
7629	/* Finally, free the driver's device data structure */
7630	lpfc_hba_free(phba);
7631
7632	return;
7633}
7634
7635/**
7636 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
7637 * @pdev: pointer to PCI device
7638 * @msg: power management message
7639 *
7640 * This routine is called from the kernel's PCI subsystem to support system
7641 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
7642 * this method, it quiesces the device by stopping the driver's worker
7643 * thread for the device, turning off device's interrupt and DMA, and bring
7644 * the device offline. Note that as the driver implements the minimum PM
7645 * requirements to a power-aware driver's PM support for suspend/resume -- all
7646 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
7647 * method call will be treated as SUSPEND and the driver will fully
7648 * reinitialize its device during resume() method call, the driver will set
7649 * device to PCI_D3hot state in PCI config space instead of setting it
7650 * according to the @msg provided by the PM.
7651 *
7652 * Return code
7653 * 	0 - driver suspended the device
7654 * 	Error otherwise
7655 **/
7656static int
7657lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
7658{
7659	struct Scsi_Host *shost = pci_get_drvdata(pdev);
7660	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7661
7662	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7663			"0298 PCI device Power Management suspend.\n");
7664
7665	/* Bring down the device */
7666	lpfc_offline_prep(phba);
7667	lpfc_offline(phba);
7668	kthread_stop(phba->worker_thread);
7669
7670	/* Disable interrupt from device */
7671	lpfc_sli4_disable_intr(phba);
7672
7673	/* Save device state to PCI config space */
7674	pci_save_state(pdev);
7675	pci_set_power_state(pdev, PCI_D3hot);
7676
7677	return 0;
7678}
7679
7680/**
7681 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
7682 * @pdev: pointer to PCI device
7683 *
7684 * This routine is called from the kernel's PCI subsystem to support system
7685 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
7686 * this method, it restores the device's PCI config space state and fully
7687 * reinitializes the device and brings it online. Note that as the driver
7688 * implements the minimum PM requirements to a power-aware driver's PM for
7689 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
7690 * to the suspend() method call will be treated as SUSPEND and the driver
7691 * will fully reinitialize its device during resume() method call, the device
7692 * will be set to PCI_D0 directly in PCI config space before restoring the
7693 * state.
7694 *
7695 * Return code
7696 * 	0 - driver suspended the device
7697 * 	Error otherwise
7698 **/
7699static int
7700lpfc_pci_resume_one_s4(struct pci_dev *pdev)
7701{
7702	struct Scsi_Host *shost = pci_get_drvdata(pdev);
7703	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7704	uint32_t intr_mode;
7705	int error;
7706
7707	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7708			"0292 PCI device Power Management resume.\n");
7709
7710	/* Restore device state from PCI config space */
7711	pci_set_power_state(pdev, PCI_D0);
7712	pci_restore_state(pdev);
7713	if (pdev->is_busmaster)
7714		pci_set_master(pdev);
7715
7716	 /* Startup the kernel thread for this host adapter. */
7717	phba->worker_thread = kthread_run(lpfc_do_work, phba,
7718					"lpfc_worker_%d", phba->brd_no);
7719	if (IS_ERR(phba->worker_thread)) {
7720		error = PTR_ERR(phba->worker_thread);
7721		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7722				"0293 PM resume failed to start worker "
7723				"thread: error=x%x.\n", error);
7724		return error;
7725	}
7726
7727	/* Configure and enable interrupt */
7728	intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
7729	if (intr_mode == LPFC_INTR_ERROR) {
7730		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7731				"0294 PM resume Failed to enable interrupt\n");
7732		return -EIO;
7733	} else
7734		phba->intr_mode = intr_mode;
7735
7736	/* Restart HBA and bring it online */
7737	lpfc_sli_brdrestart(phba);
7738	lpfc_online(phba);
7739
7740	/* Log the current active interrupt mode */
7741	lpfc_log_intr_mode(phba, phba->intr_mode);
7742
7743	return 0;
7744}
7745
7746/**
7747 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
7748 * @pdev: pointer to PCI device.
7749 * @state: the current PCI connection state.
7750 *
7751 * This routine is called from the PCI subsystem for error handling to device
7752 * with SLI-4 interface spec. This function is called by the PCI subsystem
7753 * after a PCI bus error affecting this device has been detected. When this
7754 * function is invoked, it will need to stop all the I/Os and interrupt(s)
7755 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
7756 * for the PCI subsystem to perform proper recovery as desired.
7757 *
7758 * Return codes
7759 * 	PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
7760 * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
7761 **/
7762static pci_ers_result_t
7763lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
7764{
7765	return PCI_ERS_RESULT_NEED_RESET;
7766}
7767
7768/**
7769 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
7770 * @pdev: pointer to PCI device.
7771 *
7772 * This routine is called from the PCI subsystem for error handling to device
7773 * with SLI-4 interface spec. It is called after PCI bus has been reset to
7774 * restart the PCI card from scratch, as if from a cold-boot. During the
7775 * PCI subsystem error recovery, after the driver returns
7776 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
7777 * recovery and then call this routine before calling the .resume method to
7778 * recover the device. This function will initialize the HBA device, enable
7779 * the interrupt, but it will just put the HBA to offline state without
7780 * passing any I/O traffic.
7781 *
7782 * Return codes
7783 * 	PCI_ERS_RESULT_RECOVERED - the device has been recovered
7784 * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
7785 */
7786static pci_ers_result_t
7787lpfc_io_slot_reset_s4(struct pci_dev *pdev)
7788{
7789	return PCI_ERS_RESULT_RECOVERED;
7790}
7791
7792/**
7793 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
7794 * @pdev: pointer to PCI device
7795 *
7796 * This routine is called from the PCI subsystem for error handling to device
7797 * with SLI-4 interface spec. It is called when kernel error recovery tells
7798 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
7799 * error recovery. After this call, traffic can start to flow from this device
7800 * again.
7801 **/
7802static void
7803lpfc_io_resume_s4(struct pci_dev *pdev)
7804{
7805	return;
7806}
7807
7808/**
7809 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
7810 * @pdev: pointer to PCI device
7811 * @pid: pointer to PCI device identifier
7812 *
7813 * This routine is to be registered to the kernel's PCI subsystem. When an
7814 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
7815 * at PCI device-specific information of the device and driver to see if the
7816 * driver state that it can support this kind of device. If the match is
7817 * successful, the driver core invokes this routine. This routine dispatches
7818 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
7819 * do all the initialization that it needs to do to handle the HBA device
7820 * properly.
7821 *
7822 * Return code
7823 * 	0 - driver can claim the device
7824 * 	negative value - driver can not claim the device
7825 **/
7826static int __devinit
7827lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
7828{
7829	int rc;
7830	struct lpfc_sli_intf intf;
7831
7832	if (pci_read_config_dword(pdev, LPFC_SLIREV_CONF_WORD, &intf.word0))
7833		return -ENODEV;
7834
7835	if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
7836		(bf_get(lpfc_sli_intf_rev, &intf) == LPFC_SLIREV_CONF_SLI4))
7837		rc = lpfc_pci_probe_one_s4(pdev, pid);
7838	else
7839		rc = lpfc_pci_probe_one_s3(pdev, pid);
7840
7841	return rc;
7842}
7843
7844/**
7845 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
7846 * @pdev: pointer to PCI device
7847 *
7848 * This routine is to be registered to the kernel's PCI subsystem. When an
7849 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
7850 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
7851 * remove routine, which will perform all the necessary cleanup for the
7852 * device to be removed from the PCI subsystem properly.
7853 **/
7854static void __devexit
7855lpfc_pci_remove_one(struct pci_dev *pdev)
7856{
7857	struct Scsi_Host *shost = pci_get_drvdata(pdev);
7858	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7859
7860	switch (phba->pci_dev_grp) {
7861	case LPFC_PCI_DEV_LP:
7862		lpfc_pci_remove_one_s3(pdev);
7863		break;
7864	case LPFC_PCI_DEV_OC:
7865		lpfc_pci_remove_one_s4(pdev);
7866		break;
7867	default:
7868		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7869				"1424 Invalid PCI device group: 0x%x\n",
7870				phba->pci_dev_grp);
7871		break;
7872	}
7873	return;
7874}
7875
7876/**
7877 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
7878 * @pdev: pointer to PCI device
7879 * @msg: power management message
7880 *
7881 * This routine is to be registered to the kernel's PCI subsystem to support
7882 * system Power Management (PM). When PM invokes this method, it dispatches
7883 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
7884 * suspend the device.
7885 *
7886 * Return code
7887 * 	0 - driver suspended the device
7888 * 	Error otherwise
7889 **/
7890static int
7891lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
7892{
7893	struct Scsi_Host *shost = pci_get_drvdata(pdev);
7894	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7895	int rc = -ENODEV;
7896
7897	switch (phba->pci_dev_grp) {
7898	case LPFC_PCI_DEV_LP:
7899		rc = lpfc_pci_suspend_one_s3(pdev, msg);
7900		break;
7901	case LPFC_PCI_DEV_OC:
7902		rc = lpfc_pci_suspend_one_s4(pdev, msg);
7903		break;
7904	default:
7905		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7906				"1425 Invalid PCI device group: 0x%x\n",
7907				phba->pci_dev_grp);
7908		break;
7909	}
7910	return rc;
7911}
7912
7913/**
7914 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
7915 * @pdev: pointer to PCI device
7916 *
7917 * This routine is to be registered to the kernel's PCI subsystem to support
7918 * system Power Management (PM). When PM invokes this method, it dispatches
7919 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
7920 * resume the device.
7921 *
7922 * Return code
7923 * 	0 - driver suspended the device
7924 * 	Error otherwise
7925 **/
7926static int
7927lpfc_pci_resume_one(struct pci_dev *pdev)
7928{
7929	struct Scsi_Host *shost = pci_get_drvdata(pdev);
7930	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7931	int rc = -ENODEV;
7932
7933	switch (phba->pci_dev_grp) {
7934	case LPFC_PCI_DEV_LP:
7935		rc = lpfc_pci_resume_one_s3(pdev);
7936		break;
7937	case LPFC_PCI_DEV_OC:
7938		rc = lpfc_pci_resume_one_s4(pdev);
7939		break;
7940	default:
7941		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7942				"1426 Invalid PCI device group: 0x%x\n",
7943				phba->pci_dev_grp);
7944		break;
7945	}
7946	return rc;
7947}
7948
7949/**
7950 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
7951 * @pdev: pointer to PCI device.
7952 * @state: the current PCI connection state.
7953 *
7954 * This routine is registered to the PCI subsystem for error handling. This
7955 * function is called by the PCI subsystem after a PCI bus error affecting
7956 * this device has been detected. When this routine is invoked, it dispatches
7957 * the action to the proper SLI-3 or SLI-4 device error detected handling
7958 * routine, which will perform the proper error detected operation.
7959 *
7960 * Return codes
7961 * 	PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
7962 * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
7963 **/
7964static pci_ers_result_t
7965lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
7966{
7967	struct Scsi_Host *shost = pci_get_drvdata(pdev);
7968	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7969	pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
7970
7971	switch (phba->pci_dev_grp) {
7972	case LPFC_PCI_DEV_LP:
7973		rc = lpfc_io_error_detected_s3(pdev, state);
7974		break;
7975	case LPFC_PCI_DEV_OC:
7976		rc = lpfc_io_error_detected_s4(pdev, state);
7977		break;
7978	default:
7979		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7980				"1427 Invalid PCI device group: 0x%x\n",
7981				phba->pci_dev_grp);
7982		break;
7983	}
7984	return rc;
7985}
7986
7987/**
7988 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
7989 * @pdev: pointer to PCI device.
7990 *
7991 * This routine is registered to the PCI subsystem for error handling. This
7992 * function is called after PCI bus has been reset to restart the PCI card
7993 * from scratch, as if from a cold-boot. When this routine is invoked, it
7994 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
7995 * routine, which will perform the proper device reset.
7996 *
7997 * Return codes
7998 * 	PCI_ERS_RESULT_RECOVERED - the device has been recovered
7999 * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8000 **/
8001static pci_ers_result_t
8002lpfc_io_slot_reset(struct pci_dev *pdev)
8003{
8004	struct Scsi_Host *shost = pci_get_drvdata(pdev);
8005	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8006	pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
8007
8008	switch (phba->pci_dev_grp) {
8009	case LPFC_PCI_DEV_LP:
8010		rc = lpfc_io_slot_reset_s3(pdev);
8011		break;
8012	case LPFC_PCI_DEV_OC:
8013		rc = lpfc_io_slot_reset_s4(pdev);
8014		break;
8015	default:
8016		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8017				"1428 Invalid PCI device group: 0x%x\n",
8018				phba->pci_dev_grp);
8019		break;
8020	}
8021	return rc;
8022}
8023
8024/**
8025 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
8026 * @pdev: pointer to PCI device
8027 *
8028 * This routine is registered to the PCI subsystem for error handling. It
8029 * is called when kernel error recovery tells the lpfc driver that it is
8030 * OK to resume normal PCI operation after PCI bus error recovery. When
8031 * this routine is invoked, it dispatches the action to the proper SLI-3
8032 * or SLI-4 device io_resume routine, which will resume the device operation.
8033 **/
8034static void
8035lpfc_io_resume(struct pci_dev *pdev)
8036{
8037	struct Scsi_Host *shost = pci_get_drvdata(pdev);
8038	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8039
8040	switch (phba->pci_dev_grp) {
8041	case LPFC_PCI_DEV_LP:
8042		lpfc_io_resume_s3(pdev);
8043		break;
8044	case LPFC_PCI_DEV_OC:
8045		lpfc_io_resume_s4(pdev);
8046		break;
8047	default:
8048		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8049				"1429 Invalid PCI device group: 0x%x\n",
8050				phba->pci_dev_grp);
8051		break;
8052	}
8053	return;
8054}
8055
8056static struct pci_device_id lpfc_id_table[] = {
8057	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
8058		PCI_ANY_ID, PCI_ANY_ID, },
8059	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
8060		PCI_ANY_ID, PCI_ANY_ID, },
8061	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
8062		PCI_ANY_ID, PCI_ANY_ID, },
8063	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
8064		PCI_ANY_ID, PCI_ANY_ID, },
8065	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
8066		PCI_ANY_ID, PCI_ANY_ID, },
8067	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
8068		PCI_ANY_ID, PCI_ANY_ID, },
8069	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
8070		PCI_ANY_ID, PCI_ANY_ID, },
8071	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
8072		PCI_ANY_ID, PCI_ANY_ID, },
8073	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
8074		PCI_ANY_ID, PCI_ANY_ID, },
8075	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
8076		PCI_ANY_ID, PCI_ANY_ID, },
8077	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
8078		PCI_ANY_ID, PCI_ANY_ID, },
8079	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
8080		PCI_ANY_ID, PCI_ANY_ID, },
8081	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
8082		PCI_ANY_ID, PCI_ANY_ID, },
8083	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
8084		PCI_ANY_ID, PCI_ANY_ID, },
8085	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
8086		PCI_ANY_ID, PCI_ANY_ID, },
8087	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
8088		PCI_ANY_ID, PCI_ANY_ID, },
8089	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
8090		PCI_ANY_ID, PCI_ANY_ID, },
8091	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
8092		PCI_ANY_ID, PCI_ANY_ID, },
8093	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
8094		PCI_ANY_ID, PCI_ANY_ID, },
8095	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
8096		PCI_ANY_ID, PCI_ANY_ID, },
8097	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
8098		PCI_ANY_ID, PCI_ANY_ID, },
8099	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
8100		PCI_ANY_ID, PCI_ANY_ID, },
8101	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
8102		PCI_ANY_ID, PCI_ANY_ID, },
8103	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
8104		PCI_ANY_ID, PCI_ANY_ID, },
8105	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
8106		PCI_ANY_ID, PCI_ANY_ID, },
8107	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
8108		PCI_ANY_ID, PCI_ANY_ID, },
8109	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
8110		PCI_ANY_ID, PCI_ANY_ID, },
8111	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
8112		PCI_ANY_ID, PCI_ANY_ID, },
8113	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
8114		PCI_ANY_ID, PCI_ANY_ID, },
8115	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
8116		PCI_ANY_ID, PCI_ANY_ID, },
8117	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
8118		PCI_ANY_ID, PCI_ANY_ID, },
8119	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
8120		PCI_ANY_ID, PCI_ANY_ID, },
8121	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
8122		PCI_ANY_ID, PCI_ANY_ID, },
8123	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
8124		PCI_ANY_ID, PCI_ANY_ID, },
8125	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
8126		PCI_ANY_ID, PCI_ANY_ID, },
8127	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
8128		PCI_ANY_ID, PCI_ANY_ID, },
8129	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
8130		PCI_ANY_ID, PCI_ANY_ID, },
8131	{PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
8132		PCI_ANY_ID, PCI_ANY_ID, },
8133	{PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TOMCAT,
8134		PCI_ANY_ID, PCI_ANY_ID, },
8135	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FALCON,
8136		PCI_ANY_ID, PCI_ANY_ID, },
8137	{ 0 }
8138};
8139
8140MODULE_DEVICE_TABLE(pci, lpfc_id_table);
8141
8142static struct pci_error_handlers lpfc_err_handler = {
8143	.error_detected = lpfc_io_error_detected,
8144	.slot_reset = lpfc_io_slot_reset,
8145	.resume = lpfc_io_resume,
8146};
8147
8148static struct pci_driver lpfc_driver = {
8149	.name		= LPFC_DRIVER_NAME,
8150	.id_table	= lpfc_id_table,
8151	.probe		= lpfc_pci_probe_one,
8152	.remove		= __devexit_p(lpfc_pci_remove_one),
8153	.suspend        = lpfc_pci_suspend_one,
8154	.resume		= lpfc_pci_resume_one,
8155	.err_handler    = &lpfc_err_handler,
8156};
8157
8158/**
8159 * lpfc_init - lpfc module initialization routine
8160 *
8161 * This routine is to be invoked when the lpfc module is loaded into the
8162 * kernel. The special kernel macro module_init() is used to indicate the
8163 * role of this routine to the kernel as lpfc module entry point.
8164 *
8165 * Return codes
8166 *   0 - successful
8167 *   -ENOMEM - FC attach transport failed
8168 *   all others - failed
8169 */
8170static int __init
8171lpfc_init(void)
8172{
8173	int error = 0;
8174
8175	printk(LPFC_MODULE_DESC "\n");
8176	printk(LPFC_COPYRIGHT "\n");
8177
8178	if (lpfc_enable_npiv) {
8179		lpfc_transport_functions.vport_create = lpfc_vport_create;
8180		lpfc_transport_functions.vport_delete = lpfc_vport_delete;
8181	}
8182	lpfc_transport_template =
8183				fc_attach_transport(&lpfc_transport_functions);
8184	if (lpfc_transport_template == NULL)
8185		return -ENOMEM;
8186	if (lpfc_enable_npiv) {
8187		lpfc_vport_transport_template =
8188			fc_attach_transport(&lpfc_vport_transport_functions);
8189		if (lpfc_vport_transport_template == NULL) {
8190			fc_release_transport(lpfc_transport_template);
8191			return -ENOMEM;
8192		}
8193	}
8194	error = pci_register_driver(&lpfc_driver);
8195	if (error) {
8196		fc_release_transport(lpfc_transport_template);
8197		if (lpfc_enable_npiv)
8198			fc_release_transport(lpfc_vport_transport_template);
8199	}
8200
8201	return error;
8202}
8203
8204/**
8205 * lpfc_exit - lpfc module removal routine
8206 *
8207 * This routine is invoked when the lpfc module is removed from the kernel.
8208 * The special kernel macro module_exit() is used to indicate the role of
8209 * this routine to the kernel as lpfc module exit point.
8210 */
8211static void __exit
8212lpfc_exit(void)
8213{
8214	pci_unregister_driver(&lpfc_driver);
8215	fc_release_transport(lpfc_transport_template);
8216	if (lpfc_enable_npiv)
8217		fc_release_transport(lpfc_vport_transport_template);
8218	if (_dump_buf_data) {
8219		printk(KERN_ERR	"9062 BLKGRD: freeing %lu pages for "
8220				"_dump_buf_data at 0x%p\n",
8221				(1L << _dump_buf_data_order), _dump_buf_data);
8222		free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
8223	}
8224
8225	if (_dump_buf_dif) {
8226		printk(KERN_ERR	"9049 BLKGRD: freeing %lu pages for "
8227				"_dump_buf_dif at 0x%p\n",
8228				(1L << _dump_buf_dif_order), _dump_buf_dif);
8229		free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
8230	}
8231}
8232
8233module_init(lpfc_init);
8234module_exit(lpfc_exit);
8235MODULE_LICENSE("GPL");
8236MODULE_DESCRIPTION(LPFC_MODULE_DESC);
8237MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
8238MODULE_VERSION("0:" LPFC_DRIVER_VERSION);
8239