blob: c6b30a8617bc24c8ad1a69c7678354a393d42ca7 [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
James Smart57127f12007-10-27 13:37:05 -0400215/* Completion handler for config async event mailbox command. */
216static void
217lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
218{
219 if (pmboxq->mb.mbxStatus == MBX_SUCCESS)
220 phba->temp_sensor_support = 1;
221 else
222 phba->temp_sensor_support = 0;
223 mempool_free(pmboxq, phba->mbox_mem_pool);
224 return;
225}
226
dea31012005-04-17 16:05:31 -0500227/************************************************************************/
228/* */
229/* lpfc_config_port_post */
230/* This routine will do LPFC initialization after the */
231/* CONFIG_PORT mailbox command. This will be initialized */
232/* as a SLI layer callback routine. */
233/* This routine returns 0 on success. Any other return value */
234/* indicates an error. */
235/* */
236/************************************************************************/
237int
James Smart2e0fef82007-06-17 19:56:36 -0500238lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500239{
James Smart2e0fef82007-06-17 19:56:36 -0500240 struct lpfc_vport *vport = phba->pport;
dea31012005-04-17 16:05:31 -0500241 LPFC_MBOXQ_t *pmb;
242 MAILBOX_t *mb;
243 struct lpfc_dmabuf *mp;
244 struct lpfc_sli *psli = &phba->sli;
245 uint32_t status, timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500246 int i, j;
247 int rc;
dea31012005-04-17 16:05:31 -0500248
James Smart7af67052007-10-27 13:38:11 -0400249 spin_lock_irq(&phba->hbalock);
250 /*
251 * If the Config port completed correctly the HBA is not
252 * over heated any more.
253 */
254 if (phba->over_temp_state == HBA_OVER_TEMP)
255 phba->over_temp_state = HBA_NORMAL_TEMP;
256 spin_unlock_irq(&phba->hbalock);
257
dea31012005-04-17 16:05:31 -0500258 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
259 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500260 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500261 return -ENOMEM;
262 }
263 mb = &pmb->mb;
264
dea31012005-04-17 16:05:31 -0500265 /* Get login parameters for NID. */
James Smart92d7f7b2007-06-17 19:56:38 -0500266 lpfc_read_sparam(phba, pmb, 0);
James Smarted957682007-06-17 19:56:37 -0500267 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500268 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500269 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400270 "0448 Adapter failed init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500271 "READ_SPARM mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500272 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500273 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500274 mp = (struct lpfc_dmabuf *) pmb->context1;
275 mempool_free( pmb, phba->mbox_mem_pool);
276 lpfc_mbuf_free(phba, mp->virt, mp->phys);
277 kfree(mp);
278 return -EIO;
279 }
280
281 mp = (struct lpfc_dmabuf *) pmb->context1;
282
James Smart2e0fef82007-06-17 19:56:36 -0500283 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500284 lpfc_mbuf_free(phba, mp->virt, mp->phys);
285 kfree(mp);
286 pmb->context1 = NULL;
287
James Smarta12e07b2006-12-02 13:35:30 -0500288 if (phba->cfg_soft_wwnn)
James Smart2e0fef82007-06-17 19:56:36 -0500289 u64_to_wwn(phba->cfg_soft_wwnn,
290 vport->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -0400291 if (phba->cfg_soft_wwpn)
James Smart2e0fef82007-06-17 19:56:36 -0500292 u64_to_wwn(phba->cfg_soft_wwpn,
293 vport->fc_sparam.portName.u.wwn);
294 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
dea31012005-04-17 16:05:31 -0500295 sizeof (struct lpfc_name));
James Smart2e0fef82007-06-17 19:56:36 -0500296 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
dea31012005-04-17 16:05:31 -0500297 sizeof (struct lpfc_name));
298 /* If no serial number in VPD data, use low 6 bytes of WWNN */
299 /* This should be consolidated into parse_vpd ? - mr */
300 if (phba->SerialNumber[0] == 0) {
301 uint8_t *outptr;
302
James Smart2e0fef82007-06-17 19:56:36 -0500303 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500304 for (i = 0; i < 12; i++) {
305 status = *outptr++;
306 j = ((status & 0xf0) >> 4);
307 if (j <= 9)
308 phba->SerialNumber[i] =
309 (char)((uint8_t) 0x30 + (uint8_t) j);
310 else
311 phba->SerialNumber[i] =
312 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
313 i++;
314 j = (status & 0xf);
315 if (j <= 9)
316 phba->SerialNumber[i] =
317 (char)((uint8_t) 0x30 + (uint8_t) j);
318 else
319 phba->SerialNumber[i] =
320 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
321 }
322 }
323
dea31012005-04-17 16:05:31 -0500324 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500325 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500326 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500327 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400328 "0453 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500329 "READ_CONFIG, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500330 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500331 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500332 mempool_free( pmb, phba->mbox_mem_pool);
333 return -EIO;
334 }
335
336 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
337 if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
338 phba->cfg_hba_queue_depth =
339 mb->un.varRdConfig.max_xri + 1;
340
341 phba->lmt = mb->un.varRdConfig.lmt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500342
343 /* Get the default values for Model Name and Description */
344 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
345
346 if ((phba->cfg_link_speed > LINK_SPEED_10G)
347 || ((phba->cfg_link_speed == LINK_SPEED_1G)
348 && !(phba->lmt & LMT_1Gb))
349 || ((phba->cfg_link_speed == LINK_SPEED_2G)
350 && !(phba->lmt & LMT_2Gb))
351 || ((phba->cfg_link_speed == LINK_SPEED_4G)
352 && !(phba->lmt & LMT_4Gb))
353 || ((phba->cfg_link_speed == LINK_SPEED_8G)
354 && !(phba->lmt & LMT_8Gb))
355 || ((phba->cfg_link_speed == LINK_SPEED_10G)
356 && !(phba->lmt & LMT_10Gb))) {
357 /* Reset link speed to auto */
James Smarted957682007-06-17 19:56:37 -0500358 lpfc_printf_log(phba, KERN_WARNING, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -0400359 "1302 Invalid speed for this board: "
dea31012005-04-17 16:05:31 -0500360 "Reset link speed to auto: x%x\n",
dea31012005-04-17 16:05:31 -0500361 phba->cfg_link_speed);
362 phba->cfg_link_speed = LINK_SPEED_AUTO;
363 }
364
James Smart2e0fef82007-06-17 19:56:36 -0500365 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500366
James Smart0b727fe2007-10-27 13:37:25 -0400367 /* Only process IOCBs on ELS ring till hba_state is READY */
James Smarta4bc3372006-12-02 13:34:16 -0500368 if (psli->ring[psli->extra_ring].cmdringaddr)
369 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500370 if (psli->ring[psli->fcp_ring].cmdringaddr)
371 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
372 if (psli->ring[psli->next_ring].cmdringaddr)
373 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
374
375 /* Post receive buffers for desired rings */
James Smarted957682007-06-17 19:56:37 -0500376 if (phba->sli_rev != 3)
377 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500378
379 /* Enable appropriate host interrupts */
James Smart2e0fef82007-06-17 19:56:36 -0500380 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500381 status = readl(phba->HCregaddr);
382 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
383 if (psli->num_rings > 0)
384 status |= HC_R0INT_ENA;
385 if (psli->num_rings > 1)
386 status |= HC_R1INT_ENA;
387 if (psli->num_rings > 2)
388 status |= HC_R2INT_ENA;
389 if (psli->num_rings > 3)
390 status |= HC_R3INT_ENA;
391
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500392 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
393 (phba->cfg_poll & DISABLE_FCP_RING_INT))
394 status &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
395
dea31012005-04-17 16:05:31 -0500396 writel(status, phba->HCregaddr);
397 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500398 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500399
400 /*
401 * Setup the ring 0 (els) timeout handler
402 */
403 timeout = phba->fc_ratov << 1;
James Smart2e0fef82007-06-17 19:56:36 -0500404 mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
James Smart858c9f62007-06-17 19:56:39 -0500405 mod_timer(&phba->hb_tmofunc, jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
406 phba->hb_outstanding = 0;
407 phba->last_completion_time = jiffies;
dea31012005-04-17 16:05:31 -0500408
409 lpfc_init_link(phba, pmb, phba->cfg_topology, phba->cfg_link_speed);
410 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500411 pmb->vport = vport;
James Smart8aee9182006-08-31 12:27:57 -0400412 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -0400413 lpfc_set_loopback_flag(phba);
James Smart8aee9182006-08-31 12:27:57 -0400414 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500415 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400416 "0454 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500417 "INIT_LINK, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500418 mb->mbxCommand, mb->mbxStatus);
419
420 /* Clear all interrupt enable conditions */
421 writel(0, phba->HCregaddr);
422 readl(phba->HCregaddr); /* flush */
423 /* Clear all pending interrupts */
424 writel(0xffffffff, phba->HAregaddr);
425 readl(phba->HAregaddr); /* flush */
426
James Smart2e0fef82007-06-17 19:56:36 -0500427 phba->link_state = LPFC_HBA_ERROR;
James Smart8aee9182006-08-31 12:27:57 -0400428 if (rc != MBX_BUSY)
429 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500430 return -EIO;
431 }
432 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400433 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
434 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
435 pmb->mbox_cmpl = lpfc_config_async_cmpl;
436 pmb->vport = phba->pport;
437 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500438
James Smart57127f12007-10-27 13:37:05 -0400439 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
440 lpfc_printf_log(phba,
441 KERN_ERR,
442 LOG_INIT,
443 "0456 Adapter failed to issue "
444 "ASYNCEVT_ENABLE mbox status x%x \n.",
445 rc);
446 mempool_free(pmb, phba->mbox_mem_pool);
447 }
James Smartce8b3ce2006-07-06 15:50:36 -0400448 return (0);
449}
450
dea31012005-04-17 16:05:31 -0500451/************************************************************************/
452/* */
453/* lpfc_hba_down_prep */
454/* This routine will do LPFC uninitialization before the */
455/* HBA is reset when bringing down the SLI Layer. This will be */
456/* initialized as a SLI layer callback routine. */
457/* This routine returns 0 on success. Any other return value */
458/* indicates an error. */
459/* */
460/************************************************************************/
461int
James Smart2e0fef82007-06-17 19:56:36 -0500462lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500463{
464 /* Disable interrupts */
465 writel(0, phba->HCregaddr);
466 readl(phba->HCregaddr); /* flush */
467
James Smart51ef4c22007-08-02 11:10:31 -0400468 lpfc_cleanup_discovery_resources(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -0500469 return 0;
dea31012005-04-17 16:05:31 -0500470}
471
472/************************************************************************/
473/* */
Jamie Wellnitz41415862006-02-28 19:25:27 -0500474/* lpfc_hba_down_post */
475/* This routine will do uninitialization after the HBA is reset */
476/* when bringing down the SLI Layer. */
477/* This routine returns 0 on success. Any other return value */
478/* indicates an error. */
479/* */
480/************************************************************************/
481int
James Smart2e0fef82007-06-17 19:56:36 -0500482lpfc_hba_down_post(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -0500483{
484 struct lpfc_sli *psli = &phba->sli;
485 struct lpfc_sli_ring *pring;
486 struct lpfc_dmabuf *mp, *next_mp;
487 int i;
488
James Smart92d7f7b2007-06-17 19:56:38 -0500489 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
490 lpfc_sli_hbqbuf_free_all(phba);
491 else {
492 /* Cleanup preposted buffers on the ELS ring */
493 pring = &psli->ring[LPFC_ELS_RING];
494 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
495 list_del(&mp->list);
496 pring->postbufq_cnt--;
497 lpfc_mbuf_free(phba, mp->virt, mp->phys);
498 kfree(mp);
499 }
Jamie Wellnitz41415862006-02-28 19:25:27 -0500500 }
501
502 for (i = 0; i < psli->num_rings; i++) {
503 pring = &psli->ring[i];
504 lpfc_sli_abort_iocb_ring(phba, pring);
505 }
506
507 return 0;
508}
509
James Smart858c9f62007-06-17 19:56:39 -0500510/* HBA heart beat timeout handler */
Adrian Bunka6ababd2007-11-05 18:07:33 +0100511static void
James Smart858c9f62007-06-17 19:56:39 -0500512lpfc_hb_timeout(unsigned long ptr)
513{
514 struct lpfc_hba *phba;
515 unsigned long iflag;
516
517 phba = (struct lpfc_hba *)ptr;
518 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
519 if (!(phba->pport->work_port_events & WORKER_HB_TMO))
520 phba->pport->work_port_events |= WORKER_HB_TMO;
521 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
522
523 if (phba->work_wait)
524 wake_up(phba->work_wait);
525 return;
526}
527
528static void
529lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
530{
531 unsigned long drvr_flag;
532
533 spin_lock_irqsave(&phba->hbalock, drvr_flag);
534 phba->hb_outstanding = 0;
535 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
536
537 mempool_free(pmboxq, phba->mbox_mem_pool);
538 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
539 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -0400540 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -0500541 mod_timer(&phba->hb_tmofunc,
542 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
543 return;
544}
545
546void
547lpfc_hb_timeout_handler(struct lpfc_hba *phba)
548{
549 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -0500550 struct lpfc_dmabuf *buf_ptr;
James Smart858c9f62007-06-17 19:56:39 -0500551 int retval;
552 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -0500553 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -0500554
555 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -0400556 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -0500557 (phba->pport->fc_flag & FC_OFFLINE_MODE))
558 return;
559
560 spin_lock_irq(&phba->pport->work_port_lock);
561 /* If the timer is already canceled do nothing */
562 if (!(phba->pport->work_port_events & WORKER_HB_TMO)) {
563 spin_unlock_irq(&phba->pport->work_port_lock);
564 return;
565 }
566
567 if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
568 jiffies)) {
569 spin_unlock_irq(&phba->pport->work_port_lock);
570 if (!phba->hb_outstanding)
571 mod_timer(&phba->hb_tmofunc,
572 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
573 else
574 mod_timer(&phba->hb_tmofunc,
575 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
576 return;
577 }
578 spin_unlock_irq(&phba->pport->work_port_lock);
579
James Smart0ff10d42008-01-11 01:52:36 -0500580 if (phba->elsbuf_cnt &&
581 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
582 spin_lock_irq(&phba->hbalock);
583 list_splice_init(&phba->elsbuf, &completions);
584 phba->elsbuf_cnt = 0;
585 phba->elsbuf_prev_cnt = 0;
586 spin_unlock_irq(&phba->hbalock);
587
588 while (!list_empty(&completions)) {
589 list_remove_head(&completions, buf_ptr,
590 struct lpfc_dmabuf, list);
591 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
592 kfree(buf_ptr);
593 }
594 }
595 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
596
James Smart858c9f62007-06-17 19:56:39 -0500597 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -0500598 if (phba->cfg_enable_hba_heartbeat) {
599 if (!phba->hb_outstanding) {
600 pmboxq = mempool_alloc(phba->mbox_mem_pool,GFP_KERNEL);
601 if (!pmboxq) {
602 mod_timer(&phba->hb_tmofunc,
603 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
604 return;
605 }
606
607 lpfc_heart_beat(phba, pmboxq);
608 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
609 pmboxq->vport = phba->pport;
610 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
611
612 if (retval != MBX_BUSY && retval != MBX_SUCCESS) {
613 mempool_free(pmboxq, phba->mbox_mem_pool);
614 mod_timer(&phba->hb_tmofunc,
615 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
616 return;
617 }
James Smart858c9f62007-06-17 19:56:39 -0500618 mod_timer(&phba->hb_tmofunc,
James Smart13815c82008-01-11 01:52:48 -0500619 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
620 phba->hb_outstanding = 1;
James Smart858c9f62007-06-17 19:56:39 -0500621 return;
James Smart13815c82008-01-11 01:52:48 -0500622 } else {
623 /*
624 * If heart beat timeout called with hb_outstanding set
625 * we need to take the HBA offline.
626 */
627 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
628 "0459 Adapter heartbeat failure, "
629 "taking this port offline.\n");
630
631 spin_lock_irq(&phba->hbalock);
632 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
633 spin_unlock_irq(&phba->hbalock);
634
635 lpfc_offline_prep(phba);
636 lpfc_offline(phba);
637 lpfc_unblock_mgmt_io(phba);
638 phba->link_state = LPFC_HBA_ERROR;
639 lpfc_hba_down_post(phba);
James Smart858c9f62007-06-17 19:56:39 -0500640 }
James Smart858c9f62007-06-17 19:56:39 -0500641 }
642}
643
Jamie Wellnitz41415862006-02-28 19:25:27 -0500644/************************************************************************/
645/* */
dea31012005-04-17 16:05:31 -0500646/* lpfc_handle_eratt */
647/* This routine will handle processing a Host Attention */
648/* Error Status event. This will be initialized */
649/* as a SLI layer callback routine. */
650/* */
651/************************************************************************/
652void
James Smart2e0fef82007-06-17 19:56:36 -0500653lpfc_handle_eratt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500654{
James Smart2e0fef82007-06-17 19:56:36 -0500655 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -0500656 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -0500657 struct lpfc_sli_ring *pring;
James Smart549e55c2007-08-02 11:09:51 -0400658 struct lpfc_vport **vports;
James Smartd2873e42006-08-18 17:46:43 -0400659 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -0400660 unsigned long temperature;
661 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -0500662 struct Scsi_Host *shost;
James Smart549e55c2007-08-02 11:09:51 -0400663 int i;
James Smart2e0fef82007-06-17 19:56:36 -0500664
Linas Vepstas8d63f372007-02-14 14:28:36 -0600665 /* If the pci channel is offline, ignore possible errors,
666 * since we cannot communicate with the pci card anyway. */
667 if (pci_channel_offline(phba->pcidev))
668 return;
James Smart13815c82008-01-11 01:52:48 -0500669 /* If resets are disabled then leave the HBA alone and return */
670 if (!phba->cfg_enable_hba_reset)
671 return;
dea31012005-04-17 16:05:31 -0500672
James Smartf5603512006-12-02 13:35:43 -0500673 if (phba->work_hs & HS_FFER6 ||
674 phba->work_hs & HS_FFER5) {
dea31012005-04-17 16:05:31 -0500675 /* Re-establishing Link */
676 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -0400677 "1301 Re-establishing Link "
dea31012005-04-17 16:05:31 -0500678 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400679 phba->work_hs,
dea31012005-04-17 16:05:31 -0500680 phba->work_status[0], phba->work_status[1]);
James Smart549e55c2007-08-02 11:09:51 -0400681 vports = lpfc_create_vport_work_array(phba);
682 if (vports != NULL)
683 for(i = 0;
684 i < LPFC_MAX_VPORTS && vports[i] != NULL;
685 i++){
686 shost = lpfc_shost_from_vport(vports[i]);
687 spin_lock_irq(shost->host_lock);
688 vports[i]->fc_flag |= FC_ESTABLISH_LINK;
689 spin_unlock_irq(shost->host_lock);
690 }
691 lpfc_destroy_vport_work_array(vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500692 spin_lock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -0500693 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -0500694 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500695
696 /*
697 * Firmware stops when it triggled erratt with HS_FFER6.
698 * That could cause the I/Os dropped by the firmware.
699 * Error iocb (I/O) on txcmplq and let the SCSI layer
700 * retry it after re-establishing link.
701 */
702 pring = &psli->ring[psli->fcp_ring];
703 lpfc_sli_abort_iocb_ring(phba, pring);
704
705
706 /*
707 * There was a firmware error. Take the hba offline and then
708 * attempt to restart it.
709 */
James Smart46fa3112007-04-25 09:51:45 -0400710 lpfc_offline_prep(phba);
dea31012005-04-17 16:05:31 -0500711 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500712 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -0500713 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
714 mod_timer(&phba->fc_estabtmo, jiffies + HZ * 60);
James Smart46fa3112007-04-25 09:51:45 -0400715 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -0500716 return;
717 }
James Smart46fa3112007-04-25 09:51:45 -0400718 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -0400719 } else if (phba->work_hs & HS_CRIT_TEMP) {
720 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
721 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
722 temp_event_data.event_code = LPFC_CRIT_TEMP;
723 temp_event_data.data = (uint32_t)temperature;
724
725 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
726 "0459 Adapter maximum temperature exceeded "
727 "(%ld), taking this port offline "
728 "Data: x%x x%x x%x\n",
729 temperature, phba->work_hs,
730 phba->work_status[0], phba->work_status[1]);
731
732 shost = lpfc_shost_from_vport(phba->pport);
733 fc_host_post_vendor_event(shost, fc_get_event_number(),
734 sizeof(temp_event_data),
735 (char *) &temp_event_data,
736 SCSI_NL_VID_TYPE_PCI
737 | PCI_VENDOR_ID_EMULEX);
738
James Smart7af67052007-10-27 13:38:11 -0400739 spin_lock_irq(&phba->hbalock);
James Smart57127f12007-10-27 13:37:05 -0400740 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
James Smart7af67052007-10-27 13:38:11 -0400741 phba->over_temp_state = HBA_OVER_TEMP;
742 spin_unlock_irq(&phba->hbalock);
James Smart57127f12007-10-27 13:37:05 -0400743 lpfc_offline_prep(phba);
744 lpfc_offline(phba);
745 lpfc_unblock_mgmt_io(phba);
746 phba->link_state = LPFC_HBA_ERROR;
747 lpfc_hba_down_post(phba);
748
dea31012005-04-17 16:05:31 -0500749 } else {
750 /* The if clause above forces this code path when the status
751 * failure is a value other than FFER6. Do not call the offline
752 * twice. This is the adapter hardware error path.
753 */
754 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400755 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -0500756 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400757 phba->work_hs,
dea31012005-04-17 16:05:31 -0500758 phba->work_status[0], phba->work_status[1]);
759
James Smartd2873e42006-08-18 17:46:43 -0400760 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -0500761 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -0500762 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -0400763 sizeof(event_data), (char *) &event_data,
764 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
765
James Smart92d7f7b2007-06-17 19:56:38 -0500766 spin_lock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -0500767 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -0500768 spin_unlock_irq(&phba->hbalock);
James Smart46fa3112007-04-25 09:51:45 -0400769 lpfc_offline_prep(phba);
dea31012005-04-17 16:05:31 -0500770 lpfc_offline(phba);
James Smart46fa3112007-04-25 09:51:45 -0400771 lpfc_unblock_mgmt_io(phba);
James Smart2e0fef82007-06-17 19:56:36 -0500772 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -0500773 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -0500774 }
775}
776
777/************************************************************************/
778/* */
779/* lpfc_handle_latt */
780/* This routine will handle processing a Host Attention */
781/* Link Status event. This will be initialized */
782/* as a SLI layer callback routine. */
783/* */
784/************************************************************************/
785void
James Smart2e0fef82007-06-17 19:56:36 -0500786lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500787{
James Smart2e0fef82007-06-17 19:56:36 -0500788 struct lpfc_vport *vport = phba->pport;
789 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -0500790 LPFC_MBOXQ_t *pmb;
791 volatile uint32_t control;
792 struct lpfc_dmabuf *mp;
793 int rc = -ENOMEM;
794
795 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
796 if (!pmb)
797 goto lpfc_handle_latt_err_exit;
798
799 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
800 if (!mp)
801 goto lpfc_handle_latt_free_pmb;
802
803 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
804 if (!mp->virt)
805 goto lpfc_handle_latt_free_mp;
806
807 rc = -EIO;
808
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -0500809 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -0400810 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -0500811
812 psli->slistat.link_event++;
813 lpfc_read_la(phba, pmb, mp);
814 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_la;
James Smart2e0fef82007-06-17 19:56:36 -0500815 pmb->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -0400816 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500817 if (rc == MBX_NOT_FINISHED)
James Smart14691152006-12-02 13:34:28 -0500818 goto lpfc_handle_latt_free_mbuf;
dea31012005-04-17 16:05:31 -0500819
820 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -0500821 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500822 writel(HA_LATT, phba->HAregaddr);
823 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500824 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500825
826 return;
827
James Smart14691152006-12-02 13:34:28 -0500828lpfc_handle_latt_free_mbuf:
829 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -0500830lpfc_handle_latt_free_mp:
831 kfree(mp);
832lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -0400833 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500834lpfc_handle_latt_err_exit:
835 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -0500836 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500837 psli->sli_flag |= LPFC_PROCESS_LA;
838 control = readl(phba->HCregaddr);
839 control |= HC_LAINT_ENA;
840 writel(control, phba->HCregaddr);
841 readl(phba->HCregaddr); /* flush */
842
843 /* Clear Link Attention in HA REG */
844 writel(HA_LATT, phba->HAregaddr);
845 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500846 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500847 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -0500848 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500849
850 /* The other case is an error from issue_mbox */
851 if (rc == -ENOMEM)
James Smarted957682007-06-17 19:56:37 -0500852 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
James Smarte8b62012007-08-02 11:10:09 -0400853 "0300 READ_LA: no buffers\n");
dea31012005-04-17 16:05:31 -0500854
855 return;
856}
857
858/************************************************************************/
859/* */
860/* lpfc_parse_vpd */
861/* This routine will parse the VPD data */
862/* */
863/************************************************************************/
864static int
James Smart2e0fef82007-06-17 19:56:36 -0500865lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -0500866{
867 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -0500868 int Length;
dea31012005-04-17 16:05:31 -0500869 int i, j;
870 int finished = 0;
871 int index = 0;
872
873 if (!vpd)
874 return 0;
875
876 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -0500877 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400878 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -0500879 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
880 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500881 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -0500882 switch (vpd[index]) {
883 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500884 case 0x91:
dea31012005-04-17 16:05:31 -0500885 index += 1;
886 lenlo = vpd[index];
887 index += 1;
888 lenhi = vpd[index];
889 index += 1;
890 i = ((((unsigned short)lenhi) << 8) + lenlo);
891 index += i;
892 break;
893 case 0x90:
894 index += 1;
895 lenlo = vpd[index];
896 index += 1;
897 lenhi = vpd[index];
898 index += 1;
899 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500900 if (Length > len - index)
901 Length = len - index;
dea31012005-04-17 16:05:31 -0500902 while (Length > 0) {
903 /* Look for Serial Number */
904 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
905 index += 2;
906 i = vpd[index];
907 index += 1;
908 j = 0;
909 Length -= (3+i);
910 while(i--) {
911 phba->SerialNumber[j++] = vpd[index++];
912 if (j == 31)
913 break;
914 }
915 phba->SerialNumber[j] = 0;
916 continue;
917 }
918 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
919 phba->vpd_flag |= VPD_MODEL_DESC;
920 index += 2;
921 i = vpd[index];
922 index += 1;
923 j = 0;
924 Length -= (3+i);
925 while(i--) {
926 phba->ModelDesc[j++] = vpd[index++];
927 if (j == 255)
928 break;
929 }
930 phba->ModelDesc[j] = 0;
931 continue;
932 }
933 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
934 phba->vpd_flag |= VPD_MODEL_NAME;
935 index += 2;
936 i = vpd[index];
937 index += 1;
938 j = 0;
939 Length -= (3+i);
940 while(i--) {
941 phba->ModelName[j++] = vpd[index++];
942 if (j == 79)
943 break;
944 }
945 phba->ModelName[j] = 0;
946 continue;
947 }
948 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
949 phba->vpd_flag |= VPD_PROGRAM_TYPE;
950 index += 2;
951 i = vpd[index];
952 index += 1;
953 j = 0;
954 Length -= (3+i);
955 while(i--) {
956 phba->ProgramType[j++] = vpd[index++];
957 if (j == 255)
958 break;
959 }
960 phba->ProgramType[j] = 0;
961 continue;
962 }
963 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
964 phba->vpd_flag |= VPD_PORT;
965 index += 2;
966 i = vpd[index];
967 index += 1;
968 j = 0;
969 Length -= (3+i);
970 while(i--) {
971 phba->Port[j++] = vpd[index++];
972 if (j == 19)
973 break;
974 }
975 phba->Port[j] = 0;
976 continue;
977 }
978 else {
979 index += 2;
980 i = vpd[index];
981 index += 1;
982 index += i;
983 Length -= (3 + i);
984 }
985 }
986 finished = 0;
987 break;
988 case 0x78:
989 finished = 1;
990 break;
991 default:
992 index ++;
993 break;
994 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500995 }
dea31012005-04-17 16:05:31 -0500996
997 return(1);
998}
999
1000static void
James Smart2e0fef82007-06-17 19:56:36 -05001001lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05001002{
1003 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05001004 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001005 int max_speed;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001006 struct {
1007 char * name;
1008 int max_speed;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001009 char * bus;
James Smart18a3b592006-12-02 13:35:08 -05001010 } m = {"<Unknown>", 0, ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001011
1012 if (mdp && mdp[0] != '\0'
1013 && descp && descp[0] != '\0')
1014 return;
1015
1016 if (phba->lmt & LMT_10Gb)
1017 max_speed = 10;
1018 else if (phba->lmt & LMT_8Gb)
1019 max_speed = 8;
1020 else if (phba->lmt & LMT_4Gb)
1021 max_speed = 4;
1022 else if (phba->lmt & LMT_2Gb)
1023 max_speed = 2;
1024 else
1025 max_speed = 1;
dea31012005-04-17 16:05:31 -05001026
1027 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05001028
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001029 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001030 case PCI_DEVICE_ID_FIREFLY:
James Smart18a3b592006-12-02 13:35:08 -05001031 m = (typeof(m)){"LP6000", max_speed, "PCI"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001032 break;
dea31012005-04-17 16:05:31 -05001033 case PCI_DEVICE_ID_SUPERFLY:
1034 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smart18a3b592006-12-02 13:35:08 -05001035 m = (typeof(m)){"LP7000", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001036 else
James Smart18a3b592006-12-02 13:35:08 -05001037 m = (typeof(m)){"LP7000E", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001038 break;
1039 case PCI_DEVICE_ID_DRAGONFLY:
James Smart18a3b592006-12-02 13:35:08 -05001040 m = (typeof(m)){"LP8000", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001041 break;
1042 case PCI_DEVICE_ID_CENTAUR:
1043 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smart18a3b592006-12-02 13:35:08 -05001044 m = (typeof(m)){"LP9002", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001045 else
James Smart18a3b592006-12-02 13:35:08 -05001046 m = (typeof(m)){"LP9000", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001047 break;
1048 case PCI_DEVICE_ID_RFLY:
James Smart18a3b592006-12-02 13:35:08 -05001049 m = (typeof(m)){"LP952", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001050 break;
1051 case PCI_DEVICE_ID_PEGASUS:
James Smart18a3b592006-12-02 13:35:08 -05001052 m = (typeof(m)){"LP9802", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001053 break;
1054 case PCI_DEVICE_ID_THOR:
James Smart18a3b592006-12-02 13:35:08 -05001055 m = (typeof(m)){"LP10000", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001056 break;
1057 case PCI_DEVICE_ID_VIPER:
James Smart18a3b592006-12-02 13:35:08 -05001058 m = (typeof(m)){"LPX1000", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001059 break;
1060 case PCI_DEVICE_ID_PFLY:
James Smart18a3b592006-12-02 13:35:08 -05001061 m = (typeof(m)){"LP982", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001062 break;
1063 case PCI_DEVICE_ID_TFLY:
James Smart18a3b592006-12-02 13:35:08 -05001064 m = (typeof(m)){"LP1050", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001065 break;
1066 case PCI_DEVICE_ID_HELIOS:
James Smart18a3b592006-12-02 13:35:08 -05001067 m = (typeof(m)){"LP11000", max_speed, "PCI-X2"};
dea31012005-04-17 16:05:31 -05001068 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001069 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smart18a3b592006-12-02 13:35:08 -05001070 m = (typeof(m)){"LP11000-SP", max_speed, "PCI-X2"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001071 break;
1072 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smart18a3b592006-12-02 13:35:08 -05001073 m = (typeof(m)){"LP11002-SP", max_speed, "PCI-X2"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001074 break;
1075 case PCI_DEVICE_ID_NEPTUNE:
James Smart18a3b592006-12-02 13:35:08 -05001076 m = (typeof(m)){"LPe1000", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001077 break;
1078 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smart18a3b592006-12-02 13:35:08 -05001079 m = (typeof(m)){"LPe1000-SP", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001080 break;
1081 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smart18a3b592006-12-02 13:35:08 -05001082 m = (typeof(m)){"LPe1002-SP", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001083 break;
dea31012005-04-17 16:05:31 -05001084 case PCI_DEVICE_ID_BMID:
James Smart18a3b592006-12-02 13:35:08 -05001085 m = (typeof(m)){"LP1150", max_speed, "PCI-X2"};
dea31012005-04-17 16:05:31 -05001086 break;
1087 case PCI_DEVICE_ID_BSMB:
James Smart18a3b592006-12-02 13:35:08 -05001088 m = (typeof(m)){"LP111", max_speed, "PCI-X2"};
dea31012005-04-17 16:05:31 -05001089 break;
1090 case PCI_DEVICE_ID_ZEPHYR:
James Smart18a3b592006-12-02 13:35:08 -05001091 m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
dea31012005-04-17 16:05:31 -05001092 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001093 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smart18a3b592006-12-02 13:35:08 -05001094 m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001095 break;
1096 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smart18a3b592006-12-02 13:35:08 -05001097 m = (typeof(m)){"LPe11002-SP", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001098 break;
dea31012005-04-17 16:05:31 -05001099 case PCI_DEVICE_ID_ZMID:
James Smart18a3b592006-12-02 13:35:08 -05001100 m = (typeof(m)){"LPe1150", max_speed, "PCIe"};
dea31012005-04-17 16:05:31 -05001101 break;
1102 case PCI_DEVICE_ID_ZSMB:
James Smart18a3b592006-12-02 13:35:08 -05001103 m = (typeof(m)){"LPe111", max_speed, "PCIe"};
dea31012005-04-17 16:05:31 -05001104 break;
1105 case PCI_DEVICE_ID_LP101:
James Smart18a3b592006-12-02 13:35:08 -05001106 m = (typeof(m)){"LP101", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001107 break;
1108 case PCI_DEVICE_ID_LP10000S:
James Smart18a3b592006-12-02 13:35:08 -05001109 m = (typeof(m)){"LP10000-S", max_speed, "PCI"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001110 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001111 case PCI_DEVICE_ID_LP11000S:
James Smart18a3b592006-12-02 13:35:08 -05001112 m = (typeof(m)){"LP11000-S", max_speed,
1113 "PCI-X2"};
1114 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001115 case PCI_DEVICE_ID_LPE11000S:
James Smart18a3b592006-12-02 13:35:08 -05001116 m = (typeof(m)){"LPe11000-S", max_speed,
1117 "PCIe"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05001118 break;
James Smartb87eab32007-04-25 09:53:28 -04001119 case PCI_DEVICE_ID_SAT:
1120 m = (typeof(m)){"LPe12000", max_speed, "PCIe"};
1121 break;
1122 case PCI_DEVICE_ID_SAT_MID:
1123 m = (typeof(m)){"LPe1250", max_speed, "PCIe"};
1124 break;
1125 case PCI_DEVICE_ID_SAT_SMB:
1126 m = (typeof(m)){"LPe121", max_speed, "PCIe"};
1127 break;
1128 case PCI_DEVICE_ID_SAT_DCSP:
1129 m = (typeof(m)){"LPe12002-SP", max_speed, "PCIe"};
1130 break;
1131 case PCI_DEVICE_ID_SAT_SCSP:
1132 m = (typeof(m)){"LPe12000-SP", max_speed, "PCIe"};
1133 break;
1134 case PCI_DEVICE_ID_SAT_S:
1135 m = (typeof(m)){"LPe12000-S", max_speed, "PCIe"};
1136 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05001137 default:
Randy Dunlap041976f2006-06-25 01:58:51 -07001138 m = (typeof(m)){ NULL };
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001139 break;
dea31012005-04-17 16:05:31 -05001140 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001141
1142 if (mdp && mdp[0] == '\0')
1143 snprintf(mdp, 79,"%s", m.name);
1144 if (descp && descp[0] == '\0')
1145 snprintf(descp, 255,
James Smart18a3b592006-12-02 13:35:08 -05001146 "Emulex %s %dGb %s Fibre Channel Adapter",
1147 m.name, m.max_speed, m.bus);
dea31012005-04-17 16:05:31 -05001148}
1149
1150/**************************************************/
1151/* lpfc_post_buffer */
1152/* */
1153/* This routine will post count buffers to the */
1154/* ring with the QUE_RING_BUF_CN command. This */
1155/* allows 3 buffers / command to be posted. */
1156/* Returns the number of buffers NOT posted. */
1157/**************************************************/
1158int
James Smart2e0fef82007-06-17 19:56:36 -05001159lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt,
dea31012005-04-17 16:05:31 -05001160 int type)
1161{
1162 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001163 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05001164 struct lpfc_dmabuf *mp1, *mp2;
1165
1166 cnt += pring->missbufcnt;
1167
1168 /* While there are buffers to post */
1169 while (cnt > 0) {
1170 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001171 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05001172 if (iocb == NULL) {
1173 pring->missbufcnt = cnt;
1174 return cnt;
1175 }
dea31012005-04-17 16:05:31 -05001176 icmd = &iocb->iocb;
1177
1178 /* 2 buffers can be posted per command */
1179 /* Allocate buffer to post */
1180 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1181 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04001182 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
1183 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001184 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05001185 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001186 pring->missbufcnt = cnt;
1187 return cnt;
1188 }
1189
1190 INIT_LIST_HEAD(&mp1->list);
1191 /* Allocate buffer to post */
1192 if (cnt > 1) {
1193 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1194 if (mp2)
1195 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
1196 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04001197 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001198 kfree(mp2);
dea31012005-04-17 16:05:31 -05001199 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1200 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05001201 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001202 pring->missbufcnt = cnt;
1203 return cnt;
1204 }
1205
1206 INIT_LIST_HEAD(&mp2->list);
1207 } else {
1208 mp2 = NULL;
1209 }
1210
1211 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
1212 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
1213 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
1214 icmd->ulpBdeCount = 1;
1215 cnt--;
1216 if (mp2) {
1217 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
1218 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
1219 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
1220 cnt--;
1221 icmd->ulpBdeCount = 2;
1222 }
1223
1224 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
1225 icmd->ulpLe = 1;
1226
dea31012005-04-17 16:05:31 -05001227 if (lpfc_sli_issue_iocb(phba, pring, iocb, 0) == IOCB_ERROR) {
1228 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1229 kfree(mp1);
1230 cnt++;
1231 if (mp2) {
1232 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
1233 kfree(mp2);
1234 cnt++;
1235 }
James Bottomley604a3e32005-10-29 10:28:33 -05001236 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001237 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05001238 return cnt;
1239 }
dea31012005-04-17 16:05:31 -05001240 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05001241 if (mp2)
dea31012005-04-17 16:05:31 -05001242 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05001243 }
1244 pring->missbufcnt = 0;
1245 return 0;
1246}
1247
1248/************************************************************************/
1249/* */
1250/* lpfc_post_rcv_buf */
1251/* This routine post initial rcv buffers to the configured rings */
1252/* */
1253/************************************************************************/
1254static int
James Smart2e0fef82007-06-17 19:56:36 -05001255lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001256{
1257 struct lpfc_sli *psli = &phba->sli;
1258
1259 /* Ring 0, ELS / CT buffers */
1260 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0, 1);
1261 /* Ring 2 - FCP no buffers needed */
1262
1263 return 0;
1264}
1265
1266#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
1267
1268/************************************************************************/
1269/* */
1270/* lpfc_sha_init */
1271/* */
1272/************************************************************************/
1273static void
1274lpfc_sha_init(uint32_t * HashResultPointer)
1275{
1276 HashResultPointer[0] = 0x67452301;
1277 HashResultPointer[1] = 0xEFCDAB89;
1278 HashResultPointer[2] = 0x98BADCFE;
1279 HashResultPointer[3] = 0x10325476;
1280 HashResultPointer[4] = 0xC3D2E1F0;
1281}
1282
1283/************************************************************************/
1284/* */
1285/* lpfc_sha_iterate */
1286/* */
1287/************************************************************************/
1288static void
1289lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
1290{
1291 int t;
1292 uint32_t TEMP;
1293 uint32_t A, B, C, D, E;
1294 t = 16;
1295 do {
1296 HashWorkingPointer[t] =
1297 S(1,
1298 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
1299 8] ^
1300 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
1301 } while (++t <= 79);
1302 t = 0;
1303 A = HashResultPointer[0];
1304 B = HashResultPointer[1];
1305 C = HashResultPointer[2];
1306 D = HashResultPointer[3];
1307 E = HashResultPointer[4];
1308
1309 do {
1310 if (t < 20) {
1311 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
1312 } else if (t < 40) {
1313 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
1314 } else if (t < 60) {
1315 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
1316 } else {
1317 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
1318 }
1319 TEMP += S(5, A) + E + HashWorkingPointer[t];
1320 E = D;
1321 D = C;
1322 C = S(30, B);
1323 B = A;
1324 A = TEMP;
1325 } while (++t <= 79);
1326
1327 HashResultPointer[0] += A;
1328 HashResultPointer[1] += B;
1329 HashResultPointer[2] += C;
1330 HashResultPointer[3] += D;
1331 HashResultPointer[4] += E;
1332
1333}
1334
1335/************************************************************************/
1336/* */
1337/* lpfc_challenge_key */
1338/* */
1339/************************************************************************/
1340static void
1341lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
1342{
1343 *HashWorking = (*RandomChallenge ^ *HashWorking);
1344}
1345
1346/************************************************************************/
1347/* */
1348/* lpfc_hba_init */
1349/* */
1350/************************************************************************/
1351void
1352lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
1353{
1354 int t;
1355 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05001356 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05001357
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02001358 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05001359 if (!HashWorking)
1360 return;
1361
dea31012005-04-17 16:05:31 -05001362 HashWorking[0] = HashWorking[78] = *pwwnn++;
1363 HashWorking[1] = HashWorking[79] = *pwwnn;
1364
1365 for (t = 0; t < 7; t++)
1366 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
1367
1368 lpfc_sha_init(hbainit);
1369 lpfc_sha_iterate(hbainit, HashWorking);
1370 kfree(HashWorking);
1371}
1372
James Smart87af33f2007-10-27 13:37:43 -04001373void
James Smart2e0fef82007-06-17 19:56:36 -05001374lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05001375{
James Smart87af33f2007-10-27 13:37:43 -04001376 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001377 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04001378 int i = 0;
dea31012005-04-17 16:05:31 -05001379
James Smart87af33f2007-10-27 13:37:43 -04001380 if (phba->link_state > LPFC_LINK_DOWN)
1381 lpfc_port_link_failure(vport);
1382
1383 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
1384 if (ndlp->nlp_type & NLP_FABRIC)
1385 lpfc_disc_state_machine(vport, ndlp, NULL,
1386 NLP_EVT_DEVICE_RECOVERY);
1387 lpfc_disc_state_machine(vport, ndlp, NULL,
1388 NLP_EVT_DEVICE_RM);
1389 }
1390
James Smarta8adb832007-10-27 13:37:53 -04001391 /* At this point, ALL ndlp's should be gone
1392 * because of the previous NLP_EVT_DEVICE_RM.
1393 * Lets wait for this to happen, if needed.
1394 */
James Smart87af33f2007-10-27 13:37:43 -04001395 while (!list_empty(&vport->fc_nodes)) {
1396
James Smarta8adb832007-10-27 13:37:53 -04001397 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04001398 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04001399 "0233 Nodelist not empty\n");
1400 break;
James Smart87af33f2007-10-27 13:37:43 -04001401 }
James Smarta8adb832007-10-27 13:37:53 -04001402
1403 /* Wait for any activity on ndlps to settle */
1404 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04001405 }
dea31012005-04-17 16:05:31 -05001406 return;
1407}
1408
1409static void
1410lpfc_establish_link_tmo(unsigned long ptr)
1411{
James Smart92d7f7b2007-06-17 19:56:38 -05001412 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James Smart549e55c2007-08-02 11:09:51 -04001413 struct lpfc_vport **vports;
dea31012005-04-17 16:05:31 -05001414 unsigned long iflag;
James Smart549e55c2007-08-02 11:09:51 -04001415 int i;
dea31012005-04-17 16:05:31 -05001416
dea31012005-04-17 16:05:31 -05001417 /* Re-establishing Link, timer expired */
1418 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001419 "1300 Re-establishing Link, timer expired "
dea31012005-04-17 16:05:31 -05001420 "Data: x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001421 phba->pport->fc_flag, phba->pport->port_state);
James Smart549e55c2007-08-02 11:09:51 -04001422 vports = lpfc_create_vport_work_array(phba);
1423 if (vports != NULL)
1424 for(i = 0; i < LPFC_MAX_VPORTS && vports[i] != NULL; i++) {
1425 struct Scsi_Host *shost;
1426 shost = lpfc_shost_from_vport(vports[i]);
1427 spin_lock_irqsave(shost->host_lock, iflag);
1428 vports[i]->fc_flag &= ~FC_ESTABLISH_LINK;
1429 spin_unlock_irqrestore(shost->host_lock, iflag);
1430 }
1431 lpfc_destroy_vport_work_array(vports);
dea31012005-04-17 16:05:31 -05001432}
1433
James Smart92d7f7b2007-06-17 19:56:38 -05001434void
1435lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05001436{
James Smart92d7f7b2007-06-17 19:56:38 -05001437 del_timer_sync(&vport->els_tmofunc);
1438 del_timer_sync(&vport->fc_fdmitmo);
1439 lpfc_can_disctmo(vport);
1440 return;
dea31012005-04-17 16:05:31 -05001441}
1442
James Smart2e0fef82007-06-17 19:56:36 -05001443static void
James Smart92d7f7b2007-06-17 19:56:38 -05001444lpfc_stop_phba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001445{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001446 del_timer_sync(&phba->fcp_poll_timer);
dea31012005-04-17 16:05:31 -05001447 del_timer_sync(&phba->fc_estabtmo);
James Smart51ef4c22007-08-02 11:10:31 -04001448 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05001449 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05001450 del_timer_sync(&phba->fabric_block_timer);
James Smart858c9f62007-06-17 19:56:39 -05001451 phba->hb_outstanding = 0;
1452 del_timer_sync(&phba->hb_tmofunc);
James Smart2e0fef82007-06-17 19:56:36 -05001453 return;
dea31012005-04-17 16:05:31 -05001454}
1455
Adrian Bunka6ababd2007-11-05 18:07:33 +01001456static void
1457lpfc_block_mgmt_io(struct lpfc_hba * phba)
1458{
1459 unsigned long iflag;
1460
1461 spin_lock_irqsave(&phba->hbalock, iflag);
1462 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
1463 spin_unlock_irqrestore(&phba->hbalock, iflag);
1464}
1465
dea31012005-04-17 16:05:31 -05001466int
James Smart2e0fef82007-06-17 19:56:36 -05001467lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001468{
James Smart2e0fef82007-06-17 19:56:36 -05001469 struct lpfc_vport *vport = phba->pport;
James Smart549e55c2007-08-02 11:09:51 -04001470 struct lpfc_vport **vports;
1471 int i;
James Smart2e0fef82007-06-17 19:56:36 -05001472
dea31012005-04-17 16:05:31 -05001473 if (!phba)
1474 return 0;
1475
James Smart2e0fef82007-06-17 19:56:36 -05001476 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05001477 return 0;
1478
James Smarted957682007-06-17 19:56:37 -05001479 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001480 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05001481
James Smart46fa3112007-04-25 09:51:45 -04001482 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001483
James Smart46fa3112007-04-25 09:51:45 -04001484 if (!lpfc_sli_queue_setup(phba)) {
1485 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001486 return 1;
James Smart46fa3112007-04-25 09:51:45 -04001487 }
1488
1489 if (lpfc_sli_hba_setup(phba)) { /* Initialize the HBA */
1490 lpfc_unblock_mgmt_io(phba);
1491 return 1;
1492 }
dea31012005-04-17 16:05:31 -05001493
James Smart549e55c2007-08-02 11:09:51 -04001494 vports = lpfc_create_vport_work_array(phba);
1495 if (vports != NULL)
1496 for(i = 0; i < LPFC_MAX_VPORTS && vports[i] != NULL; i++) {
1497 struct Scsi_Host *shost;
1498 shost = lpfc_shost_from_vport(vports[i]);
1499 spin_lock_irq(shost->host_lock);
1500 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
1501 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
1502 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
1503 spin_unlock_irq(shost->host_lock);
1504 }
1505 lpfc_destroy_vport_work_array(vports);
dea31012005-04-17 16:05:31 -05001506
James Smart46fa3112007-04-25 09:51:45 -04001507 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001508 return 0;
1509}
1510
James Smart46fa3112007-04-25 09:51:45 -04001511void
James Smart46fa3112007-04-25 09:51:45 -04001512lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
1513{
1514 unsigned long iflag;
1515
James Smart2e0fef82007-06-17 19:56:36 -05001516 spin_lock_irqsave(&phba->hbalock, iflag);
1517 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
1518 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04001519}
1520
1521void
1522lpfc_offline_prep(struct lpfc_hba * phba)
1523{
James Smart2e0fef82007-06-17 19:56:36 -05001524 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04001525 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04001526 struct lpfc_vport **vports;
1527 int i;
dea31012005-04-17 16:05:31 -05001528
James Smart2e0fef82007-06-17 19:56:36 -05001529 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04001530 return;
dea31012005-04-17 16:05:31 -05001531
James Smart46fa3112007-04-25 09:51:45 -04001532 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001533
1534 lpfc_linkdown(phba);
1535
James Smart87af33f2007-10-27 13:37:43 -04001536 /* Issue an unreg_login to all nodes on all vports */
1537 vports = lpfc_create_vport_work_array(phba);
1538 if (vports != NULL) {
1539 for(i = 0; i < LPFC_MAX_VPORTS && vports[i] != NULL; i++) {
1540 struct Scsi_Host *shost;
1541
James Smarta8adb832007-10-27 13:37:53 -04001542 if (vports[i]->load_flag & FC_UNLOADING)
1543 continue;
James Smart87af33f2007-10-27 13:37:43 -04001544 shost = lpfc_shost_from_vport(vports[i]);
1545 list_for_each_entry_safe(ndlp, next_ndlp,
1546 &vports[i]->fc_nodes,
1547 nlp_listp) {
1548 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
1549 continue;
1550 if (ndlp->nlp_type & NLP_FABRIC) {
1551 lpfc_disc_state_machine(vports[i], ndlp,
1552 NULL, NLP_EVT_DEVICE_RECOVERY);
1553 lpfc_disc_state_machine(vports[i], ndlp,
1554 NULL, NLP_EVT_DEVICE_RM);
1555 }
1556 spin_lock_irq(shost->host_lock);
1557 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
1558 spin_unlock_irq(shost->host_lock);
1559 lpfc_unreg_rpi(vports[i], ndlp);
1560 }
1561 }
1562 }
1563 lpfc_destroy_vport_work_array(vports);
dea31012005-04-17 16:05:31 -05001564
James Smart46fa3112007-04-25 09:51:45 -04001565 lpfc_sli_flush_mbox_queue(phba);
1566}
1567
1568void
James Smart2e0fef82007-06-17 19:56:36 -05001569lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04001570{
James Smart549e55c2007-08-02 11:09:51 -04001571 struct Scsi_Host *shost;
1572 struct lpfc_vport **vports;
1573 int i;
James Smart46fa3112007-04-25 09:51:45 -04001574
James Smart549e55c2007-08-02 11:09:51 -04001575 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04001576 return;
James Smart688a8862006-07-06 15:49:56 -04001577
dea31012005-04-17 16:05:31 -05001578 /* stop all timers associated with this hba */
James Smart92d7f7b2007-06-17 19:56:38 -05001579 lpfc_stop_phba_timers(phba);
James Smart51ef4c22007-08-02 11:10:31 -04001580 vports = lpfc_create_vport_work_array(phba);
1581 if (vports != NULL)
1582 for(i = 0; i < LPFC_MAX_VPORTS && vports[i] != NULL; i++)
1583 lpfc_stop_vport_timers(vports[i]);
1584 lpfc_destroy_vport_work_array(vports);
James Smart92d7f7b2007-06-17 19:56:38 -05001585 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001586 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05001587 /* Bring down the SLI Layer and cleanup. The HBA is offline
1588 now. */
1589 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001590 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04001591 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001592 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04001593 vports = lpfc_create_vport_work_array(phba);
1594 if (vports != NULL)
1595 for(i = 0; i < LPFC_MAX_VPORTS && vports[i] != NULL; i++) {
1596 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04001597 spin_lock_irq(shost->host_lock);
1598 vports[i]->work_port_events = 0;
1599 vports[i]->fc_flag |= FC_OFFLINE_MODE;
1600 spin_unlock_irq(shost->host_lock);
1601 }
1602 lpfc_destroy_vport_work_array(vports);
dea31012005-04-17 16:05:31 -05001603}
1604
1605/******************************************************************************
1606* Function name: lpfc_scsi_free
1607*
1608* Description: Called from lpfc_pci_remove_one free internal driver resources
1609*
1610******************************************************************************/
1611static int
James Smart2e0fef82007-06-17 19:56:36 -05001612lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001613{
1614 struct lpfc_scsi_buf *sb, *sb_next;
1615 struct lpfc_iocbq *io, *io_next;
1616
James Smart2e0fef82007-06-17 19:56:36 -05001617 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001618 /* Release all the lpfc_scsi_bufs maintained by this host. */
1619 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
1620 list_del(&sb->list);
1621 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05001622 sb->dma_handle);
dea31012005-04-17 16:05:31 -05001623 kfree(sb);
1624 phba->total_scsi_bufs--;
1625 }
1626
1627 /* Release all the lpfc_iocbq entries maintained by this host. */
1628 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
1629 list_del(&io->list);
1630 kfree(io);
1631 phba->total_iocbq_bufs--;
1632 }
1633
James Smart2e0fef82007-06-17 19:56:36 -05001634 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001635
1636 return 0;
1637}
1638
James Smart2e0fef82007-06-17 19:56:36 -05001639struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04001640lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04001641{
James Smart2e0fef82007-06-17 19:56:36 -05001642 struct lpfc_vport *vport;
1643 struct Scsi_Host *shost;
1644 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04001645
James Smart3de2a652007-08-02 11:09:59 -04001646 if (dev != &phba->pcidev->dev)
1647 shost = scsi_host_alloc(&lpfc_vport_template,
1648 sizeof(struct lpfc_vport));
1649 else
1650 shost = scsi_host_alloc(&lpfc_template,
1651 sizeof(struct lpfc_vport));
James Smart2e0fef82007-06-17 19:56:36 -05001652 if (!shost)
1653 goto out;
James Smart47a86172007-04-25 09:53:22 -04001654
James Smart2e0fef82007-06-17 19:56:36 -05001655 vport = (struct lpfc_vport *) shost->hostdata;
1656 vport->phba = phba;
James Smart47a86172007-04-25 09:53:22 -04001657
James Smart2e0fef82007-06-17 19:56:36 -05001658 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05001659 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart47a86172007-04-25 09:53:22 -04001660
James Smart3de2a652007-08-02 11:09:59 -04001661 lpfc_get_vport_cfgparam(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001662 shost->unique_id = instance;
1663 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04001664 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05001665 shost->this_id = -1;
1666 shost->max_cmd_len = 16;
James Smart47a86172007-04-25 09:53:22 -04001667 /*
James Smart2e0fef82007-06-17 19:56:36 -05001668 * Set initial can_queue value since 0 is no longer supported and
1669 * scsi_add_host will fail. This will be adjusted later based on the
1670 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04001671 */
James Smart2e0fef82007-06-17 19:56:36 -05001672 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04001673 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05001674 shost->transportt = lpfc_vport_transport_template;
1675 vport->port_type = LPFC_NPIV_PORT;
1676 } else {
1677 shost->transportt = lpfc_transport_template;
1678 vport->port_type = LPFC_PHYSICAL_PORT;
1679 }
James Smart47a86172007-04-25 09:53:22 -04001680
James Smart2e0fef82007-06-17 19:56:36 -05001681 /* Initialize all internally managed lists. */
1682 INIT_LIST_HEAD(&vport->fc_nodes);
1683 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04001684
James Smart2e0fef82007-06-17 19:56:36 -05001685 init_timer(&vport->fc_disctmo);
1686 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05001687 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04001688
James Smart2e0fef82007-06-17 19:56:36 -05001689 init_timer(&vport->fc_fdmitmo);
1690 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
James Smart92d7f7b2007-06-17 19:56:38 -05001691 vport->fc_fdmitmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04001692
James Smart2e0fef82007-06-17 19:56:36 -05001693 init_timer(&vport->els_tmofunc);
1694 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05001695 vport->els_tmofunc.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04001696
James Smart3de2a652007-08-02 11:09:59 -04001697 error = scsi_add_host(shost, dev);
James Smart2e0fef82007-06-17 19:56:36 -05001698 if (error)
1699 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04001700
James Smart549e55c2007-08-02 11:09:51 -04001701 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05001702 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04001703 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05001704 return vport;
James Smart47a86172007-04-25 09:53:22 -04001705
James Smart2e0fef82007-06-17 19:56:36 -05001706out_put_shost:
1707 scsi_host_put(shost);
1708out:
1709 return NULL;
James Smart47a86172007-04-25 09:53:22 -04001710}
1711
James Smart2e0fef82007-06-17 19:56:36 -05001712void
1713destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04001714{
James Smart92d7f7b2007-06-17 19:56:38 -05001715 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1716 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04001717
James Smart92d7f7b2007-06-17 19:56:38 -05001718 kfree(vport->vname);
James Smart47a86172007-04-25 09:53:22 -04001719
James Smart858c9f62007-06-17 19:56:39 -05001720 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05001721 fc_remove_host(shost);
1722 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04001723
James Smart92d7f7b2007-06-17 19:56:38 -05001724 spin_lock_irq(&phba->hbalock);
1725 list_del_init(&vport->listentry);
1726 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04001727
James Smart92d7f7b2007-06-17 19:56:38 -05001728 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04001729 return;
James Smart47a86172007-04-25 09:53:22 -04001730}
1731
James Smart92d7f7b2007-06-17 19:56:38 -05001732int
1733lpfc_get_instance(void)
1734{
1735 int instance = 0;
1736
1737 /* Assign an unused number */
1738 if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
1739 return -1;
1740 if (idr_get_new(&lpfc_hba_index, NULL, &instance))
1741 return -1;
1742 return instance;
1743}
1744
James Smart858c9f62007-06-17 19:56:39 -05001745/*
1746 * Note: there is no scan_start function as adapter initialization
1747 * will have asynchronously kicked off the link initialization.
1748 */
James Smart47a86172007-04-25 09:53:22 -04001749
1750int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
1751{
James Smart2e0fef82007-06-17 19:56:36 -05001752 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1753 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05001754 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04001755
James Smart858c9f62007-06-17 19:56:39 -05001756 spin_lock_irq(shost->host_lock);
1757
James Smart51ef4c22007-08-02 11:10:31 -04001758 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05001759 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04001760 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05001761 }
James Smart2e0fef82007-06-17 19:56:36 -05001762 if (time >= 30 * HZ) {
1763 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001764 "0461 Scanning longer than 30 "
1765 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05001766 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04001767 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05001768 }
1769 if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
1770 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001771 "0465 Link down longer than 15 "
1772 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05001773 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05001774 goto finished;
James Smart47a86172007-04-25 09:53:22 -04001775 }
1776
James Smart2e0fef82007-06-17 19:56:36 -05001777 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05001778 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05001779 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05001780 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05001781 if (vport->fc_map_cnt == 0 && time < 2 * HZ)
James Smart858c9f62007-06-17 19:56:39 -05001782 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05001783 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05001784 goto finished;
1785
1786 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04001787
1788finished:
James Smart858c9f62007-06-17 19:56:39 -05001789 spin_unlock_irq(shost->host_lock);
1790 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05001791}
1792
1793void lpfc_host_attrib_init(struct Scsi_Host *shost)
1794{
1795 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1796 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04001797 /*
James Smart2e0fef82007-06-17 19:56:36 -05001798 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04001799 */
1800
James Smart2e0fef82007-06-17 19:56:36 -05001801 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
1802 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04001803 fc_host_supported_classes(shost) = FC_COS_CLASS3;
1804
1805 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05001806 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04001807 fc_host_supported_fc4s(shost)[2] = 1;
1808 fc_host_supported_fc4s(shost)[7] = 1;
1809
James Smart92d7f7b2007-06-17 19:56:38 -05001810 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
1811 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04001812
1813 fc_host_supported_speeds(shost) = 0;
1814 if (phba->lmt & LMT_10Gb)
1815 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04001816 if (phba->lmt & LMT_8Gb)
1817 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04001818 if (phba->lmt & LMT_4Gb)
1819 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
1820 if (phba->lmt & LMT_2Gb)
1821 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
1822 if (phba->lmt & LMT_1Gb)
1823 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
1824
1825 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05001826 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
1827 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04001828
1829 /* This value is also unchanging */
1830 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05001831 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04001832 fc_host_active_fc4s(shost)[2] = 1;
1833 fc_host_active_fc4s(shost)[7] = 1;
1834
James Smart92d7f7b2007-06-17 19:56:38 -05001835 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04001836 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04001837 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04001838 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04001839}
dea31012005-04-17 16:05:31 -05001840
1841static int __devinit
1842lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
1843{
James Smart2e0fef82007-06-17 19:56:36 -05001844 struct lpfc_vport *vport = NULL;
1845 struct lpfc_hba *phba;
1846 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05001847 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05001848 struct Scsi_Host *shost = NULL;
James Smart51ef4c22007-08-02 11:10:31 -04001849 void *ptr;
dea31012005-04-17 16:05:31 -05001850 unsigned long bar0map_len, bar2map_len;
James Smart98c9ea52007-10-27 13:37:33 -04001851 int error = -ENODEV, retval;
James Smart51ef4c22007-08-02 11:10:31 -04001852 int i, hbq_count;
James Bottomley604a3e32005-10-29 10:28:33 -05001853 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001854
1855 if (pci_enable_device(pdev))
1856 goto out;
1857 if (pci_request_regions(pdev, LPFC_DRIVER_NAME))
1858 goto out_disable_device;
1859
James Smart2e0fef82007-06-17 19:56:36 -05001860 phba = kzalloc(sizeof (struct lpfc_hba), GFP_KERNEL);
1861 if (!phba)
dea31012005-04-17 16:05:31 -05001862 goto out_release_regions;
1863
James Smart2e0fef82007-06-17 19:56:36 -05001864 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001865
dea31012005-04-17 16:05:31 -05001866 phba->pcidev = pdev;
1867
1868 /* Assign an unused board number */
James Smart92d7f7b2007-06-17 19:56:38 -05001869 if ((phba->brd_no = lpfc_get_instance()) < 0)
James Smart2e0fef82007-06-17 19:56:36 -05001870 goto out_free_phba;
dea31012005-04-17 16:05:31 -05001871
James Smart2e0fef82007-06-17 19:56:36 -05001872 INIT_LIST_HEAD(&phba->port_list);
James Smart2e0fef82007-06-17 19:56:36 -05001873 /*
1874 * Get all the module params for configuring this host and then
1875 * establish the host.
1876 */
1877 lpfc_get_cfgparam(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001878 phba->max_vpi = LPFC_MAX_VPI;
dea31012005-04-17 16:05:31 -05001879
1880 /* Initialize timers used by driver */
1881 init_timer(&phba->fc_estabtmo);
1882 phba->fc_estabtmo.function = lpfc_establish_link_tmo;
1883 phba->fc_estabtmo.data = (unsigned long)phba;
dea31012005-04-17 16:05:31 -05001884
James Smart858c9f62007-06-17 19:56:39 -05001885 init_timer(&phba->hb_tmofunc);
1886 phba->hb_tmofunc.function = lpfc_hb_timeout;
1887 phba->hb_tmofunc.data = (unsigned long)phba;
1888
dea31012005-04-17 16:05:31 -05001889 psli = &phba->sli;
1890 init_timer(&psli->mbox_tmo);
1891 psli->mbox_tmo.function = lpfc_mbox_timeout;
James Smart2e0fef82007-06-17 19:56:36 -05001892 psli->mbox_tmo.data = (unsigned long) phba;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001893 init_timer(&phba->fcp_poll_timer);
1894 phba->fcp_poll_timer.function = lpfc_poll_timeout;
James Smart2e0fef82007-06-17 19:56:36 -05001895 phba->fcp_poll_timer.data = (unsigned long) phba;
James Smart92d7f7b2007-06-17 19:56:38 -05001896 init_timer(&phba->fabric_block_timer);
1897 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
1898 phba->fabric_block_timer.data = (unsigned long) phba;
dea31012005-04-17 16:05:31 -05001899
dea31012005-04-17 16:05:31 -05001900 pci_set_master(pdev);
Randy Dunlap694625c2007-07-09 11:55:54 -07001901 pci_try_set_mwi(pdev);
dea31012005-04-17 16:05:31 -05001902
1903 if (pci_set_dma_mask(phba->pcidev, DMA_64BIT_MASK) != 0)
1904 if (pci_set_dma_mask(phba->pcidev, DMA_32BIT_MASK) != 0)
1905 goto out_idr_remove;
1906
1907 /*
1908 * Get the bus address of Bar0 and Bar2 and the number of bytes
1909 * required by each mapping.
1910 */
1911 phba->pci_bar0_map = pci_resource_start(phba->pcidev, 0);
1912 bar0map_len = pci_resource_len(phba->pcidev, 0);
1913
1914 phba->pci_bar2_map = pci_resource_start(phba->pcidev, 2);
1915 bar2map_len = pci_resource_len(phba->pcidev, 2);
1916
Jamie Wellnitz901a9202006-02-28 19:25:19 -05001917 /* Map HBA SLIM to a kernel virtual address. */
dea31012005-04-17 16:05:31 -05001918 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05001919 if (!phba->slim_memmap_p) {
1920 error = -ENODEV;
1921 dev_printk(KERN_ERR, &pdev->dev,
1922 "ioremap failed for SLIM memory.\n");
1923 goto out_idr_remove;
1924 }
1925
1926 /* Map HBA Control Registers to a kernel virtual address. */
dea31012005-04-17 16:05:31 -05001927 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05001928 if (!phba->ctrl_regs_memmap_p) {
1929 error = -ENODEV;
1930 dev_printk(KERN_ERR, &pdev->dev,
1931 "ioremap failed for HBA control registers.\n");
1932 goto out_iounmap_slim;
1933 }
dea31012005-04-17 16:05:31 -05001934
1935 /* Allocate memory for SLI-2 structures */
1936 phba->slim2p = dma_alloc_coherent(&phba->pcidev->dev, SLI2_SLIM_SIZE,
1937 &phba->slim2p_mapping, GFP_KERNEL);
1938 if (!phba->slim2p)
1939 goto out_iounmap;
1940
James.Smart@Emulex.Comf91b3922005-10-28 20:29:28 -04001941 memset(phba->slim2p, 0, SLI2_SLIM_SIZE);
dea31012005-04-17 16:05:31 -05001942
James Smarted957682007-06-17 19:56:37 -05001943 phba->hbqslimp.virt = dma_alloc_coherent(&phba->pcidev->dev,
1944 lpfc_sli_hbq_size(),
1945 &phba->hbqslimp.phys,
1946 GFP_KERNEL);
1947 if (!phba->hbqslimp.virt)
1948 goto out_free_slim;
1949
James Smart51ef4c22007-08-02 11:10:31 -04001950 hbq_count = lpfc_sli_hbq_count();
1951 ptr = phba->hbqslimp.virt;
1952 for (i = 0; i < hbq_count; ++i) {
1953 phba->hbqs[i].hbq_virt = ptr;
1954 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
1955 ptr += (lpfc_hbq_defs[i]->entry_count *
1956 sizeof(struct lpfc_hbq_entry));
1957 }
1958 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
1959 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
1960
James Smarted957682007-06-17 19:56:37 -05001961 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
1962
dea31012005-04-17 16:05:31 -05001963 /* Initialize the SLI Layer to run with lpfc HBAs. */
1964 lpfc_sli_setup(phba);
1965 lpfc_sli_queue_setup(phba);
1966
James Smart98c9ea52007-10-27 13:37:33 -04001967 retval = lpfc_mem_alloc(phba);
1968 if (retval) {
1969 error = retval;
James Smarted957682007-06-17 19:56:37 -05001970 goto out_free_hbqslimp;
James Smart98c9ea52007-10-27 13:37:33 -04001971 }
dea31012005-04-17 16:05:31 -05001972
1973 /* Initialize and populate the iocb list per host. */
1974 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
1975 for (i = 0; i < LPFC_IOCB_LIST_CNT; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001976 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05001977 if (iocbq_entry == NULL) {
1978 printk(KERN_ERR "%s: only allocated %d iocbs of "
1979 "expected %d count. Unloading driver.\n",
1980 __FUNCTION__, i, LPFC_IOCB_LIST_CNT);
1981 error = -ENOMEM;
1982 goto out_free_iocbq;
1983 }
1984
James Bottomley604a3e32005-10-29 10:28:33 -05001985 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
1986 if (iotag == 0) {
1987 kfree (iocbq_entry);
1988 printk(KERN_ERR "%s: failed to allocate IOTAG. "
1989 "Unloading driver.\n",
1990 __FUNCTION__);
1991 error = -ENOMEM;
1992 goto out_free_iocbq;
1993 }
James Smart2e0fef82007-06-17 19:56:36 -05001994
1995 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001996 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
1997 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05001998 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001999 }
2000
2001 /* Initialize HBA structure */
2002 phba->fc_edtov = FF_DEF_EDTOV;
2003 phba->fc_ratov = FF_DEF_RATOV;
2004 phba->fc_altov = FF_DEF_ALTOV;
2005 phba->fc_arbtov = FF_DEF_ARBTOV;
2006
2007 INIT_LIST_HEAD(&phba->work_list);
2008 phba->work_ha_mask = (HA_ERATT|HA_MBATT|HA_LATT);
2009 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
2010
2011 /* Startup the kernel thread for this host adapter. */
2012 phba->worker_thread = kthread_run(lpfc_do_work, phba,
2013 "lpfc_worker_%d", phba->brd_no);
2014 if (IS_ERR(phba->worker_thread)) {
2015 error = PTR_ERR(phba->worker_thread);
2016 goto out_free_iocbq;
2017 }
2018
dea31012005-04-17 16:05:31 -05002019 /* Initialize the list of scsi buffers used by driver for scsi IO. */
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002020 spin_lock_init(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05002021 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
2022
James Smart92d7f7b2007-06-17 19:56:38 -05002023 /* Initialize list of fabric iocbs */
2024 INIT_LIST_HEAD(&phba->fabric_iocb_list);
2025
James Smart0ff10d42008-01-11 01:52:36 -05002026 /* Initialize list to save ELS buffers */
2027 INIT_LIST_HEAD(&phba->elsbuf);
2028
James Smart3de2a652007-08-02 11:09:59 -04002029 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05002030 if (!vport)
2031 goto out_kthread_stop;
2032
2033 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05002034 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05002035 lpfc_debugfs_initialize(vport);
James Smart2e0fef82007-06-17 19:56:36 -05002036
James Smart92d7f7b2007-06-17 19:56:38 -05002037 pci_set_drvdata(pdev, shost);
dea31012005-04-17 16:05:31 -05002038
James Smart4ff43242006-12-02 13:34:56 -05002039 if (phba->cfg_use_msi) {
James Smart98c9ea52007-10-27 13:37:33 -04002040 retval = pci_enable_msi(phba->pcidev);
2041 if (!retval)
James Smart51ef4c22007-08-02 11:10:31 -04002042 phba->using_msi = 1;
2043 else
James Smarte8b62012007-08-02 11:10:09 -04002044 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2045 "0452 Enable MSI failed, continuing "
2046 "with IRQ\n");
James Smart4ff43242006-12-02 13:34:56 -05002047 }
2048
James Smart98c9ea52007-10-27 13:37:33 -04002049 retval = request_irq(phba->pcidev->irq, lpfc_intr_handler, IRQF_SHARED,
James Smart2e0fef82007-06-17 19:56:36 -05002050 LPFC_DRIVER_NAME, phba);
James Smart98c9ea52007-10-27 13:37:33 -04002051 if (retval) {
dea31012005-04-17 16:05:31 -05002052 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002053 "0451 Enable interrupt handler failed\n");
James Smart98c9ea52007-10-27 13:37:33 -04002054 error = retval;
James Smart92d7f7b2007-06-17 19:56:38 -05002055 goto out_disable_msi;
dea31012005-04-17 16:05:31 -05002056 }
dea31012005-04-17 16:05:31 -05002057
James Smart2e0fef82007-06-17 19:56:36 -05002058 phba->MBslimaddr = phba->slim_memmap_p;
2059 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
2060 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
2061 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
2062 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
2063
James Smart98c9ea52007-10-27 13:37:33 -04002064 if (lpfc_alloc_sysfs_attr(vport)) {
2065 error = -ENOMEM;
dea31012005-04-17 16:05:31 -05002066 goto out_free_irq;
James Smart98c9ea52007-10-27 13:37:33 -04002067 }
dea31012005-04-17 16:05:31 -05002068
James Smart98c9ea52007-10-27 13:37:33 -04002069 if (lpfc_sli_hba_setup(phba)) {
2070 error = -ENODEV;
James Smart858c9f62007-06-17 19:56:39 -05002071 goto out_remove_device;
James Smart98c9ea52007-10-27 13:37:33 -04002072 }
James Smart858c9f62007-06-17 19:56:39 -05002073
2074 /*
2075 * hba setup may have changed the hba_queue_depth so we need to adjust
2076 * the value of can_queue.
2077 */
2078 shost->can_queue = phba->cfg_hba_queue_depth - 10;
2079
2080 lpfc_host_attrib_init(shost);
2081
James Smart2e0fef82007-06-17 19:56:36 -05002082 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
2083 spin_lock_irq(shost->host_lock);
2084 lpfc_poll_start_timer(phba);
2085 spin_unlock_irq(shost->host_lock);
2086 }
James Smart8f6d98d2006-08-01 07:34:00 -04002087
James Smart858c9f62007-06-17 19:56:39 -05002088 scsi_scan_host(shost);
dea31012005-04-17 16:05:31 -05002089
dea31012005-04-17 16:05:31 -05002090 return 0;
2091
James Smart858c9f62007-06-17 19:56:39 -05002092out_remove_device:
2093 lpfc_free_sysfs_attr(vport);
2094 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04002095 vport->load_flag |= FC_UNLOADING;
James Smart858c9f62007-06-17 19:56:39 -05002096 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002097out_free_irq:
James Smart92d7f7b2007-06-17 19:56:38 -05002098 lpfc_stop_phba_timers(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002099 phba->pport->work_port_events = 0;
dea31012005-04-17 16:05:31 -05002100 free_irq(phba->pcidev->irq, phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002101out_disable_msi:
James Smart51ef4c22007-08-02 11:10:31 -04002102 if (phba->using_msi)
2103 pci_disable_msi(phba->pcidev);
James Smart2e0fef82007-06-17 19:56:36 -05002104 destroy_port(vport);
dea31012005-04-17 16:05:31 -05002105out_kthread_stop:
2106 kthread_stop(phba->worker_thread);
2107out_free_iocbq:
2108 list_for_each_entry_safe(iocbq_entry, iocbq_next,
2109 &phba->lpfc_iocb_list, list) {
dea31012005-04-17 16:05:31 -05002110 kfree(iocbq_entry);
2111 phba->total_iocbq_bufs--;
dea31012005-04-17 16:05:31 -05002112 }
2113 lpfc_mem_free(phba);
James Smarted957682007-06-17 19:56:37 -05002114out_free_hbqslimp:
2115 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(), phba->hbqslimp.virt,
2116 phba->hbqslimp.phys);
dea31012005-04-17 16:05:31 -05002117out_free_slim:
2118 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE, phba->slim2p,
2119 phba->slim2p_mapping);
2120out_iounmap:
2121 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05002122out_iounmap_slim:
dea31012005-04-17 16:05:31 -05002123 iounmap(phba->slim_memmap_p);
2124out_idr_remove:
2125 idr_remove(&lpfc_hba_index, phba->brd_no);
James Smart2e0fef82007-06-17 19:56:36 -05002126out_free_phba:
2127 kfree(phba);
dea31012005-04-17 16:05:31 -05002128out_release_regions:
2129 pci_release_regions(pdev);
2130out_disable_device:
2131 pci_disable_device(pdev);
2132out:
James Smartdefbcf12006-04-16 22:26:50 -04002133 pci_set_drvdata(pdev, NULL);
James Smart858c9f62007-06-17 19:56:39 -05002134 if (shost)
2135 scsi_host_put(shost);
dea31012005-04-17 16:05:31 -05002136 return error;
2137}
2138
2139static void __devexit
2140lpfc_pci_remove_one(struct pci_dev *pdev)
2141{
James Smart2e0fef82007-06-17 19:56:36 -05002142 struct Scsi_Host *shost = pci_get_drvdata(pdev);
2143 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2144 struct lpfc_hba *phba = vport->phba;
James Smart549e55c2007-08-02 11:09:51 -04002145 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04002146 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04002147 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002148
James Smart858c9f62007-06-17 19:56:39 -05002149 kfree(vport->vname);
2150 lpfc_free_sysfs_attr(vport);
2151
2152 fc_remove_host(shost);
2153 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04002154 lpfc_cleanup(vport);
2155
James Smart2e0fef82007-06-17 19:56:36 -05002156 /*
2157 * Bring down the SLI Layer. This step disable all interrupts,
2158 * clears the rings, discards all mailbox commands, and resets
2159 * the HBA.
2160 */
2161 lpfc_sli_hba_down(phba);
2162 lpfc_sli_brdrestart(phba);
2163
James Smart92d7f7b2007-06-17 19:56:38 -05002164 lpfc_stop_phba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05002165 spin_lock_irq(&phba->hbalock);
2166 list_del_init(&vport->listentry);
2167 spin_unlock_irq(&phba->hbalock);
2168
James Smart858c9f62007-06-17 19:56:39 -05002169 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05002170
2171 kthread_stop(phba->worker_thread);
2172
2173 /* Release the irq reservation */
2174 free_irq(phba->pcidev->irq, phba);
James Smart51ef4c22007-08-02 11:10:31 -04002175 if (phba->using_msi)
2176 pci_disable_msi(phba->pcidev);
dea31012005-04-17 16:05:31 -05002177
2178 pci_set_drvdata(pdev, NULL);
James Smart858c9f62007-06-17 19:56:39 -05002179 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05002180
2181 /*
2182 * Call scsi_free before mem_free since scsi bufs are released to their
2183 * corresponding pools here.
2184 */
2185 lpfc_scsi_free(phba);
2186 lpfc_mem_free(phba);
2187
James Smarted957682007-06-17 19:56:37 -05002188 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(), phba->hbqslimp.virt,
2189 phba->hbqslimp.phys);
2190
James Smart2e0fef82007-06-17 19:56:36 -05002191 /* Free resources associated with SLI2 interface */
2192 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
2193 phba->slim2p, phba->slim2p_mapping);
2194
2195 /* unmap adapter SLIM and Control Registers */
2196 iounmap(phba->ctrl_regs_memmap_p);
2197 iounmap(phba->slim_memmap_p);
2198
2199 idr_remove(&lpfc_hba_index, phba->brd_no);
2200
2201 kfree(phba);
2202
2203 pci_release_regions(pdev);
2204 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05002205}
2206
Linas Vepstas8d63f372007-02-14 14:28:36 -06002207/**
2208 * lpfc_io_error_detected - called when PCI error is detected
2209 * @pdev: Pointer to PCI device
2210 * @state: The current pci conneection state
2211 *
2212 * This function is called after a PCI bus error affecting
2213 * this device has been detected.
2214 */
2215static pci_ers_result_t lpfc_io_error_detected(struct pci_dev *pdev,
2216 pci_channel_state_t state)
2217{
James Smart51ef4c22007-08-02 11:10:31 -04002218 struct Scsi_Host *shost = pci_get_drvdata(pdev);
2219 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06002220 struct lpfc_sli *psli = &phba->sli;
2221 struct lpfc_sli_ring *pring;
2222
Linas Vepstas5daa49e2007-03-08 16:19:11 -06002223 if (state == pci_channel_io_perm_failure)
Linas Vepstas8d63f372007-02-14 14:28:36 -06002224 return PCI_ERS_RESULT_DISCONNECT;
Linas Vepstas5daa49e2007-03-08 16:19:11 -06002225
Linas Vepstas8d63f372007-02-14 14:28:36 -06002226 pci_disable_device(pdev);
2227 /*
2228 * There may be I/Os dropped by the firmware.
2229 * Error iocb (I/O) on txcmplq and let the SCSI layer
2230 * retry it after re-establishing link.
2231 */
2232 pring = &psli->ring[psli->fcp_ring];
2233 lpfc_sli_abort_iocb_ring(phba, pring);
2234
James Smart51ef4c22007-08-02 11:10:31 -04002235 /* Release the irq reservation */
2236 free_irq(phba->pcidev->irq, phba);
2237 if (phba->using_msi)
2238 pci_disable_msi(phba->pcidev);
2239
Linas Vepstas8d63f372007-02-14 14:28:36 -06002240 /* Request a slot reset. */
2241 return PCI_ERS_RESULT_NEED_RESET;
2242}
2243
2244/**
2245 * lpfc_io_slot_reset - called after the pci bus has been reset.
2246 * @pdev: Pointer to PCI device
2247 *
2248 * Restart the card from scratch, as if from a cold-boot.
2249 */
2250static pci_ers_result_t lpfc_io_slot_reset(struct pci_dev *pdev)
2251{
James Smart51ef4c22007-08-02 11:10:31 -04002252 struct Scsi_Host *shost = pci_get_drvdata(pdev);
2253 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06002254 struct lpfc_sli *psli = &phba->sli;
2255 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
2256
2257 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
2258 if (pci_enable_device_bars(pdev, bars)) {
2259 printk(KERN_ERR "lpfc: Cannot re-enable "
2260 "PCI device after reset.\n");
2261 return PCI_ERS_RESULT_DISCONNECT;
2262 }
2263
2264 pci_set_master(pdev);
2265
2266 /* Re-establishing Link */
James Smart51ef4c22007-08-02 11:10:31 -04002267 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05002268 phba->pport->fc_flag |= FC_ESTABLISH_LINK;
James Smart51ef4c22007-08-02 11:10:31 -04002269 spin_unlock_irq(shost->host_lock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06002270
James Smart92d7f7b2007-06-17 19:56:38 -05002271 spin_lock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06002272 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05002273 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06002274
2275
2276 /* Take device offline; this will perform cleanup */
2277 lpfc_offline(phba);
2278 lpfc_sli_brdrestart(phba);
2279
2280 return PCI_ERS_RESULT_RECOVERED;
2281}
2282
2283/**
2284 * lpfc_io_resume - called when traffic can start flowing again.
2285 * @pdev: Pointer to PCI device
2286 *
2287 * This callback is called when the error recovery driver tells us that
2288 * its OK to resume normal operation.
2289 */
2290static void lpfc_io_resume(struct pci_dev *pdev)
2291{
James Smart51ef4c22007-08-02 11:10:31 -04002292 struct Scsi_Host *shost = pci_get_drvdata(pdev);
2293 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06002294
2295 if (lpfc_online(phba) == 0) {
2296 mod_timer(&phba->fc_estabtmo, jiffies + HZ * 60);
2297 }
2298}
2299
dea31012005-04-17 16:05:31 -05002300static struct pci_device_id lpfc_id_table[] = {
2301 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
2302 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002303 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
2304 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05002305 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
2306 PCI_ANY_ID, PCI_ANY_ID, },
2307 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
2308 PCI_ANY_ID, PCI_ANY_ID, },
2309 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
2310 PCI_ANY_ID, PCI_ANY_ID, },
2311 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
2312 PCI_ANY_ID, PCI_ANY_ID, },
2313 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
2314 PCI_ANY_ID, PCI_ANY_ID, },
2315 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
2316 PCI_ANY_ID, PCI_ANY_ID, },
2317 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
2318 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002319 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
2320 PCI_ANY_ID, PCI_ANY_ID, },
2321 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
2322 PCI_ANY_ID, PCI_ANY_ID, },
2323 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
2324 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05002325 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
2326 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002327 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
2328 PCI_ANY_ID, PCI_ANY_ID, },
2329 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
2330 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05002331 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
2332 PCI_ANY_ID, PCI_ANY_ID, },
2333 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
2334 PCI_ANY_ID, PCI_ANY_ID, },
2335 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
2336 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002337 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
2338 PCI_ANY_ID, PCI_ANY_ID, },
2339 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
2340 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05002341 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
2342 PCI_ANY_ID, PCI_ANY_ID, },
2343 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
2344 PCI_ANY_ID, PCI_ANY_ID, },
2345 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
2346 PCI_ANY_ID, PCI_ANY_ID, },
2347 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
2348 PCI_ANY_ID, PCI_ANY_ID, },
2349 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
2350 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002351 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
2352 PCI_ANY_ID, PCI_ANY_ID, },
2353 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
2354 PCI_ANY_ID, PCI_ANY_ID, },
James Smartb87eab32007-04-25 09:53:28 -04002355 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
2356 PCI_ANY_ID, PCI_ANY_ID, },
2357 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
2358 PCI_ANY_ID, PCI_ANY_ID, },
2359 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
2360 PCI_ANY_ID, PCI_ANY_ID, },
2361 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
2362 PCI_ANY_ID, PCI_ANY_ID, },
2363 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
2364 PCI_ANY_ID, PCI_ANY_ID, },
2365 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
2366 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05002367 { 0 }
2368};
2369
2370MODULE_DEVICE_TABLE(pci, lpfc_id_table);
2371
Linas Vepstas8d63f372007-02-14 14:28:36 -06002372static struct pci_error_handlers lpfc_err_handler = {
2373 .error_detected = lpfc_io_error_detected,
2374 .slot_reset = lpfc_io_slot_reset,
2375 .resume = lpfc_io_resume,
2376};
2377
dea31012005-04-17 16:05:31 -05002378static struct pci_driver lpfc_driver = {
2379 .name = LPFC_DRIVER_NAME,
2380 .id_table = lpfc_id_table,
2381 .probe = lpfc_pci_probe_one,
2382 .remove = __devexit_p(lpfc_pci_remove_one),
James Smart2e0fef82007-06-17 19:56:36 -05002383 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -05002384};
2385
2386static int __init
2387lpfc_init(void)
2388{
2389 int error = 0;
2390
2391 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04002392 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -05002393
James Smart7ee5d432007-10-27 13:37:17 -04002394 if (lpfc_enable_npiv) {
2395 lpfc_transport_functions.vport_create = lpfc_vport_create;
2396 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
2397 }
dea31012005-04-17 16:05:31 -05002398 lpfc_transport_template =
2399 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -04002400 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -05002401 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -04002402 if (lpfc_enable_npiv) {
James Smart7ee5d432007-10-27 13:37:17 -04002403 lpfc_vport_transport_template =
James Smart98c9ea52007-10-27 13:37:33 -04002404 fc_attach_transport(&lpfc_vport_transport_functions);
2405 if (lpfc_vport_transport_template == NULL) {
2406 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -04002407 return -ENOMEM;
James Smart98c9ea52007-10-27 13:37:33 -04002408 }
James Smart7ee5d432007-10-27 13:37:17 -04002409 }
dea31012005-04-17 16:05:31 -05002410 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -05002411 if (error) {
dea31012005-04-17 16:05:31 -05002412 fc_release_transport(lpfc_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -05002413 fc_release_transport(lpfc_vport_transport_template);
2414 }
dea31012005-04-17 16:05:31 -05002415
2416 return error;
2417}
2418
2419static void __exit
2420lpfc_exit(void)
2421{
2422 pci_unregister_driver(&lpfc_driver);
2423 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -04002424 if (lpfc_enable_npiv)
2425 fc_release_transport(lpfc_vport_transport_template);
dea31012005-04-17 16:05:31 -05002426}
2427
2428module_init(lpfc_init);
2429module_exit(lpfc_exit);
2430MODULE_LICENSE("GPL");
2431MODULE_DESCRIPTION(LPFC_MODULE_DESC);
2432MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
2433MODULE_VERSION("0:" LPFC_DRIVER_VERSION);