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