blob: ecebdfa0047007fd4385427eea0805522101093a [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>
James Smart92d7f7b2007-06-17 19:56:38 -050030#include <linux/ctype.h>
dea31012005-04-17 16:05:31 -050031
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040032#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050033#include <scsi/scsi_device.h>
34#include <scsi/scsi_host.h>
35#include <scsi/scsi_transport_fc.h>
36
37#include "lpfc_hw.h"
38#include "lpfc_sli.h"
39#include "lpfc_disc.h"
40#include "lpfc_scsi.h"
41#include "lpfc.h"
42#include "lpfc_logmsg.h"
43#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050044#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050045#include "lpfc_version.h"
46
Jamie Wellnitz74b72a52006-02-28 22:33:04 -050047static int lpfc_parse_vpd(struct lpfc_hba *, uint8_t *, int);
dea31012005-04-17 16:05:31 -050048static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
49static int lpfc_post_rcv_buf(struct lpfc_hba *);
50
51static struct scsi_transport_template *lpfc_transport_template = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050052static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea31012005-04-17 16:05:31 -050053static DEFINE_IDR(lpfc_hba_index);
54
55/************************************************************************/
56/* */
57/* lpfc_config_port_prep */
58/* This routine will do LPFC initialization prior to the */
59/* CONFIG_PORT mailbox command. This will be initialized */
60/* as a SLI layer callback routine. */
61/* This routine returns 0 on success or -ERESTART if it wants */
62/* the SLI layer to reset the HBA and try again. Any */
63/* other return value indicates an error. */
64/* */
65/************************************************************************/
66int
James Smart2e0fef82007-06-17 19:56:36 -050067lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050068{
69 lpfc_vpd_t *vp = &phba->vpd;
70 int i = 0, rc;
71 LPFC_MBOXQ_t *pmb;
72 MAILBOX_t *mb;
73 char *lpfc_vpd_data = NULL;
74 uint16_t offset = 0;
75 static char licensed[56] =
76 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -040077 static int init_key = 1;
dea31012005-04-17 16:05:31 -050078
79 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
80 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -050081 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -050082 return -ENOMEM;
83 }
84
85 mb = &pmb->mb;
James Smart2e0fef82007-06-17 19:56:36 -050086 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -050087
88 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -040089 if (init_key) {
90 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -050091
James Smart65a29c12006-07-06 15:50:50 -040092 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
93 *ptext = cpu_to_be32(*ptext);
94 init_key = 0;
95 }
dea31012005-04-17 16:05:31 -050096
97 lpfc_read_nv(phba, pmb);
98 memset((char*)mb->un.varRDnvp.rsvd3, 0,
99 sizeof (mb->un.varRDnvp.rsvd3));
100 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
101 sizeof (licensed));
102
103 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
104
105 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500106 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smarte8b62012007-08-02 11:10:09 -0400107 "0324 Config Port initialization "
dea31012005-04-17 16:05:31 -0500108 "error, mbxCmd x%x READ_NVPARM, "
109 "mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500110 mb->mbxCommand, mb->mbxStatus);
111 mempool_free(pmb, phba->mbox_mem_pool);
112 return -ERESTART;
113 }
114 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
James Smart2e0fef82007-06-17 19:56:36 -0500115 sizeof(phba->wwnn));
116 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
117 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500118 }
119
James Smart92d7f7b2007-06-17 19:56:38 -0500120 phba->sli3_options = 0x0;
121
dea31012005-04-17 16:05:31 -0500122 /* Setup and issue mailbox READ REV command */
123 lpfc_read_rev(phba, pmb);
124 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
125 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500126 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400127 "0439 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500128 "READ_REV, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500129 mb->mbxCommand, mb->mbxStatus);
130 mempool_free( pmb, phba->mbox_mem_pool);
131 return -ERESTART;
132 }
133
James Smart92d7f7b2007-06-17 19:56:38 -0500134
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500135 /*
136 * The value of rr must be 1 since the driver set the cv field to 1.
137 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500138 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500139 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500140 vp->rev.rBit = 0;
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500141 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400142 "0440 Adapter failed to init, READ_REV has "
143 "missing revision information.\n");
dea31012005-04-17 16:05:31 -0500144 mempool_free(pmb, phba->mbox_mem_pool);
145 return -ERESTART;
dea31012005-04-17 16:05:31 -0500146 }
147
James Smarted957682007-06-17 19:56:37 -0500148 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp)
149 return -EINVAL;
150
dea31012005-04-17 16:05:31 -0500151 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500152 vp->rev.rBit = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500153 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500154 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
155 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
156 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
157 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500158 vp->rev.biuRev = mb->un.varRdRev.biuRev;
159 vp->rev.smRev = mb->un.varRdRev.smRev;
160 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
161 vp->rev.endecRev = mb->un.varRdRev.endecRev;
162 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
163 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
164 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
165 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
166 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
167 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
168
James Smart92d7f7b2007-06-17 19:56:38 -0500169 /* If the sli feature level is less then 9, we must
170 * tear down all RPIs and VPIs on link down if NPIV
171 * is enabled.
172 */
173 if (vp->rev.feaLevelHigh < 9)
174 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
175
dea31012005-04-17 16:05:31 -0500176 if (lpfc_is_LC_HBA(phba->pcidev->device))
177 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
178 sizeof (phba->RandomData));
179
dea31012005-04-17 16:05:31 -0500180 /* Get adapter VPD information */
181 pmb->context2 = kmalloc(DMP_RSP_SIZE, GFP_KERNEL);
182 if (!pmb->context2)
183 goto out_free_mbox;
184 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
185 if (!lpfc_vpd_data)
186 goto out_free_context2;
187
188 do {
189 lpfc_dump_mem(phba, pmb, offset);
190 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
191
192 if (rc != MBX_SUCCESS) {
193 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400194 "0441 VPD not present on adapter, "
dea31012005-04-17 16:05:31 -0500195 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500196 mb->mbxCommand, mb->mbxStatus);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500197 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500198 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500199 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
200 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
dea31012005-04-17 16:05:31 -0500201 lpfc_sli_pcimem_bcopy(pmb->context2, lpfc_vpd_data + offset,
James Smart92d7f7b2007-06-17 19:56:38 -0500202 mb->un.varDmp.word_cnt);
dea31012005-04-17 16:05:31 -0500203 offset += mb->un.varDmp.word_cnt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500204 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
205 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500206
207 kfree(lpfc_vpd_data);
208out_free_context2:
209 kfree(pmb->context2);
210out_free_mbox:
211 mempool_free(pmb, phba->mbox_mem_pool);
212 return 0;
213}
214
215/************************************************************************/
216/* */
217/* lpfc_config_port_post */
218/* This routine will do LPFC initialization after the */
219/* CONFIG_PORT mailbox command. This will be initialized */
220/* as a SLI layer callback routine. */
221/* This routine returns 0 on success. Any other return value */
222/* indicates an error. */
223/* */
224/************************************************************************/
225int
James Smart2e0fef82007-06-17 19:56:36 -0500226lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500227{
James Smart2e0fef82007-06-17 19:56:36 -0500228 struct lpfc_vport *vport = phba->pport;
dea31012005-04-17 16:05:31 -0500229 LPFC_MBOXQ_t *pmb;
230 MAILBOX_t *mb;
231 struct lpfc_dmabuf *mp;
232 struct lpfc_sli *psli = &phba->sli;
233 uint32_t status, timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500234 int i, j;
235 int rc;
dea31012005-04-17 16:05:31 -0500236
237 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
238 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500239 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500240 return -ENOMEM;
241 }
242 mb = &pmb->mb;
243
dea31012005-04-17 16:05:31 -0500244 /* Get login parameters for NID. */
James Smart92d7f7b2007-06-17 19:56:38 -0500245 lpfc_read_sparam(phba, pmb, 0);
James Smarted957682007-06-17 19:56:37 -0500246 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500247 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500248 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400249 "0448 Adapter failed init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500250 "READ_SPARM mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500251 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500252 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500253 mp = (struct lpfc_dmabuf *) pmb->context1;
254 mempool_free( pmb, phba->mbox_mem_pool);
255 lpfc_mbuf_free(phba, mp->virt, mp->phys);
256 kfree(mp);
257 return -EIO;
258 }
259
260 mp = (struct lpfc_dmabuf *) pmb->context1;
261
James Smart2e0fef82007-06-17 19:56:36 -0500262 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500263 lpfc_mbuf_free(phba, mp->virt, mp->phys);
264 kfree(mp);
265 pmb->context1 = NULL;
266
James Smarta12e07b2006-12-02 13:35:30 -0500267 if (phba->cfg_soft_wwnn)
James Smart2e0fef82007-06-17 19:56:36 -0500268 u64_to_wwn(phba->cfg_soft_wwnn,
269 vport->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -0400270 if (phba->cfg_soft_wwpn)
James Smart2e0fef82007-06-17 19:56:36 -0500271 u64_to_wwn(phba->cfg_soft_wwpn,
272 vport->fc_sparam.portName.u.wwn);
273 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
dea31012005-04-17 16:05:31 -0500274 sizeof (struct lpfc_name));
James Smart2e0fef82007-06-17 19:56:36 -0500275 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
dea31012005-04-17 16:05:31 -0500276 sizeof (struct lpfc_name));
277 /* If no serial number in VPD data, use low 6 bytes of WWNN */
278 /* This should be consolidated into parse_vpd ? - mr */
279 if (phba->SerialNumber[0] == 0) {
280 uint8_t *outptr;
281
James Smart2e0fef82007-06-17 19:56:36 -0500282 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500283 for (i = 0; i < 12; i++) {
284 status = *outptr++;
285 j = ((status & 0xf0) >> 4);
286 if (j <= 9)
287 phba->SerialNumber[i] =
288 (char)((uint8_t) 0x30 + (uint8_t) j);
289 else
290 phba->SerialNumber[i] =
291 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
292 i++;
293 j = (status & 0xf);
294 if (j <= 9)
295 phba->SerialNumber[i] =
296 (char)((uint8_t) 0x30 + (uint8_t) j);
297 else
298 phba->SerialNumber[i] =
299 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
300 }
301 }
302
dea31012005-04-17 16:05:31 -0500303 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500304 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500305 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500306 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400307 "0453 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500308 "READ_CONFIG, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500309 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,
James Smarte8b62012007-08-02 11:10:09 -0400338 "1302 Invalid speed for this board: "
dea31012005-04-17 16:05:31 -0500339 "Reset link speed to auto: x%x\n",
dea31012005-04-17 16:05:31 -0500340 phba->cfg_link_speed);
341 phba->cfg_link_speed = LINK_SPEED_AUTO;
342 }
343
James Smart2e0fef82007-06-17 19:56:36 -0500344 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500345
346 /* Only process IOCBs on ring 0 till hba_state is READY */
James Smarta4bc3372006-12-02 13:34:16 -0500347 if (psli->ring[psli->extra_ring].cmdringaddr)
348 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500349 if (psli->ring[psli->fcp_ring].cmdringaddr)
350 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
351 if (psli->ring[psli->next_ring].cmdringaddr)
352 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
353
354 /* Post receive buffers for desired rings */
James Smarted957682007-06-17 19:56:37 -0500355 if (phba->sli_rev != 3)
356 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500357
358 /* Enable appropriate host interrupts */
James Smart2e0fef82007-06-17 19:56:36 -0500359 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500360 status = readl(phba->HCregaddr);
361 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
362 if (psli->num_rings > 0)
363 status |= HC_R0INT_ENA;
364 if (psli->num_rings > 1)
365 status |= HC_R1INT_ENA;
366 if (psli->num_rings > 2)
367 status |= HC_R2INT_ENA;
368 if (psli->num_rings > 3)
369 status |= HC_R3INT_ENA;
370
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500371 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
372 (phba->cfg_poll & DISABLE_FCP_RING_INT))
373 status &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
374
dea31012005-04-17 16:05:31 -0500375 writel(status, phba->HCregaddr);
376 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500377 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500378
379 /*
380 * Setup the ring 0 (els) timeout handler
381 */
382 timeout = phba->fc_ratov << 1;
James Smart2e0fef82007-06-17 19:56:36 -0500383 mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
James Smart858c9f62007-06-17 19:56:39 -0500384 mod_timer(&phba->hb_tmofunc, jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
385 phba->hb_outstanding = 0;
386 phba->last_completion_time = jiffies;
dea31012005-04-17 16:05:31 -0500387
388 lpfc_init_link(phba, pmb, phba->cfg_topology, phba->cfg_link_speed);
389 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500390 pmb->vport = vport;
James Smart8aee9182006-08-31 12:27:57 -0400391 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -0400392 lpfc_set_loopback_flag(phba);
James Smart8aee9182006-08-31 12:27:57 -0400393 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500394 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400395 "0454 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500396 "INIT_LINK, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500397 mb->mbxCommand, mb->mbxStatus);
398
399 /* Clear all interrupt enable conditions */
400 writel(0, phba->HCregaddr);
401 readl(phba->HCregaddr); /* flush */
402 /* Clear all pending interrupts */
403 writel(0xffffffff, phba->HAregaddr);
404 readl(phba->HAregaddr); /* flush */
405
James Smart2e0fef82007-06-17 19:56:36 -0500406 phba->link_state = LPFC_HBA_ERROR;
James Smart8aee9182006-08-31 12:27:57 -0400407 if (rc != MBX_BUSY)
408 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500409 return -EIO;
410 }
411 /* MBOX buffer will be freed in mbox compl */
412
James Smartce8b3ce2006-07-06 15:50:36 -0400413 return (0);
414}
415
dea31012005-04-17 16:05:31 -0500416/************************************************************************/
417/* */
418/* lpfc_hba_down_prep */
419/* This routine will do LPFC uninitialization before the */
420/* HBA is reset when bringing down the SLI Layer. This will be */
421/* initialized as a SLI layer callback routine. */
422/* This routine returns 0 on success. Any other return value */
423/* indicates an error. */
424/* */
425/************************************************************************/
426int
James Smart2e0fef82007-06-17 19:56:36 -0500427lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500428{
429 /* Disable interrupts */
430 writel(0, phba->HCregaddr);
431 readl(phba->HCregaddr); /* flush */
432
James Smart51ef4c22007-08-02 11:10:31 -0400433 lpfc_cleanup_discovery_resources(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -0500434 return 0;
dea31012005-04-17 16:05:31 -0500435}
436
437/************************************************************************/
438/* */
Jamie Wellnitz41415862006-02-28 19:25:27 -0500439/* lpfc_hba_down_post */
440/* This routine will do uninitialization after the HBA is reset */
441/* when bringing down the SLI Layer. */
442/* This routine returns 0 on success. Any other return value */
443/* indicates an error. */
444/* */
445/************************************************************************/
446int
James Smart2e0fef82007-06-17 19:56:36 -0500447lpfc_hba_down_post(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -0500448{
449 struct lpfc_sli *psli = &phba->sli;
450 struct lpfc_sli_ring *pring;
451 struct lpfc_dmabuf *mp, *next_mp;
452 int i;
453
James Smart92d7f7b2007-06-17 19:56:38 -0500454 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
455 lpfc_sli_hbqbuf_free_all(phba);
456 else {
457 /* Cleanup preposted buffers on the ELS ring */
458 pring = &psli->ring[LPFC_ELS_RING];
459 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
460 list_del(&mp->list);
461 pring->postbufq_cnt--;
462 lpfc_mbuf_free(phba, mp->virt, mp->phys);
463 kfree(mp);
464 }
Jamie Wellnitz41415862006-02-28 19:25:27 -0500465 }
466
467 for (i = 0; i < psli->num_rings; i++) {
468 pring = &psli->ring[i];
469 lpfc_sli_abort_iocb_ring(phba, pring);
470 }
471
472 return 0;
473}
474
James Smart858c9f62007-06-17 19:56:39 -0500475/* HBA heart beat timeout handler */
476void
477lpfc_hb_timeout(unsigned long ptr)
478{
479 struct lpfc_hba *phba;
480 unsigned long iflag;
481
482 phba = (struct lpfc_hba *)ptr;
483 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
484 if (!(phba->pport->work_port_events & WORKER_HB_TMO))
485 phba->pport->work_port_events |= WORKER_HB_TMO;
486 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
487
488 if (phba->work_wait)
489 wake_up(phba->work_wait);
490 return;
491}
492
493static void
494lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
495{
496 unsigned long drvr_flag;
497
498 spin_lock_irqsave(&phba->hbalock, drvr_flag);
499 phba->hb_outstanding = 0;
500 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
501
502 mempool_free(pmboxq, phba->mbox_mem_pool);
503 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
504 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -0400505 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -0500506 mod_timer(&phba->hb_tmofunc,
507 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
508 return;
509}
510
511void
512lpfc_hb_timeout_handler(struct lpfc_hba *phba)
513{
514 LPFC_MBOXQ_t *pmboxq;
515 int retval;
516 struct lpfc_sli *psli = &phba->sli;
517
518 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -0400519 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -0500520 (phba->pport->fc_flag & FC_OFFLINE_MODE))
521 return;
522
523 spin_lock_irq(&phba->pport->work_port_lock);
524 /* If the timer is already canceled do nothing */
525 if (!(phba->pport->work_port_events & WORKER_HB_TMO)) {
526 spin_unlock_irq(&phba->pport->work_port_lock);
527 return;
528 }
529
530 if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
531 jiffies)) {
532 spin_unlock_irq(&phba->pport->work_port_lock);
533 if (!phba->hb_outstanding)
534 mod_timer(&phba->hb_tmofunc,
535 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
536 else
537 mod_timer(&phba->hb_tmofunc,
538 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
539 return;
540 }
541 spin_unlock_irq(&phba->pport->work_port_lock);
542
543 /* If there is no heart beat outstanding, issue a heartbeat command */
544 if (!phba->hb_outstanding) {
545 pmboxq = mempool_alloc(phba->mbox_mem_pool,GFP_KERNEL);
546 if (!pmboxq) {
547 mod_timer(&phba->hb_tmofunc,
548 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
549 return;
550 }
551
552 lpfc_heart_beat(phba, pmboxq);
553 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
554 pmboxq->vport = phba->pport;
555 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
556
557 if (retval != MBX_BUSY && retval != MBX_SUCCESS) {
558 mempool_free(pmboxq, phba->mbox_mem_pool);
559 mod_timer(&phba->hb_tmofunc,
560 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
561 return;
562 }
563 mod_timer(&phba->hb_tmofunc,
564 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
565 phba->hb_outstanding = 1;
566 return;
567 } else {
568 /*
569 * If heart beat timeout called with hb_outstanding set we
570 * need to take the HBA offline.
571 */
572 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400573 "0459 Adapter heartbeat failure, taking "
574 "this port offline.\n");
James Smart858c9f62007-06-17 19:56:39 -0500575
576 spin_lock_irq(&phba->hbalock);
577 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
578 spin_unlock_irq(&phba->hbalock);
579
580 lpfc_offline_prep(phba);
581 lpfc_offline(phba);
582 lpfc_unblock_mgmt_io(phba);
583 phba->link_state = LPFC_HBA_ERROR;
584 lpfc_hba_down_post(phba);
585 }
586}
587
Jamie Wellnitz41415862006-02-28 19:25:27 -0500588/************************************************************************/
589/* */
dea31012005-04-17 16:05:31 -0500590/* lpfc_handle_eratt */
591/* This routine will handle processing a Host Attention */
592/* Error Status event. This will be initialized */
593/* as a SLI layer callback routine. */
594/* */
595/************************************************************************/
596void
James Smart2e0fef82007-06-17 19:56:36 -0500597lpfc_handle_eratt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500598{
James Smart2e0fef82007-06-17 19:56:36 -0500599 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -0500600 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -0500601 struct lpfc_sli_ring *pring;
James Smart549e55c2007-08-02 11:09:51 -0400602 struct lpfc_vport **vports;
James Smartd2873e42006-08-18 17:46:43 -0400603 uint32_t event_data;
James Smart92d7f7b2007-06-17 19:56:38 -0500604 struct Scsi_Host *shost;
James Smart549e55c2007-08-02 11:09:51 -0400605 int i;
James Smart2e0fef82007-06-17 19:56:36 -0500606
Linas Vepstas8d63f372007-02-14 14:28:36 -0600607 /* If the pci channel is offline, ignore possible errors,
608 * since we cannot communicate with the pci card anyway. */
609 if (pci_channel_offline(phba->pcidev))
610 return;
dea31012005-04-17 16:05:31 -0500611
James Smartf5603512006-12-02 13:35:43 -0500612 if (phba->work_hs & HS_FFER6 ||
613 phba->work_hs & HS_FFER5) {
dea31012005-04-17 16:05:31 -0500614 /* Re-establishing Link */
615 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -0400616 "1301 Re-establishing Link "
dea31012005-04-17 16:05:31 -0500617 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400618 phba->work_hs,
dea31012005-04-17 16:05:31 -0500619 phba->work_status[0], phba->work_status[1]);
James Smart549e55c2007-08-02 11:09:51 -0400620 vports = lpfc_create_vport_work_array(phba);
621 if (vports != NULL)
622 for(i = 0;
623 i < LPFC_MAX_VPORTS && vports[i] != NULL;
624 i++){
625 shost = lpfc_shost_from_vport(vports[i]);
626 spin_lock_irq(shost->host_lock);
627 vports[i]->fc_flag |= FC_ESTABLISH_LINK;
628 spin_unlock_irq(shost->host_lock);
629 }
630 lpfc_destroy_vport_work_array(vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500631 spin_lock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -0500632 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -0500633 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500634
635 /*
636 * Firmware stops when it triggled erratt with HS_FFER6.
637 * That could cause the I/Os dropped by the firmware.
638 * Error iocb (I/O) on txcmplq and let the SCSI layer
639 * retry it after re-establishing link.
640 */
641 pring = &psli->ring[psli->fcp_ring];
642 lpfc_sli_abort_iocb_ring(phba, pring);
643
644
645 /*
646 * There was a firmware error. Take the hba offline and then
647 * attempt to restart it.
648 */
James Smart46fa3112007-04-25 09:51:45 -0400649 lpfc_offline_prep(phba);
dea31012005-04-17 16:05:31 -0500650 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500651 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -0500652 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
653 mod_timer(&phba->fc_estabtmo, jiffies + HZ * 60);
James Smart46fa3112007-04-25 09:51:45 -0400654 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -0500655 return;
656 }
James Smart46fa3112007-04-25 09:51:45 -0400657 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -0500658 } else {
659 /* The if clause above forces this code path when the status
660 * failure is a value other than FFER6. Do not call the offline
661 * twice. This is the adapter hardware error path.
662 */
663 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400664 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -0500665 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400666 phba->work_hs,
dea31012005-04-17 16:05:31 -0500667 phba->work_status[0], phba->work_status[1]);
668
James Smartd2873e42006-08-18 17:46:43 -0400669 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -0500670 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -0500671 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -0400672 sizeof(event_data), (char *) &event_data,
673 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
674
James Smart92d7f7b2007-06-17 19:56:38 -0500675 spin_lock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -0500676 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -0500677 spin_unlock_irq(&phba->hbalock);
James Smart46fa3112007-04-25 09:51:45 -0400678 lpfc_offline_prep(phba);
dea31012005-04-17 16:05:31 -0500679 lpfc_offline(phba);
James Smart46fa3112007-04-25 09:51:45 -0400680 lpfc_unblock_mgmt_io(phba);
James Smart2e0fef82007-06-17 19:56:36 -0500681 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -0500682 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -0500683 }
684}
685
686/************************************************************************/
687/* */
688/* lpfc_handle_latt */
689/* This routine will handle processing a Host Attention */
690/* Link Status event. This will be initialized */
691/* as a SLI layer callback routine. */
692/* */
693/************************************************************************/
694void
James Smart2e0fef82007-06-17 19:56:36 -0500695lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500696{
James Smart2e0fef82007-06-17 19:56:36 -0500697 struct lpfc_vport *vport = phba->pport;
698 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -0500699 LPFC_MBOXQ_t *pmb;
700 volatile uint32_t control;
701 struct lpfc_dmabuf *mp;
702 int rc = -ENOMEM;
703
704 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
705 if (!pmb)
706 goto lpfc_handle_latt_err_exit;
707
708 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
709 if (!mp)
710 goto lpfc_handle_latt_free_pmb;
711
712 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
713 if (!mp->virt)
714 goto lpfc_handle_latt_free_mp;
715
716 rc = -EIO;
717
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -0500718 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -0400719 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -0500720
721 psli->slistat.link_event++;
722 lpfc_read_la(phba, pmb, mp);
723 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_la;
James Smart2e0fef82007-06-17 19:56:36 -0500724 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500725 rc = lpfc_sli_issue_mbox (phba, pmb, (MBX_NOWAIT | MBX_STOP_IOCB));
726 if (rc == MBX_NOT_FINISHED)
James Smart14691152006-12-02 13:34:28 -0500727 goto lpfc_handle_latt_free_mbuf;
dea31012005-04-17 16:05:31 -0500728
729 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -0500730 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500731 writel(HA_LATT, phba->HAregaddr);
732 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500733 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500734
735 return;
736
James Smart14691152006-12-02 13:34:28 -0500737lpfc_handle_latt_free_mbuf:
738 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -0500739lpfc_handle_latt_free_mp:
740 kfree(mp);
741lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -0400742 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500743lpfc_handle_latt_err_exit:
744 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -0500745 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500746 psli->sli_flag |= LPFC_PROCESS_LA;
747 control = readl(phba->HCregaddr);
748 control |= HC_LAINT_ENA;
749 writel(control, phba->HCregaddr);
750 readl(phba->HCregaddr); /* flush */
751
752 /* Clear Link Attention in HA REG */
753 writel(HA_LATT, phba->HAregaddr);
754 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500755 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500756 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -0500757 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500758
759 /* The other case is an error from issue_mbox */
760 if (rc == -ENOMEM)
James Smarted957682007-06-17 19:56:37 -0500761 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
James Smarte8b62012007-08-02 11:10:09 -0400762 "0300 READ_LA: no buffers\n");
dea31012005-04-17 16:05:31 -0500763
764 return;
765}
766
767/************************************************************************/
768/* */
769/* lpfc_parse_vpd */
770/* This routine will parse the VPD data */
771/* */
772/************************************************************************/
773static int
James Smart2e0fef82007-06-17 19:56:36 -0500774lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -0500775{
776 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -0500777 int Length;
dea31012005-04-17 16:05:31 -0500778 int i, j;
779 int finished = 0;
780 int index = 0;
781
782 if (!vpd)
783 return 0;
784
785 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -0500786 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400787 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -0500788 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
789 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500790 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -0500791 switch (vpd[index]) {
792 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500793 case 0x91:
dea31012005-04-17 16:05:31 -0500794 index += 1;
795 lenlo = vpd[index];
796 index += 1;
797 lenhi = vpd[index];
798 index += 1;
799 i = ((((unsigned short)lenhi) << 8) + lenlo);
800 index += i;
801 break;
802 case 0x90:
803 index += 1;
804 lenlo = vpd[index];
805 index += 1;
806 lenhi = vpd[index];
807 index += 1;
808 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500809 if (Length > len - index)
810 Length = len - index;
dea31012005-04-17 16:05:31 -0500811 while (Length > 0) {
812 /* Look for Serial Number */
813 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
814 index += 2;
815 i = vpd[index];
816 index += 1;
817 j = 0;
818 Length -= (3+i);
819 while(i--) {
820 phba->SerialNumber[j++] = vpd[index++];
821 if (j == 31)
822 break;
823 }
824 phba->SerialNumber[j] = 0;
825 continue;
826 }
827 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
828 phba->vpd_flag |= VPD_MODEL_DESC;
829 index += 2;
830 i = vpd[index];
831 index += 1;
832 j = 0;
833 Length -= (3+i);
834 while(i--) {
835 phba->ModelDesc[j++] = vpd[index++];
836 if (j == 255)
837 break;
838 }
839 phba->ModelDesc[j] = 0;
840 continue;
841 }
842 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
843 phba->vpd_flag |= VPD_MODEL_NAME;
844 index += 2;
845 i = vpd[index];
846 index += 1;
847 j = 0;
848 Length -= (3+i);
849 while(i--) {
850 phba->ModelName[j++] = vpd[index++];
851 if (j == 79)
852 break;
853 }
854 phba->ModelName[j] = 0;
855 continue;
856 }
857 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
858 phba->vpd_flag |= VPD_PROGRAM_TYPE;
859 index += 2;
860 i = vpd[index];
861 index += 1;
862 j = 0;
863 Length -= (3+i);
864 while(i--) {
865 phba->ProgramType[j++] = vpd[index++];
866 if (j == 255)
867 break;
868 }
869 phba->ProgramType[j] = 0;
870 continue;
871 }
872 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
873 phba->vpd_flag |= VPD_PORT;
874 index += 2;
875 i = vpd[index];
876 index += 1;
877 j = 0;
878 Length -= (3+i);
879 while(i--) {
880 phba->Port[j++] = vpd[index++];
881 if (j == 19)
882 break;
883 }
884 phba->Port[j] = 0;
885 continue;
886 }
887 else {
888 index += 2;
889 i = vpd[index];
890 index += 1;
891 index += i;
892 Length -= (3 + i);
893 }
894 }
895 finished = 0;
896 break;
897 case 0x78:
898 finished = 1;
899 break;
900 default:
901 index ++;
902 break;
903 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500904 }
dea31012005-04-17 16:05:31 -0500905
906 return(1);
907}
908
909static void
James Smart2e0fef82007-06-17 19:56:36 -0500910lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -0500911{
912 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -0500913 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500914 int max_speed;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500915 struct {
916 char * name;
917 int max_speed;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500918 char * bus;
James Smart18a3b592006-12-02 13:35:08 -0500919 } m = {"<Unknown>", 0, ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500920
921 if (mdp && mdp[0] != '\0'
922 && descp && descp[0] != '\0')
923 return;
924
925 if (phba->lmt & LMT_10Gb)
926 max_speed = 10;
927 else if (phba->lmt & LMT_8Gb)
928 max_speed = 8;
929 else if (phba->lmt & LMT_4Gb)
930 max_speed = 4;
931 else if (phba->lmt & LMT_2Gb)
932 max_speed = 2;
933 else
934 max_speed = 1;
dea31012005-04-17 16:05:31 -0500935
936 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -0500937
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -0500938 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -0400939 case PCI_DEVICE_ID_FIREFLY:
James Smart18a3b592006-12-02 13:35:08 -0500940 m = (typeof(m)){"LP6000", max_speed, "PCI"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -0400941 break;
dea31012005-04-17 16:05:31 -0500942 case PCI_DEVICE_ID_SUPERFLY:
943 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smart18a3b592006-12-02 13:35:08 -0500944 m = (typeof(m)){"LP7000", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -0500945 else
James Smart18a3b592006-12-02 13:35:08 -0500946 m = (typeof(m)){"LP7000E", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -0500947 break;
948 case PCI_DEVICE_ID_DRAGONFLY:
James Smart18a3b592006-12-02 13:35:08 -0500949 m = (typeof(m)){"LP8000", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -0500950 break;
951 case PCI_DEVICE_ID_CENTAUR:
952 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smart18a3b592006-12-02 13:35:08 -0500953 m = (typeof(m)){"LP9002", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -0500954 else
James Smart18a3b592006-12-02 13:35:08 -0500955 m = (typeof(m)){"LP9000", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -0500956 break;
957 case PCI_DEVICE_ID_RFLY:
James Smart18a3b592006-12-02 13:35:08 -0500958 m = (typeof(m)){"LP952", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -0500959 break;
960 case PCI_DEVICE_ID_PEGASUS:
James Smart18a3b592006-12-02 13:35:08 -0500961 m = (typeof(m)){"LP9802", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -0500962 break;
963 case PCI_DEVICE_ID_THOR:
James Smart18a3b592006-12-02 13:35:08 -0500964 m = (typeof(m)){"LP10000", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -0500965 break;
966 case PCI_DEVICE_ID_VIPER:
James Smart18a3b592006-12-02 13:35:08 -0500967 m = (typeof(m)){"LPX1000", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -0500968 break;
969 case PCI_DEVICE_ID_PFLY:
James Smart18a3b592006-12-02 13:35:08 -0500970 m = (typeof(m)){"LP982", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -0500971 break;
972 case PCI_DEVICE_ID_TFLY:
James Smart18a3b592006-12-02 13:35:08 -0500973 m = (typeof(m)){"LP1050", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -0500974 break;
975 case PCI_DEVICE_ID_HELIOS:
James Smart18a3b592006-12-02 13:35:08 -0500976 m = (typeof(m)){"LP11000", max_speed, "PCI-X2"};
dea31012005-04-17 16:05:31 -0500977 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -0500978 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smart18a3b592006-12-02 13:35:08 -0500979 m = (typeof(m)){"LP11000-SP", max_speed, "PCI-X2"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -0500980 break;
981 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smart18a3b592006-12-02 13:35:08 -0500982 m = (typeof(m)){"LP11002-SP", max_speed, "PCI-X2"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -0500983 break;
984 case PCI_DEVICE_ID_NEPTUNE:
James Smart18a3b592006-12-02 13:35:08 -0500985 m = (typeof(m)){"LPe1000", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -0500986 break;
987 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smart18a3b592006-12-02 13:35:08 -0500988 m = (typeof(m)){"LPe1000-SP", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -0500989 break;
990 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smart18a3b592006-12-02 13:35:08 -0500991 m = (typeof(m)){"LPe1002-SP", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -0500992 break;
dea31012005-04-17 16:05:31 -0500993 case PCI_DEVICE_ID_BMID:
James Smart18a3b592006-12-02 13:35:08 -0500994 m = (typeof(m)){"LP1150", max_speed, "PCI-X2"};
dea31012005-04-17 16:05:31 -0500995 break;
996 case PCI_DEVICE_ID_BSMB:
James Smart18a3b592006-12-02 13:35:08 -0500997 m = (typeof(m)){"LP111", max_speed, "PCI-X2"};
dea31012005-04-17 16:05:31 -0500998 break;
999 case PCI_DEVICE_ID_ZEPHYR:
James Smart18a3b592006-12-02 13:35:08 -05001000 m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
dea31012005-04-17 16:05:31 -05001001 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001002 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smart18a3b592006-12-02 13:35:08 -05001003 m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001004 break;
1005 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smart18a3b592006-12-02 13:35:08 -05001006 m = (typeof(m)){"LPe11002-SP", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001007 break;
dea31012005-04-17 16:05:31 -05001008 case PCI_DEVICE_ID_ZMID:
James Smart18a3b592006-12-02 13:35:08 -05001009 m = (typeof(m)){"LPe1150", max_speed, "PCIe"};
dea31012005-04-17 16:05:31 -05001010 break;
1011 case PCI_DEVICE_ID_ZSMB:
James Smart18a3b592006-12-02 13:35:08 -05001012 m = (typeof(m)){"LPe111", max_speed, "PCIe"};
dea31012005-04-17 16:05:31 -05001013 break;
1014 case PCI_DEVICE_ID_LP101:
James Smart18a3b592006-12-02 13:35:08 -05001015 m = (typeof(m)){"LP101", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001016 break;
1017 case PCI_DEVICE_ID_LP10000S:
James Smart18a3b592006-12-02 13:35:08 -05001018 m = (typeof(m)){"LP10000-S", max_speed, "PCI"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001019 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001020 case PCI_DEVICE_ID_LP11000S:
James Smart18a3b592006-12-02 13:35:08 -05001021 m = (typeof(m)){"LP11000-S", max_speed,
1022 "PCI-X2"};
1023 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001024 case PCI_DEVICE_ID_LPE11000S:
James Smart18a3b592006-12-02 13:35:08 -05001025 m = (typeof(m)){"LPe11000-S", max_speed,
1026 "PCIe"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05001027 break;
James Smartb87eab32007-04-25 09:53:28 -04001028 case PCI_DEVICE_ID_SAT:
1029 m = (typeof(m)){"LPe12000", max_speed, "PCIe"};
1030 break;
1031 case PCI_DEVICE_ID_SAT_MID:
1032 m = (typeof(m)){"LPe1250", max_speed, "PCIe"};
1033 break;
1034 case PCI_DEVICE_ID_SAT_SMB:
1035 m = (typeof(m)){"LPe121", max_speed, "PCIe"};
1036 break;
1037 case PCI_DEVICE_ID_SAT_DCSP:
1038 m = (typeof(m)){"LPe12002-SP", max_speed, "PCIe"};
1039 break;
1040 case PCI_DEVICE_ID_SAT_SCSP:
1041 m = (typeof(m)){"LPe12000-SP", max_speed, "PCIe"};
1042 break;
1043 case PCI_DEVICE_ID_SAT_S:
1044 m = (typeof(m)){"LPe12000-S", max_speed, "PCIe"};
1045 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05001046 default:
Randy Dunlap041976f2006-06-25 01:58:51 -07001047 m = (typeof(m)){ NULL };
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001048 break;
dea31012005-04-17 16:05:31 -05001049 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001050
1051 if (mdp && mdp[0] == '\0')
1052 snprintf(mdp, 79,"%s", m.name);
1053 if (descp && descp[0] == '\0')
1054 snprintf(descp, 255,
James Smart18a3b592006-12-02 13:35:08 -05001055 "Emulex %s %dGb %s Fibre Channel Adapter",
1056 m.name, m.max_speed, m.bus);
dea31012005-04-17 16:05:31 -05001057}
1058
1059/**************************************************/
1060/* lpfc_post_buffer */
1061/* */
1062/* This routine will post count buffers to the */
1063/* ring with the QUE_RING_BUF_CN command. This */
1064/* allows 3 buffers / command to be posted. */
1065/* Returns the number of buffers NOT posted. */
1066/**************************************************/
1067int
James Smart2e0fef82007-06-17 19:56:36 -05001068lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt,
dea31012005-04-17 16:05:31 -05001069 int type)
1070{
1071 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001072 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05001073 struct lpfc_dmabuf *mp1, *mp2;
1074
1075 cnt += pring->missbufcnt;
1076
1077 /* While there are buffers to post */
1078 while (cnt > 0) {
1079 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001080 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05001081 if (iocb == NULL) {
1082 pring->missbufcnt = cnt;
1083 return cnt;
1084 }
dea31012005-04-17 16:05:31 -05001085 icmd = &iocb->iocb;
1086
1087 /* 2 buffers can be posted per command */
1088 /* Allocate buffer to post */
1089 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1090 if (mp1)
1091 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
1092 &mp1->phys);
1093 if (mp1 == 0 || mp1->virt == 0) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001094 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05001095 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001096 pring->missbufcnt = cnt;
1097 return cnt;
1098 }
1099
1100 INIT_LIST_HEAD(&mp1->list);
1101 /* Allocate buffer to post */
1102 if (cnt > 1) {
1103 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1104 if (mp2)
1105 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
1106 &mp2->phys);
1107 if (mp2 == 0 || mp2->virt == 0) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001108 kfree(mp2);
dea31012005-04-17 16:05:31 -05001109 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1110 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05001111 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001112 pring->missbufcnt = cnt;
1113 return cnt;
1114 }
1115
1116 INIT_LIST_HEAD(&mp2->list);
1117 } else {
1118 mp2 = NULL;
1119 }
1120
1121 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
1122 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
1123 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
1124 icmd->ulpBdeCount = 1;
1125 cnt--;
1126 if (mp2) {
1127 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
1128 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
1129 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
1130 cnt--;
1131 icmd->ulpBdeCount = 2;
1132 }
1133
1134 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
1135 icmd->ulpLe = 1;
1136
dea31012005-04-17 16:05:31 -05001137 if (lpfc_sli_issue_iocb(phba, pring, iocb, 0) == IOCB_ERROR) {
1138 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1139 kfree(mp1);
1140 cnt++;
1141 if (mp2) {
1142 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
1143 kfree(mp2);
1144 cnt++;
1145 }
James Bottomley604a3e32005-10-29 10:28:33 -05001146 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001147 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05001148 return cnt;
1149 }
dea31012005-04-17 16:05:31 -05001150 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05001151 if (mp2)
dea31012005-04-17 16:05:31 -05001152 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05001153 }
1154 pring->missbufcnt = 0;
1155 return 0;
1156}
1157
1158/************************************************************************/
1159/* */
1160/* lpfc_post_rcv_buf */
1161/* This routine post initial rcv buffers to the configured rings */
1162/* */
1163/************************************************************************/
1164static int
James Smart2e0fef82007-06-17 19:56:36 -05001165lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001166{
1167 struct lpfc_sli *psli = &phba->sli;
1168
1169 /* Ring 0, ELS / CT buffers */
1170 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0, 1);
1171 /* Ring 2 - FCP no buffers needed */
1172
1173 return 0;
1174}
1175
1176#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
1177
1178/************************************************************************/
1179/* */
1180/* lpfc_sha_init */
1181/* */
1182/************************************************************************/
1183static void
1184lpfc_sha_init(uint32_t * HashResultPointer)
1185{
1186 HashResultPointer[0] = 0x67452301;
1187 HashResultPointer[1] = 0xEFCDAB89;
1188 HashResultPointer[2] = 0x98BADCFE;
1189 HashResultPointer[3] = 0x10325476;
1190 HashResultPointer[4] = 0xC3D2E1F0;
1191}
1192
1193/************************************************************************/
1194/* */
1195/* lpfc_sha_iterate */
1196/* */
1197/************************************************************************/
1198static void
1199lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
1200{
1201 int t;
1202 uint32_t TEMP;
1203 uint32_t A, B, C, D, E;
1204 t = 16;
1205 do {
1206 HashWorkingPointer[t] =
1207 S(1,
1208 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
1209 8] ^
1210 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
1211 } while (++t <= 79);
1212 t = 0;
1213 A = HashResultPointer[0];
1214 B = HashResultPointer[1];
1215 C = HashResultPointer[2];
1216 D = HashResultPointer[3];
1217 E = HashResultPointer[4];
1218
1219 do {
1220 if (t < 20) {
1221 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
1222 } else if (t < 40) {
1223 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
1224 } else if (t < 60) {
1225 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
1226 } else {
1227 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
1228 }
1229 TEMP += S(5, A) + E + HashWorkingPointer[t];
1230 E = D;
1231 D = C;
1232 C = S(30, B);
1233 B = A;
1234 A = TEMP;
1235 } while (++t <= 79);
1236
1237 HashResultPointer[0] += A;
1238 HashResultPointer[1] += B;
1239 HashResultPointer[2] += C;
1240 HashResultPointer[3] += D;
1241 HashResultPointer[4] += E;
1242
1243}
1244
1245/************************************************************************/
1246/* */
1247/* lpfc_challenge_key */
1248/* */
1249/************************************************************************/
1250static void
1251lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
1252{
1253 *HashWorking = (*RandomChallenge ^ *HashWorking);
1254}
1255
1256/************************************************************************/
1257/* */
1258/* lpfc_hba_init */
1259/* */
1260/************************************************************************/
1261void
1262lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
1263{
1264 int t;
1265 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05001266 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05001267
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02001268 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05001269 if (!HashWorking)
1270 return;
1271
dea31012005-04-17 16:05:31 -05001272 HashWorking[0] = HashWorking[78] = *pwwnn++;
1273 HashWorking[1] = HashWorking[79] = *pwwnn;
1274
1275 for (t = 0; t < 7; t++)
1276 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
1277
1278 lpfc_sha_init(hbainit);
1279 lpfc_sha_iterate(hbainit, HashWorking);
1280 kfree(HashWorking);
1281}
1282
1283static void
James Smart2e0fef82007-06-17 19:56:36 -05001284lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05001285{
1286 struct lpfc_nodelist *ndlp, *next_ndlp;
1287
1288 /* clean up phba - lpfc specific */
James Smart2e0fef82007-06-17 19:56:36 -05001289 lpfc_can_disctmo(vport);
1290 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp)
James Smart329f9bc2007-04-25 09:53:01 -04001291 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05001292 return;
1293}
1294
1295static void
1296lpfc_establish_link_tmo(unsigned long ptr)
1297{
James Smart92d7f7b2007-06-17 19:56:38 -05001298 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James Smart549e55c2007-08-02 11:09:51 -04001299 struct lpfc_vport **vports;
dea31012005-04-17 16:05:31 -05001300 unsigned long iflag;
James Smart549e55c2007-08-02 11:09:51 -04001301 int i;
dea31012005-04-17 16:05:31 -05001302
dea31012005-04-17 16:05:31 -05001303 /* Re-establishing Link, timer expired */
1304 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001305 "1300 Re-establishing Link, timer expired "
dea31012005-04-17 16:05:31 -05001306 "Data: x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001307 phba->pport->fc_flag, phba->pport->port_state);
James Smart549e55c2007-08-02 11:09:51 -04001308 vports = lpfc_create_vport_work_array(phba);
1309 if (vports != NULL)
1310 for(i = 0; i < LPFC_MAX_VPORTS && vports[i] != NULL; i++) {
1311 struct Scsi_Host *shost;
1312 shost = lpfc_shost_from_vport(vports[i]);
1313 spin_lock_irqsave(shost->host_lock, iflag);
1314 vports[i]->fc_flag &= ~FC_ESTABLISH_LINK;
1315 spin_unlock_irqrestore(shost->host_lock, iflag);
1316 }
1317 lpfc_destroy_vport_work_array(vports);
dea31012005-04-17 16:05:31 -05001318}
1319
James Smart92d7f7b2007-06-17 19:56:38 -05001320void
1321lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05001322{
James Smart92d7f7b2007-06-17 19:56:38 -05001323 del_timer_sync(&vport->els_tmofunc);
1324 del_timer_sync(&vport->fc_fdmitmo);
1325 lpfc_can_disctmo(vport);
1326 return;
dea31012005-04-17 16:05:31 -05001327}
1328
James Smart2e0fef82007-06-17 19:56:36 -05001329static void
James Smart92d7f7b2007-06-17 19:56:38 -05001330lpfc_stop_phba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001331{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001332 del_timer_sync(&phba->fcp_poll_timer);
dea31012005-04-17 16:05:31 -05001333 del_timer_sync(&phba->fc_estabtmo);
James Smart51ef4c22007-08-02 11:10:31 -04001334 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05001335 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05001336 del_timer_sync(&phba->fabric_block_timer);
James Smart858c9f62007-06-17 19:56:39 -05001337 phba->hb_outstanding = 0;
1338 del_timer_sync(&phba->hb_tmofunc);
James Smart2e0fef82007-06-17 19:56:36 -05001339 return;
dea31012005-04-17 16:05:31 -05001340}
1341
1342int
James Smart2e0fef82007-06-17 19:56:36 -05001343lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001344{
James Smart2e0fef82007-06-17 19:56:36 -05001345 struct lpfc_vport *vport = phba->pport;
James Smart549e55c2007-08-02 11:09:51 -04001346 struct lpfc_vport **vports;
1347 int i;
James Smart2e0fef82007-06-17 19:56:36 -05001348
dea31012005-04-17 16:05:31 -05001349 if (!phba)
1350 return 0;
1351
James Smart2e0fef82007-06-17 19:56:36 -05001352 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05001353 return 0;
1354
James Smarted957682007-06-17 19:56:37 -05001355 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001356 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05001357
James Smart46fa3112007-04-25 09:51:45 -04001358 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001359
James Smart46fa3112007-04-25 09:51:45 -04001360 if (!lpfc_sli_queue_setup(phba)) {
1361 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001362 return 1;
James Smart46fa3112007-04-25 09:51:45 -04001363 }
1364
1365 if (lpfc_sli_hba_setup(phba)) { /* Initialize the HBA */
1366 lpfc_unblock_mgmt_io(phba);
1367 return 1;
1368 }
dea31012005-04-17 16:05:31 -05001369
James Smart549e55c2007-08-02 11:09:51 -04001370 vports = lpfc_create_vport_work_array(phba);
1371 if (vports != NULL)
1372 for(i = 0; i < LPFC_MAX_VPORTS && vports[i] != NULL; i++) {
1373 struct Scsi_Host *shost;
1374 shost = lpfc_shost_from_vport(vports[i]);
1375 spin_lock_irq(shost->host_lock);
1376 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
1377 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
1378 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
1379 spin_unlock_irq(shost->host_lock);
1380 }
1381 lpfc_destroy_vport_work_array(vports);
dea31012005-04-17 16:05:31 -05001382
James Smart46fa3112007-04-25 09:51:45 -04001383 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001384 return 0;
1385}
1386
James Smart46fa3112007-04-25 09:51:45 -04001387void
1388lpfc_block_mgmt_io(struct lpfc_hba * phba)
dea31012005-04-17 16:05:31 -05001389{
dea31012005-04-17 16:05:31 -05001390 unsigned long iflag;
dea31012005-04-17 16:05:31 -05001391
James Smart2e0fef82007-06-17 19:56:36 -05001392 spin_lock_irqsave(&phba->hbalock, iflag);
1393 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
1394 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04001395}
1396
1397void
1398lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
1399{
1400 unsigned long iflag;
1401
James Smart2e0fef82007-06-17 19:56:36 -05001402 spin_lock_irqsave(&phba->hbalock, iflag);
1403 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
1404 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04001405}
1406
1407void
1408lpfc_offline_prep(struct lpfc_hba * phba)
1409{
James Smart2e0fef82007-06-17 19:56:36 -05001410 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04001411 struct lpfc_nodelist *ndlp, *next_ndlp;
dea31012005-04-17 16:05:31 -05001412
James Smart2e0fef82007-06-17 19:56:36 -05001413 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04001414 return;
dea31012005-04-17 16:05:31 -05001415
James Smart46fa3112007-04-25 09:51:45 -04001416 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001417
1418 lpfc_linkdown(phba);
1419
James Smart46fa3112007-04-25 09:51:45 -04001420 /* Issue an unreg_login to all nodes */
James Smart2e0fef82007-06-17 19:56:36 -05001421 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp)
James Smart685f0bf2007-04-25 09:53:08 -04001422 if (ndlp->nlp_state != NLP_STE_UNUSED_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05001423 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05001424
James Smart46fa3112007-04-25 09:51:45 -04001425 lpfc_sli_flush_mbox_queue(phba);
1426}
1427
1428void
James Smart2e0fef82007-06-17 19:56:36 -05001429lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04001430{
James Smart549e55c2007-08-02 11:09:51 -04001431 struct Scsi_Host *shost;
1432 struct lpfc_vport **vports;
1433 int i;
James Smart46fa3112007-04-25 09:51:45 -04001434
James Smart549e55c2007-08-02 11:09:51 -04001435 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04001436 return;
James Smart688a8862006-07-06 15:49:56 -04001437
dea31012005-04-17 16:05:31 -05001438 /* stop all timers associated with this hba */
James Smart92d7f7b2007-06-17 19:56:38 -05001439 lpfc_stop_phba_timers(phba);
James Smart51ef4c22007-08-02 11:10:31 -04001440 vports = lpfc_create_vport_work_array(phba);
1441 if (vports != NULL)
1442 for(i = 0; i < LPFC_MAX_VPORTS && vports[i] != NULL; i++)
1443 lpfc_stop_vport_timers(vports[i]);
1444 lpfc_destroy_vport_work_array(vports);
James Smart92d7f7b2007-06-17 19:56:38 -05001445 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001446 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05001447 /* Bring down the SLI Layer and cleanup. The HBA is offline
1448 now. */
1449 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001450 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04001451 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001452 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04001453 vports = lpfc_create_vport_work_array(phba);
1454 if (vports != NULL)
1455 for(i = 0; i < LPFC_MAX_VPORTS && vports[i] != NULL; i++) {
1456 shost = lpfc_shost_from_vport(vports[i]);
1457 lpfc_cleanup(vports[i]);
1458 spin_lock_irq(shost->host_lock);
1459 vports[i]->work_port_events = 0;
1460 vports[i]->fc_flag |= FC_OFFLINE_MODE;
1461 spin_unlock_irq(shost->host_lock);
1462 }
1463 lpfc_destroy_vport_work_array(vports);
dea31012005-04-17 16:05:31 -05001464}
1465
1466/******************************************************************************
1467* Function name: lpfc_scsi_free
1468*
1469* Description: Called from lpfc_pci_remove_one free internal driver resources
1470*
1471******************************************************************************/
1472static int
James Smart2e0fef82007-06-17 19:56:36 -05001473lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001474{
1475 struct lpfc_scsi_buf *sb, *sb_next;
1476 struct lpfc_iocbq *io, *io_next;
1477
James Smart2e0fef82007-06-17 19:56:36 -05001478 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001479 /* Release all the lpfc_scsi_bufs maintained by this host. */
1480 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
1481 list_del(&sb->list);
1482 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05001483 sb->dma_handle);
dea31012005-04-17 16:05:31 -05001484 kfree(sb);
1485 phba->total_scsi_bufs--;
1486 }
1487
1488 /* Release all the lpfc_iocbq entries maintained by this host. */
1489 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
1490 list_del(&io->list);
1491 kfree(io);
1492 phba->total_iocbq_bufs--;
1493 }
1494
James Smart2e0fef82007-06-17 19:56:36 -05001495 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001496
1497 return 0;
1498}
1499
James Smart2e0fef82007-06-17 19:56:36 -05001500struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04001501lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04001502{
James Smart2e0fef82007-06-17 19:56:36 -05001503 struct lpfc_vport *vport;
1504 struct Scsi_Host *shost;
1505 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04001506
James Smart3de2a652007-08-02 11:09:59 -04001507 if (dev != &phba->pcidev->dev)
1508 shost = scsi_host_alloc(&lpfc_vport_template,
1509 sizeof(struct lpfc_vport));
1510 else
1511 shost = scsi_host_alloc(&lpfc_template,
1512 sizeof(struct lpfc_vport));
James Smart2e0fef82007-06-17 19:56:36 -05001513 if (!shost)
1514 goto out;
James Smart47a86172007-04-25 09:53:22 -04001515
James Smart2e0fef82007-06-17 19:56:36 -05001516 vport = (struct lpfc_vport *) shost->hostdata;
1517 vport->phba = phba;
James Smart47a86172007-04-25 09:53:22 -04001518
James Smart2e0fef82007-06-17 19:56:36 -05001519 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05001520 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart47a86172007-04-25 09:53:22 -04001521
James Smart3de2a652007-08-02 11:09:59 -04001522 lpfc_get_vport_cfgparam(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001523 shost->unique_id = instance;
1524 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04001525 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05001526 shost->this_id = -1;
1527 shost->max_cmd_len = 16;
James Smart47a86172007-04-25 09:53:22 -04001528 /*
James Smart2e0fef82007-06-17 19:56:36 -05001529 * Set initial can_queue value since 0 is no longer supported and
1530 * scsi_add_host will fail. This will be adjusted later based on the
1531 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04001532 */
James Smart2e0fef82007-06-17 19:56:36 -05001533 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04001534 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05001535 shost->transportt = lpfc_vport_transport_template;
1536 vport->port_type = LPFC_NPIV_PORT;
1537 } else {
1538 shost->transportt = lpfc_transport_template;
1539 vport->port_type = LPFC_PHYSICAL_PORT;
1540 }
James Smart47a86172007-04-25 09:53:22 -04001541
James Smart2e0fef82007-06-17 19:56:36 -05001542 /* Initialize all internally managed lists. */
1543 INIT_LIST_HEAD(&vport->fc_nodes);
1544 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04001545
James Smart2e0fef82007-06-17 19:56:36 -05001546 init_timer(&vport->fc_disctmo);
1547 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05001548 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04001549
James Smart2e0fef82007-06-17 19:56:36 -05001550 init_timer(&vport->fc_fdmitmo);
1551 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
James Smart92d7f7b2007-06-17 19:56:38 -05001552 vport->fc_fdmitmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04001553
James Smart2e0fef82007-06-17 19:56:36 -05001554 init_timer(&vport->els_tmofunc);
1555 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05001556 vport->els_tmofunc.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04001557
James Smart3de2a652007-08-02 11:09:59 -04001558 error = scsi_add_host(shost, dev);
James Smart2e0fef82007-06-17 19:56:36 -05001559 if (error)
1560 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04001561
James Smart549e55c2007-08-02 11:09:51 -04001562 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05001563 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04001564 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05001565 return vport;
James Smart47a86172007-04-25 09:53:22 -04001566
James Smart2e0fef82007-06-17 19:56:36 -05001567out_put_shost:
1568 scsi_host_put(shost);
1569out:
1570 return NULL;
James Smart47a86172007-04-25 09:53:22 -04001571}
1572
James Smart2e0fef82007-06-17 19:56:36 -05001573void
1574destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04001575{
James Smart92d7f7b2007-06-17 19:56:38 -05001576 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1577 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04001578
James Smart92d7f7b2007-06-17 19:56:38 -05001579 kfree(vport->vname);
James Smart47a86172007-04-25 09:53:22 -04001580
James Smart858c9f62007-06-17 19:56:39 -05001581 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05001582 fc_remove_host(shost);
1583 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04001584
James Smart92d7f7b2007-06-17 19:56:38 -05001585 spin_lock_irq(&phba->hbalock);
1586 list_del_init(&vport->listentry);
1587 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04001588
James Smart92d7f7b2007-06-17 19:56:38 -05001589 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04001590 return;
James Smart47a86172007-04-25 09:53:22 -04001591}
1592
James Smart92d7f7b2007-06-17 19:56:38 -05001593int
1594lpfc_get_instance(void)
1595{
1596 int instance = 0;
1597
1598 /* Assign an unused number */
1599 if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
1600 return -1;
1601 if (idr_get_new(&lpfc_hba_index, NULL, &instance))
1602 return -1;
1603 return instance;
1604}
1605
James Smart858c9f62007-06-17 19:56:39 -05001606/*
1607 * Note: there is no scan_start function as adapter initialization
1608 * will have asynchronously kicked off the link initialization.
1609 */
James Smart47a86172007-04-25 09:53:22 -04001610
1611int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
1612{
James Smart2e0fef82007-06-17 19:56:36 -05001613 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1614 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05001615 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04001616
James Smart858c9f62007-06-17 19:56:39 -05001617 spin_lock_irq(shost->host_lock);
1618
James Smart51ef4c22007-08-02 11:10:31 -04001619 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05001620 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04001621 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05001622 }
James Smart2e0fef82007-06-17 19:56:36 -05001623 if (time >= 30 * HZ) {
1624 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001625 "0461 Scanning longer than 30 "
1626 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05001627 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04001628 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05001629 }
1630 if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
1631 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001632 "0465 Link down longer than 15 "
1633 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05001634 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05001635 goto finished;
James Smart47a86172007-04-25 09:53:22 -04001636 }
1637
James Smart2e0fef82007-06-17 19:56:36 -05001638 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05001639 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05001640 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05001641 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05001642 if (vport->fc_map_cnt == 0 && time < 2 * HZ)
James Smart858c9f62007-06-17 19:56:39 -05001643 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05001644 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05001645 goto finished;
1646
1647 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04001648
1649finished:
James Smart858c9f62007-06-17 19:56:39 -05001650 spin_unlock_irq(shost->host_lock);
1651 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05001652}
1653
1654void lpfc_host_attrib_init(struct Scsi_Host *shost)
1655{
1656 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1657 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04001658 /*
James Smart2e0fef82007-06-17 19:56:36 -05001659 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04001660 */
1661
James Smart2e0fef82007-06-17 19:56:36 -05001662 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
1663 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04001664 fc_host_supported_classes(shost) = FC_COS_CLASS3;
1665
1666 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05001667 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04001668 fc_host_supported_fc4s(shost)[2] = 1;
1669 fc_host_supported_fc4s(shost)[7] = 1;
1670
James Smart92d7f7b2007-06-17 19:56:38 -05001671 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
1672 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04001673
1674 fc_host_supported_speeds(shost) = 0;
1675 if (phba->lmt & LMT_10Gb)
1676 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
1677 if (phba->lmt & LMT_4Gb)
1678 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
1679 if (phba->lmt & LMT_2Gb)
1680 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
1681 if (phba->lmt & LMT_1Gb)
1682 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
1683
1684 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05001685 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
1686 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04001687
1688 /* This value is also unchanging */
1689 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05001690 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04001691 fc_host_active_fc4s(shost)[2] = 1;
1692 fc_host_active_fc4s(shost)[7] = 1;
1693
James Smart92d7f7b2007-06-17 19:56:38 -05001694 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04001695 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04001696 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04001697 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04001698}
dea31012005-04-17 16:05:31 -05001699
1700static int __devinit
1701lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
1702{
James Smart2e0fef82007-06-17 19:56:36 -05001703 struct lpfc_vport *vport = NULL;
1704 struct lpfc_hba *phba;
1705 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05001706 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05001707 struct Scsi_Host *shost = NULL;
James Smart51ef4c22007-08-02 11:10:31 -04001708 void *ptr;
dea31012005-04-17 16:05:31 -05001709 unsigned long bar0map_len, bar2map_len;
James Bottomleya5785032007-07-14 18:47:04 -05001710 int error = -ENODEV;
James Smart51ef4c22007-08-02 11:10:31 -04001711 int i, hbq_count;
James Bottomley604a3e32005-10-29 10:28:33 -05001712 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001713
1714 if (pci_enable_device(pdev))
1715 goto out;
1716 if (pci_request_regions(pdev, LPFC_DRIVER_NAME))
1717 goto out_disable_device;
1718
James Smart2e0fef82007-06-17 19:56:36 -05001719 phba = kzalloc(sizeof (struct lpfc_hba), GFP_KERNEL);
1720 if (!phba)
dea31012005-04-17 16:05:31 -05001721 goto out_release_regions;
1722
James Smart2e0fef82007-06-17 19:56:36 -05001723 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001724
dea31012005-04-17 16:05:31 -05001725 phba->pcidev = pdev;
1726
1727 /* Assign an unused board number */
James Smart92d7f7b2007-06-17 19:56:38 -05001728 if ((phba->brd_no = lpfc_get_instance()) < 0)
James Smart2e0fef82007-06-17 19:56:36 -05001729 goto out_free_phba;
dea31012005-04-17 16:05:31 -05001730
James Smart2e0fef82007-06-17 19:56:36 -05001731 INIT_LIST_HEAD(&phba->port_list);
James Smart2e0fef82007-06-17 19:56:36 -05001732 /*
1733 * Get all the module params for configuring this host and then
1734 * establish the host.
1735 */
1736 lpfc_get_cfgparam(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001737 phba->max_vpi = LPFC_MAX_VPI;
dea31012005-04-17 16:05:31 -05001738
1739 /* Initialize timers used by driver */
1740 init_timer(&phba->fc_estabtmo);
1741 phba->fc_estabtmo.function = lpfc_establish_link_tmo;
1742 phba->fc_estabtmo.data = (unsigned long)phba;
dea31012005-04-17 16:05:31 -05001743
James Smart858c9f62007-06-17 19:56:39 -05001744 init_timer(&phba->hb_tmofunc);
1745 phba->hb_tmofunc.function = lpfc_hb_timeout;
1746 phba->hb_tmofunc.data = (unsigned long)phba;
1747
dea31012005-04-17 16:05:31 -05001748 psli = &phba->sli;
1749 init_timer(&psli->mbox_tmo);
1750 psli->mbox_tmo.function = lpfc_mbox_timeout;
James Smart2e0fef82007-06-17 19:56:36 -05001751 psli->mbox_tmo.data = (unsigned long) phba;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001752 init_timer(&phba->fcp_poll_timer);
1753 phba->fcp_poll_timer.function = lpfc_poll_timeout;
James Smart2e0fef82007-06-17 19:56:36 -05001754 phba->fcp_poll_timer.data = (unsigned long) phba;
James Smart92d7f7b2007-06-17 19:56:38 -05001755 init_timer(&phba->fabric_block_timer);
1756 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
1757 phba->fabric_block_timer.data = (unsigned long) phba;
dea31012005-04-17 16:05:31 -05001758
dea31012005-04-17 16:05:31 -05001759 pci_set_master(pdev);
Randy Dunlap694625c2007-07-09 11:55:54 -07001760 pci_try_set_mwi(pdev);
dea31012005-04-17 16:05:31 -05001761
1762 if (pci_set_dma_mask(phba->pcidev, DMA_64BIT_MASK) != 0)
1763 if (pci_set_dma_mask(phba->pcidev, DMA_32BIT_MASK) != 0)
1764 goto out_idr_remove;
1765
1766 /*
1767 * Get the bus address of Bar0 and Bar2 and the number of bytes
1768 * required by each mapping.
1769 */
1770 phba->pci_bar0_map = pci_resource_start(phba->pcidev, 0);
1771 bar0map_len = pci_resource_len(phba->pcidev, 0);
1772
1773 phba->pci_bar2_map = pci_resource_start(phba->pcidev, 2);
1774 bar2map_len = pci_resource_len(phba->pcidev, 2);
1775
Jamie Wellnitz901a9202006-02-28 19:25:19 -05001776 /* Map HBA SLIM to a kernel virtual address. */
dea31012005-04-17 16:05:31 -05001777 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05001778 if (!phba->slim_memmap_p) {
1779 error = -ENODEV;
1780 dev_printk(KERN_ERR, &pdev->dev,
1781 "ioremap failed for SLIM memory.\n");
1782 goto out_idr_remove;
1783 }
1784
1785 /* Map HBA Control Registers to a kernel virtual address. */
dea31012005-04-17 16:05:31 -05001786 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05001787 if (!phba->ctrl_regs_memmap_p) {
1788 error = -ENODEV;
1789 dev_printk(KERN_ERR, &pdev->dev,
1790 "ioremap failed for HBA control registers.\n");
1791 goto out_iounmap_slim;
1792 }
dea31012005-04-17 16:05:31 -05001793
1794 /* Allocate memory for SLI-2 structures */
1795 phba->slim2p = dma_alloc_coherent(&phba->pcidev->dev, SLI2_SLIM_SIZE,
1796 &phba->slim2p_mapping, GFP_KERNEL);
1797 if (!phba->slim2p)
1798 goto out_iounmap;
1799
James.Smart@Emulex.Comf91b3922005-10-28 20:29:28 -04001800 memset(phba->slim2p, 0, SLI2_SLIM_SIZE);
dea31012005-04-17 16:05:31 -05001801
James Smarted957682007-06-17 19:56:37 -05001802 phba->hbqslimp.virt = dma_alloc_coherent(&phba->pcidev->dev,
1803 lpfc_sli_hbq_size(),
1804 &phba->hbqslimp.phys,
1805 GFP_KERNEL);
1806 if (!phba->hbqslimp.virt)
1807 goto out_free_slim;
1808
James Smart51ef4c22007-08-02 11:10:31 -04001809 hbq_count = lpfc_sli_hbq_count();
1810 ptr = phba->hbqslimp.virt;
1811 for (i = 0; i < hbq_count; ++i) {
1812 phba->hbqs[i].hbq_virt = ptr;
1813 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
1814 ptr += (lpfc_hbq_defs[i]->entry_count *
1815 sizeof(struct lpfc_hbq_entry));
1816 }
1817 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
1818 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
1819
James Smarted957682007-06-17 19:56:37 -05001820 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
1821
dea31012005-04-17 16:05:31 -05001822 /* Initialize the SLI Layer to run with lpfc HBAs. */
1823 lpfc_sli_setup(phba);
1824 lpfc_sli_queue_setup(phba);
1825
1826 error = lpfc_mem_alloc(phba);
1827 if (error)
James Smarted957682007-06-17 19:56:37 -05001828 goto out_free_hbqslimp;
dea31012005-04-17 16:05:31 -05001829
1830 /* Initialize and populate the iocb list per host. */
1831 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
1832 for (i = 0; i < LPFC_IOCB_LIST_CNT; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001833 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05001834 if (iocbq_entry == NULL) {
1835 printk(KERN_ERR "%s: only allocated %d iocbs of "
1836 "expected %d count. Unloading driver.\n",
1837 __FUNCTION__, i, LPFC_IOCB_LIST_CNT);
1838 error = -ENOMEM;
1839 goto out_free_iocbq;
1840 }
1841
James Bottomley604a3e32005-10-29 10:28:33 -05001842 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
1843 if (iotag == 0) {
1844 kfree (iocbq_entry);
1845 printk(KERN_ERR "%s: failed to allocate IOTAG. "
1846 "Unloading driver.\n",
1847 __FUNCTION__);
1848 error = -ENOMEM;
1849 goto out_free_iocbq;
1850 }
James Smart2e0fef82007-06-17 19:56:36 -05001851
1852 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001853 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
1854 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05001855 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001856 }
1857
1858 /* Initialize HBA structure */
1859 phba->fc_edtov = FF_DEF_EDTOV;
1860 phba->fc_ratov = FF_DEF_RATOV;
1861 phba->fc_altov = FF_DEF_ALTOV;
1862 phba->fc_arbtov = FF_DEF_ARBTOV;
1863
1864 INIT_LIST_HEAD(&phba->work_list);
1865 phba->work_ha_mask = (HA_ERATT|HA_MBATT|HA_LATT);
1866 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
1867
1868 /* Startup the kernel thread for this host adapter. */
1869 phba->worker_thread = kthread_run(lpfc_do_work, phba,
1870 "lpfc_worker_%d", phba->brd_no);
1871 if (IS_ERR(phba->worker_thread)) {
1872 error = PTR_ERR(phba->worker_thread);
1873 goto out_free_iocbq;
1874 }
1875
dea31012005-04-17 16:05:31 -05001876 /* Initialize the list of scsi buffers used by driver for scsi IO. */
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001877 spin_lock_init(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05001878 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
1879
James Smart92d7f7b2007-06-17 19:56:38 -05001880 /* Initialize list of fabric iocbs */
1881 INIT_LIST_HEAD(&phba->fabric_iocb_list);
1882
James Smart3de2a652007-08-02 11:09:59 -04001883 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05001884 if (!vport)
1885 goto out_kthread_stop;
1886
1887 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001888 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05001889 lpfc_debugfs_initialize(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001890
James Smart92d7f7b2007-06-17 19:56:38 -05001891 pci_set_drvdata(pdev, shost);
dea31012005-04-17 16:05:31 -05001892
James Smart4ff43242006-12-02 13:34:56 -05001893 if (phba->cfg_use_msi) {
1894 error = pci_enable_msi(phba->pcidev);
James Smart51ef4c22007-08-02 11:10:31 -04001895 if (!error)
1896 phba->using_msi = 1;
1897 else
James Smarte8b62012007-08-02 11:10:09 -04001898 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1899 "0452 Enable MSI failed, continuing "
1900 "with IRQ\n");
James Smart4ff43242006-12-02 13:34:56 -05001901 }
1902
Thomas Gleixner1d6f3592006-07-01 19:29:42 -07001903 error = request_irq(phba->pcidev->irq, lpfc_intr_handler, IRQF_SHARED,
James Smart2e0fef82007-06-17 19:56:36 -05001904 LPFC_DRIVER_NAME, phba);
dea31012005-04-17 16:05:31 -05001905 if (error) {
1906 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001907 "0451 Enable interrupt handler failed\n");
James Smart92d7f7b2007-06-17 19:56:38 -05001908 goto out_disable_msi;
dea31012005-04-17 16:05:31 -05001909 }
dea31012005-04-17 16:05:31 -05001910
James Smart2e0fef82007-06-17 19:56:36 -05001911 phba->MBslimaddr = phba->slim_memmap_p;
1912 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
1913 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
1914 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
1915 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
1916
James Smart92d7f7b2007-06-17 19:56:38 -05001917 if (lpfc_alloc_sysfs_attr(vport))
dea31012005-04-17 16:05:31 -05001918 goto out_free_irq;
1919
James Smart858c9f62007-06-17 19:56:39 -05001920 if (lpfc_sli_hba_setup(phba))
1921 goto out_remove_device;
1922
1923 /*
1924 * hba setup may have changed the hba_queue_depth so we need to adjust
1925 * the value of can_queue.
1926 */
1927 shost->can_queue = phba->cfg_hba_queue_depth - 10;
1928
1929 lpfc_host_attrib_init(shost);
1930
James Smart2e0fef82007-06-17 19:56:36 -05001931 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
1932 spin_lock_irq(shost->host_lock);
1933 lpfc_poll_start_timer(phba);
1934 spin_unlock_irq(shost->host_lock);
1935 }
James Smart8f6d98d2006-08-01 07:34:00 -04001936
James Smart858c9f62007-06-17 19:56:39 -05001937 scsi_scan_host(shost);
dea31012005-04-17 16:05:31 -05001938
dea31012005-04-17 16:05:31 -05001939 return 0;
1940
James Smart858c9f62007-06-17 19:56:39 -05001941out_remove_device:
1942 lpfc_free_sysfs_attr(vport);
1943 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04001944 vport->load_flag |= FC_UNLOADING;
James Smart858c9f62007-06-17 19:56:39 -05001945 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001946out_free_irq:
James Smart92d7f7b2007-06-17 19:56:38 -05001947 lpfc_stop_phba_timers(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001948 phba->pport->work_port_events = 0;
dea31012005-04-17 16:05:31 -05001949 free_irq(phba->pcidev->irq, phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001950out_disable_msi:
James Smart51ef4c22007-08-02 11:10:31 -04001951 if (phba->using_msi)
1952 pci_disable_msi(phba->pcidev);
James Smart2e0fef82007-06-17 19:56:36 -05001953 destroy_port(vport);
dea31012005-04-17 16:05:31 -05001954out_kthread_stop:
1955 kthread_stop(phba->worker_thread);
1956out_free_iocbq:
1957 list_for_each_entry_safe(iocbq_entry, iocbq_next,
1958 &phba->lpfc_iocb_list, list) {
dea31012005-04-17 16:05:31 -05001959 kfree(iocbq_entry);
1960 phba->total_iocbq_bufs--;
dea31012005-04-17 16:05:31 -05001961 }
1962 lpfc_mem_free(phba);
James Smarted957682007-06-17 19:56:37 -05001963out_free_hbqslimp:
1964 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(), phba->hbqslimp.virt,
1965 phba->hbqslimp.phys);
dea31012005-04-17 16:05:31 -05001966out_free_slim:
1967 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE, phba->slim2p,
1968 phba->slim2p_mapping);
1969out_iounmap:
1970 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05001971out_iounmap_slim:
dea31012005-04-17 16:05:31 -05001972 iounmap(phba->slim_memmap_p);
1973out_idr_remove:
1974 idr_remove(&lpfc_hba_index, phba->brd_no);
James Smart2e0fef82007-06-17 19:56:36 -05001975out_free_phba:
1976 kfree(phba);
dea31012005-04-17 16:05:31 -05001977out_release_regions:
1978 pci_release_regions(pdev);
1979out_disable_device:
1980 pci_disable_device(pdev);
1981out:
James Smartdefbcf12006-04-16 22:26:50 -04001982 pci_set_drvdata(pdev, NULL);
James Smart858c9f62007-06-17 19:56:39 -05001983 if (shost)
1984 scsi_host_put(shost);
dea31012005-04-17 16:05:31 -05001985 return error;
1986}
1987
1988static void __devexit
1989lpfc_pci_remove_one(struct pci_dev *pdev)
1990{
James Smart2e0fef82007-06-17 19:56:36 -05001991 struct Scsi_Host *shost = pci_get_drvdata(pdev);
1992 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1993 struct lpfc_hba *phba = vport->phba;
James Smart549e55c2007-08-02 11:09:51 -04001994 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04001995 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04001996 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001997
James Smart858c9f62007-06-17 19:56:39 -05001998 kfree(vport->vname);
1999 lpfc_free_sysfs_attr(vport);
2000
2001 fc_remove_host(shost);
2002 scsi_remove_host(shost);
James Smart2e0fef82007-06-17 19:56:36 -05002003 /*
2004 * Bring down the SLI Layer. This step disable all interrupts,
2005 * clears the rings, discards all mailbox commands, and resets
2006 * the HBA.
2007 */
2008 lpfc_sli_hba_down(phba);
2009 lpfc_sli_brdrestart(phba);
2010
James Smart92d7f7b2007-06-17 19:56:38 -05002011 lpfc_stop_phba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05002012 spin_lock_irq(&phba->hbalock);
2013 list_del_init(&vport->listentry);
2014 spin_unlock_irq(&phba->hbalock);
2015
James Smart858c9f62007-06-17 19:56:39 -05002016 lpfc_debugfs_terminate(vport);
2017 lpfc_cleanup(vport);
James Smart2e0fef82007-06-17 19:56:36 -05002018
2019 kthread_stop(phba->worker_thread);
2020
2021 /* Release the irq reservation */
2022 free_irq(phba->pcidev->irq, phba);
James Smart51ef4c22007-08-02 11:10:31 -04002023 if (phba->using_msi)
2024 pci_disable_msi(phba->pcidev);
dea31012005-04-17 16:05:31 -05002025
2026 pci_set_drvdata(pdev, NULL);
James Smart858c9f62007-06-17 19:56:39 -05002027 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05002028
2029 /*
2030 * Call scsi_free before mem_free since scsi bufs are released to their
2031 * corresponding pools here.
2032 */
2033 lpfc_scsi_free(phba);
2034 lpfc_mem_free(phba);
2035
James Smarted957682007-06-17 19:56:37 -05002036 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(), phba->hbqslimp.virt,
2037 phba->hbqslimp.phys);
2038
James Smart2e0fef82007-06-17 19:56:36 -05002039 /* Free resources associated with SLI2 interface */
2040 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
2041 phba->slim2p, phba->slim2p_mapping);
2042
2043 /* unmap adapter SLIM and Control Registers */
2044 iounmap(phba->ctrl_regs_memmap_p);
2045 iounmap(phba->slim_memmap_p);
2046
2047 idr_remove(&lpfc_hba_index, phba->brd_no);
2048
2049 kfree(phba);
2050
2051 pci_release_regions(pdev);
2052 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05002053}
2054
Linas Vepstas8d63f372007-02-14 14:28:36 -06002055/**
2056 * lpfc_io_error_detected - called when PCI error is detected
2057 * @pdev: Pointer to PCI device
2058 * @state: The current pci conneection state
2059 *
2060 * This function is called after a PCI bus error affecting
2061 * this device has been detected.
2062 */
2063static pci_ers_result_t lpfc_io_error_detected(struct pci_dev *pdev,
2064 pci_channel_state_t state)
2065{
James Smart51ef4c22007-08-02 11:10:31 -04002066 struct Scsi_Host *shost = pci_get_drvdata(pdev);
2067 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06002068 struct lpfc_sli *psli = &phba->sli;
2069 struct lpfc_sli_ring *pring;
2070
Linas Vepstas5daa49e2007-03-08 16:19:11 -06002071 if (state == pci_channel_io_perm_failure)
Linas Vepstas8d63f372007-02-14 14:28:36 -06002072 return PCI_ERS_RESULT_DISCONNECT;
Linas Vepstas5daa49e2007-03-08 16:19:11 -06002073
Linas Vepstas8d63f372007-02-14 14:28:36 -06002074 pci_disable_device(pdev);
2075 /*
2076 * There may be I/Os dropped by the firmware.
2077 * Error iocb (I/O) on txcmplq and let the SCSI layer
2078 * retry it after re-establishing link.
2079 */
2080 pring = &psli->ring[psli->fcp_ring];
2081 lpfc_sli_abort_iocb_ring(phba, pring);
2082
James Smart51ef4c22007-08-02 11:10:31 -04002083 /* Release the irq reservation */
2084 free_irq(phba->pcidev->irq, phba);
2085 if (phba->using_msi)
2086 pci_disable_msi(phba->pcidev);
2087
Linas Vepstas8d63f372007-02-14 14:28:36 -06002088 /* Request a slot reset. */
2089 return PCI_ERS_RESULT_NEED_RESET;
2090}
2091
2092/**
2093 * lpfc_io_slot_reset - called after the pci bus has been reset.
2094 * @pdev: Pointer to PCI device
2095 *
2096 * Restart the card from scratch, as if from a cold-boot.
2097 */
2098static pci_ers_result_t lpfc_io_slot_reset(struct pci_dev *pdev)
2099{
James Smart51ef4c22007-08-02 11:10:31 -04002100 struct Scsi_Host *shost = pci_get_drvdata(pdev);
2101 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06002102 struct lpfc_sli *psli = &phba->sli;
2103 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
2104
2105 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
2106 if (pci_enable_device_bars(pdev, bars)) {
2107 printk(KERN_ERR "lpfc: Cannot re-enable "
2108 "PCI device after reset.\n");
2109 return PCI_ERS_RESULT_DISCONNECT;
2110 }
2111
2112 pci_set_master(pdev);
2113
2114 /* Re-establishing Link */
James Smart51ef4c22007-08-02 11:10:31 -04002115 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05002116 phba->pport->fc_flag |= FC_ESTABLISH_LINK;
James Smart51ef4c22007-08-02 11:10:31 -04002117 spin_unlock_irq(shost->host_lock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06002118
James Smart92d7f7b2007-06-17 19:56:38 -05002119 spin_lock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06002120 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05002121 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06002122
2123
2124 /* Take device offline; this will perform cleanup */
2125 lpfc_offline(phba);
2126 lpfc_sli_brdrestart(phba);
2127
2128 return PCI_ERS_RESULT_RECOVERED;
2129}
2130
2131/**
2132 * lpfc_io_resume - called when traffic can start flowing again.
2133 * @pdev: Pointer to PCI device
2134 *
2135 * This callback is called when the error recovery driver tells us that
2136 * its OK to resume normal operation.
2137 */
2138static void lpfc_io_resume(struct pci_dev *pdev)
2139{
James Smart51ef4c22007-08-02 11:10:31 -04002140 struct Scsi_Host *shost = pci_get_drvdata(pdev);
2141 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06002142
2143 if (lpfc_online(phba) == 0) {
2144 mod_timer(&phba->fc_estabtmo, jiffies + HZ * 60);
2145 }
2146}
2147
dea31012005-04-17 16:05:31 -05002148static struct pci_device_id lpfc_id_table[] = {
2149 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
2150 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002151 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
2152 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05002153 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
2154 PCI_ANY_ID, PCI_ANY_ID, },
2155 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
2156 PCI_ANY_ID, PCI_ANY_ID, },
2157 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
2158 PCI_ANY_ID, PCI_ANY_ID, },
2159 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
2160 PCI_ANY_ID, PCI_ANY_ID, },
2161 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
2162 PCI_ANY_ID, PCI_ANY_ID, },
2163 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
2164 PCI_ANY_ID, PCI_ANY_ID, },
2165 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
2166 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002167 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
2168 PCI_ANY_ID, PCI_ANY_ID, },
2169 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
2170 PCI_ANY_ID, PCI_ANY_ID, },
2171 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
2172 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05002173 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
2174 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002175 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
2176 PCI_ANY_ID, PCI_ANY_ID, },
2177 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
2178 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05002179 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
2180 PCI_ANY_ID, PCI_ANY_ID, },
2181 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
2182 PCI_ANY_ID, PCI_ANY_ID, },
2183 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
2184 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002185 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
2186 PCI_ANY_ID, PCI_ANY_ID, },
2187 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
2188 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05002189 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
2190 PCI_ANY_ID, PCI_ANY_ID, },
2191 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
2192 PCI_ANY_ID, PCI_ANY_ID, },
2193 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
2194 PCI_ANY_ID, PCI_ANY_ID, },
2195 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
2196 PCI_ANY_ID, PCI_ANY_ID, },
2197 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
2198 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002199 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
2200 PCI_ANY_ID, PCI_ANY_ID, },
2201 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
2202 PCI_ANY_ID, PCI_ANY_ID, },
James Smartb87eab32007-04-25 09:53:28 -04002203 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
2204 PCI_ANY_ID, PCI_ANY_ID, },
2205 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
2206 PCI_ANY_ID, PCI_ANY_ID, },
2207 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
2208 PCI_ANY_ID, PCI_ANY_ID, },
2209 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
2210 PCI_ANY_ID, PCI_ANY_ID, },
2211 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
2212 PCI_ANY_ID, PCI_ANY_ID, },
2213 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
2214 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05002215 { 0 }
2216};
2217
2218MODULE_DEVICE_TABLE(pci, lpfc_id_table);
2219
Linas Vepstas8d63f372007-02-14 14:28:36 -06002220static struct pci_error_handlers lpfc_err_handler = {
2221 .error_detected = lpfc_io_error_detected,
2222 .slot_reset = lpfc_io_slot_reset,
2223 .resume = lpfc_io_resume,
2224};
2225
dea31012005-04-17 16:05:31 -05002226static struct pci_driver lpfc_driver = {
2227 .name = LPFC_DRIVER_NAME,
2228 .id_table = lpfc_id_table,
2229 .probe = lpfc_pci_probe_one,
2230 .remove = __devexit_p(lpfc_pci_remove_one),
James Smart2e0fef82007-06-17 19:56:36 -05002231 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -05002232};
2233
2234static int __init
2235lpfc_init(void)
2236{
2237 int error = 0;
2238
2239 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04002240 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -05002241
2242 lpfc_transport_template =
2243 fc_attach_transport(&lpfc_transport_functions);
James Smart92d7f7b2007-06-17 19:56:38 -05002244 lpfc_vport_transport_template =
2245 fc_attach_transport(&lpfc_vport_transport_functions);
2246 if (!lpfc_transport_template || !lpfc_vport_transport_template)
dea31012005-04-17 16:05:31 -05002247 return -ENOMEM;
2248 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -05002249 if (error) {
dea31012005-04-17 16:05:31 -05002250 fc_release_transport(lpfc_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -05002251 fc_release_transport(lpfc_vport_transport_template);
2252 }
dea31012005-04-17 16:05:31 -05002253
2254 return error;
2255}
2256
2257static void __exit
2258lpfc_exit(void)
2259{
2260 pci_unregister_driver(&lpfc_driver);
2261 fc_release_transport(lpfc_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -05002262 fc_release_transport(lpfc_vport_transport_template);
dea31012005-04-17 16:05:31 -05002263}
2264
2265module_init(lpfc_init);
2266module_exit(lpfc_exit);
2267MODULE_LICENSE("GPL");
2268MODULE_DESCRIPTION(LPFC_MODULE_DESC);
2269MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
2270MODULE_VERSION("0:" LPFC_DRIVER_VERSION);