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