blob: 49577d5f130f1ea50b911497dbb6d8ff09160865 [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 Smarte47c9092008-02-08 18:49:26 -05004 * Copyright (C) 2004-2008 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
James Smarte59058c2008-08-24 21:49:00 -040055/**
56 * lpfc_config_port_prep: Perform lpfc initialization prior to config port.
57 * @phba: pointer to lpfc hba data structure.
58 *
59 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
60 * mailbox command. It retrieves the revision information from the HBA and
61 * collects the Vital Product Data (VPD) about the HBA for preparing the
62 * configuration of the HBA.
63 *
64 * Return codes:
65 * 0 - success.
66 * -ERESTART - requests the SLI layer to reset the HBA and try again.
67 * Any other value - indicates an error.
68 **/
dea31012005-04-17 16:05:31 -050069int
James Smart2e0fef82007-06-17 19:56:36 -050070lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050071{
72 lpfc_vpd_t *vp = &phba->vpd;
73 int i = 0, rc;
74 LPFC_MBOXQ_t *pmb;
75 MAILBOX_t *mb;
76 char *lpfc_vpd_data = NULL;
77 uint16_t offset = 0;
78 static char licensed[56] =
79 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -040080 static int init_key = 1;
dea31012005-04-17 16:05:31 -050081
82 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
83 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -050084 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -050085 return -ENOMEM;
86 }
87
88 mb = &pmb->mb;
James Smart2e0fef82007-06-17 19:56:36 -050089 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -050090
91 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -040092 if (init_key) {
93 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -050094
James Smart65a29c12006-07-06 15:50:50 -040095 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
96 *ptext = cpu_to_be32(*ptext);
97 init_key = 0;
98 }
dea31012005-04-17 16:05:31 -050099
100 lpfc_read_nv(phba, pmb);
101 memset((char*)mb->un.varRDnvp.rsvd3, 0,
102 sizeof (mb->un.varRDnvp.rsvd3));
103 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
104 sizeof (licensed));
105
106 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
107
108 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500109 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smarte8b62012007-08-02 11:10:09 -0400110 "0324 Config Port initialization "
dea31012005-04-17 16:05:31 -0500111 "error, mbxCmd x%x READ_NVPARM, "
112 "mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500113 mb->mbxCommand, mb->mbxStatus);
114 mempool_free(pmb, phba->mbox_mem_pool);
115 return -ERESTART;
116 }
117 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
James Smart2e0fef82007-06-17 19:56:36 -0500118 sizeof(phba->wwnn));
119 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
120 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500121 }
122
James Smart92d7f7b2007-06-17 19:56:38 -0500123 phba->sli3_options = 0x0;
124
dea31012005-04-17 16:05:31 -0500125 /* Setup and issue mailbox READ REV command */
126 lpfc_read_rev(phba, pmb);
127 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
128 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500129 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400130 "0439 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500131 "READ_REV, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500132 mb->mbxCommand, mb->mbxStatus);
133 mempool_free( pmb, phba->mbox_mem_pool);
134 return -ERESTART;
135 }
136
James Smart92d7f7b2007-06-17 19:56:38 -0500137
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500138 /*
139 * The value of rr must be 1 since the driver set the cv field to 1.
140 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500141 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500142 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500143 vp->rev.rBit = 0;
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500144 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400145 "0440 Adapter failed to init, READ_REV has "
146 "missing revision information.\n");
dea31012005-04-17 16:05:31 -0500147 mempool_free(pmb, phba->mbox_mem_pool);
148 return -ERESTART;
dea31012005-04-17 16:05:31 -0500149 }
150
James Smart495a7142008-06-14 22:52:59 -0400151 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
152 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -0500153 return -EINVAL;
James Smart495a7142008-06-14 22:52:59 -0400154 }
James Smarted957682007-06-17 19:56:37 -0500155
dea31012005-04-17 16:05:31 -0500156 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500157 vp->rev.rBit = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500158 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500159 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
160 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
161 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
162 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500163 vp->rev.biuRev = mb->un.varRdRev.biuRev;
164 vp->rev.smRev = mb->un.varRdRev.smRev;
165 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
166 vp->rev.endecRev = mb->un.varRdRev.endecRev;
167 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
168 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
169 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
170 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
171 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
172 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
173
James Smart92d7f7b2007-06-17 19:56:38 -0500174 /* If the sli feature level is less then 9, we must
175 * tear down all RPIs and VPIs on link down if NPIV
176 * is enabled.
177 */
178 if (vp->rev.feaLevelHigh < 9)
179 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
180
dea31012005-04-17 16:05:31 -0500181 if (lpfc_is_LC_HBA(phba->pcidev->device))
182 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
183 sizeof (phba->RandomData));
184
dea31012005-04-17 16:05:31 -0500185 /* Get adapter VPD information */
dea31012005-04-17 16:05:31 -0500186 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
187 if (!lpfc_vpd_data)
James Smartd7c255b2008-08-24 21:50:00 -0400188 goto out_free_mbox;
dea31012005-04-17 16:05:31 -0500189
190 do {
191 lpfc_dump_mem(phba, pmb, offset);
192 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
193
194 if (rc != MBX_SUCCESS) {
195 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400196 "0441 VPD not present on adapter, "
dea31012005-04-17 16:05:31 -0500197 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500198 mb->mbxCommand, mb->mbxStatus);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500199 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500200 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500201 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
202 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
James Smartd7c255b2008-08-24 21:50:00 -0400203 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
204 lpfc_vpd_data + offset,
James Smart92d7f7b2007-06-17 19:56:38 -0500205 mb->un.varDmp.word_cnt);
dea31012005-04-17 16:05:31 -0500206 offset += mb->un.varDmp.word_cnt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500207 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
208 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500209
210 kfree(lpfc_vpd_data);
dea31012005-04-17 16:05:31 -0500211out_free_mbox:
212 mempool_free(pmb, phba->mbox_mem_pool);
213 return 0;
214}
215
James Smarte59058c2008-08-24 21:49:00 -0400216/**
217 * lpfc_config_async_cmpl: Completion handler for config async event mbox cmd.
218 * @phba: pointer to lpfc hba data structure.
219 * @pmboxq: pointer to the driver internal queue element for mailbox command.
220 *
221 * This is the completion handler for driver's configuring asynchronous event
222 * mailbox command to the device. If the mailbox command returns successfully,
223 * it will set internal async event support flag to 1; otherwise, it will
224 * set internal async event support flag to 0.
225 **/
James Smart57127f12007-10-27 13:37:05 -0400226static void
227lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
228{
229 if (pmboxq->mb.mbxStatus == MBX_SUCCESS)
230 phba->temp_sensor_support = 1;
231 else
232 phba->temp_sensor_support = 0;
233 mempool_free(pmboxq, phba->mbox_mem_pool);
234 return;
235}
236
James Smarte59058c2008-08-24 21:49:00 -0400237/**
238 * lpfc_config_port_post: Perform lpfc initialization after config port.
239 * @phba: pointer to lpfc hba data structure.
240 *
241 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
242 * command call. It performs all internal resource and state setups on the
243 * port: post IOCB buffers, enable appropriate host interrupt attentions,
244 * ELS ring timers, etc.
245 *
246 * Return codes
247 * 0 - success.
248 * Any other value - error.
249 **/
dea31012005-04-17 16:05:31 -0500250int
James Smart2e0fef82007-06-17 19:56:36 -0500251lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500252{
James Smart2e0fef82007-06-17 19:56:36 -0500253 struct lpfc_vport *vport = phba->pport;
dea31012005-04-17 16:05:31 -0500254 LPFC_MBOXQ_t *pmb;
255 MAILBOX_t *mb;
256 struct lpfc_dmabuf *mp;
257 struct lpfc_sli *psli = &phba->sli;
258 uint32_t status, timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500259 int i, j;
260 int rc;
dea31012005-04-17 16:05:31 -0500261
James Smart7af67052007-10-27 13:38:11 -0400262 spin_lock_irq(&phba->hbalock);
263 /*
264 * If the Config port completed correctly the HBA is not
265 * over heated any more.
266 */
267 if (phba->over_temp_state == HBA_OVER_TEMP)
268 phba->over_temp_state = HBA_NORMAL_TEMP;
269 spin_unlock_irq(&phba->hbalock);
270
dea31012005-04-17 16:05:31 -0500271 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
272 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500273 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500274 return -ENOMEM;
275 }
276 mb = &pmb->mb;
277
dea31012005-04-17 16:05:31 -0500278 /* Get login parameters for NID. */
James Smart92d7f7b2007-06-17 19:56:38 -0500279 lpfc_read_sparam(phba, pmb, 0);
James Smarted957682007-06-17 19:56:37 -0500280 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500281 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500282 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400283 "0448 Adapter failed init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500284 "READ_SPARM mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500285 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500286 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500287 mp = (struct lpfc_dmabuf *) pmb->context1;
288 mempool_free( pmb, phba->mbox_mem_pool);
289 lpfc_mbuf_free(phba, mp->virt, mp->phys);
290 kfree(mp);
291 return -EIO;
292 }
293
294 mp = (struct lpfc_dmabuf *) pmb->context1;
295
James Smart2e0fef82007-06-17 19:56:36 -0500296 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500297 lpfc_mbuf_free(phba, mp->virt, mp->phys);
298 kfree(mp);
299 pmb->context1 = NULL;
300
James Smarta12e07b2006-12-02 13:35:30 -0500301 if (phba->cfg_soft_wwnn)
James Smart2e0fef82007-06-17 19:56:36 -0500302 u64_to_wwn(phba->cfg_soft_wwnn,
303 vport->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -0400304 if (phba->cfg_soft_wwpn)
James Smart2e0fef82007-06-17 19:56:36 -0500305 u64_to_wwn(phba->cfg_soft_wwpn,
306 vport->fc_sparam.portName.u.wwn);
307 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
dea31012005-04-17 16:05:31 -0500308 sizeof (struct lpfc_name));
James Smart2e0fef82007-06-17 19:56:36 -0500309 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
dea31012005-04-17 16:05:31 -0500310 sizeof (struct lpfc_name));
311 /* If no serial number in VPD data, use low 6 bytes of WWNN */
312 /* This should be consolidated into parse_vpd ? - mr */
313 if (phba->SerialNumber[0] == 0) {
314 uint8_t *outptr;
315
James Smart2e0fef82007-06-17 19:56:36 -0500316 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500317 for (i = 0; i < 12; i++) {
318 status = *outptr++;
319 j = ((status & 0xf0) >> 4);
320 if (j <= 9)
321 phba->SerialNumber[i] =
322 (char)((uint8_t) 0x30 + (uint8_t) j);
323 else
324 phba->SerialNumber[i] =
325 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
326 i++;
327 j = (status & 0xf);
328 if (j <= 9)
329 phba->SerialNumber[i] =
330 (char)((uint8_t) 0x30 + (uint8_t) j);
331 else
332 phba->SerialNumber[i] =
333 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
334 }
335 }
336
dea31012005-04-17 16:05:31 -0500337 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500338 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500339 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500340 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400341 "0453 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500342 "READ_CONFIG, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500343 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500344 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500345 mempool_free( pmb, phba->mbox_mem_pool);
346 return -EIO;
347 }
348
349 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
350 if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
351 phba->cfg_hba_queue_depth =
352 mb->un.varRdConfig.max_xri + 1;
353
354 phba->lmt = mb->un.varRdConfig.lmt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500355
356 /* Get the default values for Model Name and Description */
357 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
358
359 if ((phba->cfg_link_speed > LINK_SPEED_10G)
360 || ((phba->cfg_link_speed == LINK_SPEED_1G)
361 && !(phba->lmt & LMT_1Gb))
362 || ((phba->cfg_link_speed == LINK_SPEED_2G)
363 && !(phba->lmt & LMT_2Gb))
364 || ((phba->cfg_link_speed == LINK_SPEED_4G)
365 && !(phba->lmt & LMT_4Gb))
366 || ((phba->cfg_link_speed == LINK_SPEED_8G)
367 && !(phba->lmt & LMT_8Gb))
368 || ((phba->cfg_link_speed == LINK_SPEED_10G)
369 && !(phba->lmt & LMT_10Gb))) {
370 /* Reset link speed to auto */
James Smarted957682007-06-17 19:56:37 -0500371 lpfc_printf_log(phba, KERN_WARNING, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -0400372 "1302 Invalid speed for this board: "
dea31012005-04-17 16:05:31 -0500373 "Reset link speed to auto: x%x\n",
dea31012005-04-17 16:05:31 -0500374 phba->cfg_link_speed);
375 phba->cfg_link_speed = LINK_SPEED_AUTO;
376 }
377
James Smart2e0fef82007-06-17 19:56:36 -0500378 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500379
James Smart0b727fe2007-10-27 13:37:25 -0400380 /* Only process IOCBs on ELS ring till hba_state is READY */
James Smarta4bc3372006-12-02 13:34:16 -0500381 if (psli->ring[psli->extra_ring].cmdringaddr)
382 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500383 if (psli->ring[psli->fcp_ring].cmdringaddr)
384 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
385 if (psli->ring[psli->next_ring].cmdringaddr)
386 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
387
388 /* Post receive buffers for desired rings */
James Smarted957682007-06-17 19:56:37 -0500389 if (phba->sli_rev != 3)
390 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500391
James Smart93996272008-08-24 21:50:30 -0400392 /*
393 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
394 */
395 if (phba->intr_type == MSIX) {
396 rc = lpfc_config_msi(phba, pmb);
397 if (rc) {
398 mempool_free(pmb, phba->mbox_mem_pool);
399 return -EIO;
400 }
401 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
402 if (rc != MBX_SUCCESS) {
403 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
404 "0352 Config MSI mailbox command "
405 "failed, mbxCmd x%x, mbxStatus x%x\n",
406 pmb->mb.mbxCommand, pmb->mb.mbxStatus);
407 mempool_free(pmb, phba->mbox_mem_pool);
408 return -EIO;
409 }
410 }
411
412 /* Initialize ERATT handling flag */
413 phba->hba_flag &= ~HBA_ERATT_HANDLED;
414
dea31012005-04-17 16:05:31 -0500415 /* Enable appropriate host interrupts */
James Smart2e0fef82007-06-17 19:56:36 -0500416 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500417 status = readl(phba->HCregaddr);
418 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
419 if (psli->num_rings > 0)
420 status |= HC_R0INT_ENA;
421 if (psli->num_rings > 1)
422 status |= HC_R1INT_ENA;
423 if (psli->num_rings > 2)
424 status |= HC_R2INT_ENA;
425 if (psli->num_rings > 3)
426 status |= HC_R3INT_ENA;
427
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500428 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
429 (phba->cfg_poll & DISABLE_FCP_RING_INT))
James Smart93996272008-08-24 21:50:30 -0400430 status &= ~(HC_R0INT_ENA);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500431
dea31012005-04-17 16:05:31 -0500432 writel(status, phba->HCregaddr);
433 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500434 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500435
James Smart93996272008-08-24 21:50:30 -0400436 /* Set up ring-0 (ELS) timer */
437 timeout = phba->fc_ratov * 2;
James Smart2e0fef82007-06-17 19:56:36 -0500438 mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
James Smart93996272008-08-24 21:50:30 -0400439 /* Set up heart beat (HB) timer */
James Smart858c9f62007-06-17 19:56:39 -0500440 mod_timer(&phba->hb_tmofunc, jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
441 phba->hb_outstanding = 0;
442 phba->last_completion_time = jiffies;
James Smart93996272008-08-24 21:50:30 -0400443 /* Set up error attention (ERATT) polling timer */
444 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
dea31012005-04-17 16:05:31 -0500445
446 lpfc_init_link(phba, pmb, phba->cfg_topology, phba->cfg_link_speed);
447 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -0400448 lpfc_set_loopback_flag(phba);
James Smartd7c255b2008-08-24 21:50:00 -0400449 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart8aee9182006-08-31 12:27:57 -0400450 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500451 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400452 "0454 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500453 "INIT_LINK, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500454 mb->mbxCommand, mb->mbxStatus);
455
456 /* Clear all interrupt enable conditions */
457 writel(0, phba->HCregaddr);
458 readl(phba->HCregaddr); /* flush */
459 /* Clear all pending interrupts */
460 writel(0xffffffff, phba->HAregaddr);
461 readl(phba->HAregaddr); /* flush */
462
James Smart2e0fef82007-06-17 19:56:36 -0500463 phba->link_state = LPFC_HBA_ERROR;
James Smart8aee9182006-08-31 12:27:57 -0400464 if (rc != MBX_BUSY)
465 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500466 return -EIO;
467 }
468 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400469 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
470 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
471 pmb->mbox_cmpl = lpfc_config_async_cmpl;
472 pmb->vport = phba->pport;
473 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500474
James Smart57127f12007-10-27 13:37:05 -0400475 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
476 lpfc_printf_log(phba,
477 KERN_ERR,
478 LOG_INIT,
479 "0456 Adapter failed to issue "
480 "ASYNCEVT_ENABLE mbox status x%x \n.",
481 rc);
482 mempool_free(pmb, phba->mbox_mem_pool);
483 }
James Smartd7c255b2008-08-24 21:50:00 -0400484 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400485}
486
James Smarte59058c2008-08-24 21:49:00 -0400487/**
488 * lpfc_hba_down_prep: Perform lpfc uninitialization prior to HBA reset.
489 * @phba: pointer to lpfc HBA data structure.
490 *
491 * This routine will do LPFC uninitialization before the HBA is reset when
492 * bringing down the SLI Layer.
493 *
494 * Return codes
495 * 0 - success.
496 * Any other value - error.
497 **/
dea31012005-04-17 16:05:31 -0500498int
James Smart2e0fef82007-06-17 19:56:36 -0500499lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500500{
James Smart1b32f6a2008-02-08 18:49:39 -0500501 struct lpfc_vport **vports;
502 int i;
dea31012005-04-17 16:05:31 -0500503 /* Disable interrupts */
504 writel(0, phba->HCregaddr);
505 readl(phba->HCregaddr); /* flush */
506
James Smart1b32f6a2008-02-08 18:49:39 -0500507 if (phba->pport->load_flag & FC_UNLOADING)
508 lpfc_cleanup_discovery_resources(phba->pport);
509 else {
510 vports = lpfc_create_vport_work_array(phba);
511 if (vports != NULL)
512 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++)
513 lpfc_cleanup_discovery_resources(vports[i]);
514 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500515 }
516 return 0;
dea31012005-04-17 16:05:31 -0500517}
518
James Smarte59058c2008-08-24 21:49:00 -0400519/**
520 * lpfc_hba_down_post: Perform lpfc uninitialization after HBA reset.
521 * @phba: pointer to lpfc HBA data structure.
522 *
523 * This routine will do uninitialization after the HBA is reset when bring
524 * down the SLI Layer.
525 *
526 * Return codes
527 * 0 - sucess.
528 * Any other value - error.
529 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -0500530int
James Smart2e0fef82007-06-17 19:56:36 -0500531lpfc_hba_down_post(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -0500532{
533 struct lpfc_sli *psli = &phba->sli;
534 struct lpfc_sli_ring *pring;
535 struct lpfc_dmabuf *mp, *next_mp;
James Smart09372822008-01-11 01:52:54 -0500536 struct lpfc_iocbq *iocb;
537 IOCB_t *cmd = NULL;
538 LIST_HEAD(completions);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500539 int i;
540
James Smart92d7f7b2007-06-17 19:56:38 -0500541 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
542 lpfc_sli_hbqbuf_free_all(phba);
543 else {
544 /* Cleanup preposted buffers on the ELS ring */
545 pring = &psli->ring[LPFC_ELS_RING];
546 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
547 list_del(&mp->list);
548 pring->postbufq_cnt--;
549 lpfc_mbuf_free(phba, mp->virt, mp->phys);
550 kfree(mp);
551 }
Jamie Wellnitz41415862006-02-28 19:25:27 -0500552 }
553
James Smart09372822008-01-11 01:52:54 -0500554 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500555 for (i = 0; i < psli->num_rings; i++) {
556 pring = &psli->ring[i];
James Smart09372822008-01-11 01:52:54 -0500557
558 /* At this point in time the HBA is either reset or DOA. Either
559 * way, nothing should be on txcmplq as it will NEVER complete.
560 */
561 list_splice_init(&pring->txcmplq, &completions);
562 pring->txcmplq_cnt = 0;
563 spin_unlock_irq(&phba->hbalock);
564
565 while (!list_empty(&completions)) {
566 iocb = list_get_first(&completions, struct lpfc_iocbq,
567 list);
568 cmd = &iocb->iocb;
569 list_del_init(&iocb->list);
570
571 if (!iocb->iocb_cmpl)
572 lpfc_sli_release_iocbq(phba, iocb);
573 else {
574 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
575 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
576 (iocb->iocb_cmpl) (phba, iocb, iocb);
577 }
578 }
579
Jamie Wellnitz41415862006-02-28 19:25:27 -0500580 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart09372822008-01-11 01:52:54 -0500581 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500582 }
James Smart09372822008-01-11 01:52:54 -0500583 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500584
585 return 0;
586}
587
James Smarte59058c2008-08-24 21:49:00 -0400588/**
589 * lpfc_hb_timeout: The HBA-timer timeout handler.
590 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
591 *
592 * This is the HBA-timer timeout handler registered to the lpfc driver. When
593 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
594 * work-port-events bitmap and the worker thread is notified. This timeout
595 * event will be used by the worker thread to invoke the actual timeout
596 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
597 * be performed in the timeout handler and the HBA timeout event bit shall
598 * be cleared by the worker thread after it has taken the event bitmap out.
599 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100600static void
James Smart858c9f62007-06-17 19:56:39 -0500601lpfc_hb_timeout(unsigned long ptr)
602{
603 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400604 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -0500605 unsigned long iflag;
606
607 phba = (struct lpfc_hba *)ptr;
James Smart93996272008-08-24 21:50:30 -0400608
609 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -0500610 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -0400611 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
612 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -0500613 phba->pport->work_port_events |= WORKER_HB_TMO;
614 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
615
James Smart93996272008-08-24 21:50:30 -0400616 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -0400617 if (!tmo_posted)
618 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -0500619 return;
620}
621
James Smarte59058c2008-08-24 21:49:00 -0400622/**
623 * lpfc_hb_mbox_cmpl: The lpfc heart-beat mailbox command callback function.
624 * @phba: pointer to lpfc hba data structure.
625 * @pmboxq: pointer to the driver internal queue element for mailbox command.
626 *
627 * This is the callback function to the lpfc heart-beat mailbox command.
628 * If configured, the lpfc driver issues the heart-beat mailbox command to
629 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
630 * heart-beat mailbox command is issued, the driver shall set up heart-beat
631 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
632 * heart-beat outstanding state. Once the mailbox command comes back and
633 * no error conditions detected, the heart-beat mailbox command timer is
634 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
635 * state is cleared for the next heart-beat. If the timer expired with the
636 * heart-beat outstanding state set, the driver will put the HBA offline.
637 **/
James Smart858c9f62007-06-17 19:56:39 -0500638static void
639lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
640{
641 unsigned long drvr_flag;
642
643 spin_lock_irqsave(&phba->hbalock, drvr_flag);
644 phba->hb_outstanding = 0;
645 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
646
James Smart93996272008-08-24 21:50:30 -0400647 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -0500648 mempool_free(pmboxq, phba->mbox_mem_pool);
649 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
650 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -0400651 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -0500652 mod_timer(&phba->hb_tmofunc,
653 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
654 return;
655}
656
James Smarte59058c2008-08-24 21:49:00 -0400657/**
658 * lpfc_hb_timeout_handler: The HBA-timer timeout handler.
659 * @phba: pointer to lpfc hba data structure.
660 *
661 * This is the actual HBA-timer timeout handler to be invoked by the worker
662 * thread whenever the HBA timer fired and HBA-timeout event posted. This
663 * handler performs any periodic operations needed for the device. If such
664 * periodic event has already been attended to either in the interrupt handler
665 * or by processing slow-ring or fast-ring events within the HBA-timer
666 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
667 * the timer for the next timeout period. If lpfc heart-beat mailbox command
668 * is configured and there is no heart-beat mailbox command outstanding, a
669 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
670 * has been a heart-beat mailbox command outstanding, the HBA shall be put
671 * to offline.
672 **/
James Smart858c9f62007-06-17 19:56:39 -0500673void
674lpfc_hb_timeout_handler(struct lpfc_hba *phba)
675{
676 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -0500677 struct lpfc_dmabuf *buf_ptr;
James Smart858c9f62007-06-17 19:56:39 -0500678 int retval;
679 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -0500680 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -0500681
682 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -0400683 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -0500684 (phba->pport->fc_flag & FC_OFFLINE_MODE))
685 return;
686
687 spin_lock_irq(&phba->pport->work_port_lock);
688 /* If the timer is already canceled do nothing */
689 if (!(phba->pport->work_port_events & WORKER_HB_TMO)) {
690 spin_unlock_irq(&phba->pport->work_port_lock);
691 return;
692 }
693
694 if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
695 jiffies)) {
696 spin_unlock_irq(&phba->pport->work_port_lock);
697 if (!phba->hb_outstanding)
698 mod_timer(&phba->hb_tmofunc,
699 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
700 else
701 mod_timer(&phba->hb_tmofunc,
702 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
703 return;
704 }
705 spin_unlock_irq(&phba->pport->work_port_lock);
706
James Smart0ff10d42008-01-11 01:52:36 -0500707 if (phba->elsbuf_cnt &&
708 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
709 spin_lock_irq(&phba->hbalock);
710 list_splice_init(&phba->elsbuf, &completions);
711 phba->elsbuf_cnt = 0;
712 phba->elsbuf_prev_cnt = 0;
713 spin_unlock_irq(&phba->hbalock);
714
715 while (!list_empty(&completions)) {
716 list_remove_head(&completions, buf_ptr,
717 struct lpfc_dmabuf, list);
718 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
719 kfree(buf_ptr);
720 }
721 }
722 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
723
James Smart858c9f62007-06-17 19:56:39 -0500724 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -0500725 if (phba->cfg_enable_hba_heartbeat) {
726 if (!phba->hb_outstanding) {
727 pmboxq = mempool_alloc(phba->mbox_mem_pool,GFP_KERNEL);
728 if (!pmboxq) {
729 mod_timer(&phba->hb_tmofunc,
730 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
731 return;
732 }
733
734 lpfc_heart_beat(phba, pmboxq);
735 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
736 pmboxq->vport = phba->pport;
737 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
738
739 if (retval != MBX_BUSY && retval != MBX_SUCCESS) {
740 mempool_free(pmboxq, phba->mbox_mem_pool);
741 mod_timer(&phba->hb_tmofunc,
742 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
743 return;
744 }
James Smart858c9f62007-06-17 19:56:39 -0500745 mod_timer(&phba->hb_tmofunc,
James Smart13815c82008-01-11 01:52:48 -0500746 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
747 phba->hb_outstanding = 1;
James Smart858c9f62007-06-17 19:56:39 -0500748 return;
James Smart13815c82008-01-11 01:52:48 -0500749 } else {
750 /*
751 * If heart beat timeout called with hb_outstanding set
752 * we need to take the HBA offline.
753 */
754 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
755 "0459 Adapter heartbeat failure, "
756 "taking this port offline.\n");
757
758 spin_lock_irq(&phba->hbalock);
759 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
760 spin_unlock_irq(&phba->hbalock);
761
762 lpfc_offline_prep(phba);
763 lpfc_offline(phba);
764 lpfc_unblock_mgmt_io(phba);
765 phba->link_state = LPFC_HBA_ERROR;
766 lpfc_hba_down_post(phba);
James Smart858c9f62007-06-17 19:56:39 -0500767 }
James Smart858c9f62007-06-17 19:56:39 -0500768 }
769}
770
James Smarte59058c2008-08-24 21:49:00 -0400771/**
772 * lpfc_offline_eratt: Bring lpfc offline on hardware error attention.
773 * @phba: pointer to lpfc hba data structure.
774 *
775 * This routine is called to bring the HBA offline when HBA hardware error
776 * other than Port Error 6 has been detected.
777 **/
James Smart09372822008-01-11 01:52:54 -0500778static void
779lpfc_offline_eratt(struct lpfc_hba *phba)
780{
781 struct lpfc_sli *psli = &phba->sli;
782
783 spin_lock_irq(&phba->hbalock);
784 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
785 spin_unlock_irq(&phba->hbalock);
786 lpfc_offline_prep(phba);
787
788 lpfc_offline(phba);
789 lpfc_reset_barrier(phba);
790 lpfc_sli_brdreset(phba);
791 lpfc_hba_down_post(phba);
792 lpfc_sli_brdready(phba, HS_MBRDY);
793 lpfc_unblock_mgmt_io(phba);
794 phba->link_state = LPFC_HBA_ERROR;
795 return;
796}
797
James Smarte59058c2008-08-24 21:49:00 -0400798/**
799 * lpfc_handle_eratt: The HBA hardware error handler.
800 * @phba: pointer to lpfc hba data structure.
801 *
802 * This routine is invoked to handle the following HBA hardware error
803 * conditions:
804 * 1 - HBA error attention interrupt
805 * 2 - DMA ring index out of range
806 * 3 - Mailbox command came back as unknown
807 **/
dea31012005-04-17 16:05:31 -0500808void
James Smart2e0fef82007-06-17 19:56:36 -0500809lpfc_handle_eratt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500810{
James Smart2e0fef82007-06-17 19:56:36 -0500811 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -0500812 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -0500813 struct lpfc_sli_ring *pring;
James Smartd2873e42006-08-18 17:46:43 -0400814 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -0400815 unsigned long temperature;
816 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -0500817 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -0500818
Linas Vepstas8d63f372007-02-14 14:28:36 -0600819 /* If the pci channel is offline, ignore possible errors,
820 * since we cannot communicate with the pci card anyway. */
821 if (pci_channel_offline(phba->pcidev))
822 return;
James Smart13815c82008-01-11 01:52:48 -0500823 /* If resets are disabled then leave the HBA alone and return */
824 if (!phba->cfg_enable_hba_reset)
825 return;
dea31012005-04-17 16:05:31 -0500826
James Smart97eab632008-04-07 10:16:05 -0400827 if (phba->work_hs & HS_FFER6) {
dea31012005-04-17 16:05:31 -0500828 /* Re-establishing Link */
829 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -0400830 "1301 Re-establishing Link "
dea31012005-04-17 16:05:31 -0500831 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400832 phba->work_hs,
dea31012005-04-17 16:05:31 -0500833 phba->work_status[0], phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -0400834
James Smart92d7f7b2007-06-17 19:56:38 -0500835 spin_lock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -0500836 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -0500837 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500838
839 /*
840 * Firmware stops when it triggled erratt with HS_FFER6.
841 * That could cause the I/Os dropped by the firmware.
842 * Error iocb (I/O) on txcmplq and let the SCSI layer
843 * retry it after re-establishing link.
844 */
845 pring = &psli->ring[psli->fcp_ring];
846 lpfc_sli_abort_iocb_ring(phba, pring);
847
dea31012005-04-17 16:05:31 -0500848 /*
849 * There was a firmware error. Take the hba offline and then
850 * attempt to restart it.
851 */
James Smart46fa3112007-04-25 09:51:45 -0400852 lpfc_offline_prep(phba);
dea31012005-04-17 16:05:31 -0500853 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500854 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -0500855 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -0400856 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -0500857 return;
858 }
James Smart46fa3112007-04-25 09:51:45 -0400859 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -0400860 } else if (phba->work_hs & HS_CRIT_TEMP) {
861 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
862 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
863 temp_event_data.event_code = LPFC_CRIT_TEMP;
864 temp_event_data.data = (uint32_t)temperature;
865
866 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -0400867 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -0400868 "(%ld), taking this port offline "
869 "Data: x%x x%x x%x\n",
870 temperature, phba->work_hs,
871 phba->work_status[0], phba->work_status[1]);
872
873 shost = lpfc_shost_from_vport(phba->pport);
874 fc_host_post_vendor_event(shost, fc_get_event_number(),
875 sizeof(temp_event_data),
876 (char *) &temp_event_data,
877 SCSI_NL_VID_TYPE_PCI
878 | PCI_VENDOR_ID_EMULEX);
879
James Smart7af67052007-10-27 13:38:11 -0400880 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -0400881 phba->over_temp_state = HBA_OVER_TEMP;
882 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -0500883 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -0400884
dea31012005-04-17 16:05:31 -0500885 } else {
886 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -0400887 * failure is a value other than FFER6. Do not call the offline
888 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -0500889 */
890 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400891 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -0500892 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400893 phba->work_hs,
dea31012005-04-17 16:05:31 -0500894 phba->work_status[0], phba->work_status[1]);
895
James Smartd2873e42006-08-18 17:46:43 -0400896 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -0500897 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -0500898 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -0400899 sizeof(event_data), (char *) &event_data,
900 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
901
James Smart09372822008-01-11 01:52:54 -0500902 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -0500903 }
James Smart93996272008-08-24 21:50:30 -0400904 return;
dea31012005-04-17 16:05:31 -0500905}
906
James Smarte59058c2008-08-24 21:49:00 -0400907/**
908 * lpfc_handle_latt: The HBA link event handler.
909 * @phba: pointer to lpfc hba data structure.
910 *
911 * This routine is invoked from the worker thread to handle a HBA host
912 * attention link event.
913 **/
dea31012005-04-17 16:05:31 -0500914void
James Smart2e0fef82007-06-17 19:56:36 -0500915lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500916{
James Smart2e0fef82007-06-17 19:56:36 -0500917 struct lpfc_vport *vport = phba->pport;
918 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -0500919 LPFC_MBOXQ_t *pmb;
920 volatile uint32_t control;
921 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -0500922 int rc = 0;
dea31012005-04-17 16:05:31 -0500923
924 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -0500925 if (!pmb) {
926 rc = 1;
dea31012005-04-17 16:05:31 -0500927 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -0500928 }
dea31012005-04-17 16:05:31 -0500929
930 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -0500931 if (!mp) {
932 rc = 2;
dea31012005-04-17 16:05:31 -0500933 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -0500934 }
dea31012005-04-17 16:05:31 -0500935
936 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -0500937 if (!mp->virt) {
938 rc = 3;
dea31012005-04-17 16:05:31 -0500939 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -0500940 }
dea31012005-04-17 16:05:31 -0500941
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -0500942 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -0400943 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -0500944
945 psli->slistat.link_event++;
946 lpfc_read_la(phba, pmb, mp);
947 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_la;
James Smart2e0fef82007-06-17 19:56:36 -0500948 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -0400949 /* Block ELS IOCBs until we have processed this mbox command */
950 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -0400951 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -0500952 if (rc == MBX_NOT_FINISHED) {
953 rc = 4;
James Smart14691152006-12-02 13:34:28 -0500954 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -0500955 }
dea31012005-04-17 16:05:31 -0500956
957 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -0500958 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500959 writel(HA_LATT, phba->HAregaddr);
960 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500961 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500962
963 return;
964
James Smart14691152006-12-02 13:34:28 -0500965lpfc_handle_latt_free_mbuf:
James Smart0d2b6b82008-06-14 22:52:47 -0400966 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -0500967 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -0500968lpfc_handle_latt_free_mp:
969 kfree(mp);
970lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -0400971 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500972lpfc_handle_latt_err_exit:
973 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -0500974 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500975 psli->sli_flag |= LPFC_PROCESS_LA;
976 control = readl(phba->HCregaddr);
977 control |= HC_LAINT_ENA;
978 writel(control, phba->HCregaddr);
979 readl(phba->HCregaddr); /* flush */
980
981 /* Clear Link Attention in HA REG */
982 writel(HA_LATT, phba->HAregaddr);
983 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500984 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500985 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -0500986 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500987
James Smart09372822008-01-11 01:52:54 -0500988 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
989 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -0500990
991 return;
992}
993
James Smarte59058c2008-08-24 21:49:00 -0400994/**
995 * lpfc_parse_vpd: Parse VPD (Vital Product Data).
996 * @phba: pointer to lpfc hba data structure.
997 * @vpd: pointer to the vital product data.
998 * @len: length of the vital product data in bytes.
999 *
1000 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1001 * an array of characters. In this routine, the ModelName, ProgramType, and
1002 * ModelDesc, etc. fields of the phba data structure will be populated.
1003 *
1004 * Return codes
1005 * 0 - pointer to the VPD passed in is NULL
1006 * 1 - success
1007 **/
dea31012005-04-17 16:05:31 -05001008static int
James Smart2e0fef82007-06-17 19:56:36 -05001009lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05001010{
1011 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05001012 int Length;
dea31012005-04-17 16:05:31 -05001013 int i, j;
1014 int finished = 0;
1015 int index = 0;
1016
1017 if (!vpd)
1018 return 0;
1019
1020 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05001021 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001022 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05001023 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1024 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001025 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05001026 switch (vpd[index]) {
1027 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001028 case 0x91:
dea31012005-04-17 16:05:31 -05001029 index += 1;
1030 lenlo = vpd[index];
1031 index += 1;
1032 lenhi = vpd[index];
1033 index += 1;
1034 i = ((((unsigned short)lenhi) << 8) + lenlo);
1035 index += i;
1036 break;
1037 case 0x90:
1038 index += 1;
1039 lenlo = vpd[index];
1040 index += 1;
1041 lenhi = vpd[index];
1042 index += 1;
1043 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001044 if (Length > len - index)
1045 Length = len - index;
dea31012005-04-17 16:05:31 -05001046 while (Length > 0) {
1047 /* Look for Serial Number */
1048 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1049 index += 2;
1050 i = vpd[index];
1051 index += 1;
1052 j = 0;
1053 Length -= (3+i);
1054 while(i--) {
1055 phba->SerialNumber[j++] = vpd[index++];
1056 if (j == 31)
1057 break;
1058 }
1059 phba->SerialNumber[j] = 0;
1060 continue;
1061 }
1062 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1063 phba->vpd_flag |= VPD_MODEL_DESC;
1064 index += 2;
1065 i = vpd[index];
1066 index += 1;
1067 j = 0;
1068 Length -= (3+i);
1069 while(i--) {
1070 phba->ModelDesc[j++] = vpd[index++];
1071 if (j == 255)
1072 break;
1073 }
1074 phba->ModelDesc[j] = 0;
1075 continue;
1076 }
1077 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1078 phba->vpd_flag |= VPD_MODEL_NAME;
1079 index += 2;
1080 i = vpd[index];
1081 index += 1;
1082 j = 0;
1083 Length -= (3+i);
1084 while(i--) {
1085 phba->ModelName[j++] = vpd[index++];
1086 if (j == 79)
1087 break;
1088 }
1089 phba->ModelName[j] = 0;
1090 continue;
1091 }
1092 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1093 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1094 index += 2;
1095 i = vpd[index];
1096 index += 1;
1097 j = 0;
1098 Length -= (3+i);
1099 while(i--) {
1100 phba->ProgramType[j++] = vpd[index++];
1101 if (j == 255)
1102 break;
1103 }
1104 phba->ProgramType[j] = 0;
1105 continue;
1106 }
1107 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1108 phba->vpd_flag |= VPD_PORT;
1109 index += 2;
1110 i = vpd[index];
1111 index += 1;
1112 j = 0;
1113 Length -= (3+i);
1114 while(i--) {
1115 phba->Port[j++] = vpd[index++];
1116 if (j == 19)
1117 break;
1118 }
1119 phba->Port[j] = 0;
1120 continue;
1121 }
1122 else {
1123 index += 2;
1124 i = vpd[index];
1125 index += 1;
1126 index += i;
1127 Length -= (3 + i);
1128 }
1129 }
1130 finished = 0;
1131 break;
1132 case 0x78:
1133 finished = 1;
1134 break;
1135 default:
1136 index ++;
1137 break;
1138 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001139 }
dea31012005-04-17 16:05:31 -05001140
1141 return(1);
1142}
1143
James Smarte59058c2008-08-24 21:49:00 -04001144/**
1145 * lpfc_get_hba_model_desc: Retrieve HBA device model name and description.
1146 * @phba: pointer to lpfc hba data structure.
1147 * @mdp: pointer to the data structure to hold the derived model name.
1148 * @descp: pointer to the data structure to hold the derived description.
1149 *
1150 * This routine retrieves HBA's description based on its registered PCI device
1151 * ID. The @descp passed into this function points to an array of 256 chars. It
1152 * shall be returned with the model name, maximum speed, and the host bus type.
1153 * The @mdp passed into this function points to an array of 80 chars. When the
1154 * function returns, the @mdp will be filled with the model name.
1155 **/
dea31012005-04-17 16:05:31 -05001156static void
James Smart2e0fef82007-06-17 19:56:36 -05001157lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05001158{
1159 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05001160 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001161 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04001162 int GE = 0;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001163 struct {
1164 char * name;
1165 int max_speed;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001166 char * bus;
James Smart18a3b592006-12-02 13:35:08 -05001167 } m = {"<Unknown>", 0, ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001168
1169 if (mdp && mdp[0] != '\0'
1170 && descp && descp[0] != '\0')
1171 return;
1172
1173 if (phba->lmt & LMT_10Gb)
1174 max_speed = 10;
1175 else if (phba->lmt & LMT_8Gb)
1176 max_speed = 8;
1177 else if (phba->lmt & LMT_4Gb)
1178 max_speed = 4;
1179 else if (phba->lmt & LMT_2Gb)
1180 max_speed = 2;
1181 else
1182 max_speed = 1;
dea31012005-04-17 16:05:31 -05001183
1184 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05001185
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001186 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001187 case PCI_DEVICE_ID_FIREFLY:
James Smart18a3b592006-12-02 13:35:08 -05001188 m = (typeof(m)){"LP6000", max_speed, "PCI"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001189 break;
dea31012005-04-17 16:05:31 -05001190 case PCI_DEVICE_ID_SUPERFLY:
1191 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smart18a3b592006-12-02 13:35:08 -05001192 m = (typeof(m)){"LP7000", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001193 else
James Smart18a3b592006-12-02 13:35:08 -05001194 m = (typeof(m)){"LP7000E", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001195 break;
1196 case PCI_DEVICE_ID_DRAGONFLY:
James Smart18a3b592006-12-02 13:35:08 -05001197 m = (typeof(m)){"LP8000", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001198 break;
1199 case PCI_DEVICE_ID_CENTAUR:
1200 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smart18a3b592006-12-02 13:35:08 -05001201 m = (typeof(m)){"LP9002", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001202 else
James Smart18a3b592006-12-02 13:35:08 -05001203 m = (typeof(m)){"LP9000", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001204 break;
1205 case PCI_DEVICE_ID_RFLY:
James Smart18a3b592006-12-02 13:35:08 -05001206 m = (typeof(m)){"LP952", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001207 break;
1208 case PCI_DEVICE_ID_PEGASUS:
James Smart18a3b592006-12-02 13:35:08 -05001209 m = (typeof(m)){"LP9802", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001210 break;
1211 case PCI_DEVICE_ID_THOR:
James Smart18a3b592006-12-02 13:35:08 -05001212 m = (typeof(m)){"LP10000", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001213 break;
1214 case PCI_DEVICE_ID_VIPER:
James Smart18a3b592006-12-02 13:35:08 -05001215 m = (typeof(m)){"LPX1000", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001216 break;
1217 case PCI_DEVICE_ID_PFLY:
James Smart18a3b592006-12-02 13:35:08 -05001218 m = (typeof(m)){"LP982", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001219 break;
1220 case PCI_DEVICE_ID_TFLY:
James Smart18a3b592006-12-02 13:35:08 -05001221 m = (typeof(m)){"LP1050", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001222 break;
1223 case PCI_DEVICE_ID_HELIOS:
James Smart18a3b592006-12-02 13:35:08 -05001224 m = (typeof(m)){"LP11000", max_speed, "PCI-X2"};
dea31012005-04-17 16:05:31 -05001225 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001226 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smart18a3b592006-12-02 13:35:08 -05001227 m = (typeof(m)){"LP11000-SP", max_speed, "PCI-X2"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001228 break;
1229 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smart18a3b592006-12-02 13:35:08 -05001230 m = (typeof(m)){"LP11002-SP", max_speed, "PCI-X2"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001231 break;
1232 case PCI_DEVICE_ID_NEPTUNE:
James Smart18a3b592006-12-02 13:35:08 -05001233 m = (typeof(m)){"LPe1000", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001234 break;
1235 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smart18a3b592006-12-02 13:35:08 -05001236 m = (typeof(m)){"LPe1000-SP", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001237 break;
1238 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smart18a3b592006-12-02 13:35:08 -05001239 m = (typeof(m)){"LPe1002-SP", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001240 break;
dea31012005-04-17 16:05:31 -05001241 case PCI_DEVICE_ID_BMID:
James Smart18a3b592006-12-02 13:35:08 -05001242 m = (typeof(m)){"LP1150", max_speed, "PCI-X2"};
dea31012005-04-17 16:05:31 -05001243 break;
1244 case PCI_DEVICE_ID_BSMB:
James Smart18a3b592006-12-02 13:35:08 -05001245 m = (typeof(m)){"LP111", max_speed, "PCI-X2"};
dea31012005-04-17 16:05:31 -05001246 break;
1247 case PCI_DEVICE_ID_ZEPHYR:
James Smart18a3b592006-12-02 13:35:08 -05001248 m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
dea31012005-04-17 16:05:31 -05001249 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001250 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smart18a3b592006-12-02 13:35:08 -05001251 m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001252 break;
1253 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smart18a3b592006-12-02 13:35:08 -05001254 m = (typeof(m)){"LPe11002-SP", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001255 break;
dea31012005-04-17 16:05:31 -05001256 case PCI_DEVICE_ID_ZMID:
James Smart18a3b592006-12-02 13:35:08 -05001257 m = (typeof(m)){"LPe1150", max_speed, "PCIe"};
dea31012005-04-17 16:05:31 -05001258 break;
1259 case PCI_DEVICE_ID_ZSMB:
James Smart18a3b592006-12-02 13:35:08 -05001260 m = (typeof(m)){"LPe111", max_speed, "PCIe"};
dea31012005-04-17 16:05:31 -05001261 break;
1262 case PCI_DEVICE_ID_LP101:
James Smart18a3b592006-12-02 13:35:08 -05001263 m = (typeof(m)){"LP101", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001264 break;
1265 case PCI_DEVICE_ID_LP10000S:
James Smart18a3b592006-12-02 13:35:08 -05001266 m = (typeof(m)){"LP10000-S", max_speed, "PCI"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001267 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001268 case PCI_DEVICE_ID_LP11000S:
James Smart18a3b592006-12-02 13:35:08 -05001269 m = (typeof(m)){"LP11000-S", max_speed,
1270 "PCI-X2"};
1271 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001272 case PCI_DEVICE_ID_LPE11000S:
James Smart18a3b592006-12-02 13:35:08 -05001273 m = (typeof(m)){"LPe11000-S", max_speed,
1274 "PCIe"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05001275 break;
James Smartb87eab32007-04-25 09:53:28 -04001276 case PCI_DEVICE_ID_SAT:
1277 m = (typeof(m)){"LPe12000", max_speed, "PCIe"};
1278 break;
1279 case PCI_DEVICE_ID_SAT_MID:
1280 m = (typeof(m)){"LPe1250", max_speed, "PCIe"};
1281 break;
1282 case PCI_DEVICE_ID_SAT_SMB:
1283 m = (typeof(m)){"LPe121", max_speed, "PCIe"};
1284 break;
1285 case PCI_DEVICE_ID_SAT_DCSP:
1286 m = (typeof(m)){"LPe12002-SP", max_speed, "PCIe"};
1287 break;
1288 case PCI_DEVICE_ID_SAT_SCSP:
1289 m = (typeof(m)){"LPe12000-SP", max_speed, "PCIe"};
1290 break;
1291 case PCI_DEVICE_ID_SAT_S:
1292 m = (typeof(m)){"LPe12000-S", max_speed, "PCIe"};
1293 break;
James Smart84774a42008-08-24 21:50:06 -04001294 case PCI_DEVICE_ID_HORNET:
1295 m = (typeof(m)){"LP21000", max_speed, "PCIe"};
1296 GE = 1;
1297 break;
1298 case PCI_DEVICE_ID_PROTEUS_VF:
1299 m = (typeof(m)) {"LPev12000", max_speed, "PCIe IOV"};
1300 break;
1301 case PCI_DEVICE_ID_PROTEUS_PF:
1302 m = (typeof(m)) {"LPev12000", max_speed, "PCIe IOV"};
1303 break;
1304 case PCI_DEVICE_ID_PROTEUS_S:
1305 m = (typeof(m)) {"LPemv12002-S", max_speed, "PCIe IOV"};
1306 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05001307 default:
Randy Dunlap041976f2006-06-25 01:58:51 -07001308 m = (typeof(m)){ NULL };
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001309 break;
dea31012005-04-17 16:05:31 -05001310 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001311
1312 if (mdp && mdp[0] == '\0')
1313 snprintf(mdp, 79,"%s", m.name);
1314 if (descp && descp[0] == '\0')
1315 snprintf(descp, 255,
James Smart84774a42008-08-24 21:50:06 -04001316 "Emulex %s %d%s %s %s",
1317 m.name, m.max_speed,
1318 (GE) ? "GE" : "Gb",
1319 m.bus,
1320 (GE) ? "FCoE Adapter" : "Fibre Channel Adapter");
dea31012005-04-17 16:05:31 -05001321}
1322
James Smarte59058c2008-08-24 21:49:00 -04001323/**
1324 * lpfc_post_buffer: Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring.
1325 * @phba: pointer to lpfc hba data structure.
1326 * @pring: pointer to a IOCB ring.
1327 * @cnt: the number of IOCBs to be posted to the IOCB ring.
1328 *
1329 * This routine posts a given number of IOCBs with the associated DMA buffer
1330 * descriptors specified by the cnt argument to the given IOCB ring.
1331 *
1332 * Return codes
1333 * The number of IOCBs NOT able to be posted to the IOCB ring.
1334 **/
dea31012005-04-17 16:05:31 -05001335int
James Smart495a7142008-06-14 22:52:59 -04001336lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05001337{
1338 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001339 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05001340 struct lpfc_dmabuf *mp1, *mp2;
1341
1342 cnt += pring->missbufcnt;
1343
1344 /* While there are buffers to post */
1345 while (cnt > 0) {
1346 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001347 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05001348 if (iocb == NULL) {
1349 pring->missbufcnt = cnt;
1350 return cnt;
1351 }
dea31012005-04-17 16:05:31 -05001352 icmd = &iocb->iocb;
1353
1354 /* 2 buffers can be posted per command */
1355 /* Allocate buffer to post */
1356 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1357 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04001358 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
1359 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001360 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05001361 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001362 pring->missbufcnt = cnt;
1363 return cnt;
1364 }
1365
1366 INIT_LIST_HEAD(&mp1->list);
1367 /* Allocate buffer to post */
1368 if (cnt > 1) {
1369 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1370 if (mp2)
1371 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
1372 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04001373 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001374 kfree(mp2);
dea31012005-04-17 16:05:31 -05001375 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1376 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05001377 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001378 pring->missbufcnt = cnt;
1379 return cnt;
1380 }
1381
1382 INIT_LIST_HEAD(&mp2->list);
1383 } else {
1384 mp2 = NULL;
1385 }
1386
1387 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
1388 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
1389 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
1390 icmd->ulpBdeCount = 1;
1391 cnt--;
1392 if (mp2) {
1393 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
1394 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
1395 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
1396 cnt--;
1397 icmd->ulpBdeCount = 2;
1398 }
1399
1400 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
1401 icmd->ulpLe = 1;
1402
dea31012005-04-17 16:05:31 -05001403 if (lpfc_sli_issue_iocb(phba, pring, iocb, 0) == IOCB_ERROR) {
1404 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1405 kfree(mp1);
1406 cnt++;
1407 if (mp2) {
1408 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
1409 kfree(mp2);
1410 cnt++;
1411 }
James Bottomley604a3e32005-10-29 10:28:33 -05001412 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001413 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05001414 return cnt;
1415 }
dea31012005-04-17 16:05:31 -05001416 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05001417 if (mp2)
dea31012005-04-17 16:05:31 -05001418 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05001419 }
1420 pring->missbufcnt = 0;
1421 return 0;
1422}
1423
James Smarte59058c2008-08-24 21:49:00 -04001424/**
1425 * lpfc_post_rcv_buf: Post the initial receive IOCB buffers to ELS ring.
1426 * @phba: pointer to lpfc hba data structure.
1427 *
1428 * This routine posts initial receive IOCB buffers to the ELS ring. The
1429 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
1430 * set to 64 IOCBs.
1431 *
1432 * Return codes
1433 * 0 - success (currently always success)
1434 **/
dea31012005-04-17 16:05:31 -05001435static int
James Smart2e0fef82007-06-17 19:56:36 -05001436lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001437{
1438 struct lpfc_sli *psli = &phba->sli;
1439
1440 /* Ring 0, ELS / CT buffers */
James Smart495a7142008-06-14 22:52:59 -04001441 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05001442 /* Ring 2 - FCP no buffers needed */
1443
1444 return 0;
1445}
1446
1447#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
1448
James Smarte59058c2008-08-24 21:49:00 -04001449/**
1450 * lpfc_sha_init: Set up initial array of hash table entries.
1451 * @HashResultPointer: pointer to an array as hash table.
1452 *
1453 * This routine sets up the initial values to the array of hash table entries
1454 * for the LC HBAs.
1455 **/
dea31012005-04-17 16:05:31 -05001456static void
1457lpfc_sha_init(uint32_t * HashResultPointer)
1458{
1459 HashResultPointer[0] = 0x67452301;
1460 HashResultPointer[1] = 0xEFCDAB89;
1461 HashResultPointer[2] = 0x98BADCFE;
1462 HashResultPointer[3] = 0x10325476;
1463 HashResultPointer[4] = 0xC3D2E1F0;
1464}
1465
James Smarte59058c2008-08-24 21:49:00 -04001466/**
1467 * lpfc_sha_iterate: Iterate initial hash table with the working hash table.
1468 * @HashResultPointer: pointer to an initial/result hash table.
1469 * @HashWorkingPointer: pointer to an working hash table.
1470 *
1471 * This routine iterates an initial hash table pointed by @HashResultPointer
1472 * with the values from the working hash table pointeed by @HashWorkingPointer.
1473 * The results are putting back to the initial hash table, returned through
1474 * the @HashResultPointer as the result hash table.
1475 **/
dea31012005-04-17 16:05:31 -05001476static void
1477lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
1478{
1479 int t;
1480 uint32_t TEMP;
1481 uint32_t A, B, C, D, E;
1482 t = 16;
1483 do {
1484 HashWorkingPointer[t] =
1485 S(1,
1486 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
1487 8] ^
1488 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
1489 } while (++t <= 79);
1490 t = 0;
1491 A = HashResultPointer[0];
1492 B = HashResultPointer[1];
1493 C = HashResultPointer[2];
1494 D = HashResultPointer[3];
1495 E = HashResultPointer[4];
1496
1497 do {
1498 if (t < 20) {
1499 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
1500 } else if (t < 40) {
1501 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
1502 } else if (t < 60) {
1503 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
1504 } else {
1505 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
1506 }
1507 TEMP += S(5, A) + E + HashWorkingPointer[t];
1508 E = D;
1509 D = C;
1510 C = S(30, B);
1511 B = A;
1512 A = TEMP;
1513 } while (++t <= 79);
1514
1515 HashResultPointer[0] += A;
1516 HashResultPointer[1] += B;
1517 HashResultPointer[2] += C;
1518 HashResultPointer[3] += D;
1519 HashResultPointer[4] += E;
1520
1521}
1522
James Smarte59058c2008-08-24 21:49:00 -04001523/**
1524 * lpfc_challenge_key: Create challenge key based on WWPN of the HBA.
1525 * @RandomChallenge: pointer to the entry of host challenge random number array.
1526 * @HashWorking: pointer to the entry of the working hash array.
1527 *
1528 * This routine calculates the working hash array referred by @HashWorking
1529 * from the challenge random numbers associated with the host, referred by
1530 * @RandomChallenge. The result is put into the entry of the working hash
1531 * array and returned by reference through @HashWorking.
1532 **/
dea31012005-04-17 16:05:31 -05001533static void
1534lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
1535{
1536 *HashWorking = (*RandomChallenge ^ *HashWorking);
1537}
1538
James Smarte59058c2008-08-24 21:49:00 -04001539/**
1540 * lpfc_hba_init: Perform special handling for LC HBA initialization.
1541 * @phba: pointer to lpfc hba data structure.
1542 * @hbainit: pointer to an array of unsigned 32-bit integers.
1543 *
1544 * This routine performs the special handling for LC HBA initialization.
1545 **/
dea31012005-04-17 16:05:31 -05001546void
1547lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
1548{
1549 int t;
1550 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05001551 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05001552
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02001553 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05001554 if (!HashWorking)
1555 return;
1556
dea31012005-04-17 16:05:31 -05001557 HashWorking[0] = HashWorking[78] = *pwwnn++;
1558 HashWorking[1] = HashWorking[79] = *pwwnn;
1559
1560 for (t = 0; t < 7; t++)
1561 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
1562
1563 lpfc_sha_init(hbainit);
1564 lpfc_sha_iterate(hbainit, HashWorking);
1565 kfree(HashWorking);
1566}
1567
James Smarte59058c2008-08-24 21:49:00 -04001568/**
1569 * lpfc_cleanup: Performs vport cleanups before deleting a vport.
1570 * @vport: pointer to a virtual N_Port data structure.
1571 *
1572 * This routine performs the necessary cleanups before deleting the @vport.
1573 * It invokes the discovery state machine to perform necessary state
1574 * transitions and to release the ndlps associated with the @vport. Note,
1575 * the physical port is treated as @vport 0.
1576 **/
James Smart87af33f2007-10-27 13:37:43 -04001577void
James Smart2e0fef82007-06-17 19:56:36 -05001578lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05001579{
James Smart87af33f2007-10-27 13:37:43 -04001580 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001581 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04001582 int i = 0;
dea31012005-04-17 16:05:31 -05001583
James Smart87af33f2007-10-27 13:37:43 -04001584 if (phba->link_state > LPFC_LINK_DOWN)
1585 lpfc_port_link_failure(vport);
1586
1587 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05001588 if (!NLP_CHK_NODE_ACT(ndlp)) {
1589 ndlp = lpfc_enable_node(vport, ndlp,
1590 NLP_STE_UNUSED_NODE);
1591 if (!ndlp)
1592 continue;
1593 spin_lock_irq(&phba->ndlp_lock);
1594 NLP_SET_FREE_REQ(ndlp);
1595 spin_unlock_irq(&phba->ndlp_lock);
1596 /* Trigger the release of the ndlp memory */
1597 lpfc_nlp_put(ndlp);
1598 continue;
1599 }
1600 spin_lock_irq(&phba->ndlp_lock);
1601 if (NLP_CHK_FREE_REQ(ndlp)) {
1602 /* The ndlp should not be in memory free mode already */
1603 spin_unlock_irq(&phba->ndlp_lock);
1604 continue;
1605 } else
1606 /* Indicate request for freeing ndlp memory */
1607 NLP_SET_FREE_REQ(ndlp);
1608 spin_unlock_irq(&phba->ndlp_lock);
1609
James Smart58da1ff2008-04-07 10:15:56 -04001610 if (vport->port_type != LPFC_PHYSICAL_PORT &&
1611 ndlp->nlp_DID == Fabric_DID) {
1612 /* Just free up ndlp with Fabric_DID for vports */
1613 lpfc_nlp_put(ndlp);
1614 continue;
1615 }
1616
James Smart87af33f2007-10-27 13:37:43 -04001617 if (ndlp->nlp_type & NLP_FABRIC)
1618 lpfc_disc_state_machine(vport, ndlp, NULL,
1619 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05001620
James Smart87af33f2007-10-27 13:37:43 -04001621 lpfc_disc_state_machine(vport, ndlp, NULL,
1622 NLP_EVT_DEVICE_RM);
James Smart495a7142008-06-14 22:52:59 -04001623
James Smart87af33f2007-10-27 13:37:43 -04001624 }
1625
James Smarta8adb832007-10-27 13:37:53 -04001626 /* At this point, ALL ndlp's should be gone
1627 * because of the previous NLP_EVT_DEVICE_RM.
1628 * Lets wait for this to happen, if needed.
1629 */
James Smart87af33f2007-10-27 13:37:43 -04001630 while (!list_empty(&vport->fc_nodes)) {
1631
James Smarta8adb832007-10-27 13:37:53 -04001632 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04001633 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04001634 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05001635 list_for_each_entry_safe(ndlp, next_ndlp,
1636 &vport->fc_nodes, nlp_listp) {
1637 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
1638 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04001639 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05001640 "usgmap:x%x refcnt:%d\n",
1641 ndlp->nlp_DID, (void *)ndlp,
1642 ndlp->nlp_usg_map,
1643 atomic_read(
1644 &ndlp->kref.refcount));
1645 }
James Smarta8adb832007-10-27 13:37:53 -04001646 break;
James Smart87af33f2007-10-27 13:37:43 -04001647 }
James Smarta8adb832007-10-27 13:37:53 -04001648
1649 /* Wait for any activity on ndlps to settle */
1650 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04001651 }
dea31012005-04-17 16:05:31 -05001652 return;
1653}
1654
James Smarte59058c2008-08-24 21:49:00 -04001655/**
1656 * lpfc_stop_vport_timers: Stop all the timers associated with a vport.
1657 * @vport: pointer to a virtual N_Port data structure.
1658 *
1659 * This routine stops all the timers associated with a @vport. This function
1660 * is invoked before disabling or deleting a @vport. Note that the physical
1661 * port is treated as @vport 0.
1662 **/
James Smart92d7f7b2007-06-17 19:56:38 -05001663void
1664lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05001665{
James Smart92d7f7b2007-06-17 19:56:38 -05001666 del_timer_sync(&vport->els_tmofunc);
1667 del_timer_sync(&vport->fc_fdmitmo);
1668 lpfc_can_disctmo(vport);
1669 return;
dea31012005-04-17 16:05:31 -05001670}
1671
James Smarte59058c2008-08-24 21:49:00 -04001672/**
1673 * lpfc_stop_phba_timers: Stop all the timers associated with an HBA.
1674 * @phba: pointer to lpfc hba data structure.
1675 *
1676 * This routine stops all the timers associated with a HBA. This function is
1677 * invoked before either putting a HBA offline or unloading the driver.
1678 **/
James Smart2e0fef82007-06-17 19:56:36 -05001679static void
James Smart92d7f7b2007-06-17 19:56:38 -05001680lpfc_stop_phba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001681{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001682 del_timer_sync(&phba->fcp_poll_timer);
James Smart51ef4c22007-08-02 11:10:31 -04001683 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05001684 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05001685 del_timer_sync(&phba->fabric_block_timer);
James Smart858c9f62007-06-17 19:56:39 -05001686 phba->hb_outstanding = 0;
1687 del_timer_sync(&phba->hb_tmofunc);
James Smart93996272008-08-24 21:50:30 -04001688 del_timer_sync(&phba->eratt_poll);
James Smart2e0fef82007-06-17 19:56:36 -05001689 return;
dea31012005-04-17 16:05:31 -05001690}
1691
James Smarte59058c2008-08-24 21:49:00 -04001692/**
1693 * lpfc_block_mgmt_io: Mark a HBA's management interface as blocked.
1694 * @phba: pointer to lpfc hba data structure.
1695 *
1696 * This routine marks a HBA's management interface as blocked. Once the HBA's
1697 * management interface is marked as blocked, all the user space access to
1698 * the HBA, whether they are from sysfs interface or libdfc interface will
1699 * all be blocked. The HBA is set to block the management interface when the
1700 * driver prepares the HBA interface for online or offline.
1701 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001702static void
1703lpfc_block_mgmt_io(struct lpfc_hba * phba)
1704{
1705 unsigned long iflag;
1706
1707 spin_lock_irqsave(&phba->hbalock, iflag);
1708 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
1709 spin_unlock_irqrestore(&phba->hbalock, iflag);
1710}
1711
James Smarte59058c2008-08-24 21:49:00 -04001712/**
1713 * lpfc_online: Initialize and bring a HBA online.
1714 * @phba: pointer to lpfc hba data structure.
1715 *
1716 * This routine initializes the HBA and brings a HBA online. During this
1717 * process, the management interface is blocked to prevent user space access
1718 * to the HBA interfering with the driver initialization.
1719 *
1720 * Return codes
1721 * 0 - successful
1722 * 1 - failed
1723 **/
dea31012005-04-17 16:05:31 -05001724int
James Smart2e0fef82007-06-17 19:56:36 -05001725lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001726{
James Smart2e0fef82007-06-17 19:56:36 -05001727 struct lpfc_vport *vport = phba->pport;
James Smart549e55c2007-08-02 11:09:51 -04001728 struct lpfc_vport **vports;
1729 int i;
James Smart2e0fef82007-06-17 19:56:36 -05001730
dea31012005-04-17 16:05:31 -05001731 if (!phba)
1732 return 0;
1733
James Smart2e0fef82007-06-17 19:56:36 -05001734 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05001735 return 0;
1736
James Smarted957682007-06-17 19:56:37 -05001737 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001738 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05001739
James Smart46fa3112007-04-25 09:51:45 -04001740 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001741
James Smart46fa3112007-04-25 09:51:45 -04001742 if (!lpfc_sli_queue_setup(phba)) {
1743 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001744 return 1;
James Smart46fa3112007-04-25 09:51:45 -04001745 }
1746
1747 if (lpfc_sli_hba_setup(phba)) { /* Initialize the HBA */
1748 lpfc_unblock_mgmt_io(phba);
1749 return 1;
1750 }
dea31012005-04-17 16:05:31 -05001751
James Smart549e55c2007-08-02 11:09:51 -04001752 vports = lpfc_create_vport_work_array(phba);
1753 if (vports != NULL)
James Smart09372822008-01-11 01:52:54 -05001754 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04001755 struct Scsi_Host *shost;
1756 shost = lpfc_shost_from_vport(vports[i]);
1757 spin_lock_irq(shost->host_lock);
1758 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
1759 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
1760 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
1761 spin_unlock_irq(shost->host_lock);
1762 }
James Smart09372822008-01-11 01:52:54 -05001763 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05001764
James Smart46fa3112007-04-25 09:51:45 -04001765 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001766 return 0;
1767}
1768
James Smarte59058c2008-08-24 21:49:00 -04001769/**
1770 * lpfc_unblock_mgmt_io: Mark a HBA's management interface to be not blocked.
1771 * @phba: pointer to lpfc hba data structure.
1772 *
1773 * This routine marks a HBA's management interface as not blocked. Once the
1774 * HBA's management interface is marked as not blocked, all the user space
1775 * access to the HBA, whether they are from sysfs interface or libdfc
1776 * interface will be allowed. The HBA is set to block the management interface
1777 * when the driver prepares the HBA interface for online or offline and then
1778 * set to unblock the management interface afterwards.
1779 **/
James Smart46fa3112007-04-25 09:51:45 -04001780void
James Smart46fa3112007-04-25 09:51:45 -04001781lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
1782{
1783 unsigned long iflag;
1784
James Smart2e0fef82007-06-17 19:56:36 -05001785 spin_lock_irqsave(&phba->hbalock, iflag);
1786 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
1787 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04001788}
1789
James Smarte59058c2008-08-24 21:49:00 -04001790/**
1791 * lpfc_offline_prep: Prepare a HBA to be brought offline.
1792 * @phba: pointer to lpfc hba data structure.
1793 *
1794 * This routine is invoked to prepare a HBA to be brought offline. It performs
1795 * unregistration login to all the nodes on all vports and flushes the mailbox
1796 * queue to make it ready to be brought offline.
1797 **/
James Smart46fa3112007-04-25 09:51:45 -04001798void
1799lpfc_offline_prep(struct lpfc_hba * phba)
1800{
James Smart2e0fef82007-06-17 19:56:36 -05001801 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04001802 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04001803 struct lpfc_vport **vports;
1804 int i;
dea31012005-04-17 16:05:31 -05001805
James Smart2e0fef82007-06-17 19:56:36 -05001806 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04001807 return;
dea31012005-04-17 16:05:31 -05001808
James Smart46fa3112007-04-25 09:51:45 -04001809 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001810
1811 lpfc_linkdown(phba);
1812
James Smart87af33f2007-10-27 13:37:43 -04001813 /* Issue an unreg_login to all nodes on all vports */
1814 vports = lpfc_create_vport_work_array(phba);
1815 if (vports != NULL) {
James Smart09372822008-01-11 01:52:54 -05001816 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
James Smart87af33f2007-10-27 13:37:43 -04001817 struct Scsi_Host *shost;
1818
James Smarta8adb832007-10-27 13:37:53 -04001819 if (vports[i]->load_flag & FC_UNLOADING)
1820 continue;
James Smart87af33f2007-10-27 13:37:43 -04001821 shost = lpfc_shost_from_vport(vports[i]);
1822 list_for_each_entry_safe(ndlp, next_ndlp,
1823 &vports[i]->fc_nodes,
1824 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05001825 if (!NLP_CHK_NODE_ACT(ndlp))
1826 continue;
James Smart87af33f2007-10-27 13:37:43 -04001827 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
1828 continue;
1829 if (ndlp->nlp_type & NLP_FABRIC) {
1830 lpfc_disc_state_machine(vports[i], ndlp,
1831 NULL, NLP_EVT_DEVICE_RECOVERY);
1832 lpfc_disc_state_machine(vports[i], ndlp,
1833 NULL, NLP_EVT_DEVICE_RM);
1834 }
1835 spin_lock_irq(shost->host_lock);
1836 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
1837 spin_unlock_irq(shost->host_lock);
1838 lpfc_unreg_rpi(vports[i], ndlp);
1839 }
1840 }
1841 }
James Smart09372822008-01-11 01:52:54 -05001842 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05001843
James Smart46fa3112007-04-25 09:51:45 -04001844 lpfc_sli_flush_mbox_queue(phba);
1845}
1846
James Smarte59058c2008-08-24 21:49:00 -04001847/**
1848 * lpfc_offline: Bring a HBA offline.
1849 * @phba: pointer to lpfc hba data structure.
1850 *
1851 * This routine actually brings a HBA offline. It stops all the timers
1852 * associated with the HBA, brings down the SLI layer, and eventually
1853 * marks the HBA as in offline state for the upper layer protocol.
1854 **/
James Smart46fa3112007-04-25 09:51:45 -04001855void
James Smart2e0fef82007-06-17 19:56:36 -05001856lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04001857{
James Smart549e55c2007-08-02 11:09:51 -04001858 struct Scsi_Host *shost;
1859 struct lpfc_vport **vports;
1860 int i;
James Smart46fa3112007-04-25 09:51:45 -04001861
James Smart549e55c2007-08-02 11:09:51 -04001862 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04001863 return;
James Smart688a8862006-07-06 15:49:56 -04001864
dea31012005-04-17 16:05:31 -05001865 /* stop all timers associated with this hba */
James Smart92d7f7b2007-06-17 19:56:38 -05001866 lpfc_stop_phba_timers(phba);
James Smart51ef4c22007-08-02 11:10:31 -04001867 vports = lpfc_create_vport_work_array(phba);
1868 if (vports != NULL)
James Smart09372822008-01-11 01:52:54 -05001869 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04001870 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05001871 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05001872 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001873 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05001874 /* Bring down the SLI Layer and cleanup. The HBA is offline
1875 now. */
1876 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001877 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04001878 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001879 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04001880 vports = lpfc_create_vport_work_array(phba);
1881 if (vports != NULL)
James Smart09372822008-01-11 01:52:54 -05001882 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04001883 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04001884 spin_lock_irq(shost->host_lock);
1885 vports[i]->work_port_events = 0;
1886 vports[i]->fc_flag |= FC_OFFLINE_MODE;
1887 spin_unlock_irq(shost->host_lock);
1888 }
James Smart09372822008-01-11 01:52:54 -05001889 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05001890}
1891
James Smarte59058c2008-08-24 21:49:00 -04001892/**
1893 * lpfc_scsi_free: Free all the SCSI buffers and IOCBs from driver lists.
1894 * @phba: pointer to lpfc hba data structure.
1895 *
1896 * This routine is to free all the SCSI buffers and IOCBs from the driver
1897 * list back to kernel. It is called from lpfc_pci_remove_one to free
1898 * the internal resources before the device is removed from the system.
1899 *
1900 * Return codes
1901 * 0 - successful (for now, it always returns 0)
1902 **/
dea31012005-04-17 16:05:31 -05001903static int
James Smart2e0fef82007-06-17 19:56:36 -05001904lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001905{
1906 struct lpfc_scsi_buf *sb, *sb_next;
1907 struct lpfc_iocbq *io, *io_next;
1908
James Smart2e0fef82007-06-17 19:56:36 -05001909 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001910 /* Release all the lpfc_scsi_bufs maintained by this host. */
1911 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
1912 list_del(&sb->list);
1913 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05001914 sb->dma_handle);
dea31012005-04-17 16:05:31 -05001915 kfree(sb);
1916 phba->total_scsi_bufs--;
1917 }
1918
1919 /* Release all the lpfc_iocbq entries maintained by this host. */
1920 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
1921 list_del(&io->list);
1922 kfree(io);
1923 phba->total_iocbq_bufs--;
1924 }
1925
James Smart2e0fef82007-06-17 19:56:36 -05001926 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001927
1928 return 0;
1929}
1930
James Smarte59058c2008-08-24 21:49:00 -04001931/**
1932 * lpfc_create_port: Create an FC port.
1933 * @phba: pointer to lpfc hba data structure.
1934 * @instance: a unique integer ID to this FC port.
1935 * @dev: pointer to the device data structure.
1936 *
1937 * This routine creates a FC port for the upper layer protocol. The FC port
1938 * can be created on top of either a physical port or a virtual port provided
1939 * by the HBA. This routine also allocates a SCSI host data structure (shost)
1940 * and associates the FC port created before adding the shost into the SCSI
1941 * layer.
1942 *
1943 * Return codes
1944 * @vport - pointer to the virtual N_Port data structure.
1945 * NULL - port create failed.
1946 **/
James Smart2e0fef82007-06-17 19:56:36 -05001947struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04001948lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04001949{
James Smart2e0fef82007-06-17 19:56:36 -05001950 struct lpfc_vport *vport;
1951 struct Scsi_Host *shost;
1952 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04001953
James Smart3de2a652007-08-02 11:09:59 -04001954 if (dev != &phba->pcidev->dev)
1955 shost = scsi_host_alloc(&lpfc_vport_template,
1956 sizeof(struct lpfc_vport));
1957 else
1958 shost = scsi_host_alloc(&lpfc_template,
1959 sizeof(struct lpfc_vport));
James Smart2e0fef82007-06-17 19:56:36 -05001960 if (!shost)
1961 goto out;
James Smart47a86172007-04-25 09:53:22 -04001962
James Smart2e0fef82007-06-17 19:56:36 -05001963 vport = (struct lpfc_vport *) shost->hostdata;
1964 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05001965 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05001966 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05001967 vport->fc_rscn_flush = 0;
James Smart47a86172007-04-25 09:53:22 -04001968
James Smart3de2a652007-08-02 11:09:59 -04001969 lpfc_get_vport_cfgparam(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001970 shost->unique_id = instance;
1971 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04001972 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05001973 shost->this_id = -1;
1974 shost->max_cmd_len = 16;
James Smart47a86172007-04-25 09:53:22 -04001975 /*
James Smart2e0fef82007-06-17 19:56:36 -05001976 * Set initial can_queue value since 0 is no longer supported and
1977 * scsi_add_host will fail. This will be adjusted later based on the
1978 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04001979 */
James Smart2e0fef82007-06-17 19:56:36 -05001980 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04001981 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05001982 shost->transportt = lpfc_vport_transport_template;
1983 vport->port_type = LPFC_NPIV_PORT;
1984 } else {
1985 shost->transportt = lpfc_transport_template;
1986 vport->port_type = LPFC_PHYSICAL_PORT;
1987 }
James Smart47a86172007-04-25 09:53:22 -04001988
James Smart2e0fef82007-06-17 19:56:36 -05001989 /* Initialize all internally managed lists. */
1990 INIT_LIST_HEAD(&vport->fc_nodes);
1991 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04001992
James Smart2e0fef82007-06-17 19:56:36 -05001993 init_timer(&vport->fc_disctmo);
1994 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05001995 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04001996
James Smart2e0fef82007-06-17 19:56:36 -05001997 init_timer(&vport->fc_fdmitmo);
1998 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
James Smart92d7f7b2007-06-17 19:56:38 -05001999 vport->fc_fdmitmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002000
James Smart2e0fef82007-06-17 19:56:36 -05002001 init_timer(&vport->els_tmofunc);
2002 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05002003 vport->els_tmofunc.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002004
James Smart3de2a652007-08-02 11:09:59 -04002005 error = scsi_add_host(shost, dev);
James Smart2e0fef82007-06-17 19:56:36 -05002006 if (error)
2007 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04002008
James Smart549e55c2007-08-02 11:09:51 -04002009 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002010 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04002011 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002012 return vport;
James Smart47a86172007-04-25 09:53:22 -04002013
James Smart2e0fef82007-06-17 19:56:36 -05002014out_put_shost:
2015 scsi_host_put(shost);
2016out:
2017 return NULL;
James Smart47a86172007-04-25 09:53:22 -04002018}
2019
James Smarte59058c2008-08-24 21:49:00 -04002020/**
2021 * destroy_port: Destroy an FC port.
2022 * @vport: pointer to an lpfc virtual N_Port data structure.
2023 *
2024 * This routine destroys a FC port from the upper layer protocol. All the
2025 * resources associated with the port are released.
2026 **/
James Smart2e0fef82007-06-17 19:56:36 -05002027void
2028destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04002029{
James Smart92d7f7b2007-06-17 19:56:38 -05002030 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2031 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002032
James Smart92d7f7b2007-06-17 19:56:38 -05002033 kfree(vport->vname);
James Smart47a86172007-04-25 09:53:22 -04002034
James Smart858c9f62007-06-17 19:56:39 -05002035 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05002036 fc_remove_host(shost);
2037 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04002038
James Smart92d7f7b2007-06-17 19:56:38 -05002039 spin_lock_irq(&phba->hbalock);
2040 list_del_init(&vport->listentry);
2041 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04002042
James Smart92d7f7b2007-06-17 19:56:38 -05002043 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04002044 return;
James Smart47a86172007-04-25 09:53:22 -04002045}
2046
James Smarte59058c2008-08-24 21:49:00 -04002047/**
2048 * lpfc_get_instance: Get a unique integer ID.
2049 *
2050 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
2051 * uses the kernel idr facility to perform the task.
2052 *
2053 * Return codes:
2054 * instance - a unique integer ID allocated as the new instance.
2055 * -1 - lpfc get instance failed.
2056 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002057int
2058lpfc_get_instance(void)
2059{
2060 int instance = 0;
2061
2062 /* Assign an unused number */
2063 if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
2064 return -1;
2065 if (idr_get_new(&lpfc_hba_index, NULL, &instance))
2066 return -1;
2067 return instance;
2068}
2069
James Smarte59058c2008-08-24 21:49:00 -04002070/**
2071 * lpfc_scan_finished: method for SCSI layer to detect whether scan is done.
2072 * @shost: pointer to SCSI host data structure.
2073 * @time: elapsed time of the scan in jiffies.
2074 *
2075 * This routine is called by the SCSI layer with a SCSI host to determine
2076 * whether the scan host is finished.
2077 *
2078 * Note: there is no scan_start function as adapter initialization will have
2079 * asynchronously kicked off the link initialization.
2080 *
2081 * Return codes
2082 * 0 - SCSI host scan is not over yet.
2083 * 1 - SCSI host scan is over.
2084 **/
James Smart47a86172007-04-25 09:53:22 -04002085int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2086{
James Smart2e0fef82007-06-17 19:56:36 -05002087 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2088 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05002089 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04002090
James Smart858c9f62007-06-17 19:56:39 -05002091 spin_lock_irq(shost->host_lock);
2092
James Smart51ef4c22007-08-02 11:10:31 -04002093 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05002094 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002095 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05002096 }
James Smart2e0fef82007-06-17 19:56:36 -05002097 if (time >= 30 * HZ) {
2098 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002099 "0461 Scanning longer than 30 "
2100 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05002101 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002102 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002103 }
2104 if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
2105 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002106 "0465 Link down longer than 15 "
2107 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05002108 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05002109 goto finished;
James Smart47a86172007-04-25 09:53:22 -04002110 }
2111
James Smart2e0fef82007-06-17 19:56:36 -05002112 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05002113 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002114 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05002115 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002116 if (vport->fc_map_cnt == 0 && time < 2 * HZ)
James Smart858c9f62007-06-17 19:56:39 -05002117 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002118 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05002119 goto finished;
2120
2121 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002122
2123finished:
James Smart858c9f62007-06-17 19:56:39 -05002124 spin_unlock_irq(shost->host_lock);
2125 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05002126}
2127
James Smarte59058c2008-08-24 21:49:00 -04002128/**
2129 * lpfc_host_attrib_init: Initialize SCSI host attributes on a FC port.
2130 * @shost: pointer to SCSI host data structure.
2131 *
2132 * This routine initializes a given SCSI host attributes on a FC port. The
2133 * SCSI host can be either on top of a physical port or a virtual port.
2134 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002135void lpfc_host_attrib_init(struct Scsi_Host *shost)
2136{
2137 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2138 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002139 /*
James Smart2e0fef82007-06-17 19:56:36 -05002140 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04002141 */
2142
James Smart2e0fef82007-06-17 19:56:36 -05002143 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
2144 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04002145 fc_host_supported_classes(shost) = FC_COS_CLASS3;
2146
2147 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05002148 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04002149 fc_host_supported_fc4s(shost)[2] = 1;
2150 fc_host_supported_fc4s(shost)[7] = 1;
2151
James Smart92d7f7b2007-06-17 19:56:38 -05002152 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
2153 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04002154
2155 fc_host_supported_speeds(shost) = 0;
2156 if (phba->lmt & LMT_10Gb)
2157 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04002158 if (phba->lmt & LMT_8Gb)
2159 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04002160 if (phba->lmt & LMT_4Gb)
2161 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
2162 if (phba->lmt & LMT_2Gb)
2163 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
2164 if (phba->lmt & LMT_1Gb)
2165 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
2166
2167 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05002168 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
2169 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04002170
2171 /* This value is also unchanging */
2172 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05002173 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04002174 fc_host_active_fc4s(shost)[2] = 1;
2175 fc_host_active_fc4s(shost)[7] = 1;
2176
James Smart92d7f7b2007-06-17 19:56:38 -05002177 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04002178 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04002179 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04002180 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04002181}
dea31012005-04-17 16:05:31 -05002182
James Smarte59058c2008-08-24 21:49:00 -04002183/**
2184 * lpfc_enable_msix: Enable MSI-X interrupt mode.
2185 * @phba: pointer to lpfc hba data structure.
2186 *
2187 * This routine is invoked to enable the MSI-X interrupt vectors. The kernel
2188 * function pci_enable_msix() is called to enable the MSI-X vectors. Note that
2189 * pci_enable_msix(), once invoked, enables either all or nothing, depending
2190 * on the current availability of PCI vector resources. The device driver is
2191 * responsible for calling the individual request_irq() to register each MSI-X
2192 * vector with a interrupt handler, which is done in this function. Note that
2193 * later when device is unloading, the driver should always call free_irq()
2194 * on all MSI-X vectors it has done request_irq() on before calling
2195 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
2196 * will be left with MSI-X enabled and leaks its vectors.
2197 *
2198 * Return codes
2199 * 0 - sucessful
2200 * other values - error
2201 **/
James Smartdb2378e2008-02-08 18:49:51 -05002202static int
2203lpfc_enable_msix(struct lpfc_hba *phba)
2204{
James Smart93996272008-08-24 21:50:30 -04002205 int rc, i;
2206 LPFC_MBOXQ_t *pmb;
James Smartdb2378e2008-02-08 18:49:51 -05002207
James Smart93996272008-08-24 21:50:30 -04002208 /* Set up MSI-X multi-message vectors */
2209 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
2210 phba->msix_entries[i].entry = i;
James Smartdb2378e2008-02-08 18:49:51 -05002211
James Smart93996272008-08-24 21:50:30 -04002212 /* Configure MSI-X capability structure */
2213 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
James Smartdb2378e2008-02-08 18:49:51 -05002214 ARRAY_SIZE(phba->msix_entries));
James Smart93996272008-08-24 21:50:30 -04002215 if (rc) {
James Smartdb2378e2008-02-08 18:49:51 -05002216 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2217 "0420 Enable MSI-X failed (%d), continuing "
James Smart93996272008-08-24 21:50:30 -04002218 "with MSI\n", rc);
2219 goto msi_fail_out;
2220 } else
2221 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
2222 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2223 "0477 MSI-X entry[%d]: vector=x%x "
2224 "message=%d\n", i,
2225 phba->msix_entries[i].vector,
2226 phba->msix_entries[i].entry);
2227 /*
2228 * Assign MSI-X vectors to interrupt handlers
2229 */
2230
2231 /* vector-0 is associated to slow-path handler */
2232 rc = request_irq(phba->msix_entries[0].vector, &lpfc_sp_intr_handler,
2233 IRQF_SHARED, LPFC_SP_DRIVER_HANDLER_NAME, phba);
2234 if (rc) {
2235 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2236 "0421 MSI-X slow-path request_irq failed "
2237 "(%d), continuing with MSI\n", rc);
2238 goto msi_fail_out;
James Smartdb2378e2008-02-08 18:49:51 -05002239 }
2240
James Smart93996272008-08-24 21:50:30 -04002241 /* vector-1 is associated to fast-path handler */
2242 rc = request_irq(phba->msix_entries[1].vector, &lpfc_fp_intr_handler,
2243 IRQF_SHARED, LPFC_FP_DRIVER_HANDLER_NAME, phba);
2244
2245 if (rc) {
James Smartdb2378e2008-02-08 18:49:51 -05002246 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart93996272008-08-24 21:50:30 -04002247 "0429 MSI-X fast-path request_irq failed "
2248 "(%d), continuing with MSI\n", rc);
2249 goto irq_fail_out;
James Smartdb2378e2008-02-08 18:49:51 -05002250 }
James Smart93996272008-08-24 21:50:30 -04002251
2252 /*
2253 * Configure HBA MSI-X attention conditions to messages
2254 */
2255 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2256
2257 if (!pmb) {
2258 rc = -ENOMEM;
2259 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2260 "0474 Unable to allocate memory for issuing "
2261 "MBOX_CONFIG_MSI command\n");
2262 goto mem_fail_out;
2263 }
2264 rc = lpfc_config_msi(phba, pmb);
2265 if (rc)
2266 goto mbx_fail_out;
2267 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
2268 if (rc != MBX_SUCCESS) {
2269 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
2270 "0351 Config MSI mailbox command failed, "
2271 "mbxCmd x%x, mbxStatus x%x\n",
2272 pmb->mb.mbxCommand, pmb->mb.mbxStatus);
2273 goto mbx_fail_out;
2274 }
2275
2276 /* Free memory allocated for mailbox command */
2277 mempool_free(pmb, phba->mbox_mem_pool);
2278 return rc;
2279
2280mbx_fail_out:
2281 /* Free memory allocated for mailbox command */
2282 mempool_free(pmb, phba->mbox_mem_pool);
2283
2284mem_fail_out:
2285 /* free the irq already requested */
2286 free_irq(phba->msix_entries[1].vector, phba);
2287
2288irq_fail_out:
2289 /* free the irq already requested */
2290 free_irq(phba->msix_entries[0].vector, phba);
2291
2292msi_fail_out:
2293 /* Unconfigure MSI-X capability structure */
2294 pci_disable_msix(phba->pcidev);
2295 return rc;
James Smartdb2378e2008-02-08 18:49:51 -05002296}
2297
James Smarte59058c2008-08-24 21:49:00 -04002298/**
2299 * lpfc_disable_msix: Disable MSI-X interrupt mode.
2300 * @phba: pointer to lpfc hba data structure.
2301 *
2302 * This routine is invoked to release the MSI-X vectors and then disable the
2303 * MSI-X interrupt mode.
2304 **/
James Smartdb2378e2008-02-08 18:49:51 -05002305static void
2306lpfc_disable_msix(struct lpfc_hba *phba)
2307{
James Smart93996272008-08-24 21:50:30 -04002308 int i;
2309
2310 /* Free up MSI-X multi-message vectors */
2311 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
2312 free_irq(phba->msix_entries[i].vector, phba);
2313 /* Disable MSI-X */
James Smartdb2378e2008-02-08 18:49:51 -05002314 pci_disable_msix(phba->pcidev);
2315}
2316
James Smarte59058c2008-08-24 21:49:00 -04002317/**
2318 * lpfc_pci_probe_one: lpfc PCI probe func to register device to PCI subsystem.
2319 * @pdev: pointer to PCI device
2320 * @pid: pointer to PCI device identifier
2321 *
2322 * This routine is to be registered to the kernel's PCI subsystem. When an
2323 * Emulex HBA is presented in PCI bus, the kernel PCI subsystem looks at
2324 * PCI device-specific information of the device and driver to see if the
2325 * driver state that it can support this kind of device. If the match is
2326 * successful, the driver core invokes this routine. If this routine
2327 * determines it can claim the HBA, it does all the initialization that it
2328 * needs to do to handle the HBA properly.
2329 *
2330 * Return code
2331 * 0 - driver can claim the device
2332 * negative value - driver can not claim the device
2333 **/
dea31012005-04-17 16:05:31 -05002334static int __devinit
2335lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
2336{
James Smart2e0fef82007-06-17 19:56:36 -05002337 struct lpfc_vport *vport = NULL;
2338 struct lpfc_hba *phba;
2339 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05002340 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05002341 struct Scsi_Host *shost = NULL;
James Smart51ef4c22007-08-02 11:10:31 -04002342 void *ptr;
dea31012005-04-17 16:05:31 -05002343 unsigned long bar0map_len, bar2map_len;
James Smart98c9ea52007-10-27 13:37:33 -04002344 int error = -ENODEV, retval;
James Smart51ef4c22007-08-02 11:10:31 -04002345 int i, hbq_count;
James Bottomley604a3e32005-10-29 10:28:33 -05002346 uint16_t iotag;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05002347 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
dea31012005-04-17 16:05:31 -05002348
Greg Kroah-Hartmand5f78fb2008-02-02 12:13:22 +01002349 if (pci_enable_device_mem(pdev))
dea31012005-04-17 16:05:31 -05002350 goto out;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05002351 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
dea31012005-04-17 16:05:31 -05002352 goto out_disable_device;
2353
James Smart2e0fef82007-06-17 19:56:36 -05002354 phba = kzalloc(sizeof (struct lpfc_hba), GFP_KERNEL);
2355 if (!phba)
dea31012005-04-17 16:05:31 -05002356 goto out_release_regions;
2357
James Smart2e0fef82007-06-17 19:56:36 -05002358 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002359
James Smarte47c9092008-02-08 18:49:26 -05002360 /* Initialize ndlp management spinlock */
2361 spin_lock_init(&phba->ndlp_lock);
2362
dea31012005-04-17 16:05:31 -05002363 phba->pcidev = pdev;
2364
2365 /* Assign an unused board number */
James Smart92d7f7b2007-06-17 19:56:38 -05002366 if ((phba->brd_no = lpfc_get_instance()) < 0)
James Smart2e0fef82007-06-17 19:56:36 -05002367 goto out_free_phba;
dea31012005-04-17 16:05:31 -05002368
James Smart2e0fef82007-06-17 19:56:36 -05002369 INIT_LIST_HEAD(&phba->port_list);
James Smart84774a42008-08-24 21:50:06 -04002370 init_waitqueue_head(&phba->wait_4_mlo_m_q);
James Smart2e0fef82007-06-17 19:56:36 -05002371 /*
2372 * Get all the module params for configuring this host and then
2373 * establish the host.
2374 */
2375 lpfc_get_cfgparam(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002376 phba->max_vpi = LPFC_MAX_VPI;
dea31012005-04-17 16:05:31 -05002377
2378 /* Initialize timers used by driver */
James Smart858c9f62007-06-17 19:56:39 -05002379 init_timer(&phba->hb_tmofunc);
2380 phba->hb_tmofunc.function = lpfc_hb_timeout;
2381 phba->hb_tmofunc.data = (unsigned long)phba;
2382
dea31012005-04-17 16:05:31 -05002383 psli = &phba->sli;
2384 init_timer(&psli->mbox_tmo);
2385 psli->mbox_tmo.function = lpfc_mbox_timeout;
James Smart2e0fef82007-06-17 19:56:36 -05002386 psli->mbox_tmo.data = (unsigned long) phba;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002387 init_timer(&phba->fcp_poll_timer);
2388 phba->fcp_poll_timer.function = lpfc_poll_timeout;
James Smart2e0fef82007-06-17 19:56:36 -05002389 phba->fcp_poll_timer.data = (unsigned long) phba;
James Smart92d7f7b2007-06-17 19:56:38 -05002390 init_timer(&phba->fabric_block_timer);
2391 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
2392 phba->fabric_block_timer.data = (unsigned long) phba;
James Smart93996272008-08-24 21:50:30 -04002393 init_timer(&phba->eratt_poll);
2394 phba->eratt_poll.function = lpfc_poll_eratt;
2395 phba->eratt_poll.data = (unsigned long) phba;
dea31012005-04-17 16:05:31 -05002396
dea31012005-04-17 16:05:31 -05002397 pci_set_master(pdev);
Randy Dunlap694625c2007-07-09 11:55:54 -07002398 pci_try_set_mwi(pdev);
dea31012005-04-17 16:05:31 -05002399
2400 if (pci_set_dma_mask(phba->pcidev, DMA_64BIT_MASK) != 0)
2401 if (pci_set_dma_mask(phba->pcidev, DMA_32BIT_MASK) != 0)
2402 goto out_idr_remove;
2403
2404 /*
2405 * Get the bus address of Bar0 and Bar2 and the number of bytes
2406 * required by each mapping.
2407 */
2408 phba->pci_bar0_map = pci_resource_start(phba->pcidev, 0);
2409 bar0map_len = pci_resource_len(phba->pcidev, 0);
2410
2411 phba->pci_bar2_map = pci_resource_start(phba->pcidev, 2);
2412 bar2map_len = pci_resource_len(phba->pcidev, 2);
2413
Jamie Wellnitz901a9202006-02-28 19:25:19 -05002414 /* Map HBA SLIM to a kernel virtual address. */
James Smart93996272008-08-24 21:50:30 -04002415 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05002416 if (!phba->slim_memmap_p) {
2417 error = -ENODEV;
2418 dev_printk(KERN_ERR, &pdev->dev,
2419 "ioremap failed for SLIM memory.\n");
2420 goto out_idr_remove;
2421 }
2422
2423 /* Map HBA Control Registers to a kernel virtual address. */
dea31012005-04-17 16:05:31 -05002424 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05002425 if (!phba->ctrl_regs_memmap_p) {
2426 error = -ENODEV;
2427 dev_printk(KERN_ERR, &pdev->dev,
2428 "ioremap failed for HBA control registers.\n");
2429 goto out_iounmap_slim;
2430 }
dea31012005-04-17 16:05:31 -05002431
2432 /* Allocate memory for SLI-2 structures */
James Smart34b02dc2008-08-24 21:49:55 -04002433 phba->slim2p.virt = dma_alloc_coherent(&phba->pcidev->dev,
2434 SLI2_SLIM_SIZE,
2435 &phba->slim2p.phys,
2436 GFP_KERNEL);
2437 if (!phba->slim2p.virt)
dea31012005-04-17 16:05:31 -05002438 goto out_iounmap;
2439
James Smart34b02dc2008-08-24 21:49:55 -04002440 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
James Smartd7c255b2008-08-24 21:50:00 -04002441 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
2442 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
2443 phba->IOCBs = (phba->slim2p.virt +
2444 offsetof(struct lpfc_sli2_slim, IOCBs));
dea31012005-04-17 16:05:31 -05002445
James Smarted957682007-06-17 19:56:37 -05002446 phba->hbqslimp.virt = dma_alloc_coherent(&phba->pcidev->dev,
2447 lpfc_sli_hbq_size(),
2448 &phba->hbqslimp.phys,
2449 GFP_KERNEL);
2450 if (!phba->hbqslimp.virt)
2451 goto out_free_slim;
2452
James Smart51ef4c22007-08-02 11:10:31 -04002453 hbq_count = lpfc_sli_hbq_count();
2454 ptr = phba->hbqslimp.virt;
2455 for (i = 0; i < hbq_count; ++i) {
2456 phba->hbqs[i].hbq_virt = ptr;
2457 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
2458 ptr += (lpfc_hbq_defs[i]->entry_count *
2459 sizeof(struct lpfc_hbq_entry));
2460 }
2461 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
2462 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
2463
James Smarted957682007-06-17 19:56:37 -05002464 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
2465
James Smart3163f722008-02-08 18:50:25 -05002466 INIT_LIST_HEAD(&phba->hbqbuf_in_list);
2467
dea31012005-04-17 16:05:31 -05002468 /* Initialize the SLI Layer to run with lpfc HBAs. */
2469 lpfc_sli_setup(phba);
2470 lpfc_sli_queue_setup(phba);
2471
James Smart98c9ea52007-10-27 13:37:33 -04002472 retval = lpfc_mem_alloc(phba);
2473 if (retval) {
2474 error = retval;
James Smarted957682007-06-17 19:56:37 -05002475 goto out_free_hbqslimp;
James Smart98c9ea52007-10-27 13:37:33 -04002476 }
dea31012005-04-17 16:05:31 -05002477
2478 /* Initialize and populate the iocb list per host. */
2479 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
2480 for (i = 0; i < LPFC_IOCB_LIST_CNT; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002481 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002482 if (iocbq_entry == NULL) {
2483 printk(KERN_ERR "%s: only allocated %d iocbs of "
2484 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002485 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05002486 error = -ENOMEM;
2487 goto out_free_iocbq;
2488 }
2489
James Bottomley604a3e32005-10-29 10:28:33 -05002490 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
2491 if (iotag == 0) {
2492 kfree (iocbq_entry);
2493 printk(KERN_ERR "%s: failed to allocate IOTAG. "
2494 "Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002495 __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05002496 error = -ENOMEM;
2497 goto out_free_iocbq;
2498 }
James Smart2e0fef82007-06-17 19:56:36 -05002499
2500 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002501 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
2502 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05002503 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002504 }
2505
2506 /* Initialize HBA structure */
2507 phba->fc_edtov = FF_DEF_EDTOV;
2508 phba->fc_ratov = FF_DEF_RATOV;
2509 phba->fc_altov = FF_DEF_ALTOV;
2510 phba->fc_arbtov = FF_DEF_ARBTOV;
2511
2512 INIT_LIST_HEAD(&phba->work_list);
James Smart93996272008-08-24 21:50:30 -04002513 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
dea31012005-04-17 16:05:31 -05002514 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
2515
James Smart5e9d9b82008-06-14 22:52:53 -04002516 /* Initialize the wait queue head for the kernel thread */
2517 init_waitqueue_head(&phba->work_waitq);
2518
dea31012005-04-17 16:05:31 -05002519 /* Startup the kernel thread for this host adapter. */
2520 phba->worker_thread = kthread_run(lpfc_do_work, phba,
2521 "lpfc_worker_%d", phba->brd_no);
2522 if (IS_ERR(phba->worker_thread)) {
2523 error = PTR_ERR(phba->worker_thread);
2524 goto out_free_iocbq;
2525 }
2526
dea31012005-04-17 16:05:31 -05002527 /* Initialize the list of scsi buffers used by driver for scsi IO. */
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002528 spin_lock_init(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05002529 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
2530
James Smart92d7f7b2007-06-17 19:56:38 -05002531 /* Initialize list of fabric iocbs */
2532 INIT_LIST_HEAD(&phba->fabric_iocb_list);
2533
James Smart0ff10d42008-01-11 01:52:36 -05002534 /* Initialize list to save ELS buffers */
2535 INIT_LIST_HEAD(&phba->elsbuf);
2536
James Smart3de2a652007-08-02 11:09:59 -04002537 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05002538 if (!vport)
2539 goto out_kthread_stop;
2540
2541 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05002542 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05002543 lpfc_debugfs_initialize(vport);
James Smart2e0fef82007-06-17 19:56:36 -05002544
James Smart92d7f7b2007-06-17 19:56:38 -05002545 pci_set_drvdata(pdev, shost);
James Smartdb2378e2008-02-08 18:49:51 -05002546 phba->intr_type = NONE;
dea31012005-04-17 16:05:31 -05002547
James Smart93996272008-08-24 21:50:30 -04002548 phba->MBslimaddr = phba->slim_memmap_p;
2549 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
2550 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
2551 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
2552 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
2553
2554 /* Configure and enable interrupt */
James Smartdb2378e2008-02-08 18:49:51 -05002555 if (phba->cfg_use_msi == 2) {
James Smart93996272008-08-24 21:50:30 -04002556 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
2557 error = lpfc_sli_config_port(phba, 3);
2558 if (error)
2559 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2560 "0427 Firmware not capable of SLI 3 mode.\n");
2561 else {
2562 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2563 "0426 Firmware capable of SLI 3 mode.\n");
2564 /* Now, try to enable MSI-X interrupt mode */
2565 error = lpfc_enable_msix(phba);
2566 if (!error) {
2567 phba->intr_type = MSIX;
2568 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2569 "0430 enable MSI-X mode.\n");
2570 }
2571 }
James Smartdb2378e2008-02-08 18:49:51 -05002572 }
2573
2574 /* Fallback to MSI if MSI-X initialization failed */
2575 if (phba->cfg_use_msi >= 1 && phba->intr_type == NONE) {
James Smart98c9ea52007-10-27 13:37:33 -04002576 retval = pci_enable_msi(phba->pcidev);
James Smart93996272008-08-24 21:50:30 -04002577 if (!retval) {
James Smartdb2378e2008-02-08 18:49:51 -05002578 phba->intr_type = MSI;
James Smarte8b62012007-08-02 11:10:09 -04002579 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart93996272008-08-24 21:50:30 -04002580 "0473 enable MSI mode.\n");
2581 } else
2582 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2583 "0452 enable IRQ mode.\n");
James Smart4ff43242006-12-02 13:34:56 -05002584 }
2585
James Smartdb2378e2008-02-08 18:49:51 -05002586 /* MSI-X is the only case the doesn't need to call request_irq */
2587 if (phba->intr_type != MSIX) {
2588 retval = request_irq(phba->pcidev->irq, lpfc_intr_handler,
2589 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
2590 if (retval) {
2591 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0451 Enable "
2592 "interrupt handler failed\n");
2593 error = retval;
2594 goto out_disable_msi;
2595 } else if (phba->intr_type != MSI)
2596 phba->intr_type = INTx;
dea31012005-04-17 16:05:31 -05002597 }
dea31012005-04-17 16:05:31 -05002598
James Smart98c9ea52007-10-27 13:37:33 -04002599 if (lpfc_alloc_sysfs_attr(vport)) {
James Smart93996272008-08-24 21:50:30 -04002600 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2601 "1476 Failed to allocate sysfs attr\n");
James Smart98c9ea52007-10-27 13:37:33 -04002602 error = -ENOMEM;
dea31012005-04-17 16:05:31 -05002603 goto out_free_irq;
James Smart98c9ea52007-10-27 13:37:33 -04002604 }
dea31012005-04-17 16:05:31 -05002605
James Smart98c9ea52007-10-27 13:37:33 -04002606 if (lpfc_sli_hba_setup(phba)) {
James Smart93996272008-08-24 21:50:30 -04002607 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2608 "1477 Failed to set up hba\n");
James Smart98c9ea52007-10-27 13:37:33 -04002609 error = -ENODEV;
James Smart858c9f62007-06-17 19:56:39 -05002610 goto out_remove_device;
James Smart98c9ea52007-10-27 13:37:33 -04002611 }
James Smart858c9f62007-06-17 19:56:39 -05002612
2613 /*
2614 * hba setup may have changed the hba_queue_depth so we need to adjust
2615 * the value of can_queue.
2616 */
2617 shost->can_queue = phba->cfg_hba_queue_depth - 10;
2618
2619 lpfc_host_attrib_init(shost);
2620
James Smart2e0fef82007-06-17 19:56:36 -05002621 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
2622 spin_lock_irq(shost->host_lock);
2623 lpfc_poll_start_timer(phba);
2624 spin_unlock_irq(shost->host_lock);
2625 }
James Smart8f6d98d2006-08-01 07:34:00 -04002626
James Smart93996272008-08-24 21:50:30 -04002627 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2628 "0428 Perform SCSI scan\n");
James Smart858c9f62007-06-17 19:56:39 -05002629 scsi_scan_host(shost);
dea31012005-04-17 16:05:31 -05002630
dea31012005-04-17 16:05:31 -05002631 return 0;
2632
James Smart858c9f62007-06-17 19:56:39 -05002633out_remove_device:
2634 lpfc_free_sysfs_attr(vport);
2635 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04002636 vport->load_flag |= FC_UNLOADING;
James Smart858c9f62007-06-17 19:56:39 -05002637 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05002638out_free_irq:
James Smart92d7f7b2007-06-17 19:56:38 -05002639 lpfc_stop_phba_timers(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002640 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05002641
2642 if (phba->intr_type == MSIX)
2643 lpfc_disable_msix(phba);
2644 else
2645 free_irq(phba->pcidev->irq, phba);
2646
James Smart92d7f7b2007-06-17 19:56:38 -05002647out_disable_msi:
James Smartdb2378e2008-02-08 18:49:51 -05002648 if (phba->intr_type == MSI)
James Smart51ef4c22007-08-02 11:10:31 -04002649 pci_disable_msi(phba->pcidev);
James Smart2e0fef82007-06-17 19:56:36 -05002650 destroy_port(vport);
dea31012005-04-17 16:05:31 -05002651out_kthread_stop:
2652 kthread_stop(phba->worker_thread);
2653out_free_iocbq:
2654 list_for_each_entry_safe(iocbq_entry, iocbq_next,
2655 &phba->lpfc_iocb_list, list) {
dea31012005-04-17 16:05:31 -05002656 kfree(iocbq_entry);
2657 phba->total_iocbq_bufs--;
dea31012005-04-17 16:05:31 -05002658 }
2659 lpfc_mem_free(phba);
James Smarted957682007-06-17 19:56:37 -05002660out_free_hbqslimp:
James Smart34b02dc2008-08-24 21:49:55 -04002661 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
2662 phba->hbqslimp.virt, phba->hbqslimp.phys);
dea31012005-04-17 16:05:31 -05002663out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04002664 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
2665 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05002666out_iounmap:
2667 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05002668out_iounmap_slim:
dea31012005-04-17 16:05:31 -05002669 iounmap(phba->slim_memmap_p);
2670out_idr_remove:
2671 idr_remove(&lpfc_hba_index, phba->brd_no);
James Smart2e0fef82007-06-17 19:56:36 -05002672out_free_phba:
2673 kfree(phba);
dea31012005-04-17 16:05:31 -05002674out_release_regions:
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05002675 pci_release_selected_regions(pdev, bars);
dea31012005-04-17 16:05:31 -05002676out_disable_device:
2677 pci_disable_device(pdev);
2678out:
James Smartdefbcf12006-04-16 22:26:50 -04002679 pci_set_drvdata(pdev, NULL);
James Smart858c9f62007-06-17 19:56:39 -05002680 if (shost)
2681 scsi_host_put(shost);
dea31012005-04-17 16:05:31 -05002682 return error;
2683}
2684
James Smarte59058c2008-08-24 21:49:00 -04002685/**
2686 * lpfc_pci_remove_one: lpfc PCI func to unregister device from PCI subsystem.
2687 * @pdev: pointer to PCI device
2688 *
2689 * This routine is to be registered to the kernel's PCI subsystem. When an
2690 * Emulex HBA is removed from PCI bus. It perform all the necessary cleanup
2691 * for the HBA device to be removed from the PCI subsystem properly.
2692 **/
dea31012005-04-17 16:05:31 -05002693static void __devexit
2694lpfc_pci_remove_one(struct pci_dev *pdev)
2695{
James Smart2e0fef82007-06-17 19:56:36 -05002696 struct Scsi_Host *shost = pci_get_drvdata(pdev);
2697 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2698 struct lpfc_hba *phba = vport->phba;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05002699 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
2700
James Smart549e55c2007-08-02 11:09:51 -04002701 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04002702 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04002703 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002704
James Smart858c9f62007-06-17 19:56:39 -05002705 kfree(vport->vname);
2706 lpfc_free_sysfs_attr(vport);
2707
James Smart09372822008-01-11 01:52:54 -05002708 kthread_stop(phba->worker_thread);
2709
James Smart858c9f62007-06-17 19:56:39 -05002710 fc_remove_host(shost);
2711 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04002712 lpfc_cleanup(vport);
2713
James Smart2e0fef82007-06-17 19:56:36 -05002714 /*
2715 * Bring down the SLI Layer. This step disable all interrupts,
2716 * clears the rings, discards all mailbox commands, and resets
2717 * the HBA.
2718 */
2719 lpfc_sli_hba_down(phba);
2720 lpfc_sli_brdrestart(phba);
2721
James Smart92d7f7b2007-06-17 19:56:38 -05002722 lpfc_stop_phba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05002723 spin_lock_irq(&phba->hbalock);
2724 list_del_init(&vport->listentry);
2725 spin_unlock_irq(&phba->hbalock);
2726
James Smart858c9f62007-06-17 19:56:39 -05002727 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05002728
James Smartdb2378e2008-02-08 18:49:51 -05002729 if (phba->intr_type == MSIX)
2730 lpfc_disable_msix(phba);
2731 else {
2732 free_irq(phba->pcidev->irq, phba);
2733 if (phba->intr_type == MSI)
2734 pci_disable_msi(phba->pcidev);
2735 }
dea31012005-04-17 16:05:31 -05002736
2737 pci_set_drvdata(pdev, NULL);
James Smart858c9f62007-06-17 19:56:39 -05002738 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05002739
2740 /*
2741 * Call scsi_free before mem_free since scsi bufs are released to their
2742 * corresponding pools here.
2743 */
2744 lpfc_scsi_free(phba);
2745 lpfc_mem_free(phba);
2746
James Smart34b02dc2008-08-24 21:49:55 -04002747 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
2748 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -05002749
James Smart2e0fef82007-06-17 19:56:36 -05002750 /* Free resources associated with SLI2 interface */
2751 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -04002752 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -05002753
2754 /* unmap adapter SLIM and Control Registers */
2755 iounmap(phba->ctrl_regs_memmap_p);
2756 iounmap(phba->slim_memmap_p);
2757
2758 idr_remove(&lpfc_hba_index, phba->brd_no);
2759
2760 kfree(phba);
2761
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05002762 pci_release_selected_regions(pdev, bars);
James Smart2e0fef82007-06-17 19:56:36 -05002763 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05002764}
2765
Linas Vepstas8d63f372007-02-14 14:28:36 -06002766/**
James Smarte59058c2008-08-24 21:49:00 -04002767 * lpfc_io_error_detected: Driver method for handling PCI I/O error detected.
2768 * @pdev: pointer to PCI device.
2769 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -06002770 *
James Smarte59058c2008-08-24 21:49:00 -04002771 * This routine is registered to the PCI subsystem for error handling. This
2772 * function is called by the PCI subsystem after a PCI bus error affecting
2773 * this device has been detected. When this function is invoked, it will
2774 * need to stop all the I/Os and interrupt(s) to the device. Once that is
2775 * done, it will return PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to
2776 * perform proper recovery as desired.
2777 *
2778 * Return codes
2779 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
2780 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
2781 **/
Linas Vepstas8d63f372007-02-14 14:28:36 -06002782static pci_ers_result_t lpfc_io_error_detected(struct pci_dev *pdev,
2783 pci_channel_state_t state)
2784{
James Smart51ef4c22007-08-02 11:10:31 -04002785 struct Scsi_Host *shost = pci_get_drvdata(pdev);
2786 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06002787 struct lpfc_sli *psli = &phba->sli;
2788 struct lpfc_sli_ring *pring;
2789
James Smarta8e497d2008-08-24 21:50:11 -04002790 if (state == pci_channel_io_perm_failure) {
2791 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2792 "0472 PCI channel I/O permanent failure\n");
2793 /* Block all SCSI devices' I/Os on the host */
2794 lpfc_scsi_dev_block(phba);
2795 /* Clean up all driver's outstanding SCSI I/Os */
2796 lpfc_sli_flush_fcp_rings(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06002797 return PCI_ERS_RESULT_DISCONNECT;
James Smarta8e497d2008-08-24 21:50:11 -04002798 }
Linas Vepstas5daa49e2007-03-08 16:19:11 -06002799
Linas Vepstas8d63f372007-02-14 14:28:36 -06002800 pci_disable_device(pdev);
2801 /*
2802 * There may be I/Os dropped by the firmware.
2803 * Error iocb (I/O) on txcmplq and let the SCSI layer
2804 * retry it after re-establishing link.
2805 */
2806 pring = &psli->ring[psli->fcp_ring];
2807 lpfc_sli_abort_iocb_ring(phba, pring);
2808
James Smartdb2378e2008-02-08 18:49:51 -05002809 if (phba->intr_type == MSIX)
2810 lpfc_disable_msix(phba);
2811 else {
2812 free_irq(phba->pcidev->irq, phba);
2813 if (phba->intr_type == MSI)
2814 pci_disable_msi(phba->pcidev);
2815 }
James Smart51ef4c22007-08-02 11:10:31 -04002816
Linas Vepstas8d63f372007-02-14 14:28:36 -06002817 /* Request a slot reset. */
2818 return PCI_ERS_RESULT_NEED_RESET;
2819}
2820
2821/**
James Smarte59058c2008-08-24 21:49:00 -04002822 * lpfc_io_slot_reset: Restart a PCI device from scratch.
2823 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -06002824 *
James Smarte59058c2008-08-24 21:49:00 -04002825 * This routine is registered to the PCI subsystem for error handling. This is
2826 * called after PCI bus has been reset to restart the PCI card from scratch,
2827 * as if from a cold-boot. During the PCI subsystem error recovery, after the
2828 * driver returns PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform
2829 * proper error recovery and then call this routine before calling the .resume
2830 * method to recover the device. This function will initialize the HBA device,
2831 * enable the interrupt, but it will just put the HBA to offline state without
2832 * passing any I/O traffic.
2833 *
2834 * Return codes
2835 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
2836 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -06002837 */
2838static pci_ers_result_t lpfc_io_slot_reset(struct pci_dev *pdev)
2839{
James Smart51ef4c22007-08-02 11:10:31 -04002840 struct Scsi_Host *shost = pci_get_drvdata(pdev);
2841 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06002842 struct lpfc_sli *psli = &phba->sli;
James Smart9b379602008-04-07 10:16:00 -04002843 int error, retval;
Linas Vepstas8d63f372007-02-14 14:28:36 -06002844
2845 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +11002846 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -06002847 printk(KERN_ERR "lpfc: Cannot re-enable "
2848 "PCI device after reset.\n");
2849 return PCI_ERS_RESULT_DISCONNECT;
2850 }
2851
2852 pci_set_master(pdev);
2853
James Smart92d7f7b2007-06-17 19:56:38 -05002854 spin_lock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06002855 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05002856 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06002857
James Smart9b379602008-04-07 10:16:00 -04002858 /* Enable configured interrupt method */
2859 phba->intr_type = NONE;
2860 if (phba->cfg_use_msi == 2) {
James Smart93996272008-08-24 21:50:30 -04002861 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
2862 error = lpfc_sli_config_port(phba, 3);
2863 if (error)
2864 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2865 "0478 Firmware not capable of SLI 3 mode.\n");
2866 else {
2867 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2868 "0479 Firmware capable of SLI 3 mode.\n");
2869 /* Now, try to enable MSI-X interrupt mode */
2870 error = lpfc_enable_msix(phba);
2871 if (!error) {
2872 phba->intr_type = MSIX;
2873 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2874 "0480 enable MSI-X mode.\n");
2875 }
2876 }
James Smart9b379602008-04-07 10:16:00 -04002877 }
2878
2879 /* Fallback to MSI if MSI-X initialization failed */
2880 if (phba->cfg_use_msi >= 1 && phba->intr_type == NONE) {
2881 retval = pci_enable_msi(phba->pcidev);
James Smart93996272008-08-24 21:50:30 -04002882 if (!retval) {
James Smart9b379602008-04-07 10:16:00 -04002883 phba->intr_type = MSI;
James Smart9b379602008-04-07 10:16:00 -04002884 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart93996272008-08-24 21:50:30 -04002885 "0481 enable MSI mode.\n");
2886 } else
2887 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2888 "0470 enable IRQ mode.\n");
James Smart9b379602008-04-07 10:16:00 -04002889 }
2890
2891 /* MSI-X is the only case the doesn't need to call request_irq */
2892 if (phba->intr_type != MSIX) {
2893 retval = request_irq(phba->pcidev->irq, lpfc_intr_handler,
2894 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
2895 if (retval) {
2896 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2897 "0471 Enable interrupt handler "
2898 "failed\n");
2899 } else if (phba->intr_type != MSI)
2900 phba->intr_type = INTx;
2901 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06002902
2903 /* Take device offline; this will perform cleanup */
2904 lpfc_offline(phba);
2905 lpfc_sli_brdrestart(phba);
2906
2907 return PCI_ERS_RESULT_RECOVERED;
2908}
2909
2910/**
James Smarte59058c2008-08-24 21:49:00 -04002911 * lpfc_io_resume: Resume PCI I/O operation.
2912 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -06002913 *
James Smarte59058c2008-08-24 21:49:00 -04002914 * This routine is registered to the PCI subsystem for error handling. It is
2915 * called when kernel error recovery tells the lpfc driver that it is ok to
2916 * resume normal PCI operation after PCI bus error recovery. After this call,
2917 * traffic can start to flow from this device again.
Linas Vepstas8d63f372007-02-14 14:28:36 -06002918 */
2919static void lpfc_io_resume(struct pci_dev *pdev)
2920{
James Smart51ef4c22007-08-02 11:10:31 -04002921 struct Scsi_Host *shost = pci_get_drvdata(pdev);
2922 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06002923
James Smart58da1ff2008-04-07 10:15:56 -04002924 lpfc_online(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06002925}
2926
dea31012005-04-17 16:05:31 -05002927static struct pci_device_id lpfc_id_table[] = {
2928 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
2929 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002930 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
2931 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05002932 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
2933 PCI_ANY_ID, PCI_ANY_ID, },
2934 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
2935 PCI_ANY_ID, PCI_ANY_ID, },
2936 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
2937 PCI_ANY_ID, PCI_ANY_ID, },
2938 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
2939 PCI_ANY_ID, PCI_ANY_ID, },
2940 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
2941 PCI_ANY_ID, PCI_ANY_ID, },
2942 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
2943 PCI_ANY_ID, PCI_ANY_ID, },
2944 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
2945 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002946 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
2947 PCI_ANY_ID, PCI_ANY_ID, },
2948 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
2949 PCI_ANY_ID, PCI_ANY_ID, },
2950 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
2951 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05002952 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
2953 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002954 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
2955 PCI_ANY_ID, PCI_ANY_ID, },
2956 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
2957 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05002958 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
2959 PCI_ANY_ID, PCI_ANY_ID, },
2960 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
2961 PCI_ANY_ID, PCI_ANY_ID, },
2962 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
2963 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -04002964 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
2965 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002966 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
2967 PCI_ANY_ID, PCI_ANY_ID, },
2968 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
2969 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05002970 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
2971 PCI_ANY_ID, PCI_ANY_ID, },
2972 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
2973 PCI_ANY_ID, PCI_ANY_ID, },
2974 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
2975 PCI_ANY_ID, PCI_ANY_ID, },
2976 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
2977 PCI_ANY_ID, PCI_ANY_ID, },
2978 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
2979 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002980 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
2981 PCI_ANY_ID, PCI_ANY_ID, },
2982 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
2983 PCI_ANY_ID, PCI_ANY_ID, },
James Smartb87eab32007-04-25 09:53:28 -04002984 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
2985 PCI_ANY_ID, PCI_ANY_ID, },
2986 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
2987 PCI_ANY_ID, PCI_ANY_ID, },
2988 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
2989 PCI_ANY_ID, PCI_ANY_ID, },
2990 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
2991 PCI_ANY_ID, PCI_ANY_ID, },
2992 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
2993 PCI_ANY_ID, PCI_ANY_ID, },
2994 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
2995 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -04002996 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
2997 PCI_ANY_ID, PCI_ANY_ID, },
2998 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
2999 PCI_ANY_ID, PCI_ANY_ID, },
3000 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
3001 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05003002 { 0 }
3003};
3004
3005MODULE_DEVICE_TABLE(pci, lpfc_id_table);
3006
Linas Vepstas8d63f372007-02-14 14:28:36 -06003007static struct pci_error_handlers lpfc_err_handler = {
3008 .error_detected = lpfc_io_error_detected,
3009 .slot_reset = lpfc_io_slot_reset,
3010 .resume = lpfc_io_resume,
3011};
3012
dea31012005-04-17 16:05:31 -05003013static struct pci_driver lpfc_driver = {
3014 .name = LPFC_DRIVER_NAME,
3015 .id_table = lpfc_id_table,
3016 .probe = lpfc_pci_probe_one,
3017 .remove = __devexit_p(lpfc_pci_remove_one),
James Smart2e0fef82007-06-17 19:56:36 -05003018 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -05003019};
3020
James Smarte59058c2008-08-24 21:49:00 -04003021/**
3022 * lpfc_init: lpfc module initialization routine.
3023 *
3024 * This routine is to be invoked when the lpfc module is loaded into the
3025 * kernel. The special kernel macro module_init() is used to indicate the
3026 * role of this routine to the kernel as lpfc module entry point.
3027 *
3028 * Return codes
3029 * 0 - successful
3030 * -ENOMEM - FC attach transport failed
3031 * all others - failed
3032 */
dea31012005-04-17 16:05:31 -05003033static int __init
3034lpfc_init(void)
3035{
3036 int error = 0;
3037
3038 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003039 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -05003040
James Smart7ee5d432007-10-27 13:37:17 -04003041 if (lpfc_enable_npiv) {
3042 lpfc_transport_functions.vport_create = lpfc_vport_create;
3043 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
3044 }
dea31012005-04-17 16:05:31 -05003045 lpfc_transport_template =
3046 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -04003047 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -05003048 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -04003049 if (lpfc_enable_npiv) {
James Smart7ee5d432007-10-27 13:37:17 -04003050 lpfc_vport_transport_template =
James Smart98c9ea52007-10-27 13:37:33 -04003051 fc_attach_transport(&lpfc_vport_transport_functions);
3052 if (lpfc_vport_transport_template == NULL) {
3053 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -04003054 return -ENOMEM;
James Smart98c9ea52007-10-27 13:37:33 -04003055 }
James Smart7ee5d432007-10-27 13:37:17 -04003056 }
dea31012005-04-17 16:05:31 -05003057 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -05003058 if (error) {
dea31012005-04-17 16:05:31 -05003059 fc_release_transport(lpfc_transport_template);
James Smartd7c255b2008-08-24 21:50:00 -04003060 if (lpfc_enable_npiv)
3061 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -05003062 }
dea31012005-04-17 16:05:31 -05003063
3064 return error;
3065}
3066
James Smarte59058c2008-08-24 21:49:00 -04003067/**
3068 * lpfc_exit: lpfc module removal routine.
3069 *
3070 * This routine is invoked when the lpfc module is removed from the kernel.
3071 * The special kernel macro module_exit() is used to indicate the role of
3072 * this routine to the kernel as lpfc module exit point.
3073 */
dea31012005-04-17 16:05:31 -05003074static void __exit
3075lpfc_exit(void)
3076{
3077 pci_unregister_driver(&lpfc_driver);
3078 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -04003079 if (lpfc_enable_npiv)
3080 fc_release_transport(lpfc_vport_transport_template);
dea31012005-04-17 16:05:31 -05003081}
3082
3083module_init(lpfc_init);
3084module_exit(lpfc_exit);
3085MODULE_LICENSE("GPL");
3086MODULE_DESCRIPTION(LPFC_MODULE_DESC);
3087MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
3088MODULE_VERSION("0:" LPFC_DRIVER_VERSION);