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