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