[SCSI] lpfc 8.3.9: SLI enhancments to support new hardware.
[deliverable/linux.git] / drivers / scsi / lpfc / lpfc_init.c
CommitLineData
dea3101e 1/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
c44ce173 3 * Fibre Channel Host Bus Adapters. *
4fede78f 4 * Copyright (C) 2004-2010 Emulex. All rights reserved. *
c44ce173 5 * EMULEX and SLI are trademarks of Emulex. *
dea3101e 6 * www.emulex.com *
c44ce173 7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea3101e 8 * *
9 * This program is free software; you can redistribute it and/or *
c44ce173
JSEC
10 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea3101e 20 *******************************************************************/
21
dea3101e 22#include <linux/blkdev.h>
23#include <linux/delay.h>
24#include <linux/dma-mapping.h>
25#include <linux/idr.h>
26#include <linux/interrupt.h>
27#include <linux/kthread.h>
28#include <linux/pci.h>
29#include <linux/spinlock.h>
92d7f7b0 30#include <linux/ctype.h>
0d878419 31#include <linux/aer.h>
dea3101e 32
91886523 33#include <scsi/scsi.h>
dea3101e 34#include <scsi/scsi_device.h>
35#include <scsi/scsi_host.h>
36#include <scsi/scsi_transport_fc.h>
37
da0436e9 38#include "lpfc_hw4.h"
dea3101e 39#include "lpfc_hw.h"
40#include "lpfc_sli.h"
da0436e9 41#include "lpfc_sli4.h"
ea2151b4 42#include "lpfc_nl.h"
dea3101e 43#include "lpfc_disc.h"
44#include "lpfc_scsi.h"
45#include "lpfc.h"
46#include "lpfc_logmsg.h"
47#include "lpfc_crtn.h"
92d7f7b0 48#include "lpfc_vport.h"
dea3101e 49#include "lpfc_version.h"
50
81301a9b
JS
51char *_dump_buf_data;
52unsigned long _dump_buf_data_order;
53char *_dump_buf_dif;
54unsigned long _dump_buf_dif_order;
55spinlock_t _dump_buf_lock;
56
dea3101e 57static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
58static int lpfc_post_rcv_buf(struct lpfc_hba *);
da0436e9
JS
59static int lpfc_sli4_queue_create(struct lpfc_hba *);
60static void lpfc_sli4_queue_destroy(struct lpfc_hba *);
61static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
62static int lpfc_setup_endian_order(struct lpfc_hba *);
63static int lpfc_sli4_read_config(struct lpfc_hba *);
64static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
65static void lpfc_free_sgl_list(struct lpfc_hba *);
66static int lpfc_init_sgl_list(struct lpfc_hba *);
67static int lpfc_init_active_sgl_array(struct lpfc_hba *);
68static void lpfc_free_active_sgl(struct lpfc_hba *);
69static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
70static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
71static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
72static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
73static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
dea3101e 74
75static struct scsi_transport_template *lpfc_transport_template = NULL;
92d7f7b0 76static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea3101e 77static DEFINE_IDR(lpfc_hba_index);
78
e59058c4 79/**
3621a710 80 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
e59058c4
JS
81 * @phba: pointer to lpfc hba data structure.
82 *
83 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
84 * mailbox command. It retrieves the revision information from the HBA and
85 * collects the Vital Product Data (VPD) about the HBA for preparing the
86 * configuration of the HBA.
87 *
88 * Return codes:
89 * 0 - success.
90 * -ERESTART - requests the SLI layer to reset the HBA and try again.
91 * Any other value - indicates an error.
92 **/
dea3101e 93int
2e0fef85 94lpfc_config_port_prep(struct lpfc_hba *phba)
dea3101e 95{
96 lpfc_vpd_t *vp = &phba->vpd;
97 int i = 0, rc;
98 LPFC_MBOXQ_t *pmb;
99 MAILBOX_t *mb;
100 char *lpfc_vpd_data = NULL;
101 uint16_t offset = 0;
102 static char licensed[56] =
103 "key unlock for use with gnu public licensed code only\0";
65a29c16 104 static int init_key = 1;
dea3101e 105
106 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
107 if (!pmb) {
2e0fef85 108 phba->link_state = LPFC_HBA_ERROR;
dea3101e 109 return -ENOMEM;
110 }
111
04c68496 112 mb = &pmb->u.mb;
2e0fef85 113 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e 114
115 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
65a29c16
JS
116 if (init_key) {
117 uint32_t *ptext = (uint32_t *) licensed;
dea3101e 118
65a29c16
JS
119 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
120 *ptext = cpu_to_be32(*ptext);
121 init_key = 0;
122 }
dea3101e 123
124 lpfc_read_nv(phba, pmb);
125 memset((char*)mb->un.varRDnvp.rsvd3, 0,
126 sizeof (mb->un.varRDnvp.rsvd3));
127 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
128 sizeof (licensed));
129
130 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
131
132 if (rc != MBX_SUCCESS) {
ed957684 133 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
e8b62011 134 "0324 Config Port initialization "
dea3101e 135 "error, mbxCmd x%x READ_NVPARM, "
136 "mbxStatus x%x\n",
dea3101e 137 mb->mbxCommand, mb->mbxStatus);
138 mempool_free(pmb, phba->mbox_mem_pool);
139 return -ERESTART;
140 }
141 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
2e0fef85
JS
142 sizeof(phba->wwnn));
143 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
144 sizeof(phba->wwpn));
dea3101e 145 }
146
92d7f7b0
JS
147 phba->sli3_options = 0x0;
148
dea3101e 149 /* Setup and issue mailbox READ REV command */
150 lpfc_read_rev(phba, pmb);
151 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
152 if (rc != MBX_SUCCESS) {
ed957684 153 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 154 "0439 Adapter failed to init, mbxCmd x%x "
dea3101e 155 "READ_REV, mbxStatus x%x\n",
dea3101e 156 mb->mbxCommand, mb->mbxStatus);
157 mempool_free( pmb, phba->mbox_mem_pool);
158 return -ERESTART;
159 }
160
92d7f7b0 161
1de933f3
JSEC
162 /*
163 * The value of rr must be 1 since the driver set the cv field to 1.
164 * This setting requires the FW to set all revision fields.
dea3101e 165 */
1de933f3 166 if (mb->un.varRdRev.rr == 0) {
dea3101e 167 vp->rev.rBit = 0;
1de933f3 168 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011
JS
169 "0440 Adapter failed to init, READ_REV has "
170 "missing revision information.\n");
dea3101e 171 mempool_free(pmb, phba->mbox_mem_pool);
172 return -ERESTART;
dea3101e 173 }
174
495a714c
JS
175 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
176 mempool_free(pmb, phba->mbox_mem_pool);
ed957684 177 return -EINVAL;
495a714c 178 }
ed957684 179
dea3101e 180 /* Save information as VPD data */
1de933f3 181 vp->rev.rBit = 1;
92d7f7b0 182 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
1de933f3
JSEC
183 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
184 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
185 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
186 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea3101e 187 vp->rev.biuRev = mb->un.varRdRev.biuRev;
188 vp->rev.smRev = mb->un.varRdRev.smRev;
189 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
190 vp->rev.endecRev = mb->un.varRdRev.endecRev;
191 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
192 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
193 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
194 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
195 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
196 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
197
92d7f7b0
JS
198 /* If the sli feature level is less then 9, we must
199 * tear down all RPIs and VPIs on link down if NPIV
200 * is enabled.
201 */
202 if (vp->rev.feaLevelHigh < 9)
203 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
204
dea3101e 205 if (lpfc_is_LC_HBA(phba->pcidev->device))
206 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
207 sizeof (phba->RandomData));
208
dea3101e 209 /* Get adapter VPD information */
dea3101e 210 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
211 if (!lpfc_vpd_data)
d7c255b2 212 goto out_free_mbox;
dea3101e 213
214 do {
a0c87cbd 215 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
dea3101e 216 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
217
218 if (rc != MBX_SUCCESS) {
219 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011 220 "0441 VPD not present on adapter, "
dea3101e 221 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea3101e 222 mb->mbxCommand, mb->mbxStatus);
74b72a59 223 mb->un.varDmp.word_cnt = 0;
dea3101e 224 }
04c68496
JS
225 /* dump mem may return a zero when finished or we got a
226 * mailbox error, either way we are done.
227 */
228 if (mb->un.varDmp.word_cnt == 0)
229 break;
74b72a59
JW
230 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
231 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
d7c255b2
JS
232 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
233 lpfc_vpd_data + offset,
92d7f7b0 234 mb->un.varDmp.word_cnt);
dea3101e 235 offset += mb->un.varDmp.word_cnt;
74b72a59
JW
236 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
237 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea3101e 238
239 kfree(lpfc_vpd_data);
dea3101e 240out_free_mbox:
241 mempool_free(pmb, phba->mbox_mem_pool);
242 return 0;
243}
244
e59058c4 245/**
3621a710 246 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
e59058c4
JS
247 * @phba: pointer to lpfc hba data structure.
248 * @pmboxq: pointer to the driver internal queue element for mailbox command.
249 *
250 * This is the completion handler for driver's configuring asynchronous event
251 * mailbox command to the device. If the mailbox command returns successfully,
252 * it will set internal async event support flag to 1; otherwise, it will
253 * set internal async event support flag to 0.
254 **/
57127f15
JS
255static void
256lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
257{
04c68496 258 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
57127f15
JS
259 phba->temp_sensor_support = 1;
260 else
261 phba->temp_sensor_support = 0;
262 mempool_free(pmboxq, phba->mbox_mem_pool);
263 return;
264}
265
97207482 266/**
3621a710 267 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
97207482
JS
268 * @phba: pointer to lpfc hba data structure.
269 * @pmboxq: pointer to the driver internal queue element for mailbox command.
270 *
271 * This is the completion handler for dump mailbox command for getting
272 * wake up parameters. When this command complete, the response contain
273 * Option rom version of the HBA. This function translate the version number
274 * into a human readable string and store it in OptionROMVersion.
275 **/
276static void
277lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
278{
279 struct prog_id *prg;
280 uint32_t prog_id_word;
281 char dist = ' ';
282 /* character array used for decoding dist type. */
283 char dist_char[] = "nabx";
284
04c68496 285 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
9f1e1b50 286 mempool_free(pmboxq, phba->mbox_mem_pool);
97207482 287 return;
9f1e1b50 288 }
97207482
JS
289
290 prg = (struct prog_id *) &prog_id_word;
291
292 /* word 7 contain option rom version */
04c68496 293 prog_id_word = pmboxq->u.mb.un.varWords[7];
97207482
JS
294
295 /* Decode the Option rom version word to a readable string */
296 if (prg->dist < 4)
297 dist = dist_char[prg->dist];
298
299 if ((prg->dist == 3) && (prg->num == 0))
300 sprintf(phba->OptionROMVersion, "%d.%d%d",
301 prg->ver, prg->rev, prg->lev);
302 else
303 sprintf(phba->OptionROMVersion, "%d.%d%d%c%d",
304 prg->ver, prg->rev, prg->lev,
305 dist, prg->num);
9f1e1b50 306 mempool_free(pmboxq, phba->mbox_mem_pool);
97207482
JS
307 return;
308}
309
e59058c4 310/**
3621a710 311 * lpfc_config_port_post - Perform lpfc initialization after config port
e59058c4
JS
312 * @phba: pointer to lpfc hba data structure.
313 *
314 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
315 * command call. It performs all internal resource and state setups on the
316 * port: post IOCB buffers, enable appropriate host interrupt attentions,
317 * ELS ring timers, etc.
318 *
319 * Return codes
320 * 0 - success.
321 * Any other value - error.
322 **/
dea3101e 323int
2e0fef85 324lpfc_config_port_post(struct lpfc_hba *phba)
dea3101e 325{
2e0fef85 326 struct lpfc_vport *vport = phba->pport;
a257bf90 327 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea3101e 328 LPFC_MBOXQ_t *pmb;
329 MAILBOX_t *mb;
330 struct lpfc_dmabuf *mp;
331 struct lpfc_sli *psli = &phba->sli;
332 uint32_t status, timeout;
2e0fef85
JS
333 int i, j;
334 int rc;
dea3101e 335
7af67051
JS
336 spin_lock_irq(&phba->hbalock);
337 /*
338 * If the Config port completed correctly the HBA is not
339 * over heated any more.
340 */
341 if (phba->over_temp_state == HBA_OVER_TEMP)
342 phba->over_temp_state = HBA_NORMAL_TEMP;
343 spin_unlock_irq(&phba->hbalock);
344
dea3101e 345 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
346 if (!pmb) {
2e0fef85 347 phba->link_state = LPFC_HBA_ERROR;
dea3101e 348 return -ENOMEM;
349 }
04c68496 350 mb = &pmb->u.mb;
dea3101e 351
dea3101e 352 /* Get login parameters for NID. */
92d7f7b0 353 lpfc_read_sparam(phba, pmb, 0);
ed957684 354 pmb->vport = vport;
dea3101e 355 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
ed957684 356 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 357 "0448 Adapter failed init, mbxCmd x%x "
dea3101e 358 "READ_SPARM mbxStatus x%x\n",
dea3101e 359 mb->mbxCommand, mb->mbxStatus);
2e0fef85 360 phba->link_state = LPFC_HBA_ERROR;
dea3101e 361 mp = (struct lpfc_dmabuf *) pmb->context1;
362 mempool_free( pmb, phba->mbox_mem_pool);
363 lpfc_mbuf_free(phba, mp->virt, mp->phys);
364 kfree(mp);
365 return -EIO;
366 }
367
368 mp = (struct lpfc_dmabuf *) pmb->context1;
369
2e0fef85 370 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea3101e 371 lpfc_mbuf_free(phba, mp->virt, mp->phys);
372 kfree(mp);
373 pmb->context1 = NULL;
374
a12e07bc 375 if (phba->cfg_soft_wwnn)
2e0fef85
JS
376 u64_to_wwn(phba->cfg_soft_wwnn,
377 vport->fc_sparam.nodeName.u.wwn);
c3f28afa 378 if (phba->cfg_soft_wwpn)
2e0fef85
JS
379 u64_to_wwn(phba->cfg_soft_wwpn,
380 vport->fc_sparam.portName.u.wwn);
381 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
dea3101e 382 sizeof (struct lpfc_name));
2e0fef85 383 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
dea3101e 384 sizeof (struct lpfc_name));
a257bf90
JS
385
386 /* Update the fc_host data structures with new wwn. */
387 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
388 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
21e9a0a5 389 fc_host_max_npiv_vports(shost) = phba->max_vpi;
a257bf90 390
dea3101e 391 /* If no serial number in VPD data, use low 6 bytes of WWNN */
392 /* This should be consolidated into parse_vpd ? - mr */
393 if (phba->SerialNumber[0] == 0) {
394 uint8_t *outptr;
395
2e0fef85 396 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea3101e 397 for (i = 0; i < 12; i++) {
398 status = *outptr++;
399 j = ((status & 0xf0) >> 4);
400 if (j <= 9)
401 phba->SerialNumber[i] =
402 (char)((uint8_t) 0x30 + (uint8_t) j);
403 else
404 phba->SerialNumber[i] =
405 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
406 i++;
407 j = (status & 0xf);
408 if (j <= 9)
409 phba->SerialNumber[i] =
410 (char)((uint8_t) 0x30 + (uint8_t) j);
411 else
412 phba->SerialNumber[i] =
413 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
414 }
415 }
416
dea3101e 417 lpfc_read_config(phba, pmb);
ed957684 418 pmb->vport = vport;
dea3101e 419 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
ed957684 420 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 421 "0453 Adapter failed to init, mbxCmd x%x "
dea3101e 422 "READ_CONFIG, mbxStatus x%x\n",
dea3101e 423 mb->mbxCommand, mb->mbxStatus);
2e0fef85 424 phba->link_state = LPFC_HBA_ERROR;
dea3101e 425 mempool_free( pmb, phba->mbox_mem_pool);
426 return -EIO;
427 }
428
a0c87cbd
JS
429 /* Check if the port is disabled */
430 lpfc_sli_read_link_ste(phba);
431
dea3101e 432 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
433 if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
434 phba->cfg_hba_queue_depth =
f1126688
JS
435 (mb->un.varRdConfig.max_xri + 1) -
436 lpfc_sli4_get_els_iocb_cnt(phba);
dea3101e 437
438 phba->lmt = mb->un.varRdConfig.lmt;
74b72a59
JW
439
440 /* Get the default values for Model Name and Description */
441 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
442
443 if ((phba->cfg_link_speed > LINK_SPEED_10G)
444 || ((phba->cfg_link_speed == LINK_SPEED_1G)
445 && !(phba->lmt & LMT_1Gb))
446 || ((phba->cfg_link_speed == LINK_SPEED_2G)
447 && !(phba->lmt & LMT_2Gb))
448 || ((phba->cfg_link_speed == LINK_SPEED_4G)
449 && !(phba->lmt & LMT_4Gb))
450 || ((phba->cfg_link_speed == LINK_SPEED_8G)
451 && !(phba->lmt & LMT_8Gb))
452 || ((phba->cfg_link_speed == LINK_SPEED_10G)
453 && !(phba->lmt & LMT_10Gb))) {
454 /* Reset link speed to auto */
ed957684 455 lpfc_printf_log(phba, KERN_WARNING, LOG_LINK_EVENT,
e8b62011 456 "1302 Invalid speed for this board: "
dea3101e 457 "Reset link speed to auto: x%x\n",
dea3101e 458 phba->cfg_link_speed);
459 phba->cfg_link_speed = LINK_SPEED_AUTO;
460 }
461
2e0fef85 462 phba->link_state = LPFC_LINK_DOWN;
dea3101e 463
0b727fea 464 /* Only process IOCBs on ELS ring till hba_state is READY */
a4bc3379
JS
465 if (psli->ring[psli->extra_ring].cmdringaddr)
466 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
dea3101e 467 if (psli->ring[psli->fcp_ring].cmdringaddr)
468 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
469 if (psli->ring[psli->next_ring].cmdringaddr)
470 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
471
472 /* Post receive buffers for desired rings */
ed957684
JS
473 if (phba->sli_rev != 3)
474 lpfc_post_rcv_buf(phba);
dea3101e 475
9399627f
JS
476 /*
477 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
478 */
479 if (phba->intr_type == MSIX) {
480 rc = lpfc_config_msi(phba, pmb);
481 if (rc) {
482 mempool_free(pmb, phba->mbox_mem_pool);
483 return -EIO;
484 }
485 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
486 if (rc != MBX_SUCCESS) {
487 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
488 "0352 Config MSI mailbox command "
489 "failed, mbxCmd x%x, mbxStatus x%x\n",
04c68496
JS
490 pmb->u.mb.mbxCommand,
491 pmb->u.mb.mbxStatus);
9399627f
JS
492 mempool_free(pmb, phba->mbox_mem_pool);
493 return -EIO;
494 }
495 }
496
04c68496 497 spin_lock_irq(&phba->hbalock);
9399627f
JS
498 /* Initialize ERATT handling flag */
499 phba->hba_flag &= ~HBA_ERATT_HANDLED;
500
dea3101e 501 /* Enable appropriate host interrupts */
dea3101e 502 status = readl(phba->HCregaddr);
503 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
504 if (psli->num_rings > 0)
505 status |= HC_R0INT_ENA;
506 if (psli->num_rings > 1)
507 status |= HC_R1INT_ENA;
508 if (psli->num_rings > 2)
509 status |= HC_R2INT_ENA;
510 if (psli->num_rings > 3)
511 status |= HC_R3INT_ENA;
512
875fbdfe
JSEC
513 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
514 (phba->cfg_poll & DISABLE_FCP_RING_INT))
9399627f 515 status &= ~(HC_R0INT_ENA);
875fbdfe 516
dea3101e 517 writel(status, phba->HCregaddr);
518 readl(phba->HCregaddr); /* flush */
2e0fef85 519 spin_unlock_irq(&phba->hbalock);
dea3101e 520
9399627f
JS
521 /* Set up ring-0 (ELS) timer */
522 timeout = phba->fc_ratov * 2;
2e0fef85 523 mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
9399627f 524 /* Set up heart beat (HB) timer */
858c9f6c
JS
525 mod_timer(&phba->hb_tmofunc, jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
526 phba->hb_outstanding = 0;
527 phba->last_completion_time = jiffies;
9399627f
JS
528 /* Set up error attention (ERATT) polling timer */
529 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
dea3101e 530
a0c87cbd
JS
531 if (phba->hba_flag & LINK_DISABLED) {
532 lpfc_printf_log(phba,
533 KERN_ERR, LOG_INIT,
534 "2598 Adapter Link is disabled.\n");
535 lpfc_down_link(phba, pmb);
536 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
537 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
538 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
539 lpfc_printf_log(phba,
540 KERN_ERR, LOG_INIT,
541 "2599 Adapter failed to issue DOWN_LINK"
542 " mbox command rc 0x%x\n", rc);
543
544 mempool_free(pmb, phba->mbox_mem_pool);
545 return -EIO;
546 }
547 } else {
548 lpfc_init_link(phba, pmb, phba->cfg_topology,
549 phba->cfg_link_speed);
550 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
551 lpfc_set_loopback_flag(phba);
552 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
553 if (rc != MBX_SUCCESS) {
554 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 555 "0454 Adapter failed to init, mbxCmd x%x "
dea3101e 556 "INIT_LINK, mbxStatus x%x\n",
dea3101e 557 mb->mbxCommand, mb->mbxStatus);
558
a0c87cbd
JS
559 /* Clear all interrupt enable conditions */
560 writel(0, phba->HCregaddr);
561 readl(phba->HCregaddr); /* flush */
562 /* Clear all pending interrupts */
563 writel(0xffffffff, phba->HAregaddr);
564 readl(phba->HAregaddr); /* flush */
dea3101e 565
a0c87cbd
JS
566 phba->link_state = LPFC_HBA_ERROR;
567 if (rc != MBX_BUSY)
568 mempool_free(pmb, phba->mbox_mem_pool);
569 return -EIO;
570 }
dea3101e 571 }
572 /* MBOX buffer will be freed in mbox compl */
57127f15
JS
573 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
574 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
575 pmb->mbox_cmpl = lpfc_config_async_cmpl;
576 pmb->vport = phba->pport;
577 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea3101e 578
57127f15
JS
579 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
580 lpfc_printf_log(phba,
581 KERN_ERR,
582 LOG_INIT,
583 "0456 Adapter failed to issue "
e4e74273 584 "ASYNCEVT_ENABLE mbox status x%x\n",
57127f15
JS
585 rc);
586 mempool_free(pmb, phba->mbox_mem_pool);
587 }
97207482
JS
588
589 /* Get Option rom version */
590 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
591 lpfc_dump_wakeup_param(phba, pmb);
592 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
593 pmb->vport = phba->pport;
594 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
595
596 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
597 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
e4e74273 598 "to get Option ROM version status x%x\n", rc);
97207482
JS
599 mempool_free(pmb, phba->mbox_mem_pool);
600 }
601
d7c255b2 602 return 0;
ce8b3ce5
JS
603}
604
e59058c4 605/**
3621a710 606 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
e59058c4
JS
607 * @phba: pointer to lpfc HBA data structure.
608 *
609 * This routine will do LPFC uninitialization before the HBA is reset when
610 * bringing down the SLI Layer.
611 *
612 * Return codes
613 * 0 - success.
614 * Any other value - error.
615 **/
dea3101e 616int
2e0fef85 617lpfc_hba_down_prep(struct lpfc_hba *phba)
dea3101e 618{
1b32f6aa
JS
619 struct lpfc_vport **vports;
620 int i;
3772a991
JS
621
622 if (phba->sli_rev <= LPFC_SLI_REV3) {
623 /* Disable interrupts */
624 writel(0, phba->HCregaddr);
625 readl(phba->HCregaddr); /* flush */
626 }
dea3101e 627
1b32f6aa
JS
628 if (phba->pport->load_flag & FC_UNLOADING)
629 lpfc_cleanup_discovery_resources(phba->pport);
630 else {
631 vports = lpfc_create_vport_work_array(phba);
632 if (vports != NULL)
3772a991
JS
633 for (i = 0; i <= phba->max_vports &&
634 vports[i] != NULL; i++)
1b32f6aa
JS
635 lpfc_cleanup_discovery_resources(vports[i]);
636 lpfc_destroy_vport_work_array(phba, vports);
7f5f3d0d
JS
637 }
638 return 0;
dea3101e 639}
640
e59058c4 641/**
3772a991 642 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
e59058c4
JS
643 * @phba: pointer to lpfc HBA data structure.
644 *
645 * This routine will do uninitialization after the HBA is reset when bring
646 * down the SLI Layer.
647 *
648 * Return codes
af901ca1 649 * 0 - success.
e59058c4
JS
650 * Any other value - error.
651 **/
3772a991
JS
652static int
653lpfc_hba_down_post_s3(struct lpfc_hba *phba)
41415862
JW
654{
655 struct lpfc_sli *psli = &phba->sli;
656 struct lpfc_sli_ring *pring;
657 struct lpfc_dmabuf *mp, *next_mp;
09372820 658 LIST_HEAD(completions);
41415862
JW
659 int i;
660
92d7f7b0
JS
661 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
662 lpfc_sli_hbqbuf_free_all(phba);
663 else {
664 /* Cleanup preposted buffers on the ELS ring */
665 pring = &psli->ring[LPFC_ELS_RING];
666 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
667 list_del(&mp->list);
668 pring->postbufq_cnt--;
669 lpfc_mbuf_free(phba, mp->virt, mp->phys);
670 kfree(mp);
671 }
41415862
JW
672 }
673
09372820 674 spin_lock_irq(&phba->hbalock);
41415862
JW
675 for (i = 0; i < psli->num_rings; i++) {
676 pring = &psli->ring[i];
09372820
JS
677
678 /* At this point in time the HBA is either reset or DOA. Either
679 * way, nothing should be on txcmplq as it will NEVER complete.
680 */
681 list_splice_init(&pring->txcmplq, &completions);
682 pring->txcmplq_cnt = 0;
683 spin_unlock_irq(&phba->hbalock);
684
a257bf90
JS
685 /* Cancel all the IOCBs from the completions list */
686 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
687 IOERR_SLI_ABORTED);
09372820 688
41415862 689 lpfc_sli_abort_iocb_ring(phba, pring);
09372820 690 spin_lock_irq(&phba->hbalock);
41415862 691 }
09372820 692 spin_unlock_irq(&phba->hbalock);
41415862
JW
693
694 return 0;
695}
da0436e9
JS
696/**
697 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
698 * @phba: pointer to lpfc HBA data structure.
699 *
700 * This routine will do uninitialization after the HBA is reset when bring
701 * down the SLI Layer.
702 *
703 * Return codes
af901ca1 704 * 0 - success.
da0436e9
JS
705 * Any other value - error.
706 **/
707static int
708lpfc_hba_down_post_s4(struct lpfc_hba *phba)
709{
710 struct lpfc_scsi_buf *psb, *psb_next;
711 LIST_HEAD(aborts);
712 int ret;
713 unsigned long iflag = 0;
714 ret = lpfc_hba_down_post_s3(phba);
715 if (ret)
716 return ret;
717 /* At this point in time the HBA is either reset or DOA. Either
718 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
719 * on the lpfc_sgl_list so that it can either be freed if the
720 * driver is unloading or reposted if the driver is restarting
721 * the port.
722 */
723 spin_lock_irq(&phba->hbalock); /* required for lpfc_sgl_list and */
724 /* scsl_buf_list */
725 /* abts_sgl_list_lock required because worker thread uses this
726 * list.
727 */
728 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
729 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
730 &phba->sli4_hba.lpfc_sgl_list);
731 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
732 /* abts_scsi_buf_list_lock required because worker thread uses this
733 * list.
734 */
735 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
736 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
737 &aborts);
738 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
739 spin_unlock_irq(&phba->hbalock);
740
741 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
742 psb->pCmd = NULL;
743 psb->status = IOSTAT_SUCCESS;
744 }
745 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
746 list_splice(&aborts, &phba->lpfc_scsi_buf_list);
747 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
748 return 0;
749}
750
751/**
752 * lpfc_hba_down_post - Wrapper func for hba down post routine
753 * @phba: pointer to lpfc HBA data structure.
754 *
755 * This routine wraps the actual SLI3 or SLI4 routine for performing
756 * uninitialization after the HBA is reset when bring down the SLI Layer.
757 *
758 * Return codes
af901ca1 759 * 0 - success.
da0436e9
JS
760 * Any other value - error.
761 **/
762int
763lpfc_hba_down_post(struct lpfc_hba *phba)
764{
765 return (*phba->lpfc_hba_down_post)(phba);
766}
41415862 767
e59058c4 768/**
3621a710 769 * lpfc_hb_timeout - The HBA-timer timeout handler
e59058c4
JS
770 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
771 *
772 * This is the HBA-timer timeout handler registered to the lpfc driver. When
773 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
774 * work-port-events bitmap and the worker thread is notified. This timeout
775 * event will be used by the worker thread to invoke the actual timeout
776 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
777 * be performed in the timeout handler and the HBA timeout event bit shall
778 * be cleared by the worker thread after it has taken the event bitmap out.
779 **/
a6ababd2 780static void
858c9f6c
JS
781lpfc_hb_timeout(unsigned long ptr)
782{
783 struct lpfc_hba *phba;
5e9d9b82 784 uint32_t tmo_posted;
858c9f6c
JS
785 unsigned long iflag;
786
787 phba = (struct lpfc_hba *)ptr;
9399627f
JS
788
789 /* Check for heart beat timeout conditions */
858c9f6c 790 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
5e9d9b82
JS
791 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
792 if (!tmo_posted)
858c9f6c
JS
793 phba->pport->work_port_events |= WORKER_HB_TMO;
794 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
795
9399627f 796 /* Tell the worker thread there is work to do */
5e9d9b82
JS
797 if (!tmo_posted)
798 lpfc_worker_wake_up(phba);
858c9f6c
JS
799 return;
800}
801
e59058c4 802/**
3621a710 803 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
e59058c4
JS
804 * @phba: pointer to lpfc hba data structure.
805 * @pmboxq: pointer to the driver internal queue element for mailbox command.
806 *
807 * This is the callback function to the lpfc heart-beat mailbox command.
808 * If configured, the lpfc driver issues the heart-beat mailbox command to
809 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
810 * heart-beat mailbox command is issued, the driver shall set up heart-beat
811 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
812 * heart-beat outstanding state. Once the mailbox command comes back and
813 * no error conditions detected, the heart-beat mailbox command timer is
814 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
815 * state is cleared for the next heart-beat. If the timer expired with the
816 * heart-beat outstanding state set, the driver will put the HBA offline.
817 **/
858c9f6c
JS
818static void
819lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
820{
821 unsigned long drvr_flag;
822
823 spin_lock_irqsave(&phba->hbalock, drvr_flag);
824 phba->hb_outstanding = 0;
825 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
826
9399627f 827 /* Check and reset heart-beat timer is necessary */
858c9f6c
JS
828 mempool_free(pmboxq, phba->mbox_mem_pool);
829 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
830 !(phba->link_state == LPFC_HBA_ERROR) &&
51ef4c26 831 !(phba->pport->load_flag & FC_UNLOADING))
858c9f6c
JS
832 mod_timer(&phba->hb_tmofunc,
833 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
834 return;
835}
836
e59058c4 837/**
3621a710 838 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
e59058c4
JS
839 * @phba: pointer to lpfc hba data structure.
840 *
841 * This is the actual HBA-timer timeout handler to be invoked by the worker
842 * thread whenever the HBA timer fired and HBA-timeout event posted. This
843 * handler performs any periodic operations needed for the device. If such
844 * periodic event has already been attended to either in the interrupt handler
845 * or by processing slow-ring or fast-ring events within the HBA-timer
846 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
847 * the timer for the next timeout period. If lpfc heart-beat mailbox command
848 * is configured and there is no heart-beat mailbox command outstanding, a
849 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
850 * has been a heart-beat mailbox command outstanding, the HBA shall be put
851 * to offline.
852 **/
858c9f6c
JS
853void
854lpfc_hb_timeout_handler(struct lpfc_hba *phba)
855{
45ed1190 856 struct lpfc_vport **vports;
858c9f6c 857 LPFC_MBOXQ_t *pmboxq;
0ff10d46 858 struct lpfc_dmabuf *buf_ptr;
45ed1190 859 int retval, i;
858c9f6c 860 struct lpfc_sli *psli = &phba->sli;
0ff10d46 861 LIST_HEAD(completions);
858c9f6c 862
45ed1190
JS
863 vports = lpfc_create_vport_work_array(phba);
864 if (vports != NULL)
865 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
866 lpfc_rcv_seq_check_edtov(vports[i]);
867 lpfc_destroy_vport_work_array(phba, vports);
868
858c9f6c 869 if ((phba->link_state == LPFC_HBA_ERROR) ||
51ef4c26 870 (phba->pport->load_flag & FC_UNLOADING) ||
858c9f6c
JS
871 (phba->pport->fc_flag & FC_OFFLINE_MODE))
872 return;
873
874 spin_lock_irq(&phba->pport->work_port_lock);
858c9f6c
JS
875
876 if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
877 jiffies)) {
878 spin_unlock_irq(&phba->pport->work_port_lock);
879 if (!phba->hb_outstanding)
880 mod_timer(&phba->hb_tmofunc,
881 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
882 else
883 mod_timer(&phba->hb_tmofunc,
884 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
885 return;
886 }
887 spin_unlock_irq(&phba->pport->work_port_lock);
888
0ff10d46
JS
889 if (phba->elsbuf_cnt &&
890 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
891 spin_lock_irq(&phba->hbalock);
892 list_splice_init(&phba->elsbuf, &completions);
893 phba->elsbuf_cnt = 0;
894 phba->elsbuf_prev_cnt = 0;
895 spin_unlock_irq(&phba->hbalock);
896
897 while (!list_empty(&completions)) {
898 list_remove_head(&completions, buf_ptr,
899 struct lpfc_dmabuf, list);
900 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
901 kfree(buf_ptr);
902 }
903 }
904 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
905
858c9f6c 906 /* If there is no heart beat outstanding, issue a heartbeat command */
13815c83
JS
907 if (phba->cfg_enable_hba_heartbeat) {
908 if (!phba->hb_outstanding) {
909 pmboxq = mempool_alloc(phba->mbox_mem_pool,GFP_KERNEL);
910 if (!pmboxq) {
911 mod_timer(&phba->hb_tmofunc,
912 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
913 return;
914 }
858c9f6c 915
13815c83
JS
916 lpfc_heart_beat(phba, pmboxq);
917 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
918 pmboxq->vport = phba->pport;
919 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
858c9f6c 920
13815c83
JS
921 if (retval != MBX_BUSY && retval != MBX_SUCCESS) {
922 mempool_free(pmboxq, phba->mbox_mem_pool);
923 mod_timer(&phba->hb_tmofunc,
924 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
925 return;
926 }
858c9f6c 927 mod_timer(&phba->hb_tmofunc,
13815c83
JS
928 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
929 phba->hb_outstanding = 1;
858c9f6c 930 return;
13815c83
JS
931 } else {
932 /*
933 * If heart beat timeout called with hb_outstanding set
934 * we need to take the HBA offline.
935 */
936 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
937 "0459 Adapter heartbeat failure, "
938 "taking this port offline.\n");
939
940 spin_lock_irq(&phba->hbalock);
f4b4c68f 941 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
13815c83
JS
942 spin_unlock_irq(&phba->hbalock);
943
944 lpfc_offline_prep(phba);
945 lpfc_offline(phba);
946 lpfc_unblock_mgmt_io(phba);
947 phba->link_state = LPFC_HBA_ERROR;
948 lpfc_hba_down_post(phba);
858c9f6c 949 }
858c9f6c
JS
950 }
951}
952
e59058c4 953/**
3621a710 954 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
e59058c4
JS
955 * @phba: pointer to lpfc hba data structure.
956 *
957 * This routine is called to bring the HBA offline when HBA hardware error
958 * other than Port Error 6 has been detected.
959 **/
09372820
JS
960static void
961lpfc_offline_eratt(struct lpfc_hba *phba)
962{
963 struct lpfc_sli *psli = &phba->sli;
964
965 spin_lock_irq(&phba->hbalock);
f4b4c68f 966 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
09372820
JS
967 spin_unlock_irq(&phba->hbalock);
968 lpfc_offline_prep(phba);
969
970 lpfc_offline(phba);
971 lpfc_reset_barrier(phba);
f4b4c68f 972 spin_lock_irq(&phba->hbalock);
09372820 973 lpfc_sli_brdreset(phba);
f4b4c68f 974 spin_unlock_irq(&phba->hbalock);
09372820
JS
975 lpfc_hba_down_post(phba);
976 lpfc_sli_brdready(phba, HS_MBRDY);
977 lpfc_unblock_mgmt_io(phba);
978 phba->link_state = LPFC_HBA_ERROR;
979 return;
980}
981
da0436e9
JS
982/**
983 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
984 * @phba: pointer to lpfc hba data structure.
985 *
986 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
987 * other than Port Error 6 has been detected.
988 **/
989static void
990lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
991{
992 lpfc_offline_prep(phba);
993 lpfc_offline(phba);
994 lpfc_sli4_brdreset(phba);
995 lpfc_hba_down_post(phba);
996 lpfc_sli4_post_status_check(phba);
997 lpfc_unblock_mgmt_io(phba);
998 phba->link_state = LPFC_HBA_ERROR;
999}
1000
a257bf90
JS
1001/**
1002 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1003 * @phba: pointer to lpfc hba data structure.
1004 *
1005 * This routine is invoked to handle the deferred HBA hardware error
1006 * conditions. This type of error is indicated by HBA by setting ER1
1007 * and another ER bit in the host status register. The driver will
1008 * wait until the ER1 bit clears before handling the error condition.
1009 **/
1010static void
1011lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1012{
1013 uint32_t old_host_status = phba->work_hs;
1014 struct lpfc_sli_ring *pring;
1015 struct lpfc_sli *psli = &phba->sli;
1016
f4b4c68f
JS
1017 /* If the pci channel is offline, ignore possible errors,
1018 * since we cannot communicate with the pci card anyway.
1019 */
1020 if (pci_channel_offline(phba->pcidev)) {
1021 spin_lock_irq(&phba->hbalock);
1022 phba->hba_flag &= ~DEFER_ERATT;
1023 spin_unlock_irq(&phba->hbalock);
1024 return;
1025 }
1026
a257bf90
JS
1027 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1028 "0479 Deferred Adapter Hardware Error "
1029 "Data: x%x x%x x%x\n",
1030 phba->work_hs,
1031 phba->work_status[0], phba->work_status[1]);
1032
1033 spin_lock_irq(&phba->hbalock);
f4b4c68f 1034 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
a257bf90
JS
1035 spin_unlock_irq(&phba->hbalock);
1036
1037
1038 /*
1039 * Firmware stops when it triggred erratt. That could cause the I/Os
1040 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1041 * SCSI layer retry it after re-establishing link.
1042 */
1043 pring = &psli->ring[psli->fcp_ring];
1044 lpfc_sli_abort_iocb_ring(phba, pring);
1045
1046 /*
1047 * There was a firmware error. Take the hba offline and then
1048 * attempt to restart it.
1049 */
1050 lpfc_offline_prep(phba);
1051 lpfc_offline(phba);
1052
1053 /* Wait for the ER1 bit to clear.*/
1054 while (phba->work_hs & HS_FFER1) {
1055 msleep(100);
1056 phba->work_hs = readl(phba->HSregaddr);
1057 /* If driver is unloading let the worker thread continue */
1058 if (phba->pport->load_flag & FC_UNLOADING) {
1059 phba->work_hs = 0;
1060 break;
1061 }
1062 }
1063
1064 /*
1065 * This is to ptrotect against a race condition in which
1066 * first write to the host attention register clear the
1067 * host status register.
1068 */
1069 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1070 phba->work_hs = old_host_status & ~HS_FFER1;
1071
3772a991 1072 spin_lock_irq(&phba->hbalock);
a257bf90 1073 phba->hba_flag &= ~DEFER_ERATT;
3772a991 1074 spin_unlock_irq(&phba->hbalock);
a257bf90
JS
1075 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1076 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1077}
1078
3772a991
JS
1079static void
1080lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1081{
1082 struct lpfc_board_event_header board_event;
1083 struct Scsi_Host *shost;
1084
1085 board_event.event_type = FC_REG_BOARD_EVENT;
1086 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1087 shost = lpfc_shost_from_vport(phba->pport);
1088 fc_host_post_vendor_event(shost, fc_get_event_number(),
1089 sizeof(board_event),
1090 (char *) &board_event,
1091 LPFC_NL_VENDOR_ID);
1092}
1093
e59058c4 1094/**
3772a991 1095 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
e59058c4
JS
1096 * @phba: pointer to lpfc hba data structure.
1097 *
1098 * This routine is invoked to handle the following HBA hardware error
1099 * conditions:
1100 * 1 - HBA error attention interrupt
1101 * 2 - DMA ring index out of range
1102 * 3 - Mailbox command came back as unknown
1103 **/
3772a991
JS
1104static void
1105lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea3101e 1106{
2e0fef85 1107 struct lpfc_vport *vport = phba->pport;
2e0fef85 1108 struct lpfc_sli *psli = &phba->sli;
dea3101e 1109 struct lpfc_sli_ring *pring;
d2873e4c 1110 uint32_t event_data;
57127f15
JS
1111 unsigned long temperature;
1112 struct temp_event temp_event_data;
92d7f7b0 1113 struct Scsi_Host *shost;
2e0fef85 1114
8d63f375 1115 /* If the pci channel is offline, ignore possible errors,
3772a991
JS
1116 * since we cannot communicate with the pci card anyway.
1117 */
1118 if (pci_channel_offline(phba->pcidev)) {
1119 spin_lock_irq(&phba->hbalock);
1120 phba->hba_flag &= ~DEFER_ERATT;
1121 spin_unlock_irq(&phba->hbalock);
8d63f375 1122 return;
3772a991
JS
1123 }
1124
13815c83
JS
1125 /* If resets are disabled then leave the HBA alone and return */
1126 if (!phba->cfg_enable_hba_reset)
1127 return;
dea3101e 1128
ea2151b4 1129 /* Send an internal error event to mgmt application */
3772a991 1130 lpfc_board_errevt_to_mgmt(phba);
ea2151b4 1131
a257bf90
JS
1132 if (phba->hba_flag & DEFER_ERATT)
1133 lpfc_handle_deferred_eratt(phba);
1134
97eab634 1135 if (phba->work_hs & HS_FFER6) {
dea3101e 1136 /* Re-establishing Link */
1137 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
e8b62011 1138 "1301 Re-establishing Link "
dea3101e 1139 "Data: x%x x%x x%x\n",
e8b62011 1140 phba->work_hs,
dea3101e 1141 phba->work_status[0], phba->work_status[1]);
58da1ffb 1142
92d7f7b0 1143 spin_lock_irq(&phba->hbalock);
f4b4c68f 1144 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
92d7f7b0 1145 spin_unlock_irq(&phba->hbalock);
dea3101e 1146
1147 /*
1148 * Firmware stops when it triggled erratt with HS_FFER6.
1149 * That could cause the I/Os dropped by the firmware.
1150 * Error iocb (I/O) on txcmplq and let the SCSI layer
1151 * retry it after re-establishing link.
1152 */
1153 pring = &psli->ring[psli->fcp_ring];
1154 lpfc_sli_abort_iocb_ring(phba, pring);
1155
dea3101e 1156 /*
1157 * There was a firmware error. Take the hba offline and then
1158 * attempt to restart it.
1159 */
46fa311e 1160 lpfc_offline_prep(phba);
dea3101e 1161 lpfc_offline(phba);
41415862 1162 lpfc_sli_brdrestart(phba);
dea3101e 1163 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
46fa311e 1164 lpfc_unblock_mgmt_io(phba);
dea3101e 1165 return;
1166 }
46fa311e 1167 lpfc_unblock_mgmt_io(phba);
57127f15
JS
1168 } else if (phba->work_hs & HS_CRIT_TEMP) {
1169 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1170 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1171 temp_event_data.event_code = LPFC_CRIT_TEMP;
1172 temp_event_data.data = (uint32_t)temperature;
1173
1174 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
d7c255b2 1175 "0406 Adapter maximum temperature exceeded "
57127f15
JS
1176 "(%ld), taking this port offline "
1177 "Data: x%x x%x x%x\n",
1178 temperature, phba->work_hs,
1179 phba->work_status[0], phba->work_status[1]);
1180
1181 shost = lpfc_shost_from_vport(phba->pport);
1182 fc_host_post_vendor_event(shost, fc_get_event_number(),
1183 sizeof(temp_event_data),
1184 (char *) &temp_event_data,
1185 SCSI_NL_VID_TYPE_PCI
1186 | PCI_VENDOR_ID_EMULEX);
1187
7af67051 1188 spin_lock_irq(&phba->hbalock);
7af67051
JS
1189 phba->over_temp_state = HBA_OVER_TEMP;
1190 spin_unlock_irq(&phba->hbalock);
09372820 1191 lpfc_offline_eratt(phba);
57127f15 1192
dea3101e 1193 } else {
1194 /* The if clause above forces this code path when the status
9399627f
JS
1195 * failure is a value other than FFER6. Do not call the offline
1196 * twice. This is the adapter hardware error path.
dea3101e 1197 */
1198 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 1199 "0457 Adapter Hardware Error "
dea3101e 1200 "Data: x%x x%x x%x\n",
e8b62011 1201 phba->work_hs,
dea3101e 1202 phba->work_status[0], phba->work_status[1]);
1203
d2873e4c 1204 event_data = FC_REG_DUMP_EVENT;
92d7f7b0 1205 shost = lpfc_shost_from_vport(vport);
2e0fef85 1206 fc_host_post_vendor_event(shost, fc_get_event_number(),
d2873e4c
JS
1207 sizeof(event_data), (char *) &event_data,
1208 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1209
09372820 1210 lpfc_offline_eratt(phba);
dea3101e 1211 }
9399627f 1212 return;
dea3101e 1213}
1214
da0436e9
JS
1215/**
1216 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1217 * @phba: pointer to lpfc hba data structure.
1218 *
1219 * This routine is invoked to handle the SLI4 HBA hardware error attention
1220 * conditions.
1221 **/
1222static void
1223lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1224{
1225 struct lpfc_vport *vport = phba->pport;
1226 uint32_t event_data;
1227 struct Scsi_Host *shost;
1228
1229 /* If the pci channel is offline, ignore possible errors, since
1230 * we cannot communicate with the pci card anyway.
1231 */
1232 if (pci_channel_offline(phba->pcidev))
1233 return;
1234 /* If resets are disabled then leave the HBA alone and return */
1235 if (!phba->cfg_enable_hba_reset)
1236 return;
1237
1238 /* Send an internal error event to mgmt application */
1239 lpfc_board_errevt_to_mgmt(phba);
1240
1241 /* For now, the actual action for SLI4 device handling is not
1242 * specified yet, just treated it as adaptor hardware failure
1243 */
1244 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1245 "0143 SLI4 Adapter Hardware Error Data: x%x x%x\n",
1246 phba->work_status[0], phba->work_status[1]);
1247
1248 event_data = FC_REG_DUMP_EVENT;
1249 shost = lpfc_shost_from_vport(vport);
1250 fc_host_post_vendor_event(shost, fc_get_event_number(),
1251 sizeof(event_data), (char *) &event_data,
1252 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1253
1254 lpfc_sli4_offline_eratt(phba);
1255}
1256
1257/**
1258 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1259 * @phba: pointer to lpfc HBA data structure.
1260 *
1261 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1262 * routine from the API jump table function pointer from the lpfc_hba struct.
1263 *
1264 * Return codes
af901ca1 1265 * 0 - success.
da0436e9
JS
1266 * Any other value - error.
1267 **/
1268void
1269lpfc_handle_eratt(struct lpfc_hba *phba)
1270{
1271 (*phba->lpfc_handle_eratt)(phba);
1272}
1273
e59058c4 1274/**
3621a710 1275 * lpfc_handle_latt - The HBA link event handler
e59058c4
JS
1276 * @phba: pointer to lpfc hba data structure.
1277 *
1278 * This routine is invoked from the worker thread to handle a HBA host
1279 * attention link event.
1280 **/
dea3101e 1281void
2e0fef85 1282lpfc_handle_latt(struct lpfc_hba *phba)
dea3101e 1283{
2e0fef85
JS
1284 struct lpfc_vport *vport = phba->pport;
1285 struct lpfc_sli *psli = &phba->sli;
dea3101e 1286 LPFC_MBOXQ_t *pmb;
1287 volatile uint32_t control;
1288 struct lpfc_dmabuf *mp;
09372820 1289 int rc = 0;
dea3101e 1290
1291 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
09372820
JS
1292 if (!pmb) {
1293 rc = 1;
dea3101e 1294 goto lpfc_handle_latt_err_exit;
09372820 1295 }
dea3101e 1296
1297 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
09372820
JS
1298 if (!mp) {
1299 rc = 2;
dea3101e 1300 goto lpfc_handle_latt_free_pmb;
09372820 1301 }
dea3101e 1302
1303 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
09372820
JS
1304 if (!mp->virt) {
1305 rc = 3;
dea3101e 1306 goto lpfc_handle_latt_free_mp;
09372820 1307 }
dea3101e 1308
6281bfe0 1309 /* Cleanup any outstanding ELS commands */
549e55cd 1310 lpfc_els_flush_all_cmd(phba);
dea3101e 1311
1312 psli->slistat.link_event++;
1313 lpfc_read_la(phba, pmb, mp);
1314 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_la;
2e0fef85 1315 pmb->vport = vport;
0d2b6b83
JS
1316 /* Block ELS IOCBs until we have processed this mbox command */
1317 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
0b727fea 1318 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
09372820
JS
1319 if (rc == MBX_NOT_FINISHED) {
1320 rc = 4;
14691150 1321 goto lpfc_handle_latt_free_mbuf;
09372820 1322 }
dea3101e 1323
1324 /* Clear Link Attention in HA REG */
2e0fef85 1325 spin_lock_irq(&phba->hbalock);
dea3101e 1326 writel(HA_LATT, phba->HAregaddr);
1327 readl(phba->HAregaddr); /* flush */
2e0fef85 1328 spin_unlock_irq(&phba->hbalock);
dea3101e 1329
1330 return;
1331
14691150 1332lpfc_handle_latt_free_mbuf:
0d2b6b83 1333 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
14691150 1334 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea3101e 1335lpfc_handle_latt_free_mp:
1336 kfree(mp);
1337lpfc_handle_latt_free_pmb:
1dcb58e5 1338 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 1339lpfc_handle_latt_err_exit:
1340 /* Enable Link attention interrupts */
2e0fef85 1341 spin_lock_irq(&phba->hbalock);
dea3101e 1342 psli->sli_flag |= LPFC_PROCESS_LA;
1343 control = readl(phba->HCregaddr);
1344 control |= HC_LAINT_ENA;
1345 writel(control, phba->HCregaddr);
1346 readl(phba->HCregaddr); /* flush */
1347
1348 /* Clear Link Attention in HA REG */
1349 writel(HA_LATT, phba->HAregaddr);
1350 readl(phba->HAregaddr); /* flush */
2e0fef85 1351 spin_unlock_irq(&phba->hbalock);
dea3101e 1352 lpfc_linkdown(phba);
2e0fef85 1353 phba->link_state = LPFC_HBA_ERROR;
dea3101e 1354
09372820
JS
1355 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1356 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea3101e 1357
1358 return;
1359}
1360
e59058c4 1361/**
3621a710 1362 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
e59058c4
JS
1363 * @phba: pointer to lpfc hba data structure.
1364 * @vpd: pointer to the vital product data.
1365 * @len: length of the vital product data in bytes.
1366 *
1367 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1368 * an array of characters. In this routine, the ModelName, ProgramType, and
1369 * ModelDesc, etc. fields of the phba data structure will be populated.
1370 *
1371 * Return codes
1372 * 0 - pointer to the VPD passed in is NULL
1373 * 1 - success
1374 **/
3772a991 1375int
2e0fef85 1376lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea3101e 1377{
1378 uint8_t lenlo, lenhi;
07da60c1 1379 int Length;
dea3101e 1380 int i, j;
1381 int finished = 0;
1382 int index = 0;
1383
1384 if (!vpd)
1385 return 0;
1386
1387 /* Vital Product */
ed957684 1388 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011 1389 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea3101e 1390 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1391 (uint32_t) vpd[3]);
74b72a59 1392 while (!finished && (index < (len - 4))) {
dea3101e 1393 switch (vpd[index]) {
1394 case 0x82:
74b72a59 1395 case 0x91:
dea3101e 1396 index += 1;
1397 lenlo = vpd[index];
1398 index += 1;
1399 lenhi = vpd[index];
1400 index += 1;
1401 i = ((((unsigned short)lenhi) << 8) + lenlo);
1402 index += i;
1403 break;
1404 case 0x90:
1405 index += 1;
1406 lenlo = vpd[index];
1407 index += 1;
1408 lenhi = vpd[index];
1409 index += 1;
1410 Length = ((((unsigned short)lenhi) << 8) + lenlo);
74b72a59
JW
1411 if (Length > len - index)
1412 Length = len - index;
dea3101e 1413 while (Length > 0) {
1414 /* Look for Serial Number */
1415 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1416 index += 2;
1417 i = vpd[index];
1418 index += 1;
1419 j = 0;
1420 Length -= (3+i);
1421 while(i--) {
1422 phba->SerialNumber[j++] = vpd[index++];
1423 if (j == 31)
1424 break;
1425 }
1426 phba->SerialNumber[j] = 0;
1427 continue;
1428 }
1429 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1430 phba->vpd_flag |= VPD_MODEL_DESC;
1431 index += 2;
1432 i = vpd[index];
1433 index += 1;
1434 j = 0;
1435 Length -= (3+i);
1436 while(i--) {
1437 phba->ModelDesc[j++] = vpd[index++];
1438 if (j == 255)
1439 break;
1440 }
1441 phba->ModelDesc[j] = 0;
1442 continue;
1443 }
1444 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1445 phba->vpd_flag |= VPD_MODEL_NAME;
1446 index += 2;
1447 i = vpd[index];
1448 index += 1;
1449 j = 0;
1450 Length -= (3+i);
1451 while(i--) {
1452 phba->ModelName[j++] = vpd[index++];
1453 if (j == 79)
1454 break;
1455 }
1456 phba->ModelName[j] = 0;
1457 continue;
1458 }
1459 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1460 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1461 index += 2;
1462 i = vpd[index];
1463 index += 1;
1464 j = 0;
1465 Length -= (3+i);
1466 while(i--) {
1467 phba->ProgramType[j++] = vpd[index++];
1468 if (j == 255)
1469 break;
1470 }
1471 phba->ProgramType[j] = 0;
1472 continue;
1473 }
1474 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1475 phba->vpd_flag |= VPD_PORT;
1476 index += 2;
1477 i = vpd[index];
1478 index += 1;
1479 j = 0;
1480 Length -= (3+i);
1481 while(i--) {
1482 phba->Port[j++] = vpd[index++];
1483 if (j == 19)
1484 break;
1485 }
1486 phba->Port[j] = 0;
1487 continue;
1488 }
1489 else {
1490 index += 2;
1491 i = vpd[index];
1492 index += 1;
1493 index += i;
1494 Length -= (3 + i);
1495 }
1496 }
1497 finished = 0;
1498 break;
1499 case 0x78:
1500 finished = 1;
1501 break;
1502 default:
1503 index ++;
1504 break;
1505 }
74b72a59 1506 }
dea3101e 1507
1508 return(1);
1509}
1510
e59058c4 1511/**
3621a710 1512 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
e59058c4
JS
1513 * @phba: pointer to lpfc hba data structure.
1514 * @mdp: pointer to the data structure to hold the derived model name.
1515 * @descp: pointer to the data structure to hold the derived description.
1516 *
1517 * This routine retrieves HBA's description based on its registered PCI device
1518 * ID. The @descp passed into this function points to an array of 256 chars. It
1519 * shall be returned with the model name, maximum speed, and the host bus type.
1520 * The @mdp passed into this function points to an array of 80 chars. When the
1521 * function returns, the @mdp will be filled with the model name.
1522 **/
dea3101e 1523static void
2e0fef85 1524lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea3101e 1525{
1526 lpfc_vpd_t *vp;
fefcb2b6 1527 uint16_t dev_id = phba->pcidev->device;
74b72a59 1528 int max_speed;
84774a4d 1529 int GE = 0;
da0436e9 1530 int oneConnect = 0; /* default is not a oneConnect */
74b72a59 1531 struct {
a747c9ce
JS
1532 char *name;
1533 char *bus;
1534 char *function;
1535 } m = {"<Unknown>", "", ""};
74b72a59
JW
1536
1537 if (mdp && mdp[0] != '\0'
1538 && descp && descp[0] != '\0')
1539 return;
1540
1541 if (phba->lmt & LMT_10Gb)
1542 max_speed = 10;
1543 else if (phba->lmt & LMT_8Gb)
1544 max_speed = 8;
1545 else if (phba->lmt & LMT_4Gb)
1546 max_speed = 4;
1547 else if (phba->lmt & LMT_2Gb)
1548 max_speed = 2;
1549 else
1550 max_speed = 1;
dea3101e 1551
1552 vp = &phba->vpd;
dea3101e 1553
e4adb204 1554 switch (dev_id) {
06325e74 1555 case PCI_DEVICE_ID_FIREFLY:
a747c9ce 1556 m = (typeof(m)){"LP6000", "PCI", "Fibre Channel Adapter"};
06325e74 1557 break;
dea3101e 1558 case PCI_DEVICE_ID_SUPERFLY:
1559 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
a747c9ce
JS
1560 m = (typeof(m)){"LP7000", "PCI",
1561 "Fibre Channel Adapter"};
dea3101e 1562 else
a747c9ce
JS
1563 m = (typeof(m)){"LP7000E", "PCI",
1564 "Fibre Channel Adapter"};
dea3101e 1565 break;
1566 case PCI_DEVICE_ID_DRAGONFLY:
a747c9ce
JS
1567 m = (typeof(m)){"LP8000", "PCI",
1568 "Fibre Channel Adapter"};
dea3101e 1569 break;
1570 case PCI_DEVICE_ID_CENTAUR:
1571 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
a747c9ce
JS
1572 m = (typeof(m)){"LP9002", "PCI",
1573 "Fibre Channel Adapter"};
dea3101e 1574 else
a747c9ce
JS
1575 m = (typeof(m)){"LP9000", "PCI",
1576 "Fibre Channel Adapter"};
dea3101e 1577 break;
1578 case PCI_DEVICE_ID_RFLY:
a747c9ce
JS
1579 m = (typeof(m)){"LP952", "PCI",
1580 "Fibre Channel Adapter"};
dea3101e 1581 break;
1582 case PCI_DEVICE_ID_PEGASUS:
a747c9ce
JS
1583 m = (typeof(m)){"LP9802", "PCI-X",
1584 "Fibre Channel Adapter"};
dea3101e 1585 break;
1586 case PCI_DEVICE_ID_THOR:
a747c9ce
JS
1587 m = (typeof(m)){"LP10000", "PCI-X",
1588 "Fibre Channel Adapter"};
dea3101e 1589 break;
1590 case PCI_DEVICE_ID_VIPER:
a747c9ce
JS
1591 m = (typeof(m)){"LPX1000", "PCI-X",
1592 "Fibre Channel Adapter"};
dea3101e 1593 break;
1594 case PCI_DEVICE_ID_PFLY:
a747c9ce
JS
1595 m = (typeof(m)){"LP982", "PCI-X",
1596 "Fibre Channel Adapter"};
dea3101e 1597 break;
1598 case PCI_DEVICE_ID_TFLY:
a747c9ce
JS
1599 m = (typeof(m)){"LP1050", "PCI-X",
1600 "Fibre Channel Adapter"};
dea3101e 1601 break;
1602 case PCI_DEVICE_ID_HELIOS:
a747c9ce
JS
1603 m = (typeof(m)){"LP11000", "PCI-X2",
1604 "Fibre Channel Adapter"};
dea3101e 1605 break;
e4adb204 1606 case PCI_DEVICE_ID_HELIOS_SCSP:
a747c9ce
JS
1607 m = (typeof(m)){"LP11000-SP", "PCI-X2",
1608 "Fibre Channel Adapter"};
e4adb204
JSEC
1609 break;
1610 case PCI_DEVICE_ID_HELIOS_DCSP:
a747c9ce
JS
1611 m = (typeof(m)){"LP11002-SP", "PCI-X2",
1612 "Fibre Channel Adapter"};
e4adb204
JSEC
1613 break;
1614 case PCI_DEVICE_ID_NEPTUNE:
a747c9ce 1615 m = (typeof(m)){"LPe1000", "PCIe", "Fibre Channel Adapter"};
e4adb204
JSEC
1616 break;
1617 case PCI_DEVICE_ID_NEPTUNE_SCSP:
a747c9ce 1618 m = (typeof(m)){"LPe1000-SP", "PCIe", "Fibre Channel Adapter"};
e4adb204
JSEC
1619 break;
1620 case PCI_DEVICE_ID_NEPTUNE_DCSP:
a747c9ce 1621 m = (typeof(m)){"LPe1002-SP", "PCIe", "Fibre Channel Adapter"};
e4adb204 1622 break;
dea3101e 1623 case PCI_DEVICE_ID_BMID:
a747c9ce 1624 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea3101e 1625 break;
1626 case PCI_DEVICE_ID_BSMB:
a747c9ce 1627 m = (typeof(m)){"LP111", "PCI-X2", "Fibre Channel Adapter"};
dea3101e 1628 break;
1629 case PCI_DEVICE_ID_ZEPHYR:
a747c9ce 1630 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea3101e 1631 break;
e4adb204 1632 case PCI_DEVICE_ID_ZEPHYR_SCSP:
a747c9ce 1633 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
e4adb204
JSEC
1634 break;
1635 case PCI_DEVICE_ID_ZEPHYR_DCSP:
a747c9ce 1636 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
a257bf90 1637 GE = 1;
e4adb204 1638 break;
dea3101e 1639 case PCI_DEVICE_ID_ZMID:
a747c9ce 1640 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea3101e 1641 break;
1642 case PCI_DEVICE_ID_ZSMB:
a747c9ce 1643 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea3101e 1644 break;
1645 case PCI_DEVICE_ID_LP101:
a747c9ce 1646 m = (typeof(m)){"LP101", "PCI-X", "Fibre Channel Adapter"};
dea3101e 1647 break;
1648 case PCI_DEVICE_ID_LP10000S:
a747c9ce 1649 m = (typeof(m)){"LP10000-S", "PCI", "Fibre Channel Adapter"};
06325e74 1650 break;
e4adb204 1651 case PCI_DEVICE_ID_LP11000S:
a747c9ce 1652 m = (typeof(m)){"LP11000-S", "PCI-X2", "Fibre Channel Adapter"};
18a3b596 1653 break;
e4adb204 1654 case PCI_DEVICE_ID_LPE11000S:
a747c9ce 1655 m = (typeof(m)){"LPe11000-S", "PCIe", "Fibre Channel Adapter"};
5cc36b3c 1656 break;
b87eab38 1657 case PCI_DEVICE_ID_SAT:
a747c9ce 1658 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
b87eab38
JS
1659 break;
1660 case PCI_DEVICE_ID_SAT_MID:
a747c9ce 1661 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
b87eab38
JS
1662 break;
1663 case PCI_DEVICE_ID_SAT_SMB:
a747c9ce 1664 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
b87eab38
JS
1665 break;
1666 case PCI_DEVICE_ID_SAT_DCSP:
a747c9ce 1667 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
b87eab38
JS
1668 break;
1669 case PCI_DEVICE_ID_SAT_SCSP:
a747c9ce 1670 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
b87eab38
JS
1671 break;
1672 case PCI_DEVICE_ID_SAT_S:
a747c9ce 1673 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
b87eab38 1674 break;
84774a4d 1675 case PCI_DEVICE_ID_HORNET:
a747c9ce 1676 m = (typeof(m)){"LP21000", "PCIe", "FCoE Adapter"};
84774a4d
JS
1677 GE = 1;
1678 break;
1679 case PCI_DEVICE_ID_PROTEUS_VF:
a747c9ce
JS
1680 m = (typeof(m)){"LPev12000", "PCIe IOV",
1681 "Fibre Channel Adapter"};
84774a4d
JS
1682 break;
1683 case PCI_DEVICE_ID_PROTEUS_PF:
a747c9ce
JS
1684 m = (typeof(m)){"LPev12000", "PCIe IOV",
1685 "Fibre Channel Adapter"};
84774a4d
JS
1686 break;
1687 case PCI_DEVICE_ID_PROTEUS_S:
a747c9ce
JS
1688 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
1689 "Fibre Channel Adapter"};
84774a4d 1690 break;
da0436e9
JS
1691 case PCI_DEVICE_ID_TIGERSHARK:
1692 oneConnect = 1;
a747c9ce 1693 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
da0436e9 1694 break;
a747c9ce 1695 case PCI_DEVICE_ID_TOMCAT:
6669f9bb 1696 oneConnect = 1;
a747c9ce
JS
1697 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
1698 break;
1699 case PCI_DEVICE_ID_FALCON:
1700 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
1701 "EmulexSecure Fibre"};
6669f9bb 1702 break;
5cc36b3c 1703 default:
a747c9ce 1704 m = (typeof(m)){"Unknown", "", ""};
e4adb204 1705 break;
dea3101e 1706 }
74b72a59
JW
1707
1708 if (mdp && mdp[0] == '\0')
1709 snprintf(mdp, 79,"%s", m.name);
da0436e9
JS
1710 /* oneConnect hba requires special processing, they are all initiators
1711 * and we put the port number on the end
1712 */
1713 if (descp && descp[0] == '\0') {
1714 if (oneConnect)
1715 snprintf(descp, 255,
a747c9ce
JS
1716 "Emulex OneConnect %s, %s Initiator, Port %s",
1717 m.name, m.function,
da0436e9
JS
1718 phba->Port);
1719 else
1720 snprintf(descp, 255,
1721 "Emulex %s %d%s %s %s",
a747c9ce
JS
1722 m.name, max_speed, (GE) ? "GE" : "Gb",
1723 m.bus, m.function);
da0436e9 1724 }
dea3101e 1725}
1726
e59058c4 1727/**
3621a710 1728 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
e59058c4
JS
1729 * @phba: pointer to lpfc hba data structure.
1730 * @pring: pointer to a IOCB ring.
1731 * @cnt: the number of IOCBs to be posted to the IOCB ring.
1732 *
1733 * This routine posts a given number of IOCBs with the associated DMA buffer
1734 * descriptors specified by the cnt argument to the given IOCB ring.
1735 *
1736 * Return codes
1737 * The number of IOCBs NOT able to be posted to the IOCB ring.
1738 **/
dea3101e 1739int
495a714c 1740lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea3101e 1741{
1742 IOCB_t *icmd;
0bd4ca25 1743 struct lpfc_iocbq *iocb;
dea3101e 1744 struct lpfc_dmabuf *mp1, *mp2;
1745
1746 cnt += pring->missbufcnt;
1747
1748 /* While there are buffers to post */
1749 while (cnt > 0) {
1750 /* Allocate buffer for command iocb */
0bd4ca25 1751 iocb = lpfc_sli_get_iocbq(phba);
dea3101e 1752 if (iocb == NULL) {
1753 pring->missbufcnt = cnt;
1754 return cnt;
1755 }
dea3101e 1756 icmd = &iocb->iocb;
1757
1758 /* 2 buffers can be posted per command */
1759 /* Allocate buffer to post */
1760 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1761 if (mp1)
98c9ea5c
JS
1762 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
1763 if (!mp1 || !mp1->virt) {
c9475cb0 1764 kfree(mp1);
604a3e30 1765 lpfc_sli_release_iocbq(phba, iocb);
dea3101e 1766 pring->missbufcnt = cnt;
1767 return cnt;
1768 }
1769
1770 INIT_LIST_HEAD(&mp1->list);
1771 /* Allocate buffer to post */
1772 if (cnt > 1) {
1773 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1774 if (mp2)
1775 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
1776 &mp2->phys);
98c9ea5c 1777 if (!mp2 || !mp2->virt) {
c9475cb0 1778 kfree(mp2);
dea3101e 1779 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1780 kfree(mp1);
604a3e30 1781 lpfc_sli_release_iocbq(phba, iocb);
dea3101e 1782 pring->missbufcnt = cnt;
1783 return cnt;
1784 }
1785
1786 INIT_LIST_HEAD(&mp2->list);
1787 } else {
1788 mp2 = NULL;
1789 }
1790
1791 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
1792 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
1793 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
1794 icmd->ulpBdeCount = 1;
1795 cnt--;
1796 if (mp2) {
1797 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
1798 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
1799 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
1800 cnt--;
1801 icmd->ulpBdeCount = 2;
1802 }
1803
1804 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
1805 icmd->ulpLe = 1;
1806
3772a991
JS
1807 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
1808 IOCB_ERROR) {
dea3101e 1809 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1810 kfree(mp1);
1811 cnt++;
1812 if (mp2) {
1813 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
1814 kfree(mp2);
1815 cnt++;
1816 }
604a3e30 1817 lpfc_sli_release_iocbq(phba, iocb);
dea3101e 1818 pring->missbufcnt = cnt;
dea3101e 1819 return cnt;
1820 }
dea3101e 1821 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
92d7f7b0 1822 if (mp2)
dea3101e 1823 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea3101e 1824 }
1825 pring->missbufcnt = 0;
1826 return 0;
1827}
1828
e59058c4 1829/**
3621a710 1830 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
e59058c4
JS
1831 * @phba: pointer to lpfc hba data structure.
1832 *
1833 * This routine posts initial receive IOCB buffers to the ELS ring. The
1834 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
1835 * set to 64 IOCBs.
1836 *
1837 * Return codes
1838 * 0 - success (currently always success)
1839 **/
dea3101e 1840static int
2e0fef85 1841lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea3101e 1842{
1843 struct lpfc_sli *psli = &phba->sli;
1844
1845 /* Ring 0, ELS / CT buffers */
495a714c 1846 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea3101e 1847 /* Ring 2 - FCP no buffers needed */
1848
1849 return 0;
1850}
1851
1852#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
1853
e59058c4 1854/**
3621a710 1855 * lpfc_sha_init - Set up initial array of hash table entries
e59058c4
JS
1856 * @HashResultPointer: pointer to an array as hash table.
1857 *
1858 * This routine sets up the initial values to the array of hash table entries
1859 * for the LC HBAs.
1860 **/
dea3101e 1861static void
1862lpfc_sha_init(uint32_t * HashResultPointer)
1863{
1864 HashResultPointer[0] = 0x67452301;
1865 HashResultPointer[1] = 0xEFCDAB89;
1866 HashResultPointer[2] = 0x98BADCFE;
1867 HashResultPointer[3] = 0x10325476;
1868 HashResultPointer[4] = 0xC3D2E1F0;
1869}
1870
e59058c4 1871/**
3621a710 1872 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
e59058c4
JS
1873 * @HashResultPointer: pointer to an initial/result hash table.
1874 * @HashWorkingPointer: pointer to an working hash table.
1875 *
1876 * This routine iterates an initial hash table pointed by @HashResultPointer
1877 * with the values from the working hash table pointeed by @HashWorkingPointer.
1878 * The results are putting back to the initial hash table, returned through
1879 * the @HashResultPointer as the result hash table.
1880 **/
dea3101e 1881static void
1882lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
1883{
1884 int t;
1885 uint32_t TEMP;
1886 uint32_t A, B, C, D, E;
1887 t = 16;
1888 do {
1889 HashWorkingPointer[t] =
1890 S(1,
1891 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
1892 8] ^
1893 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
1894 } while (++t <= 79);
1895 t = 0;
1896 A = HashResultPointer[0];
1897 B = HashResultPointer[1];
1898 C = HashResultPointer[2];
1899 D = HashResultPointer[3];
1900 E = HashResultPointer[4];
1901
1902 do {
1903 if (t < 20) {
1904 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
1905 } else if (t < 40) {
1906 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
1907 } else if (t < 60) {
1908 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
1909 } else {
1910 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
1911 }
1912 TEMP += S(5, A) + E + HashWorkingPointer[t];
1913 E = D;
1914 D = C;
1915 C = S(30, B);
1916 B = A;
1917 A = TEMP;
1918 } while (++t <= 79);
1919
1920 HashResultPointer[0] += A;
1921 HashResultPointer[1] += B;
1922 HashResultPointer[2] += C;
1923 HashResultPointer[3] += D;
1924 HashResultPointer[4] += E;
1925
1926}
1927
e59058c4 1928/**
3621a710 1929 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
e59058c4
JS
1930 * @RandomChallenge: pointer to the entry of host challenge random number array.
1931 * @HashWorking: pointer to the entry of the working hash array.
1932 *
1933 * This routine calculates the working hash array referred by @HashWorking
1934 * from the challenge random numbers associated with the host, referred by
1935 * @RandomChallenge. The result is put into the entry of the working hash
1936 * array and returned by reference through @HashWorking.
1937 **/
dea3101e 1938static void
1939lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
1940{
1941 *HashWorking = (*RandomChallenge ^ *HashWorking);
1942}
1943
e59058c4 1944/**
3621a710 1945 * lpfc_hba_init - Perform special handling for LC HBA initialization
e59058c4
JS
1946 * @phba: pointer to lpfc hba data structure.
1947 * @hbainit: pointer to an array of unsigned 32-bit integers.
1948 *
1949 * This routine performs the special handling for LC HBA initialization.
1950 **/
dea3101e 1951void
1952lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
1953{
1954 int t;
1955 uint32_t *HashWorking;
2e0fef85 1956 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea3101e 1957
bbfbbbc1 1958 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea3101e 1959 if (!HashWorking)
1960 return;
1961
dea3101e 1962 HashWorking[0] = HashWorking[78] = *pwwnn++;
1963 HashWorking[1] = HashWorking[79] = *pwwnn;
1964
1965 for (t = 0; t < 7; t++)
1966 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
1967
1968 lpfc_sha_init(hbainit);
1969 lpfc_sha_iterate(hbainit, HashWorking);
1970 kfree(HashWorking);
1971}
1972
e59058c4 1973/**
3621a710 1974 * lpfc_cleanup - Performs vport cleanups before deleting a vport
e59058c4
JS
1975 * @vport: pointer to a virtual N_Port data structure.
1976 *
1977 * This routine performs the necessary cleanups before deleting the @vport.
1978 * It invokes the discovery state machine to perform necessary state
1979 * transitions and to release the ndlps associated with the @vport. Note,
1980 * the physical port is treated as @vport 0.
1981 **/
87af33fe 1982void
2e0fef85 1983lpfc_cleanup(struct lpfc_vport *vport)
dea3101e 1984{
87af33fe 1985 struct lpfc_hba *phba = vport->phba;
dea3101e 1986 struct lpfc_nodelist *ndlp, *next_ndlp;
a8adb832 1987 int i = 0;
dea3101e 1988
87af33fe
JS
1989 if (phba->link_state > LPFC_LINK_DOWN)
1990 lpfc_port_link_failure(vport);
1991
1992 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
e47c9093
JS
1993 if (!NLP_CHK_NODE_ACT(ndlp)) {
1994 ndlp = lpfc_enable_node(vport, ndlp,
1995 NLP_STE_UNUSED_NODE);
1996 if (!ndlp)
1997 continue;
1998 spin_lock_irq(&phba->ndlp_lock);
1999 NLP_SET_FREE_REQ(ndlp);
2000 spin_unlock_irq(&phba->ndlp_lock);
2001 /* Trigger the release of the ndlp memory */
2002 lpfc_nlp_put(ndlp);
2003 continue;
2004 }
2005 spin_lock_irq(&phba->ndlp_lock);
2006 if (NLP_CHK_FREE_REQ(ndlp)) {
2007 /* The ndlp should not be in memory free mode already */
2008 spin_unlock_irq(&phba->ndlp_lock);
2009 continue;
2010 } else
2011 /* Indicate request for freeing ndlp memory */
2012 NLP_SET_FREE_REQ(ndlp);
2013 spin_unlock_irq(&phba->ndlp_lock);
2014
58da1ffb
JS
2015 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2016 ndlp->nlp_DID == Fabric_DID) {
2017 /* Just free up ndlp with Fabric_DID for vports */
2018 lpfc_nlp_put(ndlp);
2019 continue;
2020 }
2021
87af33fe
JS
2022 if (ndlp->nlp_type & NLP_FABRIC)
2023 lpfc_disc_state_machine(vport, ndlp, NULL,
2024 NLP_EVT_DEVICE_RECOVERY);
e47c9093 2025
87af33fe
JS
2026 lpfc_disc_state_machine(vport, ndlp, NULL,
2027 NLP_EVT_DEVICE_RM);
495a714c 2028
87af33fe
JS
2029 }
2030
a8adb832
JS
2031 /* At this point, ALL ndlp's should be gone
2032 * because of the previous NLP_EVT_DEVICE_RM.
2033 * Lets wait for this to happen, if needed.
2034 */
87af33fe 2035 while (!list_empty(&vport->fc_nodes)) {
a8adb832 2036 if (i++ > 3000) {
87af33fe 2037 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
a8adb832 2038 "0233 Nodelist not empty\n");
e47c9093
JS
2039 list_for_each_entry_safe(ndlp, next_ndlp,
2040 &vport->fc_nodes, nlp_listp) {
2041 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2042 LOG_NODE,
d7c255b2 2043 "0282 did:x%x ndlp:x%p "
e47c9093
JS
2044 "usgmap:x%x refcnt:%d\n",
2045 ndlp->nlp_DID, (void *)ndlp,
2046 ndlp->nlp_usg_map,
2047 atomic_read(
2048 &ndlp->kref.refcount));
2049 }
a8adb832 2050 break;
87af33fe 2051 }
a8adb832
JS
2052
2053 /* Wait for any activity on ndlps to settle */
2054 msleep(10);
87af33fe 2055 }
dea3101e 2056}
2057
e59058c4 2058/**
3621a710 2059 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
e59058c4
JS
2060 * @vport: pointer to a virtual N_Port data structure.
2061 *
2062 * This routine stops all the timers associated with a @vport. This function
2063 * is invoked before disabling or deleting a @vport. Note that the physical
2064 * port is treated as @vport 0.
2065 **/
92d7f7b0
JS
2066void
2067lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea3101e 2068{
92d7f7b0
JS
2069 del_timer_sync(&vport->els_tmofunc);
2070 del_timer_sync(&vport->fc_fdmitmo);
2071 lpfc_can_disctmo(vport);
2072 return;
dea3101e 2073}
2074
ecfd03c6
JS
2075/**
2076 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2077 * @phba: pointer to lpfc hba data structure.
2078 *
2079 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2080 * caller of this routine should already hold the host lock.
2081 **/
2082void
2083__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2084{
2085 /* Clear pending FCF rediscovery wait timer */
2086 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2087 /* Now, try to stop the timer */
2088 del_timer(&phba->fcf.redisc_wait);
2089}
2090
2091/**
2092 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2093 * @phba: pointer to lpfc hba data structure.
2094 *
2095 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2096 * checks whether the FCF rediscovery wait timer is pending with the host
2097 * lock held before proceeding with disabling the timer and clearing the
2098 * wait timer pendig flag.
2099 **/
2100void
2101lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2102{
2103 spin_lock_irq(&phba->hbalock);
2104 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2105 /* FCF rediscovery timer already fired or stopped */
2106 spin_unlock_irq(&phba->hbalock);
2107 return;
2108 }
2109 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
2110 spin_unlock_irq(&phba->hbalock);
2111}
2112
e59058c4 2113/**
3772a991 2114 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
e59058c4
JS
2115 * @phba: pointer to lpfc hba data structure.
2116 *
2117 * This routine stops all the timers associated with a HBA. This function is
2118 * invoked before either putting a HBA offline or unloading the driver.
2119 **/
3772a991
JS
2120void
2121lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea3101e 2122{
51ef4c26 2123 lpfc_stop_vport_timers(phba->pport);
2e0fef85 2124 del_timer_sync(&phba->sli.mbox_tmo);
92d7f7b0 2125 del_timer_sync(&phba->fabric_block_timer);
9399627f 2126 del_timer_sync(&phba->eratt_poll);
3772a991
JS
2127 del_timer_sync(&phba->hb_tmofunc);
2128 phba->hb_outstanding = 0;
2129
2130 switch (phba->pci_dev_grp) {
2131 case LPFC_PCI_DEV_LP:
2132 /* Stop any LightPulse device specific driver timers */
2133 del_timer_sync(&phba->fcp_poll_timer);
2134 break;
2135 case LPFC_PCI_DEV_OC:
2136 /* Stop any OneConnect device sepcific driver timers */
ecfd03c6 2137 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
3772a991
JS
2138 break;
2139 default:
2140 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2141 "0297 Invalid device group (x%x)\n",
2142 phba->pci_dev_grp);
2143 break;
2144 }
2e0fef85 2145 return;
dea3101e 2146}
2147
e59058c4 2148/**
3621a710 2149 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
e59058c4
JS
2150 * @phba: pointer to lpfc hba data structure.
2151 *
2152 * This routine marks a HBA's management interface as blocked. Once the HBA's
2153 * management interface is marked as blocked, all the user space access to
2154 * the HBA, whether they are from sysfs interface or libdfc interface will
2155 * all be blocked. The HBA is set to block the management interface when the
2156 * driver prepares the HBA interface for online or offline.
2157 **/
a6ababd2
AB
2158static void
2159lpfc_block_mgmt_io(struct lpfc_hba * phba)
2160{
2161 unsigned long iflag;
2162
2163 spin_lock_irqsave(&phba->hbalock, iflag);
2164 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
2165 spin_unlock_irqrestore(&phba->hbalock, iflag);
2166}
2167
e59058c4 2168/**
3621a710 2169 * lpfc_online - Initialize and bring a HBA online
e59058c4
JS
2170 * @phba: pointer to lpfc hba data structure.
2171 *
2172 * This routine initializes the HBA and brings a HBA online. During this
2173 * process, the management interface is blocked to prevent user space access
2174 * to the HBA interfering with the driver initialization.
2175 *
2176 * Return codes
2177 * 0 - successful
2178 * 1 - failed
2179 **/
dea3101e 2180int
2e0fef85 2181lpfc_online(struct lpfc_hba *phba)
dea3101e 2182{
372bd282 2183 struct lpfc_vport *vport;
549e55cd
JS
2184 struct lpfc_vport **vports;
2185 int i;
2e0fef85 2186
dea3101e 2187 if (!phba)
2188 return 0;
372bd282 2189 vport = phba->pport;
dea3101e 2190
2e0fef85 2191 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea3101e 2192 return 0;
2193
ed957684 2194 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
e8b62011 2195 "0458 Bring Adapter online\n");
dea3101e 2196
46fa311e
JS
2197 lpfc_block_mgmt_io(phba);
2198
2199 if (!lpfc_sli_queue_setup(phba)) {
2200 lpfc_unblock_mgmt_io(phba);
dea3101e 2201 return 1;
46fa311e 2202 }
dea3101e 2203
da0436e9
JS
2204 if (phba->sli_rev == LPFC_SLI_REV4) {
2205 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2206 lpfc_unblock_mgmt_io(phba);
2207 return 1;
2208 }
2209 } else {
2210 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2211 lpfc_unblock_mgmt_io(phba);
2212 return 1;
2213 }
46fa311e 2214 }
dea3101e 2215
549e55cd
JS
2216 vports = lpfc_create_vport_work_array(phba);
2217 if (vports != NULL)
da0436e9 2218 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
549e55cd
JS
2219 struct Scsi_Host *shost;
2220 shost = lpfc_shost_from_vport(vports[i]);
2221 spin_lock_irq(shost->host_lock);
2222 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2223 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2224 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
1c6834a7
JS
2225 if (phba->sli_rev == LPFC_SLI_REV4)
2226 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
549e55cd
JS
2227 spin_unlock_irq(shost->host_lock);
2228 }
09372820 2229 lpfc_destroy_vport_work_array(phba, vports);
dea3101e 2230
46fa311e 2231 lpfc_unblock_mgmt_io(phba);
dea3101e 2232 return 0;
2233}
2234
e59058c4 2235/**
3621a710 2236 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
e59058c4
JS
2237 * @phba: pointer to lpfc hba data structure.
2238 *
2239 * This routine marks a HBA's management interface as not blocked. Once the
2240 * HBA's management interface is marked as not blocked, all the user space
2241 * access to the HBA, whether they are from sysfs interface or libdfc
2242 * interface will be allowed. The HBA is set to block the management interface
2243 * when the driver prepares the HBA interface for online or offline and then
2244 * set to unblock the management interface afterwards.
2245 **/
46fa311e
JS
2246void
2247lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2248{
2249 unsigned long iflag;
2250
2e0fef85
JS
2251 spin_lock_irqsave(&phba->hbalock, iflag);
2252 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2253 spin_unlock_irqrestore(&phba->hbalock, iflag);
46fa311e
JS
2254}
2255
e59058c4 2256/**
3621a710 2257 * lpfc_offline_prep - Prepare a HBA to be brought offline
e59058c4
JS
2258 * @phba: pointer to lpfc hba data structure.
2259 *
2260 * This routine is invoked to prepare a HBA to be brought offline. It performs
2261 * unregistration login to all the nodes on all vports and flushes the mailbox
2262 * queue to make it ready to be brought offline.
2263 **/
46fa311e
JS
2264void
2265lpfc_offline_prep(struct lpfc_hba * phba)
2266{
2e0fef85 2267 struct lpfc_vport *vport = phba->pport;
46fa311e 2268 struct lpfc_nodelist *ndlp, *next_ndlp;
87af33fe
JS
2269 struct lpfc_vport **vports;
2270 int i;
dea3101e 2271
2e0fef85 2272 if (vport->fc_flag & FC_OFFLINE_MODE)
46fa311e 2273 return;
dea3101e 2274
46fa311e 2275 lpfc_block_mgmt_io(phba);
dea3101e 2276
2277 lpfc_linkdown(phba);
2278
87af33fe
JS
2279 /* Issue an unreg_login to all nodes on all vports */
2280 vports = lpfc_create_vport_work_array(phba);
2281 if (vports != NULL) {
da0436e9 2282 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
87af33fe
JS
2283 struct Scsi_Host *shost;
2284
a8adb832
JS
2285 if (vports[i]->load_flag & FC_UNLOADING)
2286 continue;
c868595d 2287 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
695a814e
JS
2288 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2289 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
2290
87af33fe
JS
2291 shost = lpfc_shost_from_vport(vports[i]);
2292 list_for_each_entry_safe(ndlp, next_ndlp,
2293 &vports[i]->fc_nodes,
2294 nlp_listp) {
e47c9093
JS
2295 if (!NLP_CHK_NODE_ACT(ndlp))
2296 continue;
87af33fe
JS
2297 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2298 continue;
2299 if (ndlp->nlp_type & NLP_FABRIC) {
2300 lpfc_disc_state_machine(vports[i], ndlp,
2301 NULL, NLP_EVT_DEVICE_RECOVERY);
2302 lpfc_disc_state_machine(vports[i], ndlp,
2303 NULL, NLP_EVT_DEVICE_RM);
2304 }
2305 spin_lock_irq(shost->host_lock);
2306 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2307 spin_unlock_irq(shost->host_lock);
2308 lpfc_unreg_rpi(vports[i], ndlp);
2309 }
2310 }
2311 }
09372820 2312 lpfc_destroy_vport_work_array(phba, vports);
dea3101e 2313
04c68496 2314 lpfc_sli_mbox_sys_shutdown(phba);
46fa311e
JS
2315}
2316
e59058c4 2317/**
3621a710 2318 * lpfc_offline - Bring a HBA offline
e59058c4
JS
2319 * @phba: pointer to lpfc hba data structure.
2320 *
2321 * This routine actually brings a HBA offline. It stops all the timers
2322 * associated with the HBA, brings down the SLI layer, and eventually
2323 * marks the HBA as in offline state for the upper layer protocol.
2324 **/
46fa311e 2325void
2e0fef85 2326lpfc_offline(struct lpfc_hba *phba)
46fa311e 2327{
549e55cd
JS
2328 struct Scsi_Host *shost;
2329 struct lpfc_vport **vports;
2330 int i;
46fa311e 2331
549e55cd 2332 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
46fa311e 2333 return;
688a8863 2334
da0436e9
JS
2335 /* stop port and all timers associated with this hba */
2336 lpfc_stop_port(phba);
51ef4c26
JS
2337 vports = lpfc_create_vport_work_array(phba);
2338 if (vports != NULL)
da0436e9 2339 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
51ef4c26 2340 lpfc_stop_vport_timers(vports[i]);
09372820 2341 lpfc_destroy_vport_work_array(phba, vports);
92d7f7b0 2342 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
e8b62011 2343 "0460 Bring Adapter offline\n");
dea3101e 2344 /* Bring down the SLI Layer and cleanup. The HBA is offline
2345 now. */
2346 lpfc_sli_hba_down(phba);
92d7f7b0 2347 spin_lock_irq(&phba->hbalock);
7054a606 2348 phba->work_ha = 0;
92d7f7b0 2349 spin_unlock_irq(&phba->hbalock);
549e55cd
JS
2350 vports = lpfc_create_vport_work_array(phba);
2351 if (vports != NULL)
da0436e9 2352 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
549e55cd 2353 shost = lpfc_shost_from_vport(vports[i]);
549e55cd
JS
2354 spin_lock_irq(shost->host_lock);
2355 vports[i]->work_port_events = 0;
2356 vports[i]->fc_flag |= FC_OFFLINE_MODE;
2357 spin_unlock_irq(shost->host_lock);
2358 }
09372820 2359 lpfc_destroy_vport_work_array(phba, vports);
dea3101e 2360}
2361
e59058c4 2362/**
3621a710 2363 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
e59058c4
JS
2364 * @phba: pointer to lpfc hba data structure.
2365 *
2366 * This routine is to free all the SCSI buffers and IOCBs from the driver
2367 * list back to kernel. It is called from lpfc_pci_remove_one to free
2368 * the internal resources before the device is removed from the system.
2369 *
2370 * Return codes
2371 * 0 - successful (for now, it always returns 0)
2372 **/
dea3101e 2373static int
2e0fef85 2374lpfc_scsi_free(struct lpfc_hba *phba)
dea3101e 2375{
2376 struct lpfc_scsi_buf *sb, *sb_next;
2377 struct lpfc_iocbq *io, *io_next;
2378
2e0fef85 2379 spin_lock_irq(&phba->hbalock);
dea3101e 2380 /* Release all the lpfc_scsi_bufs maintained by this host. */
1c6f4ef5 2381 spin_lock(&phba->scsi_buf_list_lock);
dea3101e 2382 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
2383 list_del(&sb->list);
2384 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
92d7f7b0 2385 sb->dma_handle);
dea3101e 2386 kfree(sb);
2387 phba->total_scsi_bufs--;
2388 }
1c6f4ef5 2389 spin_unlock(&phba->scsi_buf_list_lock);
dea3101e 2390
2391 /* Release all the lpfc_iocbq entries maintained by this host. */
2392 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2393 list_del(&io->list);
2394 kfree(io);
2395 phba->total_iocbq_bufs--;
2396 }
2e0fef85 2397 spin_unlock_irq(&phba->hbalock);
dea3101e 2398 return 0;
2399}
2400
e59058c4 2401/**
3621a710 2402 * lpfc_create_port - Create an FC port
e59058c4
JS
2403 * @phba: pointer to lpfc hba data structure.
2404 * @instance: a unique integer ID to this FC port.
2405 * @dev: pointer to the device data structure.
2406 *
2407 * This routine creates a FC port for the upper layer protocol. The FC port
2408 * can be created on top of either a physical port or a virtual port provided
2409 * by the HBA. This routine also allocates a SCSI host data structure (shost)
2410 * and associates the FC port created before adding the shost into the SCSI
2411 * layer.
2412 *
2413 * Return codes
2414 * @vport - pointer to the virtual N_Port data structure.
2415 * NULL - port create failed.
2416 **/
2e0fef85 2417struct lpfc_vport *
3de2a653 2418lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
47a8617c 2419{
2e0fef85
JS
2420 struct lpfc_vport *vport;
2421 struct Scsi_Host *shost;
2422 int error = 0;
47a8617c 2423
3de2a653
JS
2424 if (dev != &phba->pcidev->dev)
2425 shost = scsi_host_alloc(&lpfc_vport_template,
2426 sizeof(struct lpfc_vport));
2427 else
2428 shost = scsi_host_alloc(&lpfc_template,
2429 sizeof(struct lpfc_vport));
2e0fef85
JS
2430 if (!shost)
2431 goto out;
47a8617c 2432
2e0fef85
JS
2433 vport = (struct lpfc_vport *) shost->hostdata;
2434 vport->phba = phba;
2e0fef85 2435 vport->load_flag |= FC_LOADING;
92d7f7b0 2436 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
7f5f3d0d 2437 vport->fc_rscn_flush = 0;
47a8617c 2438
3de2a653 2439 lpfc_get_vport_cfgparam(vport);
2e0fef85
JS
2440 shost->unique_id = instance;
2441 shost->max_id = LPFC_MAX_TARGET;
3de2a653 2442 shost->max_lun = vport->cfg_max_luns;
2e0fef85
JS
2443 shost->this_id = -1;
2444 shost->max_cmd_len = 16;
da0436e9 2445 if (phba->sli_rev == LPFC_SLI_REV4) {
28baac74
JS
2446 shost->dma_boundary =
2447 phba->sli4_hba.pc_sli4_params.sge_supp_len;
da0436e9
JS
2448 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
2449 }
81301a9b 2450
47a8617c 2451 /*
2e0fef85
JS
2452 * Set initial can_queue value since 0 is no longer supported and
2453 * scsi_add_host will fail. This will be adjusted later based on the
2454 * max xri value determined in hba setup.
47a8617c 2455 */
2e0fef85 2456 shost->can_queue = phba->cfg_hba_queue_depth - 10;
3de2a653 2457 if (dev != &phba->pcidev->dev) {
92d7f7b0
JS
2458 shost->transportt = lpfc_vport_transport_template;
2459 vport->port_type = LPFC_NPIV_PORT;
2460 } else {
2461 shost->transportt = lpfc_transport_template;
2462 vport->port_type = LPFC_PHYSICAL_PORT;
2463 }
47a8617c 2464
2e0fef85
JS
2465 /* Initialize all internally managed lists. */
2466 INIT_LIST_HEAD(&vport->fc_nodes);
da0436e9 2467 INIT_LIST_HEAD(&vport->rcv_buffer_list);
2e0fef85 2468 spin_lock_init(&vport->work_port_lock);
47a8617c 2469
2e0fef85
JS
2470 init_timer(&vport->fc_disctmo);
2471 vport->fc_disctmo.function = lpfc_disc_timeout;
92d7f7b0 2472 vport->fc_disctmo.data = (unsigned long)vport;
47a8617c 2473
2e0fef85
JS
2474 init_timer(&vport->fc_fdmitmo);
2475 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
92d7f7b0 2476 vport->fc_fdmitmo.data = (unsigned long)vport;
47a8617c 2477
2e0fef85
JS
2478 init_timer(&vport->els_tmofunc);
2479 vport->els_tmofunc.function = lpfc_els_timeout;
92d7f7b0 2480 vport->els_tmofunc.data = (unsigned long)vport;
47a8617c 2481
d139b9bd 2482 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
2e0fef85
JS
2483 if (error)
2484 goto out_put_shost;
47a8617c 2485
549e55cd 2486 spin_lock_irq(&phba->hbalock);
2e0fef85 2487 list_add_tail(&vport->listentry, &phba->port_list);
549e55cd 2488 spin_unlock_irq(&phba->hbalock);
2e0fef85 2489 return vport;
47a8617c 2490
2e0fef85
JS
2491out_put_shost:
2492 scsi_host_put(shost);
2493out:
2494 return NULL;
47a8617c
JS
2495}
2496
e59058c4 2497/**
3621a710 2498 * destroy_port - destroy an FC port
e59058c4
JS
2499 * @vport: pointer to an lpfc virtual N_Port data structure.
2500 *
2501 * This routine destroys a FC port from the upper layer protocol. All the
2502 * resources associated with the port are released.
2503 **/
2e0fef85
JS
2504void
2505destroy_port(struct lpfc_vport *vport)
47a8617c 2506{
92d7f7b0
JS
2507 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2508 struct lpfc_hba *phba = vport->phba;
47a8617c 2509
858c9f6c 2510 lpfc_debugfs_terminate(vport);
92d7f7b0
JS
2511 fc_remove_host(shost);
2512 scsi_remove_host(shost);
47a8617c 2513
92d7f7b0
JS
2514 spin_lock_irq(&phba->hbalock);
2515 list_del_init(&vport->listentry);
2516 spin_unlock_irq(&phba->hbalock);
47a8617c 2517
92d7f7b0 2518 lpfc_cleanup(vport);
47a8617c 2519 return;
47a8617c
JS
2520}
2521
e59058c4 2522/**
3621a710 2523 * lpfc_get_instance - Get a unique integer ID
e59058c4
JS
2524 *
2525 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
2526 * uses the kernel idr facility to perform the task.
2527 *
2528 * Return codes:
2529 * instance - a unique integer ID allocated as the new instance.
2530 * -1 - lpfc get instance failed.
2531 **/
92d7f7b0
JS
2532int
2533lpfc_get_instance(void)
2534{
2535 int instance = 0;
47a8617c 2536
92d7f7b0
JS
2537 /* Assign an unused number */
2538 if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
2539 return -1;
2540 if (idr_get_new(&lpfc_hba_index, NULL, &instance))
2541 return -1;
2542 return instance;
47a8617c
JS
2543}
2544
e59058c4 2545/**
3621a710 2546 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
e59058c4
JS
2547 * @shost: pointer to SCSI host data structure.
2548 * @time: elapsed time of the scan in jiffies.
2549 *
2550 * This routine is called by the SCSI layer with a SCSI host to determine
2551 * whether the scan host is finished.
2552 *
2553 * Note: there is no scan_start function as adapter initialization will have
2554 * asynchronously kicked off the link initialization.
2555 *
2556 * Return codes
2557 * 0 - SCSI host scan is not over yet.
2558 * 1 - SCSI host scan is over.
2559 **/
47a8617c
JS
2560int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2561{
2e0fef85
JS
2562 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2563 struct lpfc_hba *phba = vport->phba;
858c9f6c 2564 int stat = 0;
47a8617c 2565
858c9f6c
JS
2566 spin_lock_irq(shost->host_lock);
2567
51ef4c26 2568 if (vport->load_flag & FC_UNLOADING) {
858c9f6c
JS
2569 stat = 1;
2570 goto finished;
2571 }
2e0fef85
JS
2572 if (time >= 30 * HZ) {
2573 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011
JS
2574 "0461 Scanning longer than 30 "
2575 "seconds. Continuing initialization\n");
858c9f6c 2576 stat = 1;
47a8617c 2577 goto finished;
2e0fef85
JS
2578 }
2579 if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
2580 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011
JS
2581 "0465 Link down longer than 15 "
2582 "seconds. Continuing initialization\n");
858c9f6c 2583 stat = 1;
47a8617c 2584 goto finished;
2e0fef85 2585 }
47a8617c 2586
2e0fef85 2587 if (vport->port_state != LPFC_VPORT_READY)
858c9f6c 2588 goto finished;
2e0fef85 2589 if (vport->num_disc_nodes || vport->fc_prli_sent)
858c9f6c 2590 goto finished;
2e0fef85 2591 if (vport->fc_map_cnt == 0 && time < 2 * HZ)
858c9f6c 2592 goto finished;
2e0fef85 2593 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
858c9f6c
JS
2594 goto finished;
2595
2596 stat = 1;
47a8617c
JS
2597
2598finished:
858c9f6c
JS
2599 spin_unlock_irq(shost->host_lock);
2600 return stat;
92d7f7b0 2601}
47a8617c 2602
e59058c4 2603/**
3621a710 2604 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
e59058c4
JS
2605 * @shost: pointer to SCSI host data structure.
2606 *
2607 * This routine initializes a given SCSI host attributes on a FC port. The
2608 * SCSI host can be either on top of a physical port or a virtual port.
2609 **/
92d7f7b0
JS
2610void lpfc_host_attrib_init(struct Scsi_Host *shost)
2611{
2612 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2613 struct lpfc_hba *phba = vport->phba;
47a8617c 2614 /*
2e0fef85 2615 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
47a8617c
JS
2616 */
2617
2e0fef85
JS
2618 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
2619 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
47a8617c
JS
2620 fc_host_supported_classes(shost) = FC_COS_CLASS3;
2621
2622 memset(fc_host_supported_fc4s(shost), 0,
2e0fef85 2623 sizeof(fc_host_supported_fc4s(shost)));
47a8617c
JS
2624 fc_host_supported_fc4s(shost)[2] = 1;
2625 fc_host_supported_fc4s(shost)[7] = 1;
2626
92d7f7b0
JS
2627 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
2628 sizeof fc_host_symbolic_name(shost));
47a8617c
JS
2629
2630 fc_host_supported_speeds(shost) = 0;
2631 if (phba->lmt & LMT_10Gb)
2632 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
a8adb832
JS
2633 if (phba->lmt & LMT_8Gb)
2634 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
47a8617c
JS
2635 if (phba->lmt & LMT_4Gb)
2636 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
2637 if (phba->lmt & LMT_2Gb)
2638 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
2639 if (phba->lmt & LMT_1Gb)
2640 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
2641
2642 fc_host_maxframe_size(shost) =
2e0fef85
JS
2643 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
2644 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
47a8617c
JS
2645
2646 /* This value is also unchanging */
2647 memset(fc_host_active_fc4s(shost), 0,
2e0fef85 2648 sizeof(fc_host_active_fc4s(shost)));
47a8617c
JS
2649 fc_host_active_fc4s(shost)[2] = 1;
2650 fc_host_active_fc4s(shost)[7] = 1;
2651
92d7f7b0 2652 fc_host_max_npiv_vports(shost) = phba->max_vpi;
47a8617c 2653 spin_lock_irq(shost->host_lock);
51ef4c26 2654 vport->load_flag &= ~FC_LOADING;
47a8617c 2655 spin_unlock_irq(shost->host_lock);
47a8617c 2656}
dea3101e 2657
e59058c4 2658/**
da0436e9 2659 * lpfc_stop_port_s3 - Stop SLI3 device port
e59058c4
JS
2660 * @phba: pointer to lpfc hba data structure.
2661 *
da0436e9
JS
2662 * This routine is invoked to stop an SLI3 device port, it stops the device
2663 * from generating interrupts and stops the device driver's timers for the
2664 * device.
e59058c4 2665 **/
da0436e9
JS
2666static void
2667lpfc_stop_port_s3(struct lpfc_hba *phba)
db2378e0 2668{
da0436e9
JS
2669 /* Clear all interrupt enable conditions */
2670 writel(0, phba->HCregaddr);
2671 readl(phba->HCregaddr); /* flush */
2672 /* Clear all pending interrupts */
2673 writel(0xffffffff, phba->HAregaddr);
2674 readl(phba->HAregaddr); /* flush */
db2378e0 2675
da0436e9
JS
2676 /* Reset some HBA SLI setup states */
2677 lpfc_stop_hba_timers(phba);
2678 phba->pport->work_port_events = 0;
2679}
db2378e0 2680
da0436e9
JS
2681/**
2682 * lpfc_stop_port_s4 - Stop SLI4 device port
2683 * @phba: pointer to lpfc hba data structure.
2684 *
2685 * This routine is invoked to stop an SLI4 device port, it stops the device
2686 * from generating interrupts and stops the device driver's timers for the
2687 * device.
2688 **/
2689static void
2690lpfc_stop_port_s4(struct lpfc_hba *phba)
2691{
2692 /* Reset some HBA SLI4 setup states */
2693 lpfc_stop_hba_timers(phba);
2694 phba->pport->work_port_events = 0;
2695 phba->sli4_hba.intr_enable = 0;
2696 /* Hard clear it for now, shall have more graceful way to wait later */
2697 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2698}
9399627f 2699
da0436e9
JS
2700/**
2701 * lpfc_stop_port - Wrapper function for stopping hba port
2702 * @phba: Pointer to HBA context object.
2703 *
2704 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
2705 * the API jump table function pointer from the lpfc_hba struct.
2706 **/
2707void
2708lpfc_stop_port(struct lpfc_hba *phba)
2709{
2710 phba->lpfc_stop_port(phba);
2711}
db2378e0 2712
da0436e9
JS
2713/**
2714 * lpfc_sli4_remove_dflt_fcf - Remove the driver default fcf record from the port.
2715 * @phba: pointer to lpfc hba data structure.
2716 *
2717 * This routine is invoked to remove the driver default fcf record from
2718 * the port. This routine currently acts on FCF Index 0.
2719 *
2720 **/
2721void
2722lpfc_sli_remove_dflt_fcf(struct lpfc_hba *phba)
2723{
2724 int rc = 0;
2725 LPFC_MBOXQ_t *mboxq;
2726 struct lpfc_mbx_del_fcf_tbl_entry *del_fcf_record;
2727 uint32_t mbox_tmo, req_len;
2728 uint32_t shdr_status, shdr_add_status;
9399627f 2729
da0436e9
JS
2730 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2731 if (!mboxq) {
2732 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2733 "2020 Failed to allocate mbox for ADD_FCF cmd\n");
2734 return;
db2378e0 2735 }
9399627f 2736
da0436e9
JS
2737 req_len = sizeof(struct lpfc_mbx_del_fcf_tbl_entry) -
2738 sizeof(struct lpfc_sli4_cfg_mhdr);
2739 rc = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
2740 LPFC_MBOX_OPCODE_FCOE_DELETE_FCF,
2741 req_len, LPFC_SLI4_MBX_EMBED);
9399627f 2742 /*
da0436e9
JS
2743 * In phase 1, there is a single FCF index, 0. In phase2, the driver
2744 * supports multiple FCF indices.
9399627f 2745 */
da0436e9
JS
2746 del_fcf_record = &mboxq->u.mqe.un.del_fcf_entry;
2747 bf_set(lpfc_mbx_del_fcf_tbl_count, del_fcf_record, 1);
2748 bf_set(lpfc_mbx_del_fcf_tbl_index, del_fcf_record,
ecfd03c6 2749 phba->fcf.current_rec.fcf_indx);
9399627f 2750
da0436e9
JS
2751 if (!phba->sli4_hba.intr_enable)
2752 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
2753 else {
2754 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
2755 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
9399627f 2756 }
da0436e9
JS
2757 /* The IOCTL status is embedded in the mailbox subheader. */
2758 shdr_status = bf_get(lpfc_mbox_hdr_status,
2759 &del_fcf_record->header.cfg_shdr.response);
2760 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
2761 &del_fcf_record->header.cfg_shdr.response);
2762 if (shdr_status || shdr_add_status || rc != MBX_SUCCESS) {
2763 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2764 "2516 DEL FCF of default FCF Index failed "
2765 "mbx status x%x, status x%x add_status x%x\n",
2766 rc, shdr_status, shdr_add_status);
9399627f 2767 }
da0436e9
JS
2768 if (rc != MBX_TIMEOUT)
2769 mempool_free(mboxq, phba->mbox_mem_pool);
db2378e0
JS
2770}
2771
ecfd03c6
JS
2772/**
2773 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
2774 * @phba: Pointer to hba for which this call is being executed.
2775 *
2776 * This routine starts the timer waiting for the FCF rediscovery to complete.
2777 **/
2778void
2779lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
2780{
2781 unsigned long fcf_redisc_wait_tmo =
2782 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
2783 /* Start fcf rediscovery wait period timer */
2784 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
2785 spin_lock_irq(&phba->hbalock);
2786 /* Allow action to new fcf asynchronous event */
2787 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
2788 /* Mark the FCF rediscovery pending state */
2789 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
2790 spin_unlock_irq(&phba->hbalock);
2791}
2792
2793/**
2794 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
2795 * @ptr: Map to lpfc_hba data structure pointer.
2796 *
2797 * This routine is invoked when waiting for FCF table rediscover has been
2798 * timed out. If new FCF record(s) has (have) been discovered during the
2799 * wait period, a new FCF event shall be added to the FCOE async event
2800 * list, and then worker thread shall be waked up for processing from the
2801 * worker thread context.
2802 **/
2803void
2804lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
2805{
2806 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
2807
2808 /* Don't send FCF rediscovery event if timer cancelled */
2809 spin_lock_irq(&phba->hbalock);
2810 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2811 spin_unlock_irq(&phba->hbalock);
2812 return;
2813 }
2814 /* Clear FCF rediscovery timer pending flag */
2815 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2816 /* FCF rediscovery event to worker thread */
2817 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
2818 spin_unlock_irq(&phba->hbalock);
2819 /* wake up worker thread */
2820 lpfc_worker_wake_up(phba);
2821}
2822
6669f9bb
JS
2823/**
2824 * lpfc_sli4_fw_cfg_check - Read the firmware config and verify FCoE support
2825 * @phba: pointer to lpfc hba data structure.
2826 *
2827 * This function uses the QUERY_FW_CFG mailbox command to determine if the
2828 * firmware loaded supports FCoE. A return of zero indicates that the mailbox
2829 * was successful and the firmware supports FCoE. Any other return indicates
2830 * a error. It is assumed that this function will be called before interrupts
2831 * are enabled.
2832 **/
2833static int
2834lpfc_sli4_fw_cfg_check(struct lpfc_hba *phba)
2835{
2836 int rc = 0;
2837 LPFC_MBOXQ_t *mboxq;
2838 struct lpfc_mbx_query_fw_cfg *query_fw_cfg;
2839 uint32_t length;
2840 uint32_t shdr_status, shdr_add_status;
2841
2842 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2843 if (!mboxq) {
2844 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2845 "2621 Failed to allocate mbox for "
2846 "query firmware config cmd\n");
2847 return -ENOMEM;
2848 }
2849 query_fw_cfg = &mboxq->u.mqe.un.query_fw_cfg;
2850 length = (sizeof(struct lpfc_mbx_query_fw_cfg) -
2851 sizeof(struct lpfc_sli4_cfg_mhdr));
2852 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
2853 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
2854 length, LPFC_SLI4_MBX_EMBED);
2855 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
2856 /* The IOCTL status is embedded in the mailbox subheader. */
2857 shdr_status = bf_get(lpfc_mbox_hdr_status,
2858 &query_fw_cfg->header.cfg_shdr.response);
2859 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
2860 &query_fw_cfg->header.cfg_shdr.response);
2861 if (shdr_status || shdr_add_status || rc != MBX_SUCCESS) {
2862 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2863 "2622 Query Firmware Config failed "
2864 "mbx status x%x, status x%x add_status x%x\n",
2865 rc, shdr_status, shdr_add_status);
2866 return -EINVAL;
2867 }
2868 if (!bf_get(lpfc_function_mode_fcoe_i, query_fw_cfg)) {
2869 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2870 "2623 FCoE Function not supported by firmware. "
2871 "Function mode = %08x\n",
2872 query_fw_cfg->function_mode);
2873 return -EINVAL;
2874 }
2875 if (rc != MBX_TIMEOUT)
2876 mempool_free(mboxq, phba->mbox_mem_pool);
2877 return 0;
2878}
2879
e59058c4 2880/**
da0436e9 2881 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
e59058c4 2882 * @phba: pointer to lpfc hba data structure.
da0436e9 2883 * @acqe_link: pointer to the async link completion queue entry.
e59058c4 2884 *
da0436e9
JS
2885 * This routine is to parse the SLI4 link-attention link fault code and
2886 * translate it into the base driver's read link attention mailbox command
2887 * status.
2888 *
2889 * Return: Link-attention status in terms of base driver's coding.
e59058c4 2890 **/
da0436e9
JS
2891static uint16_t
2892lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
2893 struct lpfc_acqe_link *acqe_link)
db2378e0 2894{
da0436e9 2895 uint16_t latt_fault;
9399627f 2896
da0436e9
JS
2897 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
2898 case LPFC_ASYNC_LINK_FAULT_NONE:
2899 case LPFC_ASYNC_LINK_FAULT_LOCAL:
2900 case LPFC_ASYNC_LINK_FAULT_REMOTE:
2901 latt_fault = 0;
2902 break;
2903 default:
2904 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2905 "0398 Invalid link fault code: x%x\n",
2906 bf_get(lpfc_acqe_link_fault, acqe_link));
2907 latt_fault = MBXERR_ERROR;
2908 break;
2909 }
2910 return latt_fault;
db2378e0
JS
2911}
2912
5b75da2f 2913/**
da0436e9 2914 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
5b75da2f 2915 * @phba: pointer to lpfc hba data structure.
da0436e9 2916 * @acqe_link: pointer to the async link completion queue entry.
5b75da2f 2917 *
da0436e9
JS
2918 * This routine is to parse the SLI4 link attention type and translate it
2919 * into the base driver's link attention type coding.
5b75da2f 2920 *
da0436e9
JS
2921 * Return: Link attention type in terms of base driver's coding.
2922 **/
2923static uint8_t
2924lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
2925 struct lpfc_acqe_link *acqe_link)
5b75da2f 2926{
da0436e9 2927 uint8_t att_type;
5b75da2f 2928
da0436e9
JS
2929 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
2930 case LPFC_ASYNC_LINK_STATUS_DOWN:
2931 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
2932 att_type = AT_LINK_DOWN;
2933 break;
2934 case LPFC_ASYNC_LINK_STATUS_UP:
2935 /* Ignore physical link up events - wait for logical link up */
2936 att_type = AT_RESERVED;
2937 break;
2938 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
2939 att_type = AT_LINK_UP;
2940 break;
2941 default:
2942 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2943 "0399 Invalid link attention type: x%x\n",
2944 bf_get(lpfc_acqe_link_status, acqe_link));
2945 att_type = AT_RESERVED;
2946 break;
5b75da2f 2947 }
da0436e9 2948 return att_type;
5b75da2f
JS
2949}
2950
2951/**
da0436e9 2952 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
5b75da2f 2953 * @phba: pointer to lpfc hba data structure.
da0436e9 2954 * @acqe_link: pointer to the async link completion queue entry.
5b75da2f 2955 *
da0436e9
JS
2956 * This routine is to parse the SLI4 link-attention link speed and translate
2957 * it into the base driver's link-attention link speed coding.
2958 *
2959 * Return: Link-attention link speed in terms of base driver's coding.
2960 **/
2961static uint8_t
2962lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
2963 struct lpfc_acqe_link *acqe_link)
5b75da2f 2964{
da0436e9
JS
2965 uint8_t link_speed;
2966
2967 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
2968 case LPFC_ASYNC_LINK_SPEED_ZERO:
2969 link_speed = LA_UNKNW_LINK;
2970 break;
2971 case LPFC_ASYNC_LINK_SPEED_10MBPS:
2972 link_speed = LA_UNKNW_LINK;
2973 break;
2974 case LPFC_ASYNC_LINK_SPEED_100MBPS:
2975 link_speed = LA_UNKNW_LINK;
2976 break;
2977 case LPFC_ASYNC_LINK_SPEED_1GBPS:
2978 link_speed = LA_1GHZ_LINK;
2979 break;
2980 case LPFC_ASYNC_LINK_SPEED_10GBPS:
2981 link_speed = LA_10GHZ_LINK;
2982 break;
2983 default:
2984 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2985 "0483 Invalid link-attention link speed: x%x\n",
2986 bf_get(lpfc_acqe_link_speed, acqe_link));
2987 link_speed = LA_UNKNW_LINK;
2988 break;
2989 }
2990 return link_speed;
2991}
2992
2993/**
2994 * lpfc_sli4_async_link_evt - Process the asynchronous link event
2995 * @phba: pointer to lpfc hba data structure.
2996 * @acqe_link: pointer to the async link completion queue entry.
2997 *
2998 * This routine is to handle the SLI4 asynchronous link event.
2999 **/
3000static void
3001lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3002 struct lpfc_acqe_link *acqe_link)
3003{
3004 struct lpfc_dmabuf *mp;
3005 LPFC_MBOXQ_t *pmb;
3006 MAILBOX_t *mb;
3007 READ_LA_VAR *la;
3008 uint8_t att_type;
3009
3010 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
3011 if (att_type != AT_LINK_DOWN && att_type != AT_LINK_UP)
3012 return;
32b9793f 3013 phba->fcoe_eventtag = acqe_link->event_tag;
da0436e9
JS
3014 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3015 if (!pmb) {
3016 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3017 "0395 The mboxq allocation failed\n");
3018 return;
3019 }
3020 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3021 if (!mp) {
3022 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3023 "0396 The lpfc_dmabuf allocation failed\n");
3024 goto out_free_pmb;
3025 }
3026 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3027 if (!mp->virt) {
3028 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3029 "0397 The mbuf allocation failed\n");
3030 goto out_free_dmabuf;
3031 }
3032
3033 /* Cleanup any outstanding ELS commands */
3034 lpfc_els_flush_all_cmd(phba);
3035
3036 /* Block ELS IOCBs until we have done process link event */
3037 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3038
3039 /* Update link event statistics */
3040 phba->sli.slistat.link_event++;
3041
3042 /* Create pseudo lpfc_handle_latt mailbox command from link ACQE */
3043 lpfc_read_la(phba, pmb, mp);
3044 pmb->vport = phba->pport;
3045
3046 /* Parse and translate status field */
3047 mb = &pmb->u.mb;
3048 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
3049
3050 /* Parse and translate link attention fields */
3051 la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
3052 la->eventTag = acqe_link->event_tag;
3053 la->attType = att_type;
3054 la->UlnkSpeed = lpfc_sli4_parse_latt_link_speed(phba, acqe_link);
3055
3056 /* Fake the the following irrelvant fields */
3057 la->topology = TOPOLOGY_PT_PT;
3058 la->granted_AL_PA = 0;
3059 la->il = 0;
3060 la->pb = 0;
3061 la->fa = 0;
3062 la->mm = 0;
3063
3064 /* Keep the link status for extra SLI4 state machine reference */
3065 phba->sli4_hba.link_state.speed =
3066 bf_get(lpfc_acqe_link_speed, acqe_link);
3067 phba->sli4_hba.link_state.duplex =
3068 bf_get(lpfc_acqe_link_duplex, acqe_link);
3069 phba->sli4_hba.link_state.status =
3070 bf_get(lpfc_acqe_link_status, acqe_link);
3071 phba->sli4_hba.link_state.physical =
3072 bf_get(lpfc_acqe_link_physical, acqe_link);
3073 phba->sli4_hba.link_state.fault =
3074 bf_get(lpfc_acqe_link_fault, acqe_link);
65467b6b
JS
3075 phba->sli4_hba.link_state.logical_speed =
3076 bf_get(lpfc_acqe_qos_link_speed, acqe_link);
da0436e9
JS
3077
3078 /* Invoke the lpfc_handle_latt mailbox command callback function */
3079 lpfc_mbx_cmpl_read_la(phba, pmb);
3080
5b75da2f 3081 return;
da0436e9
JS
3082
3083out_free_dmabuf:
3084 kfree(mp);
3085out_free_pmb:
3086 mempool_free(pmb, phba->mbox_mem_pool);
3087}
3088
3089/**
3090 * lpfc_sli4_async_fcoe_evt - Process the asynchronous fcoe event
3091 * @phba: pointer to lpfc hba data structure.
3092 * @acqe_link: pointer to the async fcoe completion queue entry.
3093 *
3094 * This routine is to handle the SLI4 asynchronous fcoe event.
3095 **/
3096static void
3097lpfc_sli4_async_fcoe_evt(struct lpfc_hba *phba,
3098 struct lpfc_acqe_fcoe *acqe_fcoe)
3099{
3100 uint8_t event_type = bf_get(lpfc_acqe_fcoe_event_type, acqe_fcoe);
3101 int rc;
6669f9bb
JS
3102 struct lpfc_vport *vport;
3103 struct lpfc_nodelist *ndlp;
3104 struct Scsi_Host *shost;
97957244 3105 uint32_t link_state;
695a814e
JS
3106 int active_vlink_present;
3107 struct lpfc_vport **vports;
3108 int i;
da0436e9 3109
4d9ab994 3110 phba->fc_eventTag = acqe_fcoe->event_tag;
32b9793f 3111 phba->fcoe_eventtag = acqe_fcoe->event_tag;
da0436e9
JS
3112 switch (event_type) {
3113 case LPFC_FCOE_EVENT_TYPE_NEW_FCF:
ecfd03c6 3114 case LPFC_FCOE_EVENT_TYPE_FCF_PARAM_MOD:
da0436e9 3115 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
e4e74273 3116 "2546 New FCF found index 0x%x tag 0x%x\n",
6669f9bb 3117 acqe_fcoe->index,
da0436e9 3118 acqe_fcoe->event_tag);
da0436e9 3119 spin_lock_irq(&phba->hbalock);
ecfd03c6
JS
3120 if ((phba->fcf.fcf_flag & FCF_SCAN_DONE) ||
3121 (phba->hba_flag & FCF_DISC_INPROGRESS)) {
3122 /*
3123 * If the current FCF is in discovered state or
3124 * FCF discovery is in progress, do nothing.
3125 */
3126 spin_unlock_irq(&phba->hbalock);
3127 break;
3128 }
3129 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
3130 /*
3131 * If fast FCF failover rescan event is pending,
3132 * do nothing.
3133 */
da0436e9
JS
3134 spin_unlock_irq(&phba->hbalock);
3135 break;
3136 }
3137 spin_unlock_irq(&phba->hbalock);
3138
3139 /* Read the FCF table and re-discover SAN. */
6a9c52cf 3140 rc = lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
da0436e9
JS
3141 if (rc)
3142 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
6a9c52cf
JS
3143 "2547 Read FCF record failed 0x%x\n",
3144 rc);
da0436e9
JS
3145 break;
3146
3147 case LPFC_FCOE_EVENT_TYPE_FCF_TABLE_FULL:
3148 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e4e74273 3149 "2548 FCF Table full count 0x%x tag 0x%x\n",
da0436e9
JS
3150 bf_get(lpfc_acqe_fcoe_fcf_count, acqe_fcoe),
3151 acqe_fcoe->event_tag);
3152 break;
3153
3154 case LPFC_FCOE_EVENT_TYPE_FCF_DEAD:
3155 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
6a9c52cf 3156 "2549 FCF disconnected from network index 0x%x"
6669f9bb 3157 " tag 0x%x\n", acqe_fcoe->index,
da0436e9
JS
3158 acqe_fcoe->event_tag);
3159 /* If the event is not for currently used fcf do nothing */
ecfd03c6 3160 if (phba->fcf.current_rec.fcf_indx != acqe_fcoe->index)
da0436e9
JS
3161 break;
3162 /*
3163 * Currently, driver support only one FCF - so treat this as
97957244
JS
3164 * a link down, but save the link state because we don't want
3165 * it to be changed to Link Down unless it is already down.
da0436e9 3166 */
97957244 3167 link_state = phba->link_state;
da0436e9 3168 lpfc_linkdown(phba);
97957244 3169 phba->link_state = link_state;
da0436e9
JS
3170 /* Unregister FCF if no devices connected to it */
3171 lpfc_unregister_unused_fcf(phba);
3172 break;
6669f9bb
JS
3173 case LPFC_FCOE_EVENT_TYPE_CVL:
3174 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
3175 "2718 Clear Virtual Link Received for VPI 0x%x"
3176 " tag 0x%x\n", acqe_fcoe->index, acqe_fcoe->event_tag);
3177 vport = lpfc_find_vport_by_vpid(phba,
c868595d 3178 acqe_fcoe->index - phba->vpi_base);
6669f9bb
JS
3179 if (!vport)
3180 break;
3181 ndlp = lpfc_findnode_did(vport, Fabric_DID);
3182 if (!ndlp)
3183 break;
3184 shost = lpfc_shost_from_vport(vport);
695a814e
JS
3185 if (phba->pport->port_state <= LPFC_FLOGI)
3186 break;
3187 /* If virtual link is not yet instantiated ignore CVL */
3188 if (vport->port_state <= LPFC_FDISC)
3189 break;
3190
6669f9bb 3191 lpfc_linkdown_port(vport);
695a814e
JS
3192 lpfc_cleanup_pending_mbox(vport);
3193 spin_lock_irq(shost->host_lock);
3194 vport->fc_flag |= FC_VPORT_CVL_RCVD;
3195 spin_unlock_irq(shost->host_lock);
3196 active_vlink_present = 0;
3197
3198 vports = lpfc_create_vport_work_array(phba);
3199 if (vports) {
3200 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
3201 i++) {
3202 if ((!(vports[i]->fc_flag &
3203 FC_VPORT_CVL_RCVD)) &&
3204 (vports[i]->port_state > LPFC_FDISC)) {
3205 active_vlink_present = 1;
3206 break;
3207 }
3208 }
3209 lpfc_destroy_vport_work_array(phba, vports);
3210 }
3211
3212 if (active_vlink_present) {
3213 /*
3214 * If there are other active VLinks present,
3215 * re-instantiate the Vlink using FDISC.
3216 */
6669f9bb
JS
3217 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
3218 spin_lock_irq(shost->host_lock);
3219 ndlp->nlp_flag |= NLP_DELAY_TMO;
3220 spin_unlock_irq(shost->host_lock);
695a814e
JS
3221 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
3222 vport->port_state = LPFC_FDISC;
3223 } else {
ecfd03c6
JS
3224 /*
3225 * Otherwise, we request port to rediscover
3226 * the entire FCF table for a fast recovery
3227 * from possible case that the current FCF
3228 * is no longer valid.
3229 */
3230 rc = lpfc_sli4_redisc_fcf_table(phba);
3231 if (rc)
3232 /*
3233 * Last resort will be re-try on the
3234 * the current registered FCF entry.
3235 */
3236 lpfc_retry_pport_discovery(phba);
6669f9bb
JS
3237 }
3238 break;
da0436e9
JS
3239 default:
3240 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3241 "0288 Unknown FCoE event type 0x%x event tag "
3242 "0x%x\n", event_type, acqe_fcoe->event_tag);
3243 break;
3244 }
3245}
3246
3247/**
3248 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
3249 * @phba: pointer to lpfc hba data structure.
3250 * @acqe_link: pointer to the async dcbx completion queue entry.
3251 *
3252 * This routine is to handle the SLI4 asynchronous dcbx event.
3253 **/
3254static void
3255lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
3256 struct lpfc_acqe_dcbx *acqe_dcbx)
3257{
4d9ab994 3258 phba->fc_eventTag = acqe_dcbx->event_tag;
da0436e9
JS
3259 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3260 "0290 The SLI4 DCBX asynchronous event is not "
3261 "handled yet\n");
3262}
3263
3264/**
3265 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
3266 * @phba: pointer to lpfc hba data structure.
3267 *
3268 * This routine is invoked by the worker thread to process all the pending
3269 * SLI4 asynchronous events.
3270 **/
3271void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
3272{
3273 struct lpfc_cq_event *cq_event;
3274
3275 /* First, declare the async event has been handled */
3276 spin_lock_irq(&phba->hbalock);
3277 phba->hba_flag &= ~ASYNC_EVENT;
3278 spin_unlock_irq(&phba->hbalock);
3279 /* Now, handle all the async events */
3280 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
3281 /* Get the first event from the head of the event queue */
3282 spin_lock_irq(&phba->hbalock);
3283 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
3284 cq_event, struct lpfc_cq_event, list);
3285 spin_unlock_irq(&phba->hbalock);
3286 /* Process the asynchronous event */
3287 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
3288 case LPFC_TRAILER_CODE_LINK:
3289 lpfc_sli4_async_link_evt(phba,
3290 &cq_event->cqe.acqe_link);
3291 break;
3292 case LPFC_TRAILER_CODE_FCOE:
3293 lpfc_sli4_async_fcoe_evt(phba,
3294 &cq_event->cqe.acqe_fcoe);
3295 break;
3296 case LPFC_TRAILER_CODE_DCBX:
3297 lpfc_sli4_async_dcbx_evt(phba,
3298 &cq_event->cqe.acqe_dcbx);
3299 break;
3300 default:
3301 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3302 "1804 Invalid asynchrous event code: "
3303 "x%x\n", bf_get(lpfc_trailer_code,
3304 &cq_event->cqe.mcqe_cmpl));
3305 break;
3306 }
3307 /* Free the completion event processed to the free pool */
3308 lpfc_sli4_cq_event_release(phba, cq_event);
3309 }
3310}
3311
ecfd03c6
JS
3312/**
3313 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
3314 * @phba: pointer to lpfc hba data structure.
3315 *
3316 * This routine is invoked by the worker thread to process FCF table
3317 * rediscovery pending completion event.
3318 **/
3319void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
3320{
3321 int rc;
3322
3323 spin_lock_irq(&phba->hbalock);
3324 /* Clear FCF rediscovery timeout event */
3325 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
3326 /* Clear driver fast failover FCF record flag */
3327 phba->fcf.failover_rec.flag = 0;
3328 /* Set state for FCF fast failover */
3329 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
3330 spin_unlock_irq(&phba->hbalock);
3331
3332 /* Scan FCF table from the first entry to re-discover SAN */
3333 rc = lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
3334 if (rc)
3335 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
3336 "2747 Post FCF rediscovery read FCF record "
3337 "failed 0x%x\n", rc);
3338}
3339
da0436e9
JS
3340/**
3341 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
3342 * @phba: pointer to lpfc hba data structure.
3343 * @dev_grp: The HBA PCI-Device group number.
3344 *
3345 * This routine is invoked to set up the per HBA PCI-Device group function
3346 * API jump table entries.
3347 *
3348 * Return: 0 if success, otherwise -ENODEV
3349 **/
3350int
3351lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
3352{
3353 int rc;
3354
3355 /* Set up lpfc PCI-device group */
3356 phba->pci_dev_grp = dev_grp;
3357
3358 /* The LPFC_PCI_DEV_OC uses SLI4 */
3359 if (dev_grp == LPFC_PCI_DEV_OC)
3360 phba->sli_rev = LPFC_SLI_REV4;
3361
3362 /* Set up device INIT API function jump table */
3363 rc = lpfc_init_api_table_setup(phba, dev_grp);
3364 if (rc)
3365 return -ENODEV;
3366 /* Set up SCSI API function jump table */
3367 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
3368 if (rc)
3369 return -ENODEV;
3370 /* Set up SLI API function jump table */
3371 rc = lpfc_sli_api_table_setup(phba, dev_grp);
3372 if (rc)
3373 return -ENODEV;
3374 /* Set up MBOX API function jump table */
3375 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
3376 if (rc)
3377 return -ENODEV;
3378
3379 return 0;
5b75da2f
JS
3380}
3381
3382/**
3621a710 3383 * lpfc_log_intr_mode - Log the active interrupt mode
5b75da2f
JS
3384 * @phba: pointer to lpfc hba data structure.
3385 * @intr_mode: active interrupt mode adopted.
3386 *
3387 * This routine it invoked to log the currently used active interrupt mode
3388 * to the device.
3772a991
JS
3389 **/
3390static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
5b75da2f
JS
3391{
3392 switch (intr_mode) {
3393 case 0:
3394 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3395 "0470 Enable INTx interrupt mode.\n");
3396 break;
3397 case 1:
3398 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3399 "0481 Enabled MSI interrupt mode.\n");
3400 break;
3401 case 2:
3402 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3403 "0480 Enabled MSI-X interrupt mode.\n");
3404 break;
3405 default:
3406 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3407 "0482 Illegal interrupt mode.\n");
3408 break;
3409 }
3410 return;
3411}
3412
5b75da2f 3413/**
3772a991 3414 * lpfc_enable_pci_dev - Enable a generic PCI device.
5b75da2f
JS
3415 * @phba: pointer to lpfc hba data structure.
3416 *
3772a991
JS
3417 * This routine is invoked to enable the PCI device that is common to all
3418 * PCI devices.
5b75da2f
JS
3419 *
3420 * Return codes
af901ca1 3421 * 0 - successful
3772a991 3422 * other values - error
5b75da2f 3423 **/
3772a991
JS
3424static int
3425lpfc_enable_pci_dev(struct lpfc_hba *phba)
5b75da2f 3426{
3772a991
JS
3427 struct pci_dev *pdev;
3428 int bars;
5b75da2f 3429
3772a991
JS
3430 /* Obtain PCI device reference */
3431 if (!phba->pcidev)
3432 goto out_error;
3433 else
3434 pdev = phba->pcidev;
3435 /* Select PCI BARs */
3436 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3437 /* Enable PCI device */
3438 if (pci_enable_device_mem(pdev))
3439 goto out_error;
3440 /* Request PCI resource for the device */
3441 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
3442 goto out_disable_device;
3443 /* Set up device as PCI master and save state for EEH */
3444 pci_set_master(pdev);
3445 pci_try_set_mwi(pdev);
3446 pci_save_state(pdev);
5b75da2f 3447
3772a991 3448 return 0;
5b75da2f 3449
3772a991
JS
3450out_disable_device:
3451 pci_disable_device(pdev);
3452out_error:
3453 return -ENODEV;
5b75da2f
JS
3454}
3455
3456/**
3772a991 3457 * lpfc_disable_pci_dev - Disable a generic PCI device.
5b75da2f
JS
3458 * @phba: pointer to lpfc hba data structure.
3459 *
3772a991
JS
3460 * This routine is invoked to disable the PCI device that is common to all
3461 * PCI devices.
5b75da2f
JS
3462 **/
3463static void
3772a991 3464lpfc_disable_pci_dev(struct lpfc_hba *phba)
5b75da2f 3465{
3772a991
JS
3466 struct pci_dev *pdev;
3467 int bars;
5b75da2f 3468
3772a991
JS
3469 /* Obtain PCI device reference */
3470 if (!phba->pcidev)
3471 return;
3472 else
3473 pdev = phba->pcidev;
3474 /* Select PCI BARs */
3475 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3476 /* Release PCI resource and disable PCI device */
3477 pci_release_selected_regions(pdev, bars);
3478 pci_disable_device(pdev);
3479 /* Null out PCI private reference to driver */
3480 pci_set_drvdata(pdev, NULL);
5b75da2f
JS
3481
3482 return;
3483}
3484
e59058c4 3485/**
3772a991
JS
3486 * lpfc_reset_hba - Reset a hba
3487 * @phba: pointer to lpfc hba data structure.
e59058c4 3488 *
3772a991
JS
3489 * This routine is invoked to reset a hba device. It brings the HBA
3490 * offline, performs a board restart, and then brings the board back
3491 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
3492 * on outstanding mailbox commands.
e59058c4 3493 **/
3772a991
JS
3494void
3495lpfc_reset_hba(struct lpfc_hba *phba)
dea3101e 3496{
3772a991
JS
3497 /* If resets are disabled then set error state and return. */
3498 if (!phba->cfg_enable_hba_reset) {
3499 phba->link_state = LPFC_HBA_ERROR;
3500 return;
3501 }
3502 lpfc_offline_prep(phba);
3503 lpfc_offline(phba);
3504 lpfc_sli_brdrestart(phba);
3505 lpfc_online(phba);
3506 lpfc_unblock_mgmt_io(phba);
3507}
dea3101e 3508
3772a991
JS
3509/**
3510 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
3511 * @phba: pointer to lpfc hba data structure.
3512 *
3513 * This routine is invoked to set up the driver internal resources specific to
3514 * support the SLI-3 HBA device it attached to.
3515 *
3516 * Return codes
af901ca1 3517 * 0 - successful
3772a991
JS
3518 * other values - error
3519 **/
3520static int
3521lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
3522{
3523 struct lpfc_sli *psli;
dea3101e 3524
2e0fef85 3525 /*
3772a991 3526 * Initialize timers used by driver
2e0fef85 3527 */
dea3101e 3528
3772a991 3529 /* Heartbeat timer */
858c9f6c
JS
3530 init_timer(&phba->hb_tmofunc);
3531 phba->hb_tmofunc.function = lpfc_hb_timeout;
3532 phba->hb_tmofunc.data = (unsigned long)phba;
3533
dea3101e 3534 psli = &phba->sli;
3772a991 3535 /* MBOX heartbeat timer */
dea3101e 3536 init_timer(&psli->mbox_tmo);
3537 psli->mbox_tmo.function = lpfc_mbox_timeout;
2e0fef85 3538 psli->mbox_tmo.data = (unsigned long) phba;
3772a991 3539 /* FCP polling mode timer */
875fbdfe
JSEC
3540 init_timer(&phba->fcp_poll_timer);
3541 phba->fcp_poll_timer.function = lpfc_poll_timeout;
2e0fef85 3542 phba->fcp_poll_timer.data = (unsigned long) phba;
3772a991 3543 /* Fabric block timer */
92d7f7b0
JS
3544 init_timer(&phba->fabric_block_timer);
3545 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
3546 phba->fabric_block_timer.data = (unsigned long) phba;
3772a991 3547 /* EA polling mode timer */
9399627f
JS
3548 init_timer(&phba->eratt_poll);
3549 phba->eratt_poll.function = lpfc_poll_eratt;
3550 phba->eratt_poll.data = (unsigned long) phba;
dea3101e 3551
3772a991
JS
3552 /* Host attention work mask setup */
3553 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
3554 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
dea3101e 3555
3772a991
JS
3556 /* Get all the module params for configuring this host */
3557 lpfc_get_cfgparam(phba);
dea3101e 3558 /*
3772a991
JS
3559 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
3560 * used to create the sg_dma_buf_pool must be dynamically calculated.
3561 * 2 segments are added since the IOCB needs a command and response bde.
dea3101e 3562 */
3772a991
JS
3563 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
3564 sizeof(struct fcp_rsp) +
3565 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
3566
3567 if (phba->cfg_enable_bg) {
3568 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT;
3569 phba->cfg_sg_dma_buf_size +=
3570 phba->cfg_prot_sg_seg_cnt * sizeof(struct ulp_bde64);
901a920f 3571 }
dea3101e 3572
3772a991
JS
3573 /* Also reinitialize the host templates with new values. */
3574 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
3575 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
dea3101e 3576
3772a991
JS
3577 phba->max_vpi = LPFC_MAX_VPI;
3578 /* This will be set to correct value after config_port mbox */
3579 phba->max_vports = 0;
dea3101e 3580
3772a991
JS
3581 /*
3582 * Initialize the SLI Layer to run with lpfc HBAs.
3583 */
3584 lpfc_sli_setup(phba);
3585 lpfc_sli_queue_setup(phba);
ed957684 3586
3772a991
JS
3587 /* Allocate device driver memory */
3588 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
3589 return -ENOMEM;
51ef4c26 3590
3772a991
JS
3591 return 0;
3592}
ed957684 3593
3772a991
JS
3594/**
3595 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
3596 * @phba: pointer to lpfc hba data structure.
3597 *
3598 * This routine is invoked to unset the driver internal resources set up
3599 * specific for supporting the SLI-3 HBA device it attached to.
3600 **/
3601static void
3602lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
3603{
3604 /* Free device driver memory allocated */
3605 lpfc_mem_free_all(phba);
3163f725 3606
3772a991
JS
3607 return;
3608}
dea3101e 3609
3772a991 3610/**
da0436e9 3611 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
3772a991
JS
3612 * @phba: pointer to lpfc hba data structure.
3613 *
da0436e9
JS
3614 * This routine is invoked to set up the driver internal resources specific to
3615 * support the SLI-4 HBA device it attached to.
3772a991
JS
3616 *
3617 * Return codes
af901ca1 3618 * 0 - successful
da0436e9 3619 * other values - error
3772a991
JS
3620 **/
3621static int
da0436e9 3622lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
3772a991 3623{
da0436e9 3624 struct lpfc_sli *psli;
28baac74
JS
3625 LPFC_MBOXQ_t *mboxq;
3626 int rc, i, hbq_count, buf_size, dma_buf_size, max_buf_size;
3627 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
3628 struct lpfc_mqe *mqe;
da0436e9
JS
3629
3630 /* Before proceed, wait for POST done and device ready */
3631 rc = lpfc_sli4_post_status_check(phba);
3632 if (rc)
3633 return -ENODEV;
3634
3772a991 3635 /*
da0436e9 3636 * Initialize timers used by driver
3772a991 3637 */
3772a991 3638
da0436e9
JS
3639 /* Heartbeat timer */
3640 init_timer(&phba->hb_tmofunc);
3641 phba->hb_tmofunc.function = lpfc_hb_timeout;
3642 phba->hb_tmofunc.data = (unsigned long)phba;
3772a991 3643
da0436e9
JS
3644 psli = &phba->sli;
3645 /* MBOX heartbeat timer */
3646 init_timer(&psli->mbox_tmo);
3647 psli->mbox_tmo.function = lpfc_mbox_timeout;
3648 psli->mbox_tmo.data = (unsigned long) phba;
3649 /* Fabric block timer */
3650 init_timer(&phba->fabric_block_timer);
3651 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
3652 phba->fabric_block_timer.data = (unsigned long) phba;
3653 /* EA polling mode timer */
3654 init_timer(&phba->eratt_poll);
3655 phba->eratt_poll.function = lpfc_poll_eratt;
3656 phba->eratt_poll.data = (unsigned long) phba;
ecfd03c6
JS
3657 /* FCF rediscover timer */
3658 init_timer(&phba->fcf.redisc_wait);
3659 phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
3660 phba->fcf.redisc_wait.data = (unsigned long)phba;
3661
da0436e9
JS
3662 /*
3663 * We need to do a READ_CONFIG mailbox command here before
3664 * calling lpfc_get_cfgparam. For VFs this will report the
3665 * MAX_XRI, MAX_VPI, MAX_RPI, MAX_IOCB, and MAX_VFI settings.
3666 * All of the resources allocated
3667 * for this Port are tied to these values.
3668 */
3669 /* Get all the module params for configuring this host */
3670 lpfc_get_cfgparam(phba);
3671 phba->max_vpi = LPFC_MAX_VPI;
3672 /* This will be set to correct value after the read_config mbox */
3673 phba->max_vports = 0;
3772a991 3674
da0436e9
JS
3675 /* Program the default value of vlan_id and fc_map */
3676 phba->valid_vlan = 0;
3677 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
3678 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
3679 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
3772a991 3680
da0436e9
JS
3681 /*
3682 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
3683 * used to create the sg_dma_buf_pool must be dynamically calculated.
3684 * 2 segments are added since the IOCB needs a command and response bde.
3685 * To insure that the scsi sgl does not cross a 4k page boundary only
28baac74 3686 * sgl sizes of must be a power of 2.
da0436e9 3687 */
28baac74
JS
3688 buf_size = (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp) +
3689 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct sli4_sge)));
3690 /* Feature Level 1 hardware is limited to 2 pages */
3691 if ((bf_get(lpfc_sli_intf_featurelevel1, &phba->sli4_hba.sli_intf) ==
3692 LPFC_SLI_INTF_FEATURELEVEL1_1))
3693 max_buf_size = LPFC_SLI4_FL1_MAX_BUF_SIZE;
da0436e9 3694 else
28baac74
JS
3695 max_buf_size = LPFC_SLI4_MAX_BUF_SIZE;
3696 for (dma_buf_size = LPFC_SLI4_MIN_BUF_SIZE;
3697 dma_buf_size < max_buf_size && buf_size > dma_buf_size;
3698 dma_buf_size = dma_buf_size << 1)
3699 ;
3700 if (dma_buf_size == max_buf_size)
3701 phba->cfg_sg_seg_cnt = (dma_buf_size -
3702 sizeof(struct fcp_cmnd) - sizeof(struct fcp_rsp) -
3703 (2 * sizeof(struct sli4_sge))) /
3704 sizeof(struct sli4_sge);
3705 phba->cfg_sg_dma_buf_size = dma_buf_size;
3772a991 3706
da0436e9
JS
3707 /* Initialize buffer queue management fields */
3708 hbq_count = lpfc_sli_hbq_count();
3709 for (i = 0; i < hbq_count; ++i)
3710 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
3711 INIT_LIST_HEAD(&phba->rb_pend_list);
3712 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
3713 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
3772a991 3714
da0436e9
JS
3715 /*
3716 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
3717 */
3718 /* Initialize the Abort scsi buffer list used by driver */
3719 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
3720 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
3721 /* This abort list used by worker thread */
3722 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
3772a991 3723
da0436e9
JS
3724 /*
3725 * Initialize dirver internal slow-path work queues
3726 */
3772a991 3727
da0436e9
JS
3728 /* Driver internel slow-path CQ Event pool */
3729 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
3730 /* Response IOCB work queue list */
45ed1190 3731 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
da0436e9
JS
3732 /* Asynchronous event CQ Event work queue list */
3733 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
3734 /* Fast-path XRI aborted CQ Event work queue list */
3735 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
3736 /* Slow-path XRI aborted CQ Event work queue list */
3737 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
3738 /* Receive queue CQ Event work queue list */
3739 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
3740
3741 /* Initialize the driver internal SLI layer lists. */
3742 lpfc_sli_setup(phba);
3743 lpfc_sli_queue_setup(phba);
3772a991 3744
da0436e9
JS
3745 /* Allocate device driver memory */
3746 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
3747 if (rc)
3748 return -ENOMEM;
3749
3750 /* Create the bootstrap mailbox command */
3751 rc = lpfc_create_bootstrap_mbox(phba);
3752 if (unlikely(rc))
3753 goto out_free_mem;
3754
3755 /* Set up the host's endian order with the device. */
3756 rc = lpfc_setup_endian_order(phba);
3757 if (unlikely(rc))
3758 goto out_free_bsmbx;
3759
6669f9bb
JS
3760 rc = lpfc_sli4_fw_cfg_check(phba);
3761 if (unlikely(rc))
3762 goto out_free_bsmbx;
3763
da0436e9
JS
3764 /* Set up the hba's configuration parameters. */
3765 rc = lpfc_sli4_read_config(phba);
3766 if (unlikely(rc))
3767 goto out_free_bsmbx;
3768
3769 /* Perform a function reset */
3770 rc = lpfc_pci_function_reset(phba);
3771 if (unlikely(rc))
3772 goto out_free_bsmbx;
3773
3774 /* Create all the SLI4 queues */
3775 rc = lpfc_sli4_queue_create(phba);
3776 if (rc)
3777 goto out_free_bsmbx;
3778
3779 /* Create driver internal CQE event pool */
3780 rc = lpfc_sli4_cq_event_pool_create(phba);
3781 if (rc)
3782 goto out_destroy_queue;
3783
3784 /* Initialize and populate the iocb list per host */
3785 rc = lpfc_init_sgl_list(phba);
3786 if (rc) {
3787 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3788 "1400 Failed to initialize sgl list.\n");
3789 goto out_destroy_cq_event_pool;
3790 }
3791 rc = lpfc_init_active_sgl_array(phba);
3792 if (rc) {
3793 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3794 "1430 Failed to initialize sgl list.\n");
3795 goto out_free_sgl_list;
3796 }
3797
3798 rc = lpfc_sli4_init_rpi_hdrs(phba);
3799 if (rc) {
3800 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3801 "1432 Failed to initialize rpi headers.\n");
3802 goto out_free_active_sgl;
3803 }
3804
3805 phba->sli4_hba.fcp_eq_hdl = kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
3806 phba->cfg_fcp_eq_count), GFP_KERNEL);
3807 if (!phba->sli4_hba.fcp_eq_hdl) {
3808 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3809 "2572 Failed allocate memory for fast-path "
3810 "per-EQ handle array\n");
3811 goto out_remove_rpi_hdrs;
3812 }
3813
3814 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
3815 phba->sli4_hba.cfg_eqn), GFP_KERNEL);
3816 if (!phba->sli4_hba.msix_entries) {
3817 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3818 "2573 Failed allocate memory for msi-x "
3819 "interrupt vector entries\n");
3820 goto out_free_fcp_eq_hdl;
3821 }
3822
28baac74
JS
3823 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
3824 GFP_KERNEL);
3825 if (!mboxq) {
3826 rc = -ENOMEM;
3827 goto out_free_fcp_eq_hdl;
3828 }
3829
3830 /* Get the Supported Pages. It is always available. */
3831 lpfc_supported_pages(mboxq);
3832 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
3833 if (unlikely(rc)) {
3834 rc = -EIO;
3835 mempool_free(mboxq, phba->mbox_mem_pool);
3836 goto out_free_fcp_eq_hdl;
3837 }
3838
3839 mqe = &mboxq->u.mqe;
3840 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
3841 LPFC_MAX_SUPPORTED_PAGES);
3842 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
3843 switch (pn_page[i]) {
3844 case LPFC_SLI4_PARAMETERS:
3845 phba->sli4_hba.pc_sli4_params.supported = 1;
3846 break;
3847 default:
3848 break;
3849 }
3850 }
3851
3852 /* Read the port's SLI4 Parameters capabilities if supported. */
3853 if (phba->sli4_hba.pc_sli4_params.supported)
3854 rc = lpfc_pc_sli4_params_get(phba, mboxq);
3855 mempool_free(mboxq, phba->mbox_mem_pool);
3856 if (rc) {
3857 rc = -EIO;
3858 goto out_free_fcp_eq_hdl;
3859 }
da0436e9
JS
3860 return rc;
3861
3862out_free_fcp_eq_hdl:
3863 kfree(phba->sli4_hba.fcp_eq_hdl);
3864out_remove_rpi_hdrs:
3865 lpfc_sli4_remove_rpi_hdrs(phba);
3866out_free_active_sgl:
3867 lpfc_free_active_sgl(phba);
3868out_free_sgl_list:
3869 lpfc_free_sgl_list(phba);
3870out_destroy_cq_event_pool:
3871 lpfc_sli4_cq_event_pool_destroy(phba);
3872out_destroy_queue:
3873 lpfc_sli4_queue_destroy(phba);
3874out_free_bsmbx:
3875 lpfc_destroy_bootstrap_mbox(phba);
3876out_free_mem:
3877 lpfc_mem_free(phba);
3878 return rc;
3879}
3880
3881/**
3882 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
3883 * @phba: pointer to lpfc hba data structure.
3884 *
3885 * This routine is invoked to unset the driver internal resources set up
3886 * specific for supporting the SLI-4 HBA device it attached to.
3887 **/
3888static void
3889lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
3890{
3891 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
3892
3893 /* unregister default FCFI from the HBA */
3894 lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
3895
3896 /* Free the default FCR table */
3897 lpfc_sli_remove_dflt_fcf(phba);
3898
3899 /* Free memory allocated for msi-x interrupt vector entries */
3900 kfree(phba->sli4_hba.msix_entries);
3901
3902 /* Free memory allocated for fast-path work queue handles */
3903 kfree(phba->sli4_hba.fcp_eq_hdl);
3904
3905 /* Free the allocated rpi headers. */
3906 lpfc_sli4_remove_rpi_hdrs(phba);
d11e31dd 3907 lpfc_sli4_remove_rpis(phba);
da0436e9
JS
3908
3909 /* Free the ELS sgl list */
3910 lpfc_free_active_sgl(phba);
3911 lpfc_free_sgl_list(phba);
3912
3913 /* Free the SCSI sgl management array */
3914 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
3915
3916 /* Free the SLI4 queues */
3917 lpfc_sli4_queue_destroy(phba);
3918
3919 /* Free the completion queue EQ event pool */
3920 lpfc_sli4_cq_event_release_all(phba);
3921 lpfc_sli4_cq_event_pool_destroy(phba);
3922
3923 /* Reset SLI4 HBA FCoE function */
3924 lpfc_pci_function_reset(phba);
3925
3926 /* Free the bsmbx region. */
3927 lpfc_destroy_bootstrap_mbox(phba);
3928
3929 /* Free the SLI Layer memory with SLI4 HBAs */
3930 lpfc_mem_free_all(phba);
3931
3932 /* Free the current connect table */
3933 list_for_each_entry_safe(conn_entry, next_conn_entry,
4d9ab994
JS
3934 &phba->fcf_conn_rec_list, list) {
3935 list_del_init(&conn_entry->list);
da0436e9 3936 kfree(conn_entry);
4d9ab994 3937 }
da0436e9
JS
3938
3939 return;
3940}
3941
3942/**
3943 * lpfc_init_api_table_setup - Set up init api fucntion jump table
3944 * @phba: The hba struct for which this call is being executed.
3945 * @dev_grp: The HBA PCI-Device group number.
3946 *
3947 * This routine sets up the device INIT interface API function jump table
3948 * in @phba struct.
3949 *
3950 * Returns: 0 - success, -ENODEV - failure.
3951 **/
3952int
3953lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
3954{
3955 switch (dev_grp) {
3956 case LPFC_PCI_DEV_LP:
3957 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
3958 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
3959 phba->lpfc_stop_port = lpfc_stop_port_s3;
3960 break;
3961 case LPFC_PCI_DEV_OC:
3962 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
3963 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
3964 phba->lpfc_stop_port = lpfc_stop_port_s4;
3965 break;
3966 default:
3967 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3968 "1431 Invalid HBA PCI-device group: 0x%x\n",
3969 dev_grp);
3970 return -ENODEV;
3971 break;
3972 }
3973 return 0;
3974}
3975
3976/**
3977 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
3978 * @phba: pointer to lpfc hba data structure.
3979 *
3980 * This routine is invoked to set up the driver internal resources before the
3981 * device specific resource setup to support the HBA device it attached to.
3982 *
3983 * Return codes
af901ca1 3984 * 0 - successful
da0436e9
JS
3985 * other values - error
3986 **/
3987static int
3988lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
3989{
3990 /*
3991 * Driver resources common to all SLI revisions
3992 */
3993 atomic_set(&phba->fast_event_count, 0);
3994 spin_lock_init(&phba->hbalock);
3995
3996 /* Initialize ndlp management spinlock */
3997 spin_lock_init(&phba->ndlp_lock);
3998
3999 INIT_LIST_HEAD(&phba->port_list);
4000 INIT_LIST_HEAD(&phba->work_list);
4001 init_waitqueue_head(&phba->wait_4_mlo_m_q);
4002
4003 /* Initialize the wait queue head for the kernel thread */
4004 init_waitqueue_head(&phba->work_waitq);
4005
4006 /* Initialize the scsi buffer list used by driver for scsi IO */
4007 spin_lock_init(&phba->scsi_buf_list_lock);
4008 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
4009
4010 /* Initialize the fabric iocb list */
4011 INIT_LIST_HEAD(&phba->fabric_iocb_list);
4012
4013 /* Initialize list to save ELS buffers */
4014 INIT_LIST_HEAD(&phba->elsbuf);
4015
4016 /* Initialize FCF connection rec list */
4017 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
4018
4019 return 0;
4020}
4021
4022/**
4023 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
4024 * @phba: pointer to lpfc hba data structure.
4025 *
4026 * This routine is invoked to set up the driver internal resources after the
4027 * device specific resource setup to support the HBA device it attached to.
4028 *
4029 * Return codes
af901ca1 4030 * 0 - successful
da0436e9
JS
4031 * other values - error
4032 **/
4033static int
4034lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
4035{
4036 int error;
4037
4038 /* Startup the kernel thread for this host adapter. */
4039 phba->worker_thread = kthread_run(lpfc_do_work, phba,
4040 "lpfc_worker_%d", phba->brd_no);
4041 if (IS_ERR(phba->worker_thread)) {
4042 error = PTR_ERR(phba->worker_thread);
4043 return error;
3772a991
JS
4044 }
4045
4046 return 0;
4047}
4048
4049/**
4050 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
4051 * @phba: pointer to lpfc hba data structure.
4052 *
4053 * This routine is invoked to unset the driver internal resources set up after
4054 * the device specific resource setup for supporting the HBA device it
4055 * attached to.
4056 **/
4057static void
4058lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
4059{
4060 /* Stop kernel worker thread */
4061 kthread_stop(phba->worker_thread);
4062}
4063
4064/**
4065 * lpfc_free_iocb_list - Free iocb list.
4066 * @phba: pointer to lpfc hba data structure.
4067 *
4068 * This routine is invoked to free the driver's IOCB list and memory.
4069 **/
4070static void
4071lpfc_free_iocb_list(struct lpfc_hba *phba)
4072{
4073 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
4074
4075 spin_lock_irq(&phba->hbalock);
4076 list_for_each_entry_safe(iocbq_entry, iocbq_next,
4077 &phba->lpfc_iocb_list, list) {
4078 list_del(&iocbq_entry->list);
4079 kfree(iocbq_entry);
4080 phba->total_iocbq_bufs--;
98c9ea5c 4081 }
3772a991
JS
4082 spin_unlock_irq(&phba->hbalock);
4083
4084 return;
4085}
4086
4087/**
4088 * lpfc_init_iocb_list - Allocate and initialize iocb list.
4089 * @phba: pointer to lpfc hba data structure.
4090 *
4091 * This routine is invoked to allocate and initizlize the driver's IOCB
4092 * list and set up the IOCB tag array accordingly.
4093 *
4094 * Return codes
af901ca1 4095 * 0 - successful
3772a991
JS
4096 * other values - error
4097 **/
4098static int
4099lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
4100{
4101 struct lpfc_iocbq *iocbq_entry = NULL;
4102 uint16_t iotag;
4103 int i;
dea3101e 4104
4105 /* Initialize and populate the iocb list per host. */
4106 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
3772a991 4107 for (i = 0; i < iocb_count; i++) {
dd00cc48 4108 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea3101e 4109 if (iocbq_entry == NULL) {
4110 printk(KERN_ERR "%s: only allocated %d iocbs of "
4111 "expected %d count. Unloading driver.\n",
cadbd4a5 4112 __func__, i, LPFC_IOCB_LIST_CNT);
dea3101e 4113 goto out_free_iocbq;
4114 }
4115
604a3e30
JB
4116 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
4117 if (iotag == 0) {
3772a991 4118 kfree(iocbq_entry);
604a3e30 4119 printk(KERN_ERR "%s: failed to allocate IOTAG. "
3772a991 4120 "Unloading driver.\n", __func__);
604a3e30
JB
4121 goto out_free_iocbq;
4122 }
3772a991 4123 iocbq_entry->sli4_xritag = NO_XRI;
2e0fef85
JS
4124
4125 spin_lock_irq(&phba->hbalock);
dea3101e 4126 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
4127 phba->total_iocbq_bufs++;
2e0fef85 4128 spin_unlock_irq(&phba->hbalock);
dea3101e 4129 }
4130
3772a991 4131 return 0;
dea3101e 4132
3772a991
JS
4133out_free_iocbq:
4134 lpfc_free_iocb_list(phba);
dea3101e 4135
3772a991
JS
4136 return -ENOMEM;
4137}
5e9d9b82 4138
3772a991 4139/**
da0436e9
JS
4140 * lpfc_free_sgl_list - Free sgl list.
4141 * @phba: pointer to lpfc hba data structure.
3772a991 4142 *
da0436e9 4143 * This routine is invoked to free the driver's sgl list and memory.
3772a991 4144 **/
da0436e9
JS
4145static void
4146lpfc_free_sgl_list(struct lpfc_hba *phba)
3772a991 4147{
da0436e9
JS
4148 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
4149 LIST_HEAD(sglq_list);
4150 int rc = 0;
dea3101e 4151
da0436e9
JS
4152 spin_lock_irq(&phba->hbalock);
4153 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
4154 spin_unlock_irq(&phba->hbalock);
dea3101e 4155
da0436e9
JS
4156 list_for_each_entry_safe(sglq_entry, sglq_next,
4157 &sglq_list, list) {
4158 list_del(&sglq_entry->list);
4159 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
4160 kfree(sglq_entry);
4161 phba->sli4_hba.total_sglq_bufs--;
4162 }
4163 rc = lpfc_sli4_remove_all_sgl_pages(phba);
4164 if (rc) {
4165 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6a9c52cf 4166 "2005 Unable to deregister pages from HBA: %x\n", rc);
3772a991 4167 }
da0436e9
JS
4168 kfree(phba->sli4_hba.lpfc_els_sgl_array);
4169}
92d7f7b0 4170
da0436e9
JS
4171/**
4172 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
4173 * @phba: pointer to lpfc hba data structure.
4174 *
4175 * This routine is invoked to allocate the driver's active sgl memory.
4176 * This array will hold the sglq_entry's for active IOs.
4177 **/
4178static int
4179lpfc_init_active_sgl_array(struct lpfc_hba *phba)
4180{
4181 int size;
4182 size = sizeof(struct lpfc_sglq *);
4183 size *= phba->sli4_hba.max_cfg_param.max_xri;
4184
4185 phba->sli4_hba.lpfc_sglq_active_list =
4186 kzalloc(size, GFP_KERNEL);
4187 if (!phba->sli4_hba.lpfc_sglq_active_list)
4188 return -ENOMEM;
4189 return 0;
3772a991
JS
4190}
4191
4192/**
da0436e9 4193 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
3772a991
JS
4194 * @phba: pointer to lpfc hba data structure.
4195 *
da0436e9
JS
4196 * This routine is invoked to walk through the array of active sglq entries
4197 * and free all of the resources.
4198 * This is just a place holder for now.
3772a991
JS
4199 **/
4200static void
da0436e9 4201lpfc_free_active_sgl(struct lpfc_hba *phba)
3772a991 4202{
da0436e9 4203 kfree(phba->sli4_hba.lpfc_sglq_active_list);
3772a991
JS
4204}
4205
4206/**
da0436e9 4207 * lpfc_init_sgl_list - Allocate and initialize sgl list.
3772a991
JS
4208 * @phba: pointer to lpfc hba data structure.
4209 *
da0436e9
JS
4210 * This routine is invoked to allocate and initizlize the driver's sgl
4211 * list and set up the sgl xritag tag array accordingly.
3772a991
JS
4212 *
4213 * Return codes
af901ca1 4214 * 0 - successful
da0436e9 4215 * other values - error
3772a991
JS
4216 **/
4217static int
da0436e9 4218lpfc_init_sgl_list(struct lpfc_hba *phba)
3772a991 4219{
da0436e9
JS
4220 struct lpfc_sglq *sglq_entry = NULL;
4221 int i;
4222 int els_xri_cnt;
4223
4224 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
4225 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4226 "2400 lpfc_init_sgl_list els %d.\n",
4227 els_xri_cnt);
4228 /* Initialize and populate the sglq list per host/VF. */
4229 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
4230 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
4231
4232 /* Sanity check on XRI management */
4233 if (phba->sli4_hba.max_cfg_param.max_xri <= els_xri_cnt) {
4234 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4235 "2562 No room left for SCSI XRI allocation: "
4236 "max_xri=%d, els_xri=%d\n",
4237 phba->sli4_hba.max_cfg_param.max_xri,
4238 els_xri_cnt);
4239 return -ENOMEM;
4240 }
3772a991 4241
da0436e9
JS
4242 /* Allocate memory for the ELS XRI management array */
4243 phba->sli4_hba.lpfc_els_sgl_array =
4244 kzalloc((sizeof(struct lpfc_sglq *) * els_xri_cnt),
4245 GFP_KERNEL);
0ff10d46 4246
da0436e9
JS
4247 if (!phba->sli4_hba.lpfc_els_sgl_array) {
4248 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4249 "2401 Failed to allocate memory for ELS "
4250 "XRI management array of size %d.\n",
4251 els_xri_cnt);
4252 return -ENOMEM;
4253 }
2e0fef85 4254
da0436e9
JS
4255 /* Keep the SCSI XRI into the XRI management array */
4256 phba->sli4_hba.scsi_xri_max =
4257 phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
4258 phba->sli4_hba.scsi_xri_cnt = 0;
4259
4260 phba->sli4_hba.lpfc_scsi_psb_array =
4261 kzalloc((sizeof(struct lpfc_scsi_buf *) *
4262 phba->sli4_hba.scsi_xri_max), GFP_KERNEL);
4263
4264 if (!phba->sli4_hba.lpfc_scsi_psb_array) {
4265 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4266 "2563 Failed to allocate memory for SCSI "
4267 "XRI management array of size %d.\n",
4268 phba->sli4_hba.scsi_xri_max);
4269 kfree(phba->sli4_hba.lpfc_els_sgl_array);
4270 return -ENOMEM;
4271 }
4272
4273 for (i = 0; i < els_xri_cnt; i++) {
4274 sglq_entry = kzalloc(sizeof(struct lpfc_sglq), GFP_KERNEL);
4275 if (sglq_entry == NULL) {
4276 printk(KERN_ERR "%s: only allocated %d sgls of "
4277 "expected %d count. Unloading driver.\n",
4278 __func__, i, els_xri_cnt);
4279 goto out_free_mem;
4280 }
4281
4282 sglq_entry->sli4_xritag = lpfc_sli4_next_xritag(phba);
4283 if (sglq_entry->sli4_xritag == NO_XRI) {
4284 kfree(sglq_entry);
4285 printk(KERN_ERR "%s: failed to allocate XRI.\n"
4286 "Unloading driver.\n", __func__);
4287 goto out_free_mem;
4288 }
4289 sglq_entry->buff_type = GEN_BUFF_TYPE;
4290 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0, &sglq_entry->phys);
4291 if (sglq_entry->virt == NULL) {
4292 kfree(sglq_entry);
4293 printk(KERN_ERR "%s: failed to allocate mbuf.\n"
4294 "Unloading driver.\n", __func__);
4295 goto out_free_mem;
4296 }
4297 sglq_entry->sgl = sglq_entry->virt;
4298 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
4299
4300 /* The list order is used by later block SGL registraton */
4301 spin_lock_irq(&phba->hbalock);
4302 list_add_tail(&sglq_entry->list, &phba->sli4_hba.lpfc_sgl_list);
4303 phba->sli4_hba.lpfc_els_sgl_array[i] = sglq_entry;
4304 phba->sli4_hba.total_sglq_bufs++;
4305 spin_unlock_irq(&phba->hbalock);
4306 }
4307 return 0;
4308
4309out_free_mem:
4310 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
4311 lpfc_free_sgl_list(phba);
4312 return -ENOMEM;
4313}
4314
4315/**
4316 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
4317 * @phba: pointer to lpfc hba data structure.
4318 *
4319 * This routine is invoked to post rpi header templates to the
4320 * HBA consistent with the SLI-4 interface spec. This routine
4321 * posts a PAGE_SIZE memory region to the port to hold up to
4322 * PAGE_SIZE modulo 64 rpi context headers.
4323 * No locks are held here because this is an initialization routine
4324 * called only from probe or lpfc_online when interrupts are not
4325 * enabled and the driver is reinitializing the device.
4326 *
4327 * Return codes
af901ca1 4328 * 0 - successful
da0436e9
JS
4329 * ENOMEM - No availble memory
4330 * EIO - The mailbox failed to complete successfully.
4331 **/
4332int
4333lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
4334{
4335 int rc = 0;
4336 int longs;
4337 uint16_t rpi_count;
4338 struct lpfc_rpi_hdr *rpi_hdr;
4339
4340 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
4341
4342 /*
4343 * Provision an rpi bitmask range for discovery. The total count
4344 * is the difference between max and base + 1.
4345 */
4346 rpi_count = phba->sli4_hba.max_cfg_param.rpi_base +
4347 phba->sli4_hba.max_cfg_param.max_rpi - 1;
4348
4349 longs = ((rpi_count) + BITS_PER_LONG - 1) / BITS_PER_LONG;
4350 phba->sli4_hba.rpi_bmask = kzalloc(longs * sizeof(unsigned long),
4351 GFP_KERNEL);
4352 if (!phba->sli4_hba.rpi_bmask)
4353 return -ENOMEM;
4354
4355 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
4356 if (!rpi_hdr) {
4357 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4358 "0391 Error during rpi post operation\n");
4359 lpfc_sli4_remove_rpis(phba);
4360 rc = -ENODEV;
4361 }
4362
4363 return rc;
4364}
4365
4366/**
4367 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
4368 * @phba: pointer to lpfc hba data structure.
4369 *
4370 * This routine is invoked to allocate a single 4KB memory region to
4371 * support rpis and stores them in the phba. This single region
4372 * provides support for up to 64 rpis. The region is used globally
4373 * by the device.
4374 *
4375 * Returns:
4376 * A valid rpi hdr on success.
4377 * A NULL pointer on any failure.
4378 **/
4379struct lpfc_rpi_hdr *
4380lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
4381{
4382 uint16_t rpi_limit, curr_rpi_range;
4383 struct lpfc_dmabuf *dmabuf;
4384 struct lpfc_rpi_hdr *rpi_hdr;
4385
4386 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
4387 phba->sli4_hba.max_cfg_param.max_rpi - 1;
4388
4389 spin_lock_irq(&phba->hbalock);
4390 curr_rpi_range = phba->sli4_hba.next_rpi;
4391 spin_unlock_irq(&phba->hbalock);
4392
4393 /*
4394 * The port has a limited number of rpis. The increment here
4395 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
4396 * and to allow the full max_rpi range per port.
4397 */
4398 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
4399 return NULL;
4400
4401 /*
4402 * First allocate the protocol header region for the port. The
4403 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
4404 */
4405 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4406 if (!dmabuf)
4407 return NULL;
4408
4409 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4410 LPFC_HDR_TEMPLATE_SIZE,
4411 &dmabuf->phys,
4412 GFP_KERNEL);
4413 if (!dmabuf->virt) {
4414 rpi_hdr = NULL;
4415 goto err_free_dmabuf;
4416 }
4417
4418 memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
4419 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
4420 rpi_hdr = NULL;
4421 goto err_free_coherent;
4422 }
4423
4424 /* Save the rpi header data for cleanup later. */
4425 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
4426 if (!rpi_hdr)
4427 goto err_free_coherent;
4428
4429 rpi_hdr->dmabuf = dmabuf;
4430 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
4431 rpi_hdr->page_count = 1;
4432 spin_lock_irq(&phba->hbalock);
4433 rpi_hdr->start_rpi = phba->sli4_hba.next_rpi;
4434 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
4435
4436 /*
4437 * The next_rpi stores the next module-64 rpi value to post
4438 * in any subsequent rpi memory region postings.
4439 */
4440 phba->sli4_hba.next_rpi += LPFC_RPI_HDR_COUNT;
4441 spin_unlock_irq(&phba->hbalock);
4442 return rpi_hdr;
4443
4444 err_free_coherent:
4445 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
4446 dmabuf->virt, dmabuf->phys);
4447 err_free_dmabuf:
4448 kfree(dmabuf);
4449 return NULL;
4450}
4451
4452/**
4453 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
4454 * @phba: pointer to lpfc hba data structure.
4455 *
4456 * This routine is invoked to remove all memory resources allocated
4457 * to support rpis. This routine presumes the caller has released all
4458 * rpis consumed by fabric or port logins and is prepared to have
4459 * the header pages removed.
4460 **/
4461void
4462lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
4463{
4464 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
4465
4466 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
4467 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
4468 list_del(&rpi_hdr->list);
4469 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
4470 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
4471 kfree(rpi_hdr->dmabuf);
4472 kfree(rpi_hdr);
4473 }
4474
4475 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
4476 memset(phba->sli4_hba.rpi_bmask, 0, sizeof(*phba->sli4_hba.rpi_bmask));
4477}
4478
4479/**
4480 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
4481 * @pdev: pointer to pci device data structure.
4482 *
4483 * This routine is invoked to allocate the driver hba data structure for an
4484 * HBA device. If the allocation is successful, the phba reference to the
4485 * PCI device data structure is set.
4486 *
4487 * Return codes
af901ca1 4488 * pointer to @phba - successful
da0436e9
JS
4489 * NULL - error
4490 **/
4491static struct lpfc_hba *
4492lpfc_hba_alloc(struct pci_dev *pdev)
4493{
4494 struct lpfc_hba *phba;
4495
4496 /* Allocate memory for HBA structure */
4497 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
4498 if (!phba) {
e34ccdfe 4499 dev_err(&pdev->dev, "failed to allocate hba struct\n");
da0436e9
JS
4500 return NULL;
4501 }
4502
4503 /* Set reference to PCI device in HBA structure */
4504 phba->pcidev = pdev;
4505
4506 /* Assign an unused board number */
4507 phba->brd_no = lpfc_get_instance();
4508 if (phba->brd_no < 0) {
4509 kfree(phba);
4510 return NULL;
4511 }
4512
4fede78f 4513 spin_lock_init(&phba->ct_ev_lock);
f1c3b0fc
JS
4514 INIT_LIST_HEAD(&phba->ct_ev_waiters);
4515
da0436e9
JS
4516 return phba;
4517}
4518
4519/**
4520 * lpfc_hba_free - Free driver hba data structure with a device.
4521 * @phba: pointer to lpfc hba data structure.
4522 *
4523 * This routine is invoked to free the driver hba data structure with an
4524 * HBA device.
4525 **/
4526static void
4527lpfc_hba_free(struct lpfc_hba *phba)
4528{
4529 /* Release the driver assigned board number */
4530 idr_remove(&lpfc_hba_index, phba->brd_no);
4531
4532 kfree(phba);
4533 return;
4534}
4535
4536/**
4537 * lpfc_create_shost - Create hba physical port with associated scsi host.
4538 * @phba: pointer to lpfc hba data structure.
4539 *
4540 * This routine is invoked to create HBA physical port and associate a SCSI
4541 * host with it.
4542 *
4543 * Return codes
af901ca1 4544 * 0 - successful
da0436e9
JS
4545 * other values - error
4546 **/
4547static int
4548lpfc_create_shost(struct lpfc_hba *phba)
4549{
4550 struct lpfc_vport *vport;
4551 struct Scsi_Host *shost;
4552
4553 /* Initialize HBA FC structure */
4554 phba->fc_edtov = FF_DEF_EDTOV;
4555 phba->fc_ratov = FF_DEF_RATOV;
4556 phba->fc_altov = FF_DEF_ALTOV;
4557 phba->fc_arbtov = FF_DEF_ARBTOV;
4558
4559 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
4560 if (!vport)
4561 return -ENODEV;
4562
4563 shost = lpfc_shost_from_vport(vport);
4564 phba->pport = vport;
4565 lpfc_debugfs_initialize(vport);
4566 /* Put reference to SCSI host to driver's device private data */
4567 pci_set_drvdata(phba->pcidev, shost);
2e0fef85 4568
3772a991
JS
4569 return 0;
4570}
db2378e0 4571
3772a991
JS
4572/**
4573 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
4574 * @phba: pointer to lpfc hba data structure.
4575 *
4576 * This routine is invoked to destroy HBA physical port and the associated
4577 * SCSI host.
4578 **/
4579static void
4580lpfc_destroy_shost(struct lpfc_hba *phba)
4581{
4582 struct lpfc_vport *vport = phba->pport;
4583
4584 /* Destroy physical port that associated with the SCSI host */
4585 destroy_port(vport);
4586
4587 return;
4588}
4589
4590/**
4591 * lpfc_setup_bg - Setup Block guard structures and debug areas.
4592 * @phba: pointer to lpfc hba data structure.
4593 * @shost: the shost to be used to detect Block guard settings.
4594 *
4595 * This routine sets up the local Block guard protocol settings for @shost.
4596 * This routine also allocates memory for debugging bg buffers.
4597 **/
4598static void
4599lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
4600{
4601 int pagecnt = 10;
4602 if (lpfc_prot_mask && lpfc_prot_guard) {
4603 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4604 "1478 Registering BlockGuard with the "
4605 "SCSI layer\n");
4606 scsi_host_set_prot(shost, lpfc_prot_mask);
4607 scsi_host_set_guard(shost, lpfc_prot_guard);
4608 }
4609 if (!_dump_buf_data) {
4610 while (pagecnt) {
4611 spin_lock_init(&_dump_buf_lock);
4612 _dump_buf_data =
4613 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
4614 if (_dump_buf_data) {
6a9c52cf
JS
4615 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4616 "9043 BLKGRD: allocated %d pages for "
3772a991
JS
4617 "_dump_buf_data at 0x%p\n",
4618 (1 << pagecnt), _dump_buf_data);
4619 _dump_buf_data_order = pagecnt;
4620 memset(_dump_buf_data, 0,
4621 ((1 << PAGE_SHIFT) << pagecnt));
4622 break;
4623 } else
4624 --pagecnt;
4625 }
4626 if (!_dump_buf_data_order)
6a9c52cf
JS
4627 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4628 "9044 BLKGRD: ERROR unable to allocate "
3772a991
JS
4629 "memory for hexdump\n");
4630 } else
6a9c52cf
JS
4631 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4632 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
3772a991
JS
4633 "\n", _dump_buf_data);
4634 if (!_dump_buf_dif) {
4635 while (pagecnt) {
4636 _dump_buf_dif =
4637 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
4638 if (_dump_buf_dif) {
6a9c52cf
JS
4639 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4640 "9046 BLKGRD: allocated %d pages for "
3772a991
JS
4641 "_dump_buf_dif at 0x%p\n",
4642 (1 << pagecnt), _dump_buf_dif);
4643 _dump_buf_dif_order = pagecnt;
4644 memset(_dump_buf_dif, 0,
4645 ((1 << PAGE_SHIFT) << pagecnt));
4646 break;
4647 } else
4648 --pagecnt;
4649 }
4650 if (!_dump_buf_dif_order)
6a9c52cf
JS
4651 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4652 "9047 BLKGRD: ERROR unable to allocate "
3772a991
JS
4653 "memory for hexdump\n");
4654 } else
6a9c52cf
JS
4655 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4656 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
3772a991
JS
4657 _dump_buf_dif);
4658}
4659
4660/**
4661 * lpfc_post_init_setup - Perform necessary device post initialization setup.
4662 * @phba: pointer to lpfc hba data structure.
4663 *
4664 * This routine is invoked to perform all the necessary post initialization
4665 * setup for the device.
4666 **/
4667static void
4668lpfc_post_init_setup(struct lpfc_hba *phba)
4669{
4670 struct Scsi_Host *shost;
4671 struct lpfc_adapter_event_header adapter_event;
4672
4673 /* Get the default values for Model Name and Description */
4674 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
4675
4676 /*
4677 * hba setup may have changed the hba_queue_depth so we need to
4678 * adjust the value of can_queue.
4679 */
4680 shost = pci_get_drvdata(phba->pcidev);
4681 shost->can_queue = phba->cfg_hba_queue_depth - 10;
4682 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
4683 lpfc_setup_bg(phba, shost);
4684
4685 lpfc_host_attrib_init(shost);
4686
4687 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
4688 spin_lock_irq(shost->host_lock);
4689 lpfc_poll_start_timer(phba);
4690 spin_unlock_irq(shost->host_lock);
4691 }
4692
4693 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4694 "0428 Perform SCSI scan\n");
4695 /* Send board arrival event to upper layer */
4696 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
4697 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
4698 fc_host_post_vendor_event(shost, fc_get_event_number(),
4699 sizeof(adapter_event),
4700 (char *) &adapter_event,
4701 LPFC_NL_VENDOR_ID);
4702 return;
4703}
4704
4705/**
4706 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
4707 * @phba: pointer to lpfc hba data structure.
4708 *
4709 * This routine is invoked to set up the PCI device memory space for device
4710 * with SLI-3 interface spec.
4711 *
4712 * Return codes
af901ca1 4713 * 0 - successful
3772a991
JS
4714 * other values - error
4715 **/
4716static int
4717lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
4718{
4719 struct pci_dev *pdev;
4720 unsigned long bar0map_len, bar2map_len;
4721 int i, hbq_count;
4722 void *ptr;
4723 int error = -ENODEV;
4724
4725 /* Obtain PCI device reference */
4726 if (!phba->pcidev)
4727 return error;
4728 else
4729 pdev = phba->pcidev;
4730
4731 /* Set the device DMA mask size */
8e68597d
MR
4732 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
4733 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
4734 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
4735 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
3772a991 4736 return error;
8e68597d
MR
4737 }
4738 }
3772a991
JS
4739
4740 /* Get the bus address of Bar0 and Bar2 and the number of bytes
4741 * required by each mapping.
4742 */
4743 phba->pci_bar0_map = pci_resource_start(pdev, 0);
4744 bar0map_len = pci_resource_len(pdev, 0);
4745
4746 phba->pci_bar2_map = pci_resource_start(pdev, 2);
4747 bar2map_len = pci_resource_len(pdev, 2);
4748
4749 /* Map HBA SLIM to a kernel virtual address. */
4750 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
4751 if (!phba->slim_memmap_p) {
4752 dev_printk(KERN_ERR, &pdev->dev,
4753 "ioremap failed for SLIM memory.\n");
4754 goto out;
4755 }
4756
4757 /* Map HBA Control Registers to a kernel virtual address. */
4758 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
4759 if (!phba->ctrl_regs_memmap_p) {
4760 dev_printk(KERN_ERR, &pdev->dev,
4761 "ioremap failed for HBA control registers.\n");
4762 goto out_iounmap_slim;
4763 }
4764
4765 /* Allocate memory for SLI-2 structures */
4766 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
4767 SLI2_SLIM_SIZE,
4768 &phba->slim2p.phys,
4769 GFP_KERNEL);
4770 if (!phba->slim2p.virt)
4771 goto out_iounmap;
4772
4773 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
4774 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
4775 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
4776 phba->IOCBs = (phba->slim2p.virt +
4777 offsetof(struct lpfc_sli2_slim, IOCBs));
4778
4779 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
4780 lpfc_sli_hbq_size(),
4781 &phba->hbqslimp.phys,
4782 GFP_KERNEL);
4783 if (!phba->hbqslimp.virt)
4784 goto out_free_slim;
4785
4786 hbq_count = lpfc_sli_hbq_count();
4787 ptr = phba->hbqslimp.virt;
4788 for (i = 0; i < hbq_count; ++i) {
4789 phba->hbqs[i].hbq_virt = ptr;
4790 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
4791 ptr += (lpfc_hbq_defs[i]->entry_count *
4792 sizeof(struct lpfc_hbq_entry));
4793 }
4794 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
4795 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
4796
4797 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
4798
4799 INIT_LIST_HEAD(&phba->rb_pend_list);
4800
4801 phba->MBslimaddr = phba->slim_memmap_p;
4802 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
4803 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
4804 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
4805 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
4806
4807 return 0;
4808
4809out_free_slim:
4810 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
4811 phba->slim2p.virt, phba->slim2p.phys);
4812out_iounmap:
4813 iounmap(phba->ctrl_regs_memmap_p);
4814out_iounmap_slim:
4815 iounmap(phba->slim_memmap_p);
4816out:
4817 return error;
4818}
4819
4820/**
4821 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
4822 * @phba: pointer to lpfc hba data structure.
4823 *
4824 * This routine is invoked to unset the PCI device memory space for device
4825 * with SLI-3 interface spec.
4826 **/
4827static void
4828lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
4829{
4830 struct pci_dev *pdev;
4831
4832 /* Obtain PCI device reference */
4833 if (!phba->pcidev)
4834 return;
4835 else
4836 pdev = phba->pcidev;
4837
4838 /* Free coherent DMA memory allocated */
4839 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
4840 phba->hbqslimp.virt, phba->hbqslimp.phys);
4841 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
4842 phba->slim2p.virt, phba->slim2p.phys);
4843
4844 /* I/O memory unmap */
4845 iounmap(phba->ctrl_regs_memmap_p);
4846 iounmap(phba->slim_memmap_p);
4847
4848 return;
4849}
4850
4851/**
da0436e9 4852 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
3772a991
JS
4853 * @phba: pointer to lpfc hba data structure.
4854 *
da0436e9
JS
4855 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
4856 * done and check status.
3772a991 4857 *
da0436e9 4858 * Return 0 if successful, otherwise -ENODEV.
3772a991 4859 **/
da0436e9
JS
4860int
4861lpfc_sli4_post_status_check(struct lpfc_hba *phba)
3772a991 4862{
28baac74 4863 struct lpfc_register sta_reg, uerrlo_reg, uerrhi_reg;
da0436e9 4864 int i, port_error = -ENODEV;
3772a991 4865
da0436e9
JS
4866 if (!phba->sli4_hba.STAregaddr)
4867 return -ENODEV;
3772a991 4868
da0436e9
JS
4869 /* Wait up to 30 seconds for the SLI Port POST done and ready */
4870 for (i = 0; i < 3000; i++) {
4871 sta_reg.word0 = readl(phba->sli4_hba.STAregaddr);
4872 /* Encounter fatal POST error, break out */
4873 if (bf_get(lpfc_hst_state_perr, &sta_reg)) {
4874 port_error = -ENODEV;
4875 break;
4876 }
4877 if (LPFC_POST_STAGE_ARMFW_READY ==
4878 bf_get(lpfc_hst_state_port_status, &sta_reg)) {
4879 port_error = 0;
4880 break;
4881 }
4882 msleep(10);
3772a991
JS
4883 }
4884
da0436e9
JS
4885 if (port_error)
4886 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4887 "1408 Failure HBA POST Status: sta_reg=0x%x, "
4888 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, xrom=x%x, "
4889 "dl=x%x, pstatus=x%x\n", sta_reg.word0,
4890 bf_get(lpfc_hst_state_perr, &sta_reg),
4891 bf_get(lpfc_hst_state_sfi, &sta_reg),
4892 bf_get(lpfc_hst_state_nip, &sta_reg),
4893 bf_get(lpfc_hst_state_ipc, &sta_reg),
4894 bf_get(lpfc_hst_state_xrom, &sta_reg),
4895 bf_get(lpfc_hst_state_dl, &sta_reg),
4896 bf_get(lpfc_hst_state_port_status, &sta_reg));
4897
4898 /* Log device information */
28baac74
JS
4899 phba->sli4_hba.sli_intf.word0 = readl(phba->sli4_hba.SLIINTFregaddr);
4900 if (bf_get(lpfc_sli_intf_valid,
4901 &phba->sli4_hba.sli_intf) == LPFC_SLI_INTF_VALID) {
4902 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4903 "2534 Device Info: ChipType=0x%x, SliRev=0x%x, "
4904 "FeatureL1=0x%x, FeatureL2=0x%x\n",
4905 bf_get(lpfc_sli_intf_sli_family,
4906 &phba->sli4_hba.sli_intf),
4907 bf_get(lpfc_sli_intf_slirev,
4908 &phba->sli4_hba.sli_intf),
4909 bf_get(lpfc_sli_intf_featurelevel1,
4910 &phba->sli4_hba.sli_intf),
4911 bf_get(lpfc_sli_intf_featurelevel2,
4912 &phba->sli4_hba.sli_intf));
4913 }
a747c9ce
JS
4914 phba->sli4_hba.ue_mask_lo = readl(phba->sli4_hba.UEMASKLOregaddr);
4915 phba->sli4_hba.ue_mask_hi = readl(phba->sli4_hba.UEMASKHIregaddr);
8fa38513 4916 /* With uncoverable error, log the error message and return error */
a747c9ce
JS
4917 uerrlo_reg.word0 = readl(phba->sli4_hba.UERRLOregaddr);
4918 uerrhi_reg.word0 = readl(phba->sli4_hba.UERRHIregaddr);
4919 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
4920 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
4921 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4922 "1422 HBA Unrecoverable error: "
4923 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
4924 "ue_mask_lo_reg=0x%x, ue_mask_hi_reg=0x%x\n",
4925 uerrlo_reg.word0, uerrhi_reg.word0,
4926 phba->sli4_hba.ue_mask_lo,
4927 phba->sli4_hba.ue_mask_hi);
8fa38513
JS
4928 return -ENODEV;
4929 }
4930
da0436e9
JS
4931 return port_error;
4932}
3772a991 4933
da0436e9
JS
4934/**
4935 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
4936 * @phba: pointer to lpfc hba data structure.
4937 *
4938 * This routine is invoked to set up SLI4 BAR0 PCI config space register
4939 * memory map.
4940 **/
4941static void
4942lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba)
4943{
4944 phba->sli4_hba.UERRLOregaddr = phba->sli4_hba.conf_regs_memmap_p +
4945 LPFC_UERR_STATUS_LO;
4946 phba->sli4_hba.UERRHIregaddr = phba->sli4_hba.conf_regs_memmap_p +
4947 LPFC_UERR_STATUS_HI;
a747c9ce
JS
4948 phba->sli4_hba.UEMASKLOregaddr = phba->sli4_hba.conf_regs_memmap_p +
4949 LPFC_UE_MASK_LO;
4950 phba->sli4_hba.UEMASKHIregaddr = phba->sli4_hba.conf_regs_memmap_p +
4951 LPFC_UE_MASK_HI;
28baac74
JS
4952 phba->sli4_hba.SLIINTFregaddr = phba->sli4_hba.conf_regs_memmap_p +
4953 LPFC_SLI_INTF;
da0436e9 4954}
3772a991 4955
da0436e9
JS
4956/**
4957 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
4958 * @phba: pointer to lpfc hba data structure.
4959 *
4960 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
4961 * memory map.
4962 **/
4963static void
4964lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
4965{
3772a991 4966
da0436e9
JS
4967 phba->sli4_hba.STAregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4968 LPFC_HST_STATE;
4969 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4970 LPFC_HST_ISR0;
4971 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4972 LPFC_HST_IMR0;
4973 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
4974 LPFC_HST_ISCR0;
4975 return;
3772a991
JS
4976}
4977
4978/**
da0436e9 4979 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
3772a991 4980 * @phba: pointer to lpfc hba data structure.
da0436e9 4981 * @vf: virtual function number
3772a991 4982 *
da0436e9
JS
4983 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
4984 * based on the given viftual function number, @vf.
4985 *
4986 * Return 0 if successful, otherwise -ENODEV.
3772a991 4987 **/
da0436e9
JS
4988static int
4989lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
3772a991 4990{
da0436e9
JS
4991 if (vf > LPFC_VIR_FUNC_MAX)
4992 return -ENODEV;
3772a991 4993
da0436e9
JS
4994 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4995 vf * LPFC_VFR_PAGE_SIZE + LPFC_RQ_DOORBELL);
4996 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4997 vf * LPFC_VFR_PAGE_SIZE + LPFC_WQ_DOORBELL);
4998 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
4999 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
5000 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5001 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
5002 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5003 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
5004 return 0;
3772a991
JS
5005}
5006
5007/**
da0436e9 5008 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
3772a991
JS
5009 * @phba: pointer to lpfc hba data structure.
5010 *
da0436e9
JS
5011 * This routine is invoked to create the bootstrap mailbox
5012 * region consistent with the SLI-4 interface spec. This
5013 * routine allocates all memory necessary to communicate
5014 * mailbox commands to the port and sets up all alignment
5015 * needs. No locks are expected to be held when calling
5016 * this routine.
3772a991
JS
5017 *
5018 * Return codes
af901ca1 5019 * 0 - successful
da0436e9
JS
5020 * ENOMEM - could not allocated memory.
5021 **/
3772a991 5022static int
da0436e9 5023lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
3772a991 5024{
da0436e9
JS
5025 uint32_t bmbx_size;
5026 struct lpfc_dmabuf *dmabuf;
5027 struct dma_address *dma_address;
5028 uint32_t pa_addr;
5029 uint64_t phys_addr;
5030
5031 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5032 if (!dmabuf)
5033 return -ENOMEM;
3772a991 5034
da0436e9
JS
5035 /*
5036 * The bootstrap mailbox region is comprised of 2 parts
5037 * plus an alignment restriction of 16 bytes.
5038 */
5039 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
5040 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5041 bmbx_size,
5042 &dmabuf->phys,
5043 GFP_KERNEL);
5044 if (!dmabuf->virt) {
5045 kfree(dmabuf);
5046 return -ENOMEM;
3772a991 5047 }
da0436e9 5048 memset(dmabuf->virt, 0, bmbx_size);
3772a991 5049
da0436e9
JS
5050 /*
5051 * Initialize the bootstrap mailbox pointers now so that the register
5052 * operations are simple later. The mailbox dma address is required
5053 * to be 16-byte aligned. Also align the virtual memory as each
5054 * maibox is copied into the bmbx mailbox region before issuing the
5055 * command to the port.
5056 */
5057 phba->sli4_hba.bmbx.dmabuf = dmabuf;
5058 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
5059
5060 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
5061 LPFC_ALIGN_16_BYTE);
5062 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
5063 LPFC_ALIGN_16_BYTE);
5064
5065 /*
5066 * Set the high and low physical addresses now. The SLI4 alignment
5067 * requirement is 16 bytes and the mailbox is posted to the port
5068 * as two 30-bit addresses. The other data is a bit marking whether
5069 * the 30-bit address is the high or low address.
5070 * Upcast bmbx aphys to 64bits so shift instruction compiles
5071 * clean on 32 bit machines.
5072 */
5073 dma_address = &phba->sli4_hba.bmbx.dma_address;
5074 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
5075 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
5076 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
5077 LPFC_BMBX_BIT1_ADDR_HI);
5078
5079 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
5080 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
5081 LPFC_BMBX_BIT1_ADDR_LO);
5082 return 0;
3772a991
JS
5083}
5084
5085/**
da0436e9 5086 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
3772a991
JS
5087 * @phba: pointer to lpfc hba data structure.
5088 *
da0436e9
JS
5089 * This routine is invoked to teardown the bootstrap mailbox
5090 * region and release all host resources. This routine requires
5091 * the caller to ensure all mailbox commands recovered, no
5092 * additional mailbox comands are sent, and interrupts are disabled
5093 * before calling this routine.
5094 *
5095 **/
3772a991 5096static void
da0436e9 5097lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
3772a991 5098{
da0436e9
JS
5099 dma_free_coherent(&phba->pcidev->dev,
5100 phba->sli4_hba.bmbx.bmbx_size,
5101 phba->sli4_hba.bmbx.dmabuf->virt,
5102 phba->sli4_hba.bmbx.dmabuf->phys);
5103
5104 kfree(phba->sli4_hba.bmbx.dmabuf);
5105 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
3772a991
JS
5106}
5107
5108/**
da0436e9 5109 * lpfc_sli4_read_config - Get the config parameters.
3772a991
JS
5110 * @phba: pointer to lpfc hba data structure.
5111 *
da0436e9
JS
5112 * This routine is invoked to read the configuration parameters from the HBA.
5113 * The configuration parameters are used to set the base and maximum values
5114 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
5115 * allocation for the port.
3772a991
JS
5116 *
5117 * Return codes
af901ca1 5118 * 0 - successful
da0436e9
JS
5119 * ENOMEM - No availble memory
5120 * EIO - The mailbox failed to complete successfully.
3772a991 5121 **/
da0436e9
JS
5122static int
5123lpfc_sli4_read_config(struct lpfc_hba *phba)
3772a991 5124{
da0436e9
JS
5125 LPFC_MBOXQ_t *pmb;
5126 struct lpfc_mbx_read_config *rd_config;
5127 uint32_t rc = 0;
3772a991 5128
da0436e9
JS
5129 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5130 if (!pmb) {
5131 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5132 "2011 Unable to allocate memory for issuing "
5133 "SLI_CONFIG_SPECIAL mailbox command\n");
5134 return -ENOMEM;
3772a991
JS
5135 }
5136
da0436e9 5137 lpfc_read_config(phba, pmb);
3772a991 5138
da0436e9
JS
5139 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
5140 if (rc != MBX_SUCCESS) {
5141 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5142 "2012 Mailbox failed , mbxCmd x%x "
5143 "READ_CONFIG, mbxStatus x%x\n",
5144 bf_get(lpfc_mqe_command, &pmb->u.mqe),
5145 bf_get(lpfc_mqe_status, &pmb->u.mqe));
5146 rc = -EIO;
5147 } else {
5148 rd_config = &pmb->u.mqe.un.rd_config;
5149 phba->sli4_hba.max_cfg_param.max_xri =
5150 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
5151 phba->sli4_hba.max_cfg_param.xri_base =
5152 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
5153 phba->sli4_hba.max_cfg_param.max_vpi =
5154 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
5155 phba->sli4_hba.max_cfg_param.vpi_base =
5156 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
5157 phba->sli4_hba.max_cfg_param.max_rpi =
5158 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
5159 phba->sli4_hba.max_cfg_param.rpi_base =
5160 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
5161 phba->sli4_hba.max_cfg_param.max_vfi =
5162 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
5163 phba->sli4_hba.max_cfg_param.vfi_base =
5164 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
5165 phba->sli4_hba.max_cfg_param.max_fcfi =
5166 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
5167 phba->sli4_hba.max_cfg_param.fcfi_base =
5168 bf_get(lpfc_mbx_rd_conf_fcfi_base, rd_config);
5169 phba->sli4_hba.max_cfg_param.max_eq =
5170 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
5171 phba->sli4_hba.max_cfg_param.max_rq =
5172 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
5173 phba->sli4_hba.max_cfg_param.max_wq =
5174 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
5175 phba->sli4_hba.max_cfg_param.max_cq =
5176 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
5177 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
5178 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
5179 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
5180 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
5181 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
5ffc266e
JS
5182 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
5183 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
da0436e9
JS
5184 phba->max_vports = phba->max_vpi;
5185 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5186 "2003 cfg params XRI(B:%d M:%d), "
5187 "VPI(B:%d M:%d) "
5188 "VFI(B:%d M:%d) "
5189 "RPI(B:%d M:%d) "
5190 "FCFI(B:%d M:%d)\n",
5191 phba->sli4_hba.max_cfg_param.xri_base,
5192 phba->sli4_hba.max_cfg_param.max_xri,
5193 phba->sli4_hba.max_cfg_param.vpi_base,
5194 phba->sli4_hba.max_cfg_param.max_vpi,
5195 phba->sli4_hba.max_cfg_param.vfi_base,
5196 phba->sli4_hba.max_cfg_param.max_vfi,
5197 phba->sli4_hba.max_cfg_param.rpi_base,
5198 phba->sli4_hba.max_cfg_param.max_rpi,
5199 phba->sli4_hba.max_cfg_param.fcfi_base,
5200 phba->sli4_hba.max_cfg_param.max_fcfi);
3772a991 5201 }
da0436e9
JS
5202 mempool_free(pmb, phba->mbox_mem_pool);
5203
5204 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
5205 if (phba->cfg_hba_queue_depth > (phba->sli4_hba.max_cfg_param.max_xri))
5206 phba->cfg_hba_queue_depth =
5207 phba->sli4_hba.max_cfg_param.max_xri;
5208 return rc;
3772a991
JS
5209}
5210
5211/**
da0436e9 5212 * lpfc_dev_endian_order_setup - Notify the port of the host's endian order.
3772a991
JS
5213 * @phba: pointer to lpfc hba data structure.
5214 *
da0436e9
JS
5215 * This routine is invoked to setup the host-side endian order to the
5216 * HBA consistent with the SLI-4 interface spec.
5217 *
5218 * Return codes
af901ca1 5219 * 0 - successful
da0436e9
JS
5220 * ENOMEM - No availble memory
5221 * EIO - The mailbox failed to complete successfully.
3772a991 5222 **/
da0436e9
JS
5223static int
5224lpfc_setup_endian_order(struct lpfc_hba *phba)
3772a991 5225{
da0436e9
JS
5226 LPFC_MBOXQ_t *mboxq;
5227 uint32_t rc = 0;
5228 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
5229 HOST_ENDIAN_HIGH_WORD1};
3772a991 5230
da0436e9
JS
5231 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5232 if (!mboxq) {
5233 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5234 "0492 Unable to allocate memory for issuing "
5235 "SLI_CONFIG_SPECIAL mailbox command\n");
5236 return -ENOMEM;
5237 }
3772a991 5238
da0436e9
JS
5239 /*
5240 * The SLI4_CONFIG_SPECIAL mailbox command requires the first two
5241 * words to contain special data values and no other data.
5242 */
5243 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
5244 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
5245 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5246 if (rc != MBX_SUCCESS) {
5247 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5248 "0493 SLI_CONFIG_SPECIAL mailbox failed with "
5249 "status x%x\n",
5250 rc);
5251 rc = -EIO;
5252 }
5253
5254 mempool_free(mboxq, phba->mbox_mem_pool);
5255 return rc;
3772a991
JS
5256}
5257
5258/**
da0436e9 5259 * lpfc_sli4_queue_create - Create all the SLI4 queues
3772a991
JS
5260 * @phba: pointer to lpfc hba data structure.
5261 *
da0436e9
JS
5262 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
5263 * operation. For each SLI4 queue type, the parameters such as queue entry
5264 * count (queue depth) shall be taken from the module parameter. For now,
5265 * we just use some constant number as place holder.
5266 *
5267 * Return codes
af901ca1 5268 * 0 - successful
da0436e9
JS
5269 * ENOMEM - No availble memory
5270 * EIO - The mailbox failed to complete successfully.
3772a991 5271 **/
da0436e9
JS
5272static int
5273lpfc_sli4_queue_create(struct lpfc_hba *phba)
3772a991 5274{
da0436e9
JS
5275 struct lpfc_queue *qdesc;
5276 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
5277 int cfg_fcp_wq_count;
5278 int cfg_fcp_eq_count;
3772a991 5279
da0436e9
JS
5280 /*
5281 * Sanity check for confiugred queue parameters against the run-time
5282 * device parameters
5283 */
3772a991 5284
da0436e9
JS
5285 /* Sanity check on FCP fast-path WQ parameters */
5286 cfg_fcp_wq_count = phba->cfg_fcp_wq_count;
5287 if (cfg_fcp_wq_count >
5288 (phba->sli4_hba.max_cfg_param.max_wq - LPFC_SP_WQN_DEF)) {
5289 cfg_fcp_wq_count = phba->sli4_hba.max_cfg_param.max_wq -
5290 LPFC_SP_WQN_DEF;
5291 if (cfg_fcp_wq_count < LPFC_FP_WQN_MIN) {
5292 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5293 "2581 Not enough WQs (%d) from "
5294 "the pci function for supporting "
5295 "FCP WQs (%d)\n",
5296 phba->sli4_hba.max_cfg_param.max_wq,
5297 phba->cfg_fcp_wq_count);
5298 goto out_error;
5299 }
5300 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5301 "2582 Not enough WQs (%d) from the pci "
5302 "function for supporting the requested "
5303 "FCP WQs (%d), the actual FCP WQs can "
5304 "be supported: %d\n",
5305 phba->sli4_hba.max_cfg_param.max_wq,
5306 phba->cfg_fcp_wq_count, cfg_fcp_wq_count);
5307 }
5308 /* The actual number of FCP work queues adopted */
5309 phba->cfg_fcp_wq_count = cfg_fcp_wq_count;
5310
5311 /* Sanity check on FCP fast-path EQ parameters */
5312 cfg_fcp_eq_count = phba->cfg_fcp_eq_count;
5313 if (cfg_fcp_eq_count >
5314 (phba->sli4_hba.max_cfg_param.max_eq - LPFC_SP_EQN_DEF)) {
5315 cfg_fcp_eq_count = phba->sli4_hba.max_cfg_param.max_eq -
5316 LPFC_SP_EQN_DEF;
5317 if (cfg_fcp_eq_count < LPFC_FP_EQN_MIN) {
5318 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5319 "2574 Not enough EQs (%d) from the "
5320 "pci function for supporting FCP "
5321 "EQs (%d)\n",
5322 phba->sli4_hba.max_cfg_param.max_eq,
5323 phba->cfg_fcp_eq_count);
5324 goto out_error;
5325 }
5326 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5327 "2575 Not enough EQs (%d) from the pci "
5328 "function for supporting the requested "
5329 "FCP EQs (%d), the actual FCP EQs can "
5330 "be supported: %d\n",
5331 phba->sli4_hba.max_cfg_param.max_eq,
5332 phba->cfg_fcp_eq_count, cfg_fcp_eq_count);
5333 }
5334 /* It does not make sense to have more EQs than WQs */
5335 if (cfg_fcp_eq_count > phba->cfg_fcp_wq_count) {
5336 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6a9c52cf
JS
5337 "2593 The FCP EQ count(%d) cannot be greater "
5338 "than the FCP WQ count(%d), limiting the "
5339 "FCP EQ count to %d\n", cfg_fcp_eq_count,
da0436e9
JS
5340 phba->cfg_fcp_wq_count,
5341 phba->cfg_fcp_wq_count);
5342 cfg_fcp_eq_count = phba->cfg_fcp_wq_count;
5343 }
5344 /* The actual number of FCP event queues adopted */
5345 phba->cfg_fcp_eq_count = cfg_fcp_eq_count;
5346 /* The overall number of event queues used */
5347 phba->sli4_hba.cfg_eqn = phba->cfg_fcp_eq_count + LPFC_SP_EQN_DEF;
3772a991 5348
da0436e9
JS
5349 /*
5350 * Create Event Queues (EQs)
5351 */
3772a991 5352
da0436e9
JS
5353 /* Get EQ depth from module parameter, fake the default for now */
5354 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
5355 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
3772a991 5356
da0436e9
JS
5357 /* Create slow path event queue */
5358 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5359 phba->sli4_hba.eq_ecount);
5360 if (!qdesc) {
5361 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5362 "0496 Failed allocate slow-path EQ\n");
5363 goto out_error;
5364 }
5365 phba->sli4_hba.sp_eq = qdesc;
5366
5367 /* Create fast-path FCP Event Queue(s) */
5368 phba->sli4_hba.fp_eq = kzalloc((sizeof(struct lpfc_queue *) *
5369 phba->cfg_fcp_eq_count), GFP_KERNEL);
5370 if (!phba->sli4_hba.fp_eq) {
5371 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5372 "2576 Failed allocate memory for fast-path "
5373 "EQ record array\n");
5374 goto out_free_sp_eq;
5375 }
5376 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
5377 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5378 phba->sli4_hba.eq_ecount);
5379 if (!qdesc) {
5380 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5381 "0497 Failed allocate fast-path EQ\n");
5382 goto out_free_fp_eq;
5383 }
5384 phba->sli4_hba.fp_eq[fcp_eqidx] = qdesc;
5385 }
5386
5387 /*
5388 * Create Complete Queues (CQs)
5389 */
5390
5391 /* Get CQ depth from module parameter, fake the default for now */
5392 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
5393 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
5394
5395 /* Create slow-path Mailbox Command Complete Queue */
5396 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5397 phba->sli4_hba.cq_ecount);
5398 if (!qdesc) {
5399 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5400 "0500 Failed allocate slow-path mailbox CQ\n");
5401 goto out_free_fp_eq;
5402 }
5403 phba->sli4_hba.mbx_cq = qdesc;
5404
5405 /* Create slow-path ELS Complete Queue */
5406 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5407 phba->sli4_hba.cq_ecount);
5408 if (!qdesc) {
5409 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5410 "0501 Failed allocate slow-path ELS CQ\n");
5411 goto out_free_mbx_cq;
5412 }
5413 phba->sli4_hba.els_cq = qdesc;
5414
da0436e9
JS
5415
5416 /* Create fast-path FCP Completion Queue(s), one-to-one with EQs */
5417 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
5418 phba->cfg_fcp_eq_count), GFP_KERNEL);
5419 if (!phba->sli4_hba.fcp_cq) {
5420 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5421 "2577 Failed allocate memory for fast-path "
5422 "CQ record array\n");
4d9ab994 5423 goto out_free_els_cq;
da0436e9
JS
5424 }
5425 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
5426 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5427 phba->sli4_hba.cq_ecount);
5428 if (!qdesc) {
5429 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5430 "0499 Failed allocate fast-path FCP "
5431 "CQ (%d)\n", fcp_cqidx);
5432 goto out_free_fcp_cq;
5433 }
5434 phba->sli4_hba.fcp_cq[fcp_cqidx] = qdesc;
5435 }
5436
5437 /* Create Mailbox Command Queue */
5438 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
5439 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
5440
5441 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
5442 phba->sli4_hba.mq_ecount);
5443 if (!qdesc) {
5444 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5445 "0505 Failed allocate slow-path MQ\n");
5446 goto out_free_fcp_cq;
5447 }
5448 phba->sli4_hba.mbx_wq = qdesc;
5449
5450 /*
5451 * Create all the Work Queues (WQs)
5452 */
5453 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
5454 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
5455
5456 /* Create slow-path ELS Work Queue */
5457 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
5458 phba->sli4_hba.wq_ecount);
5459 if (!qdesc) {
5460 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5461 "0504 Failed allocate slow-path ELS WQ\n");
5462 goto out_free_mbx_wq;
5463 }
5464 phba->sli4_hba.els_wq = qdesc;
5465
5466 /* Create fast-path FCP Work Queue(s) */
5467 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
5468 phba->cfg_fcp_wq_count), GFP_KERNEL);
5469 if (!phba->sli4_hba.fcp_wq) {
5470 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5471 "2578 Failed allocate memory for fast-path "
5472 "WQ record array\n");
5473 goto out_free_els_wq;
5474 }
5475 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
5476 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
5477 phba->sli4_hba.wq_ecount);
5478 if (!qdesc) {
5479 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5480 "0503 Failed allocate fast-path FCP "
5481 "WQ (%d)\n", fcp_wqidx);
5482 goto out_free_fcp_wq;
5483 }
5484 phba->sli4_hba.fcp_wq[fcp_wqidx] = qdesc;
5485 }
5486
5487 /*
5488 * Create Receive Queue (RQ)
5489 */
5490 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
5491 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
5492
5493 /* Create Receive Queue for header */
5494 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
5495 phba->sli4_hba.rq_ecount);
5496 if (!qdesc) {
5497 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5498 "0506 Failed allocate receive HRQ\n");
5499 goto out_free_fcp_wq;
5500 }
5501 phba->sli4_hba.hdr_rq = qdesc;
5502
5503 /* Create Receive Queue for data */
5504 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
5505 phba->sli4_hba.rq_ecount);
5506 if (!qdesc) {
5507 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5508 "0507 Failed allocate receive DRQ\n");
5509 goto out_free_hdr_rq;
5510 }
5511 phba->sli4_hba.dat_rq = qdesc;
5512
5513 return 0;
5514
5515out_free_hdr_rq:
5516 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
5517 phba->sli4_hba.hdr_rq = NULL;
5518out_free_fcp_wq:
5519 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--) {
5520 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_wqidx]);
5521 phba->sli4_hba.fcp_wq[fcp_wqidx] = NULL;
5522 }
5523 kfree(phba->sli4_hba.fcp_wq);
5524out_free_els_wq:
5525 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
5526 phba->sli4_hba.els_wq = NULL;
5527out_free_mbx_wq:
5528 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
5529 phba->sli4_hba.mbx_wq = NULL;
5530out_free_fcp_cq:
5531 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--) {
5532 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_cqidx]);
5533 phba->sli4_hba.fcp_cq[fcp_cqidx] = NULL;
5534 }
5535 kfree(phba->sli4_hba.fcp_cq);
da0436e9
JS
5536out_free_els_cq:
5537 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
5538 phba->sli4_hba.els_cq = NULL;
5539out_free_mbx_cq:
5540 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
5541 phba->sli4_hba.mbx_cq = NULL;
5542out_free_fp_eq:
5543 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--) {
5544 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_eqidx]);
5545 phba->sli4_hba.fp_eq[fcp_eqidx] = NULL;
5546 }
5547 kfree(phba->sli4_hba.fp_eq);
5548out_free_sp_eq:
5549 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
5550 phba->sli4_hba.sp_eq = NULL;
5551out_error:
5552 return -ENOMEM;
5553}
5554
5555/**
5556 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
5557 * @phba: pointer to lpfc hba data structure.
5558 *
5559 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
5560 * operation.
5561 *
5562 * Return codes
af901ca1 5563 * 0 - successful
da0436e9
JS
5564 * ENOMEM - No availble memory
5565 * EIO - The mailbox failed to complete successfully.
5566 **/
5567static void
5568lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
5569{
5570 int fcp_qidx;
5571
5572 /* Release mailbox command work queue */
5573 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
5574 phba->sli4_hba.mbx_wq = NULL;
5575
5576 /* Release ELS work queue */
5577 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
5578 phba->sli4_hba.els_wq = NULL;
5579
5580 /* Release FCP work queue */
5581 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
5582 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_qidx]);
5583 kfree(phba->sli4_hba.fcp_wq);
5584 phba->sli4_hba.fcp_wq = NULL;
5585
5586 /* Release unsolicited receive queue */
5587 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
5588 phba->sli4_hba.hdr_rq = NULL;
5589 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
5590 phba->sli4_hba.dat_rq = NULL;
5591
da0436e9
JS
5592 /* Release ELS complete queue */
5593 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
5594 phba->sli4_hba.els_cq = NULL;
5595
5596 /* Release mailbox command complete queue */
5597 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
5598 phba->sli4_hba.mbx_cq = NULL;
5599
5600 /* Release FCP response complete queue */
5601 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5602 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_qidx]);
5603 kfree(phba->sli4_hba.fcp_cq);
5604 phba->sli4_hba.fcp_cq = NULL;
5605
5606 /* Release fast-path event queue */
5607 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5608 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_qidx]);
5609 kfree(phba->sli4_hba.fp_eq);
5610 phba->sli4_hba.fp_eq = NULL;
5611
5612 /* Release slow-path event queue */
5613 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
5614 phba->sli4_hba.sp_eq = NULL;
5615
5616 return;
5617}
5618
5619/**
5620 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
5621 * @phba: pointer to lpfc hba data structure.
5622 *
5623 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
5624 * operation.
5625 *
5626 * Return codes
af901ca1 5627 * 0 - successful
da0436e9
JS
5628 * ENOMEM - No availble memory
5629 * EIO - The mailbox failed to complete successfully.
5630 **/
5631int
5632lpfc_sli4_queue_setup(struct lpfc_hba *phba)
5633{
5634 int rc = -ENOMEM;
5635 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
5636 int fcp_cq_index = 0;
5637
5638 /*
5639 * Set up Event Queues (EQs)
5640 */
5641
5642 /* Set up slow-path event queue */
5643 if (!phba->sli4_hba.sp_eq) {
5644 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5645 "0520 Slow-path EQ not allocated\n");
5646 goto out_error;
5647 }
5648 rc = lpfc_eq_create(phba, phba->sli4_hba.sp_eq,
5649 LPFC_SP_DEF_IMAX);
5650 if (rc) {
5651 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5652 "0521 Failed setup of slow-path EQ: "
5653 "rc = 0x%x\n", rc);
5654 goto out_error;
5655 }
5656 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5657 "2583 Slow-path EQ setup: queue-id=%d\n",
5658 phba->sli4_hba.sp_eq->queue_id);
5659
5660 /* Set up fast-path event queue */
5661 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
5662 if (!phba->sli4_hba.fp_eq[fcp_eqidx]) {
5663 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5664 "0522 Fast-path EQ (%d) not "
5665 "allocated\n", fcp_eqidx);
5666 goto out_destroy_fp_eq;
5667 }
5668 rc = lpfc_eq_create(phba, phba->sli4_hba.fp_eq[fcp_eqidx],
5669 phba->cfg_fcp_imax);
5670 if (rc) {
5671 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5672 "0523 Failed setup of fast-path EQ "
5673 "(%d), rc = 0x%x\n", fcp_eqidx, rc);
5674 goto out_destroy_fp_eq;
5675 }
5676 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5677 "2584 Fast-path EQ setup: "
5678 "queue[%d]-id=%d\n", fcp_eqidx,
5679 phba->sli4_hba.fp_eq[fcp_eqidx]->queue_id);
5680 }
5681
5682 /*
5683 * Set up Complete Queues (CQs)
5684 */
5685
5686 /* Set up slow-path MBOX Complete Queue as the first CQ */
5687 if (!phba->sli4_hba.mbx_cq) {
5688 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5689 "0528 Mailbox CQ not allocated\n");
5690 goto out_destroy_fp_eq;
5691 }
5692 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq, phba->sli4_hba.sp_eq,
5693 LPFC_MCQ, LPFC_MBOX);
5694 if (rc) {
5695 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5696 "0529 Failed setup of slow-path mailbox CQ: "
5697 "rc = 0x%x\n", rc);
5698 goto out_destroy_fp_eq;
5699 }
5700 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5701 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
5702 phba->sli4_hba.mbx_cq->queue_id,
5703 phba->sli4_hba.sp_eq->queue_id);
5704
5705 /* Set up slow-path ELS Complete Queue */
5706 if (!phba->sli4_hba.els_cq) {
5707 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5708 "0530 ELS CQ not allocated\n");
5709 goto out_destroy_mbx_cq;
5710 }
5711 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq, phba->sli4_hba.sp_eq,
5712 LPFC_WCQ, LPFC_ELS);
5713 if (rc) {
5714 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5715 "0531 Failed setup of slow-path ELS CQ: "
5716 "rc = 0x%x\n", rc);
5717 goto out_destroy_mbx_cq;
5718 }
5719 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5720 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
5721 phba->sli4_hba.els_cq->queue_id,
5722 phba->sli4_hba.sp_eq->queue_id);
5723
da0436e9
JS
5724 /* Set up fast-path FCP Response Complete Queue */
5725 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
5726 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
5727 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5728 "0526 Fast-path FCP CQ (%d) not "
5729 "allocated\n", fcp_cqidx);
5730 goto out_destroy_fcp_cq;
5731 }
5732 rc = lpfc_cq_create(phba, phba->sli4_hba.fcp_cq[fcp_cqidx],
5733 phba->sli4_hba.fp_eq[fcp_cqidx],
5734 LPFC_WCQ, LPFC_FCP);
5735 if (rc) {
5736 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5737 "0527 Failed setup of fast-path FCP "
5738 "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
5739 goto out_destroy_fcp_cq;
5740 }
5741 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5742 "2588 FCP CQ setup: cq[%d]-id=%d, "
5743 "parent eq[%d]-id=%d\n",
5744 fcp_cqidx,
5745 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
5746 fcp_cqidx,
5747 phba->sli4_hba.fp_eq[fcp_cqidx]->queue_id);
5748 }
5749
5750 /*
5751 * Set up all the Work Queues (WQs)
5752 */
5753
5754 /* Set up Mailbox Command Queue */
5755 if (!phba->sli4_hba.mbx_wq) {
5756 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5757 "0538 Slow-path MQ not allocated\n");
5758 goto out_destroy_fcp_cq;
5759 }
5760 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
5761 phba->sli4_hba.mbx_cq, LPFC_MBOX);
5762 if (rc) {
5763 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5764 "0539 Failed setup of slow-path MQ: "
5765 "rc = 0x%x\n", rc);
5766 goto out_destroy_fcp_cq;
5767 }
5768 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5769 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
5770 phba->sli4_hba.mbx_wq->queue_id,
5771 phba->sli4_hba.mbx_cq->queue_id);
5772
5773 /* Set up slow-path ELS Work Queue */
5774 if (!phba->sli4_hba.els_wq) {
5775 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5776 "0536 Slow-path ELS WQ not allocated\n");
5777 goto out_destroy_mbx_wq;
5778 }
5779 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
5780 phba->sli4_hba.els_cq, LPFC_ELS);
5781 if (rc) {
5782 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5783 "0537 Failed setup of slow-path ELS WQ: "
5784 "rc = 0x%x\n", rc);
5785 goto out_destroy_mbx_wq;
5786 }
5787 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5788 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
5789 phba->sli4_hba.els_wq->queue_id,
5790 phba->sli4_hba.els_cq->queue_id);
5791
5792 /* Set up fast-path FCP Work Queue */
5793 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
5794 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
5795 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5796 "0534 Fast-path FCP WQ (%d) not "
5797 "allocated\n", fcp_wqidx);
5798 goto out_destroy_fcp_wq;
5799 }
5800 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
5801 phba->sli4_hba.fcp_cq[fcp_cq_index],
5802 LPFC_FCP);
5803 if (rc) {
5804 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5805 "0535 Failed setup of fast-path FCP "
5806 "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
5807 goto out_destroy_fcp_wq;
5808 }
5809 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5810 "2591 FCP WQ setup: wq[%d]-id=%d, "
5811 "parent cq[%d]-id=%d\n",
5812 fcp_wqidx,
5813 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
5814 fcp_cq_index,
5815 phba->sli4_hba.fcp_cq[fcp_cq_index]->queue_id);
5816 /* Round robin FCP Work Queue's Completion Queue assignment */
5817 fcp_cq_index = ((fcp_cq_index + 1) % phba->cfg_fcp_eq_count);
5818 }
5819
5820 /*
5821 * Create Receive Queue (RQ)
5822 */
5823 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
5824 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5825 "0540 Receive Queue not allocated\n");
5826 goto out_destroy_fcp_wq;
5827 }
5828 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
4d9ab994 5829 phba->sli4_hba.els_cq, LPFC_USOL);
da0436e9
JS
5830 if (rc) {
5831 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5832 "0541 Failed setup of Receive Queue: "
5833 "rc = 0x%x\n", rc);
5834 goto out_destroy_fcp_wq;
5835 }
5836 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5837 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
5838 "parent cq-id=%d\n",
5839 phba->sli4_hba.hdr_rq->queue_id,
5840 phba->sli4_hba.dat_rq->queue_id,
4d9ab994 5841 phba->sli4_hba.els_cq->queue_id);
da0436e9
JS
5842 return 0;
5843
5844out_destroy_fcp_wq:
5845 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
5846 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
5847 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
5848out_destroy_mbx_wq:
5849 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
5850out_destroy_fcp_cq:
5851 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
5852 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
da0436e9
JS
5853 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
5854out_destroy_mbx_cq:
5855 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
5856out_destroy_fp_eq:
5857 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
5858 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_eqidx]);
5859 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
5860out_error:
5861 return rc;
5862}
5863
5864/**
5865 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
5866 * @phba: pointer to lpfc hba data structure.
5867 *
5868 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
5869 * operation.
5870 *
5871 * Return codes
af901ca1 5872 * 0 - successful
da0436e9
JS
5873 * ENOMEM - No availble memory
5874 * EIO - The mailbox failed to complete successfully.
5875 **/
5876void
5877lpfc_sli4_queue_unset(struct lpfc_hba *phba)
5878{
5879 int fcp_qidx;
5880
5881 /* Unset mailbox command work queue */
5882 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
5883 /* Unset ELS work queue */
5884 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
5885 /* Unset unsolicited receive queue */
5886 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
5887 /* Unset FCP work queue */
5888 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
5889 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
5890 /* Unset mailbox command complete queue */
5891 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
5892 /* Unset ELS complete queue */
5893 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
da0436e9
JS
5894 /* Unset FCP response complete queue */
5895 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5896 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
5897 /* Unset fast-path event queue */
5898 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5899 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_qidx]);
5900 /* Unset slow-path event queue */
5901 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
5902}
5903
5904/**
5905 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
5906 * @phba: pointer to lpfc hba data structure.
5907 *
5908 * This routine is invoked to allocate and set up a pool of completion queue
5909 * events. The body of the completion queue event is a completion queue entry
5910 * CQE. For now, this pool is used for the interrupt service routine to queue
5911 * the following HBA completion queue events for the worker thread to process:
5912 * - Mailbox asynchronous events
5913 * - Receive queue completion unsolicited events
5914 * Later, this can be used for all the slow-path events.
5915 *
5916 * Return codes
af901ca1 5917 * 0 - successful
da0436e9
JS
5918 * -ENOMEM - No availble memory
5919 **/
5920static int
5921lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
5922{
5923 struct lpfc_cq_event *cq_event;
5924 int i;
5925
5926 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
5927 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
5928 if (!cq_event)
5929 goto out_pool_create_fail;
5930 list_add_tail(&cq_event->list,
5931 &phba->sli4_hba.sp_cqe_event_pool);
5932 }
5933 return 0;
5934
5935out_pool_create_fail:
5936 lpfc_sli4_cq_event_pool_destroy(phba);
5937 return -ENOMEM;
5938}
5939
5940/**
5941 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
5942 * @phba: pointer to lpfc hba data structure.
5943 *
5944 * This routine is invoked to free the pool of completion queue events at
5945 * driver unload time. Note that, it is the responsibility of the driver
5946 * cleanup routine to free all the outstanding completion-queue events
5947 * allocated from this pool back into the pool before invoking this routine
5948 * to destroy the pool.
5949 **/
5950static void
5951lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
5952{
5953 struct lpfc_cq_event *cq_event, *next_cq_event;
5954
5955 list_for_each_entry_safe(cq_event, next_cq_event,
5956 &phba->sli4_hba.sp_cqe_event_pool, list) {
5957 list_del(&cq_event->list);
5958 kfree(cq_event);
5959 }
5960}
5961
5962/**
5963 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
5964 * @phba: pointer to lpfc hba data structure.
5965 *
5966 * This routine is the lock free version of the API invoked to allocate a
5967 * completion-queue event from the free pool.
5968 *
5969 * Return: Pointer to the newly allocated completion-queue event if successful
5970 * NULL otherwise.
5971 **/
5972struct lpfc_cq_event *
5973__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
5974{
5975 struct lpfc_cq_event *cq_event = NULL;
5976
5977 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
5978 struct lpfc_cq_event, list);
5979 return cq_event;
5980}
5981
5982/**
5983 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
5984 * @phba: pointer to lpfc hba data structure.
5985 *
5986 * This routine is the lock version of the API invoked to allocate a
5987 * completion-queue event from the free pool.
5988 *
5989 * Return: Pointer to the newly allocated completion-queue event if successful
5990 * NULL otherwise.
5991 **/
5992struct lpfc_cq_event *
5993lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
5994{
5995 struct lpfc_cq_event *cq_event;
5996 unsigned long iflags;
5997
5998 spin_lock_irqsave(&phba->hbalock, iflags);
5999 cq_event = __lpfc_sli4_cq_event_alloc(phba);
6000 spin_unlock_irqrestore(&phba->hbalock, iflags);
6001 return cq_event;
6002}
6003
6004/**
6005 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
6006 * @phba: pointer to lpfc hba data structure.
6007 * @cq_event: pointer to the completion queue event to be freed.
6008 *
6009 * This routine is the lock free version of the API invoked to release a
6010 * completion-queue event back into the free pool.
6011 **/
6012void
6013__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
6014 struct lpfc_cq_event *cq_event)
6015{
6016 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
6017}
6018
6019/**
6020 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
6021 * @phba: pointer to lpfc hba data structure.
6022 * @cq_event: pointer to the completion queue event to be freed.
6023 *
6024 * This routine is the lock version of the API invoked to release a
6025 * completion-queue event back into the free pool.
6026 **/
6027void
6028lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
6029 struct lpfc_cq_event *cq_event)
6030{
6031 unsigned long iflags;
6032 spin_lock_irqsave(&phba->hbalock, iflags);
6033 __lpfc_sli4_cq_event_release(phba, cq_event);
6034 spin_unlock_irqrestore(&phba->hbalock, iflags);
6035}
6036
6037/**
6038 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
6039 * @phba: pointer to lpfc hba data structure.
6040 *
6041 * This routine is to free all the pending completion-queue events to the
6042 * back into the free pool for device reset.
6043 **/
6044static void
6045lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
6046{
6047 LIST_HEAD(cqelist);
6048 struct lpfc_cq_event *cqe;
6049 unsigned long iflags;
6050
6051 /* Retrieve all the pending WCQEs from pending WCQE lists */
6052 spin_lock_irqsave(&phba->hbalock, iflags);
6053 /* Pending FCP XRI abort events */
6054 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
6055 &cqelist);
6056 /* Pending ELS XRI abort events */
6057 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
6058 &cqelist);
6059 /* Pending asynnc events */
6060 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
6061 &cqelist);
6062 spin_unlock_irqrestore(&phba->hbalock, iflags);
6063
6064 while (!list_empty(&cqelist)) {
6065 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
6066 lpfc_sli4_cq_event_release(phba, cqe);
6067 }
6068}
6069
6070/**
6071 * lpfc_pci_function_reset - Reset pci function.
6072 * @phba: pointer to lpfc hba data structure.
6073 *
6074 * This routine is invoked to request a PCI function reset. It will destroys
6075 * all resources assigned to the PCI function which originates this request.
6076 *
6077 * Return codes
af901ca1 6078 * 0 - successful
da0436e9
JS
6079 * ENOMEM - No availble memory
6080 * EIO - The mailbox failed to complete successfully.
6081 **/
6082int
6083lpfc_pci_function_reset(struct lpfc_hba *phba)
6084{
6085 LPFC_MBOXQ_t *mboxq;
6086 uint32_t rc = 0;
6087 uint32_t shdr_status, shdr_add_status;
6088 union lpfc_sli4_cfg_shdr *shdr;
6089
6090 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6091 if (!mboxq) {
6092 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6093 "0494 Unable to allocate memory for issuing "
6094 "SLI_FUNCTION_RESET mailbox command\n");
6095 return -ENOMEM;
6096 }
6097
6098 /* Set up PCI function reset SLI4_CONFIG mailbox-ioctl command */
6099 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
6100 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
6101 LPFC_SLI4_MBX_EMBED);
6102 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6103 shdr = (union lpfc_sli4_cfg_shdr *)
6104 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
6105 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6106 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6107 if (rc != MBX_TIMEOUT)
6108 mempool_free(mboxq, phba->mbox_mem_pool);
6109 if (shdr_status || shdr_add_status || rc) {
6110 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6111 "0495 SLI_FUNCTION_RESET mailbox failed with "
6112 "status x%x add_status x%x, mbx status x%x\n",
6113 shdr_status, shdr_add_status, rc);
6114 rc = -ENXIO;
6115 }
6116 return rc;
6117}
6118
6119/**
6120 * lpfc_sli4_send_nop_mbox_cmds - Send sli-4 nop mailbox commands
6121 * @phba: pointer to lpfc hba data structure.
6122 * @cnt: number of nop mailbox commands to send.
6123 *
6124 * This routine is invoked to send a number @cnt of NOP mailbox command and
6125 * wait for each command to complete.
6126 *
6127 * Return: the number of NOP mailbox command completed.
6128 **/
6129static int
6130lpfc_sli4_send_nop_mbox_cmds(struct lpfc_hba *phba, uint32_t cnt)
6131{
6132 LPFC_MBOXQ_t *mboxq;
6133 int length, cmdsent;
6134 uint32_t mbox_tmo;
6135 uint32_t rc = 0;
6136 uint32_t shdr_status, shdr_add_status;
6137 union lpfc_sli4_cfg_shdr *shdr;
6138
6139 if (cnt == 0) {
6140 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6141 "2518 Requested to send 0 NOP mailbox cmd\n");
6142 return cnt;
6143 }
6144
6145 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6146 if (!mboxq) {
6147 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6148 "2519 Unable to allocate memory for issuing "
6149 "NOP mailbox command\n");
6150 return 0;
6151 }
6152
6153 /* Set up NOP SLI4_CONFIG mailbox-ioctl command */
6154 length = (sizeof(struct lpfc_mbx_nop) -
6155 sizeof(struct lpfc_sli4_cfg_mhdr));
6156 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
6157 LPFC_MBOX_OPCODE_NOP, length, LPFC_SLI4_MBX_EMBED);
6158
6159 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
6160 for (cmdsent = 0; cmdsent < cnt; cmdsent++) {
6161 if (!phba->sli4_hba.intr_enable)
6162 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6163 else
6164 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
6165 if (rc == MBX_TIMEOUT)
6166 break;
6167 /* Check return status */
6168 shdr = (union lpfc_sli4_cfg_shdr *)
6169 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
6170 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6171 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
6172 &shdr->response);
6173 if (shdr_status || shdr_add_status || rc) {
6174 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6175 "2520 NOP mailbox command failed "
6176 "status x%x add_status x%x mbx "
6177 "status x%x\n", shdr_status,
6178 shdr_add_status, rc);
6179 break;
6180 }
6181 }
6182
6183 if (rc != MBX_TIMEOUT)
6184 mempool_free(mboxq, phba->mbox_mem_pool);
6185
6186 return cmdsent;
6187}
6188
6189/**
6190 * lpfc_sli4_fcfi_unreg - Unregister fcfi to device
6191 * @phba: pointer to lpfc hba data structure.
6192 * @fcfi: fcf index.
6193 *
6194 * This routine is invoked to unregister a FCFI from device.
6195 **/
6196void
6197lpfc_sli4_fcfi_unreg(struct lpfc_hba *phba, uint16_t fcfi)
6198{
6199 LPFC_MBOXQ_t *mbox;
6200 uint32_t mbox_tmo;
6201 int rc;
6202 unsigned long flags;
6203
6204 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6205
6206 if (!mbox)
6207 return;
6208
6209 lpfc_unreg_fcfi(mbox, fcfi);
6210
6211 if (!phba->sli4_hba.intr_enable)
6212 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6213 else {
6214 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
6215 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6216 }
6217 if (rc != MBX_TIMEOUT)
6218 mempool_free(mbox, phba->mbox_mem_pool);
6219 if (rc != MBX_SUCCESS)
6220 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6221 "2517 Unregister FCFI command failed "
6222 "status %d, mbxStatus x%x\n", rc,
6223 bf_get(lpfc_mqe_status, &mbox->u.mqe));
6224 else {
6225 spin_lock_irqsave(&phba->hbalock, flags);
6226 /* Mark the FCFI is no longer registered */
6227 phba->fcf.fcf_flag &=
ecfd03c6 6228 ~(FCF_AVAILABLE | FCF_REGISTERED | FCF_SCAN_DONE);
da0436e9
JS
6229 spin_unlock_irqrestore(&phba->hbalock, flags);
6230 }
6231}
6232
6233/**
6234 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
6235 * @phba: pointer to lpfc hba data structure.
6236 *
6237 * This routine is invoked to set up the PCI device memory space for device
6238 * with SLI-4 interface spec.
6239 *
6240 * Return codes
af901ca1 6241 * 0 - successful
da0436e9
JS
6242 * other values - error
6243 **/
6244static int
6245lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
6246{
6247 struct pci_dev *pdev;
6248 unsigned long bar0map_len, bar1map_len, bar2map_len;
6249 int error = -ENODEV;
6250
6251 /* Obtain PCI device reference */
6252 if (!phba->pcidev)
6253 return error;
6254 else
6255 pdev = phba->pcidev;
6256
6257 /* Set the device DMA mask size */
8e68597d
MR
6258 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
6259 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
6260 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
6261 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
da0436e9 6262 return error;
8e68597d
MR
6263 }
6264 }
da0436e9
JS
6265
6266 /* Get the bus address of SLI4 device Bar0, Bar1, and Bar2 and the
6267 * number of bytes required by each mapping. They are actually
1dfb5a47 6268 * mapping to the PCI BAR regions 0 or 1, 2, and 4 by the SLI4 device.
da0436e9 6269 */
1dfb5a47
JS
6270 if (pci_resource_start(pdev, 0)) {
6271 phba->pci_bar0_map = pci_resource_start(pdev, 0);
6272 bar0map_len = pci_resource_len(pdev, 0);
6273 } else {
6274 phba->pci_bar0_map = pci_resource_start(pdev, 1);
6275 bar0map_len = pci_resource_len(pdev, 1);
6276 }
6277 phba->pci_bar1_map = pci_resource_start(pdev, 2);
6278 bar1map_len = pci_resource_len(pdev, 2);
da0436e9 6279
1dfb5a47
JS
6280 phba->pci_bar2_map = pci_resource_start(pdev, 4);
6281 bar2map_len = pci_resource_len(pdev, 4);
da0436e9
JS
6282
6283 /* Map SLI4 PCI Config Space Register base to a kernel virtual addr */
6284 phba->sli4_hba.conf_regs_memmap_p =
6285 ioremap(phba->pci_bar0_map, bar0map_len);
6286 if (!phba->sli4_hba.conf_regs_memmap_p) {
6287 dev_printk(KERN_ERR, &pdev->dev,
6288 "ioremap failed for SLI4 PCI config registers.\n");
6289 goto out;
6290 }
6291
6292 /* Map SLI4 HBA Control Register base to a kernel virtual address. */
6293 phba->sli4_hba.ctrl_regs_memmap_p =
6294 ioremap(phba->pci_bar1_map, bar1map_len);
6295 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
6296 dev_printk(KERN_ERR, &pdev->dev,
6297 "ioremap failed for SLI4 HBA control registers.\n");
6298 goto out_iounmap_conf;
6299 }
6300
6301 /* Map SLI4 HBA Doorbell Register base to a kernel virtual address. */
6302 phba->sli4_hba.drbl_regs_memmap_p =
6303 ioremap(phba->pci_bar2_map, bar2map_len);
6304 if (!phba->sli4_hba.drbl_regs_memmap_p) {
6305 dev_printk(KERN_ERR, &pdev->dev,
6306 "ioremap failed for SLI4 HBA doorbell registers.\n");
6307 goto out_iounmap_ctrl;
6308 }
6309
6310 /* Set up BAR0 PCI config space register memory map */
6311 lpfc_sli4_bar0_register_memmap(phba);
6312
6313 /* Set up BAR1 register memory map */
6314 lpfc_sli4_bar1_register_memmap(phba);
6315
6316 /* Set up BAR2 register memory map */
6317 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
6318 if (error)
6319 goto out_iounmap_all;
6320
6321 return 0;
6322
6323out_iounmap_all:
6324 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
6325out_iounmap_ctrl:
6326 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
6327out_iounmap_conf:
6328 iounmap(phba->sli4_hba.conf_regs_memmap_p);
6329out:
6330 return error;
6331}
6332
6333/**
6334 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
6335 * @phba: pointer to lpfc hba data structure.
6336 *
6337 * This routine is invoked to unset the PCI device memory space for device
6338 * with SLI-4 interface spec.
6339 **/
6340static void
6341lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
6342{
6343 struct pci_dev *pdev;
6344
6345 /* Obtain PCI device reference */
6346 if (!phba->pcidev)
6347 return;
6348 else
6349 pdev = phba->pcidev;
6350
6351 /* Free coherent DMA memory allocated */
6352
6353 /* Unmap I/O memory space */
6354 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
6355 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
6356 iounmap(phba->sli4_hba.conf_regs_memmap_p);
6357
6358 return;
6359}
6360
6361/**
6362 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
6363 * @phba: pointer to lpfc hba data structure.
6364 *
6365 * This routine is invoked to enable the MSI-X interrupt vectors to device
6366 * with SLI-3 interface specs. The kernel function pci_enable_msix() is
6367 * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
6368 * invoked, enables either all or nothing, depending on the current
6369 * availability of PCI vector resources. The device driver is responsible
6370 * for calling the individual request_irq() to register each MSI-X vector
6371 * with a interrupt handler, which is done in this function. Note that
6372 * later when device is unloading, the driver should always call free_irq()
6373 * on all MSI-X vectors it has done request_irq() on before calling
6374 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
6375 * will be left with MSI-X enabled and leaks its vectors.
6376 *
6377 * Return codes
af901ca1 6378 * 0 - successful
da0436e9
JS
6379 * other values - error
6380 **/
6381static int
6382lpfc_sli_enable_msix(struct lpfc_hba *phba)
6383{
6384 int rc, i;
6385 LPFC_MBOXQ_t *pmb;
6386
6387 /* Set up MSI-X multi-message vectors */
6388 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6389 phba->msix_entries[i].entry = i;
6390
6391 /* Configure MSI-X capability structure */
6392 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
6393 ARRAY_SIZE(phba->msix_entries));
6394 if (rc) {
6395 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6396 "0420 PCI enable MSI-X failed (%d)\n", rc);
6397 goto msi_fail_out;
6398 }
6399 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6400 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6401 "0477 MSI-X entry[%d]: vector=x%x "
6402 "message=%d\n", i,
6403 phba->msix_entries[i].vector,
6404 phba->msix_entries[i].entry);
6405 /*
6406 * Assign MSI-X vectors to interrupt handlers
6407 */
6408
6409 /* vector-0 is associated to slow-path handler */
6410 rc = request_irq(phba->msix_entries[0].vector,
6411 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
6412 LPFC_SP_DRIVER_HANDLER_NAME, phba);
6413 if (rc) {
6414 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6415 "0421 MSI-X slow-path request_irq failed "
6416 "(%d)\n", rc);
6417 goto msi_fail_out;
6418 }
6419
6420 /* vector-1 is associated to fast-path handler */
6421 rc = request_irq(phba->msix_entries[1].vector,
6422 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
6423 LPFC_FP_DRIVER_HANDLER_NAME, phba);
6424
6425 if (rc) {
6426 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6427 "0429 MSI-X fast-path request_irq failed "
6428 "(%d)\n", rc);
6429 goto irq_fail_out;
6430 }
6431
6432 /*
6433 * Configure HBA MSI-X attention conditions to messages
6434 */
6435 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6436
6437 if (!pmb) {
6438 rc = -ENOMEM;
6439 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6440 "0474 Unable to allocate memory for issuing "
6441 "MBOX_CONFIG_MSI command\n");
6442 goto mem_fail_out;
6443 }
6444 rc = lpfc_config_msi(phba, pmb);
6445 if (rc)
6446 goto mbx_fail_out;
6447 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6448 if (rc != MBX_SUCCESS) {
6449 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
6450 "0351 Config MSI mailbox command failed, "
6451 "mbxCmd x%x, mbxStatus x%x\n",
6452 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
6453 goto mbx_fail_out;
6454 }
6455
6456 /* Free memory allocated for mailbox command */
6457 mempool_free(pmb, phba->mbox_mem_pool);
6458 return rc;
6459
6460mbx_fail_out:
6461 /* Free memory allocated for mailbox command */
6462 mempool_free(pmb, phba->mbox_mem_pool);
6463
6464mem_fail_out:
6465 /* free the irq already requested */
6466 free_irq(phba->msix_entries[1].vector, phba);
6467
6468irq_fail_out:
6469 /* free the irq already requested */
6470 free_irq(phba->msix_entries[0].vector, phba);
6471
6472msi_fail_out:
6473 /* Unconfigure MSI-X capability structure */
6474 pci_disable_msix(phba->pcidev);
6475 return rc;
6476}
6477
6478/**
6479 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
6480 * @phba: pointer to lpfc hba data structure.
6481 *
6482 * This routine is invoked to release the MSI-X vectors and then disable the
6483 * MSI-X interrupt mode to device with SLI-3 interface spec.
6484 **/
6485static void
6486lpfc_sli_disable_msix(struct lpfc_hba *phba)
6487{
6488 int i;
6489
6490 /* Free up MSI-X multi-message vectors */
6491 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6492 free_irq(phba->msix_entries[i].vector, phba);
6493 /* Disable MSI-X */
6494 pci_disable_msix(phba->pcidev);
6495
6496 return;
6497}
6498
6499/**
6500 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
6501 * @phba: pointer to lpfc hba data structure.
6502 *
6503 * This routine is invoked to enable the MSI interrupt mode to device with
6504 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
6505 * enable the MSI vector. The device driver is responsible for calling the
6506 * request_irq() to register MSI vector with a interrupt the handler, which
6507 * is done in this function.
6508 *
6509 * Return codes
af901ca1 6510 * 0 - successful
da0436e9
JS
6511 * other values - error
6512 */
6513static int
6514lpfc_sli_enable_msi(struct lpfc_hba *phba)
6515{
6516 int rc;
6517
6518 rc = pci_enable_msi(phba->pcidev);
6519 if (!rc)
6520 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6521 "0462 PCI enable MSI mode success.\n");
6522 else {
6523 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6524 "0471 PCI enable MSI mode failed (%d)\n", rc);
6525 return rc;
6526 }
6527
6528 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
6529 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6530 if (rc) {
6531 pci_disable_msi(phba->pcidev);
6532 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6533 "0478 MSI request_irq failed (%d)\n", rc);
6534 }
6535 return rc;
6536}
6537
6538/**
6539 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
6540 * @phba: pointer to lpfc hba data structure.
6541 *
6542 * This routine is invoked to disable the MSI interrupt mode to device with
6543 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
6544 * done request_irq() on before calling pci_disable_msi(). Failure to do so
6545 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
6546 * its vector.
6547 */
6548static void
6549lpfc_sli_disable_msi(struct lpfc_hba *phba)
6550{
6551 free_irq(phba->pcidev->irq, phba);
6552 pci_disable_msi(phba->pcidev);
6553 return;
6554}
6555
6556/**
6557 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
6558 * @phba: pointer to lpfc hba data structure.
6559 *
6560 * This routine is invoked to enable device interrupt and associate driver's
6561 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
6562 * spec. Depends on the interrupt mode configured to the driver, the driver
6563 * will try to fallback from the configured interrupt mode to an interrupt
6564 * mode which is supported by the platform, kernel, and device in the order
6565 * of:
6566 * MSI-X -> MSI -> IRQ.
6567 *
6568 * Return codes
af901ca1 6569 * 0 - successful
da0436e9
JS
6570 * other values - error
6571 **/
6572static uint32_t
6573lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
6574{
6575 uint32_t intr_mode = LPFC_INTR_ERROR;
6576 int retval;
6577
6578 if (cfg_mode == 2) {
6579 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
6580 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
6581 if (!retval) {
6582 /* Now, try to enable MSI-X interrupt mode */
6583 retval = lpfc_sli_enable_msix(phba);
6584 if (!retval) {
6585 /* Indicate initialization to MSI-X mode */
6586 phba->intr_type = MSIX;
6587 intr_mode = 2;
6588 }
6589 }
6590 }
6591
6592 /* Fallback to MSI if MSI-X initialization failed */
6593 if (cfg_mode >= 1 && phba->intr_type == NONE) {
6594 retval = lpfc_sli_enable_msi(phba);
6595 if (!retval) {
6596 /* Indicate initialization to MSI mode */
6597 phba->intr_type = MSI;
6598 intr_mode = 1;
6599 }
6600 }
6601
6602 /* Fallback to INTx if both MSI-X/MSI initalization failed */
6603 if (phba->intr_type == NONE) {
6604 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
6605 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6606 if (!retval) {
6607 /* Indicate initialization to INTx mode */
6608 phba->intr_type = INTx;
6609 intr_mode = 0;
6610 }
6611 }
6612 return intr_mode;
6613}
6614
6615/**
6616 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
6617 * @phba: pointer to lpfc hba data structure.
6618 *
6619 * This routine is invoked to disable device interrupt and disassociate the
6620 * driver's interrupt handler(s) from interrupt vector(s) to device with
6621 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
6622 * release the interrupt vector(s) for the message signaled interrupt.
6623 **/
6624static void
6625lpfc_sli_disable_intr(struct lpfc_hba *phba)
6626{
6627 /* Disable the currently initialized interrupt mode */
6628 if (phba->intr_type == MSIX)
6629 lpfc_sli_disable_msix(phba);
6630 else if (phba->intr_type == MSI)
6631 lpfc_sli_disable_msi(phba);
6632 else if (phba->intr_type == INTx)
6633 free_irq(phba->pcidev->irq, phba);
6634
6635 /* Reset interrupt management states */
6636 phba->intr_type = NONE;
6637 phba->sli.slistat.sli_intr = 0;
6638
6639 return;
6640}
6641
6642/**
6643 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
6644 * @phba: pointer to lpfc hba data structure.
6645 *
6646 * This routine is invoked to enable the MSI-X interrupt vectors to device
6647 * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
6648 * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
6649 * enables either all or nothing, depending on the current availability of
6650 * PCI vector resources. The device driver is responsible for calling the
6651 * individual request_irq() to register each MSI-X vector with a interrupt
6652 * handler, which is done in this function. Note that later when device is
6653 * unloading, the driver should always call free_irq() on all MSI-X vectors
6654 * it has done request_irq() on before calling pci_disable_msix(). Failure
6655 * to do so results in a BUG_ON() and a device will be left with MSI-X
6656 * enabled and leaks its vectors.
6657 *
6658 * Return codes
af901ca1 6659 * 0 - successful
da0436e9
JS
6660 * other values - error
6661 **/
6662static int
6663lpfc_sli4_enable_msix(struct lpfc_hba *phba)
6664{
6665 int rc, index;
6666
6667 /* Set up MSI-X multi-message vectors */
6668 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
6669 phba->sli4_hba.msix_entries[index].entry = index;
6670
6671 /* Configure MSI-X capability structure */
6672 rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
6673 phba->sli4_hba.cfg_eqn);
6674 if (rc) {
6675 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6676 "0484 PCI enable MSI-X failed (%d)\n", rc);
6677 goto msi_fail_out;
6678 }
6679 /* Log MSI-X vector assignment */
6680 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
6681 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6682 "0489 MSI-X entry[%d]: vector=x%x "
6683 "message=%d\n", index,
6684 phba->sli4_hba.msix_entries[index].vector,
6685 phba->sli4_hba.msix_entries[index].entry);
6686 /*
6687 * Assign MSI-X vectors to interrupt handlers
6688 */
6689
6690 /* The first vector must associated to slow-path handler for MQ */
6691 rc = request_irq(phba->sli4_hba.msix_entries[0].vector,
6692 &lpfc_sli4_sp_intr_handler, IRQF_SHARED,
6693 LPFC_SP_DRIVER_HANDLER_NAME, phba);
6694 if (rc) {
6695 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6696 "0485 MSI-X slow-path request_irq failed "
6697 "(%d)\n", rc);
6698 goto msi_fail_out;
6699 }
6700
6701 /* The rest of the vector(s) are associated to fast-path handler(s) */
6702 for (index = 1; index < phba->sli4_hba.cfg_eqn; index++) {
6703 phba->sli4_hba.fcp_eq_hdl[index - 1].idx = index - 1;
6704 phba->sli4_hba.fcp_eq_hdl[index - 1].phba = phba;
6705 rc = request_irq(phba->sli4_hba.msix_entries[index].vector,
6706 &lpfc_sli4_fp_intr_handler, IRQF_SHARED,
6707 LPFC_FP_DRIVER_HANDLER_NAME,
6708 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6709 if (rc) {
6710 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6711 "0486 MSI-X fast-path (%d) "
6712 "request_irq failed (%d)\n", index, rc);
6713 goto cfg_fail_out;
6714 }
6715 }
6716
6717 return rc;
6718
6719cfg_fail_out:
6720 /* free the irq already requested */
6721 for (--index; index >= 1; index--)
6722 free_irq(phba->sli4_hba.msix_entries[index - 1].vector,
6723 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6724
6725 /* free the irq already requested */
6726 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
6727
6728msi_fail_out:
6729 /* Unconfigure MSI-X capability structure */
6730 pci_disable_msix(phba->pcidev);
6731 return rc;
6732}
6733
6734/**
6735 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
6736 * @phba: pointer to lpfc hba data structure.
6737 *
6738 * This routine is invoked to release the MSI-X vectors and then disable the
6739 * MSI-X interrupt mode to device with SLI-4 interface spec.
6740 **/
6741static void
6742lpfc_sli4_disable_msix(struct lpfc_hba *phba)
6743{
6744 int index;
6745
6746 /* Free up MSI-X multi-message vectors */
6747 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
6748
6749 for (index = 1; index < phba->sli4_hba.cfg_eqn; index++)
6750 free_irq(phba->sli4_hba.msix_entries[index].vector,
6751 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
6752 /* Disable MSI-X */
6753 pci_disable_msix(phba->pcidev);
6754
6755 return;
6756}
6757
6758/**
6759 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
6760 * @phba: pointer to lpfc hba data structure.
6761 *
6762 * This routine is invoked to enable the MSI interrupt mode to device with
6763 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
6764 * to enable the MSI vector. The device driver is responsible for calling
6765 * the request_irq() to register MSI vector with a interrupt the handler,
6766 * which is done in this function.
6767 *
6768 * Return codes
af901ca1 6769 * 0 - successful
da0436e9
JS
6770 * other values - error
6771 **/
6772static int
6773lpfc_sli4_enable_msi(struct lpfc_hba *phba)
6774{
6775 int rc, index;
6776
6777 rc = pci_enable_msi(phba->pcidev);
6778 if (!rc)
6779 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6780 "0487 PCI enable MSI mode success.\n");
6781 else {
6782 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6783 "0488 PCI enable MSI mode failed (%d)\n", rc);
6784 return rc;
6785 }
6786
6787 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
6788 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6789 if (rc) {
6790 pci_disable_msi(phba->pcidev);
6791 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6792 "0490 MSI request_irq failed (%d)\n", rc);
6793 }
6794
6795 for (index = 0; index < phba->cfg_fcp_eq_count; index++) {
6796 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
6797 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
6798 }
6799
6800 return rc;
6801}
6802
6803/**
6804 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
6805 * @phba: pointer to lpfc hba data structure.
6806 *
6807 * This routine is invoked to disable the MSI interrupt mode to device with
6808 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
6809 * done request_irq() on before calling pci_disable_msi(). Failure to do so
6810 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
6811 * its vector.
6812 **/
6813static void
6814lpfc_sli4_disable_msi(struct lpfc_hba *phba)
6815{
6816 free_irq(phba->pcidev->irq, phba);
6817 pci_disable_msi(phba->pcidev);
6818 return;
6819}
6820
6821/**
6822 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
6823 * @phba: pointer to lpfc hba data structure.
6824 *
6825 * This routine is invoked to enable device interrupt and associate driver's
6826 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
6827 * interface spec. Depends on the interrupt mode configured to the driver,
6828 * the driver will try to fallback from the configured interrupt mode to an
6829 * interrupt mode which is supported by the platform, kernel, and device in
6830 * the order of:
6831 * MSI-X -> MSI -> IRQ.
6832 *
6833 * Return codes
af901ca1 6834 * 0 - successful
da0436e9
JS
6835 * other values - error
6836 **/
6837static uint32_t
6838lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
6839{
6840 uint32_t intr_mode = LPFC_INTR_ERROR;
6841 int retval, index;
6842
6843 if (cfg_mode == 2) {
6844 /* Preparation before conf_msi mbox cmd */
6845 retval = 0;
6846 if (!retval) {
6847 /* Now, try to enable MSI-X interrupt mode */
6848 retval = lpfc_sli4_enable_msix(phba);
6849 if (!retval) {
6850 /* Indicate initialization to MSI-X mode */
6851 phba->intr_type = MSIX;
6852 intr_mode = 2;
6853 }
6854 }
6855 }
6856
6857 /* Fallback to MSI if MSI-X initialization failed */
6858 if (cfg_mode >= 1 && phba->intr_type == NONE) {
6859 retval = lpfc_sli4_enable_msi(phba);
6860 if (!retval) {
6861 /* Indicate initialization to MSI mode */
6862 phba->intr_type = MSI;
6863 intr_mode = 1;
6864 }
6865 }
6866
6867 /* Fallback to INTx if both MSI-X/MSI initalization failed */
6868 if (phba->intr_type == NONE) {
6869 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
6870 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6871 if (!retval) {
6872 /* Indicate initialization to INTx mode */
6873 phba->intr_type = INTx;
6874 intr_mode = 0;
6875 for (index = 0; index < phba->cfg_fcp_eq_count;
6876 index++) {
6877 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
6878 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
6879 }
6880 }
6881 }
6882 return intr_mode;
6883}
6884
6885/**
6886 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
6887 * @phba: pointer to lpfc hba data structure.
6888 *
6889 * This routine is invoked to disable device interrupt and disassociate
6890 * the driver's interrupt handler(s) from interrupt vector(s) to device
6891 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
6892 * will release the interrupt vector(s) for the message signaled interrupt.
6893 **/
6894static void
6895lpfc_sli4_disable_intr(struct lpfc_hba *phba)
6896{
6897 /* Disable the currently initialized interrupt mode */
6898 if (phba->intr_type == MSIX)
6899 lpfc_sli4_disable_msix(phba);
6900 else if (phba->intr_type == MSI)
6901 lpfc_sli4_disable_msi(phba);
6902 else if (phba->intr_type == INTx)
6903 free_irq(phba->pcidev->irq, phba);
6904
6905 /* Reset interrupt management states */
6906 phba->intr_type = NONE;
6907 phba->sli.slistat.sli_intr = 0;
6908
6909 return;
6910}
6911
6912/**
6913 * lpfc_unset_hba - Unset SLI3 hba device initialization
6914 * @phba: pointer to lpfc hba data structure.
6915 *
6916 * This routine is invoked to unset the HBA device initialization steps to
6917 * a device with SLI-3 interface spec.
6918 **/
6919static void
6920lpfc_unset_hba(struct lpfc_hba *phba)
6921{
6922 struct lpfc_vport *vport = phba->pport;
6923 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6924
6925 spin_lock_irq(shost->host_lock);
6926 vport->load_flag |= FC_UNLOADING;
6927 spin_unlock_irq(shost->host_lock);
6928
6929 lpfc_stop_hba_timers(phba);
6930
6931 phba->pport->work_port_events = 0;
6932
6933 lpfc_sli_hba_down(phba);
6934
6935 lpfc_sli_brdrestart(phba);
6936
6937 lpfc_sli_disable_intr(phba);
6938
6939 return;
6940}
6941
6942/**
6943 * lpfc_sli4_unset_hba - Unset SLI4 hba device initialization.
6944 * @phba: pointer to lpfc hba data structure.
6945 *
6946 * This routine is invoked to unset the HBA device initialization steps to
6947 * a device with SLI-4 interface spec.
6948 **/
6949static void
6950lpfc_sli4_unset_hba(struct lpfc_hba *phba)
6951{
6952 struct lpfc_vport *vport = phba->pport;
6953 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6954
6955 spin_lock_irq(shost->host_lock);
6956 vport->load_flag |= FC_UNLOADING;
6957 spin_unlock_irq(shost->host_lock);
6958
6959 phba->pport->work_port_events = 0;
6960
6961 lpfc_sli4_hba_down(phba);
6962
6963 lpfc_sli4_disable_intr(phba);
6964
6965 return;
6966}
6967
6968/**
6969 * lpfc_sli4_hba_unset - Unset the fcoe hba
6970 * @phba: Pointer to HBA context object.
6971 *
6972 * This function is called in the SLI4 code path to reset the HBA's FCoE
6973 * function. The caller is not required to hold any lock. This routine
6974 * issues PCI function reset mailbox command to reset the FCoE function.
6975 * At the end of the function, it calls lpfc_hba_down_post function to
6976 * free any pending commands.
6977 **/
6978static void
6979lpfc_sli4_hba_unset(struct lpfc_hba *phba)
6980{
6981 int wait_cnt = 0;
6982 LPFC_MBOXQ_t *mboxq;
6983
6984 lpfc_stop_hba_timers(phba);
6985 phba->sli4_hba.intr_enable = 0;
6986
6987 /*
6988 * Gracefully wait out the potential current outstanding asynchronous
6989 * mailbox command.
6990 */
6991
6992 /* First, block any pending async mailbox command from posted */
6993 spin_lock_irq(&phba->hbalock);
6994 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
6995 spin_unlock_irq(&phba->hbalock);
6996 /* Now, trying to wait it out if we can */
6997 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6998 msleep(10);
6999 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
7000 break;
7001 }
7002 /* Forcefully release the outstanding mailbox command if timed out */
7003 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7004 spin_lock_irq(&phba->hbalock);
7005 mboxq = phba->sli.mbox_active;
7006 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7007 __lpfc_mbox_cmpl_put(phba, mboxq);
7008 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7009 phba->sli.mbox_active = NULL;
7010 spin_unlock_irq(&phba->hbalock);
7011 }
7012
7013 /* Tear down the queues in the HBA */
7014 lpfc_sli4_queue_unset(phba);
7015
7016 /* Disable PCI subsystem interrupt */
7017 lpfc_sli4_disable_intr(phba);
7018
7019 /* Stop kthread signal shall trigger work_done one more time */
7020 kthread_stop(phba->worker_thread);
7021
7022 /* Stop the SLI4 device port */
7023 phba->pport->work_port_events = 0;
7024}
7025
28baac74
JS
7026 /**
7027 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
7028 * @phba: Pointer to HBA context object.
7029 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
7030 *
7031 * This function is called in the SLI4 code path to read the port's
7032 * sli4 capabilities.
7033 *
7034 * This function may be be called from any context that can block-wait
7035 * for the completion. The expectation is that this routine is called
7036 * typically from probe_one or from the online routine.
7037 **/
7038int
7039lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7040{
7041 int rc;
7042 struct lpfc_mqe *mqe;
7043 struct lpfc_pc_sli4_params *sli4_params;
7044 uint32_t mbox_tmo;
7045
7046 rc = 0;
7047 mqe = &mboxq->u.mqe;
7048
7049 /* Read the port's SLI4 Parameters port capabilities */
7050 lpfc_sli4_params(mboxq);
7051 if (!phba->sli4_hba.intr_enable)
7052 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7053 else {
7054 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_PORT_CAPABILITIES);
7055 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
7056 }
7057
7058 if (unlikely(rc))
7059 return 1;
7060
7061 sli4_params = &phba->sli4_hba.pc_sli4_params;
7062 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
7063 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
7064 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
7065 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
7066 &mqe->un.sli4_params);
7067 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
7068 &mqe->un.sli4_params);
7069 sli4_params->proto_types = mqe->un.sli4_params.word3;
7070 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
7071 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
7072 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
7073 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
7074 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
7075 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
7076 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
7077 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
7078 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
7079 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
7080 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
7081 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
7082 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
7083 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
7084 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
7085 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
7086 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
7087 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
7088 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
7089 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
7090 return rc;
7091}
7092
da0436e9
JS
7093/**
7094 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
7095 * @pdev: pointer to PCI device
7096 * @pid: pointer to PCI device identifier
7097 *
7098 * This routine is to be called to attach a device with SLI-3 interface spec
7099 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
7100 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
7101 * information of the device and driver to see if the driver state that it can
7102 * support this kind of device. If the match is successful, the driver core
7103 * invokes this routine. If this routine determines it can claim the HBA, it
7104 * does all the initialization that it needs to do to handle the HBA properly.
7105 *
7106 * Return code
7107 * 0 - driver can claim the device
7108 * negative value - driver can not claim the device
7109 **/
7110static int __devinit
7111lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
7112{
7113 struct lpfc_hba *phba;
7114 struct lpfc_vport *vport = NULL;
6669f9bb 7115 struct Scsi_Host *shost = NULL;
da0436e9
JS
7116 int error;
7117 uint32_t cfg_mode, intr_mode;
7118
7119 /* Allocate memory for HBA structure */
7120 phba = lpfc_hba_alloc(pdev);
7121 if (!phba)
7122 return -ENOMEM;
7123
7124 /* Perform generic PCI device enabling operation */
7125 error = lpfc_enable_pci_dev(phba);
7126 if (error) {
7127 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7128 "1401 Failed to enable pci device.\n");
7129 goto out_free_phba;
7130 }
7131
7132 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
7133 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
7134 if (error)
7135 goto out_disable_pci_dev;
7136
7137 /* Set up SLI-3 specific device PCI memory space */
7138 error = lpfc_sli_pci_mem_setup(phba);
7139 if (error) {
7140 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7141 "1402 Failed to set up pci memory space.\n");
7142 goto out_disable_pci_dev;
7143 }
7144
7145 /* Set up phase-1 common device driver resources */
7146 error = lpfc_setup_driver_resource_phase1(phba);
7147 if (error) {
7148 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7149 "1403 Failed to set up driver resource.\n");
7150 goto out_unset_pci_mem_s3;
7151 }
7152
7153 /* Set up SLI-3 specific device driver resources */
7154 error = lpfc_sli_driver_resource_setup(phba);
7155 if (error) {
7156 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7157 "1404 Failed to set up driver resource.\n");
7158 goto out_unset_pci_mem_s3;
7159 }
7160
7161 /* Initialize and populate the iocb list per host */
7162 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
7163 if (error) {
7164 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7165 "1405 Failed to initialize iocb list.\n");
7166 goto out_unset_driver_resource_s3;
7167 }
7168
7169 /* Set up common device driver resources */
7170 error = lpfc_setup_driver_resource_phase2(phba);
7171 if (error) {
7172 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7173 "1406 Failed to set up driver resource.\n");
7174 goto out_free_iocb_list;
7175 }
7176
7177 /* Create SCSI host to the physical port */
7178 error = lpfc_create_shost(phba);
7179 if (error) {
7180 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7181 "1407 Failed to create scsi host.\n");
7182 goto out_unset_driver_resource;
7183 }
7184
7185 /* Configure sysfs attributes */
7186 vport = phba->pport;
7187 error = lpfc_alloc_sysfs_attr(vport);
7188 if (error) {
7189 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7190 "1476 Failed to allocate sysfs attr\n");
7191 goto out_destroy_shost;
7192 }
7193
6669f9bb 7194 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
da0436e9
JS
7195 /* Now, trying to enable interrupt and bring up the device */
7196 cfg_mode = phba->cfg_use_msi;
7197 while (true) {
7198 /* Put device to a known state before enabling interrupt */
7199 lpfc_stop_port(phba);
7200 /* Configure and enable interrupt */
7201 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
7202 if (intr_mode == LPFC_INTR_ERROR) {
7203 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7204 "0431 Failed to enable interrupt.\n");
7205 error = -ENODEV;
7206 goto out_free_sysfs_attr;
7207 }
7208 /* SLI-3 HBA setup */
7209 if (lpfc_sli_hba_setup(phba)) {
7210 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7211 "1477 Failed to set up hba\n");
7212 error = -ENODEV;
7213 goto out_remove_device;
7214 }
7215
7216 /* Wait 50ms for the interrupts of previous mailbox commands */
7217 msleep(50);
7218 /* Check active interrupts on message signaled interrupts */
7219 if (intr_mode == 0 ||
7220 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
7221 /* Log the current active interrupt mode */
7222 phba->intr_mode = intr_mode;
7223 lpfc_log_intr_mode(phba, intr_mode);
7224 break;
7225 } else {
7226 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7227 "0447 Configure interrupt mode (%d) "
7228 "failed active interrupt test.\n",
7229 intr_mode);
7230 /* Disable the current interrupt mode */
7231 lpfc_sli_disable_intr(phba);
7232 /* Try next level of interrupt mode */
7233 cfg_mode = --intr_mode;
7234 }
7235 }
7236
7237 /* Perform post initialization setup */
7238 lpfc_post_init_setup(phba);
7239
7240 /* Check if there are static vports to be created. */
7241 lpfc_create_static_vport(phba);
7242
7243 return 0;
7244
7245out_remove_device:
7246 lpfc_unset_hba(phba);
7247out_free_sysfs_attr:
7248 lpfc_free_sysfs_attr(vport);
7249out_destroy_shost:
7250 lpfc_destroy_shost(phba);
7251out_unset_driver_resource:
7252 lpfc_unset_driver_resource_phase2(phba);
7253out_free_iocb_list:
7254 lpfc_free_iocb_list(phba);
7255out_unset_driver_resource_s3:
7256 lpfc_sli_driver_resource_unset(phba);
7257out_unset_pci_mem_s3:
7258 lpfc_sli_pci_mem_unset(phba);
7259out_disable_pci_dev:
7260 lpfc_disable_pci_dev(phba);
6669f9bb
JS
7261 if (shost)
7262 scsi_host_put(shost);
da0436e9
JS
7263out_free_phba:
7264 lpfc_hba_free(phba);
7265 return error;
7266}
7267
7268/**
7269 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
7270 * @pdev: pointer to PCI device
7271 *
7272 * This routine is to be called to disattach a device with SLI-3 interface
7273 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
7274 * removed from PCI bus, it performs all the necessary cleanup for the HBA
7275 * device to be removed from the PCI subsystem properly.
7276 **/
7277static void __devexit
7278lpfc_pci_remove_one_s3(struct pci_dev *pdev)
7279{
7280 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7281 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
7282 struct lpfc_vport **vports;
7283 struct lpfc_hba *phba = vport->phba;
7284 int i;
7285 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
7286
7287 spin_lock_irq(&phba->hbalock);
7288 vport->load_flag |= FC_UNLOADING;
7289 spin_unlock_irq(&phba->hbalock);
7290
7291 lpfc_free_sysfs_attr(vport);
7292
7293 /* Release all the vports against this physical port */
7294 vports = lpfc_create_vport_work_array(phba);
7295 if (vports != NULL)
7296 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
7297 fc_vport_terminate(vports[i]->fc_vport);
7298 lpfc_destroy_vport_work_array(phba, vports);
7299
7300 /* Remove FC host and then SCSI host with the physical port */
7301 fc_remove_host(shost);
7302 scsi_remove_host(shost);
7303 lpfc_cleanup(vport);
7304
7305 /*
7306 * Bring down the SLI Layer. This step disable all interrupts,
7307 * clears the rings, discards all mailbox commands, and resets
7308 * the HBA.
7309 */
7310
7311 /* HBA interrupt will be diabled after this call */
7312 lpfc_sli_hba_down(phba);
7313 /* Stop kthread signal shall trigger work_done one more time */
7314 kthread_stop(phba->worker_thread);
7315 /* Final cleanup of txcmplq and reset the HBA */
7316 lpfc_sli_brdrestart(phba);
7317
7318 lpfc_stop_hba_timers(phba);
7319 spin_lock_irq(&phba->hbalock);
7320 list_del_init(&vport->listentry);
7321 spin_unlock_irq(&phba->hbalock);
7322
7323 lpfc_debugfs_terminate(vport);
7324
7325 /* Disable interrupt */
7326 lpfc_sli_disable_intr(phba);
7327
7328 pci_set_drvdata(pdev, NULL);
7329 scsi_host_put(shost);
7330
7331 /*
7332 * Call scsi_free before mem_free since scsi bufs are released to their
7333 * corresponding pools here.
7334 */
7335 lpfc_scsi_free(phba);
7336 lpfc_mem_free_all(phba);
7337
7338 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
7339 phba->hbqslimp.virt, phba->hbqslimp.phys);
7340
7341 /* Free resources associated with SLI2 interface */
7342 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7343 phba->slim2p.virt, phba->slim2p.phys);
7344
7345 /* unmap adapter SLIM and Control Registers */
7346 iounmap(phba->ctrl_regs_memmap_p);
7347 iounmap(phba->slim_memmap_p);
7348
7349 lpfc_hba_free(phba);
7350
7351 pci_release_selected_regions(pdev, bars);
7352 pci_disable_device(pdev);
7353}
7354
7355/**
7356 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
7357 * @pdev: pointer to PCI device
7358 * @msg: power management message
7359 *
7360 * This routine is to be called from the kernel's PCI subsystem to support
7361 * system Power Management (PM) to device with SLI-3 interface spec. When
7362 * PM invokes this method, it quiesces the device by stopping the driver's
7363 * worker thread for the device, turning off device's interrupt and DMA,
7364 * and bring the device offline. Note that as the driver implements the
7365 * minimum PM requirements to a power-aware driver's PM support for the
7366 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
7367 * to the suspend() method call will be treated as SUSPEND and the driver will
7368 * fully reinitialize its device during resume() method call, the driver will
7369 * set device to PCI_D3hot state in PCI config space instead of setting it
7370 * according to the @msg provided by the PM.
7371 *
7372 * Return code
7373 * 0 - driver suspended the device
7374 * Error otherwise
7375 **/
7376static int
7377lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
7378{
7379 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7380 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7381
7382 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7383 "0473 PCI device Power Management suspend.\n");
7384
7385 /* Bring down the device */
7386 lpfc_offline_prep(phba);
7387 lpfc_offline(phba);
7388 kthread_stop(phba->worker_thread);
7389
7390 /* Disable interrupt from device */
7391 lpfc_sli_disable_intr(phba);
7392
7393 /* Save device state to PCI config space */
7394 pci_save_state(pdev);
7395 pci_set_power_state(pdev, PCI_D3hot);
7396
7397 return 0;
7398}
7399
7400/**
7401 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
7402 * @pdev: pointer to PCI device
7403 *
7404 * This routine is to be called from the kernel's PCI subsystem to support
7405 * system Power Management (PM) to device with SLI-3 interface spec. When PM
7406 * invokes this method, it restores the device's PCI config space state and
7407 * fully reinitializes the device and brings it online. Note that as the
7408 * driver implements the minimum PM requirements to a power-aware driver's
7409 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
7410 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
7411 * driver will fully reinitialize its device during resume() method call,
7412 * the device will be set to PCI_D0 directly in PCI config space before
7413 * restoring the state.
7414 *
7415 * Return code
7416 * 0 - driver suspended the device
7417 * Error otherwise
7418 **/
7419static int
7420lpfc_pci_resume_one_s3(struct pci_dev *pdev)
7421{
7422 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7423 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7424 uint32_t intr_mode;
7425 int error;
7426
7427 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7428 "0452 PCI device Power Management resume.\n");
7429
7430 /* Restore device state from PCI config space */
7431 pci_set_power_state(pdev, PCI_D0);
7432 pci_restore_state(pdev);
0d878419 7433
1dfb5a47
JS
7434 /*
7435 * As the new kernel behavior of pci_restore_state() API call clears
7436 * device saved_state flag, need to save the restored state again.
7437 */
7438 pci_save_state(pdev);
7439
da0436e9
JS
7440 if (pdev->is_busmaster)
7441 pci_set_master(pdev);
7442
7443 /* Startup the kernel thread for this host adapter. */
7444 phba->worker_thread = kthread_run(lpfc_do_work, phba,
7445 "lpfc_worker_%d", phba->brd_no);
7446 if (IS_ERR(phba->worker_thread)) {
7447 error = PTR_ERR(phba->worker_thread);
7448 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7449 "0434 PM resume failed to start worker "
7450 "thread: error=x%x.\n", error);
7451 return error;
7452 }
7453
7454 /* Configure and enable interrupt */
7455 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
7456 if (intr_mode == LPFC_INTR_ERROR) {
7457 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7458 "0430 PM resume Failed to enable interrupt\n");
7459 return -EIO;
7460 } else
7461 phba->intr_mode = intr_mode;
7462
7463 /* Restart HBA and bring it online */
7464 lpfc_sli_brdrestart(phba);
7465 lpfc_online(phba);
7466
7467 /* Log the current active interrupt mode */
7468 lpfc_log_intr_mode(phba, phba->intr_mode);
7469
7470 return 0;
7471}
7472
891478a2
JS
7473/**
7474 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
7475 * @phba: pointer to lpfc hba data structure.
7476 *
7477 * This routine is called to prepare the SLI3 device for PCI slot recover. It
7478 * aborts and stops all the on-going I/Os on the pci device.
7479 **/
7480static void
7481lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
7482{
7483 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7484 "2723 PCI channel I/O abort preparing for recovery\n");
7485 /* Prepare for bringing HBA offline */
7486 lpfc_offline_prep(phba);
7487 /* Clear sli active flag to prevent sysfs access to HBA */
7488 spin_lock_irq(&phba->hbalock);
7489 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
7490 spin_unlock_irq(&phba->hbalock);
7491 /* Stop and flush all I/Os and bring HBA offline */
7492 lpfc_offline(phba);
7493}
7494
0d878419
JS
7495/**
7496 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
7497 * @phba: pointer to lpfc hba data structure.
7498 *
7499 * This routine is called to prepare the SLI3 device for PCI slot reset. It
7500 * disables the device interrupt and pci device, and aborts the internal FCP
7501 * pending I/Os.
7502 **/
7503static void
7504lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
7505{
7506 struct lpfc_sli *psli = &phba->sli;
7507 struct lpfc_sli_ring *pring;
7508
7509 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
891478a2 7510 "2710 PCI channel disable preparing for reset\n");
0d878419
JS
7511 /* Disable interrupt and pci device */
7512 lpfc_sli_disable_intr(phba);
7513 pci_disable_device(phba->pcidev);
7514 /*
7515 * There may be I/Os dropped by the firmware.
7516 * Error iocb (I/O) on txcmplq and let the SCSI layer
7517 * retry it after re-establishing link.
7518 */
7519 pring = &psli->ring[psli->fcp_ring];
7520 lpfc_sli_abort_iocb_ring(phba, pring);
7521}
7522
7523/**
7524 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
7525 * @phba: pointer to lpfc hba data structure.
7526 *
7527 * This routine is called to prepare the SLI3 device for PCI slot permanently
7528 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
7529 * pending I/Os.
7530 **/
7531static void
7532lpfc_prep_dev_for_perm_failure(struct lpfc_hba *phba)
7533{
7534 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
891478a2 7535 "2711 PCI channel permanent disable for failure\n");
0d878419
JS
7536 /* Clean up all driver's outstanding SCSI I/Os */
7537 lpfc_sli_flush_fcp_rings(phba);
7538}
7539
da0436e9
JS
7540/**
7541 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
7542 * @pdev: pointer to PCI device.
7543 * @state: the current PCI connection state.
7544 *
7545 * This routine is called from the PCI subsystem for I/O error handling to
7546 * device with SLI-3 interface spec. This function is called by the PCI
7547 * subsystem after a PCI bus error affecting this device has been detected.
7548 * When this function is invoked, it will need to stop all the I/Os and
7549 * interrupt(s) to the device. Once that is done, it will return
7550 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
7551 * as desired.
7552 *
7553 * Return codes
0d878419 7554 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
da0436e9
JS
7555 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
7556 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
7557 **/
7558static pci_ers_result_t
7559lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
7560{
7561 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7562 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
da0436e9 7563
aacc20e3
JS
7564 /* Block all SCSI devices' I/Os on the host */
7565 lpfc_scsi_dev_block(phba);
7566
0d878419
JS
7567 switch (state) {
7568 case pci_channel_io_normal:
891478a2
JS
7569 /* Non-fatal error, prepare for recovery */
7570 lpfc_sli_prep_dev_for_recover(phba);
0d878419
JS
7571 return PCI_ERS_RESULT_CAN_RECOVER;
7572 case pci_channel_io_frozen:
7573 /* Fatal error, prepare for slot reset */
7574 lpfc_sli_prep_dev_for_reset(phba);
7575 return PCI_ERS_RESULT_NEED_RESET;
7576 case pci_channel_io_perm_failure:
7577 /* Permanent failure, prepare for device down */
7578 lpfc_prep_dev_for_perm_failure(phba);
da0436e9 7579 return PCI_ERS_RESULT_DISCONNECT;
0d878419
JS
7580 default:
7581 /* Unknown state, prepare and request slot reset */
7582 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7583 "0472 Unknown PCI error state: x%x\n", state);
7584 lpfc_sli_prep_dev_for_reset(phba);
7585 return PCI_ERS_RESULT_NEED_RESET;
da0436e9 7586 }
da0436e9
JS
7587}
7588
7589/**
7590 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
7591 * @pdev: pointer to PCI device.
7592 *
7593 * This routine is called from the PCI subsystem for error handling to
7594 * device with SLI-3 interface spec. This is called after PCI bus has been
7595 * reset to restart the PCI card from scratch, as if from a cold-boot.
7596 * During the PCI subsystem error recovery, after driver returns
7597 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
7598 * recovery and then call this routine before calling the .resume method
7599 * to recover the device. This function will initialize the HBA device,
7600 * enable the interrupt, but it will just put the HBA to offline state
7601 * without passing any I/O traffic.
7602 *
7603 * Return codes
7604 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
7605 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
7606 */
7607static pci_ers_result_t
7608lpfc_io_slot_reset_s3(struct pci_dev *pdev)
7609{
7610 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7611 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7612 struct lpfc_sli *psli = &phba->sli;
7613 uint32_t intr_mode;
7614
7615 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
7616 if (pci_enable_device_mem(pdev)) {
7617 printk(KERN_ERR "lpfc: Cannot re-enable "
7618 "PCI device after reset.\n");
7619 return PCI_ERS_RESULT_DISCONNECT;
7620 }
7621
7622 pci_restore_state(pdev);
1dfb5a47
JS
7623
7624 /*
7625 * As the new kernel behavior of pci_restore_state() API call clears
7626 * device saved_state flag, need to save the restored state again.
7627 */
7628 pci_save_state(pdev);
7629
da0436e9
JS
7630 if (pdev->is_busmaster)
7631 pci_set_master(pdev);
7632
7633 spin_lock_irq(&phba->hbalock);
7634 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
7635 spin_unlock_irq(&phba->hbalock);
7636
7637 /* Configure and enable interrupt */
7638 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
7639 if (intr_mode == LPFC_INTR_ERROR) {
7640 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7641 "0427 Cannot re-enable interrupt after "
7642 "slot reset.\n");
7643 return PCI_ERS_RESULT_DISCONNECT;
7644 } else
7645 phba->intr_mode = intr_mode;
7646
7647 /* Take device offline; this will perform cleanup */
7648 lpfc_offline(phba);
7649 lpfc_sli_brdrestart(phba);
7650
7651 /* Log the current active interrupt mode */
7652 lpfc_log_intr_mode(phba, phba->intr_mode);
7653
7654 return PCI_ERS_RESULT_RECOVERED;
7655}
7656
7657/**
7658 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
7659 * @pdev: pointer to PCI device
7660 *
7661 * This routine is called from the PCI subsystem for error handling to device
7662 * with SLI-3 interface spec. It is called when kernel error recovery tells
7663 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
7664 * error recovery. After this call, traffic can start to flow from this device
7665 * again.
7666 */
7667static void
7668lpfc_io_resume_s3(struct pci_dev *pdev)
7669{
7670 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7671 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
3772a991 7672
0d878419 7673 /* Bring the device online */
da0436e9 7674 lpfc_online(phba);
0d878419
JS
7675
7676 /* Clean up Advanced Error Reporting (AER) if needed */
7677 if (phba->hba_flag & HBA_AER_ENABLED)
7678 pci_cleanup_aer_uncorrect_error_status(pdev);
da0436e9 7679}
3772a991 7680
da0436e9
JS
7681/**
7682 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
7683 * @phba: pointer to lpfc hba data structure.
7684 *
7685 * returns the number of ELS/CT IOCBs to reserve
7686 **/
7687int
7688lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
7689{
7690 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
7691
f1126688
JS
7692 if (phba->sli_rev == LPFC_SLI_REV4) {
7693 if (max_xri <= 100)
6a9c52cf 7694 return 10;
f1126688 7695 else if (max_xri <= 256)
6a9c52cf 7696 return 25;
f1126688 7697 else if (max_xri <= 512)
6a9c52cf 7698 return 50;
f1126688 7699 else if (max_xri <= 1024)
6a9c52cf 7700 return 100;
f1126688 7701 else
6a9c52cf 7702 return 150;
f1126688
JS
7703 } else
7704 return 0;
3772a991
JS
7705}
7706
7707/**
da0436e9 7708 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
3772a991
JS
7709 * @pdev: pointer to PCI device
7710 * @pid: pointer to PCI device identifier
7711 *
da0436e9
JS
7712 * This routine is called from the kernel's PCI subsystem to device with
7713 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
3772a991 7714 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
da0436e9
JS
7715 * information of the device and driver to see if the driver state that it
7716 * can support this kind of device. If the match is successful, the driver
7717 * core invokes this routine. If this routine determines it can claim the HBA,
7718 * it does all the initialization that it needs to do to handle the HBA
7719 * properly.
3772a991
JS
7720 *
7721 * Return code
7722 * 0 - driver can claim the device
7723 * negative value - driver can not claim the device
7724 **/
7725static int __devinit
da0436e9 7726lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
3772a991
JS
7727{
7728 struct lpfc_hba *phba;
7729 struct lpfc_vport *vport = NULL;
6669f9bb 7730 struct Scsi_Host *shost = NULL;
3772a991
JS
7731 int error;
7732 uint32_t cfg_mode, intr_mode;
da0436e9 7733 int mcnt;
3772a991
JS
7734
7735 /* Allocate memory for HBA structure */
7736 phba = lpfc_hba_alloc(pdev);
7737 if (!phba)
7738 return -ENOMEM;
7739
7740 /* Perform generic PCI device enabling operation */
7741 error = lpfc_enable_pci_dev(phba);
7742 if (error) {
7743 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9 7744 "1409 Failed to enable pci device.\n");
3772a991
JS
7745 goto out_free_phba;
7746 }
7747
da0436e9
JS
7748 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
7749 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
3772a991
JS
7750 if (error)
7751 goto out_disable_pci_dev;
7752
da0436e9
JS
7753 /* Set up SLI-4 specific device PCI memory space */
7754 error = lpfc_sli4_pci_mem_setup(phba);
3772a991
JS
7755 if (error) {
7756 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9 7757 "1410 Failed to set up pci memory space.\n");
3772a991
JS
7758 goto out_disable_pci_dev;
7759 }
7760
7761 /* Set up phase-1 common device driver resources */
7762 error = lpfc_setup_driver_resource_phase1(phba);
7763 if (error) {
7764 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9
JS
7765 "1411 Failed to set up driver resource.\n");
7766 goto out_unset_pci_mem_s4;
3772a991
JS
7767 }
7768
da0436e9
JS
7769 /* Set up SLI-4 Specific device driver resources */
7770 error = lpfc_sli4_driver_resource_setup(phba);
3772a991
JS
7771 if (error) {
7772 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9
JS
7773 "1412 Failed to set up driver resource.\n");
7774 goto out_unset_pci_mem_s4;
3772a991
JS
7775 }
7776
7777 /* Initialize and populate the iocb list per host */
da0436e9
JS
7778 error = lpfc_init_iocb_list(phba,
7779 phba->sli4_hba.max_cfg_param.max_xri);
3772a991
JS
7780 if (error) {
7781 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9
JS
7782 "1413 Failed to initialize iocb list.\n");
7783 goto out_unset_driver_resource_s4;
3772a991
JS
7784 }
7785
7786 /* Set up common device driver resources */
7787 error = lpfc_setup_driver_resource_phase2(phba);
7788 if (error) {
7789 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9 7790 "1414 Failed to set up driver resource.\n");
3772a991
JS
7791 goto out_free_iocb_list;
7792 }
7793
7794 /* Create SCSI host to the physical port */
7795 error = lpfc_create_shost(phba);
7796 if (error) {
7797 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9 7798 "1415 Failed to create scsi host.\n");
3772a991
JS
7799 goto out_unset_driver_resource;
7800 }
9399627f 7801
5b75da2f 7802 /* Configure sysfs attributes */
3772a991
JS
7803 vport = phba->pport;
7804 error = lpfc_alloc_sysfs_attr(vport);
7805 if (error) {
9399627f 7806 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9 7807 "1416 Failed to allocate sysfs attr\n");
3772a991 7808 goto out_destroy_shost;
98c9ea5c 7809 }
875fbdfe 7810
6669f9bb 7811 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
3772a991 7812 /* Now, trying to enable interrupt and bring up the device */
5b75da2f
JS
7813 cfg_mode = phba->cfg_use_msi;
7814 while (true) {
3772a991
JS
7815 /* Put device to a known state before enabling interrupt */
7816 lpfc_stop_port(phba);
5b75da2f 7817 /* Configure and enable interrupt */
da0436e9 7818 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
5b75da2f
JS
7819 if (intr_mode == LPFC_INTR_ERROR) {
7820 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9 7821 "0426 Failed to enable interrupt.\n");
3772a991 7822 error = -ENODEV;
5b75da2f
JS
7823 goto out_free_sysfs_attr;
7824 }
def9c7a9
JS
7825 /* Default to single FCP EQ for non-MSI-X */
7826 if (phba->intr_type != MSIX)
7827 phba->cfg_fcp_eq_count = 1;
da0436e9
JS
7828 /* Set up SLI-4 HBA */
7829 if (lpfc_sli4_hba_setup(phba)) {
5b75da2f 7830 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9 7831 "1421 Failed to set up hba\n");
5b75da2f 7832 error = -ENODEV;
da0436e9 7833 goto out_disable_intr;
5b75da2f
JS
7834 }
7835
da0436e9
JS
7836 /* Send NOP mbx cmds for non-INTx mode active interrupt test */
7837 if (intr_mode != 0)
7838 mcnt = lpfc_sli4_send_nop_mbox_cmds(phba,
7839 LPFC_ACT_INTR_CNT);
7840
7841 /* Check active interrupts received only for MSI/MSI-X */
3772a991 7842 if (intr_mode == 0 ||
da0436e9 7843 phba->sli.slistat.sli_intr >= LPFC_ACT_INTR_CNT) {
5b75da2f
JS
7844 /* Log the current active interrupt mode */
7845 phba->intr_mode = intr_mode;
7846 lpfc_log_intr_mode(phba, intr_mode);
7847 break;
5b75da2f 7848 }
da0436e9
JS
7849 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7850 "0451 Configure interrupt mode (%d) "
7851 "failed active interrupt test.\n",
7852 intr_mode);
7853 /* Unset the preivous SLI-4 HBA setup */
7854 lpfc_sli4_unset_hba(phba);
7855 /* Try next level of interrupt mode */
7856 cfg_mode = --intr_mode;
98c9ea5c 7857 }
858c9f6c 7858
3772a991
JS
7859 /* Perform post initialization setup */
7860 lpfc_post_init_setup(phba);
dea3101e 7861
1c6834a7
JS
7862 /* Check if there are static vports to be created. */
7863 lpfc_create_static_vport(phba);
7864
dea3101e 7865 return 0;
7866
da0436e9
JS
7867out_disable_intr:
7868 lpfc_sli4_disable_intr(phba);
5b75da2f
JS
7869out_free_sysfs_attr:
7870 lpfc_free_sysfs_attr(vport);
3772a991
JS
7871out_destroy_shost:
7872 lpfc_destroy_shost(phba);
7873out_unset_driver_resource:
7874 lpfc_unset_driver_resource_phase2(phba);
7875out_free_iocb_list:
7876 lpfc_free_iocb_list(phba);
da0436e9
JS
7877out_unset_driver_resource_s4:
7878 lpfc_sli4_driver_resource_unset(phba);
7879out_unset_pci_mem_s4:
7880 lpfc_sli4_pci_mem_unset(phba);
3772a991
JS
7881out_disable_pci_dev:
7882 lpfc_disable_pci_dev(phba);
6669f9bb
JS
7883 if (shost)
7884 scsi_host_put(shost);
2e0fef85 7885out_free_phba:
3772a991 7886 lpfc_hba_free(phba);
dea3101e 7887 return error;
7888}
7889
e59058c4 7890/**
da0436e9 7891 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
e59058c4
JS
7892 * @pdev: pointer to PCI device
7893 *
da0436e9
JS
7894 * This routine is called from the kernel's PCI subsystem to device with
7895 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
3772a991
JS
7896 * removed from PCI bus, it performs all the necessary cleanup for the HBA
7897 * device to be removed from the PCI subsystem properly.
e59058c4 7898 **/
dea3101e 7899static void __devexit
da0436e9 7900lpfc_pci_remove_one_s4(struct pci_dev *pdev)
dea3101e 7901{
da0436e9 7902 struct Scsi_Host *shost = pci_get_drvdata(pdev);
2e0fef85 7903 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
eada272d 7904 struct lpfc_vport **vports;
da0436e9 7905 struct lpfc_hba *phba = vport->phba;
eada272d 7906 int i;
8a4df120 7907
da0436e9 7908 /* Mark the device unloading flag */
549e55cd 7909 spin_lock_irq(&phba->hbalock);
51ef4c26 7910 vport->load_flag |= FC_UNLOADING;
549e55cd 7911 spin_unlock_irq(&phba->hbalock);
2e0fef85 7912
da0436e9 7913 /* Free the HBA sysfs attributes */
858c9f6c
JS
7914 lpfc_free_sysfs_attr(vport);
7915
eada272d
JS
7916 /* Release all the vports against this physical port */
7917 vports = lpfc_create_vport_work_array(phba);
7918 if (vports != NULL)
3772a991 7919 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
eada272d
JS
7920 fc_vport_terminate(vports[i]->fc_vport);
7921 lpfc_destroy_vport_work_array(phba, vports);
7922
7923 /* Remove FC host and then SCSI host with the physical port */
858c9f6c
JS
7924 fc_remove_host(shost);
7925 scsi_remove_host(shost);
da0436e9
JS
7926
7927 /* Perform cleanup on the physical port */
87af33fe
JS
7928 lpfc_cleanup(vport);
7929
2e0fef85 7930 /*
da0436e9 7931 * Bring down the SLI Layer. This step disables all interrupts,
2e0fef85 7932 * clears the rings, discards all mailbox commands, and resets
da0436e9 7933 * the HBA FCoE function.
2e0fef85 7934 */
da0436e9
JS
7935 lpfc_debugfs_terminate(vport);
7936 lpfc_sli4_hba_unset(phba);
a257bf90 7937
858c9f6c
JS
7938 spin_lock_irq(&phba->hbalock);
7939 list_del_init(&vport->listentry);
7940 spin_unlock_irq(&phba->hbalock);
7941
da0436e9
JS
7942 /* Call scsi_free before lpfc_sli4_driver_resource_unset since scsi
7943 * buffers are released to their corresponding pools here.
2e0fef85
JS
7944 */
7945 lpfc_scsi_free(phba);
da0436e9 7946 lpfc_sli4_driver_resource_unset(phba);
ed957684 7947
da0436e9
JS
7948 /* Unmap adapter Control and Doorbell registers */
7949 lpfc_sli4_pci_mem_unset(phba);
2e0fef85 7950
da0436e9
JS
7951 /* Release PCI resources and disable device's PCI function */
7952 scsi_host_put(shost);
7953 lpfc_disable_pci_dev(phba);
2e0fef85 7954
da0436e9 7955 /* Finally, free the driver's device data structure */
3772a991 7956 lpfc_hba_free(phba);
2e0fef85 7957
da0436e9 7958 return;
dea3101e 7959}
7960
3a55b532 7961/**
da0436e9 7962 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
3a55b532
JS
7963 * @pdev: pointer to PCI device
7964 * @msg: power management message
7965 *
da0436e9
JS
7966 * This routine is called from the kernel's PCI subsystem to support system
7967 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
7968 * this method, it quiesces the device by stopping the driver's worker
7969 * thread for the device, turning off device's interrupt and DMA, and bring
7970 * the device offline. Note that as the driver implements the minimum PM
7971 * requirements to a power-aware driver's PM support for suspend/resume -- all
7972 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
7973 * method call will be treated as SUSPEND and the driver will fully
7974 * reinitialize its device during resume() method call, the driver will set
7975 * device to PCI_D3hot state in PCI config space instead of setting it
3772a991 7976 * according to the @msg provided by the PM.
3a55b532
JS
7977 *
7978 * Return code
3772a991
JS
7979 * 0 - driver suspended the device
7980 * Error otherwise
3a55b532
JS
7981 **/
7982static int
da0436e9 7983lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
3a55b532
JS
7984{
7985 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7986 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7987
7988 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
da0436e9 7989 "0298 PCI device Power Management suspend.\n");
3a55b532
JS
7990
7991 /* Bring down the device */
7992 lpfc_offline_prep(phba);
7993 lpfc_offline(phba);
7994 kthread_stop(phba->worker_thread);
7995
7996 /* Disable interrupt from device */
da0436e9 7997 lpfc_sli4_disable_intr(phba);
3a55b532
JS
7998
7999 /* Save device state to PCI config space */
8000 pci_save_state(pdev);
8001 pci_set_power_state(pdev, PCI_D3hot);
8002
8003 return 0;
8004}
8005
8006/**
da0436e9 8007 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
3a55b532
JS
8008 * @pdev: pointer to PCI device
8009 *
da0436e9
JS
8010 * This routine is called from the kernel's PCI subsystem to support system
8011 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
8012 * this method, it restores the device's PCI config space state and fully
8013 * reinitializes the device and brings it online. Note that as the driver
8014 * implements the minimum PM requirements to a power-aware driver's PM for
8015 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
8016 * to the suspend() method call will be treated as SUSPEND and the driver
8017 * will fully reinitialize its device during resume() method call, the device
8018 * will be set to PCI_D0 directly in PCI config space before restoring the
8019 * state.
3a55b532
JS
8020 *
8021 * Return code
3772a991
JS
8022 * 0 - driver suspended the device
8023 * Error otherwise
3a55b532
JS
8024 **/
8025static int
da0436e9 8026lpfc_pci_resume_one_s4(struct pci_dev *pdev)
3a55b532
JS
8027{
8028 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8029 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
5b75da2f 8030 uint32_t intr_mode;
3a55b532
JS
8031 int error;
8032
8033 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
da0436e9 8034 "0292 PCI device Power Management resume.\n");
3a55b532
JS
8035
8036 /* Restore device state from PCI config space */
8037 pci_set_power_state(pdev, PCI_D0);
8038 pci_restore_state(pdev);
1dfb5a47
JS
8039
8040 /*
8041 * As the new kernel behavior of pci_restore_state() API call clears
8042 * device saved_state flag, need to save the restored state again.
8043 */
8044 pci_save_state(pdev);
8045
3a55b532
JS
8046 if (pdev->is_busmaster)
8047 pci_set_master(pdev);
8048
da0436e9 8049 /* Startup the kernel thread for this host adapter. */
3a55b532
JS
8050 phba->worker_thread = kthread_run(lpfc_do_work, phba,
8051 "lpfc_worker_%d", phba->brd_no);
8052 if (IS_ERR(phba->worker_thread)) {
8053 error = PTR_ERR(phba->worker_thread);
8054 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9 8055 "0293 PM resume failed to start worker "
3a55b532
JS
8056 "thread: error=x%x.\n", error);
8057 return error;
8058 }
8059
5b75da2f 8060 /* Configure and enable interrupt */
da0436e9 8061 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
5b75da2f 8062 if (intr_mode == LPFC_INTR_ERROR) {
3a55b532 8063 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
da0436e9 8064 "0294 PM resume Failed to enable interrupt\n");
5b75da2f
JS
8065 return -EIO;
8066 } else
8067 phba->intr_mode = intr_mode;
3a55b532
JS
8068
8069 /* Restart HBA and bring it online */
8070 lpfc_sli_brdrestart(phba);
8071 lpfc_online(phba);
8072
5b75da2f
JS
8073 /* Log the current active interrupt mode */
8074 lpfc_log_intr_mode(phba, phba->intr_mode);
8075
3a55b532
JS
8076 return 0;
8077}
8078
8d63f375 8079/**
da0436e9 8080 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
e59058c4
JS
8081 * @pdev: pointer to PCI device.
8082 * @state: the current PCI connection state.
8d63f375 8083 *
da0436e9
JS
8084 * This routine is called from the PCI subsystem for error handling to device
8085 * with SLI-4 interface spec. This function is called by the PCI subsystem
8086 * after a PCI bus error affecting this device has been detected. When this
8087 * function is invoked, it will need to stop all the I/Os and interrupt(s)
8088 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
8089 * for the PCI subsystem to perform proper recovery as desired.
e59058c4
JS
8090 *
8091 * Return codes
3772a991
JS
8092 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
8093 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
e59058c4 8094 **/
3772a991 8095static pci_ers_result_t
da0436e9 8096lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
8d63f375 8097{
8d63f375
LV
8098 return PCI_ERS_RESULT_NEED_RESET;
8099}
8100
8101/**
da0436e9 8102 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
e59058c4
JS
8103 * @pdev: pointer to PCI device.
8104 *
da0436e9
JS
8105 * This routine is called from the PCI subsystem for error handling to device
8106 * with SLI-4 interface spec. It is called after PCI bus has been reset to
8107 * restart the PCI card from scratch, as if from a cold-boot. During the
8108 * PCI subsystem error recovery, after the driver returns
3772a991 8109 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
da0436e9
JS
8110 * recovery and then call this routine before calling the .resume method to
8111 * recover the device. This function will initialize the HBA device, enable
8112 * the interrupt, but it will just put the HBA to offline state without
8113 * passing any I/O traffic.
8d63f375 8114 *
e59058c4 8115 * Return codes
3772a991
JS
8116 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
8117 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8d63f375 8118 */
3772a991 8119static pci_ers_result_t
da0436e9 8120lpfc_io_slot_reset_s4(struct pci_dev *pdev)
8d63f375 8121{
8d63f375
LV
8122 return PCI_ERS_RESULT_RECOVERED;
8123}
8124
8125/**
da0436e9 8126 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
e59058c4 8127 * @pdev: pointer to PCI device
8d63f375 8128 *
3772a991 8129 * This routine is called from the PCI subsystem for error handling to device
da0436e9 8130 * with SLI-4 interface spec. It is called when kernel error recovery tells
3772a991
JS
8131 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
8132 * error recovery. After this call, traffic can start to flow from this device
8133 * again.
da0436e9 8134 **/
3772a991 8135static void
da0436e9 8136lpfc_io_resume_s4(struct pci_dev *pdev)
8d63f375 8137{
da0436e9 8138 return;
8d63f375
LV
8139}
8140
3772a991
JS
8141/**
8142 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
8143 * @pdev: pointer to PCI device
8144 * @pid: pointer to PCI device identifier
8145 *
8146 * This routine is to be registered to the kernel's PCI subsystem. When an
8147 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
8148 * at PCI device-specific information of the device and driver to see if the
8149 * driver state that it can support this kind of device. If the match is
8150 * successful, the driver core invokes this routine. This routine dispatches
8151 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
8152 * do all the initialization that it needs to do to handle the HBA device
8153 * properly.
8154 *
8155 * Return code
8156 * 0 - driver can claim the device
8157 * negative value - driver can not claim the device
8158 **/
8159static int __devinit
8160lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
8161{
8162 int rc;
8fa38513 8163 struct lpfc_sli_intf intf;
3772a991 8164
28baac74 8165 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
3772a991
JS
8166 return -ENODEV;
8167
8fa38513 8168 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
28baac74 8169 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
da0436e9 8170 rc = lpfc_pci_probe_one_s4(pdev, pid);
8fa38513 8171 else
3772a991 8172 rc = lpfc_pci_probe_one_s3(pdev, pid);
8fa38513 8173
3772a991
JS
8174 return rc;
8175}
8176
8177/**
8178 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
8179 * @pdev: pointer to PCI device
8180 *
8181 * This routine is to be registered to the kernel's PCI subsystem. When an
8182 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
8183 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
8184 * remove routine, which will perform all the necessary cleanup for the
8185 * device to be removed from the PCI subsystem properly.
8186 **/
8187static void __devexit
8188lpfc_pci_remove_one(struct pci_dev *pdev)
8189{
8190 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8191 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8192
8193 switch (phba->pci_dev_grp) {
8194 case LPFC_PCI_DEV_LP:
8195 lpfc_pci_remove_one_s3(pdev);
8196 break;
da0436e9
JS
8197 case LPFC_PCI_DEV_OC:
8198 lpfc_pci_remove_one_s4(pdev);
8199 break;
3772a991
JS
8200 default:
8201 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8202 "1424 Invalid PCI device group: 0x%x\n",
8203 phba->pci_dev_grp);
8204 break;
8205 }
8206 return;
8207}
8208
8209/**
8210 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
8211 * @pdev: pointer to PCI device
8212 * @msg: power management message
8213 *
8214 * This routine is to be registered to the kernel's PCI subsystem to support
8215 * system Power Management (PM). When PM invokes this method, it dispatches
8216 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
8217 * suspend the device.
8218 *
8219 * Return code
8220 * 0 - driver suspended the device
8221 * Error otherwise
8222 **/
8223static int
8224lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
8225{
8226 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8227 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8228 int rc = -ENODEV;
8229
8230 switch (phba->pci_dev_grp) {
8231 case LPFC_PCI_DEV_LP:
8232 rc = lpfc_pci_suspend_one_s3(pdev, msg);
8233 break;
da0436e9
JS
8234 case LPFC_PCI_DEV_OC:
8235 rc = lpfc_pci_suspend_one_s4(pdev, msg);
8236 break;
3772a991
JS
8237 default:
8238 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8239 "1425 Invalid PCI device group: 0x%x\n",
8240 phba->pci_dev_grp);
8241 break;
8242 }
8243 return rc;
8244}
8245
8246/**
8247 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
8248 * @pdev: pointer to PCI device
8249 *
8250 * This routine is to be registered to the kernel's PCI subsystem to support
8251 * system Power Management (PM). When PM invokes this method, it dispatches
8252 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
8253 * resume the device.
8254 *
8255 * Return code
8256 * 0 - driver suspended the device
8257 * Error otherwise
8258 **/
8259static int
8260lpfc_pci_resume_one(struct pci_dev *pdev)
8261{
8262 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8263 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8264 int rc = -ENODEV;
8265
8266 switch (phba->pci_dev_grp) {
8267 case LPFC_PCI_DEV_LP:
8268 rc = lpfc_pci_resume_one_s3(pdev);
8269 break;
da0436e9
JS
8270 case LPFC_PCI_DEV_OC:
8271 rc = lpfc_pci_resume_one_s4(pdev);
8272 break;
3772a991
JS
8273 default:
8274 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8275 "1426 Invalid PCI device group: 0x%x\n",
8276 phba->pci_dev_grp);
8277 break;
8278 }
8279 return rc;
8280}
8281
8282/**
8283 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
8284 * @pdev: pointer to PCI device.
8285 * @state: the current PCI connection state.
8286 *
8287 * This routine is registered to the PCI subsystem for error handling. This
8288 * function is called by the PCI subsystem after a PCI bus error affecting
8289 * this device has been detected. When this routine is invoked, it dispatches
8290 * the action to the proper SLI-3 or SLI-4 device error detected handling
8291 * routine, which will perform the proper error detected operation.
8292 *
8293 * Return codes
8294 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
8295 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8296 **/
8297static pci_ers_result_t
8298lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
8299{
8300 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8301 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8302 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
8303
8304 switch (phba->pci_dev_grp) {
8305 case LPFC_PCI_DEV_LP:
8306 rc = lpfc_io_error_detected_s3(pdev, state);
8307 break;
da0436e9
JS
8308 case LPFC_PCI_DEV_OC:
8309 rc = lpfc_io_error_detected_s4(pdev, state);
8310 break;
3772a991
JS
8311 default:
8312 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8313 "1427 Invalid PCI device group: 0x%x\n",
8314 phba->pci_dev_grp);
8315 break;
8316 }
8317 return rc;
8318}
8319
8320/**
8321 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
8322 * @pdev: pointer to PCI device.
8323 *
8324 * This routine is registered to the PCI subsystem for error handling. This
8325 * function is called after PCI bus has been reset to restart the PCI card
8326 * from scratch, as if from a cold-boot. When this routine is invoked, it
8327 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
8328 * routine, which will perform the proper device reset.
8329 *
8330 * Return codes
8331 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
8332 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8333 **/
8334static pci_ers_result_t
8335lpfc_io_slot_reset(struct pci_dev *pdev)
8336{
8337 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8338 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8339 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
8340
8341 switch (phba->pci_dev_grp) {
8342 case LPFC_PCI_DEV_LP:
8343 rc = lpfc_io_slot_reset_s3(pdev);
8344 break;
da0436e9
JS
8345 case LPFC_PCI_DEV_OC:
8346 rc = lpfc_io_slot_reset_s4(pdev);
8347 break;
3772a991
JS
8348 default:
8349 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8350 "1428 Invalid PCI device group: 0x%x\n",
8351 phba->pci_dev_grp);
8352 break;
8353 }
8354 return rc;
8355}
8356
8357/**
8358 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
8359 * @pdev: pointer to PCI device
8360 *
8361 * This routine is registered to the PCI subsystem for error handling. It
8362 * is called when kernel error recovery tells the lpfc driver that it is
8363 * OK to resume normal PCI operation after PCI bus error recovery. When
8364 * this routine is invoked, it dispatches the action to the proper SLI-3
8365 * or SLI-4 device io_resume routine, which will resume the device operation.
8366 **/
8367static void
8368lpfc_io_resume(struct pci_dev *pdev)
8369{
8370 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8371 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8372
8373 switch (phba->pci_dev_grp) {
8374 case LPFC_PCI_DEV_LP:
8375 lpfc_io_resume_s3(pdev);
8376 break;
da0436e9
JS
8377 case LPFC_PCI_DEV_OC:
8378 lpfc_io_resume_s4(pdev);
8379 break;
3772a991
JS
8380 default:
8381 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8382 "1429 Invalid PCI device group: 0x%x\n",
8383 phba->pci_dev_grp);
8384 break;
8385 }
8386 return;
8387}
8388
dea3101e 8389static struct pci_device_id lpfc_id_table[] = {
8390 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
8391 PCI_ANY_ID, PCI_ANY_ID, },
06325e74
JSEC
8392 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
8393 PCI_ANY_ID, PCI_ANY_ID, },
dea3101e 8394 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
8395 PCI_ANY_ID, PCI_ANY_ID, },
8396 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
8397 PCI_ANY_ID, PCI_ANY_ID, },
8398 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
8399 PCI_ANY_ID, PCI_ANY_ID, },
8400 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
8401 PCI_ANY_ID, PCI_ANY_ID, },
8402 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
8403 PCI_ANY_ID, PCI_ANY_ID, },
8404 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
8405 PCI_ANY_ID, PCI_ANY_ID, },
8406 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
8407 PCI_ANY_ID, PCI_ANY_ID, },
e4adb204
JSEC
8408 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
8409 PCI_ANY_ID, PCI_ANY_ID, },
8410 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
8411 PCI_ANY_ID, PCI_ANY_ID, },
8412 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
8413 PCI_ANY_ID, PCI_ANY_ID, },
dea3101e 8414 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
8415 PCI_ANY_ID, PCI_ANY_ID, },
e4adb204
JSEC
8416 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
8417 PCI_ANY_ID, PCI_ANY_ID, },
8418 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
8419 PCI_ANY_ID, PCI_ANY_ID, },
dea3101e 8420 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
8421 PCI_ANY_ID, PCI_ANY_ID, },
8422 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
8423 PCI_ANY_ID, PCI_ANY_ID, },
8424 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
8425 PCI_ANY_ID, PCI_ANY_ID, },
84774a4d
JS
8426 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
8427 PCI_ANY_ID, PCI_ANY_ID, },
e4adb204
JSEC
8428 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
8429 PCI_ANY_ID, PCI_ANY_ID, },
8430 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
8431 PCI_ANY_ID, PCI_ANY_ID, },
dea3101e 8432 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
8433 PCI_ANY_ID, PCI_ANY_ID, },
8434 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
8435 PCI_ANY_ID, PCI_ANY_ID, },
8436 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
8437 PCI_ANY_ID, PCI_ANY_ID, },
8438 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
8439 PCI_ANY_ID, PCI_ANY_ID, },
8440 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
8441 PCI_ANY_ID, PCI_ANY_ID, },
e4adb204
JSEC
8442 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
8443 PCI_ANY_ID, PCI_ANY_ID, },
8444 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
8445 PCI_ANY_ID, PCI_ANY_ID, },
b87eab38
JS
8446 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
8447 PCI_ANY_ID, PCI_ANY_ID, },
8448 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
8449 PCI_ANY_ID, PCI_ANY_ID, },
8450 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
8451 PCI_ANY_ID, PCI_ANY_ID, },
8452 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
8453 PCI_ANY_ID, PCI_ANY_ID, },
8454 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
8455 PCI_ANY_ID, PCI_ANY_ID, },
8456 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
8457 PCI_ANY_ID, PCI_ANY_ID, },
84774a4d
JS
8458 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
8459 PCI_ANY_ID, PCI_ANY_ID, },
8460 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
8461 PCI_ANY_ID, PCI_ANY_ID, },
8462 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
8463 PCI_ANY_ID, PCI_ANY_ID, },
3772a991
JS
8464 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
8465 PCI_ANY_ID, PCI_ANY_ID, },
a747c9ce
JS
8466 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TOMCAT,
8467 PCI_ANY_ID, PCI_ANY_ID, },
8468 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FALCON,
6669f9bb 8469 PCI_ANY_ID, PCI_ANY_ID, },
dea3101e 8470 { 0 }
8471};
8472
8473MODULE_DEVICE_TABLE(pci, lpfc_id_table);
8474
8d63f375
LV
8475static struct pci_error_handlers lpfc_err_handler = {
8476 .error_detected = lpfc_io_error_detected,
8477 .slot_reset = lpfc_io_slot_reset,
8478 .resume = lpfc_io_resume,
8479};
8480
dea3101e 8481static struct pci_driver lpfc_driver = {
8482 .name = LPFC_DRIVER_NAME,
8483 .id_table = lpfc_id_table,
8484 .probe = lpfc_pci_probe_one,
8485 .remove = __devexit_p(lpfc_pci_remove_one),
3a55b532 8486 .suspend = lpfc_pci_suspend_one,
3772a991 8487 .resume = lpfc_pci_resume_one,
2e0fef85 8488 .err_handler = &lpfc_err_handler,
dea3101e 8489};
8490
e59058c4 8491/**
3621a710 8492 * lpfc_init - lpfc module initialization routine
e59058c4
JS
8493 *
8494 * This routine is to be invoked when the lpfc module is loaded into the
8495 * kernel. The special kernel macro module_init() is used to indicate the
8496 * role of this routine to the kernel as lpfc module entry point.
8497 *
8498 * Return codes
8499 * 0 - successful
8500 * -ENOMEM - FC attach transport failed
8501 * all others - failed
8502 */
dea3101e 8503static int __init
8504lpfc_init(void)
8505{
8506 int error = 0;
8507
8508 printk(LPFC_MODULE_DESC "\n");
c44ce173 8509 printk(LPFC_COPYRIGHT "\n");
dea3101e 8510
7ee5d43e
JS
8511 if (lpfc_enable_npiv) {
8512 lpfc_transport_functions.vport_create = lpfc_vport_create;
8513 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
8514 }
dea3101e 8515 lpfc_transport_template =
8516 fc_attach_transport(&lpfc_transport_functions);
7ee5d43e 8517 if (lpfc_transport_template == NULL)
dea3101e 8518 return -ENOMEM;
7ee5d43e 8519 if (lpfc_enable_npiv) {
7ee5d43e 8520 lpfc_vport_transport_template =
98c9ea5c
JS
8521 fc_attach_transport(&lpfc_vport_transport_functions);
8522 if (lpfc_vport_transport_template == NULL) {
8523 fc_release_transport(lpfc_transport_template);
7ee5d43e 8524 return -ENOMEM;
98c9ea5c 8525 }
7ee5d43e 8526 }
dea3101e 8527 error = pci_register_driver(&lpfc_driver);
92d7f7b0 8528 if (error) {
dea3101e 8529 fc_release_transport(lpfc_transport_template);
d7c255b2
JS
8530 if (lpfc_enable_npiv)
8531 fc_release_transport(lpfc_vport_transport_template);
92d7f7b0 8532 }
dea3101e 8533
8534 return error;
8535}
8536
e59058c4 8537/**
3621a710 8538 * lpfc_exit - lpfc module removal routine
e59058c4
JS
8539 *
8540 * This routine is invoked when the lpfc module is removed from the kernel.
8541 * The special kernel macro module_exit() is used to indicate the role of
8542 * this routine to the kernel as lpfc module exit point.
8543 */
dea3101e 8544static void __exit
8545lpfc_exit(void)
8546{
8547 pci_unregister_driver(&lpfc_driver);
8548 fc_release_transport(lpfc_transport_template);
7ee5d43e
JS
8549 if (lpfc_enable_npiv)
8550 fc_release_transport(lpfc_vport_transport_template);
81301a9b 8551 if (_dump_buf_data) {
6a9c52cf
JS
8552 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
8553 "_dump_buf_data at 0x%p\n",
81301a9b
JS
8554 (1L << _dump_buf_data_order), _dump_buf_data);
8555 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
8556 }
8557
8558 if (_dump_buf_dif) {
6a9c52cf
JS
8559 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
8560 "_dump_buf_dif at 0x%p\n",
81301a9b
JS
8561 (1L << _dump_buf_dif_order), _dump_buf_dif);
8562 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
8563 }
dea3101e 8564}
8565
8566module_init(lpfc_init);
8567module_exit(lpfc_exit);
8568MODULE_LICENSE("GPL");
8569MODULE_DESCRIPTION(LPFC_MODULE_DESC);
8570MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
8571MODULE_VERSION("0:" LPFC_DRIVER_VERSION);
This page took 1.492935 seconds and 5 git commands to generate.