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