blob: e3645e8f348732ae9101ce63c01e12716d20b026 [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"
James Smartea2151b2008-09-07 11:52:10 -040039#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050040#include "lpfc_disc.h"
41#include "lpfc_scsi.h"
42#include "lpfc.h"
43#include "lpfc_logmsg.h"
44#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050045#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050046#include "lpfc_version.h"
47
James Smart81301a92008-12-04 22:39:46 -050048char *_dump_buf_data;
49unsigned long _dump_buf_data_order;
50char *_dump_buf_dif;
51unsigned long _dump_buf_dif_order;
52spinlock_t _dump_buf_lock;
53
Jamie Wellnitz74b72a52006-02-28 22:33:04 -050054static int lpfc_parse_vpd(struct lpfc_hba *, uint8_t *, int);
dea31012005-04-17 16:05:31 -050055static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
56static int lpfc_post_rcv_buf(struct lpfc_hba *);
57
58static struct scsi_transport_template *lpfc_transport_template = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050059static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea31012005-04-17 16:05:31 -050060static DEFINE_IDR(lpfc_hba_index);
61
James Smarte59058c2008-08-24 21:49:00 -040062/**
James Smart3621a712009-04-06 18:47:14 -040063 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
James Smarte59058c2008-08-24 21:49:00 -040064 * @phba: pointer to lpfc hba data structure.
65 *
66 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
67 * mailbox command. It retrieves the revision information from the HBA and
68 * collects the Vital Product Data (VPD) about the HBA for preparing the
69 * configuration of the HBA.
70 *
71 * Return codes:
72 * 0 - success.
73 * -ERESTART - requests the SLI layer to reset the HBA and try again.
74 * Any other value - indicates an error.
75 **/
dea31012005-04-17 16:05:31 -050076int
James Smart2e0fef82007-06-17 19:56:36 -050077lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050078{
79 lpfc_vpd_t *vp = &phba->vpd;
80 int i = 0, rc;
81 LPFC_MBOXQ_t *pmb;
82 MAILBOX_t *mb;
83 char *lpfc_vpd_data = NULL;
84 uint16_t offset = 0;
85 static char licensed[56] =
86 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -040087 static int init_key = 1;
dea31012005-04-17 16:05:31 -050088
89 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
90 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -050091 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -050092 return -ENOMEM;
93 }
94
95 mb = &pmb->mb;
James Smart2e0fef82007-06-17 19:56:36 -050096 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -050097
98 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -040099 if (init_key) {
100 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -0500101
James Smart65a29c12006-07-06 15:50:50 -0400102 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
103 *ptext = cpu_to_be32(*ptext);
104 init_key = 0;
105 }
dea31012005-04-17 16:05:31 -0500106
107 lpfc_read_nv(phba, pmb);
108 memset((char*)mb->un.varRDnvp.rsvd3, 0,
109 sizeof (mb->un.varRDnvp.rsvd3));
110 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
111 sizeof (licensed));
112
113 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
114
115 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500116 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smarte8b62012007-08-02 11:10:09 -0400117 "0324 Config Port initialization "
dea31012005-04-17 16:05:31 -0500118 "error, mbxCmd x%x READ_NVPARM, "
119 "mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500120 mb->mbxCommand, mb->mbxStatus);
121 mempool_free(pmb, phba->mbox_mem_pool);
122 return -ERESTART;
123 }
124 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
James Smart2e0fef82007-06-17 19:56:36 -0500125 sizeof(phba->wwnn));
126 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
127 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500128 }
129
James Smart92d7f7b2007-06-17 19:56:38 -0500130 phba->sli3_options = 0x0;
131
dea31012005-04-17 16:05:31 -0500132 /* Setup and issue mailbox READ REV command */
133 lpfc_read_rev(phba, pmb);
134 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
135 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500136 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400137 "0439 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500138 "READ_REV, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500139 mb->mbxCommand, mb->mbxStatus);
140 mempool_free( pmb, phba->mbox_mem_pool);
141 return -ERESTART;
142 }
143
James Smart92d7f7b2007-06-17 19:56:38 -0500144
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500145 /*
146 * The value of rr must be 1 since the driver set the cv field to 1.
147 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500148 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500149 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500150 vp->rev.rBit = 0;
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500151 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400152 "0440 Adapter failed to init, READ_REV has "
153 "missing revision information.\n");
dea31012005-04-17 16:05:31 -0500154 mempool_free(pmb, phba->mbox_mem_pool);
155 return -ERESTART;
dea31012005-04-17 16:05:31 -0500156 }
157
James Smart495a7142008-06-14 22:52:59 -0400158 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
159 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -0500160 return -EINVAL;
James Smart495a7142008-06-14 22:52:59 -0400161 }
James Smarted957682007-06-17 19:56:37 -0500162
dea31012005-04-17 16:05:31 -0500163 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500164 vp->rev.rBit = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500165 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500166 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
167 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
168 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
169 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500170 vp->rev.biuRev = mb->un.varRdRev.biuRev;
171 vp->rev.smRev = mb->un.varRdRev.smRev;
172 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
173 vp->rev.endecRev = mb->un.varRdRev.endecRev;
174 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
175 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
176 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
177 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
178 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
179 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
180
James Smart92d7f7b2007-06-17 19:56:38 -0500181 /* If the sli feature level is less then 9, we must
182 * tear down all RPIs and VPIs on link down if NPIV
183 * is enabled.
184 */
185 if (vp->rev.feaLevelHigh < 9)
186 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
187
dea31012005-04-17 16:05:31 -0500188 if (lpfc_is_LC_HBA(phba->pcidev->device))
189 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
190 sizeof (phba->RandomData));
191
dea31012005-04-17 16:05:31 -0500192 /* Get adapter VPD information */
dea31012005-04-17 16:05:31 -0500193 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
194 if (!lpfc_vpd_data)
James Smartd7c255b2008-08-24 21:50:00 -0400195 goto out_free_mbox;
dea31012005-04-17 16:05:31 -0500196
197 do {
198 lpfc_dump_mem(phba, pmb, offset);
199 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
200
201 if (rc != MBX_SUCCESS) {
202 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400203 "0441 VPD not present on adapter, "
dea31012005-04-17 16:05:31 -0500204 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500205 mb->mbxCommand, mb->mbxStatus);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500206 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500207 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500208 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
209 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
James Smartd7c255b2008-08-24 21:50:00 -0400210 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
211 lpfc_vpd_data + offset,
James Smart92d7f7b2007-06-17 19:56:38 -0500212 mb->un.varDmp.word_cnt);
dea31012005-04-17 16:05:31 -0500213 offset += mb->un.varDmp.word_cnt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500214 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
215 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500216
217 kfree(lpfc_vpd_data);
dea31012005-04-17 16:05:31 -0500218out_free_mbox:
219 mempool_free(pmb, phba->mbox_mem_pool);
220 return 0;
221}
222
James Smarte59058c2008-08-24 21:49:00 -0400223/**
James Smart3621a712009-04-06 18:47:14 -0400224 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
James Smarte59058c2008-08-24 21:49:00 -0400225 * @phba: pointer to lpfc hba data structure.
226 * @pmboxq: pointer to the driver internal queue element for mailbox command.
227 *
228 * This is the completion handler for driver's configuring asynchronous event
229 * mailbox command to the device. If the mailbox command returns successfully,
230 * it will set internal async event support flag to 1; otherwise, it will
231 * set internal async event support flag to 0.
232 **/
James Smart57127f12007-10-27 13:37:05 -0400233static void
234lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
235{
236 if (pmboxq->mb.mbxStatus == MBX_SUCCESS)
237 phba->temp_sensor_support = 1;
238 else
239 phba->temp_sensor_support = 0;
240 mempool_free(pmboxq, phba->mbox_mem_pool);
241 return;
242}
243
James Smarte59058c2008-08-24 21:49:00 -0400244/**
James Smart3621a712009-04-06 18:47:14 -0400245 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
James Smart97207482008-12-04 22:39:19 -0500246 * @phba: pointer to lpfc hba data structure.
247 * @pmboxq: pointer to the driver internal queue element for mailbox command.
248 *
249 * This is the completion handler for dump mailbox command for getting
250 * wake up parameters. When this command complete, the response contain
251 * Option rom version of the HBA. This function translate the version number
252 * into a human readable string and store it in OptionROMVersion.
253 **/
254static void
255lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
256{
257 struct prog_id *prg;
258 uint32_t prog_id_word;
259 char dist = ' ';
260 /* character array used for decoding dist type. */
261 char dist_char[] = "nabx";
262
James Smart9f1e1b52008-12-04 22:39:40 -0500263 if (pmboxq->mb.mbxStatus != MBX_SUCCESS) {
264 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500265 return;
James Smart9f1e1b52008-12-04 22:39:40 -0500266 }
James Smart97207482008-12-04 22:39:19 -0500267
268 prg = (struct prog_id *) &prog_id_word;
269
270 /* word 7 contain option rom version */
271 prog_id_word = pmboxq->mb.un.varWords[7];
272
273 /* Decode the Option rom version word to a readable string */
274 if (prg->dist < 4)
275 dist = dist_char[prg->dist];
276
277 if ((prg->dist == 3) && (prg->num == 0))
278 sprintf(phba->OptionROMVersion, "%d.%d%d",
279 prg->ver, prg->rev, prg->lev);
280 else
281 sprintf(phba->OptionROMVersion, "%d.%d%d%c%d",
282 prg->ver, prg->rev, prg->lev,
283 dist, prg->num);
James Smart9f1e1b52008-12-04 22:39:40 -0500284 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500285 return;
286}
287
288/**
James Smart3621a712009-04-06 18:47:14 -0400289 * lpfc_config_port_post - Perform lpfc initialization after config port
James Smarte59058c2008-08-24 21:49:00 -0400290 * @phba: pointer to lpfc hba data structure.
291 *
292 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
293 * command call. It performs all internal resource and state setups on the
294 * port: post IOCB buffers, enable appropriate host interrupt attentions,
295 * ELS ring timers, etc.
296 *
297 * Return codes
298 * 0 - success.
299 * Any other value - error.
300 **/
dea31012005-04-17 16:05:31 -0500301int
James Smart2e0fef82007-06-17 19:56:36 -0500302lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500303{
James Smart2e0fef82007-06-17 19:56:36 -0500304 struct lpfc_vport *vport = phba->pport;
dea31012005-04-17 16:05:31 -0500305 LPFC_MBOXQ_t *pmb;
306 MAILBOX_t *mb;
307 struct lpfc_dmabuf *mp;
308 struct lpfc_sli *psli = &phba->sli;
309 uint32_t status, timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500310 int i, j;
311 int rc;
dea31012005-04-17 16:05:31 -0500312
James Smart7af67052007-10-27 13:38:11 -0400313 spin_lock_irq(&phba->hbalock);
314 /*
315 * If the Config port completed correctly the HBA is not
316 * over heated any more.
317 */
318 if (phba->over_temp_state == HBA_OVER_TEMP)
319 phba->over_temp_state = HBA_NORMAL_TEMP;
320 spin_unlock_irq(&phba->hbalock);
321
dea31012005-04-17 16:05:31 -0500322 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
323 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500324 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500325 return -ENOMEM;
326 }
327 mb = &pmb->mb;
328
dea31012005-04-17 16:05:31 -0500329 /* Get login parameters for NID. */
James Smart92d7f7b2007-06-17 19:56:38 -0500330 lpfc_read_sparam(phba, pmb, 0);
James Smarted957682007-06-17 19:56:37 -0500331 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500332 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500333 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400334 "0448 Adapter failed init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500335 "READ_SPARM mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500336 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500337 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500338 mp = (struct lpfc_dmabuf *) pmb->context1;
339 mempool_free( pmb, phba->mbox_mem_pool);
340 lpfc_mbuf_free(phba, mp->virt, mp->phys);
341 kfree(mp);
342 return -EIO;
343 }
344
345 mp = (struct lpfc_dmabuf *) pmb->context1;
346
James Smart2e0fef82007-06-17 19:56:36 -0500347 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500348 lpfc_mbuf_free(phba, mp->virt, mp->phys);
349 kfree(mp);
350 pmb->context1 = NULL;
351
James Smarta12e07b2006-12-02 13:35:30 -0500352 if (phba->cfg_soft_wwnn)
James Smart2e0fef82007-06-17 19:56:36 -0500353 u64_to_wwn(phba->cfg_soft_wwnn,
354 vport->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -0400355 if (phba->cfg_soft_wwpn)
James Smart2e0fef82007-06-17 19:56:36 -0500356 u64_to_wwn(phba->cfg_soft_wwpn,
357 vport->fc_sparam.portName.u.wwn);
358 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
dea31012005-04-17 16:05:31 -0500359 sizeof (struct lpfc_name));
James Smart2e0fef82007-06-17 19:56:36 -0500360 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
dea31012005-04-17 16:05:31 -0500361 sizeof (struct lpfc_name));
362 /* If no serial number in VPD data, use low 6 bytes of WWNN */
363 /* This should be consolidated into parse_vpd ? - mr */
364 if (phba->SerialNumber[0] == 0) {
365 uint8_t *outptr;
366
James Smart2e0fef82007-06-17 19:56:36 -0500367 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500368 for (i = 0; i < 12; i++) {
369 status = *outptr++;
370 j = ((status & 0xf0) >> 4);
371 if (j <= 9)
372 phba->SerialNumber[i] =
373 (char)((uint8_t) 0x30 + (uint8_t) j);
374 else
375 phba->SerialNumber[i] =
376 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
377 i++;
378 j = (status & 0xf);
379 if (j <= 9)
380 phba->SerialNumber[i] =
381 (char)((uint8_t) 0x30 + (uint8_t) j);
382 else
383 phba->SerialNumber[i] =
384 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
385 }
386 }
387
dea31012005-04-17 16:05:31 -0500388 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500389 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500390 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500391 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400392 "0453 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500393 "READ_CONFIG, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500394 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500395 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500396 mempool_free( pmb, phba->mbox_mem_pool);
397 return -EIO;
398 }
399
400 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
401 if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
402 phba->cfg_hba_queue_depth =
403 mb->un.varRdConfig.max_xri + 1;
404
405 phba->lmt = mb->un.varRdConfig.lmt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500406
407 /* Get the default values for Model Name and Description */
408 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
409
410 if ((phba->cfg_link_speed > LINK_SPEED_10G)
411 || ((phba->cfg_link_speed == LINK_SPEED_1G)
412 && !(phba->lmt & LMT_1Gb))
413 || ((phba->cfg_link_speed == LINK_SPEED_2G)
414 && !(phba->lmt & LMT_2Gb))
415 || ((phba->cfg_link_speed == LINK_SPEED_4G)
416 && !(phba->lmt & LMT_4Gb))
417 || ((phba->cfg_link_speed == LINK_SPEED_8G)
418 && !(phba->lmt & LMT_8Gb))
419 || ((phba->cfg_link_speed == LINK_SPEED_10G)
420 && !(phba->lmt & LMT_10Gb))) {
421 /* Reset link speed to auto */
James Smarted957682007-06-17 19:56:37 -0500422 lpfc_printf_log(phba, KERN_WARNING, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -0400423 "1302 Invalid speed for this board: "
dea31012005-04-17 16:05:31 -0500424 "Reset link speed to auto: x%x\n",
dea31012005-04-17 16:05:31 -0500425 phba->cfg_link_speed);
426 phba->cfg_link_speed = LINK_SPEED_AUTO;
427 }
428
James Smart2e0fef82007-06-17 19:56:36 -0500429 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500430
James Smart0b727fe2007-10-27 13:37:25 -0400431 /* Only process IOCBs on ELS ring till hba_state is READY */
James Smarta4bc3372006-12-02 13:34:16 -0500432 if (psli->ring[psli->extra_ring].cmdringaddr)
433 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500434 if (psli->ring[psli->fcp_ring].cmdringaddr)
435 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
436 if (psli->ring[psli->next_ring].cmdringaddr)
437 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
438
439 /* Post receive buffers for desired rings */
James Smarted957682007-06-17 19:56:37 -0500440 if (phba->sli_rev != 3)
441 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500442
James Smart93996272008-08-24 21:50:30 -0400443 /*
444 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
445 */
446 if (phba->intr_type == MSIX) {
447 rc = lpfc_config_msi(phba, pmb);
448 if (rc) {
449 mempool_free(pmb, phba->mbox_mem_pool);
450 return -EIO;
451 }
452 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
453 if (rc != MBX_SUCCESS) {
454 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
455 "0352 Config MSI mailbox command "
456 "failed, mbxCmd x%x, mbxStatus x%x\n",
457 pmb->mb.mbxCommand, pmb->mb.mbxStatus);
458 mempool_free(pmb, phba->mbox_mem_pool);
459 return -EIO;
460 }
461 }
462
463 /* Initialize ERATT handling flag */
464 phba->hba_flag &= ~HBA_ERATT_HANDLED;
465
dea31012005-04-17 16:05:31 -0500466 /* Enable appropriate host interrupts */
James Smart2e0fef82007-06-17 19:56:36 -0500467 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500468 status = readl(phba->HCregaddr);
469 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
470 if (psli->num_rings > 0)
471 status |= HC_R0INT_ENA;
472 if (psli->num_rings > 1)
473 status |= HC_R1INT_ENA;
474 if (psli->num_rings > 2)
475 status |= HC_R2INT_ENA;
476 if (psli->num_rings > 3)
477 status |= HC_R3INT_ENA;
478
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500479 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
480 (phba->cfg_poll & DISABLE_FCP_RING_INT))
James Smart93996272008-08-24 21:50:30 -0400481 status &= ~(HC_R0INT_ENA);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500482
dea31012005-04-17 16:05:31 -0500483 writel(status, phba->HCregaddr);
484 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500485 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500486
James Smart93996272008-08-24 21:50:30 -0400487 /* Set up ring-0 (ELS) timer */
488 timeout = phba->fc_ratov * 2;
James Smart2e0fef82007-06-17 19:56:36 -0500489 mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
James Smart93996272008-08-24 21:50:30 -0400490 /* Set up heart beat (HB) timer */
James Smart858c9f62007-06-17 19:56:39 -0500491 mod_timer(&phba->hb_tmofunc, jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
492 phba->hb_outstanding = 0;
493 phba->last_completion_time = jiffies;
James Smart93996272008-08-24 21:50:30 -0400494 /* Set up error attention (ERATT) polling timer */
495 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
dea31012005-04-17 16:05:31 -0500496
497 lpfc_init_link(phba, pmb, phba->cfg_topology, phba->cfg_link_speed);
498 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -0400499 lpfc_set_loopback_flag(phba);
James Smartd7c255b2008-08-24 21:50:00 -0400500 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart8aee9182006-08-31 12:27:57 -0400501 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500502 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400503 "0454 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500504 "INIT_LINK, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500505 mb->mbxCommand, mb->mbxStatus);
506
507 /* Clear all interrupt enable conditions */
508 writel(0, phba->HCregaddr);
509 readl(phba->HCregaddr); /* flush */
510 /* Clear all pending interrupts */
511 writel(0xffffffff, phba->HAregaddr);
512 readl(phba->HAregaddr); /* flush */
513
James Smart2e0fef82007-06-17 19:56:36 -0500514 phba->link_state = LPFC_HBA_ERROR;
James Smart8aee9182006-08-31 12:27:57 -0400515 if (rc != MBX_BUSY)
516 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500517 return -EIO;
518 }
519 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400520 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
521 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
522 pmb->mbox_cmpl = lpfc_config_async_cmpl;
523 pmb->vport = phba->pport;
524 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500525
James Smart57127f12007-10-27 13:37:05 -0400526 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
527 lpfc_printf_log(phba,
528 KERN_ERR,
529 LOG_INIT,
530 "0456 Adapter failed to issue "
531 "ASYNCEVT_ENABLE mbox status x%x \n.",
532 rc);
533 mempool_free(pmb, phba->mbox_mem_pool);
534 }
James Smart97207482008-12-04 22:39:19 -0500535
536 /* Get Option rom version */
537 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
538 lpfc_dump_wakeup_param(phba, pmb);
539 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
540 pmb->vport = phba->pport;
541 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
542
543 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
544 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
545 "to get Option ROM version status x%x\n.", rc);
546 mempool_free(pmb, phba->mbox_mem_pool);
547 }
548
James Smartd7c255b2008-08-24 21:50:00 -0400549 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400550}
551
James Smarte59058c2008-08-24 21:49:00 -0400552/**
James Smart3621a712009-04-06 18:47:14 -0400553 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400554 * @phba: pointer to lpfc HBA data structure.
555 *
556 * This routine will do LPFC uninitialization before the HBA is reset when
557 * bringing down the SLI Layer.
558 *
559 * Return codes
560 * 0 - success.
561 * Any other value - error.
562 **/
dea31012005-04-17 16:05:31 -0500563int
James Smart2e0fef82007-06-17 19:56:36 -0500564lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500565{
James Smart1b32f6a2008-02-08 18:49:39 -0500566 struct lpfc_vport **vports;
567 int i;
dea31012005-04-17 16:05:31 -0500568 /* Disable interrupts */
569 writel(0, phba->HCregaddr);
570 readl(phba->HCregaddr); /* flush */
571
James Smart1b32f6a2008-02-08 18:49:39 -0500572 if (phba->pport->load_flag & FC_UNLOADING)
573 lpfc_cleanup_discovery_resources(phba->pport);
574 else {
575 vports = lpfc_create_vport_work_array(phba);
576 if (vports != NULL)
577 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++)
578 lpfc_cleanup_discovery_resources(vports[i]);
579 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500580 }
581 return 0;
dea31012005-04-17 16:05:31 -0500582}
583
James Smarte59058c2008-08-24 21:49:00 -0400584/**
James Smart3621a712009-04-06 18:47:14 -0400585 * lpfc_hba_down_post - Perform lpfc uninitialization after HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400586 * @phba: pointer to lpfc HBA data structure.
587 *
588 * This routine will do uninitialization after the HBA is reset when bring
589 * down the SLI Layer.
590 *
591 * Return codes
592 * 0 - sucess.
593 * Any other value - error.
594 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -0500595int
James Smart2e0fef82007-06-17 19:56:36 -0500596lpfc_hba_down_post(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -0500597{
598 struct lpfc_sli *psli = &phba->sli;
599 struct lpfc_sli_ring *pring;
600 struct lpfc_dmabuf *mp, *next_mp;
James Smart09372822008-01-11 01:52:54 -0500601 struct lpfc_iocbq *iocb;
602 IOCB_t *cmd = NULL;
603 LIST_HEAD(completions);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500604 int i;
605
James Smart92d7f7b2007-06-17 19:56:38 -0500606 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
607 lpfc_sli_hbqbuf_free_all(phba);
608 else {
609 /* Cleanup preposted buffers on the ELS ring */
610 pring = &psli->ring[LPFC_ELS_RING];
611 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
612 list_del(&mp->list);
613 pring->postbufq_cnt--;
614 lpfc_mbuf_free(phba, mp->virt, mp->phys);
615 kfree(mp);
616 }
Jamie Wellnitz41415862006-02-28 19:25:27 -0500617 }
618
James Smart09372822008-01-11 01:52:54 -0500619 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500620 for (i = 0; i < psli->num_rings; i++) {
621 pring = &psli->ring[i];
James Smart09372822008-01-11 01:52:54 -0500622
623 /* At this point in time the HBA is either reset or DOA. Either
624 * way, nothing should be on txcmplq as it will NEVER complete.
625 */
626 list_splice_init(&pring->txcmplq, &completions);
627 pring->txcmplq_cnt = 0;
628 spin_unlock_irq(&phba->hbalock);
629
630 while (!list_empty(&completions)) {
631 iocb = list_get_first(&completions, struct lpfc_iocbq,
632 list);
633 cmd = &iocb->iocb;
634 list_del_init(&iocb->list);
635
636 if (!iocb->iocb_cmpl)
637 lpfc_sli_release_iocbq(phba, iocb);
638 else {
639 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
640 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
641 (iocb->iocb_cmpl) (phba, iocb, iocb);
642 }
643 }
644
Jamie Wellnitz41415862006-02-28 19:25:27 -0500645 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart09372822008-01-11 01:52:54 -0500646 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500647 }
James Smart09372822008-01-11 01:52:54 -0500648 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500649
650 return 0;
651}
652
James Smarte59058c2008-08-24 21:49:00 -0400653/**
James Smart3621a712009-04-06 18:47:14 -0400654 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -0400655 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
656 *
657 * This is the HBA-timer timeout handler registered to the lpfc driver. When
658 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
659 * work-port-events bitmap and the worker thread is notified. This timeout
660 * event will be used by the worker thread to invoke the actual timeout
661 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
662 * be performed in the timeout handler and the HBA timeout event bit shall
663 * be cleared by the worker thread after it has taken the event bitmap out.
664 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100665static void
James Smart858c9f62007-06-17 19:56:39 -0500666lpfc_hb_timeout(unsigned long ptr)
667{
668 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400669 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -0500670 unsigned long iflag;
671
672 phba = (struct lpfc_hba *)ptr;
James Smart93996272008-08-24 21:50:30 -0400673
674 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -0500675 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -0400676 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
677 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -0500678 phba->pport->work_port_events |= WORKER_HB_TMO;
679 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
680
James Smart93996272008-08-24 21:50:30 -0400681 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -0400682 if (!tmo_posted)
683 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -0500684 return;
685}
686
James Smarte59058c2008-08-24 21:49:00 -0400687/**
James Smart3621a712009-04-06 18:47:14 -0400688 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -0400689 * @phba: pointer to lpfc hba data structure.
690 * @pmboxq: pointer to the driver internal queue element for mailbox command.
691 *
692 * This is the callback function to the lpfc heart-beat mailbox command.
693 * If configured, the lpfc driver issues the heart-beat mailbox command to
694 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
695 * heart-beat mailbox command is issued, the driver shall set up heart-beat
696 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
697 * heart-beat outstanding state. Once the mailbox command comes back and
698 * no error conditions detected, the heart-beat mailbox command timer is
699 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
700 * state is cleared for the next heart-beat. If the timer expired with the
701 * heart-beat outstanding state set, the driver will put the HBA offline.
702 **/
James Smart858c9f62007-06-17 19:56:39 -0500703static void
704lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
705{
706 unsigned long drvr_flag;
707
708 spin_lock_irqsave(&phba->hbalock, drvr_flag);
709 phba->hb_outstanding = 0;
710 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
711
James Smart93996272008-08-24 21:50:30 -0400712 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -0500713 mempool_free(pmboxq, phba->mbox_mem_pool);
714 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
715 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -0400716 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -0500717 mod_timer(&phba->hb_tmofunc,
718 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
719 return;
720}
721
James Smarte59058c2008-08-24 21:49:00 -0400722/**
James Smart3621a712009-04-06 18:47:14 -0400723 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -0400724 * @phba: pointer to lpfc hba data structure.
725 *
726 * This is the actual HBA-timer timeout handler to be invoked by the worker
727 * thread whenever the HBA timer fired and HBA-timeout event posted. This
728 * handler performs any periodic operations needed for the device. If such
729 * periodic event has already been attended to either in the interrupt handler
730 * or by processing slow-ring or fast-ring events within the HBA-timer
731 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
732 * the timer for the next timeout period. If lpfc heart-beat mailbox command
733 * is configured and there is no heart-beat mailbox command outstanding, a
734 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
735 * has been a heart-beat mailbox command outstanding, the HBA shall be put
736 * to offline.
737 **/
James Smart858c9f62007-06-17 19:56:39 -0500738void
739lpfc_hb_timeout_handler(struct lpfc_hba *phba)
740{
741 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -0500742 struct lpfc_dmabuf *buf_ptr;
James Smart858c9f62007-06-17 19:56:39 -0500743 int retval;
744 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -0500745 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -0500746
747 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -0400748 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -0500749 (phba->pport->fc_flag & FC_OFFLINE_MODE))
750 return;
751
752 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -0500753
754 if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
755 jiffies)) {
756 spin_unlock_irq(&phba->pport->work_port_lock);
757 if (!phba->hb_outstanding)
758 mod_timer(&phba->hb_tmofunc,
759 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
760 else
761 mod_timer(&phba->hb_tmofunc,
762 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
763 return;
764 }
765 spin_unlock_irq(&phba->pport->work_port_lock);
766
James Smart0ff10d42008-01-11 01:52:36 -0500767 if (phba->elsbuf_cnt &&
768 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
769 spin_lock_irq(&phba->hbalock);
770 list_splice_init(&phba->elsbuf, &completions);
771 phba->elsbuf_cnt = 0;
772 phba->elsbuf_prev_cnt = 0;
773 spin_unlock_irq(&phba->hbalock);
774
775 while (!list_empty(&completions)) {
776 list_remove_head(&completions, buf_ptr,
777 struct lpfc_dmabuf, list);
778 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
779 kfree(buf_ptr);
780 }
781 }
782 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
783
James Smart858c9f62007-06-17 19:56:39 -0500784 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -0500785 if (phba->cfg_enable_hba_heartbeat) {
786 if (!phba->hb_outstanding) {
787 pmboxq = mempool_alloc(phba->mbox_mem_pool,GFP_KERNEL);
788 if (!pmboxq) {
789 mod_timer(&phba->hb_tmofunc,
790 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
791 return;
792 }
793
794 lpfc_heart_beat(phba, pmboxq);
795 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
796 pmboxq->vport = phba->pport;
797 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
798
799 if (retval != MBX_BUSY && retval != MBX_SUCCESS) {
800 mempool_free(pmboxq, phba->mbox_mem_pool);
801 mod_timer(&phba->hb_tmofunc,
802 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
803 return;
804 }
James Smart858c9f62007-06-17 19:56:39 -0500805 mod_timer(&phba->hb_tmofunc,
James Smart13815c82008-01-11 01:52:48 -0500806 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
807 phba->hb_outstanding = 1;
James Smart858c9f62007-06-17 19:56:39 -0500808 return;
James Smart13815c82008-01-11 01:52:48 -0500809 } else {
810 /*
811 * If heart beat timeout called with hb_outstanding set
812 * we need to take the HBA offline.
813 */
814 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
815 "0459 Adapter heartbeat failure, "
816 "taking this port offline.\n");
817
818 spin_lock_irq(&phba->hbalock);
819 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
820 spin_unlock_irq(&phba->hbalock);
821
822 lpfc_offline_prep(phba);
823 lpfc_offline(phba);
824 lpfc_unblock_mgmt_io(phba);
825 phba->link_state = LPFC_HBA_ERROR;
826 lpfc_hba_down_post(phba);
James Smart858c9f62007-06-17 19:56:39 -0500827 }
James Smart858c9f62007-06-17 19:56:39 -0500828 }
829}
830
James Smarte59058c2008-08-24 21:49:00 -0400831/**
James Smart3621a712009-04-06 18:47:14 -0400832 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -0400833 * @phba: pointer to lpfc hba data structure.
834 *
835 * This routine is called to bring the HBA offline when HBA hardware error
836 * other than Port Error 6 has been detected.
837 **/
James Smart09372822008-01-11 01:52:54 -0500838static void
839lpfc_offline_eratt(struct lpfc_hba *phba)
840{
841 struct lpfc_sli *psli = &phba->sli;
842
843 spin_lock_irq(&phba->hbalock);
844 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
845 spin_unlock_irq(&phba->hbalock);
846 lpfc_offline_prep(phba);
847
848 lpfc_offline(phba);
849 lpfc_reset_barrier(phba);
850 lpfc_sli_brdreset(phba);
851 lpfc_hba_down_post(phba);
852 lpfc_sli_brdready(phba, HS_MBRDY);
853 lpfc_unblock_mgmt_io(phba);
854 phba->link_state = LPFC_HBA_ERROR;
855 return;
856}
857
James Smarte59058c2008-08-24 21:49:00 -0400858/**
James Smart3621a712009-04-06 18:47:14 -0400859 * lpfc_handle_eratt - The HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -0400860 * @phba: pointer to lpfc hba data structure.
861 *
862 * This routine is invoked to handle the following HBA hardware error
863 * conditions:
864 * 1 - HBA error attention interrupt
865 * 2 - DMA ring index out of range
866 * 3 - Mailbox command came back as unknown
867 **/
dea31012005-04-17 16:05:31 -0500868void
James Smart2e0fef82007-06-17 19:56:36 -0500869lpfc_handle_eratt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500870{
James Smart2e0fef82007-06-17 19:56:36 -0500871 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -0500872 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -0500873 struct lpfc_sli_ring *pring;
James Smartd2873e42006-08-18 17:46:43 -0400874 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -0400875 unsigned long temperature;
876 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -0500877 struct Scsi_Host *shost;
James Smartea2151b2008-09-07 11:52:10 -0400878 struct lpfc_board_event_header board_event;
James Smart2e0fef82007-06-17 19:56:36 -0500879
Linas Vepstas8d63f372007-02-14 14:28:36 -0600880 /* If the pci channel is offline, ignore possible errors,
881 * since we cannot communicate with the pci card anyway. */
882 if (pci_channel_offline(phba->pcidev))
883 return;
James Smart13815c82008-01-11 01:52:48 -0500884 /* If resets are disabled then leave the HBA alone and return */
885 if (!phba->cfg_enable_hba_reset)
886 return;
dea31012005-04-17 16:05:31 -0500887
James Smartea2151b2008-09-07 11:52:10 -0400888 /* Send an internal error event to mgmt application */
889 board_event.event_type = FC_REG_BOARD_EVENT;
890 board_event.subcategory = LPFC_EVENT_PORTINTERR;
891 shost = lpfc_shost_from_vport(phba->pport);
892 fc_host_post_vendor_event(shost, fc_get_event_number(),
893 sizeof(board_event),
894 (char *) &board_event,
James Smartddcc50f2008-12-04 22:38:46 -0500895 LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -0400896
James Smart97eab632008-04-07 10:16:05 -0400897 if (phba->work_hs & HS_FFER6) {
dea31012005-04-17 16:05:31 -0500898 /* Re-establishing Link */
899 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -0400900 "1301 Re-establishing Link "
dea31012005-04-17 16:05:31 -0500901 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400902 phba->work_hs,
dea31012005-04-17 16:05:31 -0500903 phba->work_status[0], phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -0400904
James Smart92d7f7b2007-06-17 19:56:38 -0500905 spin_lock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -0500906 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -0500907 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500908
909 /*
910 * Firmware stops when it triggled erratt with HS_FFER6.
911 * That could cause the I/Os dropped by the firmware.
912 * Error iocb (I/O) on txcmplq and let the SCSI layer
913 * retry it after re-establishing link.
914 */
915 pring = &psli->ring[psli->fcp_ring];
916 lpfc_sli_abort_iocb_ring(phba, pring);
917
dea31012005-04-17 16:05:31 -0500918 /*
919 * There was a firmware error. Take the hba offline and then
920 * attempt to restart it.
921 */
James Smart46fa3112007-04-25 09:51:45 -0400922 lpfc_offline_prep(phba);
dea31012005-04-17 16:05:31 -0500923 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500924 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -0500925 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -0400926 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -0500927 return;
928 }
James Smart46fa3112007-04-25 09:51:45 -0400929 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -0400930 } else if (phba->work_hs & HS_CRIT_TEMP) {
931 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
932 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
933 temp_event_data.event_code = LPFC_CRIT_TEMP;
934 temp_event_data.data = (uint32_t)temperature;
935
936 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -0400937 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -0400938 "(%ld), taking this port offline "
939 "Data: x%x x%x x%x\n",
940 temperature, phba->work_hs,
941 phba->work_status[0], phba->work_status[1]);
942
943 shost = lpfc_shost_from_vport(phba->pport);
944 fc_host_post_vendor_event(shost, fc_get_event_number(),
945 sizeof(temp_event_data),
946 (char *) &temp_event_data,
947 SCSI_NL_VID_TYPE_PCI
948 | PCI_VENDOR_ID_EMULEX);
949
James Smart7af67052007-10-27 13:38:11 -0400950 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -0400951 phba->over_temp_state = HBA_OVER_TEMP;
952 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -0500953 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -0400954
dea31012005-04-17 16:05:31 -0500955 } else {
956 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -0400957 * failure is a value other than FFER6. Do not call the offline
958 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -0500959 */
960 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400961 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -0500962 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400963 phba->work_hs,
dea31012005-04-17 16:05:31 -0500964 phba->work_status[0], phba->work_status[1]);
965
James Smartd2873e42006-08-18 17:46:43 -0400966 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -0500967 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -0500968 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -0400969 sizeof(event_data), (char *) &event_data,
970 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
971
James Smart09372822008-01-11 01:52:54 -0500972 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -0500973 }
James Smart93996272008-08-24 21:50:30 -0400974 return;
dea31012005-04-17 16:05:31 -0500975}
976
James Smarte59058c2008-08-24 21:49:00 -0400977/**
James Smart3621a712009-04-06 18:47:14 -0400978 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -0400979 * @phba: pointer to lpfc hba data structure.
980 *
981 * This routine is invoked from the worker thread to handle a HBA host
982 * attention link event.
983 **/
dea31012005-04-17 16:05:31 -0500984void
James Smart2e0fef82007-06-17 19:56:36 -0500985lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500986{
James Smart2e0fef82007-06-17 19:56:36 -0500987 struct lpfc_vport *vport = phba->pport;
988 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -0500989 LPFC_MBOXQ_t *pmb;
990 volatile uint32_t control;
991 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -0500992 int rc = 0;
dea31012005-04-17 16:05:31 -0500993
994 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -0500995 if (!pmb) {
996 rc = 1;
dea31012005-04-17 16:05:31 -0500997 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -0500998 }
dea31012005-04-17 16:05:31 -0500999
1000 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001001 if (!mp) {
1002 rc = 2;
dea31012005-04-17 16:05:31 -05001003 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05001004 }
dea31012005-04-17 16:05:31 -05001005
1006 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05001007 if (!mp->virt) {
1008 rc = 3;
dea31012005-04-17 16:05:31 -05001009 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05001010 }
dea31012005-04-17 16:05:31 -05001011
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05001012 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04001013 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05001014
1015 psli->slistat.link_event++;
1016 lpfc_read_la(phba, pmb, mp);
1017 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_la;
James Smart2e0fef82007-06-17 19:56:36 -05001018 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04001019 /* Block ELS IOCBs until we have processed this mbox command */
1020 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04001021 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05001022 if (rc == MBX_NOT_FINISHED) {
1023 rc = 4;
James Smart14691152006-12-02 13:34:28 -05001024 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05001025 }
dea31012005-04-17 16:05:31 -05001026
1027 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05001028 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001029 writel(HA_LATT, phba->HAregaddr);
1030 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001031 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001032
1033 return;
1034
James Smart14691152006-12-02 13:34:28 -05001035lpfc_handle_latt_free_mbuf:
James Smart0d2b6b82008-06-14 22:52:47 -04001036 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05001037 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001038lpfc_handle_latt_free_mp:
1039 kfree(mp);
1040lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04001041 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001042lpfc_handle_latt_err_exit:
1043 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001044 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001045 psli->sli_flag |= LPFC_PROCESS_LA;
1046 control = readl(phba->HCregaddr);
1047 control |= HC_LAINT_ENA;
1048 writel(control, phba->HCregaddr);
1049 readl(phba->HCregaddr); /* flush */
1050
1051 /* Clear Link Attention in HA REG */
1052 writel(HA_LATT, phba->HAregaddr);
1053 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001054 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001055 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001056 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001057
James Smart09372822008-01-11 01:52:54 -05001058 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1059 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05001060
1061 return;
1062}
1063
James Smarte59058c2008-08-24 21:49:00 -04001064/**
James Smart3621a712009-04-06 18:47:14 -04001065 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04001066 * @phba: pointer to lpfc hba data structure.
1067 * @vpd: pointer to the vital product data.
1068 * @len: length of the vital product data in bytes.
1069 *
1070 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1071 * an array of characters. In this routine, the ModelName, ProgramType, and
1072 * ModelDesc, etc. fields of the phba data structure will be populated.
1073 *
1074 * Return codes
1075 * 0 - pointer to the VPD passed in is NULL
1076 * 1 - success
1077 **/
dea31012005-04-17 16:05:31 -05001078static int
James Smart2e0fef82007-06-17 19:56:36 -05001079lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05001080{
1081 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05001082 int Length;
dea31012005-04-17 16:05:31 -05001083 int i, j;
1084 int finished = 0;
1085 int index = 0;
1086
1087 if (!vpd)
1088 return 0;
1089
1090 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05001091 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001092 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05001093 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1094 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001095 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05001096 switch (vpd[index]) {
1097 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001098 case 0x91:
dea31012005-04-17 16:05:31 -05001099 index += 1;
1100 lenlo = vpd[index];
1101 index += 1;
1102 lenhi = vpd[index];
1103 index += 1;
1104 i = ((((unsigned short)lenhi) << 8) + lenlo);
1105 index += i;
1106 break;
1107 case 0x90:
1108 index += 1;
1109 lenlo = vpd[index];
1110 index += 1;
1111 lenhi = vpd[index];
1112 index += 1;
1113 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001114 if (Length > len - index)
1115 Length = len - index;
dea31012005-04-17 16:05:31 -05001116 while (Length > 0) {
1117 /* Look for Serial Number */
1118 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1119 index += 2;
1120 i = vpd[index];
1121 index += 1;
1122 j = 0;
1123 Length -= (3+i);
1124 while(i--) {
1125 phba->SerialNumber[j++] = vpd[index++];
1126 if (j == 31)
1127 break;
1128 }
1129 phba->SerialNumber[j] = 0;
1130 continue;
1131 }
1132 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1133 phba->vpd_flag |= VPD_MODEL_DESC;
1134 index += 2;
1135 i = vpd[index];
1136 index += 1;
1137 j = 0;
1138 Length -= (3+i);
1139 while(i--) {
1140 phba->ModelDesc[j++] = vpd[index++];
1141 if (j == 255)
1142 break;
1143 }
1144 phba->ModelDesc[j] = 0;
1145 continue;
1146 }
1147 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1148 phba->vpd_flag |= VPD_MODEL_NAME;
1149 index += 2;
1150 i = vpd[index];
1151 index += 1;
1152 j = 0;
1153 Length -= (3+i);
1154 while(i--) {
1155 phba->ModelName[j++] = vpd[index++];
1156 if (j == 79)
1157 break;
1158 }
1159 phba->ModelName[j] = 0;
1160 continue;
1161 }
1162 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1163 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1164 index += 2;
1165 i = vpd[index];
1166 index += 1;
1167 j = 0;
1168 Length -= (3+i);
1169 while(i--) {
1170 phba->ProgramType[j++] = vpd[index++];
1171 if (j == 255)
1172 break;
1173 }
1174 phba->ProgramType[j] = 0;
1175 continue;
1176 }
1177 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1178 phba->vpd_flag |= VPD_PORT;
1179 index += 2;
1180 i = vpd[index];
1181 index += 1;
1182 j = 0;
1183 Length -= (3+i);
1184 while(i--) {
1185 phba->Port[j++] = vpd[index++];
1186 if (j == 19)
1187 break;
1188 }
1189 phba->Port[j] = 0;
1190 continue;
1191 }
1192 else {
1193 index += 2;
1194 i = vpd[index];
1195 index += 1;
1196 index += i;
1197 Length -= (3 + i);
1198 }
1199 }
1200 finished = 0;
1201 break;
1202 case 0x78:
1203 finished = 1;
1204 break;
1205 default:
1206 index ++;
1207 break;
1208 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001209 }
dea31012005-04-17 16:05:31 -05001210
1211 return(1);
1212}
1213
James Smarte59058c2008-08-24 21:49:00 -04001214/**
James Smart3621a712009-04-06 18:47:14 -04001215 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04001216 * @phba: pointer to lpfc hba data structure.
1217 * @mdp: pointer to the data structure to hold the derived model name.
1218 * @descp: pointer to the data structure to hold the derived description.
1219 *
1220 * This routine retrieves HBA's description based on its registered PCI device
1221 * ID. The @descp passed into this function points to an array of 256 chars. It
1222 * shall be returned with the model name, maximum speed, and the host bus type.
1223 * The @mdp passed into this function points to an array of 80 chars. When the
1224 * function returns, the @mdp will be filled with the model name.
1225 **/
dea31012005-04-17 16:05:31 -05001226static void
James Smart2e0fef82007-06-17 19:56:36 -05001227lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05001228{
1229 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05001230 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001231 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04001232 int GE = 0;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001233 struct {
1234 char * name;
1235 int max_speed;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001236 char * bus;
James Smart18a3b592006-12-02 13:35:08 -05001237 } m = {"<Unknown>", 0, ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001238
1239 if (mdp && mdp[0] != '\0'
1240 && descp && descp[0] != '\0')
1241 return;
1242
1243 if (phba->lmt & LMT_10Gb)
1244 max_speed = 10;
1245 else if (phba->lmt & LMT_8Gb)
1246 max_speed = 8;
1247 else if (phba->lmt & LMT_4Gb)
1248 max_speed = 4;
1249 else if (phba->lmt & LMT_2Gb)
1250 max_speed = 2;
1251 else
1252 max_speed = 1;
dea31012005-04-17 16:05:31 -05001253
1254 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05001255
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001256 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001257 case PCI_DEVICE_ID_FIREFLY:
James Smart18a3b592006-12-02 13:35:08 -05001258 m = (typeof(m)){"LP6000", max_speed, "PCI"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001259 break;
dea31012005-04-17 16:05:31 -05001260 case PCI_DEVICE_ID_SUPERFLY:
1261 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smart18a3b592006-12-02 13:35:08 -05001262 m = (typeof(m)){"LP7000", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001263 else
James Smart18a3b592006-12-02 13:35:08 -05001264 m = (typeof(m)){"LP7000E", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001265 break;
1266 case PCI_DEVICE_ID_DRAGONFLY:
James Smart18a3b592006-12-02 13:35:08 -05001267 m = (typeof(m)){"LP8000", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001268 break;
1269 case PCI_DEVICE_ID_CENTAUR:
1270 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smart18a3b592006-12-02 13:35:08 -05001271 m = (typeof(m)){"LP9002", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001272 else
James Smart18a3b592006-12-02 13:35:08 -05001273 m = (typeof(m)){"LP9000", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001274 break;
1275 case PCI_DEVICE_ID_RFLY:
James Smart18a3b592006-12-02 13:35:08 -05001276 m = (typeof(m)){"LP952", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -05001277 break;
1278 case PCI_DEVICE_ID_PEGASUS:
James Smart18a3b592006-12-02 13:35:08 -05001279 m = (typeof(m)){"LP9802", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001280 break;
1281 case PCI_DEVICE_ID_THOR:
James Smart18a3b592006-12-02 13:35:08 -05001282 m = (typeof(m)){"LP10000", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001283 break;
1284 case PCI_DEVICE_ID_VIPER:
James Smart18a3b592006-12-02 13:35:08 -05001285 m = (typeof(m)){"LPX1000", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001286 break;
1287 case PCI_DEVICE_ID_PFLY:
James Smart18a3b592006-12-02 13:35:08 -05001288 m = (typeof(m)){"LP982", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001289 break;
1290 case PCI_DEVICE_ID_TFLY:
James Smart18a3b592006-12-02 13:35:08 -05001291 m = (typeof(m)){"LP1050", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001292 break;
1293 case PCI_DEVICE_ID_HELIOS:
James Smart18a3b592006-12-02 13:35:08 -05001294 m = (typeof(m)){"LP11000", max_speed, "PCI-X2"};
dea31012005-04-17 16:05:31 -05001295 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001296 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smart18a3b592006-12-02 13:35:08 -05001297 m = (typeof(m)){"LP11000-SP", max_speed, "PCI-X2"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001298 break;
1299 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smart18a3b592006-12-02 13:35:08 -05001300 m = (typeof(m)){"LP11002-SP", max_speed, "PCI-X2"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001301 break;
1302 case PCI_DEVICE_ID_NEPTUNE:
James Smart18a3b592006-12-02 13:35:08 -05001303 m = (typeof(m)){"LPe1000", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001304 break;
1305 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smart18a3b592006-12-02 13:35:08 -05001306 m = (typeof(m)){"LPe1000-SP", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001307 break;
1308 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smart18a3b592006-12-02 13:35:08 -05001309 m = (typeof(m)){"LPe1002-SP", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001310 break;
dea31012005-04-17 16:05:31 -05001311 case PCI_DEVICE_ID_BMID:
James Smart18a3b592006-12-02 13:35:08 -05001312 m = (typeof(m)){"LP1150", max_speed, "PCI-X2"};
dea31012005-04-17 16:05:31 -05001313 break;
1314 case PCI_DEVICE_ID_BSMB:
James Smart18a3b592006-12-02 13:35:08 -05001315 m = (typeof(m)){"LP111", max_speed, "PCI-X2"};
dea31012005-04-17 16:05:31 -05001316 break;
1317 case PCI_DEVICE_ID_ZEPHYR:
James Smart18a3b592006-12-02 13:35:08 -05001318 m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
dea31012005-04-17 16:05:31 -05001319 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001320 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smart18a3b592006-12-02 13:35:08 -05001321 m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001322 break;
1323 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smart18a3b592006-12-02 13:35:08 -05001324 m = (typeof(m)){"LPe11002-SP", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001325 break;
dea31012005-04-17 16:05:31 -05001326 case PCI_DEVICE_ID_ZMID:
James Smart18a3b592006-12-02 13:35:08 -05001327 m = (typeof(m)){"LPe1150", max_speed, "PCIe"};
dea31012005-04-17 16:05:31 -05001328 break;
1329 case PCI_DEVICE_ID_ZSMB:
James Smart18a3b592006-12-02 13:35:08 -05001330 m = (typeof(m)){"LPe111", max_speed, "PCIe"};
dea31012005-04-17 16:05:31 -05001331 break;
1332 case PCI_DEVICE_ID_LP101:
James Smart18a3b592006-12-02 13:35:08 -05001333 m = (typeof(m)){"LP101", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -05001334 break;
1335 case PCI_DEVICE_ID_LP10000S:
James Smart18a3b592006-12-02 13:35:08 -05001336 m = (typeof(m)){"LP10000-S", max_speed, "PCI"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001337 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001338 case PCI_DEVICE_ID_LP11000S:
James Smart18a3b592006-12-02 13:35:08 -05001339 m = (typeof(m)){"LP11000-S", max_speed,
1340 "PCI-X2"};
1341 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001342 case PCI_DEVICE_ID_LPE11000S:
James Smart18a3b592006-12-02 13:35:08 -05001343 m = (typeof(m)){"LPe11000-S", max_speed,
1344 "PCIe"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05001345 break;
James Smartb87eab32007-04-25 09:53:28 -04001346 case PCI_DEVICE_ID_SAT:
1347 m = (typeof(m)){"LPe12000", max_speed, "PCIe"};
1348 break;
1349 case PCI_DEVICE_ID_SAT_MID:
1350 m = (typeof(m)){"LPe1250", max_speed, "PCIe"};
1351 break;
1352 case PCI_DEVICE_ID_SAT_SMB:
1353 m = (typeof(m)){"LPe121", max_speed, "PCIe"};
1354 break;
1355 case PCI_DEVICE_ID_SAT_DCSP:
1356 m = (typeof(m)){"LPe12002-SP", max_speed, "PCIe"};
1357 break;
1358 case PCI_DEVICE_ID_SAT_SCSP:
1359 m = (typeof(m)){"LPe12000-SP", max_speed, "PCIe"};
1360 break;
1361 case PCI_DEVICE_ID_SAT_S:
1362 m = (typeof(m)){"LPe12000-S", max_speed, "PCIe"};
1363 break;
James Smart84774a42008-08-24 21:50:06 -04001364 case PCI_DEVICE_ID_HORNET:
1365 m = (typeof(m)){"LP21000", max_speed, "PCIe"};
1366 GE = 1;
1367 break;
1368 case PCI_DEVICE_ID_PROTEUS_VF:
1369 m = (typeof(m)) {"LPev12000", max_speed, "PCIe IOV"};
1370 break;
1371 case PCI_DEVICE_ID_PROTEUS_PF:
1372 m = (typeof(m)) {"LPev12000", max_speed, "PCIe IOV"};
1373 break;
1374 case PCI_DEVICE_ID_PROTEUS_S:
1375 m = (typeof(m)) {"LPemv12002-S", max_speed, "PCIe IOV"};
1376 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05001377 default:
Randy Dunlap041976f2006-06-25 01:58:51 -07001378 m = (typeof(m)){ NULL };
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001379 break;
dea31012005-04-17 16:05:31 -05001380 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001381
1382 if (mdp && mdp[0] == '\0')
1383 snprintf(mdp, 79,"%s", m.name);
1384 if (descp && descp[0] == '\0')
1385 snprintf(descp, 255,
James Smart84774a42008-08-24 21:50:06 -04001386 "Emulex %s %d%s %s %s",
1387 m.name, m.max_speed,
1388 (GE) ? "GE" : "Gb",
1389 m.bus,
1390 (GE) ? "FCoE Adapter" : "Fibre Channel Adapter");
dea31012005-04-17 16:05:31 -05001391}
1392
James Smarte59058c2008-08-24 21:49:00 -04001393/**
James Smart3621a712009-04-06 18:47:14 -04001394 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04001395 * @phba: pointer to lpfc hba data structure.
1396 * @pring: pointer to a IOCB ring.
1397 * @cnt: the number of IOCBs to be posted to the IOCB ring.
1398 *
1399 * This routine posts a given number of IOCBs with the associated DMA buffer
1400 * descriptors specified by the cnt argument to the given IOCB ring.
1401 *
1402 * Return codes
1403 * The number of IOCBs NOT able to be posted to the IOCB ring.
1404 **/
dea31012005-04-17 16:05:31 -05001405int
James Smart495a7142008-06-14 22:52:59 -04001406lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05001407{
1408 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001409 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05001410 struct lpfc_dmabuf *mp1, *mp2;
1411
1412 cnt += pring->missbufcnt;
1413
1414 /* While there are buffers to post */
1415 while (cnt > 0) {
1416 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001417 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05001418 if (iocb == NULL) {
1419 pring->missbufcnt = cnt;
1420 return cnt;
1421 }
dea31012005-04-17 16:05:31 -05001422 icmd = &iocb->iocb;
1423
1424 /* 2 buffers can be posted per command */
1425 /* Allocate buffer to post */
1426 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1427 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04001428 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
1429 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001430 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05001431 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001432 pring->missbufcnt = cnt;
1433 return cnt;
1434 }
1435
1436 INIT_LIST_HEAD(&mp1->list);
1437 /* Allocate buffer to post */
1438 if (cnt > 1) {
1439 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1440 if (mp2)
1441 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
1442 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04001443 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001444 kfree(mp2);
dea31012005-04-17 16:05:31 -05001445 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1446 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05001447 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001448 pring->missbufcnt = cnt;
1449 return cnt;
1450 }
1451
1452 INIT_LIST_HEAD(&mp2->list);
1453 } else {
1454 mp2 = NULL;
1455 }
1456
1457 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
1458 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
1459 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
1460 icmd->ulpBdeCount = 1;
1461 cnt--;
1462 if (mp2) {
1463 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
1464 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
1465 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
1466 cnt--;
1467 icmd->ulpBdeCount = 2;
1468 }
1469
1470 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
1471 icmd->ulpLe = 1;
1472
dea31012005-04-17 16:05:31 -05001473 if (lpfc_sli_issue_iocb(phba, pring, iocb, 0) == IOCB_ERROR) {
1474 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1475 kfree(mp1);
1476 cnt++;
1477 if (mp2) {
1478 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
1479 kfree(mp2);
1480 cnt++;
1481 }
James Bottomley604a3e32005-10-29 10:28:33 -05001482 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001483 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05001484 return cnt;
1485 }
dea31012005-04-17 16:05:31 -05001486 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05001487 if (mp2)
dea31012005-04-17 16:05:31 -05001488 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05001489 }
1490 pring->missbufcnt = 0;
1491 return 0;
1492}
1493
James Smarte59058c2008-08-24 21:49:00 -04001494/**
James Smart3621a712009-04-06 18:47:14 -04001495 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04001496 * @phba: pointer to lpfc hba data structure.
1497 *
1498 * This routine posts initial receive IOCB buffers to the ELS ring. The
1499 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
1500 * set to 64 IOCBs.
1501 *
1502 * Return codes
1503 * 0 - success (currently always success)
1504 **/
dea31012005-04-17 16:05:31 -05001505static int
James Smart2e0fef82007-06-17 19:56:36 -05001506lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001507{
1508 struct lpfc_sli *psli = &phba->sli;
1509
1510 /* Ring 0, ELS / CT buffers */
James Smart495a7142008-06-14 22:52:59 -04001511 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05001512 /* Ring 2 - FCP no buffers needed */
1513
1514 return 0;
1515}
1516
1517#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
1518
James Smarte59058c2008-08-24 21:49:00 -04001519/**
James Smart3621a712009-04-06 18:47:14 -04001520 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04001521 * @HashResultPointer: pointer to an array as hash table.
1522 *
1523 * This routine sets up the initial values to the array of hash table entries
1524 * for the LC HBAs.
1525 **/
dea31012005-04-17 16:05:31 -05001526static void
1527lpfc_sha_init(uint32_t * HashResultPointer)
1528{
1529 HashResultPointer[0] = 0x67452301;
1530 HashResultPointer[1] = 0xEFCDAB89;
1531 HashResultPointer[2] = 0x98BADCFE;
1532 HashResultPointer[3] = 0x10325476;
1533 HashResultPointer[4] = 0xC3D2E1F0;
1534}
1535
James Smarte59058c2008-08-24 21:49:00 -04001536/**
James Smart3621a712009-04-06 18:47:14 -04001537 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04001538 * @HashResultPointer: pointer to an initial/result hash table.
1539 * @HashWorkingPointer: pointer to an working hash table.
1540 *
1541 * This routine iterates an initial hash table pointed by @HashResultPointer
1542 * with the values from the working hash table pointeed by @HashWorkingPointer.
1543 * The results are putting back to the initial hash table, returned through
1544 * the @HashResultPointer as the result hash table.
1545 **/
dea31012005-04-17 16:05:31 -05001546static void
1547lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
1548{
1549 int t;
1550 uint32_t TEMP;
1551 uint32_t A, B, C, D, E;
1552 t = 16;
1553 do {
1554 HashWorkingPointer[t] =
1555 S(1,
1556 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
1557 8] ^
1558 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
1559 } while (++t <= 79);
1560 t = 0;
1561 A = HashResultPointer[0];
1562 B = HashResultPointer[1];
1563 C = HashResultPointer[2];
1564 D = HashResultPointer[3];
1565 E = HashResultPointer[4];
1566
1567 do {
1568 if (t < 20) {
1569 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
1570 } else if (t < 40) {
1571 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
1572 } else if (t < 60) {
1573 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
1574 } else {
1575 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
1576 }
1577 TEMP += S(5, A) + E + HashWorkingPointer[t];
1578 E = D;
1579 D = C;
1580 C = S(30, B);
1581 B = A;
1582 A = TEMP;
1583 } while (++t <= 79);
1584
1585 HashResultPointer[0] += A;
1586 HashResultPointer[1] += B;
1587 HashResultPointer[2] += C;
1588 HashResultPointer[3] += D;
1589 HashResultPointer[4] += E;
1590
1591}
1592
James Smarte59058c2008-08-24 21:49:00 -04001593/**
James Smart3621a712009-04-06 18:47:14 -04001594 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04001595 * @RandomChallenge: pointer to the entry of host challenge random number array.
1596 * @HashWorking: pointer to the entry of the working hash array.
1597 *
1598 * This routine calculates the working hash array referred by @HashWorking
1599 * from the challenge random numbers associated with the host, referred by
1600 * @RandomChallenge. The result is put into the entry of the working hash
1601 * array and returned by reference through @HashWorking.
1602 **/
dea31012005-04-17 16:05:31 -05001603static void
1604lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
1605{
1606 *HashWorking = (*RandomChallenge ^ *HashWorking);
1607}
1608
James Smarte59058c2008-08-24 21:49:00 -04001609/**
James Smart3621a712009-04-06 18:47:14 -04001610 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04001611 * @phba: pointer to lpfc hba data structure.
1612 * @hbainit: pointer to an array of unsigned 32-bit integers.
1613 *
1614 * This routine performs the special handling for LC HBA initialization.
1615 **/
dea31012005-04-17 16:05:31 -05001616void
1617lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
1618{
1619 int t;
1620 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05001621 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05001622
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02001623 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05001624 if (!HashWorking)
1625 return;
1626
dea31012005-04-17 16:05:31 -05001627 HashWorking[0] = HashWorking[78] = *pwwnn++;
1628 HashWorking[1] = HashWorking[79] = *pwwnn;
1629
1630 for (t = 0; t < 7; t++)
1631 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
1632
1633 lpfc_sha_init(hbainit);
1634 lpfc_sha_iterate(hbainit, HashWorking);
1635 kfree(HashWorking);
1636}
1637
James Smarte59058c2008-08-24 21:49:00 -04001638/**
James Smart3621a712009-04-06 18:47:14 -04001639 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04001640 * @vport: pointer to a virtual N_Port data structure.
1641 *
1642 * This routine performs the necessary cleanups before deleting the @vport.
1643 * It invokes the discovery state machine to perform necessary state
1644 * transitions and to release the ndlps associated with the @vport. Note,
1645 * the physical port is treated as @vport 0.
1646 **/
James Smart87af33f2007-10-27 13:37:43 -04001647void
James Smart2e0fef82007-06-17 19:56:36 -05001648lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05001649{
James Smart87af33f2007-10-27 13:37:43 -04001650 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001651 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04001652 int i = 0;
dea31012005-04-17 16:05:31 -05001653
James Smart87af33f2007-10-27 13:37:43 -04001654 if (phba->link_state > LPFC_LINK_DOWN)
1655 lpfc_port_link_failure(vport);
1656
1657 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05001658 if (!NLP_CHK_NODE_ACT(ndlp)) {
1659 ndlp = lpfc_enable_node(vport, ndlp,
1660 NLP_STE_UNUSED_NODE);
1661 if (!ndlp)
1662 continue;
1663 spin_lock_irq(&phba->ndlp_lock);
1664 NLP_SET_FREE_REQ(ndlp);
1665 spin_unlock_irq(&phba->ndlp_lock);
1666 /* Trigger the release of the ndlp memory */
1667 lpfc_nlp_put(ndlp);
1668 continue;
1669 }
1670 spin_lock_irq(&phba->ndlp_lock);
1671 if (NLP_CHK_FREE_REQ(ndlp)) {
1672 /* The ndlp should not be in memory free mode already */
1673 spin_unlock_irq(&phba->ndlp_lock);
1674 continue;
1675 } else
1676 /* Indicate request for freeing ndlp memory */
1677 NLP_SET_FREE_REQ(ndlp);
1678 spin_unlock_irq(&phba->ndlp_lock);
1679
James Smart58da1ff2008-04-07 10:15:56 -04001680 if (vport->port_type != LPFC_PHYSICAL_PORT &&
1681 ndlp->nlp_DID == Fabric_DID) {
1682 /* Just free up ndlp with Fabric_DID for vports */
1683 lpfc_nlp_put(ndlp);
1684 continue;
1685 }
1686
James Smart87af33f2007-10-27 13:37:43 -04001687 if (ndlp->nlp_type & NLP_FABRIC)
1688 lpfc_disc_state_machine(vport, ndlp, NULL,
1689 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05001690
James Smart87af33f2007-10-27 13:37:43 -04001691 lpfc_disc_state_machine(vport, ndlp, NULL,
1692 NLP_EVT_DEVICE_RM);
James Smart495a7142008-06-14 22:52:59 -04001693
James Smart87af33f2007-10-27 13:37:43 -04001694 }
1695
James Smarta8adb832007-10-27 13:37:53 -04001696 /* At this point, ALL ndlp's should be gone
1697 * because of the previous NLP_EVT_DEVICE_RM.
1698 * Lets wait for this to happen, if needed.
1699 */
James Smart87af33f2007-10-27 13:37:43 -04001700 while (!list_empty(&vport->fc_nodes)) {
1701
James Smarta8adb832007-10-27 13:37:53 -04001702 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04001703 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04001704 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05001705 list_for_each_entry_safe(ndlp, next_ndlp,
1706 &vport->fc_nodes, nlp_listp) {
1707 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
1708 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04001709 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05001710 "usgmap:x%x refcnt:%d\n",
1711 ndlp->nlp_DID, (void *)ndlp,
1712 ndlp->nlp_usg_map,
1713 atomic_read(
1714 &ndlp->kref.refcount));
1715 }
James Smarta8adb832007-10-27 13:37:53 -04001716 break;
James Smart87af33f2007-10-27 13:37:43 -04001717 }
James Smarta8adb832007-10-27 13:37:53 -04001718
1719 /* Wait for any activity on ndlps to settle */
1720 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04001721 }
dea31012005-04-17 16:05:31 -05001722 return;
1723}
1724
James Smarte59058c2008-08-24 21:49:00 -04001725/**
James Smart3621a712009-04-06 18:47:14 -04001726 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04001727 * @vport: pointer to a virtual N_Port data structure.
1728 *
1729 * This routine stops all the timers associated with a @vport. This function
1730 * is invoked before disabling or deleting a @vport. Note that the physical
1731 * port is treated as @vport 0.
1732 **/
James Smart92d7f7b2007-06-17 19:56:38 -05001733void
1734lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05001735{
James Smart92d7f7b2007-06-17 19:56:38 -05001736 del_timer_sync(&vport->els_tmofunc);
1737 del_timer_sync(&vport->fc_fdmitmo);
1738 lpfc_can_disctmo(vport);
1739 return;
dea31012005-04-17 16:05:31 -05001740}
1741
James Smarte59058c2008-08-24 21:49:00 -04001742/**
James Smart3621a712009-04-06 18:47:14 -04001743 * lpfc_stop_phba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04001744 * @phba: pointer to lpfc hba data structure.
1745 *
1746 * This routine stops all the timers associated with a HBA. This function is
1747 * invoked before either putting a HBA offline or unloading the driver.
1748 **/
James Smart2e0fef82007-06-17 19:56:36 -05001749static void
James Smart92d7f7b2007-06-17 19:56:38 -05001750lpfc_stop_phba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001751{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001752 del_timer_sync(&phba->fcp_poll_timer);
James Smart51ef4c22007-08-02 11:10:31 -04001753 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05001754 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05001755 del_timer_sync(&phba->fabric_block_timer);
James Smart858c9f62007-06-17 19:56:39 -05001756 phba->hb_outstanding = 0;
1757 del_timer_sync(&phba->hb_tmofunc);
James Smart93996272008-08-24 21:50:30 -04001758 del_timer_sync(&phba->eratt_poll);
James Smart2e0fef82007-06-17 19:56:36 -05001759 return;
dea31012005-04-17 16:05:31 -05001760}
1761
James Smarte59058c2008-08-24 21:49:00 -04001762/**
James Smart3621a712009-04-06 18:47:14 -04001763 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04001764 * @phba: pointer to lpfc hba data structure.
1765 *
1766 * This routine marks a HBA's management interface as blocked. Once the HBA's
1767 * management interface is marked as blocked, all the user space access to
1768 * the HBA, whether they are from sysfs interface or libdfc interface will
1769 * all be blocked. The HBA is set to block the management interface when the
1770 * driver prepares the HBA interface for online or offline.
1771 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001772static void
1773lpfc_block_mgmt_io(struct lpfc_hba * phba)
1774{
1775 unsigned long iflag;
1776
1777 spin_lock_irqsave(&phba->hbalock, iflag);
1778 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
1779 spin_unlock_irqrestore(&phba->hbalock, iflag);
1780}
1781
James Smarte59058c2008-08-24 21:49:00 -04001782/**
James Smart3621a712009-04-06 18:47:14 -04001783 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04001784 * @phba: pointer to lpfc hba data structure.
1785 *
1786 * This routine initializes the HBA and brings a HBA online. During this
1787 * process, the management interface is blocked to prevent user space access
1788 * to the HBA interfering with the driver initialization.
1789 *
1790 * Return codes
1791 * 0 - successful
1792 * 1 - failed
1793 **/
dea31012005-04-17 16:05:31 -05001794int
James Smart2e0fef82007-06-17 19:56:36 -05001795lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001796{
Julia Lawall372bd282008-12-16 16:15:08 +01001797 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04001798 struct lpfc_vport **vports;
1799 int i;
James Smart2e0fef82007-06-17 19:56:36 -05001800
dea31012005-04-17 16:05:31 -05001801 if (!phba)
1802 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01001803 vport = phba->pport;
dea31012005-04-17 16:05:31 -05001804
James Smart2e0fef82007-06-17 19:56:36 -05001805 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05001806 return 0;
1807
James Smarted957682007-06-17 19:56:37 -05001808 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001809 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05001810
James Smart46fa3112007-04-25 09:51:45 -04001811 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001812
James Smart46fa3112007-04-25 09:51:45 -04001813 if (!lpfc_sli_queue_setup(phba)) {
1814 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001815 return 1;
James Smart46fa3112007-04-25 09:51:45 -04001816 }
1817
1818 if (lpfc_sli_hba_setup(phba)) { /* Initialize the HBA */
1819 lpfc_unblock_mgmt_io(phba);
1820 return 1;
1821 }
dea31012005-04-17 16:05:31 -05001822
James Smart549e55c2007-08-02 11:09:51 -04001823 vports = lpfc_create_vport_work_array(phba);
1824 if (vports != NULL)
James Smart09372822008-01-11 01:52:54 -05001825 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04001826 struct Scsi_Host *shost;
1827 shost = lpfc_shost_from_vport(vports[i]);
1828 spin_lock_irq(shost->host_lock);
1829 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
1830 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
1831 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
1832 spin_unlock_irq(shost->host_lock);
1833 }
James Smart09372822008-01-11 01:52:54 -05001834 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05001835
James Smart46fa3112007-04-25 09:51:45 -04001836 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001837 return 0;
1838}
1839
James Smarte59058c2008-08-24 21:49:00 -04001840/**
James Smart3621a712009-04-06 18:47:14 -04001841 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04001842 * @phba: pointer to lpfc hba data structure.
1843 *
1844 * This routine marks a HBA's management interface as not blocked. Once the
1845 * HBA's management interface is marked as not blocked, all the user space
1846 * access to the HBA, whether they are from sysfs interface or libdfc
1847 * interface will be allowed. The HBA is set to block the management interface
1848 * when the driver prepares the HBA interface for online or offline and then
1849 * set to unblock the management interface afterwards.
1850 **/
James Smart46fa3112007-04-25 09:51:45 -04001851void
James Smart46fa3112007-04-25 09:51:45 -04001852lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
1853{
1854 unsigned long iflag;
1855
James Smart2e0fef82007-06-17 19:56:36 -05001856 spin_lock_irqsave(&phba->hbalock, iflag);
1857 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
1858 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04001859}
1860
James Smarte59058c2008-08-24 21:49:00 -04001861/**
James Smart3621a712009-04-06 18:47:14 -04001862 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04001863 * @phba: pointer to lpfc hba data structure.
1864 *
1865 * This routine is invoked to prepare a HBA to be brought offline. It performs
1866 * unregistration login to all the nodes on all vports and flushes the mailbox
1867 * queue to make it ready to be brought offline.
1868 **/
James Smart46fa3112007-04-25 09:51:45 -04001869void
1870lpfc_offline_prep(struct lpfc_hba * phba)
1871{
James Smart2e0fef82007-06-17 19:56:36 -05001872 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04001873 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04001874 struct lpfc_vport **vports;
1875 int i;
dea31012005-04-17 16:05:31 -05001876
James Smart2e0fef82007-06-17 19:56:36 -05001877 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04001878 return;
dea31012005-04-17 16:05:31 -05001879
James Smart46fa3112007-04-25 09:51:45 -04001880 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001881
1882 lpfc_linkdown(phba);
1883
James Smart87af33f2007-10-27 13:37:43 -04001884 /* Issue an unreg_login to all nodes on all vports */
1885 vports = lpfc_create_vport_work_array(phba);
1886 if (vports != NULL) {
James Smart09372822008-01-11 01:52:54 -05001887 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
James Smart87af33f2007-10-27 13:37:43 -04001888 struct Scsi_Host *shost;
1889
James Smarta8adb832007-10-27 13:37:53 -04001890 if (vports[i]->load_flag & FC_UNLOADING)
1891 continue;
James Smart87af33f2007-10-27 13:37:43 -04001892 shost = lpfc_shost_from_vport(vports[i]);
1893 list_for_each_entry_safe(ndlp, next_ndlp,
1894 &vports[i]->fc_nodes,
1895 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05001896 if (!NLP_CHK_NODE_ACT(ndlp))
1897 continue;
James Smart87af33f2007-10-27 13:37:43 -04001898 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
1899 continue;
1900 if (ndlp->nlp_type & NLP_FABRIC) {
1901 lpfc_disc_state_machine(vports[i], ndlp,
1902 NULL, NLP_EVT_DEVICE_RECOVERY);
1903 lpfc_disc_state_machine(vports[i], ndlp,
1904 NULL, NLP_EVT_DEVICE_RM);
1905 }
1906 spin_lock_irq(shost->host_lock);
1907 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
1908 spin_unlock_irq(shost->host_lock);
1909 lpfc_unreg_rpi(vports[i], ndlp);
1910 }
1911 }
1912 }
James Smart09372822008-01-11 01:52:54 -05001913 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05001914
James Smart46fa3112007-04-25 09:51:45 -04001915 lpfc_sli_flush_mbox_queue(phba);
1916}
1917
James Smarte59058c2008-08-24 21:49:00 -04001918/**
James Smart3621a712009-04-06 18:47:14 -04001919 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04001920 * @phba: pointer to lpfc hba data structure.
1921 *
1922 * This routine actually brings a HBA offline. It stops all the timers
1923 * associated with the HBA, brings down the SLI layer, and eventually
1924 * marks the HBA as in offline state for the upper layer protocol.
1925 **/
James Smart46fa3112007-04-25 09:51:45 -04001926void
James Smart2e0fef82007-06-17 19:56:36 -05001927lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04001928{
James Smart549e55c2007-08-02 11:09:51 -04001929 struct Scsi_Host *shost;
1930 struct lpfc_vport **vports;
1931 int i;
James Smart46fa3112007-04-25 09:51:45 -04001932
James Smart549e55c2007-08-02 11:09:51 -04001933 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04001934 return;
James Smart688a8862006-07-06 15:49:56 -04001935
dea31012005-04-17 16:05:31 -05001936 /* stop all timers associated with this hba */
James Smart92d7f7b2007-06-17 19:56:38 -05001937 lpfc_stop_phba_timers(phba);
James Smart51ef4c22007-08-02 11:10:31 -04001938 vports = lpfc_create_vport_work_array(phba);
1939 if (vports != NULL)
James Smart09372822008-01-11 01:52:54 -05001940 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04001941 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05001942 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05001943 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001944 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05001945 /* Bring down the SLI Layer and cleanup. The HBA is offline
1946 now. */
1947 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001948 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04001949 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001950 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04001951 vports = lpfc_create_vport_work_array(phba);
1952 if (vports != NULL)
James Smart09372822008-01-11 01:52:54 -05001953 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04001954 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04001955 spin_lock_irq(shost->host_lock);
1956 vports[i]->work_port_events = 0;
1957 vports[i]->fc_flag |= FC_OFFLINE_MODE;
1958 spin_unlock_irq(shost->host_lock);
1959 }
James Smart09372822008-01-11 01:52:54 -05001960 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05001961}
1962
James Smarte59058c2008-08-24 21:49:00 -04001963/**
James Smart3621a712009-04-06 18:47:14 -04001964 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04001965 * @phba: pointer to lpfc hba data structure.
1966 *
1967 * This routine is to free all the SCSI buffers and IOCBs from the driver
1968 * list back to kernel. It is called from lpfc_pci_remove_one to free
1969 * the internal resources before the device is removed from the system.
1970 *
1971 * Return codes
1972 * 0 - successful (for now, it always returns 0)
1973 **/
dea31012005-04-17 16:05:31 -05001974static int
James Smart2e0fef82007-06-17 19:56:36 -05001975lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001976{
1977 struct lpfc_scsi_buf *sb, *sb_next;
1978 struct lpfc_iocbq *io, *io_next;
1979
James Smart2e0fef82007-06-17 19:56:36 -05001980 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001981 /* Release all the lpfc_scsi_bufs maintained by this host. */
1982 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
1983 list_del(&sb->list);
1984 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05001985 sb->dma_handle);
dea31012005-04-17 16:05:31 -05001986 kfree(sb);
1987 phba->total_scsi_bufs--;
1988 }
1989
1990 /* Release all the lpfc_iocbq entries maintained by this host. */
1991 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
1992 list_del(&io->list);
1993 kfree(io);
1994 phba->total_iocbq_bufs--;
1995 }
1996
James Smart2e0fef82007-06-17 19:56:36 -05001997 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001998
1999 return 0;
2000}
2001
James Smarte59058c2008-08-24 21:49:00 -04002002/**
James Smart3621a712009-04-06 18:47:14 -04002003 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04002004 * @phba: pointer to lpfc hba data structure.
2005 * @instance: a unique integer ID to this FC port.
2006 * @dev: pointer to the device data structure.
2007 *
2008 * This routine creates a FC port for the upper layer protocol. The FC port
2009 * can be created on top of either a physical port or a virtual port provided
2010 * by the HBA. This routine also allocates a SCSI host data structure (shost)
2011 * and associates the FC port created before adding the shost into the SCSI
2012 * layer.
2013 *
2014 * Return codes
2015 * @vport - pointer to the virtual N_Port data structure.
2016 * NULL - port create failed.
2017 **/
James Smart2e0fef82007-06-17 19:56:36 -05002018struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04002019lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04002020{
James Smart2e0fef82007-06-17 19:56:36 -05002021 struct lpfc_vport *vport;
2022 struct Scsi_Host *shost;
2023 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04002024
James Smart3de2a652007-08-02 11:09:59 -04002025 if (dev != &phba->pcidev->dev)
2026 shost = scsi_host_alloc(&lpfc_vport_template,
2027 sizeof(struct lpfc_vport));
2028 else
2029 shost = scsi_host_alloc(&lpfc_template,
2030 sizeof(struct lpfc_vport));
James Smart2e0fef82007-06-17 19:56:36 -05002031 if (!shost)
2032 goto out;
James Smart47a86172007-04-25 09:53:22 -04002033
James Smart2e0fef82007-06-17 19:56:36 -05002034 vport = (struct lpfc_vport *) shost->hostdata;
2035 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05002036 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05002037 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05002038 vport->fc_rscn_flush = 0;
James Smart47a86172007-04-25 09:53:22 -04002039
James Smart3de2a652007-08-02 11:09:59 -04002040 lpfc_get_vport_cfgparam(vport);
James Smart2e0fef82007-06-17 19:56:36 -05002041 shost->unique_id = instance;
2042 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04002043 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05002044 shost->this_id = -1;
2045 shost->max_cmd_len = 16;
James Smart81301a92008-12-04 22:39:46 -05002046
James Smart47a86172007-04-25 09:53:22 -04002047 /*
James Smart2e0fef82007-06-17 19:56:36 -05002048 * Set initial can_queue value since 0 is no longer supported and
2049 * scsi_add_host will fail. This will be adjusted later based on the
2050 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04002051 */
James Smart2e0fef82007-06-17 19:56:36 -05002052 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04002053 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05002054 shost->transportt = lpfc_vport_transport_template;
2055 vport->port_type = LPFC_NPIV_PORT;
2056 } else {
2057 shost->transportt = lpfc_transport_template;
2058 vport->port_type = LPFC_PHYSICAL_PORT;
2059 }
James Smart47a86172007-04-25 09:53:22 -04002060
James Smart2e0fef82007-06-17 19:56:36 -05002061 /* Initialize all internally managed lists. */
2062 INIT_LIST_HEAD(&vport->fc_nodes);
2063 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04002064
James Smart2e0fef82007-06-17 19:56:36 -05002065 init_timer(&vport->fc_disctmo);
2066 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05002067 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002068
James Smart2e0fef82007-06-17 19:56:36 -05002069 init_timer(&vport->fc_fdmitmo);
2070 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
James Smart92d7f7b2007-06-17 19:56:38 -05002071 vport->fc_fdmitmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002072
James Smart2e0fef82007-06-17 19:56:36 -05002073 init_timer(&vport->els_tmofunc);
2074 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05002075 vport->els_tmofunc.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002076
James Smart3de2a652007-08-02 11:09:59 -04002077 error = scsi_add_host(shost, dev);
James Smart2e0fef82007-06-17 19:56:36 -05002078 if (error)
2079 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04002080
James Smart549e55c2007-08-02 11:09:51 -04002081 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002082 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04002083 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002084 return vport;
James Smart47a86172007-04-25 09:53:22 -04002085
James Smart2e0fef82007-06-17 19:56:36 -05002086out_put_shost:
2087 scsi_host_put(shost);
2088out:
2089 return NULL;
James Smart47a86172007-04-25 09:53:22 -04002090}
2091
James Smarte59058c2008-08-24 21:49:00 -04002092/**
James Smart3621a712009-04-06 18:47:14 -04002093 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04002094 * @vport: pointer to an lpfc virtual N_Port data structure.
2095 *
2096 * This routine destroys a FC port from the upper layer protocol. All the
2097 * resources associated with the port are released.
2098 **/
James Smart2e0fef82007-06-17 19:56:36 -05002099void
2100destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04002101{
James Smart92d7f7b2007-06-17 19:56:38 -05002102 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2103 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002104
James Smart858c9f62007-06-17 19:56:39 -05002105 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05002106 fc_remove_host(shost);
2107 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04002108
James Smart92d7f7b2007-06-17 19:56:38 -05002109 spin_lock_irq(&phba->hbalock);
2110 list_del_init(&vport->listentry);
2111 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04002112
James Smart92d7f7b2007-06-17 19:56:38 -05002113 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04002114 return;
James Smart47a86172007-04-25 09:53:22 -04002115}
2116
James Smarte59058c2008-08-24 21:49:00 -04002117/**
James Smart3621a712009-04-06 18:47:14 -04002118 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04002119 *
2120 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
2121 * uses the kernel idr facility to perform the task.
2122 *
2123 * Return codes:
2124 * instance - a unique integer ID allocated as the new instance.
2125 * -1 - lpfc get instance failed.
2126 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002127int
2128lpfc_get_instance(void)
2129{
2130 int instance = 0;
2131
2132 /* Assign an unused number */
2133 if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
2134 return -1;
2135 if (idr_get_new(&lpfc_hba_index, NULL, &instance))
2136 return -1;
2137 return instance;
2138}
2139
James Smarte59058c2008-08-24 21:49:00 -04002140/**
James Smart3621a712009-04-06 18:47:14 -04002141 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04002142 * @shost: pointer to SCSI host data structure.
2143 * @time: elapsed time of the scan in jiffies.
2144 *
2145 * This routine is called by the SCSI layer with a SCSI host to determine
2146 * whether the scan host is finished.
2147 *
2148 * Note: there is no scan_start function as adapter initialization will have
2149 * asynchronously kicked off the link initialization.
2150 *
2151 * Return codes
2152 * 0 - SCSI host scan is not over yet.
2153 * 1 - SCSI host scan is over.
2154 **/
James Smart47a86172007-04-25 09:53:22 -04002155int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2156{
James Smart2e0fef82007-06-17 19:56:36 -05002157 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2158 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05002159 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04002160
James Smart858c9f62007-06-17 19:56:39 -05002161 spin_lock_irq(shost->host_lock);
2162
James Smart51ef4c22007-08-02 11:10:31 -04002163 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05002164 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002165 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05002166 }
James Smart2e0fef82007-06-17 19:56:36 -05002167 if (time >= 30 * HZ) {
2168 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002169 "0461 Scanning longer than 30 "
2170 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05002171 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002172 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002173 }
2174 if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
2175 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002176 "0465 Link down longer than 15 "
2177 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05002178 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05002179 goto finished;
James Smart47a86172007-04-25 09:53:22 -04002180 }
2181
James Smart2e0fef82007-06-17 19:56:36 -05002182 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05002183 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002184 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05002185 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002186 if (vport->fc_map_cnt == 0 && time < 2 * HZ)
James Smart858c9f62007-06-17 19:56:39 -05002187 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002188 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05002189 goto finished;
2190
2191 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002192
2193finished:
James Smart858c9f62007-06-17 19:56:39 -05002194 spin_unlock_irq(shost->host_lock);
2195 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05002196}
2197
James Smarte59058c2008-08-24 21:49:00 -04002198/**
James Smart3621a712009-04-06 18:47:14 -04002199 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04002200 * @shost: pointer to SCSI host data structure.
2201 *
2202 * This routine initializes a given SCSI host attributes on a FC port. The
2203 * SCSI host can be either on top of a physical port or a virtual port.
2204 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002205void lpfc_host_attrib_init(struct Scsi_Host *shost)
2206{
2207 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2208 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002209 /*
James Smart2e0fef82007-06-17 19:56:36 -05002210 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04002211 */
2212
James Smart2e0fef82007-06-17 19:56:36 -05002213 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
2214 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04002215 fc_host_supported_classes(shost) = FC_COS_CLASS3;
2216
2217 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05002218 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04002219 fc_host_supported_fc4s(shost)[2] = 1;
2220 fc_host_supported_fc4s(shost)[7] = 1;
2221
James Smart92d7f7b2007-06-17 19:56:38 -05002222 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
2223 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04002224
2225 fc_host_supported_speeds(shost) = 0;
2226 if (phba->lmt & LMT_10Gb)
2227 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04002228 if (phba->lmt & LMT_8Gb)
2229 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04002230 if (phba->lmt & LMT_4Gb)
2231 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
2232 if (phba->lmt & LMT_2Gb)
2233 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
2234 if (phba->lmt & LMT_1Gb)
2235 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
2236
2237 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05002238 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
2239 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04002240
2241 /* This value is also unchanging */
2242 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05002243 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04002244 fc_host_active_fc4s(shost)[2] = 1;
2245 fc_host_active_fc4s(shost)[7] = 1;
2246
James Smart92d7f7b2007-06-17 19:56:38 -05002247 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04002248 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04002249 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04002250 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04002251}
dea31012005-04-17 16:05:31 -05002252
James Smarte59058c2008-08-24 21:49:00 -04002253/**
James Smart3621a712009-04-06 18:47:14 -04002254 * lpfc_enable_msix - Enable MSI-X interrupt mode
James Smarte59058c2008-08-24 21:49:00 -04002255 * @phba: pointer to lpfc hba data structure.
2256 *
2257 * This routine is invoked to enable the MSI-X interrupt vectors. The kernel
2258 * function pci_enable_msix() is called to enable the MSI-X vectors. Note that
2259 * pci_enable_msix(), once invoked, enables either all or nothing, depending
2260 * on the current availability of PCI vector resources. The device driver is
2261 * responsible for calling the individual request_irq() to register each MSI-X
2262 * vector with a interrupt handler, which is done in this function. Note that
2263 * later when device is unloading, the driver should always call free_irq()
2264 * on all MSI-X vectors it has done request_irq() on before calling
2265 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
2266 * will be left with MSI-X enabled and leaks its vectors.
2267 *
2268 * Return codes
2269 * 0 - sucessful
2270 * other values - error
2271 **/
James Smartdb2378e2008-02-08 18:49:51 -05002272static int
2273lpfc_enable_msix(struct lpfc_hba *phba)
2274{
James Smart93996272008-08-24 21:50:30 -04002275 int rc, i;
2276 LPFC_MBOXQ_t *pmb;
James Smartdb2378e2008-02-08 18:49:51 -05002277
James Smart93996272008-08-24 21:50:30 -04002278 /* Set up MSI-X multi-message vectors */
2279 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
2280 phba->msix_entries[i].entry = i;
James Smartdb2378e2008-02-08 18:49:51 -05002281
James Smart93996272008-08-24 21:50:30 -04002282 /* Configure MSI-X capability structure */
2283 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
James Smartdb2378e2008-02-08 18:49:51 -05002284 ARRAY_SIZE(phba->msix_entries));
James Smart93996272008-08-24 21:50:30 -04002285 if (rc) {
James Smartdb2378e2008-02-08 18:49:51 -05002286 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -05002287 "0420 PCI enable MSI-X failed (%d)\n", rc);
James Smart93996272008-08-24 21:50:30 -04002288 goto msi_fail_out;
2289 } else
2290 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
2291 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2292 "0477 MSI-X entry[%d]: vector=x%x "
2293 "message=%d\n", i,
2294 phba->msix_entries[i].vector,
2295 phba->msix_entries[i].entry);
2296 /*
2297 * Assign MSI-X vectors to interrupt handlers
2298 */
2299
2300 /* vector-0 is associated to slow-path handler */
2301 rc = request_irq(phba->msix_entries[0].vector, &lpfc_sp_intr_handler,
2302 IRQF_SHARED, LPFC_SP_DRIVER_HANDLER_NAME, phba);
2303 if (rc) {
James Smart5b75da22008-12-04 22:39:35 -05002304 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smart93996272008-08-24 21:50:30 -04002305 "0421 MSI-X slow-path request_irq failed "
James Smart5b75da22008-12-04 22:39:35 -05002306 "(%d)\n", rc);
James Smart93996272008-08-24 21:50:30 -04002307 goto msi_fail_out;
James Smartdb2378e2008-02-08 18:49:51 -05002308 }
2309
James Smart93996272008-08-24 21:50:30 -04002310 /* vector-1 is associated to fast-path handler */
2311 rc = request_irq(phba->msix_entries[1].vector, &lpfc_fp_intr_handler,
2312 IRQF_SHARED, LPFC_FP_DRIVER_HANDLER_NAME, phba);
2313
2314 if (rc) {
James Smart5b75da22008-12-04 22:39:35 -05002315 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smart93996272008-08-24 21:50:30 -04002316 "0429 MSI-X fast-path request_irq failed "
James Smart5b75da22008-12-04 22:39:35 -05002317 "(%d)\n", rc);
James Smart93996272008-08-24 21:50:30 -04002318 goto irq_fail_out;
James Smartdb2378e2008-02-08 18:49:51 -05002319 }
James Smart93996272008-08-24 21:50:30 -04002320
2321 /*
2322 * Configure HBA MSI-X attention conditions to messages
2323 */
2324 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2325
2326 if (!pmb) {
2327 rc = -ENOMEM;
2328 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2329 "0474 Unable to allocate memory for issuing "
2330 "MBOX_CONFIG_MSI command\n");
2331 goto mem_fail_out;
2332 }
2333 rc = lpfc_config_msi(phba, pmb);
2334 if (rc)
2335 goto mbx_fail_out;
2336 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
2337 if (rc != MBX_SUCCESS) {
James Smart5b75da22008-12-04 22:39:35 -05002338 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
James Smart93996272008-08-24 21:50:30 -04002339 "0351 Config MSI mailbox command failed, "
2340 "mbxCmd x%x, mbxStatus x%x\n",
2341 pmb->mb.mbxCommand, pmb->mb.mbxStatus);
2342 goto mbx_fail_out;
2343 }
2344
2345 /* Free memory allocated for mailbox command */
2346 mempool_free(pmb, phba->mbox_mem_pool);
2347 return rc;
2348
2349mbx_fail_out:
2350 /* Free memory allocated for mailbox command */
2351 mempool_free(pmb, phba->mbox_mem_pool);
2352
2353mem_fail_out:
2354 /* free the irq already requested */
2355 free_irq(phba->msix_entries[1].vector, phba);
2356
2357irq_fail_out:
2358 /* free the irq already requested */
2359 free_irq(phba->msix_entries[0].vector, phba);
2360
2361msi_fail_out:
2362 /* Unconfigure MSI-X capability structure */
2363 pci_disable_msix(phba->pcidev);
2364 return rc;
James Smartdb2378e2008-02-08 18:49:51 -05002365}
2366
James Smarte59058c2008-08-24 21:49:00 -04002367/**
James Smart3621a712009-04-06 18:47:14 -04002368 * lpfc_disable_msix - Disable MSI-X interrupt mode
James Smarte59058c2008-08-24 21:49:00 -04002369 * @phba: pointer to lpfc hba data structure.
2370 *
2371 * This routine is invoked to release the MSI-X vectors and then disable the
2372 * MSI-X interrupt mode.
2373 **/
James Smartdb2378e2008-02-08 18:49:51 -05002374static void
2375lpfc_disable_msix(struct lpfc_hba *phba)
2376{
James Smart93996272008-08-24 21:50:30 -04002377 int i;
2378
2379 /* Free up MSI-X multi-message vectors */
2380 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
2381 free_irq(phba->msix_entries[i].vector, phba);
2382 /* Disable MSI-X */
James Smartdb2378e2008-02-08 18:49:51 -05002383 pci_disable_msix(phba->pcidev);
2384}
2385
James Smarte59058c2008-08-24 21:49:00 -04002386/**
James Smart3621a712009-04-06 18:47:14 -04002387 * lpfc_enable_msi - Enable MSI interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05002388 * @phba: pointer to lpfc hba data structure.
2389 *
2390 * This routine is invoked to enable the MSI interrupt mode. The kernel
2391 * function pci_enable_msi() is called to enable the MSI vector. The
2392 * device driver is responsible for calling the request_irq() to register
2393 * MSI vector with a interrupt the handler, which is done in this function.
2394 *
2395 * Return codes
2396 * 0 - sucessful
2397 * other values - error
2398 */
2399static int
2400lpfc_enable_msi(struct lpfc_hba *phba)
2401{
2402 int rc;
2403
2404 rc = pci_enable_msi(phba->pcidev);
2405 if (!rc)
2406 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2407 "0462 PCI enable MSI mode success.\n");
2408 else {
2409 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2410 "0471 PCI enable MSI mode failed (%d)\n", rc);
2411 return rc;
2412 }
2413
2414 rc = request_irq(phba->pcidev->irq, lpfc_intr_handler,
2415 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
2416 if (rc) {
2417 pci_disable_msi(phba->pcidev);
2418 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2419 "0478 MSI request_irq failed (%d)\n", rc);
2420 }
2421 return rc;
2422}
2423
2424/**
James Smart3621a712009-04-06 18:47:14 -04002425 * lpfc_disable_msi - Disable MSI interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05002426 * @phba: pointer to lpfc hba data structure.
2427 *
2428 * This routine is invoked to disable the MSI interrupt mode. The driver
2429 * calls free_irq() on MSI vector it has done request_irq() on before
2430 * calling pci_disable_msi(). Failure to do so results in a BUG_ON() and
2431 * a device will be left with MSI enabled and leaks its vector.
2432 */
2433
2434static void
2435lpfc_disable_msi(struct lpfc_hba *phba)
2436{
2437 free_irq(phba->pcidev->irq, phba);
2438 pci_disable_msi(phba->pcidev);
2439 return;
2440}
2441
2442/**
James Smart3621a712009-04-06 18:47:14 -04002443 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05002444 * @phba: pointer to lpfc hba data structure.
2445 * @intr_mode: active interrupt mode adopted.
2446 *
2447 * This routine it invoked to log the currently used active interrupt mode
2448 * to the device.
2449 */
2450static void
2451lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
2452{
2453 switch (intr_mode) {
2454 case 0:
2455 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2456 "0470 Enable INTx interrupt mode.\n");
2457 break;
2458 case 1:
2459 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2460 "0481 Enabled MSI interrupt mode.\n");
2461 break;
2462 case 2:
2463 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2464 "0480 Enabled MSI-X interrupt mode.\n");
2465 break;
2466 default:
2467 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2468 "0482 Illegal interrupt mode.\n");
2469 break;
2470 }
2471 return;
2472}
2473
2474static void
2475lpfc_stop_port(struct lpfc_hba *phba)
2476{
2477 /* Clear all interrupt enable conditions */
2478 writel(0, phba->HCregaddr);
2479 readl(phba->HCregaddr); /* flush */
2480 /* Clear all pending interrupts */
2481 writel(0xffffffff, phba->HAregaddr);
2482 readl(phba->HAregaddr); /* flush */
2483
2484 /* Reset some HBA SLI setup states */
2485 lpfc_stop_phba_timers(phba);
2486 phba->pport->work_port_events = 0;
2487
2488 return;
2489}
2490
2491/**
James Smart3621a712009-04-06 18:47:14 -04002492 * lpfc_enable_intr - Enable device interrupt
James Smart5b75da22008-12-04 22:39:35 -05002493 * @phba: pointer to lpfc hba data structure.
2494 *
2495 * This routine is invoked to enable device interrupt and associate driver's
2496 * interrupt handler(s) to interrupt vector(s). Depends on the interrupt
2497 * mode configured to the driver, the driver will try to fallback from the
2498 * configured interrupt mode to an interrupt mode which is supported by the
2499 * platform, kernel, and device in the order of: MSI-X -> MSI -> IRQ.
2500 *
2501 * Return codes
2502 * 0 - sucessful
2503 * other values - error
2504 **/
2505static uint32_t
2506lpfc_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
2507{
2508 uint32_t intr_mode = LPFC_INTR_ERROR;
2509 int retval;
2510
2511 if (cfg_mode == 2) {
2512 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
2513 retval = lpfc_sli_config_port(phba, 3);
2514 if (!retval) {
2515 /* Now, try to enable MSI-X interrupt mode */
2516 retval = lpfc_enable_msix(phba);
2517 if (!retval) {
2518 /* Indicate initialization to MSI-X mode */
2519 phba->intr_type = MSIX;
2520 intr_mode = 2;
2521 }
2522 }
2523 }
2524
2525 /* Fallback to MSI if MSI-X initialization failed */
2526 if (cfg_mode >= 1 && phba->intr_type == NONE) {
2527 retval = lpfc_enable_msi(phba);
2528 if (!retval) {
2529 /* Indicate initialization to MSI mode */
2530 phba->intr_type = MSI;
2531 intr_mode = 1;
2532 }
2533 }
2534
2535 /* Fallback to INTx if both MSI-X/MSI initalization failed */
2536 if (phba->intr_type == NONE) {
2537 retval = request_irq(phba->pcidev->irq, lpfc_intr_handler,
2538 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
2539 if (!retval) {
2540 /* Indicate initialization to INTx mode */
2541 phba->intr_type = INTx;
2542 intr_mode = 0;
2543 }
2544 }
2545 return intr_mode;
2546}
2547
2548/**
James Smart3621a712009-04-06 18:47:14 -04002549 * lpfc_disable_intr - Disable device interrupt
James Smart5b75da22008-12-04 22:39:35 -05002550 * @phba: pointer to lpfc hba data structure.
2551 *
2552 * This routine is invoked to disable device interrupt and disassociate the
2553 * driver's interrupt handler(s) from interrupt vector(s). Depending on the
2554 * interrupt mode, the driver will release the interrupt vector(s) for the
2555 * message signaled interrupt.
2556 **/
2557static void
2558lpfc_disable_intr(struct lpfc_hba *phba)
2559{
2560 /* Disable the currently initialized interrupt mode */
2561 if (phba->intr_type == MSIX)
2562 lpfc_disable_msix(phba);
2563 else if (phba->intr_type == MSI)
2564 lpfc_disable_msi(phba);
2565 else if (phba->intr_type == INTx)
2566 free_irq(phba->pcidev->irq, phba);
2567
2568 /* Reset interrupt management states */
2569 phba->intr_type = NONE;
2570 phba->sli.slistat.sli_intr = 0;
2571
2572 return;
2573}
2574
2575/**
James Smart3621a712009-04-06 18:47:14 -04002576 * lpfc_pci_probe_one - lpfc PCI probe func to register device to PCI subsystem
James Smarte59058c2008-08-24 21:49:00 -04002577 * @pdev: pointer to PCI device
2578 * @pid: pointer to PCI device identifier
2579 *
2580 * This routine is to be registered to the kernel's PCI subsystem. When an
2581 * Emulex HBA is presented in PCI bus, the kernel PCI subsystem looks at
2582 * PCI device-specific information of the device and driver to see if the
2583 * driver state that it can support this kind of device. If the match is
2584 * successful, the driver core invokes this routine. If this routine
2585 * determines it can claim the HBA, it does all the initialization that it
2586 * needs to do to handle the HBA properly.
2587 *
2588 * Return code
2589 * 0 - driver can claim the device
2590 * negative value - driver can not claim the device
2591 **/
dea31012005-04-17 16:05:31 -05002592static int __devinit
2593lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
2594{
James Smart2e0fef82007-06-17 19:56:36 -05002595 struct lpfc_vport *vport = NULL;
2596 struct lpfc_hba *phba;
2597 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05002598 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05002599 struct Scsi_Host *shost = NULL;
James Smart51ef4c22007-08-02 11:10:31 -04002600 void *ptr;
dea31012005-04-17 16:05:31 -05002601 unsigned long bar0map_len, bar2map_len;
James Smart98c9ea52007-10-27 13:37:33 -04002602 int error = -ENODEV, retval;
James Smart51ef4c22007-08-02 11:10:31 -04002603 int i, hbq_count;
James Bottomley604a3e32005-10-29 10:28:33 -05002604 uint16_t iotag;
James Smart5b75da22008-12-04 22:39:35 -05002605 uint32_t cfg_mode, intr_mode;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05002606 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
James Smartea2151b2008-09-07 11:52:10 -04002607 struct lpfc_adapter_event_header adapter_event;
dea31012005-04-17 16:05:31 -05002608
Greg Kroah-Hartmand5f78fb2008-02-02 12:13:22 +01002609 if (pci_enable_device_mem(pdev))
dea31012005-04-17 16:05:31 -05002610 goto out;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05002611 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
dea31012005-04-17 16:05:31 -05002612 goto out_disable_device;
2613
James Smart2e0fef82007-06-17 19:56:36 -05002614 phba = kzalloc(sizeof (struct lpfc_hba), GFP_KERNEL);
2615 if (!phba)
dea31012005-04-17 16:05:31 -05002616 goto out_release_regions;
2617
James Smartea2151b2008-09-07 11:52:10 -04002618 atomic_set(&phba->fast_event_count, 0);
James Smart2e0fef82007-06-17 19:56:36 -05002619 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002620
James Smarte47c9092008-02-08 18:49:26 -05002621 /* Initialize ndlp management spinlock */
2622 spin_lock_init(&phba->ndlp_lock);
2623
dea31012005-04-17 16:05:31 -05002624 phba->pcidev = pdev;
2625
2626 /* Assign an unused board number */
James Smart92d7f7b2007-06-17 19:56:38 -05002627 if ((phba->brd_no = lpfc_get_instance()) < 0)
James Smart2e0fef82007-06-17 19:56:36 -05002628 goto out_free_phba;
dea31012005-04-17 16:05:31 -05002629
James Smart2e0fef82007-06-17 19:56:36 -05002630 INIT_LIST_HEAD(&phba->port_list);
James Smart84774a42008-08-24 21:50:06 -04002631 init_waitqueue_head(&phba->wait_4_mlo_m_q);
James Smart2e0fef82007-06-17 19:56:36 -05002632 /*
2633 * Get all the module params for configuring this host and then
2634 * establish the host.
2635 */
2636 lpfc_get_cfgparam(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002637 phba->max_vpi = LPFC_MAX_VPI;
dea31012005-04-17 16:05:31 -05002638
2639 /* Initialize timers used by driver */
James Smart858c9f62007-06-17 19:56:39 -05002640 init_timer(&phba->hb_tmofunc);
2641 phba->hb_tmofunc.function = lpfc_hb_timeout;
2642 phba->hb_tmofunc.data = (unsigned long)phba;
2643
dea31012005-04-17 16:05:31 -05002644 psli = &phba->sli;
2645 init_timer(&psli->mbox_tmo);
2646 psli->mbox_tmo.function = lpfc_mbox_timeout;
James Smart2e0fef82007-06-17 19:56:36 -05002647 psli->mbox_tmo.data = (unsigned long) phba;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002648 init_timer(&phba->fcp_poll_timer);
2649 phba->fcp_poll_timer.function = lpfc_poll_timeout;
James Smart2e0fef82007-06-17 19:56:36 -05002650 phba->fcp_poll_timer.data = (unsigned long) phba;
James Smart92d7f7b2007-06-17 19:56:38 -05002651 init_timer(&phba->fabric_block_timer);
2652 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
2653 phba->fabric_block_timer.data = (unsigned long) phba;
James Smart93996272008-08-24 21:50:30 -04002654 init_timer(&phba->eratt_poll);
2655 phba->eratt_poll.function = lpfc_poll_eratt;
2656 phba->eratt_poll.data = (unsigned long) phba;
dea31012005-04-17 16:05:31 -05002657
dea31012005-04-17 16:05:31 -05002658 pci_set_master(pdev);
James Smart97207482008-12-04 22:39:19 -05002659 pci_save_state(pdev);
Randy Dunlap694625c2007-07-09 11:55:54 -07002660 pci_try_set_mwi(pdev);
dea31012005-04-17 16:05:31 -05002661
Yang Hongyang6a355282009-04-06 19:01:13 -07002662 if (pci_set_dma_mask(phba->pcidev, DMA_BIT_MASK(64)) != 0)
Yang Hongyang284901a2009-04-06 19:01:15 -07002663 if (pci_set_dma_mask(phba->pcidev, DMA_BIT_MASK(32)) != 0)
dea31012005-04-17 16:05:31 -05002664 goto out_idr_remove;
2665
2666 /*
2667 * Get the bus address of Bar0 and Bar2 and the number of bytes
2668 * required by each mapping.
2669 */
2670 phba->pci_bar0_map = pci_resource_start(phba->pcidev, 0);
2671 bar0map_len = pci_resource_len(phba->pcidev, 0);
2672
2673 phba->pci_bar2_map = pci_resource_start(phba->pcidev, 2);
2674 bar2map_len = pci_resource_len(phba->pcidev, 2);
2675
Jamie Wellnitz901a9202006-02-28 19:25:19 -05002676 /* Map HBA SLIM to a kernel virtual address. */
James Smart93996272008-08-24 21:50:30 -04002677 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05002678 if (!phba->slim_memmap_p) {
2679 error = -ENODEV;
2680 dev_printk(KERN_ERR, &pdev->dev,
2681 "ioremap failed for SLIM memory.\n");
2682 goto out_idr_remove;
2683 }
2684
2685 /* Map HBA Control Registers to a kernel virtual address. */
dea31012005-04-17 16:05:31 -05002686 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05002687 if (!phba->ctrl_regs_memmap_p) {
2688 error = -ENODEV;
2689 dev_printk(KERN_ERR, &pdev->dev,
2690 "ioremap failed for HBA control registers.\n");
2691 goto out_iounmap_slim;
2692 }
dea31012005-04-17 16:05:31 -05002693
2694 /* Allocate memory for SLI-2 structures */
James Smart34b02dc2008-08-24 21:49:55 -04002695 phba->slim2p.virt = dma_alloc_coherent(&phba->pcidev->dev,
2696 SLI2_SLIM_SIZE,
2697 &phba->slim2p.phys,
2698 GFP_KERNEL);
2699 if (!phba->slim2p.virt)
dea31012005-04-17 16:05:31 -05002700 goto out_iounmap;
2701
James Smart34b02dc2008-08-24 21:49:55 -04002702 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
James Smartd7c255b2008-08-24 21:50:00 -04002703 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
2704 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
2705 phba->IOCBs = (phba->slim2p.virt +
2706 offsetof(struct lpfc_sli2_slim, IOCBs));
dea31012005-04-17 16:05:31 -05002707
James Smarted957682007-06-17 19:56:37 -05002708 phba->hbqslimp.virt = dma_alloc_coherent(&phba->pcidev->dev,
2709 lpfc_sli_hbq_size(),
2710 &phba->hbqslimp.phys,
2711 GFP_KERNEL);
2712 if (!phba->hbqslimp.virt)
2713 goto out_free_slim;
2714
James Smart51ef4c22007-08-02 11:10:31 -04002715 hbq_count = lpfc_sli_hbq_count();
2716 ptr = phba->hbqslimp.virt;
2717 for (i = 0; i < hbq_count; ++i) {
2718 phba->hbqs[i].hbq_virt = ptr;
2719 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
2720 ptr += (lpfc_hbq_defs[i]->entry_count *
2721 sizeof(struct lpfc_hbq_entry));
2722 }
2723 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
2724 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
2725
James Smarted957682007-06-17 19:56:37 -05002726 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
2727
James Smart3163f722008-02-08 18:50:25 -05002728 INIT_LIST_HEAD(&phba->hbqbuf_in_list);
2729
dea31012005-04-17 16:05:31 -05002730 /* Initialize the SLI Layer to run with lpfc HBAs. */
2731 lpfc_sli_setup(phba);
2732 lpfc_sli_queue_setup(phba);
2733
James Smart98c9ea52007-10-27 13:37:33 -04002734 retval = lpfc_mem_alloc(phba);
2735 if (retval) {
2736 error = retval;
James Smarted957682007-06-17 19:56:37 -05002737 goto out_free_hbqslimp;
James Smart98c9ea52007-10-27 13:37:33 -04002738 }
dea31012005-04-17 16:05:31 -05002739
2740 /* Initialize and populate the iocb list per host. */
2741 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
2742 for (i = 0; i < LPFC_IOCB_LIST_CNT; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002743 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002744 if (iocbq_entry == NULL) {
2745 printk(KERN_ERR "%s: only allocated %d iocbs of "
2746 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002747 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05002748 error = -ENOMEM;
2749 goto out_free_iocbq;
2750 }
2751
James Bottomley604a3e32005-10-29 10:28:33 -05002752 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
2753 if (iotag == 0) {
2754 kfree (iocbq_entry);
2755 printk(KERN_ERR "%s: failed to allocate IOTAG. "
2756 "Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002757 __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05002758 error = -ENOMEM;
2759 goto out_free_iocbq;
2760 }
James Smart2e0fef82007-06-17 19:56:36 -05002761
2762 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002763 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
2764 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05002765 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002766 }
2767
2768 /* Initialize HBA structure */
2769 phba->fc_edtov = FF_DEF_EDTOV;
2770 phba->fc_ratov = FF_DEF_RATOV;
2771 phba->fc_altov = FF_DEF_ALTOV;
2772 phba->fc_arbtov = FF_DEF_ARBTOV;
2773
2774 INIT_LIST_HEAD(&phba->work_list);
James Smart93996272008-08-24 21:50:30 -04002775 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
dea31012005-04-17 16:05:31 -05002776 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
2777
James Smart5e9d9b82008-06-14 22:52:53 -04002778 /* Initialize the wait queue head for the kernel thread */
2779 init_waitqueue_head(&phba->work_waitq);
2780
dea31012005-04-17 16:05:31 -05002781 /* Startup the kernel thread for this host adapter. */
2782 phba->worker_thread = kthread_run(lpfc_do_work, phba,
2783 "lpfc_worker_%d", phba->brd_no);
2784 if (IS_ERR(phba->worker_thread)) {
2785 error = PTR_ERR(phba->worker_thread);
2786 goto out_free_iocbq;
2787 }
2788
dea31012005-04-17 16:05:31 -05002789 /* Initialize the list of scsi buffers used by driver for scsi IO. */
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002790 spin_lock_init(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05002791 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
2792
James Smart92d7f7b2007-06-17 19:56:38 -05002793 /* Initialize list of fabric iocbs */
2794 INIT_LIST_HEAD(&phba->fabric_iocb_list);
2795
James Smart0ff10d42008-01-11 01:52:36 -05002796 /* Initialize list to save ELS buffers */
2797 INIT_LIST_HEAD(&phba->elsbuf);
2798
James Smart3de2a652007-08-02 11:09:59 -04002799 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05002800 if (!vport)
2801 goto out_kthread_stop;
2802
2803 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05002804 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05002805 lpfc_debugfs_initialize(vport);
James Smart2e0fef82007-06-17 19:56:36 -05002806
James Smart92d7f7b2007-06-17 19:56:38 -05002807 pci_set_drvdata(pdev, shost);
dea31012005-04-17 16:05:31 -05002808
James Smart93996272008-08-24 21:50:30 -04002809 phba->MBslimaddr = phba->slim_memmap_p;
2810 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
2811 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
2812 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
2813 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
2814
James Smart5b75da22008-12-04 22:39:35 -05002815 /* Configure sysfs attributes */
James Smart98c9ea52007-10-27 13:37:33 -04002816 if (lpfc_alloc_sysfs_attr(vport)) {
James Smart93996272008-08-24 21:50:30 -04002817 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2818 "1476 Failed to allocate sysfs attr\n");
James Smart98c9ea52007-10-27 13:37:33 -04002819 error = -ENOMEM;
James Smart5b75da22008-12-04 22:39:35 -05002820 goto out_destroy_port;
James Smart98c9ea52007-10-27 13:37:33 -04002821 }
dea31012005-04-17 16:05:31 -05002822
James Smart5b75da22008-12-04 22:39:35 -05002823 cfg_mode = phba->cfg_use_msi;
2824 while (true) {
2825 /* Configure and enable interrupt */
2826 intr_mode = lpfc_enable_intr(phba, cfg_mode);
2827 if (intr_mode == LPFC_INTR_ERROR) {
2828 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2829 "0426 Failed to enable interrupt.\n");
2830 goto out_free_sysfs_attr;
2831 }
2832 /* HBA SLI setup */
2833 if (lpfc_sli_hba_setup(phba)) {
2834 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2835 "1477 Failed to set up hba\n");
2836 error = -ENODEV;
2837 goto out_remove_device;
2838 }
2839
2840 /* Wait 50ms for the interrupts of previous mailbox commands */
2841 msleep(50);
2842 /* Check active interrupts received */
2843 if (phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
2844 /* Log the current active interrupt mode */
2845 phba->intr_mode = intr_mode;
2846 lpfc_log_intr_mode(phba, intr_mode);
2847 break;
2848 } else {
2849 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2850 "0451 Configure interrupt mode (%d) "
2851 "failed active interrupt test.\n",
2852 intr_mode);
2853 if (intr_mode == 0) {
2854 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2855 "0479 Failed to enable "
2856 "interrupt.\n");
2857 error = -ENODEV;
2858 goto out_remove_device;
2859 }
2860 /* Stop HBA SLI setups */
2861 lpfc_stop_port(phba);
2862 /* Disable the current interrupt mode */
2863 lpfc_disable_intr(phba);
2864 /* Try next level of interrupt mode */
2865 cfg_mode = --intr_mode;
2866 }
James Smart98c9ea52007-10-27 13:37:33 -04002867 }
James Smart858c9f62007-06-17 19:56:39 -05002868
2869 /*
2870 * hba setup may have changed the hba_queue_depth so we need to adjust
2871 * the value of can_queue.
2872 */
2873 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart81301a92008-12-04 22:39:46 -05002874 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
2875
2876 if (lpfc_prot_mask && lpfc_prot_guard) {
2877 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2878 "1478 Registering BlockGuard with the "
2879 "SCSI layer\n");
2880
2881 scsi_host_set_prot(shost, lpfc_prot_mask);
2882 scsi_host_set_guard(shost, lpfc_prot_guard);
2883 }
2884 }
2885
2886 if (!_dump_buf_data) {
2887 int pagecnt = 10;
2888 while (pagecnt) {
2889 spin_lock_init(&_dump_buf_lock);
2890 _dump_buf_data =
2891 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
2892 if (_dump_buf_data) {
2893 printk(KERN_ERR "BLKGRD allocated %d pages for "
2894 "_dump_buf_data at 0x%p\n",
2895 (1 << pagecnt), _dump_buf_data);
2896 _dump_buf_data_order = pagecnt;
2897 memset(_dump_buf_data, 0, ((1 << PAGE_SHIFT)
2898 << pagecnt));
2899 break;
2900 } else {
2901 --pagecnt;
2902 }
2903
2904 }
2905
2906 if (!_dump_buf_data_order)
2907 printk(KERN_ERR "BLKGRD ERROR unable to allocate "
2908 "memory for hexdump\n");
2909
2910 } else {
2911 printk(KERN_ERR "BLKGRD already allocated _dump_buf_data=0x%p"
2912 "\n", _dump_buf_data);
2913 }
2914
2915
2916 if (!_dump_buf_dif) {
2917 int pagecnt = 10;
2918 while (pagecnt) {
2919 _dump_buf_dif =
2920 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
2921 if (_dump_buf_dif) {
2922 printk(KERN_ERR "BLKGRD allocated %d pages for "
2923 "_dump_buf_dif at 0x%p\n",
2924 (1 << pagecnt), _dump_buf_dif);
2925 _dump_buf_dif_order = pagecnt;
2926 memset(_dump_buf_dif, 0, ((1 << PAGE_SHIFT)
2927 << pagecnt));
2928 break;
2929 } else {
2930 --pagecnt;
2931 }
2932
2933 }
2934
2935 if (!_dump_buf_dif_order)
2936 printk(KERN_ERR "BLKGRD ERROR unable to allocate "
2937 "memory for hexdump\n");
2938
2939 } else {
2940 printk(KERN_ERR "BLKGRD already allocated _dump_buf_dif=0x%p\n",
2941 _dump_buf_dif);
2942 }
James Smart858c9f62007-06-17 19:56:39 -05002943
2944 lpfc_host_attrib_init(shost);
2945
James Smart2e0fef82007-06-17 19:56:36 -05002946 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
2947 spin_lock_irq(shost->host_lock);
2948 lpfc_poll_start_timer(phba);
2949 spin_unlock_irq(shost->host_lock);
2950 }
James Smart8f6d98d2006-08-01 07:34:00 -04002951
James Smart93996272008-08-24 21:50:30 -04002952 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2953 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04002954 /* Send board arrival event to upper layer */
2955 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
2956 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
2957 fc_host_post_vendor_event(shost, fc_get_event_number(),
2958 sizeof(adapter_event),
2959 (char *) &adapter_event,
James Smartddcc50f2008-12-04 22:38:46 -05002960 LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04002961
dea31012005-04-17 16:05:31 -05002962 return 0;
2963
James Smart858c9f62007-06-17 19:56:39 -05002964out_remove_device:
James Smart858c9f62007-06-17 19:56:39 -05002965 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04002966 vport->load_flag |= FC_UNLOADING;
James Smart858c9f62007-06-17 19:56:39 -05002967 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05002968 lpfc_stop_phba_timers(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002969 phba->pport->work_port_events = 0;
James Smart5b75da22008-12-04 22:39:35 -05002970 lpfc_disable_intr(phba);
James Smart9f1e1b52008-12-04 22:39:40 -05002971 lpfc_sli_hba_down(phba);
2972 lpfc_sli_brdrestart(phba);
James Smart5b75da22008-12-04 22:39:35 -05002973out_free_sysfs_attr:
2974 lpfc_free_sysfs_attr(vport);
2975out_destroy_port:
James Smart2e0fef82007-06-17 19:56:36 -05002976 destroy_port(vport);
dea31012005-04-17 16:05:31 -05002977out_kthread_stop:
2978 kthread_stop(phba->worker_thread);
2979out_free_iocbq:
2980 list_for_each_entry_safe(iocbq_entry, iocbq_next,
2981 &phba->lpfc_iocb_list, list) {
dea31012005-04-17 16:05:31 -05002982 kfree(iocbq_entry);
2983 phba->total_iocbq_bufs--;
dea31012005-04-17 16:05:31 -05002984 }
2985 lpfc_mem_free(phba);
James Smarted957682007-06-17 19:56:37 -05002986out_free_hbqslimp:
James Smart34b02dc2008-08-24 21:49:55 -04002987 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
2988 phba->hbqslimp.virt, phba->hbqslimp.phys);
dea31012005-04-17 16:05:31 -05002989out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04002990 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
2991 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05002992out_iounmap:
2993 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05002994out_iounmap_slim:
dea31012005-04-17 16:05:31 -05002995 iounmap(phba->slim_memmap_p);
2996out_idr_remove:
2997 idr_remove(&lpfc_hba_index, phba->brd_no);
James Smart2e0fef82007-06-17 19:56:36 -05002998out_free_phba:
2999 kfree(phba);
dea31012005-04-17 16:05:31 -05003000out_release_regions:
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05003001 pci_release_selected_regions(pdev, bars);
dea31012005-04-17 16:05:31 -05003002out_disable_device:
3003 pci_disable_device(pdev);
3004out:
James Smartdefbcf12006-04-16 22:26:50 -04003005 pci_set_drvdata(pdev, NULL);
James Smart858c9f62007-06-17 19:56:39 -05003006 if (shost)
3007 scsi_host_put(shost);
dea31012005-04-17 16:05:31 -05003008 return error;
3009}
3010
James Smarte59058c2008-08-24 21:49:00 -04003011/**
James Smart3621a712009-04-06 18:47:14 -04003012 * lpfc_pci_remove_one - lpfc PCI func to unregister device from PCI subsystem
James Smarte59058c2008-08-24 21:49:00 -04003013 * @pdev: pointer to PCI device
3014 *
3015 * This routine is to be registered to the kernel's PCI subsystem. When an
James Smart3a55b532008-12-04 22:38:54 -05003016 * Emulex HBA is removed from PCI bus, it performs all the necessary cleanup
James Smarte59058c2008-08-24 21:49:00 -04003017 * for the HBA device to be removed from the PCI subsystem properly.
3018 **/
dea31012005-04-17 16:05:31 -05003019static void __devexit
3020lpfc_pci_remove_one(struct pci_dev *pdev)
3021{
James Smart2e0fef82007-06-17 19:56:36 -05003022 struct Scsi_Host *shost = pci_get_drvdata(pdev);
3023 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -05003024 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -05003025 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -05003026 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05003027 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
3028
James Smart549e55c2007-08-02 11:09:51 -04003029 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04003030 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04003031 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003032
James Smart858c9f62007-06-17 19:56:39 -05003033 lpfc_free_sysfs_attr(vport);
3034
James Smart09372822008-01-11 01:52:54 -05003035 kthread_stop(phba->worker_thread);
3036
James Smarteada2722008-12-04 22:39:13 -05003037 /* Release all the vports against this physical port */
3038 vports = lpfc_create_vport_work_array(phba);
3039 if (vports != NULL)
3040 for (i = 1; i <= phba->max_vpi && vports[i] != NULL; i++)
3041 fc_vport_terminate(vports[i]->fc_vport);
3042 lpfc_destroy_vport_work_array(phba, vports);
3043
3044 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -05003045 fc_remove_host(shost);
3046 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04003047 lpfc_cleanup(vport);
3048
James Smart2e0fef82007-06-17 19:56:36 -05003049 /*
3050 * Bring down the SLI Layer. This step disable all interrupts,
3051 * clears the rings, discards all mailbox commands, and resets
3052 * the HBA.
3053 */
3054 lpfc_sli_hba_down(phba);
3055 lpfc_sli_brdrestart(phba);
3056
James Smart92d7f7b2007-06-17 19:56:38 -05003057 lpfc_stop_phba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05003058 spin_lock_irq(&phba->hbalock);
3059 list_del_init(&vport->listentry);
3060 spin_unlock_irq(&phba->hbalock);
3061
James Smart858c9f62007-06-17 19:56:39 -05003062 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05003063
James Smart5b75da22008-12-04 22:39:35 -05003064 /* Disable interrupt */
3065 lpfc_disable_intr(phba);
dea31012005-04-17 16:05:31 -05003066
3067 pci_set_drvdata(pdev, NULL);
James Smart858c9f62007-06-17 19:56:39 -05003068 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05003069
3070 /*
3071 * Call scsi_free before mem_free since scsi bufs are released to their
3072 * corresponding pools here.
3073 */
3074 lpfc_scsi_free(phba);
3075 lpfc_mem_free(phba);
3076
James Smart34b02dc2008-08-24 21:49:55 -04003077 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
3078 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -05003079
James Smart2e0fef82007-06-17 19:56:36 -05003080 /* Free resources associated with SLI2 interface */
3081 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -04003082 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -05003083
3084 /* unmap adapter SLIM and Control Registers */
3085 iounmap(phba->ctrl_regs_memmap_p);
3086 iounmap(phba->slim_memmap_p);
3087
3088 idr_remove(&lpfc_hba_index, phba->brd_no);
3089
3090 kfree(phba);
3091
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05003092 pci_release_selected_regions(pdev, bars);
James Smart2e0fef82007-06-17 19:56:36 -05003093 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05003094}
3095
Linas Vepstas8d63f372007-02-14 14:28:36 -06003096/**
James Smart3621a712009-04-06 18:47:14 -04003097 * lpfc_pci_suspend_one - lpfc PCI func to suspend device for power management
James Smart3a55b532008-12-04 22:38:54 -05003098 * @pdev: pointer to PCI device
3099 * @msg: power management message
3100 *
3101 * This routine is to be registered to the kernel's PCI subsystem to support
3102 * system Power Management (PM). When PM invokes this method, it quiesces the
3103 * device by stopping the driver's worker thread for the device, turning off
3104 * device's interrupt and DMA, and bring the device offline. Note that as the
3105 * driver implements the minimum PM requirements to a power-aware driver's PM
3106 * support for suspend/resume -- all the possible PM messages (SUSPEND,
3107 * HIBERNATE, FREEZE) to the suspend() method call will be treated as SUSPEND
3108 * and the driver will fully reinitialize its device during resume() method
3109 * call, the driver will set device to PCI_D3hot state in PCI config space
3110 * instead of setting it according to the @msg provided by the PM.
3111 *
3112 * Return code
3113 * 0 - driver suspended the device
3114 * Error otherwise
3115 **/
3116static int
3117lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
3118{
3119 struct Scsi_Host *shost = pci_get_drvdata(pdev);
3120 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
3121
3122 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3123 "0473 PCI device Power Management suspend.\n");
3124
3125 /* Bring down the device */
3126 lpfc_offline_prep(phba);
3127 lpfc_offline(phba);
3128 kthread_stop(phba->worker_thread);
3129
3130 /* Disable interrupt from device */
3131 lpfc_disable_intr(phba);
3132
3133 /* Save device state to PCI config space */
3134 pci_save_state(pdev);
3135 pci_set_power_state(pdev, PCI_D3hot);
3136
3137 return 0;
3138}
3139
3140/**
James Smart3621a712009-04-06 18:47:14 -04003141 * lpfc_pci_resume_one - lpfc PCI func to resume device for power management
James Smart3a55b532008-12-04 22:38:54 -05003142 * @pdev: pointer to PCI device
3143 *
3144 * This routine is to be registered to the kernel's PCI subsystem to support
3145 * system Power Management (PM). When PM invokes this method, it restores
3146 * the device's PCI config space state and fully reinitializes the device
3147 * and brings it online. Note that as the driver implements the minimum PM
3148 * requirements to a power-aware driver's PM for suspend/resume -- all
3149 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
3150 * method call will be treated as SUSPEND and the driver will fully
3151 * reinitialize its device during resume() method call, the device will be
3152 * set to PCI_D0 directly in PCI config space before restoring the state.
3153 *
3154 * Return code
3155 * 0 - driver suspended the device
3156 * Error otherwise
3157 **/
3158static int
3159lpfc_pci_resume_one(struct pci_dev *pdev)
3160{
3161 struct Scsi_Host *shost = pci_get_drvdata(pdev);
3162 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -05003163 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05003164 int error;
3165
3166 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3167 "0452 PCI device Power Management resume.\n");
3168
3169 /* Restore device state from PCI config space */
3170 pci_set_power_state(pdev, PCI_D0);
3171 pci_restore_state(pdev);
3172 if (pdev->is_busmaster)
3173 pci_set_master(pdev);
3174
3175 /* Startup the kernel thread for this host adapter. */
3176 phba->worker_thread = kthread_run(lpfc_do_work, phba,
3177 "lpfc_worker_%d", phba->brd_no);
3178 if (IS_ERR(phba->worker_thread)) {
3179 error = PTR_ERR(phba->worker_thread);
3180 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3181 "0434 PM resume failed to start worker "
3182 "thread: error=x%x.\n", error);
3183 return error;
3184 }
3185
James Smart5b75da22008-12-04 22:39:35 -05003186 /* Configure and enable interrupt */
3187 intr_mode = lpfc_enable_intr(phba, phba->intr_mode);
3188 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -05003189 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -05003190 "0430 PM resume Failed to enable interrupt\n");
3191 return -EIO;
3192 } else
3193 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05003194
3195 /* Restart HBA and bring it online */
3196 lpfc_sli_brdrestart(phba);
3197 lpfc_online(phba);
3198
James Smart5b75da22008-12-04 22:39:35 -05003199 /* Log the current active interrupt mode */
3200 lpfc_log_intr_mode(phba, phba->intr_mode);
3201
James Smart3a55b532008-12-04 22:38:54 -05003202 return 0;
3203}
3204
3205/**
James Smart3621a712009-04-06 18:47:14 -04003206 * lpfc_io_error_detected - Driver method for handling PCI I/O error detected
James Smarte59058c2008-08-24 21:49:00 -04003207 * @pdev: pointer to PCI device.
3208 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -06003209 *
James Smarte59058c2008-08-24 21:49:00 -04003210 * This routine is registered to the PCI subsystem for error handling. This
3211 * function is called by the PCI subsystem after a PCI bus error affecting
3212 * this device has been detected. When this function is invoked, it will
3213 * need to stop all the I/Os and interrupt(s) to the device. Once that is
3214 * done, it will return PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to
3215 * perform proper recovery as desired.
3216 *
3217 * Return codes
3218 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
3219 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
3220 **/
Linas Vepstas8d63f372007-02-14 14:28:36 -06003221static pci_ers_result_t lpfc_io_error_detected(struct pci_dev *pdev,
3222 pci_channel_state_t state)
3223{
James Smart51ef4c22007-08-02 11:10:31 -04003224 struct Scsi_Host *shost = pci_get_drvdata(pdev);
3225 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06003226 struct lpfc_sli *psli = &phba->sli;
3227 struct lpfc_sli_ring *pring;
3228
James Smarta8e497d2008-08-24 21:50:11 -04003229 if (state == pci_channel_io_perm_failure) {
3230 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3231 "0472 PCI channel I/O permanent failure\n");
3232 /* Block all SCSI devices' I/Os on the host */
3233 lpfc_scsi_dev_block(phba);
3234 /* Clean up all driver's outstanding SCSI I/Os */
3235 lpfc_sli_flush_fcp_rings(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06003236 return PCI_ERS_RESULT_DISCONNECT;
James Smarta8e497d2008-08-24 21:50:11 -04003237 }
Linas Vepstas5daa49e2007-03-08 16:19:11 -06003238
Linas Vepstas8d63f372007-02-14 14:28:36 -06003239 pci_disable_device(pdev);
3240 /*
3241 * There may be I/Os dropped by the firmware.
3242 * Error iocb (I/O) on txcmplq and let the SCSI layer
3243 * retry it after re-establishing link.
3244 */
3245 pring = &psli->ring[psli->fcp_ring];
3246 lpfc_sli_abort_iocb_ring(phba, pring);
3247
James Smart5b75da22008-12-04 22:39:35 -05003248 /* Disable interrupt */
3249 lpfc_disable_intr(phba);
James Smart51ef4c22007-08-02 11:10:31 -04003250
Linas Vepstas8d63f372007-02-14 14:28:36 -06003251 /* Request a slot reset. */
3252 return PCI_ERS_RESULT_NEED_RESET;
3253}
3254
3255/**
James Smart3621a712009-04-06 18:47:14 -04003256 * lpfc_io_slot_reset - Restart a PCI device from scratch
James Smarte59058c2008-08-24 21:49:00 -04003257 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -06003258 *
James Smarte59058c2008-08-24 21:49:00 -04003259 * This routine is registered to the PCI subsystem for error handling. This is
3260 * called after PCI bus has been reset to restart the PCI card from scratch,
3261 * as if from a cold-boot. During the PCI subsystem error recovery, after the
3262 * driver returns PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform
3263 * proper error recovery and then call this routine before calling the .resume
3264 * method to recover the device. This function will initialize the HBA device,
3265 * enable the interrupt, but it will just put the HBA to offline state without
3266 * passing any I/O traffic.
3267 *
3268 * Return codes
3269 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
3270 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -06003271 */
3272static pci_ers_result_t lpfc_io_slot_reset(struct pci_dev *pdev)
3273{
James Smart51ef4c22007-08-02 11:10:31 -04003274 struct Scsi_Host *shost = pci_get_drvdata(pdev);
3275 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06003276 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -05003277 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06003278
3279 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +11003280 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -06003281 printk(KERN_ERR "lpfc: Cannot re-enable "
3282 "PCI device after reset.\n");
3283 return PCI_ERS_RESULT_DISCONNECT;
3284 }
3285
James Smart97207482008-12-04 22:39:19 -05003286 pci_restore_state(pdev);
3287 if (pdev->is_busmaster)
3288 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06003289
James Smart92d7f7b2007-06-17 19:56:38 -05003290 spin_lock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06003291 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05003292 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06003293
James Smart5b75da22008-12-04 22:39:35 -05003294 /* Configure and enable interrupt */
3295 intr_mode = lpfc_enable_intr(phba, phba->intr_mode);
3296 if (intr_mode == LPFC_INTR_ERROR) {
3297 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3298 "0427 Cannot re-enable interrupt after "
3299 "slot reset.\n");
3300 return PCI_ERS_RESULT_DISCONNECT;
3301 } else
3302 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06003303
3304 /* Take device offline; this will perform cleanup */
3305 lpfc_offline(phba);
3306 lpfc_sli_brdrestart(phba);
3307
James Smart5b75da22008-12-04 22:39:35 -05003308 /* Log the current active interrupt mode */
3309 lpfc_log_intr_mode(phba, phba->intr_mode);
3310
Linas Vepstas8d63f372007-02-14 14:28:36 -06003311 return PCI_ERS_RESULT_RECOVERED;
3312}
3313
3314/**
James Smart3621a712009-04-06 18:47:14 -04003315 * lpfc_io_resume - Resume PCI I/O operation
James Smarte59058c2008-08-24 21:49:00 -04003316 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -06003317 *
James Smarte59058c2008-08-24 21:49:00 -04003318 * This routine is registered to the PCI subsystem for error handling. It is
3319 * called when kernel error recovery tells the lpfc driver that it is ok to
3320 * resume normal PCI operation after PCI bus error recovery. After this call,
3321 * traffic can start to flow from this device again.
Linas Vepstas8d63f372007-02-14 14:28:36 -06003322 */
3323static void lpfc_io_resume(struct pci_dev *pdev)
3324{
James Smart51ef4c22007-08-02 11:10:31 -04003325 struct Scsi_Host *shost = pci_get_drvdata(pdev);
3326 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06003327
James Smart58da1ff2008-04-07 10:15:56 -04003328 lpfc_online(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06003329}
3330
dea31012005-04-17 16:05:31 -05003331static struct pci_device_id lpfc_id_table[] = {
3332 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
3333 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04003334 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
3335 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05003336 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
3337 PCI_ANY_ID, PCI_ANY_ID, },
3338 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
3339 PCI_ANY_ID, PCI_ANY_ID, },
3340 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
3341 PCI_ANY_ID, PCI_ANY_ID, },
3342 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
3343 PCI_ANY_ID, PCI_ANY_ID, },
3344 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
3345 PCI_ANY_ID, PCI_ANY_ID, },
3346 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
3347 PCI_ANY_ID, PCI_ANY_ID, },
3348 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
3349 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05003350 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
3351 PCI_ANY_ID, PCI_ANY_ID, },
3352 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
3353 PCI_ANY_ID, PCI_ANY_ID, },
3354 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
3355 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05003356 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
3357 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05003358 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
3359 PCI_ANY_ID, PCI_ANY_ID, },
3360 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
3361 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05003362 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
3363 PCI_ANY_ID, PCI_ANY_ID, },
3364 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
3365 PCI_ANY_ID, PCI_ANY_ID, },
3366 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
3367 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -04003368 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
3369 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05003370 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
3371 PCI_ANY_ID, PCI_ANY_ID, },
3372 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
3373 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05003374 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
3375 PCI_ANY_ID, PCI_ANY_ID, },
3376 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
3377 PCI_ANY_ID, PCI_ANY_ID, },
3378 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
3379 PCI_ANY_ID, PCI_ANY_ID, },
3380 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
3381 PCI_ANY_ID, PCI_ANY_ID, },
3382 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
3383 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05003384 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
3385 PCI_ANY_ID, PCI_ANY_ID, },
3386 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
3387 PCI_ANY_ID, PCI_ANY_ID, },
James Smartb87eab32007-04-25 09:53:28 -04003388 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
3389 PCI_ANY_ID, PCI_ANY_ID, },
3390 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
3391 PCI_ANY_ID, PCI_ANY_ID, },
3392 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
3393 PCI_ANY_ID, PCI_ANY_ID, },
3394 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
3395 PCI_ANY_ID, PCI_ANY_ID, },
3396 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
3397 PCI_ANY_ID, PCI_ANY_ID, },
3398 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
3399 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -04003400 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
3401 PCI_ANY_ID, PCI_ANY_ID, },
3402 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
3403 PCI_ANY_ID, PCI_ANY_ID, },
3404 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
3405 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05003406 { 0 }
3407};
3408
3409MODULE_DEVICE_TABLE(pci, lpfc_id_table);
3410
Linas Vepstas8d63f372007-02-14 14:28:36 -06003411static struct pci_error_handlers lpfc_err_handler = {
3412 .error_detected = lpfc_io_error_detected,
3413 .slot_reset = lpfc_io_slot_reset,
3414 .resume = lpfc_io_resume,
3415};
3416
dea31012005-04-17 16:05:31 -05003417static struct pci_driver lpfc_driver = {
3418 .name = LPFC_DRIVER_NAME,
3419 .id_table = lpfc_id_table,
3420 .probe = lpfc_pci_probe_one,
3421 .remove = __devexit_p(lpfc_pci_remove_one),
James Smart3a55b532008-12-04 22:38:54 -05003422 .suspend = lpfc_pci_suspend_one,
3423 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -05003424 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -05003425};
3426
James Smarte59058c2008-08-24 21:49:00 -04003427/**
James Smart3621a712009-04-06 18:47:14 -04003428 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -04003429 *
3430 * This routine is to be invoked when the lpfc module is loaded into the
3431 * kernel. The special kernel macro module_init() is used to indicate the
3432 * role of this routine to the kernel as lpfc module entry point.
3433 *
3434 * Return codes
3435 * 0 - successful
3436 * -ENOMEM - FC attach transport failed
3437 * all others - failed
3438 */
dea31012005-04-17 16:05:31 -05003439static int __init
3440lpfc_init(void)
3441{
3442 int error = 0;
3443
3444 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003445 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -05003446
James Smart7ee5d432007-10-27 13:37:17 -04003447 if (lpfc_enable_npiv) {
3448 lpfc_transport_functions.vport_create = lpfc_vport_create;
3449 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
3450 }
dea31012005-04-17 16:05:31 -05003451 lpfc_transport_template =
3452 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -04003453 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -05003454 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -04003455 if (lpfc_enable_npiv) {
James Smart7ee5d432007-10-27 13:37:17 -04003456 lpfc_vport_transport_template =
James Smart98c9ea52007-10-27 13:37:33 -04003457 fc_attach_transport(&lpfc_vport_transport_functions);
3458 if (lpfc_vport_transport_template == NULL) {
3459 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -04003460 return -ENOMEM;
James Smart98c9ea52007-10-27 13:37:33 -04003461 }
James Smart7ee5d432007-10-27 13:37:17 -04003462 }
dea31012005-04-17 16:05:31 -05003463 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -05003464 if (error) {
dea31012005-04-17 16:05:31 -05003465 fc_release_transport(lpfc_transport_template);
James Smartd7c255b2008-08-24 21:50:00 -04003466 if (lpfc_enable_npiv)
3467 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -05003468 }
dea31012005-04-17 16:05:31 -05003469
3470 return error;
3471}
3472
James Smarte59058c2008-08-24 21:49:00 -04003473/**
James Smart3621a712009-04-06 18:47:14 -04003474 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -04003475 *
3476 * This routine is invoked when the lpfc module is removed from the kernel.
3477 * The special kernel macro module_exit() is used to indicate the role of
3478 * this routine to the kernel as lpfc module exit point.
3479 */
dea31012005-04-17 16:05:31 -05003480static void __exit
3481lpfc_exit(void)
3482{
3483 pci_unregister_driver(&lpfc_driver);
3484 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -04003485 if (lpfc_enable_npiv)
3486 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -05003487 if (_dump_buf_data) {
3488 printk(KERN_ERR "BLKGRD freeing %lu pages for _dump_buf_data "
3489 "at 0x%p\n",
3490 (1L << _dump_buf_data_order), _dump_buf_data);
3491 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
3492 }
3493
3494 if (_dump_buf_dif) {
3495 printk(KERN_ERR "BLKGRD freeing %lu pages for _dump_buf_dif "
3496 "at 0x%p\n",
3497 (1L << _dump_buf_dif_order), _dump_buf_dif);
3498 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
3499 }
dea31012005-04-17 16:05:31 -05003500}
3501
3502module_init(lpfc_init);
3503module_exit(lpfc_exit);
3504MODULE_LICENSE("GPL");
3505MODULE_DESCRIPTION(LPFC_MODULE_DESC);
3506MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
3507MODULE_VERSION("0:" LPFC_DRIVER_VERSION);