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