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