[SCSI] lpfc 8.2.8 : Miscellaneous Discovery Fixes
[deliverable/linux.git] / drivers / scsi / lpfc / lpfc_init.c
CommitLineData
dea3101e 1/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
c44ce173 3 * Fibre Channel Host Bus Adapters. *
e47c9093 4 * Copyright (C) 2004-2008 Emulex. All rights reserved. *
c44ce173 5 * EMULEX and SLI are trademarks of Emulex. *
dea3101e 6 * www.emulex.com *
c44ce173 7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea3101e 8 * *
9 * This program is free software; you can redistribute it and/or *
c44ce173
JSEC
10 * 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. *
dea3101e 20 *******************************************************************/
21
dea3101e 22#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>
92d7f7b0 30#include <linux/ctype.h>
dea3101e 31
91886523 32#include <scsi/scsi.h>
dea3101e 33#include <scsi/scsi_device.h>
34#include <scsi/scsi_host.h>
35#include <scsi/scsi_transport_fc.h>
36
37#include "lpfc_hw.h"
38#include "lpfc_sli.h"
39#include "lpfc_disc.h"
40#include "lpfc_scsi.h"
41#include "lpfc.h"
42#include "lpfc_logmsg.h"
43#include "lpfc_crtn.h"
92d7f7b0 44#include "lpfc_vport.h"
dea3101e 45#include "lpfc_version.h"
46
74b72a59 47static int lpfc_parse_vpd(struct lpfc_hba *, uint8_t *, int);
dea3101e 48static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
49static int lpfc_post_rcv_buf(struct lpfc_hba *);
50
51static struct scsi_transport_template *lpfc_transport_template = NULL;
92d7f7b0 52static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea3101e 53static DEFINE_IDR(lpfc_hba_index);
54
e59058c4
JS
55/**
56 * lpfc_config_port_prep: Perform lpfc initialization prior to config port.
57 * @phba: pointer to lpfc hba data structure.
58 *
59 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
60 * mailbox command. It retrieves the revision information from the HBA and
61 * collects the Vital Product Data (VPD) about the HBA for preparing the
62 * configuration of the HBA.
63 *
64 * Return codes:
65 * 0 - success.
66 * -ERESTART - requests the SLI layer to reset the HBA and try again.
67 * Any other value - indicates an error.
68 **/
dea3101e 69int
2e0fef85 70lpfc_config_port_prep(struct lpfc_hba *phba)
dea3101e 71{
72 lpfc_vpd_t *vp = &phba->vpd;
73 int i = 0, rc;
74 LPFC_MBOXQ_t *pmb;
75 MAILBOX_t *mb;
76 char *lpfc_vpd_data = NULL;
77 uint16_t offset = 0;
78 static char licensed[56] =
79 "key unlock for use with gnu public licensed code only\0";
65a29c16 80 static int init_key = 1;
dea3101e 81
82 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
83 if (!pmb) {
2e0fef85 84 phba->link_state = LPFC_HBA_ERROR;
dea3101e 85 return -ENOMEM;
86 }
87
88 mb = &pmb->mb;
2e0fef85 89 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e 90
91 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
65a29c16
JS
92 if (init_key) {
93 uint32_t *ptext = (uint32_t *) licensed;
dea3101e 94
65a29c16
JS
95 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
96 *ptext = cpu_to_be32(*ptext);
97 init_key = 0;
98 }
dea3101e 99
100 lpfc_read_nv(phba, pmb);
101 memset((char*)mb->un.varRDnvp.rsvd3, 0,
102 sizeof (mb->un.varRDnvp.rsvd3));
103 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
104 sizeof (licensed));
105
106 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
107
108 if (rc != MBX_SUCCESS) {
ed957684 109 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
e8b62011 110 "0324 Config Port initialization "
dea3101e 111 "error, mbxCmd x%x READ_NVPARM, "
112 "mbxStatus x%x\n",
dea3101e 113 mb->mbxCommand, mb->mbxStatus);
114 mempool_free(pmb, phba->mbox_mem_pool);
115 return -ERESTART;
116 }
117 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
2e0fef85
JS
118 sizeof(phba->wwnn));
119 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
120 sizeof(phba->wwpn));
dea3101e 121 }
122
92d7f7b0
JS
123 phba->sli3_options = 0x0;
124
dea3101e 125 /* Setup and issue mailbox READ REV command */
126 lpfc_read_rev(phba, pmb);
127 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
128 if (rc != MBX_SUCCESS) {
ed957684 129 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 130 "0439 Adapter failed to init, mbxCmd x%x "
dea3101e 131 "READ_REV, mbxStatus x%x\n",
dea3101e 132 mb->mbxCommand, mb->mbxStatus);
133 mempool_free( pmb, phba->mbox_mem_pool);
134 return -ERESTART;
135 }
136
92d7f7b0 137
1de933f3
JSEC
138 /*
139 * The value of rr must be 1 since the driver set the cv field to 1.
140 * This setting requires the FW to set all revision fields.
dea3101e 141 */
1de933f3 142 if (mb->un.varRdRev.rr == 0) {
dea3101e 143 vp->rev.rBit = 0;
1de933f3 144 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011
JS
145 "0440 Adapter failed to init, READ_REV has "
146 "missing revision information.\n");
dea3101e 147 mempool_free(pmb, phba->mbox_mem_pool);
148 return -ERESTART;
dea3101e 149 }
150
495a714c
JS
151 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
152 mempool_free(pmb, phba->mbox_mem_pool);
ed957684 153 return -EINVAL;
495a714c 154 }
ed957684 155
dea3101e 156 /* Save information as VPD data */
1de933f3 157 vp->rev.rBit = 1;
92d7f7b0 158 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
1de933f3
JSEC
159 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
160 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
161 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
162 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea3101e 163 vp->rev.biuRev = mb->un.varRdRev.biuRev;
164 vp->rev.smRev = mb->un.varRdRev.smRev;
165 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
166 vp->rev.endecRev = mb->un.varRdRev.endecRev;
167 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
168 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
169 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
170 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
171 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
172 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
173
92d7f7b0
JS
174 /* If the sli feature level is less then 9, we must
175 * tear down all RPIs and VPIs on link down if NPIV
176 * is enabled.
177 */
178 if (vp->rev.feaLevelHigh < 9)
179 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
180
dea3101e 181 if (lpfc_is_LC_HBA(phba->pcidev->device))
182 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
183 sizeof (phba->RandomData));
184
dea3101e 185 /* Get adapter VPD information */
186 pmb->context2 = kmalloc(DMP_RSP_SIZE, GFP_KERNEL);
187 if (!pmb->context2)
188 goto out_free_mbox;
189 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
190 if (!lpfc_vpd_data)
191 goto out_free_context2;
192
193 do {
194 lpfc_dump_mem(phba, pmb, offset);
195 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
196
197 if (rc != MBX_SUCCESS) {
198 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011 199 "0441 VPD not present on adapter, "
dea3101e 200 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea3101e 201 mb->mbxCommand, mb->mbxStatus);
74b72a59 202 mb->un.varDmp.word_cnt = 0;
dea3101e 203 }
74b72a59
JW
204 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
205 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
dea3101e 206 lpfc_sli_pcimem_bcopy(pmb->context2, lpfc_vpd_data + offset,
92d7f7b0 207 mb->un.varDmp.word_cnt);
dea3101e 208 offset += mb->un.varDmp.word_cnt;
74b72a59
JW
209 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
210 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea3101e 211
212 kfree(lpfc_vpd_data);
213out_free_context2:
214 kfree(pmb->context2);
215out_free_mbox:
216 mempool_free(pmb, phba->mbox_mem_pool);
217 return 0;
218}
219
e59058c4
JS
220/**
221 * lpfc_config_async_cmpl: Completion handler for config async event mbox cmd.
222 * @phba: pointer to lpfc hba data structure.
223 * @pmboxq: pointer to the driver internal queue element for mailbox command.
224 *
225 * This is the completion handler for driver's configuring asynchronous event
226 * mailbox command to the device. If the mailbox command returns successfully,
227 * it will set internal async event support flag to 1; otherwise, it will
228 * set internal async event support flag to 0.
229 **/
57127f15
JS
230static void
231lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
232{
233 if (pmboxq->mb.mbxStatus == MBX_SUCCESS)
234 phba->temp_sensor_support = 1;
235 else
236 phba->temp_sensor_support = 0;
237 mempool_free(pmboxq, phba->mbox_mem_pool);
238 return;
239}
240
e59058c4
JS
241/**
242 * lpfc_config_port_post: Perform lpfc initialization after config port.
243 * @phba: pointer to lpfc hba data structure.
244 *
245 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
246 * command call. It performs all internal resource and state setups on the
247 * port: post IOCB buffers, enable appropriate host interrupt attentions,
248 * ELS ring timers, etc.
249 *
250 * Return codes
251 * 0 - success.
252 * Any other value - error.
253 **/
dea3101e 254int
2e0fef85 255lpfc_config_port_post(struct lpfc_hba *phba)
dea3101e 256{
2e0fef85 257 struct lpfc_vport *vport = phba->pport;
dea3101e 258 LPFC_MBOXQ_t *pmb;
259 MAILBOX_t *mb;
260 struct lpfc_dmabuf *mp;
261 struct lpfc_sli *psli = &phba->sli;
262 uint32_t status, timeout;
2e0fef85
JS
263 int i, j;
264 int rc;
dea3101e 265
7af67051
JS
266 spin_lock_irq(&phba->hbalock);
267 /*
268 * If the Config port completed correctly the HBA is not
269 * over heated any more.
270 */
271 if (phba->over_temp_state == HBA_OVER_TEMP)
272 phba->over_temp_state = HBA_NORMAL_TEMP;
273 spin_unlock_irq(&phba->hbalock);
274
dea3101e 275 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
276 if (!pmb) {
2e0fef85 277 phba->link_state = LPFC_HBA_ERROR;
dea3101e 278 return -ENOMEM;
279 }
280 mb = &pmb->mb;
281
dea3101e 282 /* Get login parameters for NID. */
92d7f7b0 283 lpfc_read_sparam(phba, pmb, 0);
ed957684 284 pmb->vport = vport;
dea3101e 285 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
ed957684 286 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 287 "0448 Adapter failed init, mbxCmd x%x "
dea3101e 288 "READ_SPARM mbxStatus x%x\n",
dea3101e 289 mb->mbxCommand, mb->mbxStatus);
2e0fef85 290 phba->link_state = LPFC_HBA_ERROR;
dea3101e 291 mp = (struct lpfc_dmabuf *) pmb->context1;
292 mempool_free( pmb, phba->mbox_mem_pool);
293 lpfc_mbuf_free(phba, mp->virt, mp->phys);
294 kfree(mp);
295 return -EIO;
296 }
297
298 mp = (struct lpfc_dmabuf *) pmb->context1;
299
2e0fef85 300 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea3101e 301 lpfc_mbuf_free(phba, mp->virt, mp->phys);
302 kfree(mp);
303 pmb->context1 = NULL;
304
a12e07bc 305 if (phba->cfg_soft_wwnn)
2e0fef85
JS
306 u64_to_wwn(phba->cfg_soft_wwnn,
307 vport->fc_sparam.nodeName.u.wwn);
c3f28afa 308 if (phba->cfg_soft_wwpn)
2e0fef85
JS
309 u64_to_wwn(phba->cfg_soft_wwpn,
310 vport->fc_sparam.portName.u.wwn);
311 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
dea3101e 312 sizeof (struct lpfc_name));
2e0fef85 313 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
dea3101e 314 sizeof (struct lpfc_name));
315 /* If no serial number in VPD data, use low 6 bytes of WWNN */
316 /* This should be consolidated into parse_vpd ? - mr */
317 if (phba->SerialNumber[0] == 0) {
318 uint8_t *outptr;
319
2e0fef85 320 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea3101e 321 for (i = 0; i < 12; i++) {
322 status = *outptr++;
323 j = ((status & 0xf0) >> 4);
324 if (j <= 9)
325 phba->SerialNumber[i] =
326 (char)((uint8_t) 0x30 + (uint8_t) j);
327 else
328 phba->SerialNumber[i] =
329 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
330 i++;
331 j = (status & 0xf);
332 if (j <= 9)
333 phba->SerialNumber[i] =
334 (char)((uint8_t) 0x30 + (uint8_t) j);
335 else
336 phba->SerialNumber[i] =
337 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
338 }
339 }
340
dea3101e 341 lpfc_read_config(phba, pmb);
ed957684 342 pmb->vport = vport;
dea3101e 343 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
ed957684 344 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 345 "0453 Adapter failed to init, mbxCmd x%x "
dea3101e 346 "READ_CONFIG, mbxStatus x%x\n",
dea3101e 347 mb->mbxCommand, mb->mbxStatus);
2e0fef85 348 phba->link_state = LPFC_HBA_ERROR;
dea3101e 349 mempool_free( pmb, phba->mbox_mem_pool);
350 return -EIO;
351 }
352
353 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
354 if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
355 phba->cfg_hba_queue_depth =
356 mb->un.varRdConfig.max_xri + 1;
357
358 phba->lmt = mb->un.varRdConfig.lmt;
74b72a59
JW
359
360 /* Get the default values for Model Name and Description */
361 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
362
363 if ((phba->cfg_link_speed > LINK_SPEED_10G)
364 || ((phba->cfg_link_speed == LINK_SPEED_1G)
365 && !(phba->lmt & LMT_1Gb))
366 || ((phba->cfg_link_speed == LINK_SPEED_2G)
367 && !(phba->lmt & LMT_2Gb))
368 || ((phba->cfg_link_speed == LINK_SPEED_4G)
369 && !(phba->lmt & LMT_4Gb))
370 || ((phba->cfg_link_speed == LINK_SPEED_8G)
371 && !(phba->lmt & LMT_8Gb))
372 || ((phba->cfg_link_speed == LINK_SPEED_10G)
373 && !(phba->lmt & LMT_10Gb))) {
374 /* Reset link speed to auto */
ed957684 375 lpfc_printf_log(phba, KERN_WARNING, LOG_LINK_EVENT,
e8b62011 376 "1302 Invalid speed for this board: "
dea3101e 377 "Reset link speed to auto: x%x\n",
dea3101e 378 phba->cfg_link_speed);
379 phba->cfg_link_speed = LINK_SPEED_AUTO;
380 }
381
2e0fef85 382 phba->link_state = LPFC_LINK_DOWN;
dea3101e 383
0b727fea 384 /* Only process IOCBs on ELS ring till hba_state is READY */
a4bc3379
JS
385 if (psli->ring[psli->extra_ring].cmdringaddr)
386 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
dea3101e 387 if (psli->ring[psli->fcp_ring].cmdringaddr)
388 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
389 if (psli->ring[psli->next_ring].cmdringaddr)
390 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
391
392 /* Post receive buffers for desired rings */
ed957684
JS
393 if (phba->sli_rev != 3)
394 lpfc_post_rcv_buf(phba);
dea3101e 395
396 /* Enable appropriate host interrupts */
2e0fef85 397 spin_lock_irq(&phba->hbalock);
dea3101e 398 status = readl(phba->HCregaddr);
399 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
400 if (psli->num_rings > 0)
401 status |= HC_R0INT_ENA;
402 if (psli->num_rings > 1)
403 status |= HC_R1INT_ENA;
404 if (psli->num_rings > 2)
405 status |= HC_R2INT_ENA;
406 if (psli->num_rings > 3)
407 status |= HC_R3INT_ENA;
408
875fbdfe
JSEC
409 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
410 (phba->cfg_poll & DISABLE_FCP_RING_INT))
411 status &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
412
dea3101e 413 writel(status, phba->HCregaddr);
414 readl(phba->HCregaddr); /* flush */
2e0fef85 415 spin_unlock_irq(&phba->hbalock);
dea3101e 416
417 /*
418 * Setup the ring 0 (els) timeout handler
419 */
420 timeout = phba->fc_ratov << 1;
2e0fef85 421 mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
858c9f6c
JS
422 mod_timer(&phba->hb_tmofunc, jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
423 phba->hb_outstanding = 0;
424 phba->last_completion_time = jiffies;
dea3101e 425
426 lpfc_init_link(phba, pmb, phba->cfg_topology, phba->cfg_link_speed);
427 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
ed957684 428 pmb->vport = vport;
8aee918a 429 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
5b8bd0c9 430 lpfc_set_loopback_flag(phba);
8aee918a 431 if (rc != MBX_SUCCESS) {
ed957684 432 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 433 "0454 Adapter failed to init, mbxCmd x%x "
dea3101e 434 "INIT_LINK, mbxStatus x%x\n",
dea3101e 435 mb->mbxCommand, mb->mbxStatus);
436
437 /* Clear all interrupt enable conditions */
438 writel(0, phba->HCregaddr);
439 readl(phba->HCregaddr); /* flush */
440 /* Clear all pending interrupts */
441 writel(0xffffffff, phba->HAregaddr);
442 readl(phba->HAregaddr); /* flush */
443
2e0fef85 444 phba->link_state = LPFC_HBA_ERROR;
8aee918a
JS
445 if (rc != MBX_BUSY)
446 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 447 return -EIO;
448 }
449 /* MBOX buffer will be freed in mbox compl */
57127f15
JS
450 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
451 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
452 pmb->mbox_cmpl = lpfc_config_async_cmpl;
453 pmb->vport = phba->pport;
454 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea3101e 455
57127f15
JS
456 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
457 lpfc_printf_log(phba,
458 KERN_ERR,
459 LOG_INIT,
460 "0456 Adapter failed to issue "
461 "ASYNCEVT_ENABLE mbox status x%x \n.",
462 rc);
463 mempool_free(pmb, phba->mbox_mem_pool);
464 }
ce8b3ce5
JS
465 return (0);
466}
467
e59058c4
JS
468/**
469 * lpfc_hba_down_prep: Perform lpfc uninitialization prior to HBA reset.
470 * @phba: pointer to lpfc HBA data structure.
471 *
472 * This routine will do LPFC uninitialization before the HBA is reset when
473 * bringing down the SLI Layer.
474 *
475 * Return codes
476 * 0 - success.
477 * Any other value - error.
478 **/
dea3101e 479int
2e0fef85 480lpfc_hba_down_prep(struct lpfc_hba *phba)
dea3101e 481{
1b32f6aa
JS
482 struct lpfc_vport **vports;
483 int i;
dea3101e 484 /* Disable interrupts */
485 writel(0, phba->HCregaddr);
486 readl(phba->HCregaddr); /* flush */
487
1b32f6aa
JS
488 if (phba->pport->load_flag & FC_UNLOADING)
489 lpfc_cleanup_discovery_resources(phba->pport);
490 else {
491 vports = lpfc_create_vport_work_array(phba);
492 if (vports != NULL)
493 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++)
494 lpfc_cleanup_discovery_resources(vports[i]);
495 lpfc_destroy_vport_work_array(phba, vports);
7f5f3d0d
JS
496 }
497 return 0;
dea3101e 498}
499
e59058c4
JS
500/**
501 * lpfc_hba_down_post: Perform lpfc uninitialization after HBA reset.
502 * @phba: pointer to lpfc HBA data structure.
503 *
504 * This routine will do uninitialization after the HBA is reset when bring
505 * down the SLI Layer.
506 *
507 * Return codes
508 * 0 - sucess.
509 * Any other value - error.
510 **/
41415862 511int
2e0fef85 512lpfc_hba_down_post(struct lpfc_hba *phba)
41415862
JW
513{
514 struct lpfc_sli *psli = &phba->sli;
515 struct lpfc_sli_ring *pring;
516 struct lpfc_dmabuf *mp, *next_mp;
09372820
JS
517 struct lpfc_iocbq *iocb;
518 IOCB_t *cmd = NULL;
519 LIST_HEAD(completions);
41415862
JW
520 int i;
521
92d7f7b0
JS
522 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
523 lpfc_sli_hbqbuf_free_all(phba);
524 else {
525 /* Cleanup preposted buffers on the ELS ring */
526 pring = &psli->ring[LPFC_ELS_RING];
527 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
528 list_del(&mp->list);
529 pring->postbufq_cnt--;
530 lpfc_mbuf_free(phba, mp->virt, mp->phys);
531 kfree(mp);
532 }
41415862
JW
533 }
534
09372820 535 spin_lock_irq(&phba->hbalock);
41415862
JW
536 for (i = 0; i < psli->num_rings; i++) {
537 pring = &psli->ring[i];
09372820
JS
538
539 /* At this point in time the HBA is either reset or DOA. Either
540 * way, nothing should be on txcmplq as it will NEVER complete.
541 */
542 list_splice_init(&pring->txcmplq, &completions);
543 pring->txcmplq_cnt = 0;
544 spin_unlock_irq(&phba->hbalock);
545
546 while (!list_empty(&completions)) {
547 iocb = list_get_first(&completions, struct lpfc_iocbq,
548 list);
549 cmd = &iocb->iocb;
550 list_del_init(&iocb->list);
551
552 if (!iocb->iocb_cmpl)
553 lpfc_sli_release_iocbq(phba, iocb);
554 else {
555 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
556 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
557 (iocb->iocb_cmpl) (phba, iocb, iocb);
558 }
559 }
560
41415862 561 lpfc_sli_abort_iocb_ring(phba, pring);
09372820 562 spin_lock_irq(&phba->hbalock);
41415862 563 }
09372820 564 spin_unlock_irq(&phba->hbalock);
41415862
JW
565
566 return 0;
567}
568
e59058c4
JS
569/**
570 * lpfc_hb_timeout: The HBA-timer timeout handler.
571 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
572 *
573 * This is the HBA-timer timeout handler registered to the lpfc driver. When
574 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
575 * work-port-events bitmap and the worker thread is notified. This timeout
576 * event will be used by the worker thread to invoke the actual timeout
577 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
578 * be performed in the timeout handler and the HBA timeout event bit shall
579 * be cleared by the worker thread after it has taken the event bitmap out.
580 **/
a6ababd2 581static void
858c9f6c
JS
582lpfc_hb_timeout(unsigned long ptr)
583{
584 struct lpfc_hba *phba;
5e9d9b82 585 uint32_t tmo_posted;
858c9f6c
JS
586 unsigned long iflag;
587
588 phba = (struct lpfc_hba *)ptr;
589 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
5e9d9b82
JS
590 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
591 if (!tmo_posted)
858c9f6c
JS
592 phba->pport->work_port_events |= WORKER_HB_TMO;
593 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
594
5e9d9b82
JS
595 if (!tmo_posted)
596 lpfc_worker_wake_up(phba);
858c9f6c
JS
597 return;
598}
599
e59058c4
JS
600/**
601 * lpfc_hb_mbox_cmpl: The lpfc heart-beat mailbox command callback function.
602 * @phba: pointer to lpfc hba data structure.
603 * @pmboxq: pointer to the driver internal queue element for mailbox command.
604 *
605 * This is the callback function to the lpfc heart-beat mailbox command.
606 * If configured, the lpfc driver issues the heart-beat mailbox command to
607 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
608 * heart-beat mailbox command is issued, the driver shall set up heart-beat
609 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
610 * heart-beat outstanding state. Once the mailbox command comes back and
611 * no error conditions detected, the heart-beat mailbox command timer is
612 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
613 * state is cleared for the next heart-beat. If the timer expired with the
614 * heart-beat outstanding state set, the driver will put the HBA offline.
615 **/
858c9f6c
JS
616static void
617lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
618{
619 unsigned long drvr_flag;
620
621 spin_lock_irqsave(&phba->hbalock, drvr_flag);
622 phba->hb_outstanding = 0;
623 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
624
625 mempool_free(pmboxq, phba->mbox_mem_pool);
626 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
627 !(phba->link_state == LPFC_HBA_ERROR) &&
51ef4c26 628 !(phba->pport->load_flag & FC_UNLOADING))
858c9f6c
JS
629 mod_timer(&phba->hb_tmofunc,
630 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
631 return;
632}
633
e59058c4
JS
634/**
635 * lpfc_hb_timeout_handler: The HBA-timer timeout handler.
636 * @phba: pointer to lpfc hba data structure.
637 *
638 * This is the actual HBA-timer timeout handler to be invoked by the worker
639 * thread whenever the HBA timer fired and HBA-timeout event posted. This
640 * handler performs any periodic operations needed for the device. If such
641 * periodic event has already been attended to either in the interrupt handler
642 * or by processing slow-ring or fast-ring events within the HBA-timer
643 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
644 * the timer for the next timeout period. If lpfc heart-beat mailbox command
645 * is configured and there is no heart-beat mailbox command outstanding, a
646 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
647 * has been a heart-beat mailbox command outstanding, the HBA shall be put
648 * to offline.
649 **/
858c9f6c
JS
650void
651lpfc_hb_timeout_handler(struct lpfc_hba *phba)
652{
653 LPFC_MBOXQ_t *pmboxq;
0ff10d46 654 struct lpfc_dmabuf *buf_ptr;
858c9f6c
JS
655 int retval;
656 struct lpfc_sli *psli = &phba->sli;
0ff10d46 657 LIST_HEAD(completions);
858c9f6c
JS
658
659 if ((phba->link_state == LPFC_HBA_ERROR) ||
51ef4c26 660 (phba->pport->load_flag & FC_UNLOADING) ||
858c9f6c
JS
661 (phba->pport->fc_flag & FC_OFFLINE_MODE))
662 return;
663
664 spin_lock_irq(&phba->pport->work_port_lock);
665 /* If the timer is already canceled do nothing */
666 if (!(phba->pport->work_port_events & WORKER_HB_TMO)) {
667 spin_unlock_irq(&phba->pport->work_port_lock);
668 return;
669 }
670
671 if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
672 jiffies)) {
673 spin_unlock_irq(&phba->pport->work_port_lock);
674 if (!phba->hb_outstanding)
675 mod_timer(&phba->hb_tmofunc,
676 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
677 else
678 mod_timer(&phba->hb_tmofunc,
679 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
680 return;
681 }
682 spin_unlock_irq(&phba->pport->work_port_lock);
683
0ff10d46
JS
684 if (phba->elsbuf_cnt &&
685 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
686 spin_lock_irq(&phba->hbalock);
687 list_splice_init(&phba->elsbuf, &completions);
688 phba->elsbuf_cnt = 0;
689 phba->elsbuf_prev_cnt = 0;
690 spin_unlock_irq(&phba->hbalock);
691
692 while (!list_empty(&completions)) {
693 list_remove_head(&completions, buf_ptr,
694 struct lpfc_dmabuf, list);
695 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
696 kfree(buf_ptr);
697 }
698 }
699 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
700
858c9f6c 701 /* If there is no heart beat outstanding, issue a heartbeat command */
13815c83
JS
702 if (phba->cfg_enable_hba_heartbeat) {
703 if (!phba->hb_outstanding) {
704 pmboxq = mempool_alloc(phba->mbox_mem_pool,GFP_KERNEL);
705 if (!pmboxq) {
706 mod_timer(&phba->hb_tmofunc,
707 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
708 return;
709 }
858c9f6c 710
13815c83
JS
711 lpfc_heart_beat(phba, pmboxq);
712 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
713 pmboxq->vport = phba->pport;
714 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
858c9f6c 715
13815c83
JS
716 if (retval != MBX_BUSY && retval != MBX_SUCCESS) {
717 mempool_free(pmboxq, phba->mbox_mem_pool);
718 mod_timer(&phba->hb_tmofunc,
719 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
720 return;
721 }
858c9f6c 722 mod_timer(&phba->hb_tmofunc,
13815c83
JS
723 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
724 phba->hb_outstanding = 1;
858c9f6c 725 return;
13815c83
JS
726 } else {
727 /*
728 * If heart beat timeout called with hb_outstanding set
729 * we need to take the HBA offline.
730 */
731 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
732 "0459 Adapter heartbeat failure, "
733 "taking this port offline.\n");
734
735 spin_lock_irq(&phba->hbalock);
736 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
737 spin_unlock_irq(&phba->hbalock);
738
739 lpfc_offline_prep(phba);
740 lpfc_offline(phba);
741 lpfc_unblock_mgmt_io(phba);
742 phba->link_state = LPFC_HBA_ERROR;
743 lpfc_hba_down_post(phba);
858c9f6c 744 }
858c9f6c
JS
745 }
746}
747
e59058c4
JS
748/**
749 * lpfc_offline_eratt: Bring lpfc offline on hardware error attention.
750 * @phba: pointer to lpfc hba data structure.
751 *
752 * This routine is called to bring the HBA offline when HBA hardware error
753 * other than Port Error 6 has been detected.
754 **/
09372820
JS
755static void
756lpfc_offline_eratt(struct lpfc_hba *phba)
757{
758 struct lpfc_sli *psli = &phba->sli;
759
760 spin_lock_irq(&phba->hbalock);
761 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
762 spin_unlock_irq(&phba->hbalock);
763 lpfc_offline_prep(phba);
764
765 lpfc_offline(phba);
766 lpfc_reset_barrier(phba);
767 lpfc_sli_brdreset(phba);
768 lpfc_hba_down_post(phba);
769 lpfc_sli_brdready(phba, HS_MBRDY);
770 lpfc_unblock_mgmt_io(phba);
771 phba->link_state = LPFC_HBA_ERROR;
772 return;
773}
774
e59058c4
JS
775/**
776 * lpfc_handle_eratt: The HBA hardware error handler.
777 * @phba: pointer to lpfc hba data structure.
778 *
779 * This routine is invoked to handle the following HBA hardware error
780 * conditions:
781 * 1 - HBA error attention interrupt
782 * 2 - DMA ring index out of range
783 * 3 - Mailbox command came back as unknown
784 **/
dea3101e 785void
2e0fef85 786lpfc_handle_eratt(struct lpfc_hba *phba)
dea3101e 787{
2e0fef85 788 struct lpfc_vport *vport = phba->pport;
2e0fef85 789 struct lpfc_sli *psli = &phba->sli;
dea3101e 790 struct lpfc_sli_ring *pring;
d2873e4c 791 uint32_t event_data;
57127f15
JS
792 unsigned long temperature;
793 struct temp_event temp_event_data;
92d7f7b0 794 struct Scsi_Host *shost;
2e0fef85 795
8d63f375
LV
796 /* If the pci channel is offline, ignore possible errors,
797 * since we cannot communicate with the pci card anyway. */
798 if (pci_channel_offline(phba->pcidev))
799 return;
13815c83
JS
800 /* If resets are disabled then leave the HBA alone and return */
801 if (!phba->cfg_enable_hba_reset)
802 return;
dea3101e 803
97eab634 804 if (phba->work_hs & HS_FFER6) {
dea3101e 805 /* Re-establishing Link */
806 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
e8b62011 807 "1301 Re-establishing Link "
dea3101e 808 "Data: x%x x%x x%x\n",
e8b62011 809 phba->work_hs,
dea3101e 810 phba->work_status[0], phba->work_status[1]);
58da1ffb 811
92d7f7b0 812 spin_lock_irq(&phba->hbalock);
9290831f 813 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
92d7f7b0 814 spin_unlock_irq(&phba->hbalock);
dea3101e 815
816 /*
817 * Firmware stops when it triggled erratt with HS_FFER6.
818 * That could cause the I/Os dropped by the firmware.
819 * Error iocb (I/O) on txcmplq and let the SCSI layer
820 * retry it after re-establishing link.
821 */
822 pring = &psli->ring[psli->fcp_ring];
823 lpfc_sli_abort_iocb_ring(phba, pring);
824
dea3101e 825 /*
826 * There was a firmware error. Take the hba offline and then
827 * attempt to restart it.
828 */
46fa311e 829 lpfc_offline_prep(phba);
dea3101e 830 lpfc_offline(phba);
41415862 831 lpfc_sli_brdrestart(phba);
dea3101e 832 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
46fa311e 833 lpfc_unblock_mgmt_io(phba);
dea3101e 834 return;
835 }
46fa311e 836 lpfc_unblock_mgmt_io(phba);
57127f15
JS
837 } else if (phba->work_hs & HS_CRIT_TEMP) {
838 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
839 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
840 temp_event_data.event_code = LPFC_CRIT_TEMP;
841 temp_event_data.data = (uint32_t)temperature;
842
843 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
844 "0459 Adapter maximum temperature exceeded "
845 "(%ld), taking this port offline "
846 "Data: x%x x%x x%x\n",
847 temperature, phba->work_hs,
848 phba->work_status[0], phba->work_status[1]);
849
850 shost = lpfc_shost_from_vport(phba->pport);
851 fc_host_post_vendor_event(shost, fc_get_event_number(),
852 sizeof(temp_event_data),
853 (char *) &temp_event_data,
854 SCSI_NL_VID_TYPE_PCI
855 | PCI_VENDOR_ID_EMULEX);
856
7af67051 857 spin_lock_irq(&phba->hbalock);
7af67051
JS
858 phba->over_temp_state = HBA_OVER_TEMP;
859 spin_unlock_irq(&phba->hbalock);
09372820 860 lpfc_offline_eratt(phba);
57127f15 861
dea3101e 862 } else {
863 /* The if clause above forces this code path when the status
864 * failure is a value other than FFER6. Do not call the offline
865 * twice. This is the adapter hardware error path.
866 */
867 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 868 "0457 Adapter Hardware Error "
dea3101e 869 "Data: x%x x%x x%x\n",
e8b62011 870 phba->work_hs,
dea3101e 871 phba->work_status[0], phba->work_status[1]);
872
d2873e4c 873 event_data = FC_REG_DUMP_EVENT;
92d7f7b0 874 shost = lpfc_shost_from_vport(vport);
2e0fef85 875 fc_host_post_vendor_event(shost, fc_get_event_number(),
d2873e4c
JS
876 sizeof(event_data), (char *) &event_data,
877 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
878
09372820 879 lpfc_offline_eratt(phba);
dea3101e 880 }
881}
882
e59058c4
JS
883/**
884 * lpfc_handle_latt: The HBA link event handler.
885 * @phba: pointer to lpfc hba data structure.
886 *
887 * This routine is invoked from the worker thread to handle a HBA host
888 * attention link event.
889 **/
dea3101e 890void
2e0fef85 891lpfc_handle_latt(struct lpfc_hba *phba)
dea3101e 892{
2e0fef85
JS
893 struct lpfc_vport *vport = phba->pport;
894 struct lpfc_sli *psli = &phba->sli;
dea3101e 895 LPFC_MBOXQ_t *pmb;
896 volatile uint32_t control;
897 struct lpfc_dmabuf *mp;
09372820 898 int rc = 0;
dea3101e 899
900 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
09372820
JS
901 if (!pmb) {
902 rc = 1;
dea3101e 903 goto lpfc_handle_latt_err_exit;
09372820 904 }
dea3101e 905
906 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
09372820
JS
907 if (!mp) {
908 rc = 2;
dea3101e 909 goto lpfc_handle_latt_free_pmb;
09372820 910 }
dea3101e 911
912 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
09372820
JS
913 if (!mp->virt) {
914 rc = 3;
dea3101e 915 goto lpfc_handle_latt_free_mp;
09372820 916 }
dea3101e 917
6281bfe0 918 /* Cleanup any outstanding ELS commands */
549e55cd 919 lpfc_els_flush_all_cmd(phba);
dea3101e 920
921 psli->slistat.link_event++;
922 lpfc_read_la(phba, pmb, mp);
923 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_la;
2e0fef85 924 pmb->vport = vport;
0d2b6b83
JS
925 /* Block ELS IOCBs until we have processed this mbox command */
926 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
0b727fea 927 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
09372820
JS
928 if (rc == MBX_NOT_FINISHED) {
929 rc = 4;
14691150 930 goto lpfc_handle_latt_free_mbuf;
09372820 931 }
dea3101e 932
933 /* Clear Link Attention in HA REG */
2e0fef85 934 spin_lock_irq(&phba->hbalock);
dea3101e 935 writel(HA_LATT, phba->HAregaddr);
936 readl(phba->HAregaddr); /* flush */
2e0fef85 937 spin_unlock_irq(&phba->hbalock);
dea3101e 938
939 return;
940
14691150 941lpfc_handle_latt_free_mbuf:
0d2b6b83 942 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
14691150 943 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea3101e 944lpfc_handle_latt_free_mp:
945 kfree(mp);
946lpfc_handle_latt_free_pmb:
1dcb58e5 947 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 948lpfc_handle_latt_err_exit:
949 /* Enable Link attention interrupts */
2e0fef85 950 spin_lock_irq(&phba->hbalock);
dea3101e 951 psli->sli_flag |= LPFC_PROCESS_LA;
952 control = readl(phba->HCregaddr);
953 control |= HC_LAINT_ENA;
954 writel(control, phba->HCregaddr);
955 readl(phba->HCregaddr); /* flush */
956
957 /* Clear Link Attention in HA REG */
958 writel(HA_LATT, phba->HAregaddr);
959 readl(phba->HAregaddr); /* flush */
2e0fef85 960 spin_unlock_irq(&phba->hbalock);
dea3101e 961 lpfc_linkdown(phba);
2e0fef85 962 phba->link_state = LPFC_HBA_ERROR;
dea3101e 963
09372820
JS
964 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
965 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea3101e 966
967 return;
968}
969
e59058c4
JS
970/**
971 * lpfc_parse_vpd: Parse VPD (Vital Product Data).
972 * @phba: pointer to lpfc hba data structure.
973 * @vpd: pointer to the vital product data.
974 * @len: length of the vital product data in bytes.
975 *
976 * This routine parses the Vital Product Data (VPD). The VPD is treated as
977 * an array of characters. In this routine, the ModelName, ProgramType, and
978 * ModelDesc, etc. fields of the phba data structure will be populated.
979 *
980 * Return codes
981 * 0 - pointer to the VPD passed in is NULL
982 * 1 - success
983 **/
dea3101e 984static int
2e0fef85 985lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea3101e 986{
987 uint8_t lenlo, lenhi;
07da60c1 988 int Length;
dea3101e 989 int i, j;
990 int finished = 0;
991 int index = 0;
992
993 if (!vpd)
994 return 0;
995
996 /* Vital Product */
ed957684 997 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011 998 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea3101e 999 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1000 (uint32_t) vpd[3]);
74b72a59 1001 while (!finished && (index < (len - 4))) {
dea3101e 1002 switch (vpd[index]) {
1003 case 0x82:
74b72a59 1004 case 0x91:
dea3101e 1005 index += 1;
1006 lenlo = vpd[index];
1007 index += 1;
1008 lenhi = vpd[index];
1009 index += 1;
1010 i = ((((unsigned short)lenhi) << 8) + lenlo);
1011 index += i;
1012 break;
1013 case 0x90:
1014 index += 1;
1015 lenlo = vpd[index];
1016 index += 1;
1017 lenhi = vpd[index];
1018 index += 1;
1019 Length = ((((unsigned short)lenhi) << 8) + lenlo);
74b72a59
JW
1020 if (Length > len - index)
1021 Length = len - index;
dea3101e 1022 while (Length > 0) {
1023 /* Look for Serial Number */
1024 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1025 index += 2;
1026 i = vpd[index];
1027 index += 1;
1028 j = 0;
1029 Length -= (3+i);
1030 while(i--) {
1031 phba->SerialNumber[j++] = vpd[index++];
1032 if (j == 31)
1033 break;
1034 }
1035 phba->SerialNumber[j] = 0;
1036 continue;
1037 }
1038 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1039 phba->vpd_flag |= VPD_MODEL_DESC;
1040 index += 2;
1041 i = vpd[index];
1042 index += 1;
1043 j = 0;
1044 Length -= (3+i);
1045 while(i--) {
1046 phba->ModelDesc[j++] = vpd[index++];
1047 if (j == 255)
1048 break;
1049 }
1050 phba->ModelDesc[j] = 0;
1051 continue;
1052 }
1053 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1054 phba->vpd_flag |= VPD_MODEL_NAME;
1055 index += 2;
1056 i = vpd[index];
1057 index += 1;
1058 j = 0;
1059 Length -= (3+i);
1060 while(i--) {
1061 phba->ModelName[j++] = vpd[index++];
1062 if (j == 79)
1063 break;
1064 }
1065 phba->ModelName[j] = 0;
1066 continue;
1067 }
1068 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1069 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1070 index += 2;
1071 i = vpd[index];
1072 index += 1;
1073 j = 0;
1074 Length -= (3+i);
1075 while(i--) {
1076 phba->ProgramType[j++] = vpd[index++];
1077 if (j == 255)
1078 break;
1079 }
1080 phba->ProgramType[j] = 0;
1081 continue;
1082 }
1083 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1084 phba->vpd_flag |= VPD_PORT;
1085 index += 2;
1086 i = vpd[index];
1087 index += 1;
1088 j = 0;
1089 Length -= (3+i);
1090 while(i--) {
1091 phba->Port[j++] = vpd[index++];
1092 if (j == 19)
1093 break;
1094 }
1095 phba->Port[j] = 0;
1096 continue;
1097 }
1098 else {
1099 index += 2;
1100 i = vpd[index];
1101 index += 1;
1102 index += i;
1103 Length -= (3 + i);
1104 }
1105 }
1106 finished = 0;
1107 break;
1108 case 0x78:
1109 finished = 1;
1110 break;
1111 default:
1112 index ++;
1113 break;
1114 }
74b72a59 1115 }
dea3101e 1116
1117 return(1);
1118}
1119
e59058c4
JS
1120/**
1121 * lpfc_get_hba_model_desc: Retrieve HBA device model name and description.
1122 * @phba: pointer to lpfc hba data structure.
1123 * @mdp: pointer to the data structure to hold the derived model name.
1124 * @descp: pointer to the data structure to hold the derived description.
1125 *
1126 * This routine retrieves HBA's description based on its registered PCI device
1127 * ID. The @descp passed into this function points to an array of 256 chars. It
1128 * shall be returned with the model name, maximum speed, and the host bus type.
1129 * The @mdp passed into this function points to an array of 80 chars. When the
1130 * function returns, the @mdp will be filled with the model name.
1131 **/
dea3101e 1132static void
2e0fef85 1133lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea3101e 1134{
1135 lpfc_vpd_t *vp;
fefcb2b6 1136 uint16_t dev_id = phba->pcidev->device;
74b72a59 1137 int max_speed;
74b72a59
JW
1138 struct {
1139 char * name;
1140 int max_speed;
74b72a59 1141 char * bus;
18a3b596 1142 } m = {"<Unknown>", 0, ""};
74b72a59
JW
1143
1144 if (mdp && mdp[0] != '\0'
1145 && descp && descp[0] != '\0')
1146 return;
1147
1148 if (phba->lmt & LMT_10Gb)
1149 max_speed = 10;
1150 else if (phba->lmt & LMT_8Gb)
1151 max_speed = 8;
1152 else if (phba->lmt & LMT_4Gb)
1153 max_speed = 4;
1154 else if (phba->lmt & LMT_2Gb)
1155 max_speed = 2;
1156 else
1157 max_speed = 1;
dea3101e 1158
1159 vp = &phba->vpd;
dea3101e 1160
e4adb204 1161 switch (dev_id) {
06325e74 1162 case PCI_DEVICE_ID_FIREFLY:
18a3b596 1163 m = (typeof(m)){"LP6000", max_speed, "PCI"};
06325e74 1164 break;
dea3101e 1165 case PCI_DEVICE_ID_SUPERFLY:
1166 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
18a3b596 1167 m = (typeof(m)){"LP7000", max_speed, "PCI"};
dea3101e 1168 else
18a3b596 1169 m = (typeof(m)){"LP7000E", max_speed, "PCI"};
dea3101e 1170 break;
1171 case PCI_DEVICE_ID_DRAGONFLY:
18a3b596 1172 m = (typeof(m)){"LP8000", max_speed, "PCI"};
dea3101e 1173 break;
1174 case PCI_DEVICE_ID_CENTAUR:
1175 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
18a3b596 1176 m = (typeof(m)){"LP9002", max_speed, "PCI"};
dea3101e 1177 else
18a3b596 1178 m = (typeof(m)){"LP9000", max_speed, "PCI"};
dea3101e 1179 break;
1180 case PCI_DEVICE_ID_RFLY:
18a3b596 1181 m = (typeof(m)){"LP952", max_speed, "PCI"};
dea3101e 1182 break;
1183 case PCI_DEVICE_ID_PEGASUS:
18a3b596 1184 m = (typeof(m)){"LP9802", max_speed, "PCI-X"};
dea3101e 1185 break;
1186 case PCI_DEVICE_ID_THOR:
18a3b596 1187 m = (typeof(m)){"LP10000", max_speed, "PCI-X"};
dea3101e 1188 break;
1189 case PCI_DEVICE_ID_VIPER:
18a3b596 1190 m = (typeof(m)){"LPX1000", max_speed, "PCI-X"};
dea3101e 1191 break;
1192 case PCI_DEVICE_ID_PFLY:
18a3b596 1193 m = (typeof(m)){"LP982", max_speed, "PCI-X"};
dea3101e 1194 break;
1195 case PCI_DEVICE_ID_TFLY:
18a3b596 1196 m = (typeof(m)){"LP1050", max_speed, "PCI-X"};
dea3101e 1197 break;
1198 case PCI_DEVICE_ID_HELIOS:
18a3b596 1199 m = (typeof(m)){"LP11000", max_speed, "PCI-X2"};
dea3101e 1200 break;
e4adb204 1201 case PCI_DEVICE_ID_HELIOS_SCSP:
18a3b596 1202 m = (typeof(m)){"LP11000-SP", max_speed, "PCI-X2"};
e4adb204
JSEC
1203 break;
1204 case PCI_DEVICE_ID_HELIOS_DCSP:
18a3b596 1205 m = (typeof(m)){"LP11002-SP", max_speed, "PCI-X2"};
e4adb204
JSEC
1206 break;
1207 case PCI_DEVICE_ID_NEPTUNE:
18a3b596 1208 m = (typeof(m)){"LPe1000", max_speed, "PCIe"};
e4adb204
JSEC
1209 break;
1210 case PCI_DEVICE_ID_NEPTUNE_SCSP:
18a3b596 1211 m = (typeof(m)){"LPe1000-SP", max_speed, "PCIe"};
e4adb204
JSEC
1212 break;
1213 case PCI_DEVICE_ID_NEPTUNE_DCSP:
18a3b596 1214 m = (typeof(m)){"LPe1002-SP", max_speed, "PCIe"};
e4adb204 1215 break;
dea3101e 1216 case PCI_DEVICE_ID_BMID:
18a3b596 1217 m = (typeof(m)){"LP1150", max_speed, "PCI-X2"};
dea3101e 1218 break;
1219 case PCI_DEVICE_ID_BSMB:
18a3b596 1220 m = (typeof(m)){"LP111", max_speed, "PCI-X2"};
dea3101e 1221 break;
1222 case PCI_DEVICE_ID_ZEPHYR:
18a3b596 1223 m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
dea3101e 1224 break;
e4adb204 1225 case PCI_DEVICE_ID_ZEPHYR_SCSP:
18a3b596 1226 m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
e4adb204
JSEC
1227 break;
1228 case PCI_DEVICE_ID_ZEPHYR_DCSP:
18a3b596 1229 m = (typeof(m)){"LPe11002-SP", max_speed, "PCIe"};
e4adb204 1230 break;
dea3101e 1231 case PCI_DEVICE_ID_ZMID:
18a3b596 1232 m = (typeof(m)){"LPe1150", max_speed, "PCIe"};
dea3101e 1233 break;
1234 case PCI_DEVICE_ID_ZSMB:
18a3b596 1235 m = (typeof(m)){"LPe111", max_speed, "PCIe"};
dea3101e 1236 break;
1237 case PCI_DEVICE_ID_LP101:
18a3b596 1238 m = (typeof(m)){"LP101", max_speed, "PCI-X"};
dea3101e 1239 break;
1240 case PCI_DEVICE_ID_LP10000S:
18a3b596 1241 m = (typeof(m)){"LP10000-S", max_speed, "PCI"};
06325e74 1242 break;
e4adb204 1243 case PCI_DEVICE_ID_LP11000S:
18a3b596
JS
1244 m = (typeof(m)){"LP11000-S", max_speed,
1245 "PCI-X2"};
1246 break;
e4adb204 1247 case PCI_DEVICE_ID_LPE11000S:
18a3b596
JS
1248 m = (typeof(m)){"LPe11000-S", max_speed,
1249 "PCIe"};
5cc36b3c 1250 break;
b87eab38
JS
1251 case PCI_DEVICE_ID_SAT:
1252 m = (typeof(m)){"LPe12000", max_speed, "PCIe"};
1253 break;
1254 case PCI_DEVICE_ID_SAT_MID:
1255 m = (typeof(m)){"LPe1250", max_speed, "PCIe"};
1256 break;
1257 case PCI_DEVICE_ID_SAT_SMB:
1258 m = (typeof(m)){"LPe121", max_speed, "PCIe"};
1259 break;
1260 case PCI_DEVICE_ID_SAT_DCSP:
1261 m = (typeof(m)){"LPe12002-SP", max_speed, "PCIe"};
1262 break;
1263 case PCI_DEVICE_ID_SAT_SCSP:
1264 m = (typeof(m)){"LPe12000-SP", max_speed, "PCIe"};
1265 break;
1266 case PCI_DEVICE_ID_SAT_S:
1267 m = (typeof(m)){"LPe12000-S", max_speed, "PCIe"};
1268 break;
5cc36b3c 1269 default:
041976fb 1270 m = (typeof(m)){ NULL };
e4adb204 1271 break;
dea3101e 1272 }
74b72a59
JW
1273
1274 if (mdp && mdp[0] == '\0')
1275 snprintf(mdp, 79,"%s", m.name);
1276 if (descp && descp[0] == '\0')
1277 snprintf(descp, 255,
18a3b596
JS
1278 "Emulex %s %dGb %s Fibre Channel Adapter",
1279 m.name, m.max_speed, m.bus);
dea3101e 1280}
1281
e59058c4
JS
1282/**
1283 * lpfc_post_buffer: Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring.
1284 * @phba: pointer to lpfc hba data structure.
1285 * @pring: pointer to a IOCB ring.
1286 * @cnt: the number of IOCBs to be posted to the IOCB ring.
1287 *
1288 * This routine posts a given number of IOCBs with the associated DMA buffer
1289 * descriptors specified by the cnt argument to the given IOCB ring.
1290 *
1291 * Return codes
1292 * The number of IOCBs NOT able to be posted to the IOCB ring.
1293 **/
dea3101e 1294int
495a714c 1295lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea3101e 1296{
1297 IOCB_t *icmd;
0bd4ca25 1298 struct lpfc_iocbq *iocb;
dea3101e 1299 struct lpfc_dmabuf *mp1, *mp2;
1300
1301 cnt += pring->missbufcnt;
1302
1303 /* While there are buffers to post */
1304 while (cnt > 0) {
1305 /* Allocate buffer for command iocb */
0bd4ca25 1306 iocb = lpfc_sli_get_iocbq(phba);
dea3101e 1307 if (iocb == NULL) {
1308 pring->missbufcnt = cnt;
1309 return cnt;
1310 }
dea3101e 1311 icmd = &iocb->iocb;
1312
1313 /* 2 buffers can be posted per command */
1314 /* Allocate buffer to post */
1315 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1316 if (mp1)
98c9ea5c
JS
1317 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
1318 if (!mp1 || !mp1->virt) {
c9475cb0 1319 kfree(mp1);
604a3e30 1320 lpfc_sli_release_iocbq(phba, iocb);
dea3101e 1321 pring->missbufcnt = cnt;
1322 return cnt;
1323 }
1324
1325 INIT_LIST_HEAD(&mp1->list);
1326 /* Allocate buffer to post */
1327 if (cnt > 1) {
1328 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1329 if (mp2)
1330 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
1331 &mp2->phys);
98c9ea5c 1332 if (!mp2 || !mp2->virt) {
c9475cb0 1333 kfree(mp2);
dea3101e 1334 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1335 kfree(mp1);
604a3e30 1336 lpfc_sli_release_iocbq(phba, iocb);
dea3101e 1337 pring->missbufcnt = cnt;
1338 return cnt;
1339 }
1340
1341 INIT_LIST_HEAD(&mp2->list);
1342 } else {
1343 mp2 = NULL;
1344 }
1345
1346 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
1347 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
1348 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
1349 icmd->ulpBdeCount = 1;
1350 cnt--;
1351 if (mp2) {
1352 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
1353 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
1354 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
1355 cnt--;
1356 icmd->ulpBdeCount = 2;
1357 }
1358
1359 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
1360 icmd->ulpLe = 1;
1361
dea3101e 1362 if (lpfc_sli_issue_iocb(phba, pring, iocb, 0) == IOCB_ERROR) {
1363 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1364 kfree(mp1);
1365 cnt++;
1366 if (mp2) {
1367 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
1368 kfree(mp2);
1369 cnt++;
1370 }
604a3e30 1371 lpfc_sli_release_iocbq(phba, iocb);
dea3101e 1372 pring->missbufcnt = cnt;
dea3101e 1373 return cnt;
1374 }
dea3101e 1375 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
92d7f7b0 1376 if (mp2)
dea3101e 1377 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea3101e 1378 }
1379 pring->missbufcnt = 0;
1380 return 0;
1381}
1382
e59058c4
JS
1383/**
1384 * lpfc_post_rcv_buf: Post the initial receive IOCB buffers to ELS ring.
1385 * @phba: pointer to lpfc hba data structure.
1386 *
1387 * This routine posts initial receive IOCB buffers to the ELS ring. The
1388 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
1389 * set to 64 IOCBs.
1390 *
1391 * Return codes
1392 * 0 - success (currently always success)
1393 **/
dea3101e 1394static int
2e0fef85 1395lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea3101e 1396{
1397 struct lpfc_sli *psli = &phba->sli;
1398
1399 /* Ring 0, ELS / CT buffers */
495a714c 1400 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea3101e 1401 /* Ring 2 - FCP no buffers needed */
1402
1403 return 0;
1404}
1405
1406#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
1407
e59058c4
JS
1408/**
1409 * lpfc_sha_init: Set up initial array of hash table entries.
1410 * @HashResultPointer: pointer to an array as hash table.
1411 *
1412 * This routine sets up the initial values to the array of hash table entries
1413 * for the LC HBAs.
1414 **/
dea3101e 1415static void
1416lpfc_sha_init(uint32_t * HashResultPointer)
1417{
1418 HashResultPointer[0] = 0x67452301;
1419 HashResultPointer[1] = 0xEFCDAB89;
1420 HashResultPointer[2] = 0x98BADCFE;
1421 HashResultPointer[3] = 0x10325476;
1422 HashResultPointer[4] = 0xC3D2E1F0;
1423}
1424
e59058c4
JS
1425/**
1426 * lpfc_sha_iterate: Iterate initial hash table with the working hash table.
1427 * @HashResultPointer: pointer to an initial/result hash table.
1428 * @HashWorkingPointer: pointer to an working hash table.
1429 *
1430 * This routine iterates an initial hash table pointed by @HashResultPointer
1431 * with the values from the working hash table pointeed by @HashWorkingPointer.
1432 * The results are putting back to the initial hash table, returned through
1433 * the @HashResultPointer as the result hash table.
1434 **/
dea3101e 1435static void
1436lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
1437{
1438 int t;
1439 uint32_t TEMP;
1440 uint32_t A, B, C, D, E;
1441 t = 16;
1442 do {
1443 HashWorkingPointer[t] =
1444 S(1,
1445 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
1446 8] ^
1447 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
1448 } while (++t <= 79);
1449 t = 0;
1450 A = HashResultPointer[0];
1451 B = HashResultPointer[1];
1452 C = HashResultPointer[2];
1453 D = HashResultPointer[3];
1454 E = HashResultPointer[4];
1455
1456 do {
1457 if (t < 20) {
1458 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
1459 } else if (t < 40) {
1460 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
1461 } else if (t < 60) {
1462 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
1463 } else {
1464 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
1465 }
1466 TEMP += S(5, A) + E + HashWorkingPointer[t];
1467 E = D;
1468 D = C;
1469 C = S(30, B);
1470 B = A;
1471 A = TEMP;
1472 } while (++t <= 79);
1473
1474 HashResultPointer[0] += A;
1475 HashResultPointer[1] += B;
1476 HashResultPointer[2] += C;
1477 HashResultPointer[3] += D;
1478 HashResultPointer[4] += E;
1479
1480}
1481
e59058c4
JS
1482/**
1483 * lpfc_challenge_key: Create challenge key based on WWPN of the HBA.
1484 * @RandomChallenge: pointer to the entry of host challenge random number array.
1485 * @HashWorking: pointer to the entry of the working hash array.
1486 *
1487 * This routine calculates the working hash array referred by @HashWorking
1488 * from the challenge random numbers associated with the host, referred by
1489 * @RandomChallenge. The result is put into the entry of the working hash
1490 * array and returned by reference through @HashWorking.
1491 **/
dea3101e 1492static void
1493lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
1494{
1495 *HashWorking = (*RandomChallenge ^ *HashWorking);
1496}
1497
e59058c4
JS
1498/**
1499 * lpfc_hba_init: Perform special handling for LC HBA initialization.
1500 * @phba: pointer to lpfc hba data structure.
1501 * @hbainit: pointer to an array of unsigned 32-bit integers.
1502 *
1503 * This routine performs the special handling for LC HBA initialization.
1504 **/
dea3101e 1505void
1506lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
1507{
1508 int t;
1509 uint32_t *HashWorking;
2e0fef85 1510 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea3101e 1511
bbfbbbc1 1512 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea3101e 1513 if (!HashWorking)
1514 return;
1515
dea3101e 1516 HashWorking[0] = HashWorking[78] = *pwwnn++;
1517 HashWorking[1] = HashWorking[79] = *pwwnn;
1518
1519 for (t = 0; t < 7; t++)
1520 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
1521
1522 lpfc_sha_init(hbainit);
1523 lpfc_sha_iterate(hbainit, HashWorking);
1524 kfree(HashWorking);
1525}
1526
e59058c4
JS
1527/**
1528 * lpfc_cleanup: Performs vport cleanups before deleting a vport.
1529 * @vport: pointer to a virtual N_Port data structure.
1530 *
1531 * This routine performs the necessary cleanups before deleting the @vport.
1532 * It invokes the discovery state machine to perform necessary state
1533 * transitions and to release the ndlps associated with the @vport. Note,
1534 * the physical port is treated as @vport 0.
1535 **/
87af33fe 1536void
2e0fef85 1537lpfc_cleanup(struct lpfc_vport *vport)
dea3101e 1538{
87af33fe 1539 struct lpfc_hba *phba = vport->phba;
dea3101e 1540 struct lpfc_nodelist *ndlp, *next_ndlp;
a8adb832 1541 int i = 0;
dea3101e 1542
87af33fe
JS
1543 if (phba->link_state > LPFC_LINK_DOWN)
1544 lpfc_port_link_failure(vport);
1545
1546 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
e47c9093
JS
1547 if (!NLP_CHK_NODE_ACT(ndlp)) {
1548 ndlp = lpfc_enable_node(vport, ndlp,
1549 NLP_STE_UNUSED_NODE);
1550 if (!ndlp)
1551 continue;
1552 spin_lock_irq(&phba->ndlp_lock);
1553 NLP_SET_FREE_REQ(ndlp);
1554 spin_unlock_irq(&phba->ndlp_lock);
1555 /* Trigger the release of the ndlp memory */
1556 lpfc_nlp_put(ndlp);
1557 continue;
1558 }
1559 spin_lock_irq(&phba->ndlp_lock);
1560 if (NLP_CHK_FREE_REQ(ndlp)) {
1561 /* The ndlp should not be in memory free mode already */
1562 spin_unlock_irq(&phba->ndlp_lock);
1563 continue;
1564 } else
1565 /* Indicate request for freeing ndlp memory */
1566 NLP_SET_FREE_REQ(ndlp);
1567 spin_unlock_irq(&phba->ndlp_lock);
1568
58da1ffb
JS
1569 if (vport->port_type != LPFC_PHYSICAL_PORT &&
1570 ndlp->nlp_DID == Fabric_DID) {
1571 /* Just free up ndlp with Fabric_DID for vports */
1572 lpfc_nlp_put(ndlp);
1573 continue;
1574 }
1575
87af33fe
JS
1576 if (ndlp->nlp_type & NLP_FABRIC)
1577 lpfc_disc_state_machine(vport, ndlp, NULL,
1578 NLP_EVT_DEVICE_RECOVERY);
e47c9093 1579
87af33fe
JS
1580 lpfc_disc_state_machine(vport, ndlp, NULL,
1581 NLP_EVT_DEVICE_RM);
495a714c 1582
87af33fe
JS
1583 }
1584
a8adb832
JS
1585 /* At this point, ALL ndlp's should be gone
1586 * because of the previous NLP_EVT_DEVICE_RM.
1587 * Lets wait for this to happen, if needed.
1588 */
87af33fe
JS
1589 while (!list_empty(&vport->fc_nodes)) {
1590
a8adb832 1591 if (i++ > 3000) {
87af33fe 1592 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
a8adb832 1593 "0233 Nodelist not empty\n");
e47c9093
JS
1594 list_for_each_entry_safe(ndlp, next_ndlp,
1595 &vport->fc_nodes, nlp_listp) {
1596 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
1597 LOG_NODE,
1598 "0282: did:x%x ndlp:x%p "
1599 "usgmap:x%x refcnt:%d\n",
1600 ndlp->nlp_DID, (void *)ndlp,
1601 ndlp->nlp_usg_map,
1602 atomic_read(
1603 &ndlp->kref.refcount));
1604 }
a8adb832 1605 break;
87af33fe 1606 }
a8adb832
JS
1607
1608 /* Wait for any activity on ndlps to settle */
1609 msleep(10);
87af33fe 1610 }
dea3101e 1611 return;
1612}
1613
e59058c4
JS
1614/**
1615 * lpfc_stop_vport_timers: Stop all the timers associated with a vport.
1616 * @vport: pointer to a virtual N_Port data structure.
1617 *
1618 * This routine stops all the timers associated with a @vport. This function
1619 * is invoked before disabling or deleting a @vport. Note that the physical
1620 * port is treated as @vport 0.
1621 **/
92d7f7b0
JS
1622void
1623lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea3101e 1624{
92d7f7b0
JS
1625 del_timer_sync(&vport->els_tmofunc);
1626 del_timer_sync(&vport->fc_fdmitmo);
1627 lpfc_can_disctmo(vport);
1628 return;
dea3101e 1629}
1630
e59058c4
JS
1631/**
1632 * lpfc_stop_phba_timers: Stop all the timers associated with an HBA.
1633 * @phba: pointer to lpfc hba data structure.
1634 *
1635 * This routine stops all the timers associated with a HBA. This function is
1636 * invoked before either putting a HBA offline or unloading the driver.
1637 **/
2e0fef85 1638static void
92d7f7b0 1639lpfc_stop_phba_timers(struct lpfc_hba *phba)
dea3101e 1640{
875fbdfe 1641 del_timer_sync(&phba->fcp_poll_timer);
51ef4c26 1642 lpfc_stop_vport_timers(phba->pport);
2e0fef85 1643 del_timer_sync(&phba->sli.mbox_tmo);
92d7f7b0 1644 del_timer_sync(&phba->fabric_block_timer);
858c9f6c
JS
1645 phba->hb_outstanding = 0;
1646 del_timer_sync(&phba->hb_tmofunc);
2e0fef85 1647 return;
dea3101e 1648}
1649
e59058c4
JS
1650/**
1651 * lpfc_block_mgmt_io: Mark a HBA's management interface as blocked.
1652 * @phba: pointer to lpfc hba data structure.
1653 *
1654 * This routine marks a HBA's management interface as blocked. Once the HBA's
1655 * management interface is marked as blocked, all the user space access to
1656 * the HBA, whether they are from sysfs interface or libdfc interface will
1657 * all be blocked. The HBA is set to block the management interface when the
1658 * driver prepares the HBA interface for online or offline.
1659 **/
a6ababd2
AB
1660static void
1661lpfc_block_mgmt_io(struct lpfc_hba * phba)
1662{
1663 unsigned long iflag;
1664
1665 spin_lock_irqsave(&phba->hbalock, iflag);
1666 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
1667 spin_unlock_irqrestore(&phba->hbalock, iflag);
1668}
1669
e59058c4
JS
1670/**
1671 * lpfc_online: Initialize and bring a HBA online.
1672 * @phba: pointer to lpfc hba data structure.
1673 *
1674 * This routine initializes the HBA and brings a HBA online. During this
1675 * process, the management interface is blocked to prevent user space access
1676 * to the HBA interfering with the driver initialization.
1677 *
1678 * Return codes
1679 * 0 - successful
1680 * 1 - failed
1681 **/
dea3101e 1682int
2e0fef85 1683lpfc_online(struct lpfc_hba *phba)
dea3101e 1684{
2e0fef85 1685 struct lpfc_vport *vport = phba->pport;
549e55cd
JS
1686 struct lpfc_vport **vports;
1687 int i;
2e0fef85 1688
dea3101e 1689 if (!phba)
1690 return 0;
1691
2e0fef85 1692 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea3101e 1693 return 0;
1694
ed957684 1695 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
e8b62011 1696 "0458 Bring Adapter online\n");
dea3101e 1697
46fa311e
JS
1698 lpfc_block_mgmt_io(phba);
1699
1700 if (!lpfc_sli_queue_setup(phba)) {
1701 lpfc_unblock_mgmt_io(phba);
dea3101e 1702 return 1;
46fa311e 1703 }
dea3101e 1704
46fa311e
JS
1705 if (lpfc_sli_hba_setup(phba)) { /* Initialize the HBA */
1706 lpfc_unblock_mgmt_io(phba);
dea3101e 1707 return 1;
46fa311e 1708 }
dea3101e 1709
549e55cd
JS
1710 vports = lpfc_create_vport_work_array(phba);
1711 if (vports != NULL)
09372820 1712 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
549e55cd
JS
1713 struct Scsi_Host *shost;
1714 shost = lpfc_shost_from_vport(vports[i]);
1715 spin_lock_irq(shost->host_lock);
1716 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
1717 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
1718 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
1719 spin_unlock_irq(shost->host_lock);
1720 }
09372820 1721 lpfc_destroy_vport_work_array(phba, vports);
dea3101e 1722
46fa311e 1723 lpfc_unblock_mgmt_io(phba);
dea3101e 1724 return 0;
1725}
1726
e59058c4
JS
1727/**
1728 * lpfc_unblock_mgmt_io: Mark a HBA's management interface to be not blocked.
1729 * @phba: pointer to lpfc hba data structure.
1730 *
1731 * This routine marks a HBA's management interface as not blocked. Once the
1732 * HBA's management interface is marked as not blocked, all the user space
1733 * access to the HBA, whether they are from sysfs interface or libdfc
1734 * interface will be allowed. The HBA is set to block the management interface
1735 * when the driver prepares the HBA interface for online or offline and then
1736 * set to unblock the management interface afterwards.
1737 **/
46fa311e
JS
1738void
1739lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
1740{
1741 unsigned long iflag;
1742
2e0fef85
JS
1743 spin_lock_irqsave(&phba->hbalock, iflag);
1744 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
1745 spin_unlock_irqrestore(&phba->hbalock, iflag);
46fa311e
JS
1746}
1747
e59058c4
JS
1748/**
1749 * lpfc_offline_prep: Prepare a HBA to be brought offline.
1750 * @phba: pointer to lpfc hba data structure.
1751 *
1752 * This routine is invoked to prepare a HBA to be brought offline. It performs
1753 * unregistration login to all the nodes on all vports and flushes the mailbox
1754 * queue to make it ready to be brought offline.
1755 **/
46fa311e
JS
1756void
1757lpfc_offline_prep(struct lpfc_hba * phba)
1758{
2e0fef85 1759 struct lpfc_vport *vport = phba->pport;
46fa311e 1760 struct lpfc_nodelist *ndlp, *next_ndlp;
87af33fe
JS
1761 struct lpfc_vport **vports;
1762 int i;
dea3101e 1763
2e0fef85 1764 if (vport->fc_flag & FC_OFFLINE_MODE)
46fa311e 1765 return;
dea3101e 1766
46fa311e 1767 lpfc_block_mgmt_io(phba);
dea3101e 1768
1769 lpfc_linkdown(phba);
1770
87af33fe
JS
1771 /* Issue an unreg_login to all nodes on all vports */
1772 vports = lpfc_create_vport_work_array(phba);
1773 if (vports != NULL) {
09372820 1774 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
87af33fe
JS
1775 struct Scsi_Host *shost;
1776
a8adb832
JS
1777 if (vports[i]->load_flag & FC_UNLOADING)
1778 continue;
87af33fe
JS
1779 shost = lpfc_shost_from_vport(vports[i]);
1780 list_for_each_entry_safe(ndlp, next_ndlp,
1781 &vports[i]->fc_nodes,
1782 nlp_listp) {
e47c9093
JS
1783 if (!NLP_CHK_NODE_ACT(ndlp))
1784 continue;
87af33fe
JS
1785 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
1786 continue;
1787 if (ndlp->nlp_type & NLP_FABRIC) {
1788 lpfc_disc_state_machine(vports[i], ndlp,
1789 NULL, NLP_EVT_DEVICE_RECOVERY);
1790 lpfc_disc_state_machine(vports[i], ndlp,
1791 NULL, NLP_EVT_DEVICE_RM);
1792 }
1793 spin_lock_irq(shost->host_lock);
1794 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
1795 spin_unlock_irq(shost->host_lock);
1796 lpfc_unreg_rpi(vports[i], ndlp);
1797 }
1798 }
1799 }
09372820 1800 lpfc_destroy_vport_work_array(phba, vports);
dea3101e 1801
46fa311e
JS
1802 lpfc_sli_flush_mbox_queue(phba);
1803}
1804
e59058c4
JS
1805/**
1806 * lpfc_offline: Bring a HBA offline.
1807 * @phba: pointer to lpfc hba data structure.
1808 *
1809 * This routine actually brings a HBA offline. It stops all the timers
1810 * associated with the HBA, brings down the SLI layer, and eventually
1811 * marks the HBA as in offline state for the upper layer protocol.
1812 **/
46fa311e 1813void
2e0fef85 1814lpfc_offline(struct lpfc_hba *phba)
46fa311e 1815{
549e55cd
JS
1816 struct Scsi_Host *shost;
1817 struct lpfc_vport **vports;
1818 int i;
46fa311e 1819
549e55cd 1820 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
46fa311e 1821 return;
688a8863 1822
dea3101e 1823 /* stop all timers associated with this hba */
92d7f7b0 1824 lpfc_stop_phba_timers(phba);
51ef4c26
JS
1825 vports = lpfc_create_vport_work_array(phba);
1826 if (vports != NULL)
09372820 1827 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++)
51ef4c26 1828 lpfc_stop_vport_timers(vports[i]);
09372820 1829 lpfc_destroy_vport_work_array(phba, vports);
92d7f7b0 1830 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
e8b62011 1831 "0460 Bring Adapter offline\n");
dea3101e 1832 /* Bring down the SLI Layer and cleanup. The HBA is offline
1833 now. */
1834 lpfc_sli_hba_down(phba);
92d7f7b0 1835 spin_lock_irq(&phba->hbalock);
7054a606 1836 phba->work_ha = 0;
92d7f7b0 1837 spin_unlock_irq(&phba->hbalock);
549e55cd
JS
1838 vports = lpfc_create_vport_work_array(phba);
1839 if (vports != NULL)
09372820 1840 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
549e55cd 1841 shost = lpfc_shost_from_vport(vports[i]);
549e55cd
JS
1842 spin_lock_irq(shost->host_lock);
1843 vports[i]->work_port_events = 0;
1844 vports[i]->fc_flag |= FC_OFFLINE_MODE;
1845 spin_unlock_irq(shost->host_lock);
1846 }
09372820 1847 lpfc_destroy_vport_work_array(phba, vports);
dea3101e 1848}
1849
e59058c4
JS
1850/**
1851 * lpfc_scsi_free: Free all the SCSI buffers and IOCBs from driver lists.
1852 * @phba: pointer to lpfc hba data structure.
1853 *
1854 * This routine is to free all the SCSI buffers and IOCBs from the driver
1855 * list back to kernel. It is called from lpfc_pci_remove_one to free
1856 * the internal resources before the device is removed from the system.
1857 *
1858 * Return codes
1859 * 0 - successful (for now, it always returns 0)
1860 **/
dea3101e 1861static int
2e0fef85 1862lpfc_scsi_free(struct lpfc_hba *phba)
dea3101e 1863{
1864 struct lpfc_scsi_buf *sb, *sb_next;
1865 struct lpfc_iocbq *io, *io_next;
1866
2e0fef85 1867 spin_lock_irq(&phba->hbalock);
dea3101e 1868 /* Release all the lpfc_scsi_bufs maintained by this host. */
1869 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
1870 list_del(&sb->list);
1871 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
92d7f7b0 1872 sb->dma_handle);
dea3101e 1873 kfree(sb);
1874 phba->total_scsi_bufs--;
1875 }
1876
1877 /* Release all the lpfc_iocbq entries maintained by this host. */
1878 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
1879 list_del(&io->list);
1880 kfree(io);
1881 phba->total_iocbq_bufs--;
1882 }
1883
2e0fef85 1884 spin_unlock_irq(&phba->hbalock);
dea3101e 1885
1886 return 0;
1887}
1888
e59058c4
JS
1889/**
1890 * lpfc_create_port: Create an FC port.
1891 * @phba: pointer to lpfc hba data structure.
1892 * @instance: a unique integer ID to this FC port.
1893 * @dev: pointer to the device data structure.
1894 *
1895 * This routine creates a FC port for the upper layer protocol. The FC port
1896 * can be created on top of either a physical port or a virtual port provided
1897 * by the HBA. This routine also allocates a SCSI host data structure (shost)
1898 * and associates the FC port created before adding the shost into the SCSI
1899 * layer.
1900 *
1901 * Return codes
1902 * @vport - pointer to the virtual N_Port data structure.
1903 * NULL - port create failed.
1904 **/
2e0fef85 1905struct lpfc_vport *
3de2a653 1906lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
47a8617c 1907{
2e0fef85
JS
1908 struct lpfc_vport *vport;
1909 struct Scsi_Host *shost;
1910 int error = 0;
47a8617c 1911
3de2a653
JS
1912 if (dev != &phba->pcidev->dev)
1913 shost = scsi_host_alloc(&lpfc_vport_template,
1914 sizeof(struct lpfc_vport));
1915 else
1916 shost = scsi_host_alloc(&lpfc_template,
1917 sizeof(struct lpfc_vport));
2e0fef85
JS
1918 if (!shost)
1919 goto out;
47a8617c 1920
2e0fef85
JS
1921 vport = (struct lpfc_vport *) shost->hostdata;
1922 vport->phba = phba;
2e0fef85 1923 vport->load_flag |= FC_LOADING;
92d7f7b0 1924 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
7f5f3d0d 1925 vport->fc_rscn_flush = 0;
47a8617c 1926
3de2a653 1927 lpfc_get_vport_cfgparam(vport);
2e0fef85
JS
1928 shost->unique_id = instance;
1929 shost->max_id = LPFC_MAX_TARGET;
3de2a653 1930 shost->max_lun = vport->cfg_max_luns;
2e0fef85
JS
1931 shost->this_id = -1;
1932 shost->max_cmd_len = 16;
47a8617c 1933 /*
2e0fef85
JS
1934 * Set initial can_queue value since 0 is no longer supported and
1935 * scsi_add_host will fail. This will be adjusted later based on the
1936 * max xri value determined in hba setup.
47a8617c 1937 */
2e0fef85 1938 shost->can_queue = phba->cfg_hba_queue_depth - 10;
3de2a653 1939 if (dev != &phba->pcidev->dev) {
92d7f7b0
JS
1940 shost->transportt = lpfc_vport_transport_template;
1941 vport->port_type = LPFC_NPIV_PORT;
1942 } else {
1943 shost->transportt = lpfc_transport_template;
1944 vport->port_type = LPFC_PHYSICAL_PORT;
1945 }
47a8617c 1946
2e0fef85
JS
1947 /* Initialize all internally managed lists. */
1948 INIT_LIST_HEAD(&vport->fc_nodes);
1949 spin_lock_init(&vport->work_port_lock);
47a8617c 1950
2e0fef85
JS
1951 init_timer(&vport->fc_disctmo);
1952 vport->fc_disctmo.function = lpfc_disc_timeout;
92d7f7b0 1953 vport->fc_disctmo.data = (unsigned long)vport;
47a8617c 1954
2e0fef85
JS
1955 init_timer(&vport->fc_fdmitmo);
1956 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
92d7f7b0 1957 vport->fc_fdmitmo.data = (unsigned long)vport;
47a8617c 1958
2e0fef85
JS
1959 init_timer(&vport->els_tmofunc);
1960 vport->els_tmofunc.function = lpfc_els_timeout;
92d7f7b0 1961 vport->els_tmofunc.data = (unsigned long)vport;
47a8617c 1962
3de2a653 1963 error = scsi_add_host(shost, dev);
2e0fef85
JS
1964 if (error)
1965 goto out_put_shost;
47a8617c 1966
549e55cd 1967 spin_lock_irq(&phba->hbalock);
2e0fef85 1968 list_add_tail(&vport->listentry, &phba->port_list);
549e55cd 1969 spin_unlock_irq(&phba->hbalock);
2e0fef85 1970 return vport;
47a8617c 1971
2e0fef85
JS
1972out_put_shost:
1973 scsi_host_put(shost);
1974out:
1975 return NULL;
47a8617c
JS
1976}
1977
e59058c4
JS
1978/**
1979 * destroy_port: Destroy an FC port.
1980 * @vport: pointer to an lpfc virtual N_Port data structure.
1981 *
1982 * This routine destroys a FC port from the upper layer protocol. All the
1983 * resources associated with the port are released.
1984 **/
2e0fef85
JS
1985void
1986destroy_port(struct lpfc_vport *vport)
47a8617c 1987{
92d7f7b0
JS
1988 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1989 struct lpfc_hba *phba = vport->phba;
47a8617c 1990
92d7f7b0 1991 kfree(vport->vname);
47a8617c 1992
858c9f6c 1993 lpfc_debugfs_terminate(vport);
92d7f7b0
JS
1994 fc_remove_host(shost);
1995 scsi_remove_host(shost);
47a8617c 1996
92d7f7b0
JS
1997 spin_lock_irq(&phba->hbalock);
1998 list_del_init(&vport->listentry);
1999 spin_unlock_irq(&phba->hbalock);
47a8617c 2000
92d7f7b0 2001 lpfc_cleanup(vport);
47a8617c 2002 return;
47a8617c
JS
2003}
2004
e59058c4
JS
2005/**
2006 * lpfc_get_instance: Get a unique integer ID.
2007 *
2008 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
2009 * uses the kernel idr facility to perform the task.
2010 *
2011 * Return codes:
2012 * instance - a unique integer ID allocated as the new instance.
2013 * -1 - lpfc get instance failed.
2014 **/
92d7f7b0
JS
2015int
2016lpfc_get_instance(void)
2017{
2018 int instance = 0;
47a8617c 2019
92d7f7b0
JS
2020 /* Assign an unused number */
2021 if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
2022 return -1;
2023 if (idr_get_new(&lpfc_hba_index, NULL, &instance))
2024 return -1;
2025 return instance;
47a8617c
JS
2026}
2027
e59058c4
JS
2028/**
2029 * lpfc_scan_finished: method for SCSI layer to detect whether scan is done.
2030 * @shost: pointer to SCSI host data structure.
2031 * @time: elapsed time of the scan in jiffies.
2032 *
2033 * This routine is called by the SCSI layer with a SCSI host to determine
2034 * whether the scan host is finished.
2035 *
2036 * Note: there is no scan_start function as adapter initialization will have
2037 * asynchronously kicked off the link initialization.
2038 *
2039 * Return codes
2040 * 0 - SCSI host scan is not over yet.
2041 * 1 - SCSI host scan is over.
2042 **/
47a8617c
JS
2043int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2044{
2e0fef85
JS
2045 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2046 struct lpfc_hba *phba = vport->phba;
858c9f6c 2047 int stat = 0;
47a8617c 2048
858c9f6c
JS
2049 spin_lock_irq(shost->host_lock);
2050
51ef4c26 2051 if (vport->load_flag & FC_UNLOADING) {
858c9f6c
JS
2052 stat = 1;
2053 goto finished;
2054 }
2e0fef85
JS
2055 if (time >= 30 * HZ) {
2056 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011
JS
2057 "0461 Scanning longer than 30 "
2058 "seconds. Continuing initialization\n");
858c9f6c 2059 stat = 1;
47a8617c 2060 goto finished;
2e0fef85
JS
2061 }
2062 if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
2063 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011
JS
2064 "0465 Link down longer than 15 "
2065 "seconds. Continuing initialization\n");
858c9f6c 2066 stat = 1;
47a8617c 2067 goto finished;
2e0fef85 2068 }
47a8617c 2069
2e0fef85 2070 if (vport->port_state != LPFC_VPORT_READY)
858c9f6c 2071 goto finished;
2e0fef85 2072 if (vport->num_disc_nodes || vport->fc_prli_sent)
858c9f6c 2073 goto finished;
2e0fef85 2074 if (vport->fc_map_cnt == 0 && time < 2 * HZ)
858c9f6c 2075 goto finished;
2e0fef85 2076 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
858c9f6c
JS
2077 goto finished;
2078
2079 stat = 1;
47a8617c
JS
2080
2081finished:
858c9f6c
JS
2082 spin_unlock_irq(shost->host_lock);
2083 return stat;
92d7f7b0 2084}
47a8617c 2085
e59058c4
JS
2086/**
2087 * lpfc_host_attrib_init: Initialize SCSI host attributes on a FC port.
2088 * @shost: pointer to SCSI host data structure.
2089 *
2090 * This routine initializes a given SCSI host attributes on a FC port. The
2091 * SCSI host can be either on top of a physical port or a virtual port.
2092 **/
92d7f7b0
JS
2093void lpfc_host_attrib_init(struct Scsi_Host *shost)
2094{
2095 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2096 struct lpfc_hba *phba = vport->phba;
47a8617c 2097 /*
2e0fef85 2098 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
47a8617c
JS
2099 */
2100
2e0fef85
JS
2101 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
2102 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
47a8617c
JS
2103 fc_host_supported_classes(shost) = FC_COS_CLASS3;
2104
2105 memset(fc_host_supported_fc4s(shost), 0,
2e0fef85 2106 sizeof(fc_host_supported_fc4s(shost)));
47a8617c
JS
2107 fc_host_supported_fc4s(shost)[2] = 1;
2108 fc_host_supported_fc4s(shost)[7] = 1;
2109
92d7f7b0
JS
2110 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
2111 sizeof fc_host_symbolic_name(shost));
47a8617c
JS
2112
2113 fc_host_supported_speeds(shost) = 0;
2114 if (phba->lmt & LMT_10Gb)
2115 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
a8adb832
JS
2116 if (phba->lmt & LMT_8Gb)
2117 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
47a8617c
JS
2118 if (phba->lmt & LMT_4Gb)
2119 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
2120 if (phba->lmt & LMT_2Gb)
2121 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
2122 if (phba->lmt & LMT_1Gb)
2123 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
2124
2125 fc_host_maxframe_size(shost) =
2e0fef85
JS
2126 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
2127 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
47a8617c
JS
2128
2129 /* This value is also unchanging */
2130 memset(fc_host_active_fc4s(shost), 0,
2e0fef85 2131 sizeof(fc_host_active_fc4s(shost)));
47a8617c
JS
2132 fc_host_active_fc4s(shost)[2] = 1;
2133 fc_host_active_fc4s(shost)[7] = 1;
2134
92d7f7b0 2135 fc_host_max_npiv_vports(shost) = phba->max_vpi;
47a8617c 2136 spin_lock_irq(shost->host_lock);
51ef4c26 2137 vport->load_flag &= ~FC_LOADING;
47a8617c 2138 spin_unlock_irq(shost->host_lock);
47a8617c 2139}
dea3101e 2140
e59058c4
JS
2141/**
2142 * lpfc_enable_msix: Enable MSI-X interrupt mode.
2143 * @phba: pointer to lpfc hba data structure.
2144 *
2145 * This routine is invoked to enable the MSI-X interrupt vectors. The kernel
2146 * function pci_enable_msix() is called to enable the MSI-X vectors. Note that
2147 * pci_enable_msix(), once invoked, enables either all or nothing, depending
2148 * on the current availability of PCI vector resources. The device driver is
2149 * responsible for calling the individual request_irq() to register each MSI-X
2150 * vector with a interrupt handler, which is done in this function. Note that
2151 * later when device is unloading, the driver should always call free_irq()
2152 * on all MSI-X vectors it has done request_irq() on before calling
2153 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
2154 * will be left with MSI-X enabled and leaks its vectors.
2155 *
2156 * Return codes
2157 * 0 - sucessful
2158 * other values - error
2159 **/
db2378e0
JS
2160static int
2161lpfc_enable_msix(struct lpfc_hba *phba)
2162{
2163 int error;
2164
2165 phba->msix_entries[0].entry = 0;
2166 phba->msix_entries[0].vector = 0;
2167
2168 error = pci_enable_msix(phba->pcidev, phba->msix_entries,
2169 ARRAY_SIZE(phba->msix_entries));
2170 if (error) {
2171 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2172 "0420 Enable MSI-X failed (%d), continuing "
2173 "with MSI\n", error);
2174 pci_disable_msix(phba->pcidev);
2175 return error;
2176 }
2177
2178 error = request_irq(phba->msix_entries[0].vector, lpfc_intr_handler, 0,
2179 LPFC_DRIVER_NAME, phba);
2180 if (error) {
2181 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2182 "0421 MSI-X request_irq failed (%d), "
2183 "continuing with MSI\n", error);
2184 pci_disable_msix(phba->pcidev);
2185 }
2186 return error;
2187}
2188
e59058c4
JS
2189/**
2190 * lpfc_disable_msix: Disable MSI-X interrupt mode.
2191 * @phba: pointer to lpfc hba data structure.
2192 *
2193 * This routine is invoked to release the MSI-X vectors and then disable the
2194 * MSI-X interrupt mode.
2195 **/
db2378e0
JS
2196static void
2197lpfc_disable_msix(struct lpfc_hba *phba)
2198{
2199 free_irq(phba->msix_entries[0].vector, phba);
2200 pci_disable_msix(phba->pcidev);
2201}
2202
e59058c4
JS
2203/**
2204 * lpfc_pci_probe_one: lpfc PCI probe func to register device to PCI subsystem.
2205 * @pdev: pointer to PCI device
2206 * @pid: pointer to PCI device identifier
2207 *
2208 * This routine is to be registered to the kernel's PCI subsystem. When an
2209 * Emulex HBA is presented in PCI bus, the kernel PCI subsystem looks at
2210 * PCI device-specific information of the device and driver to see if the
2211 * driver state that it can support this kind of device. If the match is
2212 * successful, the driver core invokes this routine. If this routine
2213 * determines it can claim the HBA, it does all the initialization that it
2214 * needs to do to handle the HBA properly.
2215 *
2216 * Return code
2217 * 0 - driver can claim the device
2218 * negative value - driver can not claim the device
2219 **/
dea3101e 2220static int __devinit
2221lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
2222{
2e0fef85
JS
2223 struct lpfc_vport *vport = NULL;
2224 struct lpfc_hba *phba;
2225 struct lpfc_sli *psli;
dea3101e 2226 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
2e0fef85 2227 struct Scsi_Host *shost = NULL;
51ef4c26 2228 void *ptr;
dea3101e 2229 unsigned long bar0map_len, bar2map_len;
98c9ea5c 2230 int error = -ENODEV, retval;
51ef4c26 2231 int i, hbq_count;
604a3e30 2232 uint16_t iotag;
8a4df120 2233 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
dea3101e 2234
d5f78fb0 2235 if (pci_enable_device_mem(pdev))
dea3101e 2236 goto out;
8a4df120 2237 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
dea3101e 2238 goto out_disable_device;
2239
2e0fef85
JS
2240 phba = kzalloc(sizeof (struct lpfc_hba), GFP_KERNEL);
2241 if (!phba)
dea3101e 2242 goto out_release_regions;
2243
2e0fef85 2244 spin_lock_init(&phba->hbalock);
dea3101e 2245
e47c9093
JS
2246 /* Initialize ndlp management spinlock */
2247 spin_lock_init(&phba->ndlp_lock);
2248
dea3101e 2249 phba->pcidev = pdev;
2250
2251 /* Assign an unused board number */
92d7f7b0 2252 if ((phba->brd_no = lpfc_get_instance()) < 0)
2e0fef85 2253 goto out_free_phba;
dea3101e 2254
2e0fef85 2255 INIT_LIST_HEAD(&phba->port_list);
2e0fef85
JS
2256 /*
2257 * Get all the module params for configuring this host and then
2258 * establish the host.
2259 */
2260 lpfc_get_cfgparam(phba);
92d7f7b0 2261 phba->max_vpi = LPFC_MAX_VPI;
dea3101e 2262
2263 /* Initialize timers used by driver */
858c9f6c
JS
2264 init_timer(&phba->hb_tmofunc);
2265 phba->hb_tmofunc.function = lpfc_hb_timeout;
2266 phba->hb_tmofunc.data = (unsigned long)phba;
2267
dea3101e 2268 psli = &phba->sli;
2269 init_timer(&psli->mbox_tmo);
2270 psli->mbox_tmo.function = lpfc_mbox_timeout;
2e0fef85 2271 psli->mbox_tmo.data = (unsigned long) phba;
875fbdfe
JSEC
2272 init_timer(&phba->fcp_poll_timer);
2273 phba->fcp_poll_timer.function = lpfc_poll_timeout;
2e0fef85 2274 phba->fcp_poll_timer.data = (unsigned long) phba;
92d7f7b0
JS
2275 init_timer(&phba->fabric_block_timer);
2276 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
2277 phba->fabric_block_timer.data = (unsigned long) phba;
dea3101e 2278
dea3101e 2279 pci_set_master(pdev);
694625c0 2280 pci_try_set_mwi(pdev);
dea3101e 2281
2282 if (pci_set_dma_mask(phba->pcidev, DMA_64BIT_MASK) != 0)
2283 if (pci_set_dma_mask(phba->pcidev, DMA_32BIT_MASK) != 0)
2284 goto out_idr_remove;
2285
2286 /*
2287 * Get the bus address of Bar0 and Bar2 and the number of bytes
2288 * required by each mapping.
2289 */
2290 phba->pci_bar0_map = pci_resource_start(phba->pcidev, 0);
2291 bar0map_len = pci_resource_len(phba->pcidev, 0);
2292
2293 phba->pci_bar2_map = pci_resource_start(phba->pcidev, 2);
2294 bar2map_len = pci_resource_len(phba->pcidev, 2);
2295
901a920f 2296 /* Map HBA SLIM to a kernel virtual address. */
dea3101e 2297 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
901a920f
JW
2298 if (!phba->slim_memmap_p) {
2299 error = -ENODEV;
2300 dev_printk(KERN_ERR, &pdev->dev,
2301 "ioremap failed for SLIM memory.\n");
2302 goto out_idr_remove;
2303 }
2304
2305 /* Map HBA Control Registers to a kernel virtual address. */
dea3101e 2306 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
901a920f
JW
2307 if (!phba->ctrl_regs_memmap_p) {
2308 error = -ENODEV;
2309 dev_printk(KERN_ERR, &pdev->dev,
2310 "ioremap failed for HBA control registers.\n");
2311 goto out_iounmap_slim;
2312 }
dea3101e 2313
2314 /* Allocate memory for SLI-2 structures */
2315 phba->slim2p = dma_alloc_coherent(&phba->pcidev->dev, SLI2_SLIM_SIZE,
2316 &phba->slim2p_mapping, GFP_KERNEL);
2317 if (!phba->slim2p)
2318 goto out_iounmap;
2319
f91b392c 2320 memset(phba->slim2p, 0, SLI2_SLIM_SIZE);
dea3101e 2321
ed957684
JS
2322 phba->hbqslimp.virt = dma_alloc_coherent(&phba->pcidev->dev,
2323 lpfc_sli_hbq_size(),
2324 &phba->hbqslimp.phys,
2325 GFP_KERNEL);
2326 if (!phba->hbqslimp.virt)
2327 goto out_free_slim;
2328
51ef4c26
JS
2329 hbq_count = lpfc_sli_hbq_count();
2330 ptr = phba->hbqslimp.virt;
2331 for (i = 0; i < hbq_count; ++i) {
2332 phba->hbqs[i].hbq_virt = ptr;
2333 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
2334 ptr += (lpfc_hbq_defs[i]->entry_count *
2335 sizeof(struct lpfc_hbq_entry));
2336 }
2337 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
2338 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
2339
ed957684
JS
2340 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
2341
3163f725
JS
2342 INIT_LIST_HEAD(&phba->hbqbuf_in_list);
2343
dea3101e 2344 /* Initialize the SLI Layer to run with lpfc HBAs. */
2345 lpfc_sli_setup(phba);
2346 lpfc_sli_queue_setup(phba);
2347
98c9ea5c
JS
2348 retval = lpfc_mem_alloc(phba);
2349 if (retval) {
2350 error = retval;
ed957684 2351 goto out_free_hbqslimp;
98c9ea5c 2352 }
dea3101e 2353
2354 /* Initialize and populate the iocb list per host. */
2355 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
2356 for (i = 0; i < LPFC_IOCB_LIST_CNT; i++) {
dd00cc48 2357 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea3101e 2358 if (iocbq_entry == NULL) {
2359 printk(KERN_ERR "%s: only allocated %d iocbs of "
2360 "expected %d count. Unloading driver.\n",
cadbd4a5 2361 __func__, i, LPFC_IOCB_LIST_CNT);
dea3101e 2362 error = -ENOMEM;
2363 goto out_free_iocbq;
2364 }
2365
604a3e30
JB
2366 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
2367 if (iotag == 0) {
2368 kfree (iocbq_entry);
2369 printk(KERN_ERR "%s: failed to allocate IOTAG. "
2370 "Unloading driver.\n",
cadbd4a5 2371 __func__);
604a3e30
JB
2372 error = -ENOMEM;
2373 goto out_free_iocbq;
2374 }
2e0fef85
JS
2375
2376 spin_lock_irq(&phba->hbalock);
dea3101e 2377 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
2378 phba->total_iocbq_bufs++;
2e0fef85 2379 spin_unlock_irq(&phba->hbalock);
dea3101e 2380 }
2381
2382 /* Initialize HBA structure */
2383 phba->fc_edtov = FF_DEF_EDTOV;
2384 phba->fc_ratov = FF_DEF_RATOV;
2385 phba->fc_altov = FF_DEF_ALTOV;
2386 phba->fc_arbtov = FF_DEF_ARBTOV;
2387
2388 INIT_LIST_HEAD(&phba->work_list);
2389 phba->work_ha_mask = (HA_ERATT|HA_MBATT|HA_LATT);
2390 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
2391
5e9d9b82
JS
2392 /* Initialize the wait queue head for the kernel thread */
2393 init_waitqueue_head(&phba->work_waitq);
2394
dea3101e 2395 /* Startup the kernel thread for this host adapter. */
2396 phba->worker_thread = kthread_run(lpfc_do_work, phba,
2397 "lpfc_worker_%d", phba->brd_no);
2398 if (IS_ERR(phba->worker_thread)) {
2399 error = PTR_ERR(phba->worker_thread);
2400 goto out_free_iocbq;
2401 }
2402
dea3101e 2403 /* Initialize the list of scsi buffers used by driver for scsi IO. */
875fbdfe 2404 spin_lock_init(&phba->scsi_buf_list_lock);
dea3101e 2405 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
2406
92d7f7b0
JS
2407 /* Initialize list of fabric iocbs */
2408 INIT_LIST_HEAD(&phba->fabric_iocb_list);
2409
0ff10d46
JS
2410 /* Initialize list to save ELS buffers */
2411 INIT_LIST_HEAD(&phba->elsbuf);
2412
3de2a653 2413 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
2e0fef85
JS
2414 if (!vport)
2415 goto out_kthread_stop;
2416
2417 shost = lpfc_shost_from_vport(vport);
2e0fef85 2418 phba->pport = vport;
858c9f6c 2419 lpfc_debugfs_initialize(vport);
2e0fef85 2420
92d7f7b0 2421 pci_set_drvdata(pdev, shost);
db2378e0
JS
2422 phba->intr_type = NONE;
2423
2424 if (phba->cfg_use_msi == 2) {
2425 error = lpfc_enable_msix(phba);
2426 if (!error)
2427 phba->intr_type = MSIX;
2428 }
dea3101e 2429
db2378e0
JS
2430 /* Fallback to MSI if MSI-X initialization failed */
2431 if (phba->cfg_use_msi >= 1 && phba->intr_type == NONE) {
98c9ea5c
JS
2432 retval = pci_enable_msi(phba->pcidev);
2433 if (!retval)
db2378e0 2434 phba->intr_type = MSI;
51ef4c26 2435 else
e8b62011
JS
2436 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2437 "0452 Enable MSI failed, continuing "
2438 "with IRQ\n");
4ff43246
JS
2439 }
2440
db2378e0
JS
2441 /* MSI-X is the only case the doesn't need to call request_irq */
2442 if (phba->intr_type != MSIX) {
2443 retval = request_irq(phba->pcidev->irq, lpfc_intr_handler,
2444 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
2445 if (retval) {
2446 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0451 Enable "
2447 "interrupt handler failed\n");
2448 error = retval;
2449 goto out_disable_msi;
2450 } else if (phba->intr_type != MSI)
2451 phba->intr_type = INTx;
dea3101e 2452 }
dea3101e 2453
2e0fef85
JS
2454 phba->MBslimaddr = phba->slim_memmap_p;
2455 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
2456 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
2457 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
2458 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
2459
98c9ea5c
JS
2460 if (lpfc_alloc_sysfs_attr(vport)) {
2461 error = -ENOMEM;
dea3101e 2462 goto out_free_irq;
98c9ea5c 2463 }
875fbdfe 2464
98c9ea5c
JS
2465 if (lpfc_sli_hba_setup(phba)) {
2466 error = -ENODEV;
858c9f6c 2467 goto out_remove_device;
98c9ea5c 2468 }
858c9f6c
JS
2469
2470 /*
2471 * hba setup may have changed the hba_queue_depth so we need to adjust
2472 * the value of can_queue.
2473 */
2474 shost->can_queue = phba->cfg_hba_queue_depth - 10;
2475
2476 lpfc_host_attrib_init(shost);
2477
2e0fef85
JS
2478 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
2479 spin_lock_irq(shost->host_lock);
2480 lpfc_poll_start_timer(phba);
2481 spin_unlock_irq(shost->host_lock);
2482 }
dea3101e 2483
858c9f6c 2484 scsi_scan_host(shost);
dea3101e 2485
dea3101e 2486 return 0;
2487
858c9f6c
JS
2488out_remove_device:
2489 lpfc_free_sysfs_attr(vport);
2490 spin_lock_irq(shost->host_lock);
51ef4c26 2491 vport->load_flag |= FC_UNLOADING;
858c9f6c 2492 spin_unlock_irq(shost->host_lock);
dea3101e 2493out_free_irq:
92d7f7b0 2494 lpfc_stop_phba_timers(phba);
2e0fef85 2495 phba->pport->work_port_events = 0;
db2378e0
JS
2496
2497 if (phba->intr_type == MSIX)
2498 lpfc_disable_msix(phba);
2499 else
2500 free_irq(phba->pcidev->irq, phba);
2501
92d7f7b0 2502out_disable_msi:
db2378e0 2503 if (phba->intr_type == MSI)
51ef4c26 2504 pci_disable_msi(phba->pcidev);
2e0fef85 2505 destroy_port(vport);
dea3101e 2506out_kthread_stop:
2507 kthread_stop(phba->worker_thread);
2508out_free_iocbq:
2509 list_for_each_entry_safe(iocbq_entry, iocbq_next,
2510 &phba->lpfc_iocb_list, list) {
dea3101e 2511 kfree(iocbq_entry);
2512 phba->total_iocbq_bufs--;
dea3101e 2513 }
2514 lpfc_mem_free(phba);
ed957684
JS
2515out_free_hbqslimp:
2516 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(), phba->hbqslimp.virt,
2517 phba->hbqslimp.phys);
dea3101e 2518out_free_slim:
2519 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE, phba->slim2p,
2520 phba->slim2p_mapping);
2521out_iounmap:
2522 iounmap(phba->ctrl_regs_memmap_p);
901a920f 2523out_iounmap_slim:
dea3101e 2524 iounmap(phba->slim_memmap_p);
2525out_idr_remove:
2526 idr_remove(&lpfc_hba_index, phba->brd_no);
2e0fef85
JS
2527out_free_phba:
2528 kfree(phba);
dea3101e 2529out_release_regions:
8a4df120 2530 pci_release_selected_regions(pdev, bars);
dea3101e 2531out_disable_device:
2532 pci_disable_device(pdev);
2533out:
defbcf11 2534 pci_set_drvdata(pdev, NULL);
858c9f6c
JS
2535 if (shost)
2536 scsi_host_put(shost);
dea3101e 2537 return error;
2538}
2539
e59058c4
JS
2540/**
2541 * lpfc_pci_remove_one: lpfc PCI func to unregister device from PCI subsystem.
2542 * @pdev: pointer to PCI device
2543 *
2544 * This routine is to be registered to the kernel's PCI subsystem. When an
2545 * Emulex HBA is removed from PCI bus. It perform all the necessary cleanup
2546 * for the HBA device to be removed from the PCI subsystem properly.
2547 **/
dea3101e 2548static void __devexit
2549lpfc_pci_remove_one(struct pci_dev *pdev)
2550{
2e0fef85
JS
2551 struct Scsi_Host *shost = pci_get_drvdata(pdev);
2552 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2553 struct lpfc_hba *phba = vport->phba;
8a4df120
TK
2554 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
2555
549e55cd 2556 spin_lock_irq(&phba->hbalock);
51ef4c26 2557 vport->load_flag |= FC_UNLOADING;
549e55cd 2558 spin_unlock_irq(&phba->hbalock);
2e0fef85 2559
858c9f6c
JS
2560 kfree(vport->vname);
2561 lpfc_free_sysfs_attr(vport);
2562
09372820
JS
2563 kthread_stop(phba->worker_thread);
2564
858c9f6c
JS
2565 fc_remove_host(shost);
2566 scsi_remove_host(shost);
87af33fe
JS
2567 lpfc_cleanup(vport);
2568
2e0fef85
JS
2569 /*
2570 * Bring down the SLI Layer. This step disable all interrupts,
2571 * clears the rings, discards all mailbox commands, and resets
2572 * the HBA.
2573 */
2574 lpfc_sli_hba_down(phba);
2575 lpfc_sli_brdrestart(phba);
2576
92d7f7b0 2577 lpfc_stop_phba_timers(phba);
858c9f6c
JS
2578 spin_lock_irq(&phba->hbalock);
2579 list_del_init(&vport->listentry);
2580 spin_unlock_irq(&phba->hbalock);
2581
858c9f6c 2582 lpfc_debugfs_terminate(vport);
2e0fef85 2583
db2378e0
JS
2584 if (phba->intr_type == MSIX)
2585 lpfc_disable_msix(phba);
2586 else {
2587 free_irq(phba->pcidev->irq, phba);
2588 if (phba->intr_type == MSI)
2589 pci_disable_msi(phba->pcidev);
2590 }
dea3101e 2591
2592 pci_set_drvdata(pdev, NULL);
858c9f6c 2593 scsi_host_put(shost);
2e0fef85
JS
2594
2595 /*
2596 * Call scsi_free before mem_free since scsi bufs are released to their
2597 * corresponding pools here.
2598 */
2599 lpfc_scsi_free(phba);
2600 lpfc_mem_free(phba);
2601
ed957684
JS
2602 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(), phba->hbqslimp.virt,
2603 phba->hbqslimp.phys);
2604
2e0fef85
JS
2605 /* Free resources associated with SLI2 interface */
2606 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
2607 phba->slim2p, phba->slim2p_mapping);
2608
2609 /* unmap adapter SLIM and Control Registers */
2610 iounmap(phba->ctrl_regs_memmap_p);
2611 iounmap(phba->slim_memmap_p);
2612
2613 idr_remove(&lpfc_hba_index, phba->brd_no);
2614
2615 kfree(phba);
2616
8a4df120 2617 pci_release_selected_regions(pdev, bars);
2e0fef85 2618 pci_disable_device(pdev);
dea3101e 2619}
2620
8d63f375 2621/**
e59058c4
JS
2622 * lpfc_io_error_detected: Driver method for handling PCI I/O error detected.
2623 * @pdev: pointer to PCI device.
2624 * @state: the current PCI connection state.
8d63f375 2625 *
e59058c4
JS
2626 * This routine is registered to the PCI subsystem for error handling. This
2627 * function is called by the PCI subsystem after a PCI bus error affecting
2628 * this device has been detected. When this function is invoked, it will
2629 * need to stop all the I/Os and interrupt(s) to the device. Once that is
2630 * done, it will return PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to
2631 * perform proper recovery as desired.
2632 *
2633 * Return codes
2634 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
2635 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
2636 **/
8d63f375
LV
2637static pci_ers_result_t lpfc_io_error_detected(struct pci_dev *pdev,
2638 pci_channel_state_t state)
2639{
51ef4c26
JS
2640 struct Scsi_Host *shost = pci_get_drvdata(pdev);
2641 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8d63f375
LV
2642 struct lpfc_sli *psli = &phba->sli;
2643 struct lpfc_sli_ring *pring;
2644
5daa49ef 2645 if (state == pci_channel_io_perm_failure)
8d63f375 2646 return PCI_ERS_RESULT_DISCONNECT;
5daa49ef 2647
8d63f375
LV
2648 pci_disable_device(pdev);
2649 /*
2650 * There may be I/Os dropped by the firmware.
2651 * Error iocb (I/O) on txcmplq and let the SCSI layer
2652 * retry it after re-establishing link.
2653 */
2654 pring = &psli->ring[psli->fcp_ring];
2655 lpfc_sli_abort_iocb_ring(phba, pring);
2656
db2378e0
JS
2657 if (phba->intr_type == MSIX)
2658 lpfc_disable_msix(phba);
2659 else {
2660 free_irq(phba->pcidev->irq, phba);
2661 if (phba->intr_type == MSI)
2662 pci_disable_msi(phba->pcidev);
2663 }
51ef4c26 2664
8d63f375
LV
2665 /* Request a slot reset. */
2666 return PCI_ERS_RESULT_NEED_RESET;
2667}
2668
2669/**
e59058c4
JS
2670 * lpfc_io_slot_reset: Restart a PCI device from scratch.
2671 * @pdev: pointer to PCI device.
2672 *
2673 * This routine is registered to the PCI subsystem for error handling. This is
2674 * called after PCI bus has been reset to restart the PCI card from scratch,
2675 * as if from a cold-boot. During the PCI subsystem error recovery, after the
2676 * driver returns PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform
2677 * proper error recovery and then call this routine before calling the .resume
2678 * method to recover the device. This function will initialize the HBA device,
2679 * enable the interrupt, but it will just put the HBA to offline state without
2680 * passing any I/O traffic.
8d63f375 2681 *
e59058c4
JS
2682 * Return codes
2683 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
2684 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8d63f375
LV
2685 */
2686static pci_ers_result_t lpfc_io_slot_reset(struct pci_dev *pdev)
2687{
51ef4c26
JS
2688 struct Scsi_Host *shost = pci_get_drvdata(pdev);
2689 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8d63f375 2690 struct lpfc_sli *psli = &phba->sli;
9b379605 2691 int error, retval;
8d63f375
LV
2692
2693 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
09483916 2694 if (pci_enable_device_mem(pdev)) {
8d63f375
LV
2695 printk(KERN_ERR "lpfc: Cannot re-enable "
2696 "PCI device after reset.\n");
2697 return PCI_ERS_RESULT_DISCONNECT;
2698 }
2699
2700 pci_set_master(pdev);
2701
92d7f7b0 2702 spin_lock_irq(&phba->hbalock);
8d63f375 2703 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
92d7f7b0 2704 spin_unlock_irq(&phba->hbalock);
8d63f375 2705
9b379605
JS
2706 /* Enable configured interrupt method */
2707 phba->intr_type = NONE;
2708 if (phba->cfg_use_msi == 2) {
2709 error = lpfc_enable_msix(phba);
2710 if (!error)
2711 phba->intr_type = MSIX;
2712 }
2713
2714 /* Fallback to MSI if MSI-X initialization failed */
2715 if (phba->cfg_use_msi >= 1 && phba->intr_type == NONE) {
2716 retval = pci_enable_msi(phba->pcidev);
2717 if (!retval)
2718 phba->intr_type = MSI;
2719 else
2720 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2721 "0470 Enable MSI failed, continuing "
2722 "with IRQ\n");
2723 }
2724
2725 /* MSI-X is the only case the doesn't need to call request_irq */
2726 if (phba->intr_type != MSIX) {
2727 retval = request_irq(phba->pcidev->irq, lpfc_intr_handler,
2728 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
2729 if (retval) {
2730 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2731 "0471 Enable interrupt handler "
2732 "failed\n");
2733 } else if (phba->intr_type != MSI)
2734 phba->intr_type = INTx;
2735 }
8d63f375
LV
2736
2737 /* Take device offline; this will perform cleanup */
2738 lpfc_offline(phba);
2739 lpfc_sli_brdrestart(phba);
2740
2741 return PCI_ERS_RESULT_RECOVERED;
2742}
2743
2744/**
e59058c4
JS
2745 * lpfc_io_resume: Resume PCI I/O operation.
2746 * @pdev: pointer to PCI device
8d63f375 2747 *
e59058c4
JS
2748 * This routine is registered to the PCI subsystem for error handling. It is
2749 * called when kernel error recovery tells the lpfc driver that it is ok to
2750 * resume normal PCI operation after PCI bus error recovery. After this call,
2751 * traffic can start to flow from this device again.
8d63f375
LV
2752 */
2753static void lpfc_io_resume(struct pci_dev *pdev)
2754{
51ef4c26
JS
2755 struct Scsi_Host *shost = pci_get_drvdata(pdev);
2756 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8d63f375 2757
58da1ffb 2758 lpfc_online(phba);
8d63f375
LV
2759}
2760
dea3101e 2761static struct pci_device_id lpfc_id_table[] = {
2762 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
2763 PCI_ANY_ID, PCI_ANY_ID, },
06325e74
JSEC
2764 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
2765 PCI_ANY_ID, PCI_ANY_ID, },
dea3101e 2766 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
2767 PCI_ANY_ID, PCI_ANY_ID, },
2768 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
2769 PCI_ANY_ID, PCI_ANY_ID, },
2770 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
2771 PCI_ANY_ID, PCI_ANY_ID, },
2772 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
2773 PCI_ANY_ID, PCI_ANY_ID, },
2774 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
2775 PCI_ANY_ID, PCI_ANY_ID, },
2776 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
2777 PCI_ANY_ID, PCI_ANY_ID, },
2778 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
2779 PCI_ANY_ID, PCI_ANY_ID, },
e4adb204
JSEC
2780 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
2781 PCI_ANY_ID, PCI_ANY_ID, },
2782 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
2783 PCI_ANY_ID, PCI_ANY_ID, },
2784 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
2785 PCI_ANY_ID, PCI_ANY_ID, },
dea3101e 2786 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
2787 PCI_ANY_ID, PCI_ANY_ID, },
e4adb204
JSEC
2788 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
2789 PCI_ANY_ID, PCI_ANY_ID, },
2790 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
2791 PCI_ANY_ID, PCI_ANY_ID, },
dea3101e 2792 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
2793 PCI_ANY_ID, PCI_ANY_ID, },
2794 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
2795 PCI_ANY_ID, PCI_ANY_ID, },
2796 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
2797 PCI_ANY_ID, PCI_ANY_ID, },
e4adb204
JSEC
2798 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
2799 PCI_ANY_ID, PCI_ANY_ID, },
2800 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
2801 PCI_ANY_ID, PCI_ANY_ID, },
dea3101e 2802 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
2803 PCI_ANY_ID, PCI_ANY_ID, },
2804 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
2805 PCI_ANY_ID, PCI_ANY_ID, },
2806 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
2807 PCI_ANY_ID, PCI_ANY_ID, },
2808 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
2809 PCI_ANY_ID, PCI_ANY_ID, },
2810 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
2811 PCI_ANY_ID, PCI_ANY_ID, },
e4adb204
JSEC
2812 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
2813 PCI_ANY_ID, PCI_ANY_ID, },
2814 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
2815 PCI_ANY_ID, PCI_ANY_ID, },
b87eab38
JS
2816 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
2817 PCI_ANY_ID, PCI_ANY_ID, },
2818 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
2819 PCI_ANY_ID, PCI_ANY_ID, },
2820 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
2821 PCI_ANY_ID, PCI_ANY_ID, },
2822 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
2823 PCI_ANY_ID, PCI_ANY_ID, },
2824 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
2825 PCI_ANY_ID, PCI_ANY_ID, },
2826 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
2827 PCI_ANY_ID, PCI_ANY_ID, },
dea3101e 2828 { 0 }
2829};
2830
2831MODULE_DEVICE_TABLE(pci, lpfc_id_table);
2832
8d63f375
LV
2833static struct pci_error_handlers lpfc_err_handler = {
2834 .error_detected = lpfc_io_error_detected,
2835 .slot_reset = lpfc_io_slot_reset,
2836 .resume = lpfc_io_resume,
2837};
2838
dea3101e 2839static struct pci_driver lpfc_driver = {
2840 .name = LPFC_DRIVER_NAME,
2841 .id_table = lpfc_id_table,
2842 .probe = lpfc_pci_probe_one,
2843 .remove = __devexit_p(lpfc_pci_remove_one),
2e0fef85 2844 .err_handler = &lpfc_err_handler,
dea3101e 2845};
2846
e59058c4
JS
2847/**
2848 * lpfc_init: lpfc module initialization routine.
2849 *
2850 * This routine is to be invoked when the lpfc module is loaded into the
2851 * kernel. The special kernel macro module_init() is used to indicate the
2852 * role of this routine to the kernel as lpfc module entry point.
2853 *
2854 * Return codes
2855 * 0 - successful
2856 * -ENOMEM - FC attach transport failed
2857 * all others - failed
2858 */
dea3101e 2859static int __init
2860lpfc_init(void)
2861{
2862 int error = 0;
2863
2864 printk(LPFC_MODULE_DESC "\n");
c44ce173 2865 printk(LPFC_COPYRIGHT "\n");
dea3101e 2866
7ee5d43e
JS
2867 if (lpfc_enable_npiv) {
2868 lpfc_transport_functions.vport_create = lpfc_vport_create;
2869 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
2870 }
dea3101e 2871 lpfc_transport_template =
2872 fc_attach_transport(&lpfc_transport_functions);
7ee5d43e 2873 if (lpfc_transport_template == NULL)
dea3101e 2874 return -ENOMEM;
7ee5d43e 2875 if (lpfc_enable_npiv) {
7ee5d43e 2876 lpfc_vport_transport_template =
98c9ea5c
JS
2877 fc_attach_transport(&lpfc_vport_transport_functions);
2878 if (lpfc_vport_transport_template == NULL) {
2879 fc_release_transport(lpfc_transport_template);
7ee5d43e 2880 return -ENOMEM;
98c9ea5c 2881 }
7ee5d43e 2882 }
dea3101e 2883 error = pci_register_driver(&lpfc_driver);
92d7f7b0 2884 if (error) {
dea3101e 2885 fc_release_transport(lpfc_transport_template);
92d7f7b0
JS
2886 fc_release_transport(lpfc_vport_transport_template);
2887 }
dea3101e 2888
2889 return error;
2890}
2891
e59058c4
JS
2892/**
2893 * lpfc_exit: lpfc module removal routine.
2894 *
2895 * This routine is invoked when the lpfc module is removed from the kernel.
2896 * The special kernel macro module_exit() is used to indicate the role of
2897 * this routine to the kernel as lpfc module exit point.
2898 */
dea3101e 2899static void __exit
2900lpfc_exit(void)
2901{
2902 pci_unregister_driver(&lpfc_driver);
2903 fc_release_transport(lpfc_transport_template);
7ee5d43e
JS
2904 if (lpfc_enable_npiv)
2905 fc_release_transport(lpfc_vport_transport_template);
dea3101e 2906}
2907
2908module_init(lpfc_init);
2909module_exit(lpfc_exit);
2910MODULE_LICENSE("GPL");
2911MODULE_DESCRIPTION(LPFC_MODULE_DESC);
2912MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
2913MODULE_VERSION("0:" LPFC_DRIVER_VERSION);
This page took 0.502935 seconds and 5 git commands to generate.