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