lpfc_init.c revision 2e0fef85e098f6794956b8b80b111179fbb4cbb7
1/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for         *
3 * Fibre Channel Host Bus Adapters.                                *
4 * Copyright (C) 2004-2007 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
31#include <scsi/scsi.h>
32#include <scsi/scsi_device.h>
33#include <scsi/scsi_host.h>
34#include <scsi/scsi_transport_fc.h>
35
36#include "lpfc_hw.h"
37#include "lpfc_sli.h"
38#include "lpfc_disc.h"
39#include "lpfc_scsi.h"
40#include "lpfc.h"
41#include "lpfc_logmsg.h"
42#include "lpfc_crtn.h"
43#include "lpfc_version.h"
44
45static int lpfc_parse_vpd(struct lpfc_hba *, uint8_t *, int);
46static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
47static int lpfc_post_rcv_buf(struct lpfc_hba *);
48
49static struct scsi_transport_template *lpfc_transport_template = NULL;
50static DEFINE_IDR(lpfc_hba_index);
51
52
53
54/************************************************************************/
55/*                                                                      */
56/*    lpfc_config_port_prep                                             */
57/*    This routine will do LPFC initialization prior to the             */
58/*    CONFIG_PORT mailbox command. This will be initialized             */
59/*    as a SLI layer callback routine.                                  */
60/*    This routine returns 0 on success or -ERESTART if it wants        */
61/*    the SLI layer to reset the HBA and try again. Any                 */
62/*    other return value indicates an error.                            */
63/*                                                                      */
64/************************************************************************/
65int
66lpfc_config_port_prep(struct lpfc_hba *phba)
67{
68	lpfc_vpd_t *vp = &phba->vpd;
69	int i = 0, rc;
70	LPFC_MBOXQ_t *pmb;
71	MAILBOX_t *mb;
72	char *lpfc_vpd_data = NULL;
73	uint16_t offset = 0;
74	static char licensed[56] =
75		    "key unlock for use with gnu public licensed code only\0";
76	static int init_key = 1;
77
78	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
79	if (!pmb) {
80		phba->link_state = LPFC_HBA_ERROR;
81		return -ENOMEM;
82	}
83
84	mb = &pmb->mb;
85	phba->link_state = LPFC_INIT_MBX_CMDS;
86
87	if (lpfc_is_LC_HBA(phba->pcidev->device)) {
88		if (init_key) {
89			uint32_t *ptext = (uint32_t *) licensed;
90
91			for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
92				*ptext = cpu_to_be32(*ptext);
93			init_key = 0;
94		}
95
96		lpfc_read_nv(phba, pmb);
97		memset((char*)mb->un.varRDnvp.rsvd3, 0,
98			sizeof (mb->un.varRDnvp.rsvd3));
99		memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
100			 sizeof (licensed));
101
102		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
103
104		if (rc != MBX_SUCCESS) {
105			lpfc_printf_log(phba,
106					KERN_ERR,
107					LOG_MBOX,
108					"%d:0324 Config Port initialization "
109					"error, mbxCmd x%x READ_NVPARM, "
110					"mbxStatus x%x\n",
111					phba->brd_no,
112					mb->mbxCommand, mb->mbxStatus);
113			mempool_free(pmb, phba->mbox_mem_pool);
114			return -ERESTART;
115		}
116		memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
117		       sizeof(phba->wwnn));
118		memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
119		       sizeof(phba->wwpn));
120	}
121
122	/* Setup and issue mailbox READ REV command */
123	lpfc_read_rev(phba, pmb);
124	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
125	if (rc != MBX_SUCCESS) {
126		lpfc_printf_log(phba,
127				KERN_ERR,
128				LOG_INIT,
129				"%d:0439 Adapter failed to init, mbxCmd x%x "
130				"READ_REV, mbxStatus x%x\n",
131				phba->brd_no,
132				mb->mbxCommand, mb->mbxStatus);
133		mempool_free( pmb, phba->mbox_mem_pool);
134		return -ERESTART;
135	}
136
137	/*
138	 * The value of rr must be 1 since the driver set the cv field to 1.
139	 * This setting requires the FW to set all revision fields.
140	 */
141	if (mb->un.varRdRev.rr == 0) {
142		vp->rev.rBit = 0;
143		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
144				"%d:0440 Adapter failed to init, READ_REV has "
145				"missing revision information.\n",
146				phba->brd_no);
147		mempool_free(pmb, phba->mbox_mem_pool);
148		return -ERESTART;
149	}
150
151	/* Save information as VPD data */
152	vp->rev.rBit = 1;
153	vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
154	memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
155	vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
156	memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
157	vp->rev.biuRev = mb->un.varRdRev.biuRev;
158	vp->rev.smRev = mb->un.varRdRev.smRev;
159	vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
160	vp->rev.endecRev = mb->un.varRdRev.endecRev;
161	vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
162	vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
163	vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
164	vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
165	vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
166	vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
167
168	if (lpfc_is_LC_HBA(phba->pcidev->device))
169		memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
170						sizeof (phba->RandomData));
171
172	/* Get adapter VPD information */
173	pmb->context2 = kmalloc(DMP_RSP_SIZE, GFP_KERNEL);
174	if (!pmb->context2)
175		goto out_free_mbox;
176	lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
177	if (!lpfc_vpd_data)
178		goto out_free_context2;
179
180	do {
181		lpfc_dump_mem(phba, pmb, offset);
182		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
183
184		if (rc != MBX_SUCCESS) {
185			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
186					"%d:0441 VPD not present on adapter, "
187					"mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
188					phba->brd_no,
189					mb->mbxCommand, mb->mbxStatus);
190			mb->un.varDmp.word_cnt = 0;
191		}
192		if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
193			mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
194		lpfc_sli_pcimem_bcopy(pmb->context2, lpfc_vpd_data + offset,
195							mb->un.varDmp.word_cnt);
196		offset += mb->un.varDmp.word_cnt;
197	} while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
198	lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
199
200	kfree(lpfc_vpd_data);
201out_free_context2:
202	kfree(pmb->context2);
203out_free_mbox:
204	mempool_free(pmb, phba->mbox_mem_pool);
205	return 0;
206}
207
208/************************************************************************/
209/*                                                                      */
210/*    lpfc_config_port_post                                             */
211/*    This routine will do LPFC initialization after the                */
212/*    CONFIG_PORT mailbox command. This will be initialized             */
213/*    as a SLI layer callback routine.                                  */
214/*    This routine returns 0 on success. Any other return value         */
215/*    indicates an error.                                               */
216/*                                                                      */
217/************************************************************************/
218int
219lpfc_config_port_post(struct lpfc_hba *phba)
220{
221	struct lpfc_vport *vport = phba->pport;
222	LPFC_MBOXQ_t *pmb;
223	MAILBOX_t *mb;
224	struct lpfc_dmabuf *mp;
225	struct lpfc_sli *psli = &phba->sli;
226	uint32_t status, timeout;
227	int i, j;
228	int rc;
229
230	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
231	if (!pmb) {
232		phba->link_state = LPFC_HBA_ERROR;
233		return -ENOMEM;
234	}
235	mb = &pmb->mb;
236
237	/* Get login parameters for NID.  */
238	lpfc_read_sparam(phba, pmb);
239	if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
240		lpfc_printf_log(phba,
241				KERN_ERR,
242				LOG_INIT,
243				"%d:0448 Adapter failed init, mbxCmd x%x "
244				"READ_SPARM mbxStatus x%x\n",
245				phba->brd_no,
246				mb->mbxCommand, mb->mbxStatus);
247		phba->link_state = LPFC_HBA_ERROR;
248		mp = (struct lpfc_dmabuf *) pmb->context1;
249		mempool_free( pmb, phba->mbox_mem_pool);
250		lpfc_mbuf_free(phba, mp->virt, mp->phys);
251		kfree(mp);
252		return -EIO;
253	}
254
255	mp = (struct lpfc_dmabuf *) pmb->context1;
256
257	memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
258	lpfc_mbuf_free(phba, mp->virt, mp->phys);
259	kfree(mp);
260	pmb->context1 = NULL;
261
262	if (phba->cfg_soft_wwnn)
263		u64_to_wwn(phba->cfg_soft_wwnn,
264			   vport->fc_sparam.nodeName.u.wwn);
265	if (phba->cfg_soft_wwpn)
266		u64_to_wwn(phba->cfg_soft_wwpn,
267			   vport->fc_sparam.portName.u.wwn);
268	memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
269	       sizeof (struct lpfc_name));
270	memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
271	       sizeof (struct lpfc_name));
272	/* If no serial number in VPD data, use low 6 bytes of WWNN */
273	/* This should be consolidated into parse_vpd ? - mr */
274	if (phba->SerialNumber[0] == 0) {
275		uint8_t *outptr;
276
277		outptr = &vport->fc_nodename.u.s.IEEE[0];
278		for (i = 0; i < 12; i++) {
279			status = *outptr++;
280			j = ((status & 0xf0) >> 4);
281			if (j <= 9)
282				phba->SerialNumber[i] =
283				    (char)((uint8_t) 0x30 + (uint8_t) j);
284			else
285				phba->SerialNumber[i] =
286				    (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
287			i++;
288			j = (status & 0xf);
289			if (j <= 9)
290				phba->SerialNumber[i] =
291				    (char)((uint8_t) 0x30 + (uint8_t) j);
292			else
293				phba->SerialNumber[i] =
294				    (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
295		}
296	}
297
298	lpfc_read_config(phba, pmb);
299	if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
300		lpfc_printf_log(phba,
301				KERN_ERR,
302				LOG_INIT,
303				"%d:0453 Adapter failed to init, mbxCmd x%x "
304				"READ_CONFIG, mbxStatus x%x\n",
305				phba->brd_no,
306				mb->mbxCommand, mb->mbxStatus);
307		phba->link_state = LPFC_HBA_ERROR;
308		mempool_free( pmb, phba->mbox_mem_pool);
309		return -EIO;
310	}
311
312	/* Reset the DFT_HBA_Q_DEPTH to the max xri  */
313	if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
314		phba->cfg_hba_queue_depth =
315			mb->un.varRdConfig.max_xri + 1;
316
317	phba->lmt = mb->un.varRdConfig.lmt;
318
319	/* Get the default values for Model Name and Description */
320	lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
321
322	if ((phba->cfg_link_speed > LINK_SPEED_10G)
323	    || ((phba->cfg_link_speed == LINK_SPEED_1G)
324		&& !(phba->lmt & LMT_1Gb))
325	    || ((phba->cfg_link_speed == LINK_SPEED_2G)
326		&& !(phba->lmt & LMT_2Gb))
327	    || ((phba->cfg_link_speed == LINK_SPEED_4G)
328		&& !(phba->lmt & LMT_4Gb))
329	    || ((phba->cfg_link_speed == LINK_SPEED_8G)
330		&& !(phba->lmt & LMT_8Gb))
331	    || ((phba->cfg_link_speed == LINK_SPEED_10G)
332		&& !(phba->lmt & LMT_10Gb))) {
333		/* Reset link speed to auto */
334		lpfc_printf_log(phba,
335			KERN_WARNING,
336			LOG_LINK_EVENT,
337			"%d:1302 Invalid speed for this board: "
338			"Reset link speed to auto: x%x\n",
339			phba->brd_no,
340			phba->cfg_link_speed);
341			phba->cfg_link_speed = LINK_SPEED_AUTO;
342	}
343
344	phba->link_state = LPFC_LINK_DOWN;
345
346	/* Only process IOCBs on ring 0 till hba_state is READY */
347	if (psli->ring[psli->extra_ring].cmdringaddr)
348		psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
349	if (psli->ring[psli->fcp_ring].cmdringaddr)
350		psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
351	if (psli->ring[psli->next_ring].cmdringaddr)
352		psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
353
354	/* Post receive buffers for desired rings */
355	lpfc_post_rcv_buf(phba);
356
357	/* Enable appropriate host interrupts */
358	spin_lock_irq(&phba->hbalock);
359	status = readl(phba->HCregaddr);
360	status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
361	if (psli->num_rings > 0)
362		status |= HC_R0INT_ENA;
363	if (psli->num_rings > 1)
364		status |= HC_R1INT_ENA;
365	if (psli->num_rings > 2)
366		status |= HC_R2INT_ENA;
367	if (psli->num_rings > 3)
368		status |= HC_R3INT_ENA;
369
370	if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
371	    (phba->cfg_poll & DISABLE_FCP_RING_INT))
372		status &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
373
374	writel(status, phba->HCregaddr);
375	readl(phba->HCregaddr); /* flush */
376	spin_unlock_irq(&phba->hbalock);
377
378	/*
379	 * Setup the ring 0 (els)  timeout handler
380	 */
381	timeout = phba->fc_ratov << 1;
382	mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
383
384	lpfc_init_link(phba, pmb, phba->cfg_topology, phba->cfg_link_speed);
385	pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
386	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
387	lpfc_set_loopback_flag(phba);
388	if (rc != MBX_SUCCESS) {
389		lpfc_printf_log(phba,
390				KERN_ERR,
391				LOG_INIT,
392				"%d:0454 Adapter failed to init, mbxCmd x%x "
393				"INIT_LINK, mbxStatus x%x\n",
394				phba->brd_no,
395				mb->mbxCommand, mb->mbxStatus);
396
397		/* Clear all interrupt enable conditions */
398		writel(0, phba->HCregaddr);
399		readl(phba->HCregaddr); /* flush */
400		/* Clear all pending interrupts */
401		writel(0xffffffff, phba->HAregaddr);
402		readl(phba->HAregaddr); /* flush */
403
404		phba->link_state = LPFC_HBA_ERROR;
405		if (rc != MBX_BUSY)
406			mempool_free(pmb, phba->mbox_mem_pool);
407		return -EIO;
408	}
409	/* MBOX buffer will be freed in mbox compl */
410
411	return (0);
412}
413
414/************************************************************************/
415/*                                                                      */
416/*    lpfc_hba_down_prep                                                */
417/*    This routine will do LPFC uninitialization before the             */
418/*    HBA is reset when bringing down the SLI Layer. This will be       */
419/*    initialized as a SLI layer callback routine.                      */
420/*    This routine returns 0 on success. Any other return value         */
421/*    indicates an error.                                               */
422/*                                                                      */
423/************************************************************************/
424int
425lpfc_hba_down_prep(struct lpfc_hba *phba)
426{
427	struct lpfc_vport *vport = phba->pport;
428
429	/* Disable interrupts */
430	writel(0, phba->HCregaddr);
431	readl(phba->HCregaddr); /* flush */
432
433				/* Cleanup potential discovery resources */
434	lpfc_els_flush_rscn(vport);
435	lpfc_els_flush_cmd(vport);
436	lpfc_disc_flush_list(vport);
437
438	return 0;
439}
440
441/************************************************************************/
442/*                                                                      */
443/*    lpfc_hba_down_post                                                */
444/*    This routine will do uninitialization after the HBA is reset      */
445/*    when bringing down the SLI Layer.                                 */
446/*    This routine returns 0 on success. Any other return value         */
447/*    indicates an error.                                               */
448/*                                                                      */
449/************************************************************************/
450int
451lpfc_hba_down_post(struct lpfc_hba *phba)
452{
453	struct lpfc_sli *psli = &phba->sli;
454	struct lpfc_sli_ring *pring;
455	struct lpfc_dmabuf *mp, *next_mp;
456	int i;
457
458	/* Cleanup preposted buffers on the ELS ring */
459	pring = &psli->ring[LPFC_ELS_RING];
460	list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
461		list_del(&mp->list);
462		pring->postbufq_cnt--;
463		lpfc_mbuf_free(phba, mp->virt, mp->phys);
464		kfree(mp);
465	}
466
467	for (i = 0; i < psli->num_rings; i++) {
468		pring = &psli->ring[i];
469		lpfc_sli_abort_iocb_ring(phba, pring);
470	}
471
472	return 0;
473}
474
475/************************************************************************/
476/*                                                                      */
477/*    lpfc_handle_eratt                                                 */
478/*    This routine will handle processing a Host Attention              */
479/*    Error Status event. This will be initialized                      */
480/*    as a SLI layer callback routine.                                  */
481/*                                                                      */
482/************************************************************************/
483void
484lpfc_handle_eratt(struct lpfc_hba *phba)
485{
486	struct lpfc_vport *vport = phba->pport;
487	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
488	struct lpfc_sli   *psli = &phba->sli;
489	struct lpfc_sli_ring  *pring;
490	uint32_t event_data;
491
492	/* If the pci channel is offline, ignore possible errors,
493	 * since we cannot communicate with the pci card anyway. */
494	if (pci_channel_offline(phba->pcidev))
495		return;
496
497	if (phba->work_hs & HS_FFER6 ||
498	    phba->work_hs & HS_FFER5) {
499		/* Re-establishing Link */
500		lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
501				"%d:1301 Re-establishing Link "
502				"Data: x%x x%x x%x\n",
503				phba->brd_no, phba->work_hs,
504				phba->work_status[0], phba->work_status[1]);
505		spin_lock_irq(shost->host_lock);
506		vport->fc_flag |= FC_ESTABLISH_LINK;
507		psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
508		spin_unlock_irq(shost->host_lock);
509
510		/*
511		* Firmware stops when it triggled erratt with HS_FFER6.
512		* That could cause the I/Os dropped by the firmware.
513		* Error iocb (I/O) on txcmplq and let the SCSI layer
514		* retry it after re-establishing link.
515		*/
516		pring = &psli->ring[psli->fcp_ring];
517		lpfc_sli_abort_iocb_ring(phba, pring);
518
519
520		/*
521		 * There was a firmware error.  Take the hba offline and then
522		 * attempt to restart it.
523		 */
524		lpfc_offline_prep(phba);
525		lpfc_offline(phba);
526		lpfc_sli_brdrestart(phba);
527		if (lpfc_online(phba) == 0) {	/* Initialize the HBA */
528			mod_timer(&phba->fc_estabtmo, jiffies + HZ * 60);
529			lpfc_unblock_mgmt_io(phba);
530			return;
531		}
532		lpfc_unblock_mgmt_io(phba);
533	} else {
534		/* The if clause above forces this code path when the status
535		 * failure is a value other than FFER6.  Do not call the offline
536		 *  twice. This is the adapter hardware error path.
537		 */
538		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
539				"%d:0457 Adapter Hardware Error "
540				"Data: x%x x%x x%x\n",
541				phba->brd_no, phba->work_hs,
542				phba->work_status[0], phba->work_status[1]);
543
544		event_data = FC_REG_DUMP_EVENT;
545		fc_host_post_vendor_event(shost, fc_get_event_number(),
546				sizeof(event_data), (char *) &event_data,
547				SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
548
549		psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
550		lpfc_offline_prep(phba);
551		lpfc_offline(phba);
552		lpfc_unblock_mgmt_io(phba);
553		phba->link_state = LPFC_HBA_ERROR;
554		lpfc_hba_down_post(phba);
555	}
556}
557
558/************************************************************************/
559/*                                                                      */
560/*    lpfc_handle_latt                                                  */
561/*    This routine will handle processing a Host Attention              */
562/*    Link Status event. This will be initialized                       */
563/*    as a SLI layer callback routine.                                  */
564/*                                                                      */
565/************************************************************************/
566void
567lpfc_handle_latt(struct lpfc_hba *phba)
568{
569	struct lpfc_vport *vport = phba->pport;
570	struct lpfc_sli   *psli = &phba->sli;
571	LPFC_MBOXQ_t *pmb;
572	volatile uint32_t control;
573	struct lpfc_dmabuf *mp;
574	int rc = -ENOMEM;
575
576	pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
577	if (!pmb)
578		goto lpfc_handle_latt_err_exit;
579
580	mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
581	if (!mp)
582		goto lpfc_handle_latt_free_pmb;
583
584	mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
585	if (!mp->virt)
586		goto lpfc_handle_latt_free_mp;
587
588	rc = -EIO;
589
590	/* Cleanup any outstanding ELS commands */
591	lpfc_els_flush_cmd(vport);
592
593	psli->slistat.link_event++;
594	lpfc_read_la(phba, pmb, mp);
595	pmb->mbox_cmpl = lpfc_mbx_cmpl_read_la;
596	pmb->vport = vport;
597	rc = lpfc_sli_issue_mbox (phba, pmb, (MBX_NOWAIT | MBX_STOP_IOCB));
598	if (rc == MBX_NOT_FINISHED)
599		goto lpfc_handle_latt_free_mbuf;
600
601	/* Clear Link Attention in HA REG */
602	spin_lock_irq(&phba->hbalock);
603	writel(HA_LATT, phba->HAregaddr);
604	readl(phba->HAregaddr); /* flush */
605	spin_unlock_irq(&phba->hbalock);
606
607	return;
608
609lpfc_handle_latt_free_mbuf:
610	lpfc_mbuf_free(phba, mp->virt, mp->phys);
611lpfc_handle_latt_free_mp:
612	kfree(mp);
613lpfc_handle_latt_free_pmb:
614	mempool_free(pmb, phba->mbox_mem_pool);
615lpfc_handle_latt_err_exit:
616	/* Enable Link attention interrupts */
617	spin_lock_irq(&phba->hbalock);
618	psli->sli_flag |= LPFC_PROCESS_LA;
619	control = readl(phba->HCregaddr);
620	control |= HC_LAINT_ENA;
621	writel(control, phba->HCregaddr);
622	readl(phba->HCregaddr); /* flush */
623
624	/* Clear Link Attention in HA REG */
625	writel(HA_LATT, phba->HAregaddr);
626	readl(phba->HAregaddr); /* flush */
627	spin_unlock_irq(&phba->hbalock);
628	lpfc_linkdown(phba);
629	phba->link_state = LPFC_HBA_ERROR;
630
631	/* The other case is an error from issue_mbox */
632	if (rc == -ENOMEM)
633		lpfc_printf_log(phba,
634				KERN_WARNING,
635				LOG_MBOX,
636			        "%d:0300 READ_LA: no buffers\n",
637				phba->brd_no);
638
639	return;
640}
641
642/************************************************************************/
643/*                                                                      */
644/*   lpfc_parse_vpd                                                     */
645/*   This routine will parse the VPD data                               */
646/*                                                                      */
647/************************************************************************/
648static int
649lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
650{
651	uint8_t lenlo, lenhi;
652	int Length;
653	int i, j;
654	int finished = 0;
655	int index = 0;
656
657	if (!vpd)
658		return 0;
659
660	/* Vital Product */
661	lpfc_printf_log(phba,
662			KERN_INFO,
663			LOG_INIT,
664			"%d:0455 Vital Product Data: x%x x%x x%x x%x\n",
665			phba->brd_no,
666			(uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
667			(uint32_t) vpd[3]);
668	while (!finished && (index < (len - 4))) {
669		switch (vpd[index]) {
670		case 0x82:
671		case 0x91:
672			index += 1;
673			lenlo = vpd[index];
674			index += 1;
675			lenhi = vpd[index];
676			index += 1;
677			i = ((((unsigned short)lenhi) << 8) + lenlo);
678			index += i;
679			break;
680		case 0x90:
681			index += 1;
682			lenlo = vpd[index];
683			index += 1;
684			lenhi = vpd[index];
685			index += 1;
686			Length = ((((unsigned short)lenhi) << 8) + lenlo);
687			if (Length > len - index)
688				Length = len - index;
689			while (Length > 0) {
690			/* Look for Serial Number */
691			if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
692				index += 2;
693				i = vpd[index];
694				index += 1;
695				j = 0;
696				Length -= (3+i);
697				while(i--) {
698					phba->SerialNumber[j++] = vpd[index++];
699					if (j == 31)
700						break;
701				}
702				phba->SerialNumber[j] = 0;
703				continue;
704			}
705			else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
706				phba->vpd_flag |= VPD_MODEL_DESC;
707				index += 2;
708				i = vpd[index];
709				index += 1;
710				j = 0;
711				Length -= (3+i);
712				while(i--) {
713					phba->ModelDesc[j++] = vpd[index++];
714					if (j == 255)
715						break;
716				}
717				phba->ModelDesc[j] = 0;
718				continue;
719			}
720			else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
721				phba->vpd_flag |= VPD_MODEL_NAME;
722				index += 2;
723				i = vpd[index];
724				index += 1;
725				j = 0;
726				Length -= (3+i);
727				while(i--) {
728					phba->ModelName[j++] = vpd[index++];
729					if (j == 79)
730						break;
731				}
732				phba->ModelName[j] = 0;
733				continue;
734			}
735			else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
736				phba->vpd_flag |= VPD_PROGRAM_TYPE;
737				index += 2;
738				i = vpd[index];
739				index += 1;
740				j = 0;
741				Length -= (3+i);
742				while(i--) {
743					phba->ProgramType[j++] = vpd[index++];
744					if (j == 255)
745						break;
746				}
747				phba->ProgramType[j] = 0;
748				continue;
749			}
750			else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
751				phba->vpd_flag |= VPD_PORT;
752				index += 2;
753				i = vpd[index];
754				index += 1;
755				j = 0;
756				Length -= (3+i);
757				while(i--) {
758				phba->Port[j++] = vpd[index++];
759				if (j == 19)
760					break;
761				}
762				phba->Port[j] = 0;
763				continue;
764			}
765			else {
766				index += 2;
767				i = vpd[index];
768				index += 1;
769				index += i;
770				Length -= (3 + i);
771			}
772		}
773		finished = 0;
774		break;
775		case 0x78:
776			finished = 1;
777			break;
778		default:
779			index ++;
780			break;
781		}
782	}
783
784	return(1);
785}
786
787static void
788lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
789{
790	lpfc_vpd_t *vp;
791	uint16_t dev_id = phba->pcidev->device;
792	int max_speed;
793	struct {
794		char * name;
795		int    max_speed;
796		char * bus;
797	} m = {"<Unknown>", 0, ""};
798
799	if (mdp && mdp[0] != '\0'
800		&& descp && descp[0] != '\0')
801		return;
802
803	if (phba->lmt & LMT_10Gb)
804		max_speed = 10;
805	else if (phba->lmt & LMT_8Gb)
806		max_speed = 8;
807	else if (phba->lmt & LMT_4Gb)
808		max_speed = 4;
809	else if (phba->lmt & LMT_2Gb)
810		max_speed = 2;
811	else
812		max_speed = 1;
813
814	vp = &phba->vpd;
815
816	switch (dev_id) {
817	case PCI_DEVICE_ID_FIREFLY:
818		m = (typeof(m)){"LP6000", max_speed, "PCI"};
819		break;
820	case PCI_DEVICE_ID_SUPERFLY:
821		if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
822			m = (typeof(m)){"LP7000", max_speed,  "PCI"};
823		else
824			m = (typeof(m)){"LP7000E", max_speed, "PCI"};
825		break;
826	case PCI_DEVICE_ID_DRAGONFLY:
827		m = (typeof(m)){"LP8000", max_speed, "PCI"};
828		break;
829	case PCI_DEVICE_ID_CENTAUR:
830		if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
831			m = (typeof(m)){"LP9002", max_speed, "PCI"};
832		else
833			m = (typeof(m)){"LP9000", max_speed, "PCI"};
834		break;
835	case PCI_DEVICE_ID_RFLY:
836		m = (typeof(m)){"LP952", max_speed, "PCI"};
837		break;
838	case PCI_DEVICE_ID_PEGASUS:
839		m = (typeof(m)){"LP9802", max_speed, "PCI-X"};
840		break;
841	case PCI_DEVICE_ID_THOR:
842		m = (typeof(m)){"LP10000", max_speed, "PCI-X"};
843		break;
844	case PCI_DEVICE_ID_VIPER:
845		m = (typeof(m)){"LPX1000", max_speed,  "PCI-X"};
846		break;
847	case PCI_DEVICE_ID_PFLY:
848		m = (typeof(m)){"LP982", max_speed, "PCI-X"};
849		break;
850	case PCI_DEVICE_ID_TFLY:
851		m = (typeof(m)){"LP1050", max_speed, "PCI-X"};
852		break;
853	case PCI_DEVICE_ID_HELIOS:
854		m = (typeof(m)){"LP11000", max_speed, "PCI-X2"};
855		break;
856	case PCI_DEVICE_ID_HELIOS_SCSP:
857		m = (typeof(m)){"LP11000-SP", max_speed, "PCI-X2"};
858		break;
859	case PCI_DEVICE_ID_HELIOS_DCSP:
860		m = (typeof(m)){"LP11002-SP", max_speed, "PCI-X2"};
861		break;
862	case PCI_DEVICE_ID_NEPTUNE:
863		m = (typeof(m)){"LPe1000", max_speed, "PCIe"};
864		break;
865	case PCI_DEVICE_ID_NEPTUNE_SCSP:
866		m = (typeof(m)){"LPe1000-SP", max_speed, "PCIe"};
867		break;
868	case PCI_DEVICE_ID_NEPTUNE_DCSP:
869		m = (typeof(m)){"LPe1002-SP", max_speed, "PCIe"};
870		break;
871	case PCI_DEVICE_ID_BMID:
872		m = (typeof(m)){"LP1150", max_speed, "PCI-X2"};
873		break;
874	case PCI_DEVICE_ID_BSMB:
875		m = (typeof(m)){"LP111", max_speed, "PCI-X2"};
876		break;
877	case PCI_DEVICE_ID_ZEPHYR:
878		m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
879		break;
880	case PCI_DEVICE_ID_ZEPHYR_SCSP:
881		m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
882		break;
883	case PCI_DEVICE_ID_ZEPHYR_DCSP:
884		m = (typeof(m)){"LPe11002-SP", max_speed, "PCIe"};
885		break;
886	case PCI_DEVICE_ID_ZMID:
887		m = (typeof(m)){"LPe1150", max_speed, "PCIe"};
888		break;
889	case PCI_DEVICE_ID_ZSMB:
890		m = (typeof(m)){"LPe111", max_speed, "PCIe"};
891		break;
892	case PCI_DEVICE_ID_LP101:
893		m = (typeof(m)){"LP101", max_speed, "PCI-X"};
894		break;
895	case PCI_DEVICE_ID_LP10000S:
896		m = (typeof(m)){"LP10000-S", max_speed, "PCI"};
897		break;
898	case PCI_DEVICE_ID_LP11000S:
899		m = (typeof(m)){"LP11000-S", max_speed,
900			"PCI-X2"};
901		break;
902	case PCI_DEVICE_ID_LPE11000S:
903		m = (typeof(m)){"LPe11000-S", max_speed,
904			"PCIe"};
905		break;
906	case PCI_DEVICE_ID_SAT:
907		m = (typeof(m)){"LPe12000", max_speed, "PCIe"};
908		break;
909	case PCI_DEVICE_ID_SAT_MID:
910		m = (typeof(m)){"LPe1250", max_speed, "PCIe"};
911		break;
912	case PCI_DEVICE_ID_SAT_SMB:
913		m = (typeof(m)){"LPe121", max_speed, "PCIe"};
914		break;
915	case PCI_DEVICE_ID_SAT_DCSP:
916		m = (typeof(m)){"LPe12002-SP", max_speed, "PCIe"};
917		break;
918	case PCI_DEVICE_ID_SAT_SCSP:
919		m = (typeof(m)){"LPe12000-SP", max_speed, "PCIe"};
920		break;
921	case PCI_DEVICE_ID_SAT_S:
922		m = (typeof(m)){"LPe12000-S", max_speed, "PCIe"};
923		break;
924	default:
925		m = (typeof(m)){ NULL };
926		break;
927	}
928
929	if (mdp && mdp[0] == '\0')
930		snprintf(mdp, 79,"%s", m.name);
931	if (descp && descp[0] == '\0')
932		snprintf(descp, 255,
933			 "Emulex %s %dGb %s Fibre Channel Adapter",
934			 m.name, m.max_speed, m.bus);
935}
936
937/**************************************************/
938/*   lpfc_post_buffer                             */
939/*                                                */
940/*   This routine will post count buffers to the  */
941/*   ring with the QUE_RING_BUF_CN command. This  */
942/*   allows 3 buffers / command to be posted.     */
943/*   Returns the number of buffers NOT posted.    */
944/**************************************************/
945int
946lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt,
947		 int type)
948{
949	IOCB_t *icmd;
950	struct lpfc_iocbq *iocb;
951	struct lpfc_dmabuf *mp1, *mp2;
952
953	cnt += pring->missbufcnt;
954
955	/* While there are buffers to post */
956	while (cnt > 0) {
957		/* Allocate buffer for  command iocb */
958		iocb = lpfc_sli_get_iocbq(phba);
959		if (iocb == NULL) {
960			pring->missbufcnt = cnt;
961			return cnt;
962		}
963		icmd = &iocb->iocb;
964
965		/* 2 buffers can be posted per command */
966		/* Allocate buffer to post */
967		mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
968		if (mp1)
969		    mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
970						&mp1->phys);
971		if (mp1 == 0 || mp1->virt == 0) {
972			kfree(mp1);
973			lpfc_sli_release_iocbq(phba, iocb);
974			pring->missbufcnt = cnt;
975			return cnt;
976		}
977
978		INIT_LIST_HEAD(&mp1->list);
979		/* Allocate buffer to post */
980		if (cnt > 1) {
981			mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
982			if (mp2)
983				mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
984							    &mp2->phys);
985			if (mp2 == 0 || mp2->virt == 0) {
986				kfree(mp2);
987				lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
988				kfree(mp1);
989				lpfc_sli_release_iocbq(phba, iocb);
990				pring->missbufcnt = cnt;
991				return cnt;
992			}
993
994			INIT_LIST_HEAD(&mp2->list);
995		} else {
996			mp2 = NULL;
997		}
998
999		icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
1000		icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
1001		icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
1002		icmd->ulpBdeCount = 1;
1003		cnt--;
1004		if (mp2) {
1005			icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
1006			icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
1007			icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
1008			cnt--;
1009			icmd->ulpBdeCount = 2;
1010		}
1011
1012		icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
1013		icmd->ulpLe = 1;
1014
1015		if (lpfc_sli_issue_iocb(phba, pring, iocb, 0) == IOCB_ERROR) {
1016			lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1017			kfree(mp1);
1018			cnt++;
1019			if (mp2) {
1020				lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
1021				kfree(mp2);
1022				cnt++;
1023			}
1024			lpfc_sli_release_iocbq(phba, iocb);
1025			pring->missbufcnt = cnt;
1026			return cnt;
1027		}
1028		lpfc_sli_ringpostbuf_put(phba, pring, mp1);
1029		if (mp2) {
1030			lpfc_sli_ringpostbuf_put(phba, pring, mp2);
1031		}
1032	}
1033	pring->missbufcnt = 0;
1034	return 0;
1035}
1036
1037/************************************************************************/
1038/*                                                                      */
1039/*   lpfc_post_rcv_buf                                                  */
1040/*   This routine post initial rcv buffers to the configured rings      */
1041/*                                                                      */
1042/************************************************************************/
1043static int
1044lpfc_post_rcv_buf(struct lpfc_hba *phba)
1045{
1046	struct lpfc_sli *psli = &phba->sli;
1047
1048	/* Ring 0, ELS / CT buffers */
1049	lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0, 1);
1050	/* Ring 2 - FCP no buffers needed */
1051
1052	return 0;
1053}
1054
1055#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
1056
1057/************************************************************************/
1058/*                                                                      */
1059/*   lpfc_sha_init                                                      */
1060/*                                                                      */
1061/************************************************************************/
1062static void
1063lpfc_sha_init(uint32_t * HashResultPointer)
1064{
1065	HashResultPointer[0] = 0x67452301;
1066	HashResultPointer[1] = 0xEFCDAB89;
1067	HashResultPointer[2] = 0x98BADCFE;
1068	HashResultPointer[3] = 0x10325476;
1069	HashResultPointer[4] = 0xC3D2E1F0;
1070}
1071
1072/************************************************************************/
1073/*                                                                      */
1074/*   lpfc_sha_iterate                                                   */
1075/*                                                                      */
1076/************************************************************************/
1077static void
1078lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
1079{
1080	int t;
1081	uint32_t TEMP;
1082	uint32_t A, B, C, D, E;
1083	t = 16;
1084	do {
1085		HashWorkingPointer[t] =
1086		    S(1,
1087		      HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
1088								     8] ^
1089		      HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
1090	} while (++t <= 79);
1091	t = 0;
1092	A = HashResultPointer[0];
1093	B = HashResultPointer[1];
1094	C = HashResultPointer[2];
1095	D = HashResultPointer[3];
1096	E = HashResultPointer[4];
1097
1098	do {
1099		if (t < 20) {
1100			TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
1101		} else if (t < 40) {
1102			TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
1103		} else if (t < 60) {
1104			TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
1105		} else {
1106			TEMP = (B ^ C ^ D) + 0xCA62C1D6;
1107		}
1108		TEMP += S(5, A) + E + HashWorkingPointer[t];
1109		E = D;
1110		D = C;
1111		C = S(30, B);
1112		B = A;
1113		A = TEMP;
1114	} while (++t <= 79);
1115
1116	HashResultPointer[0] += A;
1117	HashResultPointer[1] += B;
1118	HashResultPointer[2] += C;
1119	HashResultPointer[3] += D;
1120	HashResultPointer[4] += E;
1121
1122}
1123
1124/************************************************************************/
1125/*                                                                      */
1126/*   lpfc_challenge_key                                                 */
1127/*                                                                      */
1128/************************************************************************/
1129static void
1130lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
1131{
1132	*HashWorking = (*RandomChallenge ^ *HashWorking);
1133}
1134
1135/************************************************************************/
1136/*                                                                      */
1137/*   lpfc_hba_init                                                      */
1138/*                                                                      */
1139/************************************************************************/
1140void
1141lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
1142{
1143	int t;
1144	uint32_t *HashWorking;
1145	uint32_t *pwwnn = (uint32_t *) phba->wwnn;
1146
1147	HashWorking = kmalloc(80 * sizeof(uint32_t), GFP_KERNEL);
1148	if (!HashWorking)
1149		return;
1150
1151	memset(HashWorking, 0, (80 * sizeof(uint32_t)));
1152	HashWorking[0] = HashWorking[78] = *pwwnn++;
1153	HashWorking[1] = HashWorking[79] = *pwwnn;
1154
1155	for (t = 0; t < 7; t++)
1156		lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
1157
1158	lpfc_sha_init(hbainit);
1159	lpfc_sha_iterate(hbainit, HashWorking);
1160	kfree(HashWorking);
1161}
1162
1163static void
1164lpfc_cleanup(struct lpfc_vport *vport)
1165{
1166	struct lpfc_nodelist *ndlp, *next_ndlp;
1167
1168	/* clean up phba - lpfc specific */
1169	lpfc_can_disctmo(vport);
1170	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp)
1171		lpfc_nlp_put(ndlp);
1172
1173	INIT_LIST_HEAD(&vport->fc_nodes);
1174
1175	return;
1176}
1177
1178static void
1179lpfc_establish_link_tmo(unsigned long ptr)
1180{
1181	struct lpfc_hba   *phba = (struct lpfc_hba *)ptr;
1182	struct lpfc_vport *vport = phba->pport;
1183	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1184	unsigned long iflag;
1185
1186
1187	/* Re-establishing Link, timer expired */
1188	lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1189			"%d:1300 Re-establishing Link, timer expired "
1190			"Data: x%x x%x\n",
1191			phba->brd_no, vport->fc_flag,
1192			vport->port_state);
1193	spin_lock_irqsave(shost->host_lock, iflag);
1194	vport->fc_flag &= ~FC_ESTABLISH_LINK;
1195	spin_unlock_irqrestore(shost->host_lock, iflag);
1196}
1197
1198static void
1199lpfc_stop_timer(struct lpfc_hba *phba)
1200{
1201	struct lpfc_vport *vport = phba->pport;
1202
1203	del_timer_sync(&phba->fcp_poll_timer);
1204	del_timer_sync(&phba->fc_estabtmo);
1205	del_timer_sync(&vport->els_tmofunc);
1206	del_timer_sync(&vport->fc_fdmitmo);
1207	del_timer_sync(&vport->fc_disctmo);
1208	del_timer_sync(&phba->sli.mbox_tmo);
1209	return;
1210}
1211
1212int
1213lpfc_online(struct lpfc_hba *phba)
1214{
1215	struct lpfc_vport *vport = phba->pport;
1216	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1217
1218	if (!phba)
1219		return 0;
1220
1221	if (!(vport->fc_flag & FC_OFFLINE_MODE))
1222		return 0;
1223
1224	lpfc_printf_log(phba,
1225		       KERN_WARNING,
1226		       LOG_INIT,
1227		       "%d:0458 Bring Adapter online\n",
1228		       phba->brd_no);
1229
1230	lpfc_block_mgmt_io(phba);
1231
1232	if (!lpfc_sli_queue_setup(phba)) {
1233		lpfc_unblock_mgmt_io(phba);
1234		return 1;
1235	}
1236
1237	if (lpfc_sli_hba_setup(phba)) {	/* Initialize the HBA */
1238		lpfc_unblock_mgmt_io(phba);
1239		return 1;
1240	}
1241
1242	spin_lock_irq(shost->host_lock);
1243	vport->fc_flag &= ~FC_OFFLINE_MODE;
1244	spin_unlock_irq(shost->host_lock);
1245
1246	lpfc_unblock_mgmt_io(phba);
1247	return 0;
1248}
1249
1250void
1251lpfc_block_mgmt_io(struct lpfc_hba * phba)
1252{
1253	unsigned long iflag;
1254
1255	spin_lock_irqsave(&phba->hbalock, iflag);
1256	phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
1257	spin_unlock_irqrestore(&phba->hbalock, iflag);
1258}
1259
1260void
1261lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
1262{
1263	unsigned long iflag;
1264
1265	spin_lock_irqsave(&phba->hbalock, iflag);
1266	phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
1267	spin_unlock_irqrestore(&phba->hbalock, iflag);
1268}
1269
1270void
1271lpfc_offline_prep(struct lpfc_hba * phba)
1272{
1273	struct lpfc_vport *vport = phba->pport;
1274	struct lpfc_nodelist  *ndlp, *next_ndlp;
1275
1276	if (vport->fc_flag & FC_OFFLINE_MODE)
1277		return;
1278
1279	lpfc_block_mgmt_io(phba);
1280
1281	lpfc_linkdown(phba);
1282
1283	/* Issue an unreg_login to all nodes */
1284	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp)
1285		if (ndlp->nlp_state != NLP_STE_UNUSED_NODE)
1286			lpfc_unreg_rpi(vport, ndlp);
1287
1288	lpfc_sli_flush_mbox_queue(phba);
1289}
1290
1291void
1292lpfc_offline(struct lpfc_hba *phba)
1293{
1294	struct lpfc_vport *vport = phba->pport;
1295	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1296	unsigned long iflag;
1297
1298	if (vport->fc_flag & FC_OFFLINE_MODE)
1299		return;
1300
1301	/* stop all timers associated with this hba */
1302	lpfc_stop_timer(phba);
1303
1304	lpfc_printf_log(phba,
1305		       KERN_WARNING,
1306		       LOG_INIT,
1307		       "%d:0460 Bring Adapter offline\n",
1308		       phba->brd_no);
1309
1310	/* Bring down the SLI Layer and cleanup.  The HBA is offline
1311	   now.  */
1312	lpfc_sli_hba_down(phba);
1313	lpfc_cleanup(vport);
1314	spin_lock_irqsave(shost->host_lock, iflag);
1315	spin_lock(&phba->hbalock);
1316	phba->work_ha = 0;
1317	vport->work_port_events = 0;
1318	vport->fc_flag |= FC_OFFLINE_MODE;
1319	spin_unlock(&phba->hbalock);
1320	spin_unlock_irqrestore(shost->host_lock, iflag);
1321}
1322
1323/******************************************************************************
1324* Function name: lpfc_scsi_free
1325*
1326* Description: Called from lpfc_pci_remove_one free internal driver resources
1327*
1328******************************************************************************/
1329static int
1330lpfc_scsi_free(struct lpfc_hba *phba)
1331{
1332	struct lpfc_scsi_buf *sb, *sb_next;
1333	struct lpfc_iocbq *io, *io_next;
1334
1335	spin_lock_irq(&phba->hbalock);
1336	/* Release all the lpfc_scsi_bufs maintained by this host. */
1337	list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
1338		list_del(&sb->list);
1339		pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
1340								sb->dma_handle);
1341		kfree(sb);
1342		phba->total_scsi_bufs--;
1343	}
1344
1345	/* Release all the lpfc_iocbq entries maintained by this host. */
1346	list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
1347		list_del(&io->list);
1348		kfree(io);
1349		phba->total_iocbq_bufs--;
1350	}
1351
1352	spin_unlock_irq(&phba->hbalock);
1353
1354	return 0;
1355}
1356
1357struct lpfc_vport *
1358lpfc_create_port(struct lpfc_hba *phba, int instance)
1359{
1360	struct lpfc_vport *vport;
1361	struct Scsi_Host  *shost;
1362	int error = 0;
1363
1364	shost = scsi_host_alloc(&lpfc_template, sizeof(struct lpfc_vport));
1365	if (!shost)
1366		goto out;
1367
1368	vport = (struct lpfc_vport *) shost->hostdata;
1369	vport->phba = phba;
1370
1371	vport->load_flag |= FC_LOADING;
1372
1373	shost->unique_id = instance;
1374	shost->max_id = LPFC_MAX_TARGET;
1375	shost->max_lun = phba->cfg_max_luns;
1376	shost->this_id = -1;
1377	shost->max_cmd_len = 16;
1378	/*
1379	 * Set initial can_queue value since 0 is no longer supported and
1380	 * scsi_add_host will fail. This will be adjusted later based on the
1381	 * max xri value determined in hba setup.
1382	 */
1383	shost->can_queue = phba->cfg_hba_queue_depth - 10;
1384	shost->transportt = lpfc_transport_template;
1385
1386	/* Initialize all internally managed lists. */
1387	INIT_LIST_HEAD(&vport->fc_nodes);
1388	spin_lock_init(&vport->work_port_lock);
1389
1390	init_timer(&vport->fc_disctmo);
1391	vport->fc_disctmo.function = lpfc_disc_timeout;
1392	vport->fc_disctmo.data = (unsigned long) vport;
1393
1394	init_timer(&vport->fc_fdmitmo);
1395	vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
1396	vport->fc_fdmitmo.data = (unsigned long) vport;
1397
1398	init_timer(&vport->els_tmofunc);
1399	vport->els_tmofunc.function = lpfc_els_timeout;
1400	vport->els_tmofunc.data = (unsigned long) vport;
1401
1402	error = scsi_add_host(shost, &phba->pcidev->dev);
1403	if (error)
1404		goto out_put_shost;
1405
1406	list_add_tail(&vport->listentry, &phba->port_list);
1407	scsi_scan_host(shost);
1408	return vport;
1409
1410out_put_shost:
1411	scsi_host_put(shost);
1412out:
1413	return NULL;
1414}
1415
1416void
1417destroy_port(struct lpfc_vport *vport)
1418{
1419	lpfc_cleanup(vport);
1420	list_del(&vport->listentry);
1421	lpfc_free_sysfs_attr(vport);
1422	fc_remove_host(lpfc_shost_from_vport(vport));
1423	scsi_remove_host(lpfc_shost_from_vport(vport));
1424	return;
1425}
1426
1427static void
1428lpfc_remove_device(struct lpfc_vport *vport)
1429{
1430	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1431	struct lpfc_hba  *phba  = vport->phba;
1432
1433	lpfc_free_sysfs_attr(vport);
1434
1435	spin_lock_irq(shost->host_lock);
1436	vport->fc_flag |= FC_UNLOADING;
1437	spin_unlock_irq(shost->host_lock);
1438
1439	fc_remove_host(shost);
1440	scsi_remove_host(shost);
1441
1442	kthread_stop(phba->worker_thread);
1443}
1444
1445void lpfc_scan_start(struct Scsi_Host *shost)
1446{
1447	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1448	struct lpfc_hba   *phba = vport->phba;
1449
1450	if (lpfc_alloc_sysfs_attr(vport))
1451		goto error;
1452
1453	/*
1454	 * hba setup may have changed the hba_queue_depth so we need to adjust
1455	 * the value of can_queue.
1456	 */
1457	shost->can_queue = phba->cfg_hba_queue_depth - 10;
1458	return;
1459
1460error:
1461	lpfc_remove_device(vport);
1462}
1463
1464int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
1465{
1466	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1467	struct lpfc_hba   *phba = vport->phba;
1468
1469	if (time >= 30 * HZ) {
1470		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1471				"%d:0461 Scanning longer than 30 "
1472				"seconds.  Continuing initialization\n",
1473				phba->brd_no);
1474		goto finished;
1475	}
1476	if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
1477		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1478				"%d:0465 Link down longer than 15 "
1479				"seconds.  Continuing initialization\n",
1480				phba->brd_no);
1481		goto finished;
1482	}
1483
1484	if (vport->port_state != LPFC_VPORT_READY)
1485		return 0;
1486	if (vport->num_disc_nodes || vport->fc_prli_sent)
1487		return 0;
1488	if (vport->fc_map_cnt == 0 && time < 2 * HZ)
1489		return 0;
1490	if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
1491		return 0;
1492
1493finished:
1494	/*
1495	 * Set fixed host attributes.  Must done after lpfc_sli_hba_setup().
1496	 */
1497
1498	fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
1499	fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
1500	fc_host_supported_classes(shost) = FC_COS_CLASS3;
1501
1502	memset(fc_host_supported_fc4s(shost), 0,
1503	       sizeof(fc_host_supported_fc4s(shost)));
1504	fc_host_supported_fc4s(shost)[2] = 1;
1505	fc_host_supported_fc4s(shost)[7] = 1;
1506
1507	lpfc_get_hba_sym_node_name(phba, fc_host_symbolic_name(shost));
1508
1509	fc_host_supported_speeds(shost) = 0;
1510	if (phba->lmt & LMT_10Gb)
1511		fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
1512	if (phba->lmt & LMT_4Gb)
1513		fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
1514	if (phba->lmt & LMT_2Gb)
1515		fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
1516	if (phba->lmt & LMT_1Gb)
1517		fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
1518
1519	fc_host_maxframe_size(shost) =
1520		(((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
1521		(uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
1522
1523	/* This value is also unchanging */
1524	memset(fc_host_active_fc4s(shost), 0,
1525	       sizeof(fc_host_active_fc4s(shost)));
1526	fc_host_active_fc4s(shost)[2] = 1;
1527	fc_host_active_fc4s(shost)[7] = 1;
1528
1529	spin_lock_irq(shost->host_lock);
1530	vport->fc_flag &= ~FC_LOADING;
1531	spin_unlock_irq(shost->host_lock);
1532
1533	return 1;
1534}
1535
1536static int __devinit
1537lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
1538{
1539	struct lpfc_vport *vport = NULL;
1540	struct lpfc_hba   *phba;
1541	struct lpfc_sli   *psli;
1542	struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
1543	struct Scsi_Host  *shost = NULL;
1544	unsigned long bar0map_len, bar2map_len;
1545	int error = -ENODEV, retval;
1546	int i;
1547	uint16_t iotag;
1548
1549	if (pci_enable_device(pdev))
1550		goto out;
1551	if (pci_request_regions(pdev, LPFC_DRIVER_NAME))
1552		goto out_disable_device;
1553
1554	phba = kzalloc(sizeof (struct lpfc_hba), GFP_KERNEL);
1555	if (!phba)
1556		goto out_release_regions;
1557
1558	spin_lock_init(&phba->hbalock);
1559
1560	phba->pcidev = pdev;
1561
1562	/* Assign an unused board number */
1563	if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
1564		goto out_free_phba;
1565
1566	error = idr_get_new(&lpfc_hba_index, NULL, &phba->brd_no);
1567	if (error)
1568		goto out_free_phba;
1569
1570	INIT_LIST_HEAD(&phba->port_list);
1571
1572	/*
1573	 * Get all the module params for configuring this host and then
1574	 * establish the host.
1575	 */
1576	lpfc_get_cfgparam(phba);
1577
1578	/* Initialize timers used by driver */
1579	init_timer(&phba->fc_estabtmo);
1580	phba->fc_estabtmo.function = lpfc_establish_link_tmo;
1581	phba->fc_estabtmo.data = (unsigned long) phba;
1582	psli = &phba->sli;
1583	init_timer(&psli->mbox_tmo);
1584	psli->mbox_tmo.function = lpfc_mbox_timeout;
1585	psli->mbox_tmo.data = (unsigned long) phba;
1586	init_timer(&phba->fcp_poll_timer);
1587	phba->fcp_poll_timer.function = lpfc_poll_timeout;
1588	phba->fcp_poll_timer.data = (unsigned long) phba;
1589
1590	pci_set_master(pdev);
1591	retval = pci_set_mwi(pdev);
1592	if (retval)
1593		dev_printk(KERN_WARNING, &pdev->dev,
1594			   "Warning: pci_set_mwi returned %d\n", retval);
1595
1596	if (pci_set_dma_mask(phba->pcidev, DMA_64BIT_MASK) != 0)
1597		if (pci_set_dma_mask(phba->pcidev, DMA_32BIT_MASK) != 0)
1598			goto out_idr_remove;
1599
1600	/*
1601	 * Get the bus address of Bar0 and Bar2 and the number of bytes
1602	 * required by each mapping.
1603	 */
1604	phba->pci_bar0_map = pci_resource_start(phba->pcidev, 0);
1605	bar0map_len        = pci_resource_len(phba->pcidev, 0);
1606
1607	phba->pci_bar2_map = pci_resource_start(phba->pcidev, 2);
1608	bar2map_len        = pci_resource_len(phba->pcidev, 2);
1609
1610	/* Map HBA SLIM to a kernel virtual address. */
1611	phba->slim_memmap_p      = ioremap(phba->pci_bar0_map, bar0map_len);
1612	if (!phba->slim_memmap_p) {
1613		error = -ENODEV;
1614		dev_printk(KERN_ERR, &pdev->dev,
1615			   "ioremap failed for SLIM memory.\n");
1616		goto out_idr_remove;
1617	}
1618
1619	/* Map HBA Control Registers to a kernel virtual address. */
1620	phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
1621	if (!phba->ctrl_regs_memmap_p) {
1622		error = -ENODEV;
1623		dev_printk(KERN_ERR, &pdev->dev,
1624			   "ioremap failed for HBA control registers.\n");
1625		goto out_iounmap_slim;
1626	}
1627
1628	/* Allocate memory for SLI-2 structures */
1629	phba->slim2p = dma_alloc_coherent(&phba->pcidev->dev, SLI2_SLIM_SIZE,
1630					  &phba->slim2p_mapping, GFP_KERNEL);
1631	if (!phba->slim2p)
1632		goto out_iounmap;
1633
1634	memset(phba->slim2p, 0, SLI2_SLIM_SIZE);
1635
1636	/* Initialize the SLI Layer to run with lpfc HBAs. */
1637	lpfc_sli_setup(phba);
1638	lpfc_sli_queue_setup(phba);
1639
1640	error = lpfc_mem_alloc(phba);
1641	if (error)
1642		goto out_free_slim;
1643
1644	/* Initialize and populate the iocb list per host.  */
1645	INIT_LIST_HEAD(&phba->lpfc_iocb_list);
1646	for (i = 0; i < LPFC_IOCB_LIST_CNT; i++) {
1647		iocbq_entry = kmalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
1648		if (iocbq_entry == NULL) {
1649			printk(KERN_ERR "%s: only allocated %d iocbs of "
1650				"expected %d count. Unloading driver.\n",
1651				__FUNCTION__, i, LPFC_IOCB_LIST_CNT);
1652			error = -ENOMEM;
1653			goto out_free_iocbq;
1654		}
1655
1656		memset(iocbq_entry, 0, sizeof(struct lpfc_iocbq));
1657		iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
1658		if (iotag == 0) {
1659			kfree (iocbq_entry);
1660			printk(KERN_ERR "%s: failed to allocate IOTAG. "
1661			       "Unloading driver.\n",
1662				__FUNCTION__);
1663			error = -ENOMEM;
1664			goto out_free_iocbq;
1665		}
1666
1667		spin_lock_irq(&phba->hbalock);
1668		list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
1669		phba->total_iocbq_bufs++;
1670		spin_unlock_irq(&phba->hbalock);
1671	}
1672
1673	/* Initialize HBA structure */
1674	phba->fc_edtov = FF_DEF_EDTOV;
1675	phba->fc_ratov = FF_DEF_RATOV;
1676	phba->fc_altov = FF_DEF_ALTOV;
1677	phba->fc_arbtov = FF_DEF_ARBTOV;
1678
1679	INIT_LIST_HEAD(&phba->work_list);
1680	phba->work_ha_mask = (HA_ERATT|HA_MBATT|HA_LATT);
1681	phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
1682
1683	/* Startup the kernel thread for this host adapter. */
1684	phba->worker_thread = kthread_run(lpfc_do_work, phba,
1685				       "lpfc_worker_%d", phba->brd_no);
1686	if (IS_ERR(phba->worker_thread)) {
1687		error = PTR_ERR(phba->worker_thread);
1688		goto out_free_iocbq;
1689	}
1690
1691	/* Initialize the list of scsi buffers used by driver for scsi IO. */
1692	spin_lock_init(&phba->scsi_buf_list_lock);
1693	INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
1694
1695	vport = lpfc_create_port(phba, phba->brd_no);
1696	if (!vport)
1697		goto out_kthread_stop;
1698
1699	shost = lpfc_shost_from_vport(vport);
1700	vport->port_type = LPFC_PHYSICAL_PORT;
1701	phba->pport = vport;
1702
1703	pci_set_drvdata(pdev, lpfc_shost_from_vport(vport));
1704
1705	if (phba->cfg_use_msi) {
1706		error = pci_enable_msi(phba->pcidev);
1707		if (error)
1708			lpfc_printf_log(phba, KERN_INFO, LOG_INIT, "%d:0452 "
1709					"Enable MSI failed, continuing with "
1710					"IRQ\n", phba->brd_no);
1711	}
1712
1713	error =	request_irq(phba->pcidev->irq, lpfc_intr_handler, IRQF_SHARED,
1714			    LPFC_DRIVER_NAME, phba);
1715	if (error) {
1716		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1717			"%d:0451 Enable interrupt handler failed\n",
1718			phba->brd_no);
1719		goto out_destroy_port;
1720	}
1721
1722	phba->MBslimaddr = phba->slim_memmap_p;
1723	phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
1724	phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
1725	phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
1726	phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
1727
1728	error = lpfc_sli_hba_setup(phba);
1729	if (error)
1730		goto out_free_irq;
1731
1732	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
1733		spin_lock_irq(shost->host_lock);
1734		lpfc_poll_start_timer(phba);
1735		spin_unlock_irq(shost->host_lock);
1736	}
1737
1738	return 0;
1739
1740out_free_irq:
1741	lpfc_stop_timer(phba);
1742	phba->pport->work_port_events = 0;
1743	free_irq(phba->pcidev->irq, phba);
1744	pci_disable_msi(phba->pcidev);
1745out_destroy_port:
1746	destroy_port(vport);
1747out_kthread_stop:
1748	kthread_stop(phba->worker_thread);
1749out_free_iocbq:
1750	list_for_each_entry_safe(iocbq_entry, iocbq_next,
1751						&phba->lpfc_iocb_list, list) {
1752		kfree(iocbq_entry);
1753		phba->total_iocbq_bufs--;
1754	}
1755	lpfc_mem_free(phba);
1756out_free_slim:
1757	dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE, phba->slim2p,
1758							phba->slim2p_mapping);
1759out_iounmap:
1760	iounmap(phba->ctrl_regs_memmap_p);
1761out_iounmap_slim:
1762	iounmap(phba->slim_memmap_p);
1763out_idr_remove:
1764	idr_remove(&lpfc_hba_index, phba->brd_no);
1765out_free_phba:
1766	kfree(phba);
1767out_release_regions:
1768	pci_release_regions(pdev);
1769out_disable_device:
1770	pci_disable_device(pdev);
1771out:
1772	pci_set_drvdata(pdev, NULL);
1773	return error;
1774}
1775
1776static void __devexit
1777lpfc_pci_remove_one(struct pci_dev *pdev)
1778{
1779	struct Scsi_Host  *shost = pci_get_drvdata(pdev);
1780	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1781	struct lpfc_hba   *phba = vport->phba;
1782
1783	vport->load_flag |= FC_UNLOADING;
1784	lpfc_remove_device(vport);
1785
1786	/*
1787	 * Bring down the SLI Layer. This step disable all interrupts,
1788	 * clears the rings, discards all mailbox commands, and resets
1789	 * the HBA.
1790	 */
1791	lpfc_sli_hba_down(phba);
1792	lpfc_sli_brdrestart(phba);
1793
1794	lpfc_stop_timer(phba);
1795
1796	kthread_stop(phba->worker_thread);
1797
1798	/* Release the irq reservation */
1799	free_irq(phba->pcidev->irq, phba);
1800	pci_disable_msi(phba->pcidev);
1801
1802	vport->work_port_events = 0;
1803	destroy_port(vport);
1804
1805	pci_set_drvdata(pdev, NULL);
1806
1807	/*
1808	 * Call scsi_free before mem_free since scsi bufs are released to their
1809	 * corresponding pools here.
1810	 */
1811	lpfc_scsi_free(phba);
1812	lpfc_mem_free(phba);
1813
1814	/* Free resources associated with SLI2 interface */
1815	dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
1816			  phba->slim2p, phba->slim2p_mapping);
1817
1818	/* unmap adapter SLIM and Control Registers */
1819	iounmap(phba->ctrl_regs_memmap_p);
1820	iounmap(phba->slim_memmap_p);
1821
1822	idr_remove(&lpfc_hba_index, phba->brd_no);
1823
1824	kfree(phba);
1825
1826	pci_release_regions(pdev);
1827	pci_disable_device(pdev);
1828}
1829
1830/**
1831 * lpfc_io_error_detected - called when PCI error is detected
1832 * @pdev: Pointer to PCI device
1833 * @state: The current pci conneection state
1834 *
1835 * This function is called after a PCI bus error affecting
1836 * this device has been detected.
1837 */
1838static pci_ers_result_t lpfc_io_error_detected(struct pci_dev *pdev,
1839				pci_channel_state_t state)
1840{
1841	struct Scsi_Host *host = pci_get_drvdata(pdev);
1842	struct lpfc_hba *phba = (struct lpfc_hba *)host->hostdata;
1843	struct lpfc_sli *psli = &phba->sli;
1844	struct lpfc_sli_ring  *pring;
1845
1846	if (state == pci_channel_io_perm_failure)
1847		return PCI_ERS_RESULT_DISCONNECT;
1848
1849	pci_disable_device(pdev);
1850	/*
1851	 * There may be I/Os dropped by the firmware.
1852	 * Error iocb (I/O) on txcmplq and let the SCSI layer
1853	 * retry it after re-establishing link.
1854	 */
1855	pring = &psli->ring[psli->fcp_ring];
1856	lpfc_sli_abort_iocb_ring(phba, pring);
1857
1858	/* Request a slot reset. */
1859	return PCI_ERS_RESULT_NEED_RESET;
1860}
1861
1862/**
1863 * lpfc_io_slot_reset - called after the pci bus has been reset.
1864 * @pdev: Pointer to PCI device
1865 *
1866 * Restart the card from scratch, as if from a cold-boot.
1867 */
1868static pci_ers_result_t lpfc_io_slot_reset(struct pci_dev *pdev)
1869{
1870	struct Scsi_Host *host = pci_get_drvdata(pdev);
1871	struct lpfc_hba *phba = (struct lpfc_hba *)host->hostdata;
1872	struct lpfc_sli *psli = &phba->sli;
1873	int bars = pci_select_bars(pdev, IORESOURCE_MEM);
1874
1875	dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
1876	if (pci_enable_device_bars(pdev, bars)) {
1877		printk(KERN_ERR "lpfc: Cannot re-enable "
1878			"PCI device after reset.\n");
1879		return PCI_ERS_RESULT_DISCONNECT;
1880	}
1881
1882	pci_set_master(pdev);
1883
1884	/* Re-establishing Link */
1885	spin_lock_irq(&phba->hbalock);
1886	phba->pport->fc_flag |= FC_ESTABLISH_LINK;
1887	spin_unlock_irq(&phba->hbalock);
1888	spin_lock_irq(host->host_lock);
1889	psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
1890	spin_unlock_irq(host->host_lock);
1891
1892
1893	/* Take device offline; this will perform cleanup */
1894	lpfc_offline(phba);
1895	lpfc_sli_brdrestart(phba);
1896
1897	return PCI_ERS_RESULT_RECOVERED;
1898}
1899
1900/**
1901 * lpfc_io_resume - called when traffic can start flowing again.
1902 * @pdev: Pointer to PCI device
1903 *
1904 * This callback is called when the error recovery driver tells us that
1905 * its OK to resume normal operation.
1906 */
1907static void lpfc_io_resume(struct pci_dev *pdev)
1908{
1909	struct Scsi_Host *host = pci_get_drvdata(pdev);
1910	struct lpfc_hba *phba = (struct lpfc_hba *)host->hostdata;
1911
1912	if (lpfc_online(phba) == 0) {
1913		mod_timer(&phba->fc_estabtmo, jiffies + HZ * 60);
1914	}
1915}
1916
1917static struct pci_device_id lpfc_id_table[] = {
1918	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
1919		PCI_ANY_ID, PCI_ANY_ID, },
1920	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
1921		PCI_ANY_ID, PCI_ANY_ID, },
1922	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
1923		PCI_ANY_ID, PCI_ANY_ID, },
1924	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
1925		PCI_ANY_ID, PCI_ANY_ID, },
1926	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
1927		PCI_ANY_ID, PCI_ANY_ID, },
1928	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
1929		PCI_ANY_ID, PCI_ANY_ID, },
1930	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
1931		PCI_ANY_ID, PCI_ANY_ID, },
1932	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
1933		PCI_ANY_ID, PCI_ANY_ID, },
1934	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
1935		PCI_ANY_ID, PCI_ANY_ID, },
1936	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
1937		PCI_ANY_ID, PCI_ANY_ID, },
1938	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
1939		PCI_ANY_ID, PCI_ANY_ID, },
1940	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
1941		PCI_ANY_ID, PCI_ANY_ID, },
1942	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
1943		PCI_ANY_ID, PCI_ANY_ID, },
1944	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
1945		PCI_ANY_ID, PCI_ANY_ID, },
1946	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
1947		PCI_ANY_ID, PCI_ANY_ID, },
1948	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
1949		PCI_ANY_ID, PCI_ANY_ID, },
1950	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
1951		PCI_ANY_ID, PCI_ANY_ID, },
1952	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
1953		PCI_ANY_ID, PCI_ANY_ID, },
1954	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
1955		PCI_ANY_ID, PCI_ANY_ID, },
1956	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
1957		PCI_ANY_ID, PCI_ANY_ID, },
1958	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
1959		PCI_ANY_ID, PCI_ANY_ID, },
1960	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
1961		PCI_ANY_ID, PCI_ANY_ID, },
1962	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
1963		PCI_ANY_ID, PCI_ANY_ID, },
1964	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
1965		PCI_ANY_ID, PCI_ANY_ID, },
1966	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
1967		PCI_ANY_ID, PCI_ANY_ID, },
1968	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
1969		PCI_ANY_ID, PCI_ANY_ID, },
1970	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
1971		PCI_ANY_ID, PCI_ANY_ID, },
1972	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
1973		PCI_ANY_ID, PCI_ANY_ID, },
1974	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
1975		PCI_ANY_ID, PCI_ANY_ID, },
1976	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
1977		PCI_ANY_ID, PCI_ANY_ID, },
1978	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
1979		PCI_ANY_ID, PCI_ANY_ID, },
1980	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
1981		PCI_ANY_ID, PCI_ANY_ID, },
1982	{PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
1983		PCI_ANY_ID, PCI_ANY_ID, },
1984	{ 0 }
1985};
1986
1987MODULE_DEVICE_TABLE(pci, lpfc_id_table);
1988
1989static struct pci_error_handlers lpfc_err_handler = {
1990	.error_detected = lpfc_io_error_detected,
1991	.slot_reset = lpfc_io_slot_reset,
1992	.resume = lpfc_io_resume,
1993};
1994
1995static struct pci_driver lpfc_driver = {
1996	.name		= LPFC_DRIVER_NAME,
1997	.id_table	= lpfc_id_table,
1998	.probe		= lpfc_pci_probe_one,
1999	.remove		= __devexit_p(lpfc_pci_remove_one),
2000	.err_handler    = &lpfc_err_handler,
2001};
2002
2003static int __init
2004lpfc_init(void)
2005{
2006	int error = 0;
2007
2008	printk(LPFC_MODULE_DESC "\n");
2009	printk(LPFC_COPYRIGHT "\n");
2010
2011	lpfc_transport_template =
2012				fc_attach_transport(&lpfc_transport_functions);
2013	if (!lpfc_transport_template)
2014		return -ENOMEM;
2015	error = pci_register_driver(&lpfc_driver);
2016	if (error)
2017		fc_release_transport(lpfc_transport_template);
2018
2019	return error;
2020}
2021
2022static void __exit
2023lpfc_exit(void)
2024{
2025	pci_unregister_driver(&lpfc_driver);
2026	fc_release_transport(lpfc_transport_template);
2027}
2028
2029module_init(lpfc_init);
2030module_exit(lpfc_exit);
2031MODULE_LICENSE("GPL");
2032MODULE_DESCRIPTION(LPFC_MODULE_DESC);
2033MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
2034MODULE_VERSION("0:" LPFC_DRIVER_VERSION);
2035