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