blob: e50c5ad252f9734415b3eabda6ccdc890acbbc19 [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smart9413aff2007-04-25 09:53:35 -04004 * Copyright (C) 2004-2007 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * 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. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#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
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040031#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050032#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
Jamie Wellnitz74b72a52006-02-28 22:33:04 -050045static int lpfc_parse_vpd(struct lpfc_hba *, uint8_t *, int);
dea31012005-04-17 16:05:31 -050046static 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
James Smarted957682007-06-17 19:56:37 -050052int 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");
James Smart2e0fef82007-06-17 19:56:36 -050058
59
dea31012005-04-17 16:05:31 -050060/************************************************************************/
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
James Smart2e0fef82007-06-17 19:56:36 -050072lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050073{
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";
James Smart65a29c12006-07-06 15:50:50 -040082 static int init_key = 1;
dea31012005-04-17 16:05:31 -050083
84 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
85 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -050086 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -050087 return -ENOMEM;
88 }
89
90 mb = &pmb->mb;
James Smart2e0fef82007-06-17 19:56:36 -050091 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -050092
93 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -040094 if (init_key) {
95 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -050096
James Smart65a29c12006-07-06 15:50:50 -040097 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
98 *ptext = cpu_to_be32(*ptext);
99 init_key = 0;
100 }
dea31012005-04-17 16:05:31 -0500101
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) {
James Smarted957682007-06-17 19:56:37 -0500111 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
dea31012005-04-17 16:05:31 -0500112 "%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,
James Smart2e0fef82007-06-17 19:56:36 -0500121 sizeof(phba->wwnn));
122 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
123 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500124 }
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) {
James Smarted957682007-06-17 19:56:37 -0500130 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
dea31012005-04-17 16:05:31 -0500131 "%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
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500139 /*
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.
dea31012005-04-17 16:05:31 -0500142 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500143 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500144 vp->rev.rBit = 0;
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500145 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);
dea31012005-04-17 16:05:31 -0500149 mempool_free(pmb, phba->mbox_mem_pool);
150 return -ERESTART;
dea31012005-04-17 16:05:31 -0500151 }
152
James Smarted957682007-06-17 19:56:37 -0500153 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp)
154 return -EINVAL;
155
dea31012005-04-17 16:05:31 -0500156 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500157 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);
dea31012005-04-17 16:05:31 -0500162 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
dea31012005-04-17 16:05:31 -0500177 /* 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);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500195 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500196 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500197 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
198 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
dea31012005-04-17 16:05:31 -0500199 lpfc_sli_pcimem_bcopy(pmb->context2, lpfc_vpd_data + offset,
200 mb->un.varDmp.word_cnt);
201 offset += mb->un.varDmp.word_cnt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500202 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
203 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500204
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
James Smart2e0fef82007-06-17 19:56:36 -0500224lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500225{
James Smart2e0fef82007-06-17 19:56:36 -0500226 struct lpfc_vport *vport = phba->pport;
dea31012005-04-17 16:05:31 -0500227 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;
James Smart2e0fef82007-06-17 19:56:36 -0500232 int i, j;
233 int rc;
dea31012005-04-17 16:05:31 -0500234
235 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
236 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500237 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500238 return -ENOMEM;
239 }
240 mb = &pmb->mb;
241
dea31012005-04-17 16:05:31 -0500242 /* Get login parameters for NID. */
243 lpfc_read_sparam(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500244 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500245 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500246 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
dea31012005-04-17 16:05:31 -0500247 "%d:0448 Adapter failed init, mbxCmd x%x "
248 "READ_SPARM mbxStatus x%x\n",
249 phba->brd_no,
250 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500251 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500252 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
James Smart2e0fef82007-06-17 19:56:36 -0500261 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500262 lpfc_mbuf_free(phba, mp->virt, mp->phys);
263 kfree(mp);
264 pmb->context1 = NULL;
265
James Smarta12e07b2006-12-02 13:35:30 -0500266 if (phba->cfg_soft_wwnn)
James Smart2e0fef82007-06-17 19:56:36 -0500267 u64_to_wwn(phba->cfg_soft_wwnn,
268 vport->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -0400269 if (phba->cfg_soft_wwpn)
James Smart2e0fef82007-06-17 19:56:36 -0500270 u64_to_wwn(phba->cfg_soft_wwpn,
271 vport->fc_sparam.portName.u.wwn);
272 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
dea31012005-04-17 16:05:31 -0500273 sizeof (struct lpfc_name));
James Smart2e0fef82007-06-17 19:56:36 -0500274 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
dea31012005-04-17 16:05:31 -0500275 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
James Smart2e0fef82007-06-17 19:56:36 -0500281 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500282 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
dea31012005-04-17 16:05:31 -0500302 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500303 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500304 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500305 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
dea31012005-04-17 16:05:31 -0500306 "%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);
James Smart2e0fef82007-06-17 19:56:36 -0500310 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500311 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;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500321
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 */
James Smarted957682007-06-17 19:56:37 -0500337 lpfc_printf_log(phba, KERN_WARNING, LOG_LINK_EVENT,
dea31012005-04-17 16:05:31 -0500338 "%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
James Smart2e0fef82007-06-17 19:56:36 -0500345 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500346
347 /* Only process IOCBs on ring 0 till hba_state is READY */
James Smarta4bc3372006-12-02 13:34:16 -0500348 if (psli->ring[psli->extra_ring].cmdringaddr)
349 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500350 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 */
James Smarted957682007-06-17 19:56:37 -0500356 if (phba->sli_rev != 3)
357 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500358
359 /* Enable appropriate host interrupts */
James Smart2e0fef82007-06-17 19:56:36 -0500360 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500361 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
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500372 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
dea31012005-04-17 16:05:31 -0500376 writel(status, phba->HCregaddr);
377 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500378 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500379
380 /*
381 * Setup the ring 0 (els) timeout handler
382 */
383 timeout = phba->fc_ratov << 1;
James Smart2e0fef82007-06-17 19:56:36 -0500384 mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
dea31012005-04-17 16:05:31 -0500385
386 lpfc_init_link(phba, pmb, phba->cfg_topology, phba->cfg_link_speed);
387 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500388 pmb->vport = vport;
James Smart8aee9182006-08-31 12:27:57 -0400389 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -0400390 lpfc_set_loopback_flag(phba);
James Smart8aee9182006-08-31 12:27:57 -0400391 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500392 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
dea31012005-04-17 16:05:31 -0500393 "%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
James Smart2e0fef82007-06-17 19:56:36 -0500405 phba->link_state = LPFC_HBA_ERROR;
James Smart8aee9182006-08-31 12:27:57 -0400406 if (rc != MBX_BUSY)
407 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500408 return -EIO;
409 }
410 /* MBOX buffer will be freed in mbox compl */
411
James Smartce8b3ce2006-07-06 15:50:36 -0400412 return (0);
413}
414
dea31012005-04-17 16:05:31 -0500415/************************************************************************/
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
James Smart2e0fef82007-06-17 19:56:36 -0500426lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500427{
James Smart2e0fef82007-06-17 19:56:36 -0500428 struct lpfc_vport *vport = phba->pport;
429
dea31012005-04-17 16:05:31 -0500430 /* Disable interrupts */
431 writel(0, phba->HCregaddr);
432 readl(phba->HCregaddr); /* flush */
433
James Smart2e0fef82007-06-17 19:56:36 -0500434 /* Cleanup potential discovery resources */
435 lpfc_els_flush_rscn(vport);
436 lpfc_els_flush_cmd(vport);
437 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -0500438
James Smart2e0fef82007-06-17 19:56:36 -0500439 return 0;
dea31012005-04-17 16:05:31 -0500440}
441
442/************************************************************************/
443/* */
Jamie Wellnitz41415862006-02-28 19:25:27 -0500444/* 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
James Smart2e0fef82007-06-17 19:56:36 -0500452lpfc_hba_down_post(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -0500453{
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/* */
dea31012005-04-17 16:05:31 -0500478/* 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
James Smart2e0fef82007-06-17 19:56:36 -0500485lpfc_handle_eratt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500486{
James Smart2e0fef82007-06-17 19:56:36 -0500487 struct lpfc_vport *vport = phba->pport;
488 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
489 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -0500490 struct lpfc_sli_ring *pring;
James Smartd2873e42006-08-18 17:46:43 -0400491 uint32_t event_data;
James Smart2e0fef82007-06-17 19:56:36 -0500492
Linas Vepstas8d63f372007-02-14 14:28:36 -0600493 /* 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;
dea31012005-04-17 16:05:31 -0500497
James Smartf5603512006-12-02 13:35:43 -0500498 if (phba->work_hs & HS_FFER6 ||
499 phba->work_hs & HS_FFER5) {
dea31012005-04-17 16:05:31 -0500500 /* 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]);
James Smart2e0fef82007-06-17 19:56:36 -0500506 spin_lock_irq(shost->host_lock);
507 vport->fc_flag |= FC_ESTABLISH_LINK;
James Smart92908312006-03-07 15:04:13 -0500508 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -0500509 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500510
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 */
James Smart46fa3112007-04-25 09:51:45 -0400525 lpfc_offline_prep(phba);
dea31012005-04-17 16:05:31 -0500526 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500527 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -0500528 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
529 mod_timer(&phba->fc_estabtmo, jiffies + HZ * 60);
James Smart46fa3112007-04-25 09:51:45 -0400530 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -0500531 return;
532 }
James Smart46fa3112007-04-25 09:51:45 -0400533 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -0500534 } 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
James Smartd2873e42006-08-18 17:46:43 -0400545 event_data = FC_REG_DUMP_EVENT;
James Smart2e0fef82007-06-17 19:56:36 -0500546 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -0400547 sizeof(event_data), (char *) &event_data,
548 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
549
James Smart92908312006-03-07 15:04:13 -0500550 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
James Smart46fa3112007-04-25 09:51:45 -0400551 lpfc_offline_prep(phba);
dea31012005-04-17 16:05:31 -0500552 lpfc_offline(phba);
James Smart46fa3112007-04-25 09:51:45 -0400553 lpfc_unblock_mgmt_io(phba);
James Smart2e0fef82007-06-17 19:56:36 -0500554 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -0500555 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -0500556 }
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
James Smart2e0fef82007-06-17 19:56:36 -0500568lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500569{
James Smart2e0fef82007-06-17 19:56:36 -0500570 struct lpfc_vport *vport = phba->pport;
571 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -0500572 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
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -0500591 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -0500592 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -0500593
594 psli->slistat.link_event++;
595 lpfc_read_la(phba, pmb, mp);
596 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_la;
James Smart2e0fef82007-06-17 19:56:36 -0500597 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500598 rc = lpfc_sli_issue_mbox (phba, pmb, (MBX_NOWAIT | MBX_STOP_IOCB));
599 if (rc == MBX_NOT_FINISHED)
James Smart14691152006-12-02 13:34:28 -0500600 goto lpfc_handle_latt_free_mbuf;
dea31012005-04-17 16:05:31 -0500601
602 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -0500603 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500604 writel(HA_LATT, phba->HAregaddr);
605 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500606 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500607
608 return;
609
James Smart14691152006-12-02 13:34:28 -0500610lpfc_handle_latt_free_mbuf:
611 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -0500612lpfc_handle_latt_free_mp:
613 kfree(mp);
614lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -0400615 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500616lpfc_handle_latt_err_exit:
617 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -0500618 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500619 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 */
James Smart2e0fef82007-06-17 19:56:36 -0500628 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500629 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -0500630 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500631
632 /* The other case is an error from issue_mbox */
633 if (rc == -ENOMEM)
James Smarted957682007-06-17 19:56:37 -0500634 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
dea31012005-04-17 16:05:31 -0500635 "%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
James Smart2e0fef82007-06-17 19:56:36 -0500648lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -0500649{
650 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -0500651 int Length;
dea31012005-04-17 16:05:31 -0500652 int i, j;
653 int finished = 0;
654 int index = 0;
655
656 if (!vpd)
657 return 0;
658
659 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -0500660 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
dea31012005-04-17 16:05:31 -0500661 "%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]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500665 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -0500666 switch (vpd[index]) {
667 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500668 case 0x91:
dea31012005-04-17 16:05:31 -0500669 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);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500684 if (Length > len - index)
685 Length = len - index;
dea31012005-04-17 16:05:31 -0500686 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 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500779 }
dea31012005-04-17 16:05:31 -0500780
781 return(1);
782}
783
784static void
James Smart2e0fef82007-06-17 19:56:36 -0500785lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -0500786{
787 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -0500788 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500789 int max_speed;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500790 struct {
791 char * name;
792 int max_speed;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500793 char * bus;
James Smart18a3b592006-12-02 13:35:08 -0500794 } m = {"<Unknown>", 0, ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500795
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;
dea31012005-04-17 16:05:31 -0500810
811 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -0500812
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -0500813 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -0400814 case PCI_DEVICE_ID_FIREFLY:
James Smart18a3b592006-12-02 13:35:08 -0500815 m = (typeof(m)){"LP6000", max_speed, "PCI"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -0400816 break;
dea31012005-04-17 16:05:31 -0500817 case PCI_DEVICE_ID_SUPERFLY:
818 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smart18a3b592006-12-02 13:35:08 -0500819 m = (typeof(m)){"LP7000", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -0500820 else
James Smart18a3b592006-12-02 13:35:08 -0500821 m = (typeof(m)){"LP7000E", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -0500822 break;
823 case PCI_DEVICE_ID_DRAGONFLY:
James Smart18a3b592006-12-02 13:35:08 -0500824 m = (typeof(m)){"LP8000", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -0500825 break;
826 case PCI_DEVICE_ID_CENTAUR:
827 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smart18a3b592006-12-02 13:35:08 -0500828 m = (typeof(m)){"LP9002", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -0500829 else
James Smart18a3b592006-12-02 13:35:08 -0500830 m = (typeof(m)){"LP9000", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -0500831 break;
832 case PCI_DEVICE_ID_RFLY:
James Smart18a3b592006-12-02 13:35:08 -0500833 m = (typeof(m)){"LP952", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -0500834 break;
835 case PCI_DEVICE_ID_PEGASUS:
James Smart18a3b592006-12-02 13:35:08 -0500836 m = (typeof(m)){"LP9802", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -0500837 break;
838 case PCI_DEVICE_ID_THOR:
James Smart18a3b592006-12-02 13:35:08 -0500839 m = (typeof(m)){"LP10000", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -0500840 break;
841 case PCI_DEVICE_ID_VIPER:
James Smart18a3b592006-12-02 13:35:08 -0500842 m = (typeof(m)){"LPX1000", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -0500843 break;
844 case PCI_DEVICE_ID_PFLY:
James Smart18a3b592006-12-02 13:35:08 -0500845 m = (typeof(m)){"LP982", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -0500846 break;
847 case PCI_DEVICE_ID_TFLY:
James Smart18a3b592006-12-02 13:35:08 -0500848 m = (typeof(m)){"LP1050", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -0500849 break;
850 case PCI_DEVICE_ID_HELIOS:
James Smart18a3b592006-12-02 13:35:08 -0500851 m = (typeof(m)){"LP11000", max_speed, "PCI-X2"};
dea31012005-04-17 16:05:31 -0500852 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -0500853 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smart18a3b592006-12-02 13:35:08 -0500854 m = (typeof(m)){"LP11000-SP", max_speed, "PCI-X2"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -0500855 break;
856 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smart18a3b592006-12-02 13:35:08 -0500857 m = (typeof(m)){"LP11002-SP", max_speed, "PCI-X2"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -0500858 break;
859 case PCI_DEVICE_ID_NEPTUNE:
James Smart18a3b592006-12-02 13:35:08 -0500860 m = (typeof(m)){"LPe1000", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -0500861 break;
862 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smart18a3b592006-12-02 13:35:08 -0500863 m = (typeof(m)){"LPe1000-SP", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -0500864 break;
865 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smart18a3b592006-12-02 13:35:08 -0500866 m = (typeof(m)){"LPe1002-SP", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -0500867 break;
dea31012005-04-17 16:05:31 -0500868 case PCI_DEVICE_ID_BMID:
James Smart18a3b592006-12-02 13:35:08 -0500869 m = (typeof(m)){"LP1150", max_speed, "PCI-X2"};
dea31012005-04-17 16:05:31 -0500870 break;
871 case PCI_DEVICE_ID_BSMB:
James Smart18a3b592006-12-02 13:35:08 -0500872 m = (typeof(m)){"LP111", max_speed, "PCI-X2"};
dea31012005-04-17 16:05:31 -0500873 break;
874 case PCI_DEVICE_ID_ZEPHYR:
James Smart18a3b592006-12-02 13:35:08 -0500875 m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
dea31012005-04-17 16:05:31 -0500876 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -0500877 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smart18a3b592006-12-02 13:35:08 -0500878 m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -0500879 break;
880 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smart18a3b592006-12-02 13:35:08 -0500881 m = (typeof(m)){"LPe11002-SP", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -0500882 break;
dea31012005-04-17 16:05:31 -0500883 case PCI_DEVICE_ID_ZMID:
James Smart18a3b592006-12-02 13:35:08 -0500884 m = (typeof(m)){"LPe1150", max_speed, "PCIe"};
dea31012005-04-17 16:05:31 -0500885 break;
886 case PCI_DEVICE_ID_ZSMB:
James Smart18a3b592006-12-02 13:35:08 -0500887 m = (typeof(m)){"LPe111", max_speed, "PCIe"};
dea31012005-04-17 16:05:31 -0500888 break;
889 case PCI_DEVICE_ID_LP101:
James Smart18a3b592006-12-02 13:35:08 -0500890 m = (typeof(m)){"LP101", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -0500891 break;
892 case PCI_DEVICE_ID_LP10000S:
James Smart18a3b592006-12-02 13:35:08 -0500893 m = (typeof(m)){"LP10000-S", max_speed, "PCI"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -0400894 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -0500895 case PCI_DEVICE_ID_LP11000S:
James Smart18a3b592006-12-02 13:35:08 -0500896 m = (typeof(m)){"LP11000-S", max_speed,
897 "PCI-X2"};
898 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -0500899 case PCI_DEVICE_ID_LPE11000S:
James Smart18a3b592006-12-02 13:35:08 -0500900 m = (typeof(m)){"LPe11000-S", max_speed,
901 "PCIe"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -0500902 break;
James Smartb87eab32007-04-25 09:53:28 -0400903 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;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -0500921 default:
Randy Dunlap041976f2006-06-25 01:58:51 -0700922 m = (typeof(m)){ NULL };
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -0500923 break;
dea31012005-04-17 16:05:31 -0500924 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500925
926 if (mdp && mdp[0] == '\0')
927 snprintf(mdp, 79,"%s", m.name);
928 if (descp && descp[0] == '\0')
929 snprintf(descp, 255,
James Smart18a3b592006-12-02 13:35:08 -0500930 "Emulex %s %dGb %s Fibre Channel Adapter",
931 m.name, m.max_speed, m.bus);
dea31012005-04-17 16:05:31 -0500932}
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
James Smart2e0fef82007-06-17 19:56:36 -0500943lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt,
dea31012005-04-17 16:05:31 -0500944 int type)
945{
946 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400947 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -0500948 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 */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400955 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -0500956 if (iocb == NULL) {
957 pring->missbufcnt = cnt;
958 return cnt;
959 }
dea31012005-04-17 16:05:31 -0500960 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) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -0800969 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -0500970 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -0500971 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) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -0800983 kfree(mp2);
dea31012005-04-17 16:05:31 -0500984 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
985 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -0500986 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -0500987 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
dea31012005-04-17 16:05:31 -05001012 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 }
James Bottomley604a3e32005-10-29 10:28:33 -05001021 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001022 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05001023 return cnt;
1024 }
dea31012005-04-17 16:05:31 -05001025 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
James Smart2e0fef82007-06-17 19:56:36 -05001041lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001042{
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;
James Smart2e0fef82007-06-17 19:56:36 -05001142 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05001143
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
James Smart2e0fef82007-06-17 19:56:36 -05001161lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05001162{
1163 struct lpfc_nodelist *ndlp, *next_ndlp;
1164
1165 /* clean up phba - lpfc specific */
James Smart2e0fef82007-06-17 19:56:36 -05001166 lpfc_can_disctmo(vport);
1167 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp)
James Smart329f9bc2007-04-25 09:53:01 -04001168 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001169
James Smart2e0fef82007-06-17 19:56:36 -05001170 INIT_LIST_HEAD(&vport->fc_nodes);
dea31012005-04-17 16:05:31 -05001171
dea31012005-04-17 16:05:31 -05001172 return;
1173}
1174
1175static void
1176lpfc_establish_link_tmo(unsigned long ptr)
1177{
James Smart2e0fef82007-06-17 19:56:36 -05001178 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);
dea31012005-04-17 16:05:31 -05001181 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",
James Smart2e0fef82007-06-17 19:56:36 -05001188 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);
dea31012005-04-17 16:05:31 -05001193}
1194
James Smart2e0fef82007-06-17 19:56:36 -05001195static void
1196lpfc_stop_timer(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001197{
James Smart2e0fef82007-06-17 19:56:36 -05001198 struct lpfc_vport *vport = phba->pport;
dea31012005-04-17 16:05:31 -05001199
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001200 del_timer_sync(&phba->fcp_poll_timer);
dea31012005-04-17 16:05:31 -05001201 del_timer_sync(&phba->fc_estabtmo);
James Smart2e0fef82007-06-17 19:56:36 -05001202 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;
dea31012005-04-17 16:05:31 -05001207}
1208
1209int
James Smart2e0fef82007-06-17 19:56:36 -05001210lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001211{
James Smart2e0fef82007-06-17 19:56:36 -05001212 struct lpfc_vport *vport = phba->pport;
1213 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1214
dea31012005-04-17 16:05:31 -05001215 if (!phba)
1216 return 0;
1217
James Smart2e0fef82007-06-17 19:56:36 -05001218 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05001219 return 0;
1220
James Smarted957682007-06-17 19:56:37 -05001221 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
dea31012005-04-17 16:05:31 -05001222 "%d:0458 Bring Adapter online\n",
1223 phba->brd_no);
1224
James Smart46fa3112007-04-25 09:51:45 -04001225 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001226
James Smart46fa3112007-04-25 09:51:45 -04001227 if (!lpfc_sli_queue_setup(phba)) {
1228 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001229 return 1;
James Smart46fa3112007-04-25 09:51:45 -04001230 }
1231
1232 if (lpfc_sli_hba_setup(phba)) { /* Initialize the HBA */
1233 lpfc_unblock_mgmt_io(phba);
1234 return 1;
1235 }
dea31012005-04-17 16:05:31 -05001236
James Smart2e0fef82007-06-17 19:56:36 -05001237 spin_lock_irq(shost->host_lock);
1238 vport->fc_flag &= ~FC_OFFLINE_MODE;
1239 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001240
James Smart46fa3112007-04-25 09:51:45 -04001241 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001242 return 0;
1243}
1244
James Smart46fa3112007-04-25 09:51:45 -04001245void
1246lpfc_block_mgmt_io(struct lpfc_hba * phba)
dea31012005-04-17 16:05:31 -05001247{
dea31012005-04-17 16:05:31 -05001248 unsigned long iflag;
dea31012005-04-17 16:05:31 -05001249
James Smart2e0fef82007-06-17 19:56:36 -05001250 spin_lock_irqsave(&phba->hbalock, iflag);
1251 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
1252 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04001253}
1254
1255void
1256lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
1257{
1258 unsigned long iflag;
1259
James Smart2e0fef82007-06-17 19:56:36 -05001260 spin_lock_irqsave(&phba->hbalock, iflag);
1261 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
1262 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04001263}
1264
1265void
1266lpfc_offline_prep(struct lpfc_hba * phba)
1267{
James Smart2e0fef82007-06-17 19:56:36 -05001268 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04001269 struct lpfc_nodelist *ndlp, *next_ndlp;
dea31012005-04-17 16:05:31 -05001270
James Smart2e0fef82007-06-17 19:56:36 -05001271 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04001272 return;
dea31012005-04-17 16:05:31 -05001273
James Smart46fa3112007-04-25 09:51:45 -04001274 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001275
1276 lpfc_linkdown(phba);
1277
James Smart46fa3112007-04-25 09:51:45 -04001278 /* Issue an unreg_login to all nodes */
James Smart2e0fef82007-06-17 19:56:36 -05001279 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp)
James Smart685f0bf2007-04-25 09:53:08 -04001280 if (ndlp->nlp_state != NLP_STE_UNUSED_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05001281 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05001282
James Smart46fa3112007-04-25 09:51:45 -04001283 lpfc_sli_flush_mbox_queue(phba);
1284}
1285
1286void
James Smart2e0fef82007-06-17 19:56:36 -05001287lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04001288{
James Smart2e0fef82007-06-17 19:56:36 -05001289 struct lpfc_vport *vport = phba->pport;
1290 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart46fa3112007-04-25 09:51:45 -04001291 unsigned long iflag;
1292
James Smart2e0fef82007-06-17 19:56:36 -05001293 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04001294 return;
James Smart688a8862006-07-06 15:49:56 -04001295
dea31012005-04-17 16:05:31 -05001296 /* stop all timers associated with this hba */
1297 lpfc_stop_timer(phba);
dea31012005-04-17 16:05:31 -05001298
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);
James Smart2e0fef82007-06-17 19:56:36 -05001308 lpfc_cleanup(vport);
1309 spin_lock_irqsave(shost->host_lock, iflag);
1310 spin_lock(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04001311 phba->work_ha = 0;
James Smart2e0fef82007-06-17 19:56:36 -05001312 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);
dea31012005-04-17 16:05:31 -05001316}
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
James Smart2e0fef82007-06-17 19:56:36 -05001325lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001326{
1327 struct lpfc_scsi_buf *sb, *sb_next;
1328 struct lpfc_iocbq *io, *io_next;
1329
James Smart2e0fef82007-06-17 19:56:36 -05001330 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001331 /* 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
James Smart2e0fef82007-06-17 19:56:36 -05001347 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001348
1349 return 0;
1350}
1351
James Smart2e0fef82007-06-17 19:56:36 -05001352struct lpfc_vport *
1353lpfc_create_port(struct lpfc_hba *phba, int instance)
James Smart47a86172007-04-25 09:53:22 -04001354{
James Smart2e0fef82007-06-17 19:56:36 -05001355 struct lpfc_vport *vport;
1356 struct Scsi_Host *shost;
1357 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04001358
James Smart2e0fef82007-06-17 19:56:36 -05001359 shost = scsi_host_alloc(&lpfc_template, sizeof(struct lpfc_vport));
1360 if (!shost)
1361 goto out;
James Smart47a86172007-04-25 09:53:22 -04001362
James Smart2e0fef82007-06-17 19:56:36 -05001363 vport = (struct lpfc_vport *) shost->hostdata;
1364 vport->phba = phba;
James Smart47a86172007-04-25 09:53:22 -04001365
James Smart2e0fef82007-06-17 19:56:36 -05001366 vport->load_flag |= FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04001367
James Smart2e0fef82007-06-17 19:56:36 -05001368 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;
James Smart47a86172007-04-25 09:53:22 -04001373 /*
James Smart2e0fef82007-06-17 19:56:36 -05001374 * 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.
James Smart47a86172007-04-25 09:53:22 -04001377 */
James Smart2e0fef82007-06-17 19:56:36 -05001378 shost->can_queue = phba->cfg_hba_queue_depth - 10;
1379 shost->transportt = lpfc_transport_template;
James Smart47a86172007-04-25 09:53:22 -04001380
James Smart2e0fef82007-06-17 19:56:36 -05001381 /* Initialize all internally managed lists. */
1382 INIT_LIST_HEAD(&vport->fc_nodes);
1383 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04001384
James Smart2e0fef82007-06-17 19:56:36 -05001385 init_timer(&vport->fc_disctmo);
1386 vport->fc_disctmo.function = lpfc_disc_timeout;
1387 vport->fc_disctmo.data = (unsigned long) vport;
James Smart47a86172007-04-25 09:53:22 -04001388
James Smart2e0fef82007-06-17 19:56:36 -05001389 init_timer(&vport->fc_fdmitmo);
1390 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
1391 vport->fc_fdmitmo.data = (unsigned long) vport;
James Smart47a86172007-04-25 09:53:22 -04001392
James Smart2e0fef82007-06-17 19:56:36 -05001393 init_timer(&vport->els_tmofunc);
1394 vport->els_tmofunc.function = lpfc_els_timeout;
1395 vport->els_tmofunc.data = (unsigned long) vport;
James Smart47a86172007-04-25 09:53:22 -04001396
James Smart2e0fef82007-06-17 19:56:36 -05001397 error = scsi_add_host(shost, &phba->pcidev->dev);
1398 if (error)
1399 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04001400
James Smart2e0fef82007-06-17 19:56:36 -05001401 list_add_tail(&vport->listentry, &phba->port_list);
1402 scsi_scan_host(shost);
1403 return vport;
James Smart47a86172007-04-25 09:53:22 -04001404
James Smart2e0fef82007-06-17 19:56:36 -05001405out_put_shost:
1406 scsi_host_put(shost);
1407out:
1408 return NULL;
James Smart47a86172007-04-25 09:53:22 -04001409}
1410
James Smart2e0fef82007-06-17 19:56:36 -05001411void
1412destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04001413{
James Smart2e0fef82007-06-17 19:56:36 -05001414 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}
James Smart47a86172007-04-25 09:53:22 -04001421
James Smart2e0fef82007-06-17 19:56:36 -05001422static 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;
James Smart47a86172007-04-25 09:53:22 -04001427
James Smart2e0fef82007-06-17 19:56:36 -05001428 lpfc_free_sysfs_attr(vport);
James Smart47a86172007-04-25 09:53:22 -04001429
James Smart2e0fef82007-06-17 19:56:36 -05001430 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))
James Smart47a86172007-04-25 09:53:22 -04001446 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 */
James Smart2e0fef82007-06-17 19:56:36 -05001452 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart47a86172007-04-25 09:53:22 -04001453 return;
1454
1455error:
James Smart2e0fef82007-06-17 19:56:36 -05001456 lpfc_remove_device(vport);
James Smart47a86172007-04-25 09:53:22 -04001457}
1458
1459int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
1460{
James Smart2e0fef82007-06-17 19:56:36 -05001461 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1462 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04001463
James Smart2e0fef82007-06-17 19:56:36 -05001464 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);
James Smart47a86172007-04-25 09:53:22 -04001469 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05001470 }
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 }
James Smart47a86172007-04-25 09:53:22 -04001478
James Smart2e0fef82007-06-17 19:56:36 -05001479 if (vport->port_state != LPFC_VPORT_READY)
James Smart47a86172007-04-25 09:53:22 -04001480 return 0;
James Smart2e0fef82007-06-17 19:56:36 -05001481 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart47a86172007-04-25 09:53:22 -04001482 return 0;
James Smart2e0fef82007-06-17 19:56:36 -05001483 if (vport->fc_map_cnt == 0 && time < 2 * HZ)
James Smart47a86172007-04-25 09:53:22 -04001484 return 0;
James Smart2e0fef82007-06-17 19:56:36 -05001485 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart47a86172007-04-25 09:53:22 -04001486 return 0;
1487
1488finished:
James Smart47a86172007-04-25 09:53:22 -04001489 /*
James Smart2e0fef82007-06-17 19:56:36 -05001490 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04001491 */
1492
James Smart2e0fef82007-06-17 19:56:36 -05001493 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);
James Smart47a86172007-04-25 09:53:22 -04001495 fc_host_supported_classes(shost) = FC_COS_CLASS3;
1496
1497 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05001498 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04001499 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) =
James Smart2e0fef82007-06-17 19:56:36 -05001515 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
1516 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04001517
1518 /* This value is also unchanging */
1519 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05001520 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04001521 fc_host_active_fc4s(shost)[2] = 1;
1522 fc_host_active_fc4s(shost)[7] = 1;
1523
1524 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -05001525 vport->fc_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04001526 spin_unlock_irq(shost->host_lock);
1527
1528 return 1;
1529}
dea31012005-04-17 16:05:31 -05001530
1531static int __devinit
1532lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
1533{
James Smart2e0fef82007-06-17 19:56:36 -05001534 struct lpfc_vport *vport = NULL;
1535 struct lpfc_hba *phba;
1536 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05001537 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05001538 struct Scsi_Host *shost = NULL;
dea31012005-04-17 16:05:31 -05001539 unsigned long bar0map_len, bar2map_len;
1540 int error = -ENODEV, retval;
1541 int i;
James Bottomley604a3e32005-10-29 10:28:33 -05001542 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001543
1544 if (pci_enable_device(pdev))
1545 goto out;
1546 if (pci_request_regions(pdev, LPFC_DRIVER_NAME))
1547 goto out_disable_device;
1548
James Smart2e0fef82007-06-17 19:56:36 -05001549 phba = kzalloc(sizeof (struct lpfc_hba), GFP_KERNEL);
1550 if (!phba)
dea31012005-04-17 16:05:31 -05001551 goto out_release_regions;
1552
James Smart2e0fef82007-06-17 19:56:36 -05001553 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001554
dea31012005-04-17 16:05:31 -05001555 phba->pcidev = pdev;
1556
1557 /* Assign an unused board number */
1558 if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
James Smart2e0fef82007-06-17 19:56:36 -05001559 goto out_free_phba;
dea31012005-04-17 16:05:31 -05001560
1561 error = idr_get_new(&lpfc_hba_index, NULL, &phba->brd_no);
1562 if (error)
James Smart2e0fef82007-06-17 19:56:36 -05001563 goto out_free_phba;
dea31012005-04-17 16:05:31 -05001564
James Smart2e0fef82007-06-17 19:56:36 -05001565 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);
dea31012005-04-17 16:05:31 -05001572
1573 /* Initialize timers used by driver */
1574 init_timer(&phba->fc_estabtmo);
1575 phba->fc_estabtmo.function = lpfc_establish_link_tmo;
James Smart2e0fef82007-06-17 19:56:36 -05001576 phba->fc_estabtmo.data = (unsigned long) phba;
dea31012005-04-17 16:05:31 -05001577 psli = &phba->sli;
1578 init_timer(&psli->mbox_tmo);
1579 psli->mbox_tmo.function = lpfc_mbox_timeout;
James Smart2e0fef82007-06-17 19:56:36 -05001580 psli->mbox_tmo.data = (unsigned long) phba;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001581 init_timer(&phba->fcp_poll_timer);
1582 phba->fcp_poll_timer.function = lpfc_poll_timeout;
James Smart2e0fef82007-06-17 19:56:36 -05001583 phba->fcp_poll_timer.data = (unsigned long) phba;
dea31012005-04-17 16:05:31 -05001584
dea31012005-04-17 16:05:31 -05001585 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
Jamie Wellnitz901a9202006-02-28 19:25:19 -05001605 /* Map HBA SLIM to a kernel virtual address. */
dea31012005-04-17 16:05:31 -05001606 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05001607 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. */
dea31012005-04-17 16:05:31 -05001615 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05001616 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 }
dea31012005-04-17 16:05:31 -05001622
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
James.Smart@Emulex.Comf91b3922005-10-28 20:29:28 -04001629 memset(phba->slim2p, 0, SLI2_SLIM_SIZE);
dea31012005-04-17 16:05:31 -05001630
James Smarted957682007-06-17 19:56:37 -05001631 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
dea31012005-04-17 16:05:31 -05001640 /* 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)
James Smarted957682007-06-17 19:56:37 -05001646 goto out_free_hbqslimp;
dea31012005-04-17 16:05:31 -05001647
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));
James Bottomley604a3e32005-10-29 10:28:33 -05001661 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 }
James Smart2e0fef82007-06-17 19:56:36 -05001670
1671 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001672 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
1673 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05001674 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001675 }
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
dea31012005-04-17 16:05:31 -05001695 /* Initialize the list of scsi buffers used by driver for scsi IO. */
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001696 spin_lock_init(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05001697 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
1698
James Smart2e0fef82007-06-17 19:56:36 -05001699 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));
dea31012005-04-17 16:05:31 -05001708
James Smart4ff43242006-12-02 13:34:56 -05001709 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
Thomas Gleixner1d6f3592006-07-01 19:29:42 -07001717 error = request_irq(phba->pcidev->irq, lpfc_intr_handler, IRQF_SHARED,
James Smart2e0fef82007-06-17 19:56:36 -05001718 LPFC_DRIVER_NAME, phba);
dea31012005-04-17 16:05:31 -05001719 if (error) {
1720 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1721 "%d:0451 Enable interrupt handler failed\n",
1722 phba->brd_no);
James Smart2e0fef82007-06-17 19:56:36 -05001723 goto out_destroy_port;
dea31012005-04-17 16:05:31 -05001724 }
dea31012005-04-17 16:05:31 -05001725
James Smart2e0fef82007-06-17 19:56:36 -05001726 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);
James Smart47a86172007-04-25 09:53:22 -04001733 if (error)
dea31012005-04-17 16:05:31 -05001734 goto out_free_irq;
1735
James Smart2e0fef82007-06-17 19:56:36 -05001736 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 }
James Smart8f6d98d2006-08-01 07:34:00 -04001741
dea31012005-04-17 16:05:31 -05001742 return 0;
1743
1744out_free_irq:
1745 lpfc_stop_timer(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001746 phba->pport->work_port_events = 0;
dea31012005-04-17 16:05:31 -05001747 free_irq(phba->pcidev->irq, phba);
James Smart4ff43242006-12-02 13:34:56 -05001748 pci_disable_msi(phba->pcidev);
James Smart2e0fef82007-06-17 19:56:36 -05001749out_destroy_port:
1750 destroy_port(vport);
dea31012005-04-17 16:05:31 -05001751out_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) {
dea31012005-04-17 16:05:31 -05001756 kfree(iocbq_entry);
1757 phba->total_iocbq_bufs--;
dea31012005-04-17 16:05:31 -05001758 }
1759 lpfc_mem_free(phba);
James Smarted957682007-06-17 19:56:37 -05001760out_free_hbqslimp:
1761 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(), phba->hbqslimp.virt,
1762 phba->hbqslimp.phys);
dea31012005-04-17 16:05:31 -05001763out_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);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05001768out_iounmap_slim:
dea31012005-04-17 16:05:31 -05001769 iounmap(phba->slim_memmap_p);
1770out_idr_remove:
1771 idr_remove(&lpfc_hba_index, phba->brd_no);
James Smart2e0fef82007-06-17 19:56:36 -05001772out_free_phba:
1773 kfree(phba);
dea31012005-04-17 16:05:31 -05001774out_release_regions:
1775 pci_release_regions(pdev);
1776out_disable_device:
1777 pci_disable_device(pdev);
1778out:
James Smartdefbcf12006-04-16 22:26:50 -04001779 pci_set_drvdata(pdev, NULL);
dea31012005-04-17 16:05:31 -05001780 return error;
1781}
1782
1783static void __devexit
1784lpfc_pci_remove_one(struct pci_dev *pdev)
1785{
James Smart2e0fef82007-06-17 19:56:36 -05001786 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;
dea31012005-04-17 16:05:31 -05001789
James Smart2e0fef82007-06-17 19:56:36 -05001790 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);
dea31012005-04-17 16:05:31 -05001811
1812 pci_set_drvdata(pdev, NULL);
James Smart2e0fef82007-06-17 19:56:36 -05001813
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
James Smarted957682007-06-17 19:56:37 -05001821 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(), phba->hbqslimp.virt,
1822 phba->hbqslimp.phys);
1823
James Smart2e0fef82007-06-17 19:56:36 -05001824 /* 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);
dea31012005-04-17 16:05:31 -05001838}
1839
Linas Vepstas8d63f372007-02-14 14:28:36 -06001840/**
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
Linas Vepstas5daa49e2007-03-08 16:19:11 -06001856 if (state == pci_channel_io_perm_failure)
Linas Vepstas8d63f372007-02-14 14:28:36 -06001857 return PCI_ERS_RESULT_DISCONNECT;
Linas Vepstas5daa49e2007-03-08 16:19:11 -06001858
Linas Vepstas8d63f372007-02-14 14:28:36 -06001859 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 */
James Smart2e0fef82007-06-17 19:56:36 -05001895 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);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001899 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05001900 spin_unlock_irq(host->host_lock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001901
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
dea31012005-04-17 16:05:31 -05001927static struct pci_device_id lpfc_id_table[] = {
1928 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
1929 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001930 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
1931 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05001932 {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, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001946 {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, },
dea31012005-04-17 16:05:31 -05001952 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
1953 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001954 {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, },
dea31012005-04-17 16:05:31 -05001958 {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, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001964 {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, },
dea31012005-04-17 16:05:31 -05001968 {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, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001978 {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, },
James Smartb87eab32007-04-25 09:53:28 -04001982 {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, },
dea31012005-04-17 16:05:31 -05001994 { 0 }
1995};
1996
1997MODULE_DEVICE_TABLE(pci, lpfc_id_table);
1998
Linas Vepstas8d63f372007-02-14 14:28:36 -06001999static 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
dea31012005-04-17 16:05:31 -05002005static 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),
James Smart2e0fef82007-06-17 19:56:36 -05002010 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -05002011};
2012
2013static int __init
2014lpfc_init(void)
2015{
2016 int error = 0;
2017
2018 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04002019 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -05002020
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);