blob: 4b1eb98c228df823a986f5568f8b948a7b2ef9bf [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smartd080abe2017-02-12 13:52:39 -08004 * Copyright (C) 2017 Broadcom. All Rights Reserved. The term *
5 * “Broadcom” refers to Broadcom Limited and/or its subsidiaries. *
James Smart50611572016-03-31 14:12:34 -07006 * Copyright (C) 2004-2016 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * EMULEX and SLI are trademarks of Emulex. *
James Smartd080abe2017-02-12 13:52:39 -08008 * www.broadcom.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04009 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -050010 * *
11 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040012 * modify it under the terms of version 2 of the GNU General *
13 * Public License as published by the Free Software Foundation. *
14 * This program is distributed in the hope that it will be useful. *
15 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
16 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
17 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
18 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19 * TO BE LEGALLY INVALID. See the GNU General Public License for *
20 * more details, a copy of which can be found in the file COPYING *
21 * included with this package. *
dea31012005-04-17 16:05:31 -050022 *******************************************************************/
23
dea31012005-04-17 16:05:31 -050024#include <linux/blkdev.h>
25#include <linux/delay.h>
26#include <linux/dma-mapping.h>
27#include <linux/idr.h>
28#include <linux/interrupt.h>
Paul Gortmakeracf3368f2011-05-27 09:47:43 -040029#include <linux/module.h>
dea31012005-04-17 16:05:31 -050030#include <linux/kthread.h>
31#include <linux/pci.h>
32#include <linux/spinlock.h>
James Smart92d7f7b2007-06-17 19:56:38 -050033#include <linux/ctype.h>
James Smart0d878412009-10-02 15:16:56 -040034#include <linux/aer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
James Smart52d52442011-05-24 11:42:45 -040036#include <linux/firmware.h>
James Smart3ef6d242012-01-18 16:23:48 -050037#include <linux/miscdevice.h>
James Smart7bb03bb2013-04-17 20:19:16 -040038#include <linux/percpu.h>
James Smart895427b2017-02-12 13:52:30 -080039#include <linux/msi.h>
dea31012005-04-17 16:05:31 -050040
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040041#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050042#include <scsi/scsi_device.h>
43#include <scsi/scsi_host.h>
44#include <scsi/scsi_transport_fc.h>
James Smart86c67372017-04-21 16:05:04 -070045#include <scsi/scsi_tcq.h>
46#include <scsi/fc/fc_fs.h>
47
48#include <linux/nvme-fc-driver.h>
dea31012005-04-17 16:05:31 -050049
James Smartda0436e2009-05-22 14:51:39 -040050#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050051#include "lpfc_hw.h"
52#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040053#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040054#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050055#include "lpfc_disc.h"
dea31012005-04-17 16:05:31 -050056#include "lpfc.h"
James Smart895427b2017-02-12 13:52:30 -080057#include "lpfc_scsi.h"
58#include "lpfc_nvme.h"
James Smart86c67372017-04-21 16:05:04 -070059#include "lpfc_nvmet.h"
dea31012005-04-17 16:05:31 -050060#include "lpfc_logmsg.h"
61#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050062#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050063#include "lpfc_version.h"
James Smart12f44452016-07-06 12:36:03 -070064#include "lpfc_ids.h"
dea31012005-04-17 16:05:31 -050065
James Smart81301a92008-12-04 22:39:46 -050066char *_dump_buf_data;
67unsigned long _dump_buf_data_order;
68char *_dump_buf_dif;
69unsigned long _dump_buf_dif_order;
70spinlock_t _dump_buf_lock;
71
James Smart7bb03bb2013-04-17 20:19:16 -040072/* Used when mapping IRQ vectors in a driver centric manner */
James Smartb246de12013-05-31 17:03:07 -040073uint16_t *lpfc_used_cpu;
74uint32_t lpfc_present_cpu;
James Smart7bb03bb2013-04-17 20:19:16 -040075
dea31012005-04-17 16:05:31 -050076static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
77static int lpfc_post_rcv_buf(struct lpfc_hba *);
James Smart5350d872011-10-10 21:33:49 -040078static int lpfc_sli4_queue_verify(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040079static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
80static int lpfc_setup_endian_order(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040081static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
James Smart8a9d2e82012-05-09 21:16:12 -040082static void lpfc_free_els_sgl_list(struct lpfc_hba *);
James Smartf358dd02017-02-12 13:52:34 -080083static void lpfc_free_nvmet_sgl_list(struct lpfc_hba *);
James Smart8a9d2e82012-05-09 21:16:12 -040084static void lpfc_init_sgl_list(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040085static int lpfc_init_active_sgl_array(struct lpfc_hba *);
86static void lpfc_free_active_sgl(struct lpfc_hba *);
87static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
88static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
89static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
90static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
91static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
James Smart618a5232012-06-12 13:54:36 -040092static void lpfc_sli4_disable_intr(struct lpfc_hba *);
93static uint32_t lpfc_sli4_enable_intr(struct lpfc_hba *, uint32_t);
James Smart1ba981f2014-02-20 09:56:45 -050094static void lpfc_sli4_oas_verify(struct lpfc_hba *phba);
dea31012005-04-17 16:05:31 -050095
96static struct scsi_transport_template *lpfc_transport_template = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050097static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea31012005-04-17 16:05:31 -050098static DEFINE_IDR(lpfc_hba_index);
James Smartf358dd02017-02-12 13:52:34 -080099#define LPFC_NVMET_BUF_POST 254
dea31012005-04-17 16:05:31 -0500100
James Smarte59058c2008-08-24 21:49:00 -0400101/**
James Smart3621a712009-04-06 18:47:14 -0400102 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
James Smarte59058c2008-08-24 21:49:00 -0400103 * @phba: pointer to lpfc hba data structure.
104 *
105 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
106 * mailbox command. It retrieves the revision information from the HBA and
107 * collects the Vital Product Data (VPD) about the HBA for preparing the
108 * configuration of the HBA.
109 *
110 * Return codes:
111 * 0 - success.
112 * -ERESTART - requests the SLI layer to reset the HBA and try again.
113 * Any other value - indicates an error.
114 **/
dea31012005-04-17 16:05:31 -0500115int
James Smart2e0fef82007-06-17 19:56:36 -0500116lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500117{
118 lpfc_vpd_t *vp = &phba->vpd;
119 int i = 0, rc;
120 LPFC_MBOXQ_t *pmb;
121 MAILBOX_t *mb;
122 char *lpfc_vpd_data = NULL;
123 uint16_t offset = 0;
124 static char licensed[56] =
125 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -0400126 static int init_key = 1;
dea31012005-04-17 16:05:31 -0500127
128 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
129 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500130 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500131 return -ENOMEM;
132 }
133
James Smart04c68492009-05-22 14:52:52 -0400134 mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -0500135 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500136
137 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -0400138 if (init_key) {
139 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -0500140
James Smart65a29c12006-07-06 15:50:50 -0400141 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
142 *ptext = cpu_to_be32(*ptext);
143 init_key = 0;
144 }
dea31012005-04-17 16:05:31 -0500145
146 lpfc_read_nv(phba, pmb);
147 memset((char*)mb->un.varRDnvp.rsvd3, 0,
148 sizeof (mb->un.varRDnvp.rsvd3));
149 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
150 sizeof (licensed));
151
152 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
153
154 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500155 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smarte8b62012007-08-02 11:10:09 -0400156 "0324 Config Port initialization "
dea31012005-04-17 16:05:31 -0500157 "error, mbxCmd x%x READ_NVPARM, "
158 "mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500159 mb->mbxCommand, mb->mbxStatus);
160 mempool_free(pmb, phba->mbox_mem_pool);
161 return -ERESTART;
162 }
163 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
James Smart2e0fef82007-06-17 19:56:36 -0500164 sizeof(phba->wwnn));
165 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
166 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500167 }
168
James Smart92d7f7b2007-06-17 19:56:38 -0500169 phba->sli3_options = 0x0;
170
dea31012005-04-17 16:05:31 -0500171 /* Setup and issue mailbox READ REV command */
172 lpfc_read_rev(phba, pmb);
173 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
174 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500175 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400176 "0439 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500177 "READ_REV, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500178 mb->mbxCommand, mb->mbxStatus);
179 mempool_free( pmb, phba->mbox_mem_pool);
180 return -ERESTART;
181 }
182
James Smart92d7f7b2007-06-17 19:56:38 -0500183
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500184 /*
185 * The value of rr must be 1 since the driver set the cv field to 1.
186 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500187 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500188 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500189 vp->rev.rBit = 0;
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500190 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400191 "0440 Adapter failed to init, READ_REV has "
192 "missing revision information.\n");
dea31012005-04-17 16:05:31 -0500193 mempool_free(pmb, phba->mbox_mem_pool);
194 return -ERESTART;
dea31012005-04-17 16:05:31 -0500195 }
196
James Smart495a7142008-06-14 22:52:59 -0400197 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
198 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -0500199 return -EINVAL;
James Smart495a7142008-06-14 22:52:59 -0400200 }
James Smarted957682007-06-17 19:56:37 -0500201
dea31012005-04-17 16:05:31 -0500202 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500203 vp->rev.rBit = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500204 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500205 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
206 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
207 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
208 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500209 vp->rev.biuRev = mb->un.varRdRev.biuRev;
210 vp->rev.smRev = mb->un.varRdRev.smRev;
211 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
212 vp->rev.endecRev = mb->un.varRdRev.endecRev;
213 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
214 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
215 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
216 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
217 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
218 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
219
James Smart92d7f7b2007-06-17 19:56:38 -0500220 /* If the sli feature level is less then 9, we must
221 * tear down all RPIs and VPIs on link down if NPIV
222 * is enabled.
223 */
224 if (vp->rev.feaLevelHigh < 9)
225 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
226
dea31012005-04-17 16:05:31 -0500227 if (lpfc_is_LC_HBA(phba->pcidev->device))
228 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
229 sizeof (phba->RandomData));
230
dea31012005-04-17 16:05:31 -0500231 /* Get adapter VPD information */
dea31012005-04-17 16:05:31 -0500232 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
233 if (!lpfc_vpd_data)
James Smartd7c255b2008-08-24 21:50:00 -0400234 goto out_free_mbox;
dea31012005-04-17 16:05:31 -0500235 do {
James Smarta0c87cb2009-07-19 10:01:10 -0400236 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
dea31012005-04-17 16:05:31 -0500237 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
238
239 if (rc != MBX_SUCCESS) {
240 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400241 "0441 VPD not present on adapter, "
dea31012005-04-17 16:05:31 -0500242 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500243 mb->mbxCommand, mb->mbxStatus);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500244 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500245 }
James Smart04c68492009-05-22 14:52:52 -0400246 /* dump mem may return a zero when finished or we got a
247 * mailbox error, either way we are done.
248 */
249 if (mb->un.varDmp.word_cnt == 0)
250 break;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500251 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
252 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
James Smartd7c255b2008-08-24 21:50:00 -0400253 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
254 lpfc_vpd_data + offset,
James Smart92d7f7b2007-06-17 19:56:38 -0500255 mb->un.varDmp.word_cnt);
dea31012005-04-17 16:05:31 -0500256 offset += mb->un.varDmp.word_cnt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500257 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
258 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500259
260 kfree(lpfc_vpd_data);
dea31012005-04-17 16:05:31 -0500261out_free_mbox:
262 mempool_free(pmb, phba->mbox_mem_pool);
263 return 0;
264}
265
James Smarte59058c2008-08-24 21:49:00 -0400266/**
James Smart3621a712009-04-06 18:47:14 -0400267 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
James Smarte59058c2008-08-24 21:49:00 -0400268 * @phba: pointer to lpfc hba data structure.
269 * @pmboxq: pointer to the driver internal queue element for mailbox command.
270 *
271 * This is the completion handler for driver's configuring asynchronous event
272 * mailbox command to the device. If the mailbox command returns successfully,
273 * it will set internal async event support flag to 1; otherwise, it will
274 * set internal async event support flag to 0.
275 **/
James Smart57127f12007-10-27 13:37:05 -0400276static void
277lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
278{
James Smart04c68492009-05-22 14:52:52 -0400279 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
James Smart57127f12007-10-27 13:37:05 -0400280 phba->temp_sensor_support = 1;
281 else
282 phba->temp_sensor_support = 0;
283 mempool_free(pmboxq, phba->mbox_mem_pool);
284 return;
285}
286
James Smarte59058c2008-08-24 21:49:00 -0400287/**
James Smart3621a712009-04-06 18:47:14 -0400288 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
James Smart97207482008-12-04 22:39:19 -0500289 * @phba: pointer to lpfc hba data structure.
290 * @pmboxq: pointer to the driver internal queue element for mailbox command.
291 *
292 * This is the completion handler for dump mailbox command for getting
293 * wake up parameters. When this command complete, the response contain
294 * Option rom version of the HBA. This function translate the version number
295 * into a human readable string and store it in OptionROMVersion.
296 **/
297static void
298lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
299{
300 struct prog_id *prg;
301 uint32_t prog_id_word;
302 char dist = ' ';
303 /* character array used for decoding dist type. */
304 char dist_char[] = "nabx";
305
James Smart04c68492009-05-22 14:52:52 -0400306 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
James Smart9f1e1b52008-12-04 22:39:40 -0500307 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500308 return;
James Smart9f1e1b52008-12-04 22:39:40 -0500309 }
James Smart97207482008-12-04 22:39:19 -0500310
311 prg = (struct prog_id *) &prog_id_word;
312
313 /* word 7 contain option rom version */
James Smart04c68492009-05-22 14:52:52 -0400314 prog_id_word = pmboxq->u.mb.un.varWords[7];
James Smart97207482008-12-04 22:39:19 -0500315
316 /* Decode the Option rom version word to a readable string */
317 if (prg->dist < 4)
318 dist = dist_char[prg->dist];
319
320 if ((prg->dist == 3) && (prg->num == 0))
James Smarta2fc4aef2014-09-03 12:57:55 -0400321 snprintf(phba->OptionROMVersion, 32, "%d.%d%d",
James Smart97207482008-12-04 22:39:19 -0500322 prg->ver, prg->rev, prg->lev);
323 else
James Smarta2fc4aef2014-09-03 12:57:55 -0400324 snprintf(phba->OptionROMVersion, 32, "%d.%d%d%c%d",
James Smart97207482008-12-04 22:39:19 -0500325 prg->ver, prg->rev, prg->lev,
326 dist, prg->num);
James Smart9f1e1b52008-12-04 22:39:40 -0500327 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500328 return;
329}
330
331/**
James Smart05580562011-05-24 11:40:48 -0400332 * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
333 * cfg_soft_wwnn, cfg_soft_wwpn
334 * @vport: pointer to lpfc vport data structure.
335 *
336 *
337 * Return codes
338 * None.
339 **/
340void
341lpfc_update_vport_wwn(struct lpfc_vport *vport)
342{
James Smartaeb3c812017-04-21 16:05:02 -0700343 uint8_t vvvl = vport->fc_sparam.cmn.valid_vendor_ver_level;
344 u32 *fawwpn_key = (u32 *)&vport->fc_sparam.un.vendorVersion[0];
345
James Smart05580562011-05-24 11:40:48 -0400346 /* If the soft name exists then update it using the service params */
347 if (vport->phba->cfg_soft_wwnn)
348 u64_to_wwn(vport->phba->cfg_soft_wwnn,
349 vport->fc_sparam.nodeName.u.wwn);
350 if (vport->phba->cfg_soft_wwpn)
351 u64_to_wwn(vport->phba->cfg_soft_wwpn,
352 vport->fc_sparam.portName.u.wwn);
353
354 /*
355 * If the name is empty or there exists a soft name
356 * then copy the service params name, otherwise use the fc name
357 */
358 if (vport->fc_nodename.u.wwn[0] == 0 || vport->phba->cfg_soft_wwnn)
359 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
360 sizeof(struct lpfc_name));
361 else
362 memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
363 sizeof(struct lpfc_name));
364
James Smartaeb3c812017-04-21 16:05:02 -0700365 /*
366 * If the port name has changed, then set the Param changes flag
367 * to unreg the login
368 */
369 if (vport->fc_portname.u.wwn[0] != 0 &&
370 memcmp(&vport->fc_portname, &vport->fc_sparam.portName,
371 sizeof(struct lpfc_name)))
372 vport->vport_flag |= FAWWPN_PARAM_CHG;
373
374 if (vport->fc_portname.u.wwn[0] == 0 ||
375 vport->phba->cfg_soft_wwpn ||
376 (vvvl == 1 && cpu_to_be32(*fawwpn_key) == FAPWWN_KEY_VENDOR) ||
377 vport->vport_flag & FAWWPN_SET) {
James Smart05580562011-05-24 11:40:48 -0400378 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
379 sizeof(struct lpfc_name));
James Smartaeb3c812017-04-21 16:05:02 -0700380 vport->vport_flag &= ~FAWWPN_SET;
381 if (vvvl == 1 && cpu_to_be32(*fawwpn_key) == FAPWWN_KEY_VENDOR)
382 vport->vport_flag |= FAWWPN_SET;
383 }
James Smart05580562011-05-24 11:40:48 -0400384 else
385 memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
386 sizeof(struct lpfc_name));
387}
388
389/**
James Smart3621a712009-04-06 18:47:14 -0400390 * lpfc_config_port_post - Perform lpfc initialization after config port
James Smarte59058c2008-08-24 21:49:00 -0400391 * @phba: pointer to lpfc hba data structure.
392 *
393 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
394 * command call. It performs all internal resource and state setups on the
395 * port: post IOCB buffers, enable appropriate host interrupt attentions,
396 * ELS ring timers, etc.
397 *
398 * Return codes
399 * 0 - success.
400 * Any other value - error.
401 **/
dea31012005-04-17 16:05:31 -0500402int
James Smart2e0fef82007-06-17 19:56:36 -0500403lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500404{
James Smart2e0fef82007-06-17 19:56:36 -0500405 struct lpfc_vport *vport = phba->pport;
James Smarta257bf92009-04-06 18:48:10 -0400406 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -0500407 LPFC_MBOXQ_t *pmb;
408 MAILBOX_t *mb;
409 struct lpfc_dmabuf *mp;
410 struct lpfc_sli *psli = &phba->sli;
411 uint32_t status, timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500412 int i, j;
413 int rc;
dea31012005-04-17 16:05:31 -0500414
James Smart7af67052007-10-27 13:38:11 -0400415 spin_lock_irq(&phba->hbalock);
416 /*
417 * If the Config port completed correctly the HBA is not
418 * over heated any more.
419 */
420 if (phba->over_temp_state == HBA_OVER_TEMP)
421 phba->over_temp_state = HBA_NORMAL_TEMP;
422 spin_unlock_irq(&phba->hbalock);
423
dea31012005-04-17 16:05:31 -0500424 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
425 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500426 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500427 return -ENOMEM;
428 }
James Smart04c68492009-05-22 14:52:52 -0400429 mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500430
dea31012005-04-17 16:05:31 -0500431 /* Get login parameters for NID. */
James Smart9f1177a2010-02-26 14:12:57 -0500432 rc = lpfc_read_sparam(phba, pmb, 0);
433 if (rc) {
434 mempool_free(pmb, phba->mbox_mem_pool);
435 return -ENOMEM;
436 }
437
James Smarted957682007-06-17 19:56:37 -0500438 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500439 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500440 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400441 "0448 Adapter failed init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500442 "READ_SPARM mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500443 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500444 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500445 mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart9f1177a2010-02-26 14:12:57 -0500446 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500447 lpfc_mbuf_free(phba, mp->virt, mp->phys);
448 kfree(mp);
449 return -EIO;
450 }
451
452 mp = (struct lpfc_dmabuf *) pmb->context1;
453
James Smart2e0fef82007-06-17 19:56:36 -0500454 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500455 lpfc_mbuf_free(phba, mp->virt, mp->phys);
456 kfree(mp);
457 pmb->context1 = NULL;
James Smart05580562011-05-24 11:40:48 -0400458 lpfc_update_vport_wwn(vport);
James Smarta257bf92009-04-06 18:48:10 -0400459
460 /* Update the fc_host data structures with new wwn. */
461 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
462 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart21e9a0a2009-05-22 14:53:21 -0400463 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smarta257bf92009-04-06 18:48:10 -0400464
dea31012005-04-17 16:05:31 -0500465 /* If no serial number in VPD data, use low 6 bytes of WWNN */
466 /* This should be consolidated into parse_vpd ? - mr */
467 if (phba->SerialNumber[0] == 0) {
468 uint8_t *outptr;
469
James Smart2e0fef82007-06-17 19:56:36 -0500470 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500471 for (i = 0; i < 12; i++) {
472 status = *outptr++;
473 j = ((status & 0xf0) >> 4);
474 if (j <= 9)
475 phba->SerialNumber[i] =
476 (char)((uint8_t) 0x30 + (uint8_t) j);
477 else
478 phba->SerialNumber[i] =
479 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
480 i++;
481 j = (status & 0xf);
482 if (j <= 9)
483 phba->SerialNumber[i] =
484 (char)((uint8_t) 0x30 + (uint8_t) j);
485 else
486 phba->SerialNumber[i] =
487 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
488 }
489 }
490
dea31012005-04-17 16:05:31 -0500491 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500492 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500493 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500494 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400495 "0453 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500496 "READ_CONFIG, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500497 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500498 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500499 mempool_free( pmb, phba->mbox_mem_pool);
500 return -EIO;
501 }
502
James Smarta0c87cb2009-07-19 10:01:10 -0400503 /* Check if the port is disabled */
504 lpfc_sli_read_link_ste(phba);
505
dea31012005-04-17 16:05:31 -0500506 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -0400507 i = (mb->un.varRdConfig.max_xri + 1);
508 if (phba->cfg_hba_queue_depth > i) {
509 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
510 "3359 HBA queue depth changed from %d to %d\n",
511 phba->cfg_hba_queue_depth, i);
512 phba->cfg_hba_queue_depth = i;
513 }
514
515 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
516 i = (mb->un.varRdConfig.max_xri >> 3);
517 if (phba->pport->cfg_lun_queue_depth > i) {
518 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
519 "3360 LUN queue depth changed from %d to %d\n",
520 phba->pport->cfg_lun_queue_depth, i);
521 phba->pport->cfg_lun_queue_depth = i;
522 }
dea31012005-04-17 16:05:31 -0500523
524 phba->lmt = mb->un.varRdConfig.lmt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500525
526 /* Get the default values for Model Name and Description */
527 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
528
James Smart2e0fef82007-06-17 19:56:36 -0500529 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500530
James Smart0b727fe2007-10-27 13:37:25 -0400531 /* Only process IOCBs on ELS ring till hba_state is READY */
James Smart895427b2017-02-12 13:52:30 -0800532 if (psli->sli3_ring[LPFC_EXTRA_RING].sli.sli3.cmdringaddr)
533 psli->sli3_ring[LPFC_EXTRA_RING].flag |= LPFC_STOP_IOCB_EVENT;
534 if (psli->sli3_ring[LPFC_FCP_RING].sli.sli3.cmdringaddr)
535 psli->sli3_ring[LPFC_FCP_RING].flag |= LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500536
537 /* Post receive buffers for desired rings */
James Smarted957682007-06-17 19:56:37 -0500538 if (phba->sli_rev != 3)
539 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500540
James Smart93996272008-08-24 21:50:30 -0400541 /*
542 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
543 */
544 if (phba->intr_type == MSIX) {
545 rc = lpfc_config_msi(phba, pmb);
546 if (rc) {
547 mempool_free(pmb, phba->mbox_mem_pool);
548 return -EIO;
549 }
550 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
551 if (rc != MBX_SUCCESS) {
552 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
553 "0352 Config MSI mailbox command "
554 "failed, mbxCmd x%x, mbxStatus x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400555 pmb->u.mb.mbxCommand,
556 pmb->u.mb.mbxStatus);
James Smart93996272008-08-24 21:50:30 -0400557 mempool_free(pmb, phba->mbox_mem_pool);
558 return -EIO;
559 }
560 }
561
James Smart04c68492009-05-22 14:52:52 -0400562 spin_lock_irq(&phba->hbalock);
James Smart93996272008-08-24 21:50:30 -0400563 /* Initialize ERATT handling flag */
564 phba->hba_flag &= ~HBA_ERATT_HANDLED;
565
dea31012005-04-17 16:05:31 -0500566 /* Enable appropriate host interrupts */
James Smart9940b972011-03-11 16:06:12 -0500567 if (lpfc_readl(phba->HCregaddr, &status)) {
568 spin_unlock_irq(&phba->hbalock);
569 return -EIO;
570 }
dea31012005-04-17 16:05:31 -0500571 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
572 if (psli->num_rings > 0)
573 status |= HC_R0INT_ENA;
574 if (psli->num_rings > 1)
575 status |= HC_R1INT_ENA;
576 if (psli->num_rings > 2)
577 status |= HC_R2INT_ENA;
578 if (psli->num_rings > 3)
579 status |= HC_R3INT_ENA;
580
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500581 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
582 (phba->cfg_poll & DISABLE_FCP_RING_INT))
James Smart93996272008-08-24 21:50:30 -0400583 status &= ~(HC_R0INT_ENA);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500584
dea31012005-04-17 16:05:31 -0500585 writel(status, phba->HCregaddr);
586 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500587 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500588
James Smart93996272008-08-24 21:50:30 -0400589 /* Set up ring-0 (ELS) timer */
590 timeout = phba->fc_ratov * 2;
James Smart256ec0d2013-04-17 20:14:58 -0400591 mod_timer(&vport->els_tmofunc,
592 jiffies + msecs_to_jiffies(1000 * timeout));
James Smart93996272008-08-24 21:50:30 -0400593 /* Set up heart beat (HB) timer */
James Smart256ec0d2013-04-17 20:14:58 -0400594 mod_timer(&phba->hb_tmofunc,
595 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -0500596 phba->hb_outstanding = 0;
597 phba->last_completion_time = jiffies;
James Smart93996272008-08-24 21:50:30 -0400598 /* Set up error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -0400599 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -0700600 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
dea31012005-04-17 16:05:31 -0500601
James Smarta0c87cb2009-07-19 10:01:10 -0400602 if (phba->hba_flag & LINK_DISABLED) {
603 lpfc_printf_log(phba,
604 KERN_ERR, LOG_INIT,
605 "2598 Adapter Link is disabled.\n");
606 lpfc_down_link(phba, pmb);
607 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
608 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
609 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
610 lpfc_printf_log(phba,
611 KERN_ERR, LOG_INIT,
612 "2599 Adapter failed to issue DOWN_LINK"
613 " mbox command rc 0x%x\n", rc);
614
615 mempool_free(pmb, phba->mbox_mem_pool);
616 return -EIO;
617 }
James Smarte40a02c2010-02-26 14:13:54 -0500618 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart026abb82011-12-13 13:20:45 -0500619 mempool_free(pmb, phba->mbox_mem_pool);
620 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
621 if (rc)
622 return rc;
dea31012005-04-17 16:05:31 -0500623 }
624 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400625 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500626 if (!pmb) {
627 phba->link_state = LPFC_HBA_ERROR;
628 return -ENOMEM;
629 }
630
James Smart57127f12007-10-27 13:37:05 -0400631 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
632 pmb->mbox_cmpl = lpfc_config_async_cmpl;
633 pmb->vport = phba->pport;
634 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500635
James Smart57127f12007-10-27 13:37:05 -0400636 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
637 lpfc_printf_log(phba,
638 KERN_ERR,
639 LOG_INIT,
640 "0456 Adapter failed to issue "
James Smarte4e74272009-07-19 10:01:38 -0400641 "ASYNCEVT_ENABLE mbox status x%x\n",
James Smart57127f12007-10-27 13:37:05 -0400642 rc);
643 mempool_free(pmb, phba->mbox_mem_pool);
644 }
James Smart97207482008-12-04 22:39:19 -0500645
646 /* Get Option rom version */
647 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500648 if (!pmb) {
649 phba->link_state = LPFC_HBA_ERROR;
650 return -ENOMEM;
651 }
652
James Smart97207482008-12-04 22:39:19 -0500653 lpfc_dump_wakeup_param(phba, pmb);
654 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
655 pmb->vport = phba->pport;
656 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
657
658 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
659 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
James Smarte4e74272009-07-19 10:01:38 -0400660 "to get Option ROM version status x%x\n", rc);
James Smart97207482008-12-04 22:39:19 -0500661 mempool_free(pmb, phba->mbox_mem_pool);
662 }
663
James Smartd7c255b2008-08-24 21:50:00 -0400664 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400665}
666
James Smarte59058c2008-08-24 21:49:00 -0400667/**
James Smart84d1b002010-02-12 14:42:33 -0500668 * lpfc_hba_init_link - Initialize the FC link
669 * @phba: pointer to lpfc hba data structure.
James Smart6e7288d2010-06-07 15:23:35 -0400670 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500671 *
672 * This routine will issue the INIT_LINK mailbox command call.
673 * It is available to other drivers through the lpfc_hba data
674 * structure for use as a delayed link up mechanism with the
675 * module parameter lpfc_suppress_link_up.
676 *
677 * Return code
678 * 0 - success
679 * Any other value - error
680 **/
Rashika Kheriae399b222014-09-03 12:55:28 -0400681static int
James Smart6e7288d2010-06-07 15:23:35 -0400682lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500683{
James Smart1b511972011-12-13 13:23:09 -0500684 return lpfc_hba_init_link_fc_topology(phba, phba->cfg_topology, flag);
685}
686
687/**
688 * lpfc_hba_init_link_fc_topology - Initialize FC link with desired topology
689 * @phba: pointer to lpfc hba data structure.
690 * @fc_topology: desired fc topology.
691 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
692 *
693 * This routine will issue the INIT_LINK mailbox command call.
694 * It is available to other drivers through the lpfc_hba data
695 * structure for use as a delayed link up mechanism with the
696 * module parameter lpfc_suppress_link_up.
697 *
698 * Return code
699 * 0 - success
700 * Any other value - error
701 **/
702int
703lpfc_hba_init_link_fc_topology(struct lpfc_hba *phba, uint32_t fc_topology,
704 uint32_t flag)
705{
James Smart84d1b002010-02-12 14:42:33 -0500706 struct lpfc_vport *vport = phba->pport;
707 LPFC_MBOXQ_t *pmb;
708 MAILBOX_t *mb;
709 int rc;
710
711 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
712 if (!pmb) {
713 phba->link_state = LPFC_HBA_ERROR;
714 return -ENOMEM;
715 }
716 mb = &pmb->u.mb;
717 pmb->vport = vport;
718
James Smart026abb82011-12-13 13:20:45 -0500719 if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_MAX) ||
720 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G) &&
721 !(phba->lmt & LMT_1Gb)) ||
722 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G) &&
723 !(phba->lmt & LMT_2Gb)) ||
724 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G) &&
725 !(phba->lmt & LMT_4Gb)) ||
726 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G) &&
727 !(phba->lmt & LMT_8Gb)) ||
728 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G) &&
729 !(phba->lmt & LMT_10Gb)) ||
730 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G) &&
James Smartd38dd522015-08-31 16:48:17 -0400731 !(phba->lmt & LMT_16Gb)) ||
732 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_32G) &&
733 !(phba->lmt & LMT_32Gb))) {
James Smart026abb82011-12-13 13:20:45 -0500734 /* Reset link speed to auto */
735 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
736 "1302 Invalid speed for this board:%d "
737 "Reset link speed to auto.\n",
738 phba->cfg_link_speed);
739 phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
740 }
James Smart1b511972011-12-13 13:23:09 -0500741 lpfc_init_link(phba, pmb, fc_topology, phba->cfg_link_speed);
James Smart84d1b002010-02-12 14:42:33 -0500742 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart1b511972011-12-13 13:23:09 -0500743 if (phba->sli_rev < LPFC_SLI_REV4)
744 lpfc_set_loopback_flag(phba);
James Smart6e7288d2010-06-07 15:23:35 -0400745 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart76a95d72010-11-20 23:11:48 -0500746 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
James Smart84d1b002010-02-12 14:42:33 -0500747 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
748 "0498 Adapter failed to init, mbxCmd x%x "
749 "INIT_LINK, mbxStatus x%x\n",
750 mb->mbxCommand, mb->mbxStatus);
James Smart76a95d72010-11-20 23:11:48 -0500751 if (phba->sli_rev <= LPFC_SLI_REV3) {
752 /* Clear all interrupt enable conditions */
753 writel(0, phba->HCregaddr);
754 readl(phba->HCregaddr); /* flush */
755 /* Clear all pending interrupts */
756 writel(0xffffffff, phba->HAregaddr);
757 readl(phba->HAregaddr); /* flush */
758 }
James Smart84d1b002010-02-12 14:42:33 -0500759 phba->link_state = LPFC_HBA_ERROR;
James Smart6e7288d2010-06-07 15:23:35 -0400760 if (rc != MBX_BUSY || flag == MBX_POLL)
James Smart84d1b002010-02-12 14:42:33 -0500761 mempool_free(pmb, phba->mbox_mem_pool);
762 return -EIO;
763 }
James Smarte40a02c2010-02-26 14:13:54 -0500764 phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
James Smart6e7288d2010-06-07 15:23:35 -0400765 if (flag == MBX_POLL)
766 mempool_free(pmb, phba->mbox_mem_pool);
James Smart84d1b002010-02-12 14:42:33 -0500767
768 return 0;
769}
770
771/**
772 * lpfc_hba_down_link - this routine downs the FC link
James Smart6e7288d2010-06-07 15:23:35 -0400773 * @phba: pointer to lpfc hba data structure.
774 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500775 *
776 * This routine will issue the DOWN_LINK mailbox command call.
777 * It is available to other drivers through the lpfc_hba data
778 * structure for use to stop the link.
779 *
780 * Return code
781 * 0 - success
782 * Any other value - error
783 **/
Rashika Kheriae399b222014-09-03 12:55:28 -0400784static int
James Smart6e7288d2010-06-07 15:23:35 -0400785lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500786{
787 LPFC_MBOXQ_t *pmb;
788 int rc;
789
790 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
791 if (!pmb) {
792 phba->link_state = LPFC_HBA_ERROR;
793 return -ENOMEM;
794 }
795
796 lpfc_printf_log(phba,
797 KERN_ERR, LOG_INIT,
798 "0491 Adapter Link is disabled.\n");
799 lpfc_down_link(phba, pmb);
800 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6e7288d2010-06-07 15:23:35 -0400801 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart84d1b002010-02-12 14:42:33 -0500802 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
803 lpfc_printf_log(phba,
804 KERN_ERR, LOG_INIT,
805 "2522 Adapter failed to issue DOWN_LINK"
806 " mbox command rc 0x%x\n", rc);
807
808 mempool_free(pmb, phba->mbox_mem_pool);
809 return -EIO;
810 }
James Smart6e7288d2010-06-07 15:23:35 -0400811 if (flag == MBX_POLL)
812 mempool_free(pmb, phba->mbox_mem_pool);
813
James Smart84d1b002010-02-12 14:42:33 -0500814 return 0;
815}
816
817/**
James Smart3621a712009-04-06 18:47:14 -0400818 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400819 * @phba: pointer to lpfc HBA data structure.
820 *
821 * This routine will do LPFC uninitialization before the HBA is reset when
822 * bringing down the SLI Layer.
823 *
824 * Return codes
825 * 0 - success.
826 * Any other value - error.
827 **/
dea31012005-04-17 16:05:31 -0500828int
James Smart2e0fef82007-06-17 19:56:36 -0500829lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500830{
James Smart1b32f6a2008-02-08 18:49:39 -0500831 struct lpfc_vport **vports;
832 int i;
James Smart3772a992009-05-22 14:50:54 -0400833
834 if (phba->sli_rev <= LPFC_SLI_REV3) {
835 /* Disable interrupts */
836 writel(0, phba->HCregaddr);
837 readl(phba->HCregaddr); /* flush */
838 }
dea31012005-04-17 16:05:31 -0500839
James Smart1b32f6a2008-02-08 18:49:39 -0500840 if (phba->pport->load_flag & FC_UNLOADING)
841 lpfc_cleanup_discovery_resources(phba->pport);
842 else {
843 vports = lpfc_create_vport_work_array(phba);
844 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400845 for (i = 0; i <= phba->max_vports &&
846 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500847 lpfc_cleanup_discovery_resources(vports[i]);
848 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500849 }
850 return 0;
dea31012005-04-17 16:05:31 -0500851}
852
James Smarte59058c2008-08-24 21:49:00 -0400853/**
James Smart68e814f2014-05-21 08:04:59 -0400854 * lpfc_sli4_free_sp_events - Cleanup sp_queue_events to free
855 * rspiocb which got deferred
856 *
857 * @phba: pointer to lpfc HBA data structure.
858 *
859 * This routine will cleanup completed slow path events after HBA is reset
860 * when bringing down the SLI Layer.
861 *
862 *
863 * Return codes
864 * void.
865 **/
866static void
867lpfc_sli4_free_sp_events(struct lpfc_hba *phba)
868{
869 struct lpfc_iocbq *rspiocbq;
870 struct hbq_dmabuf *dmabuf;
871 struct lpfc_cq_event *cq_event;
872
873 spin_lock_irq(&phba->hbalock);
874 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
875 spin_unlock_irq(&phba->hbalock);
876
877 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
878 /* Get the response iocb from the head of work queue */
879 spin_lock_irq(&phba->hbalock);
880 list_remove_head(&phba->sli4_hba.sp_queue_event,
881 cq_event, struct lpfc_cq_event, list);
882 spin_unlock_irq(&phba->hbalock);
883
884 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
885 case CQE_CODE_COMPL_WQE:
886 rspiocbq = container_of(cq_event, struct lpfc_iocbq,
887 cq_event);
888 lpfc_sli_release_iocbq(phba, rspiocbq);
889 break;
890 case CQE_CODE_RECEIVE:
891 case CQE_CODE_RECEIVE_V1:
892 dmabuf = container_of(cq_event, struct hbq_dmabuf,
893 cq_event);
894 lpfc_in_buf_free(phba, &dmabuf->dbuf);
895 }
896 }
897}
898
899/**
James Smartbcece5f2014-04-04 13:51:34 -0400900 * lpfc_hba_free_post_buf - Perform lpfc uninitialization after HBA reset
901 * @phba: pointer to lpfc HBA data structure.
902 *
903 * This routine will cleanup posted ELS buffers after the HBA is reset
904 * when bringing down the SLI Layer.
905 *
906 *
907 * Return codes
908 * void.
909 **/
910static void
911lpfc_hba_free_post_buf(struct lpfc_hba *phba)
912{
913 struct lpfc_sli *psli = &phba->sli;
914 struct lpfc_sli_ring *pring;
915 struct lpfc_dmabuf *mp, *next_mp;
James Smart07eab622014-04-04 13:51:44 -0400916 LIST_HEAD(buflist);
917 int count;
James Smartbcece5f2014-04-04 13:51:34 -0400918
919 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
920 lpfc_sli_hbqbuf_free_all(phba);
921 else {
922 /* Cleanup preposted buffers on the ELS ring */
James Smart895427b2017-02-12 13:52:30 -0800923 pring = &psli->sli3_ring[LPFC_ELS_RING];
James Smart07eab622014-04-04 13:51:44 -0400924 spin_lock_irq(&phba->hbalock);
925 list_splice_init(&pring->postbufq, &buflist);
926 spin_unlock_irq(&phba->hbalock);
927
928 count = 0;
929 list_for_each_entry_safe(mp, next_mp, &buflist, list) {
James Smartbcece5f2014-04-04 13:51:34 -0400930 list_del(&mp->list);
James Smart07eab622014-04-04 13:51:44 -0400931 count++;
James Smartbcece5f2014-04-04 13:51:34 -0400932 lpfc_mbuf_free(phba, mp->virt, mp->phys);
933 kfree(mp);
934 }
James Smart07eab622014-04-04 13:51:44 -0400935
936 spin_lock_irq(&phba->hbalock);
937 pring->postbufq_cnt -= count;
James Smartbcece5f2014-04-04 13:51:34 -0400938 spin_unlock_irq(&phba->hbalock);
939 }
940}
941
942/**
943 * lpfc_hba_clean_txcmplq - Perform lpfc uninitialization after HBA reset
944 * @phba: pointer to lpfc HBA data structure.
945 *
946 * This routine will cleanup the txcmplq after the HBA is reset when bringing
947 * down the SLI Layer.
948 *
949 * Return codes
950 * void
951 **/
952static void
953lpfc_hba_clean_txcmplq(struct lpfc_hba *phba)
954{
955 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -0800956 struct lpfc_queue *qp = NULL;
James Smartbcece5f2014-04-04 13:51:34 -0400957 struct lpfc_sli_ring *pring;
958 LIST_HEAD(completions);
959 int i;
960
James Smart895427b2017-02-12 13:52:30 -0800961 if (phba->sli_rev != LPFC_SLI_REV4) {
962 for (i = 0; i < psli->num_rings; i++) {
963 pring = &psli->sli3_ring[i];
James Smartbcece5f2014-04-04 13:51:34 -0400964 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -0800965 /* At this point in time the HBA is either reset or DOA
966 * Nothing should be on txcmplq as it will
967 * NEVER complete.
968 */
969 list_splice_init(&pring->txcmplq, &completions);
970 pring->txcmplq_cnt = 0;
James Smartbcece5f2014-04-04 13:51:34 -0400971 spin_unlock_irq(&phba->hbalock);
972
James Smart895427b2017-02-12 13:52:30 -0800973 lpfc_sli_abort_iocb_ring(phba, pring);
974 }
James Smartbcece5f2014-04-04 13:51:34 -0400975 /* Cancel all the IOCBs from the completions list */
James Smart895427b2017-02-12 13:52:30 -0800976 lpfc_sli_cancel_iocbs(phba, &completions,
977 IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
978 return;
979 }
980 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
981 pring = qp->pring;
982 if (!pring)
983 continue;
984 spin_lock_irq(&pring->ring_lock);
985 list_splice_init(&pring->txcmplq, &completions);
986 pring->txcmplq_cnt = 0;
987 spin_unlock_irq(&pring->ring_lock);
James Smartbcece5f2014-04-04 13:51:34 -0400988 lpfc_sli_abort_iocb_ring(phba, pring);
989 }
James Smart895427b2017-02-12 13:52:30 -0800990 /* Cancel all the IOCBs from the completions list */
991 lpfc_sli_cancel_iocbs(phba, &completions,
992 IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
James Smartbcece5f2014-04-04 13:51:34 -0400993}
994
995/**
James Smart3772a992009-05-22 14:50:54 -0400996 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smartbcece5f2014-04-04 13:51:34 -0400997 int i;
James Smarte59058c2008-08-24 21:49:00 -0400998 * @phba: pointer to lpfc HBA data structure.
999 *
1000 * This routine will do uninitialization after the HBA is reset when bring
1001 * down the SLI Layer.
1002 *
1003 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001004 * 0 - success.
James Smarte59058c2008-08-24 21:49:00 -04001005 * Any other value - error.
1006 **/
James Smart3772a992009-05-22 14:50:54 -04001007static int
1008lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05001009{
James Smartbcece5f2014-04-04 13:51:34 -04001010 lpfc_hba_free_post_buf(phba);
1011 lpfc_hba_clean_txcmplq(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001012 return 0;
1013}
James Smart5af5eee2010-10-22 11:06:38 -04001014
James Smartda0436e2009-05-22 14:51:39 -04001015/**
1016 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
1017 * @phba: pointer to lpfc HBA data structure.
1018 *
1019 * This routine will do uninitialization after the HBA is reset when bring
1020 * down the SLI Layer.
1021 *
1022 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001023 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001024 * Any other value - error.
1025 **/
1026static int
1027lpfc_hba_down_post_s4(struct lpfc_hba *phba)
1028{
1029 struct lpfc_scsi_buf *psb, *psb_next;
James Smart86c67372017-04-21 16:05:04 -07001030 struct lpfc_nvmet_rcv_ctx *ctxp, *ctxp_next;
James Smartda0436e2009-05-22 14:51:39 -04001031 LIST_HEAD(aborts);
James Smart895427b2017-02-12 13:52:30 -08001032 LIST_HEAD(nvme_aborts);
James Smart86c67372017-04-21 16:05:04 -07001033 LIST_HEAD(nvmet_aborts);
James Smartda0436e2009-05-22 14:51:39 -04001034 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -05001035 struct lpfc_sglq *sglq_entry = NULL;
1036
James Smart895427b2017-02-12 13:52:30 -08001037
1038 lpfc_sli_hbqbuf_free_all(phba);
James Smartbcece5f2014-04-04 13:51:34 -04001039 lpfc_hba_clean_txcmplq(phba);
1040
James Smartda0436e2009-05-22 14:51:39 -04001041 /* At this point in time the HBA is either reset or DOA. Either
1042 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
James Smart895427b2017-02-12 13:52:30 -08001043 * on the lpfc_els_sgl_list so that it can either be freed if the
James Smartda0436e2009-05-22 14:51:39 -04001044 * driver is unloading or reposted if the driver is restarting
1045 * the port.
1046 */
James Smart895427b2017-02-12 13:52:30 -08001047 spin_lock_irq(&phba->hbalock); /* required for lpfc_els_sgl_list and */
James Smartda0436e2009-05-22 14:51:39 -04001048 /* scsl_buf_list */
James Smart895427b2017-02-12 13:52:30 -08001049 /* sgl_list_lock required because worker thread uses this
James Smartda0436e2009-05-22 14:51:39 -04001050 * list.
1051 */
James Smart895427b2017-02-12 13:52:30 -08001052 spin_lock(&phba->sli4_hba.sgl_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -05001053 list_for_each_entry(sglq_entry,
1054 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
1055 sglq_entry->state = SGL_FREED;
1056
James Smartda0436e2009-05-22 14:51:39 -04001057 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
James Smart895427b2017-02-12 13:52:30 -08001058 &phba->sli4_hba.lpfc_els_sgl_list);
1059
James Smartf358dd02017-02-12 13:52:34 -08001060
James Smart895427b2017-02-12 13:52:30 -08001061 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04001062 /* abts_scsi_buf_list_lock required because worker thread uses this
1063 * list.
1064 */
James Smart895427b2017-02-12 13:52:30 -08001065 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
1066 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
1067 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
1068 &aborts);
1069 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
1070 }
1071
1072 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
1073 spin_lock(&phba->sli4_hba.abts_nvme_buf_list_lock);
1074 list_splice_init(&phba->sli4_hba.lpfc_abts_nvme_buf_list,
1075 &nvme_aborts);
James Smart86c67372017-04-21 16:05:04 -07001076 list_splice_init(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
1077 &nvmet_aborts);
James Smart895427b2017-02-12 13:52:30 -08001078 spin_unlock(&phba->sli4_hba.abts_nvme_buf_list_lock);
1079 }
1080
James Smartda0436e2009-05-22 14:51:39 -04001081 spin_unlock_irq(&phba->hbalock);
1082
1083 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
1084 psb->pCmd = NULL;
1085 psb->status = IOSTAT_SUCCESS;
1086 }
James Smarta40fc5f2013-04-17 20:17:40 -04001087 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
1088 list_splice(&aborts, &phba->lpfc_scsi_buf_list_put);
1089 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
James Smart68e814f2014-05-21 08:04:59 -04001090
James Smart86c67372017-04-21 16:05:04 -07001091 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
1092 list_for_each_entry_safe(psb, psb_next, &nvme_aborts, list) {
1093 psb->pCmd = NULL;
1094 psb->status = IOSTAT_SUCCESS;
1095 }
1096 spin_lock_irqsave(&phba->nvme_buf_list_put_lock, iflag);
1097 list_splice(&nvme_aborts, &phba->lpfc_nvme_buf_list_put);
1098 spin_unlock_irqrestore(&phba->nvme_buf_list_put_lock, iflag);
1099
1100 list_for_each_entry_safe(ctxp, ctxp_next, &nvmet_aborts, list) {
1101 ctxp->flag &= ~(LPFC_NVMET_XBUSY | LPFC_NVMET_ABORT_OP);
1102 lpfc_nvmet_rq_post(phba, ctxp, &ctxp->rqb_buffer->hbuf);
1103 }
James Smart895427b2017-02-12 13:52:30 -08001104 }
James Smart895427b2017-02-12 13:52:30 -08001105
James Smart68e814f2014-05-21 08:04:59 -04001106 lpfc_sli4_free_sp_events(phba);
James Smartda0436e2009-05-22 14:51:39 -04001107 return 0;
1108}
1109
1110/**
1111 * lpfc_hba_down_post - Wrapper func for hba down post routine
1112 * @phba: pointer to lpfc HBA data structure.
1113 *
1114 * This routine wraps the actual SLI3 or SLI4 routine for performing
1115 * uninitialization after the HBA is reset when bring down the SLI Layer.
1116 *
1117 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001118 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001119 * Any other value - error.
1120 **/
1121int
1122lpfc_hba_down_post(struct lpfc_hba *phba)
1123{
1124 return (*phba->lpfc_hba_down_post)(phba);
1125}
Jamie Wellnitz41415862006-02-28 19:25:27 -05001126
James Smarte59058c2008-08-24 21:49:00 -04001127/**
James Smart3621a712009-04-06 18:47:14 -04001128 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001129 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1130 *
1131 * This is the HBA-timer timeout handler registered to the lpfc driver. When
1132 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
1133 * work-port-events bitmap and the worker thread is notified. This timeout
1134 * event will be used by the worker thread to invoke the actual timeout
1135 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
1136 * be performed in the timeout handler and the HBA timeout event bit shall
1137 * be cleared by the worker thread after it has taken the event bitmap out.
1138 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001139static void
James Smart858c9f62007-06-17 19:56:39 -05001140lpfc_hb_timeout(unsigned long ptr)
1141{
1142 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -04001143 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -05001144 unsigned long iflag;
1145
1146 phba = (struct lpfc_hba *)ptr;
James Smart93996272008-08-24 21:50:30 -04001147
1148 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -05001149 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04001150 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
1151 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -05001152 phba->pport->work_port_events |= WORKER_HB_TMO;
1153 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
1154
James Smart93996272008-08-24 21:50:30 -04001155 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -04001156 if (!tmo_posted)
1157 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -05001158 return;
1159}
1160
James Smarte59058c2008-08-24 21:49:00 -04001161/**
James Smart19ca7602010-11-20 23:11:55 -05001162 * lpfc_rrq_timeout - The RRQ-timer timeout handler
1163 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1164 *
1165 * This is the RRQ-timer timeout handler registered to the lpfc driver. When
1166 * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
1167 * work-port-events bitmap and the worker thread is notified. This timeout
1168 * event will be used by the worker thread to invoke the actual timeout
1169 * handler routine, lpfc_rrq_handler. Any periodical operations will
1170 * be performed in the timeout handler and the RRQ timeout event bit shall
1171 * be cleared by the worker thread after it has taken the event bitmap out.
1172 **/
1173static void
1174lpfc_rrq_timeout(unsigned long ptr)
1175{
1176 struct lpfc_hba *phba;
James Smart19ca7602010-11-20 23:11:55 -05001177 unsigned long iflag;
1178
1179 phba = (struct lpfc_hba *)ptr;
1180 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001181 if (!(phba->pport->load_flag & FC_UNLOADING))
1182 phba->hba_flag |= HBA_RRQ_ACTIVE;
1183 else
1184 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart19ca7602010-11-20 23:11:55 -05001185 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001186
1187 if (!(phba->pport->load_flag & FC_UNLOADING))
1188 lpfc_worker_wake_up(phba);
James Smart19ca7602010-11-20 23:11:55 -05001189}
1190
1191/**
James Smart3621a712009-04-06 18:47:14 -04001192 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -04001193 * @phba: pointer to lpfc hba data structure.
1194 * @pmboxq: pointer to the driver internal queue element for mailbox command.
1195 *
1196 * This is the callback function to the lpfc heart-beat mailbox command.
1197 * If configured, the lpfc driver issues the heart-beat mailbox command to
1198 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
1199 * heart-beat mailbox command is issued, the driver shall set up heart-beat
1200 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
1201 * heart-beat outstanding state. Once the mailbox command comes back and
1202 * no error conditions detected, the heart-beat mailbox command timer is
1203 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1204 * state is cleared for the next heart-beat. If the timer expired with the
1205 * heart-beat outstanding state set, the driver will put the HBA offline.
1206 **/
James Smart858c9f62007-06-17 19:56:39 -05001207static void
1208lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1209{
1210 unsigned long drvr_flag;
1211
1212 spin_lock_irqsave(&phba->hbalock, drvr_flag);
1213 phba->hb_outstanding = 0;
1214 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1215
James Smart93996272008-08-24 21:50:30 -04001216 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -05001217 mempool_free(pmboxq, phba->mbox_mem_pool);
1218 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1219 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -04001220 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -05001221 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001222 jiffies +
1223 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001224 return;
1225}
1226
James Smarte59058c2008-08-24 21:49:00 -04001227/**
James Smart3621a712009-04-06 18:47:14 -04001228 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001229 * @phba: pointer to lpfc hba data structure.
1230 *
1231 * This is the actual HBA-timer timeout handler to be invoked by the worker
1232 * thread whenever the HBA timer fired and HBA-timeout event posted. This
1233 * handler performs any periodic operations needed for the device. If such
1234 * periodic event has already been attended to either in the interrupt handler
1235 * or by processing slow-ring or fast-ring events within the HBA-timer
1236 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1237 * the timer for the next timeout period. If lpfc heart-beat mailbox command
1238 * is configured and there is no heart-beat mailbox command outstanding, a
1239 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1240 * has been a heart-beat mailbox command outstanding, the HBA shall be put
1241 * to offline.
1242 **/
James Smart858c9f62007-06-17 19:56:39 -05001243void
1244lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1245{
James Smart45ed1192009-10-02 15:17:02 -04001246 struct lpfc_vport **vports;
James Smart858c9f62007-06-17 19:56:39 -05001247 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -05001248 struct lpfc_dmabuf *buf_ptr;
James Smart45ed1192009-10-02 15:17:02 -04001249 int retval, i;
James Smart858c9f62007-06-17 19:56:39 -05001250 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -05001251 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -05001252
James Smart45ed1192009-10-02 15:17:02 -04001253 vports = lpfc_create_vport_work_array(phba);
1254 if (vports != NULL)
James Smart4258e982015-12-16 18:11:58 -05001255 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart45ed1192009-10-02 15:17:02 -04001256 lpfc_rcv_seq_check_edtov(vports[i]);
James Smart4258e982015-12-16 18:11:58 -05001257 lpfc_fdmi_num_disc_check(vports[i]);
1258 }
James Smart45ed1192009-10-02 15:17:02 -04001259 lpfc_destroy_vport_work_array(phba, vports);
1260
James Smart858c9f62007-06-17 19:56:39 -05001261 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -04001262 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -05001263 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1264 return;
1265
1266 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -05001267
James Smart256ec0d2013-04-17 20:14:58 -04001268 if (time_after(phba->last_completion_time +
1269 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL),
1270 jiffies)) {
James Smart858c9f62007-06-17 19:56:39 -05001271 spin_unlock_irq(&phba->pport->work_port_lock);
1272 if (!phba->hb_outstanding)
1273 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001274 jiffies +
1275 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001276 else
1277 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001278 jiffies +
1279 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001280 return;
1281 }
1282 spin_unlock_irq(&phba->pport->work_port_lock);
1283
James Smart0ff10d42008-01-11 01:52:36 -05001284 if (phba->elsbuf_cnt &&
1285 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1286 spin_lock_irq(&phba->hbalock);
1287 list_splice_init(&phba->elsbuf, &completions);
1288 phba->elsbuf_cnt = 0;
1289 phba->elsbuf_prev_cnt = 0;
1290 spin_unlock_irq(&phba->hbalock);
1291
1292 while (!list_empty(&completions)) {
1293 list_remove_head(&completions, buf_ptr,
1294 struct lpfc_dmabuf, list);
1295 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1296 kfree(buf_ptr);
1297 }
1298 }
1299 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1300
James Smart858c9f62007-06-17 19:56:39 -05001301 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -05001302 if (phba->cfg_enable_hba_heartbeat) {
1303 if (!phba->hb_outstanding) {
James Smartbc739052010-08-04 16:11:18 -04001304 if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1305 (list_empty(&psli->mboxq))) {
1306 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1307 GFP_KERNEL);
1308 if (!pmboxq) {
1309 mod_timer(&phba->hb_tmofunc,
1310 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001311 msecs_to_jiffies(1000 *
1312 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001313 return;
1314 }
James Smart13815c82008-01-11 01:52:48 -05001315
James Smartbc739052010-08-04 16:11:18 -04001316 lpfc_heart_beat(phba, pmboxq);
1317 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1318 pmboxq->vport = phba->pport;
1319 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1320 MBX_NOWAIT);
James Smart13815c82008-01-11 01:52:48 -05001321
James Smartbc739052010-08-04 16:11:18 -04001322 if (retval != MBX_BUSY &&
1323 retval != MBX_SUCCESS) {
1324 mempool_free(pmboxq,
1325 phba->mbox_mem_pool);
1326 mod_timer(&phba->hb_tmofunc,
1327 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001328 msecs_to_jiffies(1000 *
1329 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001330 return;
1331 }
1332 phba->skipped_hb = 0;
1333 phba->hb_outstanding = 1;
1334 } else if (time_before_eq(phba->last_completion_time,
1335 phba->skipped_hb)) {
1336 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1337 "2857 Last completion time not "
1338 " updated in %d ms\n",
1339 jiffies_to_msecs(jiffies
1340 - phba->last_completion_time));
1341 } else
1342 phba->skipped_hb = jiffies;
1343
James Smart858c9f62007-06-17 19:56:39 -05001344 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001345 jiffies +
1346 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001347 return;
James Smart13815c82008-01-11 01:52:48 -05001348 } else {
1349 /*
1350 * If heart beat timeout called with hb_outstanding set
James Smartdcf2a4e2010-09-29 11:18:53 -04001351 * we need to give the hb mailbox cmd a chance to
1352 * complete or TMO.
James Smart13815c82008-01-11 01:52:48 -05001353 */
James Smartdcf2a4e2010-09-29 11:18:53 -04001354 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1355 "0459 Adapter heartbeat still out"
1356 "standing:last compl time was %d ms.\n",
1357 jiffies_to_msecs(jiffies
1358 - phba->last_completion_time));
1359 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001360 jiffies +
1361 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001362 }
James Smart4258e982015-12-16 18:11:58 -05001363 } else {
1364 mod_timer(&phba->hb_tmofunc,
1365 jiffies +
1366 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001367 }
1368}
1369
James Smarte59058c2008-08-24 21:49:00 -04001370/**
James Smart3621a712009-04-06 18:47:14 -04001371 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -04001372 * @phba: pointer to lpfc hba data structure.
1373 *
1374 * This routine is called to bring the HBA offline when HBA hardware error
1375 * other than Port Error 6 has been detected.
1376 **/
James Smart09372822008-01-11 01:52:54 -05001377static void
1378lpfc_offline_eratt(struct lpfc_hba *phba)
1379{
1380 struct lpfc_sli *psli = &phba->sli;
1381
1382 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001383 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -05001384 spin_unlock_irq(&phba->hbalock);
James Smart618a5232012-06-12 13:54:36 -04001385 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart09372822008-01-11 01:52:54 -05001386
1387 lpfc_offline(phba);
1388 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001389 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001390 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001391 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001392 lpfc_hba_down_post(phba);
1393 lpfc_sli_brdready(phba, HS_MBRDY);
1394 lpfc_unblock_mgmt_io(phba);
1395 phba->link_state = LPFC_HBA_ERROR;
1396 return;
1397}
1398
James Smarte59058c2008-08-24 21:49:00 -04001399/**
James Smartda0436e2009-05-22 14:51:39 -04001400 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1401 * @phba: pointer to lpfc hba data structure.
1402 *
1403 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1404 * other than Port Error 6 has been detected.
1405 **/
James Smarta88dbb62013-04-17 20:18:39 -04001406void
James Smartda0436e2009-05-22 14:51:39 -04001407lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1408{
James Smart946727d2015-04-07 15:07:09 -04001409 spin_lock_irq(&phba->hbalock);
1410 phba->link_state = LPFC_HBA_ERROR;
1411 spin_unlock_irq(&phba->hbalock);
1412
James Smart618a5232012-06-12 13:54:36 -04001413 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smartda0436e2009-05-22 14:51:39 -04001414 lpfc_offline(phba);
James Smartda0436e2009-05-22 14:51:39 -04001415 lpfc_hba_down_post(phba);
James Smartda0436e2009-05-22 14:51:39 -04001416 lpfc_unblock_mgmt_io(phba);
James Smartda0436e2009-05-22 14:51:39 -04001417}
1418
1419/**
James Smarta257bf92009-04-06 18:48:10 -04001420 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1421 * @phba: pointer to lpfc hba data structure.
1422 *
1423 * This routine is invoked to handle the deferred HBA hardware error
1424 * conditions. This type of error is indicated by HBA by setting ER1
1425 * and another ER bit in the host status register. The driver will
1426 * wait until the ER1 bit clears before handling the error condition.
1427 **/
1428static void
1429lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1430{
1431 uint32_t old_host_status = phba->work_hs;
James Smarta257bf92009-04-06 18:48:10 -04001432 struct lpfc_sli *psli = &phba->sli;
1433
James Smartf4b4c682009-05-22 14:53:12 -04001434 /* If the pci channel is offline, ignore possible errors,
1435 * since we cannot communicate with the pci card anyway.
1436 */
1437 if (pci_channel_offline(phba->pcidev)) {
1438 spin_lock_irq(&phba->hbalock);
1439 phba->hba_flag &= ~DEFER_ERATT;
1440 spin_unlock_irq(&phba->hbalock);
1441 return;
1442 }
1443
James Smarta257bf92009-04-06 18:48:10 -04001444 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1445 "0479 Deferred Adapter Hardware Error "
1446 "Data: x%x x%x x%x\n",
1447 phba->work_hs,
1448 phba->work_status[0], phba->work_status[1]);
1449
1450 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001451 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001452 spin_unlock_irq(&phba->hbalock);
1453
1454
1455 /*
1456 * Firmware stops when it triggred erratt. That could cause the I/Os
1457 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1458 * SCSI layer retry it after re-establishing link.
1459 */
James Smartdb55fba2014-04-04 13:52:02 -04001460 lpfc_sli_abort_fcp_rings(phba);
James Smarta257bf92009-04-06 18:48:10 -04001461
1462 /*
1463 * There was a firmware error. Take the hba offline and then
1464 * attempt to restart it.
1465 */
James Smart618a5232012-06-12 13:54:36 -04001466 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smarta257bf92009-04-06 18:48:10 -04001467 lpfc_offline(phba);
1468
1469 /* Wait for the ER1 bit to clear.*/
1470 while (phba->work_hs & HS_FFER1) {
1471 msleep(100);
James Smart9940b972011-03-11 16:06:12 -05001472 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1473 phba->work_hs = UNPLUG_ERR ;
1474 break;
1475 }
James Smarta257bf92009-04-06 18:48:10 -04001476 /* If driver is unloading let the worker thread continue */
1477 if (phba->pport->load_flag & FC_UNLOADING) {
1478 phba->work_hs = 0;
1479 break;
1480 }
1481 }
1482
1483 /*
1484 * This is to ptrotect against a race condition in which
1485 * first write to the host attention register clear the
1486 * host status register.
1487 */
1488 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1489 phba->work_hs = old_host_status & ~HS_FFER1;
1490
James Smart3772a992009-05-22 14:50:54 -04001491 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001492 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001493 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001494 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1495 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1496}
1497
James Smart3772a992009-05-22 14:50:54 -04001498static void
1499lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1500{
1501 struct lpfc_board_event_header board_event;
1502 struct Scsi_Host *shost;
1503
1504 board_event.event_type = FC_REG_BOARD_EVENT;
1505 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1506 shost = lpfc_shost_from_vport(phba->pport);
1507 fc_host_post_vendor_event(shost, fc_get_event_number(),
1508 sizeof(board_event),
1509 (char *) &board_event,
1510 LPFC_NL_VENDOR_ID);
1511}
1512
James Smarta257bf92009-04-06 18:48:10 -04001513/**
James Smart3772a992009-05-22 14:50:54 -04001514 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001515 * @phba: pointer to lpfc hba data structure.
1516 *
1517 * This routine is invoked to handle the following HBA hardware error
1518 * conditions:
1519 * 1 - HBA error attention interrupt
1520 * 2 - DMA ring index out of range
1521 * 3 - Mailbox command came back as unknown
1522 **/
James Smart3772a992009-05-22 14:50:54 -04001523static void
1524lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001525{
James Smart2e0fef82007-06-17 19:56:36 -05001526 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001527 struct lpfc_sli *psli = &phba->sli;
James Smartd2873e42006-08-18 17:46:43 -04001528 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001529 unsigned long temperature;
1530 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001531 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001532
Linas Vepstas8d63f372007-02-14 14:28:36 -06001533 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001534 * since we cannot communicate with the pci card anyway.
1535 */
1536 if (pci_channel_offline(phba->pcidev)) {
1537 spin_lock_irq(&phba->hbalock);
1538 phba->hba_flag &= ~DEFER_ERATT;
1539 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001540 return;
James Smart3772a992009-05-22 14:50:54 -04001541 }
1542
James Smart13815c82008-01-11 01:52:48 -05001543 /* If resets are disabled then leave the HBA alone and return */
1544 if (!phba->cfg_enable_hba_reset)
1545 return;
dea31012005-04-17 16:05:31 -05001546
James Smartea2151b2008-09-07 11:52:10 -04001547 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001548 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001549
James Smarta257bf92009-04-06 18:48:10 -04001550 if (phba->hba_flag & DEFER_ERATT)
1551 lpfc_handle_deferred_eratt(phba);
1552
James Smartdcf2a4e2010-09-29 11:18:53 -04001553 if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1554 if (phba->work_hs & HS_FFER6)
1555 /* Re-establishing Link */
1556 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1557 "1301 Re-establishing Link "
1558 "Data: x%x x%x x%x\n",
1559 phba->work_hs, phba->work_status[0],
1560 phba->work_status[1]);
1561 if (phba->work_hs & HS_FFER8)
1562 /* Device Zeroization */
1563 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1564 "2861 Host Authentication device "
1565 "zeroization Data:x%x x%x x%x\n",
1566 phba->work_hs, phba->work_status[0],
1567 phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001568
James Smart92d7f7b2007-06-17 19:56:38 -05001569 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001570 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001571 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001572
1573 /*
1574 * Firmware stops when it triggled erratt with HS_FFER6.
1575 * That could cause the I/Os dropped by the firmware.
1576 * Error iocb (I/O) on txcmplq and let the SCSI layer
1577 * retry it after re-establishing link.
1578 */
James Smartdb55fba2014-04-04 13:52:02 -04001579 lpfc_sli_abort_fcp_rings(phba);
dea31012005-04-17 16:05:31 -05001580
dea31012005-04-17 16:05:31 -05001581 /*
1582 * There was a firmware error. Take the hba offline and then
1583 * attempt to restart it.
1584 */
James Smart618a5232012-06-12 13:54:36 -04001585 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
dea31012005-04-17 16:05:31 -05001586 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001587 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001588 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001589 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001590 return;
1591 }
James Smart46fa3112007-04-25 09:51:45 -04001592 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001593 } else if (phba->work_hs & HS_CRIT_TEMP) {
1594 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1595 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1596 temp_event_data.event_code = LPFC_CRIT_TEMP;
1597 temp_event_data.data = (uint32_t)temperature;
1598
1599 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001600 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001601 "(%ld), taking this port offline "
1602 "Data: x%x x%x x%x\n",
1603 temperature, phba->work_hs,
1604 phba->work_status[0], phba->work_status[1]);
1605
1606 shost = lpfc_shost_from_vport(phba->pport);
1607 fc_host_post_vendor_event(shost, fc_get_event_number(),
1608 sizeof(temp_event_data),
1609 (char *) &temp_event_data,
1610 SCSI_NL_VID_TYPE_PCI
1611 | PCI_VENDOR_ID_EMULEX);
1612
James Smart7af67052007-10-27 13:38:11 -04001613 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001614 phba->over_temp_state = HBA_OVER_TEMP;
1615 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001616 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001617
dea31012005-04-17 16:05:31 -05001618 } else {
1619 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001620 * failure is a value other than FFER6. Do not call the offline
1621 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001622 */
1623 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001624 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001625 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001626 phba->work_hs,
dea31012005-04-17 16:05:31 -05001627 phba->work_status[0], phba->work_status[1]);
1628
James Smartd2873e42006-08-18 17:46:43 -04001629 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001630 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001631 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001632 sizeof(event_data), (char *) &event_data,
1633 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1634
James Smart09372822008-01-11 01:52:54 -05001635 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001636 }
James Smart93996272008-08-24 21:50:30 -04001637 return;
dea31012005-04-17 16:05:31 -05001638}
1639
James Smarte59058c2008-08-24 21:49:00 -04001640/**
James Smart618a5232012-06-12 13:54:36 -04001641 * lpfc_sli4_port_sta_fn_reset - The SLI4 function reset due to port status reg
1642 * @phba: pointer to lpfc hba data structure.
1643 * @mbx_action: flag for mailbox shutdown action.
1644 *
1645 * This routine is invoked to perform an SLI4 port PCI function reset in
1646 * response to port status register polling attention. It waits for port
1647 * status register (ERR, RDY, RN) bits before proceeding with function reset.
1648 * During this process, interrupt vectors are freed and later requested
1649 * for handling possible port resource change.
1650 **/
1651static int
James Smarte10b2022014-02-20 09:57:43 -05001652lpfc_sli4_port_sta_fn_reset(struct lpfc_hba *phba, int mbx_action,
1653 bool en_rn_msg)
James Smart618a5232012-06-12 13:54:36 -04001654{
1655 int rc;
1656 uint32_t intr_mode;
1657
James Smart65791f12016-07-06 12:35:56 -07001658 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
1659 LPFC_SLI_INTF_IF_TYPE_2) {
1660 /*
1661 * On error status condition, driver need to wait for port
1662 * ready before performing reset.
1663 */
1664 rc = lpfc_sli4_pdev_status_reg_wait(phba);
James Smart0e916ee2016-07-06 12:36:06 -07001665 if (rc)
James Smart65791f12016-07-06 12:35:56 -07001666 return rc;
James Smart618a5232012-06-12 13:54:36 -04001667 }
James Smart0e916ee2016-07-06 12:36:06 -07001668
James Smart65791f12016-07-06 12:35:56 -07001669 /* need reset: attempt for port recovery */
1670 if (en_rn_msg)
1671 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1672 "2887 Reset Needed: Attempting Port "
1673 "Recovery...\n");
1674 lpfc_offline_prep(phba, mbx_action);
1675 lpfc_offline(phba);
1676 /* release interrupt for possible resource change */
1677 lpfc_sli4_disable_intr(phba);
1678 lpfc_sli_brdrestart(phba);
1679 /* request and enable interrupt */
1680 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
1681 if (intr_mode == LPFC_INTR_ERROR) {
1682 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1683 "3175 Failed to enable interrupt\n");
1684 return -EIO;
1685 }
1686 phba->intr_mode = intr_mode;
1687 rc = lpfc_online(phba);
1688 if (rc == 0)
1689 lpfc_unblock_mgmt_io(phba);
1690
James Smart618a5232012-06-12 13:54:36 -04001691 return rc;
1692}
1693
1694/**
James Smartda0436e2009-05-22 14:51:39 -04001695 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1696 * @phba: pointer to lpfc hba data structure.
1697 *
1698 * This routine is invoked to handle the SLI4 HBA hardware error attention
1699 * conditions.
1700 **/
1701static void
1702lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1703{
1704 struct lpfc_vport *vport = phba->pport;
1705 uint32_t event_data;
1706 struct Scsi_Host *shost;
James Smart2fcee4b2010-12-15 17:57:46 -05001707 uint32_t if_type;
James Smart2e90f4b2011-12-13 13:22:37 -05001708 struct lpfc_register portstat_reg = {0};
1709 uint32_t reg_err1, reg_err2;
1710 uint32_t uerrlo_reg, uemasklo_reg;
James Smart65791f12016-07-06 12:35:56 -07001711 uint32_t smphr_port_status = 0, pci_rd_rc1, pci_rd_rc2;
James Smarte10b2022014-02-20 09:57:43 -05001712 bool en_rn_msg = true;
James Smart946727d2015-04-07 15:07:09 -04001713 struct temp_event temp_event_data;
James Smart65791f12016-07-06 12:35:56 -07001714 struct lpfc_register portsmphr_reg;
1715 int rc, i;
James Smartda0436e2009-05-22 14:51:39 -04001716
1717 /* If the pci channel is offline, ignore possible errors, since
1718 * we cannot communicate with the pci card anyway.
1719 */
1720 if (pci_channel_offline(phba->pcidev))
1721 return;
James Smartda0436e2009-05-22 14:51:39 -04001722
James Smart65791f12016-07-06 12:35:56 -07001723 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
James Smart2fcee4b2010-12-15 17:57:46 -05001724 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1725 switch (if_type) {
1726 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart2e90f4b2011-12-13 13:22:37 -05001727 pci_rd_rc1 = lpfc_readl(
1728 phba->sli4_hba.u.if_type0.UERRLOregaddr,
1729 &uerrlo_reg);
1730 pci_rd_rc2 = lpfc_readl(
1731 phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
1732 &uemasklo_reg);
1733 /* consider PCI bus read error as pci_channel_offline */
1734 if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
1735 return;
James Smart65791f12016-07-06 12:35:56 -07001736 if (!(phba->hba_flag & HBA_RECOVERABLE_UE)) {
1737 lpfc_sli4_offline_eratt(phba);
1738 return;
1739 }
1740 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1741 "7623 Checking UE recoverable");
1742
1743 for (i = 0; i < phba->sli4_hba.ue_to_sr / 1000; i++) {
1744 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1745 &portsmphr_reg.word0))
1746 continue;
1747
1748 smphr_port_status = bf_get(lpfc_port_smphr_port_status,
1749 &portsmphr_reg);
1750 if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1751 LPFC_PORT_SEM_UE_RECOVERABLE)
1752 break;
1753 /*Sleep for 1Sec, before checking SEMAPHORE */
1754 msleep(1000);
1755 }
1756
1757 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1758 "4827 smphr_port_status x%x : Waited %dSec",
1759 smphr_port_status, i);
1760
1761 /* Recoverable UE, reset the HBA device */
1762 if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1763 LPFC_PORT_SEM_UE_RECOVERABLE) {
1764 for (i = 0; i < 20; i++) {
1765 msleep(1000);
1766 if (!lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1767 &portsmphr_reg.word0) &&
1768 (LPFC_POST_STAGE_PORT_READY ==
1769 bf_get(lpfc_port_smphr_port_status,
1770 &portsmphr_reg))) {
1771 rc = lpfc_sli4_port_sta_fn_reset(phba,
1772 LPFC_MBX_NO_WAIT, en_rn_msg);
1773 if (rc == 0)
1774 return;
1775 lpfc_printf_log(phba,
1776 KERN_ERR, LOG_INIT,
1777 "4215 Failed to recover UE");
1778 break;
1779 }
1780 }
1781 }
1782 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1783 "7624 Firmware not ready: Failing UE recovery,"
1784 " waited %dSec", i);
James Smart2fcee4b2010-12-15 17:57:46 -05001785 lpfc_sli4_offline_eratt(phba);
1786 break;
James Smart946727d2015-04-07 15:07:09 -04001787
James Smart2fcee4b2010-12-15 17:57:46 -05001788 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart2e90f4b2011-12-13 13:22:37 -05001789 pci_rd_rc1 = lpfc_readl(
1790 phba->sli4_hba.u.if_type2.STATUSregaddr,
1791 &portstat_reg.word0);
1792 /* consider PCI bus read error as pci_channel_offline */
James Smart6b5151f2012-01-18 16:24:06 -05001793 if (pci_rd_rc1 == -EIO) {
1794 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1795 "3151 PCI bus read access failure: x%x\n",
1796 readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
James Smart2e90f4b2011-12-13 13:22:37 -05001797 return;
James Smart6b5151f2012-01-18 16:24:06 -05001798 }
James Smart2e90f4b2011-12-13 13:22:37 -05001799 reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
1800 reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
James Smart2fcee4b2010-12-15 17:57:46 -05001801 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
James Smart2fcee4b2010-12-15 17:57:46 -05001802 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1803 "2889 Port Overtemperature event, "
James Smart946727d2015-04-07 15:07:09 -04001804 "taking port offline Data: x%x x%x\n",
1805 reg_err1, reg_err2);
1806
James Smart310429e2016-07-06 12:35:54 -07001807 phba->sfp_alarm |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
James Smart946727d2015-04-07 15:07:09 -04001808 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1809 temp_event_data.event_code = LPFC_CRIT_TEMP;
1810 temp_event_data.data = 0xFFFFFFFF;
1811
1812 shost = lpfc_shost_from_vport(phba->pport);
1813 fc_host_post_vendor_event(shost, fc_get_event_number(),
1814 sizeof(temp_event_data),
1815 (char *)&temp_event_data,
1816 SCSI_NL_VID_TYPE_PCI
1817 | PCI_VENDOR_ID_EMULEX);
1818
James Smart2fcee4b2010-12-15 17:57:46 -05001819 spin_lock_irq(&phba->hbalock);
1820 phba->over_temp_state = HBA_OVER_TEMP;
1821 spin_unlock_irq(&phba->hbalock);
1822 lpfc_sli4_offline_eratt(phba);
James Smart946727d2015-04-07 15:07:09 -04001823 return;
James Smart2fcee4b2010-12-15 17:57:46 -05001824 }
James Smart2e90f4b2011-12-13 13:22:37 -05001825 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smarte10b2022014-02-20 09:57:43 -05001826 reg_err2 == SLIPORT_ERR2_REG_FW_RESTART) {
James Smart2e90f4b2011-12-13 13:22:37 -05001827 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte10b2022014-02-20 09:57:43 -05001828 "3143 Port Down: Firmware Update "
1829 "Detected\n");
1830 en_rn_msg = false;
1831 } else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smart2e90f4b2011-12-13 13:22:37 -05001832 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1833 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1834 "3144 Port Down: Debug Dump\n");
1835 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1836 reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
1837 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1838 "3145 Port Down: Provisioning\n");
James Smart618a5232012-06-12 13:54:36 -04001839
James Smart946727d2015-04-07 15:07:09 -04001840 /* If resets are disabled then leave the HBA alone and return */
1841 if (!phba->cfg_enable_hba_reset)
1842 return;
1843
James Smart618a5232012-06-12 13:54:36 -04001844 /* Check port status register for function reset */
James Smarte10b2022014-02-20 09:57:43 -05001845 rc = lpfc_sli4_port_sta_fn_reset(phba, LPFC_MBX_NO_WAIT,
1846 en_rn_msg);
James Smart618a5232012-06-12 13:54:36 -04001847 if (rc == 0) {
1848 /* don't report event on forced debug dump */
1849 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1850 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1851 return;
1852 else
1853 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001854 }
James Smart618a5232012-06-12 13:54:36 -04001855 /* fall through for not able to recover */
James Smart6b5151f2012-01-18 16:24:06 -05001856 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1857 "3152 Unrecoverable error, bring the port "
1858 "offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001859 lpfc_sli4_offline_eratt(phba);
1860 break;
1861 case LPFC_SLI_INTF_IF_TYPE_1:
1862 default:
1863 break;
1864 }
James Smart2e90f4b2011-12-13 13:22:37 -05001865 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1866 "3123 Report dump event to upper layer\n");
1867 /* Send an internal error event to mgmt application */
1868 lpfc_board_errevt_to_mgmt(phba);
1869
1870 event_data = FC_REG_DUMP_EVENT;
1871 shost = lpfc_shost_from_vport(vport);
1872 fc_host_post_vendor_event(shost, fc_get_event_number(),
1873 sizeof(event_data), (char *) &event_data,
1874 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
James Smartda0436e2009-05-22 14:51:39 -04001875}
1876
1877/**
1878 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1879 * @phba: pointer to lpfc HBA data structure.
1880 *
1881 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1882 * routine from the API jump table function pointer from the lpfc_hba struct.
1883 *
1884 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001885 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001886 * Any other value - error.
1887 **/
1888void
1889lpfc_handle_eratt(struct lpfc_hba *phba)
1890{
1891 (*phba->lpfc_handle_eratt)(phba);
1892}
1893
1894/**
James Smart3621a712009-04-06 18:47:14 -04001895 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04001896 * @phba: pointer to lpfc hba data structure.
1897 *
1898 * This routine is invoked from the worker thread to handle a HBA host
James Smart895427b2017-02-12 13:52:30 -08001899 * attention link event. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04001900 **/
dea31012005-04-17 16:05:31 -05001901void
James Smart2e0fef82007-06-17 19:56:36 -05001902lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001903{
James Smart2e0fef82007-06-17 19:56:36 -05001904 struct lpfc_vport *vport = phba->pport;
1905 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001906 LPFC_MBOXQ_t *pmb;
1907 volatile uint32_t control;
1908 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05001909 int rc = 0;
dea31012005-04-17 16:05:31 -05001910
1911 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001912 if (!pmb) {
1913 rc = 1;
dea31012005-04-17 16:05:31 -05001914 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05001915 }
dea31012005-04-17 16:05:31 -05001916
1917 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001918 if (!mp) {
1919 rc = 2;
dea31012005-04-17 16:05:31 -05001920 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05001921 }
dea31012005-04-17 16:05:31 -05001922
1923 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05001924 if (!mp->virt) {
1925 rc = 3;
dea31012005-04-17 16:05:31 -05001926 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05001927 }
dea31012005-04-17 16:05:31 -05001928
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05001929 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04001930 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05001931
1932 psli->slistat.link_event++;
James Smart76a95d72010-11-20 23:11:48 -05001933 lpfc_read_topology(phba, pmb, mp);
1934 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smart2e0fef82007-06-17 19:56:36 -05001935 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04001936 /* Block ELS IOCBs until we have processed this mbox command */
James Smart895427b2017-02-12 13:52:30 -08001937 phba->sli.sli3_ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04001938 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05001939 if (rc == MBX_NOT_FINISHED) {
1940 rc = 4;
James Smart14691152006-12-02 13:34:28 -05001941 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05001942 }
dea31012005-04-17 16:05:31 -05001943
1944 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05001945 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001946 writel(HA_LATT, phba->HAregaddr);
1947 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001948 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001949
1950 return;
1951
James Smart14691152006-12-02 13:34:28 -05001952lpfc_handle_latt_free_mbuf:
James Smart895427b2017-02-12 13:52:30 -08001953 phba->sli.sli3_ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05001954 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001955lpfc_handle_latt_free_mp:
1956 kfree(mp);
1957lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04001958 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001959lpfc_handle_latt_err_exit:
1960 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001961 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001962 psli->sli_flag |= LPFC_PROCESS_LA;
1963 control = readl(phba->HCregaddr);
1964 control |= HC_LAINT_ENA;
1965 writel(control, phba->HCregaddr);
1966 readl(phba->HCregaddr); /* flush */
1967
1968 /* Clear Link Attention in HA REG */
1969 writel(HA_LATT, phba->HAregaddr);
1970 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001971 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001972 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001973 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001974
James Smart09372822008-01-11 01:52:54 -05001975 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1976 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05001977
1978 return;
1979}
1980
James Smarte59058c2008-08-24 21:49:00 -04001981/**
James Smart3621a712009-04-06 18:47:14 -04001982 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04001983 * @phba: pointer to lpfc hba data structure.
1984 * @vpd: pointer to the vital product data.
1985 * @len: length of the vital product data in bytes.
1986 *
1987 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1988 * an array of characters. In this routine, the ModelName, ProgramType, and
1989 * ModelDesc, etc. fields of the phba data structure will be populated.
1990 *
1991 * Return codes
1992 * 0 - pointer to the VPD passed in is NULL
1993 * 1 - success
1994 **/
James Smart3772a992009-05-22 14:50:54 -04001995int
James Smart2e0fef82007-06-17 19:56:36 -05001996lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05001997{
1998 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05001999 int Length;
dea31012005-04-17 16:05:31 -05002000 int i, j;
2001 int finished = 0;
2002 int index = 0;
2003
2004 if (!vpd)
2005 return 0;
2006
2007 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05002008 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002009 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05002010 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
2011 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002012 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05002013 switch (vpd[index]) {
2014 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002015 case 0x91:
dea31012005-04-17 16:05:31 -05002016 index += 1;
2017 lenlo = vpd[index];
2018 index += 1;
2019 lenhi = vpd[index];
2020 index += 1;
2021 i = ((((unsigned short)lenhi) << 8) + lenlo);
2022 index += i;
2023 break;
2024 case 0x90:
2025 index += 1;
2026 lenlo = vpd[index];
2027 index += 1;
2028 lenhi = vpd[index];
2029 index += 1;
2030 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002031 if (Length > len - index)
2032 Length = len - index;
dea31012005-04-17 16:05:31 -05002033 while (Length > 0) {
2034 /* Look for Serial Number */
2035 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
2036 index += 2;
2037 i = vpd[index];
2038 index += 1;
2039 j = 0;
2040 Length -= (3+i);
2041 while(i--) {
2042 phba->SerialNumber[j++] = vpd[index++];
2043 if (j == 31)
2044 break;
2045 }
2046 phba->SerialNumber[j] = 0;
2047 continue;
2048 }
2049 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
2050 phba->vpd_flag |= VPD_MODEL_DESC;
2051 index += 2;
2052 i = vpd[index];
2053 index += 1;
2054 j = 0;
2055 Length -= (3+i);
2056 while(i--) {
2057 phba->ModelDesc[j++] = vpd[index++];
2058 if (j == 255)
2059 break;
2060 }
2061 phba->ModelDesc[j] = 0;
2062 continue;
2063 }
2064 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
2065 phba->vpd_flag |= VPD_MODEL_NAME;
2066 index += 2;
2067 i = vpd[index];
2068 index += 1;
2069 j = 0;
2070 Length -= (3+i);
2071 while(i--) {
2072 phba->ModelName[j++] = vpd[index++];
2073 if (j == 79)
2074 break;
2075 }
2076 phba->ModelName[j] = 0;
2077 continue;
2078 }
2079 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
2080 phba->vpd_flag |= VPD_PROGRAM_TYPE;
2081 index += 2;
2082 i = vpd[index];
2083 index += 1;
2084 j = 0;
2085 Length -= (3+i);
2086 while(i--) {
2087 phba->ProgramType[j++] = vpd[index++];
2088 if (j == 255)
2089 break;
2090 }
2091 phba->ProgramType[j] = 0;
2092 continue;
2093 }
2094 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
2095 phba->vpd_flag |= VPD_PORT;
2096 index += 2;
2097 i = vpd[index];
2098 index += 1;
2099 j = 0;
2100 Length -= (3+i);
2101 while(i--) {
James Smartcd1c8302011-10-10 21:33:25 -04002102 if ((phba->sli_rev == LPFC_SLI_REV4) &&
2103 (phba->sli4_hba.pport_name_sta ==
2104 LPFC_SLI4_PPNAME_GET)) {
2105 j++;
2106 index++;
2107 } else
2108 phba->Port[j++] = vpd[index++];
2109 if (j == 19)
2110 break;
dea31012005-04-17 16:05:31 -05002111 }
James Smartcd1c8302011-10-10 21:33:25 -04002112 if ((phba->sli_rev != LPFC_SLI_REV4) ||
2113 (phba->sli4_hba.pport_name_sta ==
2114 LPFC_SLI4_PPNAME_NON))
2115 phba->Port[j] = 0;
dea31012005-04-17 16:05:31 -05002116 continue;
2117 }
2118 else {
2119 index += 2;
2120 i = vpd[index];
2121 index += 1;
2122 index += i;
2123 Length -= (3 + i);
2124 }
2125 }
2126 finished = 0;
2127 break;
2128 case 0x78:
2129 finished = 1;
2130 break;
2131 default:
2132 index ++;
2133 break;
2134 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002135 }
dea31012005-04-17 16:05:31 -05002136
2137 return(1);
2138}
2139
James Smarte59058c2008-08-24 21:49:00 -04002140/**
James Smart3621a712009-04-06 18:47:14 -04002141 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04002142 * @phba: pointer to lpfc hba data structure.
2143 * @mdp: pointer to the data structure to hold the derived model name.
2144 * @descp: pointer to the data structure to hold the derived description.
2145 *
2146 * This routine retrieves HBA's description based on its registered PCI device
2147 * ID. The @descp passed into this function points to an array of 256 chars. It
2148 * shall be returned with the model name, maximum speed, and the host bus type.
2149 * The @mdp passed into this function points to an array of 80 chars. When the
2150 * function returns, the @mdp will be filled with the model name.
2151 **/
dea31012005-04-17 16:05:31 -05002152static void
James Smart2e0fef82007-06-17 19:56:36 -05002153lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05002154{
2155 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05002156 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002157 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04002158 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04002159 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002160 struct {
James Smarta747c9c2009-11-18 15:41:10 -05002161 char *name;
2162 char *bus;
2163 char *function;
2164 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002165
2166 if (mdp && mdp[0] != '\0'
2167 && descp && descp[0] != '\0')
2168 return;
2169
James Smartd38dd522015-08-31 16:48:17 -04002170 if (phba->lmt & LMT_32Gb)
2171 max_speed = 32;
2172 else if (phba->lmt & LMT_16Gb)
James Smartc0c11512011-05-24 11:41:34 -04002173 max_speed = 16;
2174 else if (phba->lmt & LMT_10Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002175 max_speed = 10;
2176 else if (phba->lmt & LMT_8Gb)
2177 max_speed = 8;
2178 else if (phba->lmt & LMT_4Gb)
2179 max_speed = 4;
2180 else if (phba->lmt & LMT_2Gb)
2181 max_speed = 2;
James Smart4169d862012-09-29 11:32:09 -04002182 else if (phba->lmt & LMT_1Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002183 max_speed = 1;
James Smart4169d862012-09-29 11:32:09 -04002184 else
2185 max_speed = 0;
dea31012005-04-17 16:05:31 -05002186
2187 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05002188
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002189 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002190 case PCI_DEVICE_ID_FIREFLY:
James Smart12222f42014-05-21 08:05:19 -04002191 m = (typeof(m)){"LP6000", "PCI",
2192 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002193 break;
dea31012005-04-17 16:05:31 -05002194 case PCI_DEVICE_ID_SUPERFLY:
2195 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smart12222f42014-05-21 08:05:19 -04002196 m = (typeof(m)){"LP7000", "PCI", ""};
dea31012005-04-17 16:05:31 -05002197 else
James Smart12222f42014-05-21 08:05:19 -04002198 m = (typeof(m)){"LP7000E", "PCI", ""};
2199 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
dea31012005-04-17 16:05:31 -05002200 break;
2201 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002202 m = (typeof(m)){"LP8000", "PCI",
James Smart12222f42014-05-21 08:05:19 -04002203 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002204 break;
2205 case PCI_DEVICE_ID_CENTAUR:
2206 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smart12222f42014-05-21 08:05:19 -04002207 m = (typeof(m)){"LP9002", "PCI", ""};
dea31012005-04-17 16:05:31 -05002208 else
James Smart12222f42014-05-21 08:05:19 -04002209 m = (typeof(m)){"LP9000", "PCI", ""};
2210 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
dea31012005-04-17 16:05:31 -05002211 break;
2212 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002213 m = (typeof(m)){"LP952", "PCI",
James Smart12222f42014-05-21 08:05:19 -04002214 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002215 break;
2216 case PCI_DEVICE_ID_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05002217 m = (typeof(m)){"LP9802", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002218 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002219 break;
2220 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05002221 m = (typeof(m)){"LP10000", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002222 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002223 break;
2224 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05002225 m = (typeof(m)){"LPX1000", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002226 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002227 break;
2228 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002229 m = (typeof(m)){"LP982", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002230 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002231 break;
2232 case PCI_DEVICE_ID_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002233 m = (typeof(m)){"LP1050", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002234 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002235 break;
2236 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05002237 m = (typeof(m)){"LP11000", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002238 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002239 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002240 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002241 m = (typeof(m)){"LP11000-SP", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002242 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002243 break;
2244 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002245 m = (typeof(m)){"LP11002-SP", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002246 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002247 break;
2248 case PCI_DEVICE_ID_NEPTUNE:
James Smart12222f42014-05-21 08:05:19 -04002249 m = (typeof(m)){"LPe1000", "PCIe",
2250 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002251 break;
2252 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smart12222f42014-05-21 08:05:19 -04002253 m = (typeof(m)){"LPe1000-SP", "PCIe",
2254 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002255 break;
2256 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smart12222f42014-05-21 08:05:19 -04002257 m = (typeof(m)){"LPe1002-SP", "PCIe",
2258 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002259 break;
dea31012005-04-17 16:05:31 -05002260 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05002261 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002262 break;
2263 case PCI_DEVICE_ID_BSMB:
James Smart12222f42014-05-21 08:05:19 -04002264 m = (typeof(m)){"LP111", "PCI-X2",
2265 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002266 break;
2267 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05002268 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002269 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002270 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002271 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002272 break;
2273 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002274 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04002275 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002276 break;
dea31012005-04-17 16:05:31 -05002277 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05002278 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002279 break;
2280 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05002281 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002282 break;
2283 case PCI_DEVICE_ID_LP101:
James Smart12222f42014-05-21 08:05:19 -04002284 m = (typeof(m)){"LP101", "PCI-X",
2285 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002286 break;
2287 case PCI_DEVICE_ID_LP10000S:
James Smart12222f42014-05-21 08:05:19 -04002288 m = (typeof(m)){"LP10000-S", "PCI",
2289 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002290 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002291 case PCI_DEVICE_ID_LP11000S:
James Smart12222f42014-05-21 08:05:19 -04002292 m = (typeof(m)){"LP11000-S", "PCI-X2",
2293 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05002294 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002295 case PCI_DEVICE_ID_LPE11000S:
James Smart12222f42014-05-21 08:05:19 -04002296 m = (typeof(m)){"LPe11000-S", "PCIe",
2297 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002298 break;
James Smartb87eab32007-04-25 09:53:28 -04002299 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05002300 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002301 break;
2302 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05002303 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002304 break;
2305 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05002306 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002307 break;
2308 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002309 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002310 break;
2311 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002312 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002313 break;
2314 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05002315 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002316 break;
James Smart84774a42008-08-24 21:50:06 -04002317 case PCI_DEVICE_ID_HORNET:
James Smart12222f42014-05-21 08:05:19 -04002318 m = (typeof(m)){"LP21000", "PCIe",
2319 "Obsolete, Unsupported FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002320 GE = 1;
2321 break;
2322 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05002323 m = (typeof(m)){"LPev12000", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002324 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002325 break;
2326 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05002327 m = (typeof(m)){"LPev12000", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002328 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002329 break;
2330 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05002331 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002332 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002333 break;
James Smartda0436e2009-05-22 14:51:39 -04002334 case PCI_DEVICE_ID_TIGERSHARK:
2335 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002336 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04002337 break;
James Smarta747c9c2009-11-18 15:41:10 -05002338 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04002339 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002340 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
2341 break;
2342 case PCI_DEVICE_ID_FALCON:
2343 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
2344 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04002345 break;
James Smart98fc5dd2010-06-07 15:24:29 -04002346 case PCI_DEVICE_ID_BALIUS:
2347 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
James Smart12222f42014-05-21 08:05:19 -04002348 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart98fc5dd2010-06-07 15:24:29 -04002349 break;
James Smart085c6472010-11-20 23:11:37 -05002350 case PCI_DEVICE_ID_LANCER_FC:
James Smartc0c11512011-05-24 11:41:34 -04002351 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
James Smart085c6472010-11-20 23:11:37 -05002352 break;
James Smart12222f42014-05-21 08:05:19 -04002353 case PCI_DEVICE_ID_LANCER_FC_VF:
2354 m = (typeof(m)){"LPe16000", "PCIe",
2355 "Obsolete, Unsupported Fibre Channel Adapter"};
2356 break;
James Smart085c6472010-11-20 23:11:37 -05002357 case PCI_DEVICE_ID_LANCER_FCOE:
2358 oneConnect = 1;
James Smart079b5c92011-08-21 21:48:49 -04002359 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
James Smart085c6472010-11-20 23:11:37 -05002360 break;
James Smart12222f42014-05-21 08:05:19 -04002361 case PCI_DEVICE_ID_LANCER_FCOE_VF:
2362 oneConnect = 1;
2363 m = (typeof(m)){"OCe15100", "PCIe",
2364 "Obsolete, Unsupported FCoE"};
2365 break;
James Smartd38dd522015-08-31 16:48:17 -04002366 case PCI_DEVICE_ID_LANCER_G6_FC:
2367 m = (typeof(m)){"LPe32000", "PCIe", "Fibre Channel Adapter"};
2368 break;
James Smartf8cafd32012-08-03 12:42:51 -04002369 case PCI_DEVICE_ID_SKYHAWK:
2370 case PCI_DEVICE_ID_SKYHAWK_VF:
2371 oneConnect = 1;
2372 m = (typeof(m)){"OCe14000", "PCIe", "FCoE"};
2373 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002374 default:
James Smarta747c9c2009-11-18 15:41:10 -05002375 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002376 break;
dea31012005-04-17 16:05:31 -05002377 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002378
2379 if (mdp && mdp[0] == '\0')
2380 snprintf(mdp, 79,"%s", m.name);
James Smartc0c11512011-05-24 11:41:34 -04002381 /*
2382 * oneConnect hba requires special processing, they are all initiators
James Smartda0436e2009-05-22 14:51:39 -04002383 * and we put the port number on the end
2384 */
2385 if (descp && descp[0] == '\0') {
2386 if (oneConnect)
2387 snprintf(descp, 255,
James Smart4169d862012-09-29 11:32:09 -04002388 "Emulex OneConnect %s, %s Initiator %s",
James Smarta747c9c2009-11-18 15:41:10 -05002389 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04002390 phba->Port);
James Smart4169d862012-09-29 11:32:09 -04002391 else if (max_speed == 0)
2392 snprintf(descp, 255,
Sebastian Herbszt290237d2015-08-31 16:48:08 -04002393 "Emulex %s %s %s",
James Smart4169d862012-09-29 11:32:09 -04002394 m.name, m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002395 else
2396 snprintf(descp, 255,
2397 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05002398 m.name, max_speed, (GE) ? "GE" : "Gb",
2399 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002400 }
dea31012005-04-17 16:05:31 -05002401}
2402
James Smarte59058c2008-08-24 21:49:00 -04002403/**
James Smart3621a712009-04-06 18:47:14 -04002404 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04002405 * @phba: pointer to lpfc hba data structure.
2406 * @pring: pointer to a IOCB ring.
2407 * @cnt: the number of IOCBs to be posted to the IOCB ring.
2408 *
2409 * This routine posts a given number of IOCBs with the associated DMA buffer
2410 * descriptors specified by the cnt argument to the given IOCB ring.
2411 *
2412 * Return codes
2413 * The number of IOCBs NOT able to be posted to the IOCB ring.
2414 **/
dea31012005-04-17 16:05:31 -05002415int
James Smart495a7142008-06-14 22:52:59 -04002416lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05002417{
2418 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002419 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05002420 struct lpfc_dmabuf *mp1, *mp2;
2421
2422 cnt += pring->missbufcnt;
2423
2424 /* While there are buffers to post */
2425 while (cnt > 0) {
2426 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002427 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002428 if (iocb == NULL) {
2429 pring->missbufcnt = cnt;
2430 return cnt;
2431 }
dea31012005-04-17 16:05:31 -05002432 icmd = &iocb->iocb;
2433
2434 /* 2 buffers can be posted per command */
2435 /* Allocate buffer to post */
2436 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2437 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04002438 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2439 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002440 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002441 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002442 pring->missbufcnt = cnt;
2443 return cnt;
2444 }
2445
2446 INIT_LIST_HEAD(&mp1->list);
2447 /* Allocate buffer to post */
2448 if (cnt > 1) {
2449 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2450 if (mp2)
2451 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2452 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04002453 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002454 kfree(mp2);
dea31012005-04-17 16:05:31 -05002455 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2456 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002457 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002458 pring->missbufcnt = cnt;
2459 return cnt;
2460 }
2461
2462 INIT_LIST_HEAD(&mp2->list);
2463 } else {
2464 mp2 = NULL;
2465 }
2466
2467 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2468 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2469 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2470 icmd->ulpBdeCount = 1;
2471 cnt--;
2472 if (mp2) {
2473 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2474 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2475 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2476 cnt--;
2477 icmd->ulpBdeCount = 2;
2478 }
2479
2480 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2481 icmd->ulpLe = 1;
2482
James Smart3772a992009-05-22 14:50:54 -04002483 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2484 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05002485 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2486 kfree(mp1);
2487 cnt++;
2488 if (mp2) {
2489 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2490 kfree(mp2);
2491 cnt++;
2492 }
James Bottomley604a3e32005-10-29 10:28:33 -05002493 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002494 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05002495 return cnt;
2496 }
dea31012005-04-17 16:05:31 -05002497 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05002498 if (mp2)
dea31012005-04-17 16:05:31 -05002499 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05002500 }
2501 pring->missbufcnt = 0;
2502 return 0;
2503}
2504
James Smarte59058c2008-08-24 21:49:00 -04002505/**
James Smart3621a712009-04-06 18:47:14 -04002506 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04002507 * @phba: pointer to lpfc hba data structure.
2508 *
2509 * This routine posts initial receive IOCB buffers to the ELS ring. The
2510 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
James Smart895427b2017-02-12 13:52:30 -08002511 * set to 64 IOCBs. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04002512 *
2513 * Return codes
2514 * 0 - success (currently always success)
2515 **/
dea31012005-04-17 16:05:31 -05002516static int
James Smart2e0fef82007-06-17 19:56:36 -05002517lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002518{
2519 struct lpfc_sli *psli = &phba->sli;
2520
2521 /* Ring 0, ELS / CT buffers */
James Smart895427b2017-02-12 13:52:30 -08002522 lpfc_post_buffer(phba, &psli->sli3_ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05002523 /* Ring 2 - FCP no buffers needed */
2524
2525 return 0;
2526}
2527
2528#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2529
James Smarte59058c2008-08-24 21:49:00 -04002530/**
James Smart3621a712009-04-06 18:47:14 -04002531 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04002532 * @HashResultPointer: pointer to an array as hash table.
2533 *
2534 * This routine sets up the initial values to the array of hash table entries
2535 * for the LC HBAs.
2536 **/
dea31012005-04-17 16:05:31 -05002537static void
2538lpfc_sha_init(uint32_t * HashResultPointer)
2539{
2540 HashResultPointer[0] = 0x67452301;
2541 HashResultPointer[1] = 0xEFCDAB89;
2542 HashResultPointer[2] = 0x98BADCFE;
2543 HashResultPointer[3] = 0x10325476;
2544 HashResultPointer[4] = 0xC3D2E1F0;
2545}
2546
James Smarte59058c2008-08-24 21:49:00 -04002547/**
James Smart3621a712009-04-06 18:47:14 -04002548 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04002549 * @HashResultPointer: pointer to an initial/result hash table.
2550 * @HashWorkingPointer: pointer to an working hash table.
2551 *
2552 * This routine iterates an initial hash table pointed by @HashResultPointer
2553 * with the values from the working hash table pointeed by @HashWorkingPointer.
2554 * The results are putting back to the initial hash table, returned through
2555 * the @HashResultPointer as the result hash table.
2556 **/
dea31012005-04-17 16:05:31 -05002557static void
2558lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2559{
2560 int t;
2561 uint32_t TEMP;
2562 uint32_t A, B, C, D, E;
2563 t = 16;
2564 do {
2565 HashWorkingPointer[t] =
2566 S(1,
2567 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2568 8] ^
2569 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2570 } while (++t <= 79);
2571 t = 0;
2572 A = HashResultPointer[0];
2573 B = HashResultPointer[1];
2574 C = HashResultPointer[2];
2575 D = HashResultPointer[3];
2576 E = HashResultPointer[4];
2577
2578 do {
2579 if (t < 20) {
2580 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2581 } else if (t < 40) {
2582 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2583 } else if (t < 60) {
2584 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2585 } else {
2586 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2587 }
2588 TEMP += S(5, A) + E + HashWorkingPointer[t];
2589 E = D;
2590 D = C;
2591 C = S(30, B);
2592 B = A;
2593 A = TEMP;
2594 } while (++t <= 79);
2595
2596 HashResultPointer[0] += A;
2597 HashResultPointer[1] += B;
2598 HashResultPointer[2] += C;
2599 HashResultPointer[3] += D;
2600 HashResultPointer[4] += E;
2601
2602}
2603
James Smarte59058c2008-08-24 21:49:00 -04002604/**
James Smart3621a712009-04-06 18:47:14 -04002605 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04002606 * @RandomChallenge: pointer to the entry of host challenge random number array.
2607 * @HashWorking: pointer to the entry of the working hash array.
2608 *
2609 * This routine calculates the working hash array referred by @HashWorking
2610 * from the challenge random numbers associated with the host, referred by
2611 * @RandomChallenge. The result is put into the entry of the working hash
2612 * array and returned by reference through @HashWorking.
2613 **/
dea31012005-04-17 16:05:31 -05002614static void
2615lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2616{
2617 *HashWorking = (*RandomChallenge ^ *HashWorking);
2618}
2619
James Smarte59058c2008-08-24 21:49:00 -04002620/**
James Smart3621a712009-04-06 18:47:14 -04002621 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04002622 * @phba: pointer to lpfc hba data structure.
2623 * @hbainit: pointer to an array of unsigned 32-bit integers.
2624 *
2625 * This routine performs the special handling for LC HBA initialization.
2626 **/
dea31012005-04-17 16:05:31 -05002627void
2628lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2629{
2630 int t;
2631 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05002632 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05002633
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002634 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002635 if (!HashWorking)
2636 return;
2637
dea31012005-04-17 16:05:31 -05002638 HashWorking[0] = HashWorking[78] = *pwwnn++;
2639 HashWorking[1] = HashWorking[79] = *pwwnn;
2640
2641 for (t = 0; t < 7; t++)
2642 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2643
2644 lpfc_sha_init(hbainit);
2645 lpfc_sha_iterate(hbainit, HashWorking);
2646 kfree(HashWorking);
2647}
2648
James Smarte59058c2008-08-24 21:49:00 -04002649/**
James Smart3621a712009-04-06 18:47:14 -04002650 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04002651 * @vport: pointer to a virtual N_Port data structure.
2652 *
2653 * This routine performs the necessary cleanups before deleting the @vport.
2654 * It invokes the discovery state machine to perform necessary state
2655 * transitions and to release the ndlps associated with the @vport. Note,
2656 * the physical port is treated as @vport 0.
2657 **/
James Smart87af33f2007-10-27 13:37:43 -04002658void
James Smart2e0fef82007-06-17 19:56:36 -05002659lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002660{
James Smart87af33f2007-10-27 13:37:43 -04002661 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002662 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04002663 int i = 0;
dea31012005-04-17 16:05:31 -05002664
James Smart87af33f2007-10-27 13:37:43 -04002665 if (phba->link_state > LPFC_LINK_DOWN)
2666 lpfc_port_link_failure(vport);
2667
2668 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002669 if (!NLP_CHK_NODE_ACT(ndlp)) {
2670 ndlp = lpfc_enable_node(vport, ndlp,
2671 NLP_STE_UNUSED_NODE);
2672 if (!ndlp)
2673 continue;
2674 spin_lock_irq(&phba->ndlp_lock);
2675 NLP_SET_FREE_REQ(ndlp);
2676 spin_unlock_irq(&phba->ndlp_lock);
2677 /* Trigger the release of the ndlp memory */
2678 lpfc_nlp_put(ndlp);
2679 continue;
2680 }
2681 spin_lock_irq(&phba->ndlp_lock);
2682 if (NLP_CHK_FREE_REQ(ndlp)) {
2683 /* The ndlp should not be in memory free mode already */
2684 spin_unlock_irq(&phba->ndlp_lock);
2685 continue;
2686 } else
2687 /* Indicate request for freeing ndlp memory */
2688 NLP_SET_FREE_REQ(ndlp);
2689 spin_unlock_irq(&phba->ndlp_lock);
2690
James Smart58da1ff2008-04-07 10:15:56 -04002691 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2692 ndlp->nlp_DID == Fabric_DID) {
2693 /* Just free up ndlp with Fabric_DID for vports */
2694 lpfc_nlp_put(ndlp);
2695 continue;
2696 }
2697
James Smarteff4a012012-01-18 16:25:25 -05002698 /* take care of nodes in unused state before the state
2699 * machine taking action.
2700 */
2701 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
2702 lpfc_nlp_put(ndlp);
2703 continue;
2704 }
2705
James Smart87af33f2007-10-27 13:37:43 -04002706 if (ndlp->nlp_type & NLP_FABRIC)
2707 lpfc_disc_state_machine(vport, ndlp, NULL,
2708 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002709
James Smarta0f2d3e2017-02-12 13:52:31 -08002710 if (ndlp->nlp_fc4_type & NLP_FC4_NVME) {
2711 /* Remove the NVME transport reference now and
2712 * continue to remove the node.
2713 */
2714 lpfc_nlp_put(ndlp);
2715 }
2716
James Smart87af33f2007-10-27 13:37:43 -04002717 lpfc_disc_state_machine(vport, ndlp, NULL,
2718 NLP_EVT_DEVICE_RM);
2719 }
2720
James Smarta8adb832007-10-27 13:37:53 -04002721 /* At this point, ALL ndlp's should be gone
2722 * because of the previous NLP_EVT_DEVICE_RM.
2723 * Lets wait for this to happen, if needed.
2724 */
James Smart87af33f2007-10-27 13:37:43 -04002725 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002726 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002727 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002728 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002729 list_for_each_entry_safe(ndlp, next_ndlp,
2730 &vport->fc_nodes, nlp_listp) {
2731 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2732 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002733 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002734 "usgmap:x%x refcnt:%d\n",
2735 ndlp->nlp_DID, (void *)ndlp,
2736 ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01002737 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05002738 }
James Smarta8adb832007-10-27 13:37:53 -04002739 break;
James Smart87af33f2007-10-27 13:37:43 -04002740 }
James Smarta8adb832007-10-27 13:37:53 -04002741
2742 /* Wait for any activity on ndlps to settle */
2743 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002744 }
James Smart1151e3e2011-02-16 12:39:35 -05002745 lpfc_cleanup_vports_rrqs(vport, NULL);
dea31012005-04-17 16:05:31 -05002746}
2747
James Smarte59058c2008-08-24 21:49:00 -04002748/**
James Smart3621a712009-04-06 18:47:14 -04002749 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002750 * @vport: pointer to a virtual N_Port data structure.
2751 *
2752 * This routine stops all the timers associated with a @vport. This function
2753 * is invoked before disabling or deleting a @vport. Note that the physical
2754 * port is treated as @vport 0.
2755 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002756void
2757lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002758{
James Smart92d7f7b2007-06-17 19:56:38 -05002759 del_timer_sync(&vport->els_tmofunc);
James Smart92494142011-02-16 12:39:44 -05002760 del_timer_sync(&vport->delayed_disc_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002761 lpfc_can_disctmo(vport);
2762 return;
dea31012005-04-17 16:05:31 -05002763}
2764
James Smarte59058c2008-08-24 21:49:00 -04002765/**
James Smartecfd03c2010-02-12 14:41:27 -05002766 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2767 * @phba: pointer to lpfc hba data structure.
2768 *
2769 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2770 * caller of this routine should already hold the host lock.
2771 **/
2772void
2773__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2774{
James Smart5ac6b302010-10-22 11:05:36 -04002775 /* Clear pending FCF rediscovery wait flag */
2776 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2777
James Smartecfd03c2010-02-12 14:41:27 -05002778 /* Now, try to stop the timer */
2779 del_timer(&phba->fcf.redisc_wait);
2780}
2781
2782/**
2783 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2784 * @phba: pointer to lpfc hba data structure.
2785 *
2786 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2787 * checks whether the FCF rediscovery wait timer is pending with the host
2788 * lock held before proceeding with disabling the timer and clearing the
2789 * wait timer pendig flag.
2790 **/
2791void
2792lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2793{
2794 spin_lock_irq(&phba->hbalock);
2795 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2796 /* FCF rediscovery timer already fired or stopped */
2797 spin_unlock_irq(&phba->hbalock);
2798 return;
2799 }
2800 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart5ac6b302010-10-22 11:05:36 -04002801 /* Clear failover in progress flags */
2802 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
James Smartecfd03c2010-02-12 14:41:27 -05002803 spin_unlock_irq(&phba->hbalock);
2804}
2805
2806/**
James Smart3772a992009-05-22 14:50:54 -04002807 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002808 * @phba: pointer to lpfc hba data structure.
2809 *
2810 * This routine stops all the timers associated with a HBA. This function is
2811 * invoked before either putting a HBA offline or unloading the driver.
2812 **/
James Smart3772a992009-05-22 14:50:54 -04002813void
2814lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002815{
James Smart51ef4c22007-08-02 11:10:31 -04002816 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05002817 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002818 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002819 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002820 del_timer_sync(&phba->hb_tmofunc);
James Smart1151e3e2011-02-16 12:39:35 -05002821 if (phba->sli_rev == LPFC_SLI_REV4) {
2822 del_timer_sync(&phba->rrq_tmr);
2823 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2824 }
James Smart3772a992009-05-22 14:50:54 -04002825 phba->hb_outstanding = 0;
2826
2827 switch (phba->pci_dev_grp) {
2828 case LPFC_PCI_DEV_LP:
2829 /* Stop any LightPulse device specific driver timers */
2830 del_timer_sync(&phba->fcp_poll_timer);
2831 break;
2832 case LPFC_PCI_DEV_OC:
2833 /* Stop any OneConnect device sepcific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05002834 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04002835 break;
2836 default:
2837 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2838 "0297 Invalid device group (x%x)\n",
2839 phba->pci_dev_grp);
2840 break;
2841 }
James Smart2e0fef82007-06-17 19:56:36 -05002842 return;
dea31012005-04-17 16:05:31 -05002843}
2844
James Smarte59058c2008-08-24 21:49:00 -04002845/**
James Smart3621a712009-04-06 18:47:14 -04002846 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002847 * @phba: pointer to lpfc hba data structure.
2848 *
2849 * This routine marks a HBA's management interface as blocked. Once the HBA's
2850 * management interface is marked as blocked, all the user space access to
2851 * the HBA, whether they are from sysfs interface or libdfc interface will
2852 * all be blocked. The HBA is set to block the management interface when the
2853 * driver prepares the HBA interface for online or offline.
2854 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002855static void
James Smart618a5232012-06-12 13:54:36 -04002856lpfc_block_mgmt_io(struct lpfc_hba *phba, int mbx_action)
Adrian Bunka6ababd2007-11-05 18:07:33 +01002857{
2858 unsigned long iflag;
James Smart6e7288d2010-06-07 15:23:35 -04002859 uint8_t actcmd = MBX_HEARTBEAT;
2860 unsigned long timeout;
2861
Adrian Bunka6ababd2007-11-05 18:07:33 +01002862 spin_lock_irqsave(&phba->hbalock, iflag);
2863 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
James Smart618a5232012-06-12 13:54:36 -04002864 spin_unlock_irqrestore(&phba->hbalock, iflag);
2865 if (mbx_action == LPFC_MBX_NO_WAIT)
2866 return;
2867 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
2868 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002869 if (phba->sli.mbox_active) {
James Smart6e7288d2010-06-07 15:23:35 -04002870 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
James Smarta183a152011-10-10 21:32:43 -04002871 /* Determine how long we might wait for the active mailbox
2872 * command to be gracefully completed by firmware.
2873 */
2874 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
2875 phba->sli.mbox_active) * 1000) + jiffies;
2876 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002877 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002878
James Smart6e7288d2010-06-07 15:23:35 -04002879 /* Wait for the outstnading mailbox command to complete */
2880 while (phba->sli.mbox_active) {
2881 /* Check active mailbox complete status every 2ms */
2882 msleep(2);
2883 if (time_after(jiffies, timeout)) {
2884 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2885 "2813 Mgmt IO is Blocked %x "
2886 "- mbox cmd %x still active\n",
2887 phba->sli.sli_flag, actcmd);
2888 break;
2889 }
2890 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002891}
2892
James Smarte59058c2008-08-24 21:49:00 -04002893/**
James Smart6b5151f2012-01-18 16:24:06 -05002894 * lpfc_sli4_node_prep - Assign RPIs for active nodes.
2895 * @phba: pointer to lpfc hba data structure.
2896 *
2897 * Allocate RPIs for all active remote nodes. This is needed whenever
2898 * an SLI4 adapter is reset and the driver is not unloading. Its purpose
2899 * is to fixup the temporary rpi assignments.
2900 **/
2901void
2902lpfc_sli4_node_prep(struct lpfc_hba *phba)
2903{
2904 struct lpfc_nodelist *ndlp, *next_ndlp;
2905 struct lpfc_vport **vports;
James Smart9d3d3402017-04-21 16:05:00 -07002906 int i, rpi;
2907 unsigned long flags;
James Smart6b5151f2012-01-18 16:24:06 -05002908
2909 if (phba->sli_rev != LPFC_SLI_REV4)
2910 return;
2911
2912 vports = lpfc_create_vport_work_array(phba);
James Smart9d3d3402017-04-21 16:05:00 -07002913 if (vports == NULL)
2914 return;
James Smart6b5151f2012-01-18 16:24:06 -05002915
James Smart9d3d3402017-04-21 16:05:00 -07002916 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2917 if (vports[i]->load_flag & FC_UNLOADING)
2918 continue;
2919
2920 list_for_each_entry_safe(ndlp, next_ndlp,
2921 &vports[i]->fc_nodes,
2922 nlp_listp) {
2923 if (!NLP_CHK_NODE_ACT(ndlp))
2924 continue;
2925 rpi = lpfc_sli4_alloc_rpi(phba);
2926 if (rpi == LPFC_RPI_ALLOC_ERROR) {
2927 spin_lock_irqsave(&phba->ndlp_lock, flags);
2928 NLP_CLR_NODE_ACT(ndlp);
2929 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
2930 continue;
James Smart6b5151f2012-01-18 16:24:06 -05002931 }
James Smart9d3d3402017-04-21 16:05:00 -07002932 ndlp->nlp_rpi = rpi;
2933 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
2934 "0009 rpi:%x DID:%x "
2935 "flg:%x map:%x %p\n", ndlp->nlp_rpi,
2936 ndlp->nlp_DID, ndlp->nlp_flag,
2937 ndlp->nlp_usg_map, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -05002938 }
2939 }
2940 lpfc_destroy_vport_work_array(phba, vports);
2941}
2942
2943/**
James Smart3621a712009-04-06 18:47:14 -04002944 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04002945 * @phba: pointer to lpfc hba data structure.
2946 *
2947 * This routine initializes the HBA and brings a HBA online. During this
2948 * process, the management interface is blocked to prevent user space access
2949 * to the HBA interfering with the driver initialization.
2950 *
2951 * Return codes
2952 * 0 - successful
2953 * 1 - failed
2954 **/
dea31012005-04-17 16:05:31 -05002955int
James Smart2e0fef82007-06-17 19:56:36 -05002956lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002957{
Julia Lawall372bd282008-12-16 16:15:08 +01002958 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04002959 struct lpfc_vport **vports;
2960 int i;
James Smart16a3a202013-04-17 20:14:38 -04002961 bool vpis_cleared = false;
James Smart2e0fef82007-06-17 19:56:36 -05002962
dea31012005-04-17 16:05:31 -05002963 if (!phba)
2964 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01002965 vport = phba->pport;
dea31012005-04-17 16:05:31 -05002966
James Smart2e0fef82007-06-17 19:56:36 -05002967 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05002968 return 0;
2969
James Smarted957682007-06-17 19:56:37 -05002970 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002971 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05002972
James Smart618a5232012-06-12 13:54:36 -04002973 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05002974
James Smartda0436e2009-05-22 14:51:39 -04002975 if (phba->sli_rev == LPFC_SLI_REV4) {
2976 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2977 lpfc_unblock_mgmt_io(phba);
2978 return 1;
2979 }
James Smart16a3a202013-04-17 20:14:38 -04002980 spin_lock_irq(&phba->hbalock);
2981 if (!phba->sli4_hba.max_cfg_param.vpi_used)
2982 vpis_cleared = true;
2983 spin_unlock_irq(&phba->hbalock);
James Smartda0436e2009-05-22 14:51:39 -04002984 } else {
James Smart895427b2017-02-12 13:52:30 -08002985 lpfc_sli_queue_init(phba);
James Smartda0436e2009-05-22 14:51:39 -04002986 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2987 lpfc_unblock_mgmt_io(phba);
2988 return 1;
2989 }
James Smart46fa3112007-04-25 09:51:45 -04002990 }
dea31012005-04-17 16:05:31 -05002991
James Smart549e55c2007-08-02 11:09:51 -04002992 vports = lpfc_create_vport_work_array(phba);
Arnd Bergmannaeb66412016-03-14 15:29:44 +01002993 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04002994 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002995 struct Scsi_Host *shost;
2996 shost = lpfc_shost_from_vport(vports[i]);
2997 spin_lock_irq(shost->host_lock);
2998 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2999 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
3000 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart16a3a202013-04-17 20:14:38 -04003001 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart1c6834a2009-07-19 10:01:26 -04003002 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart16a3a202013-04-17 20:14:38 -04003003 if ((vpis_cleared) &&
3004 (vports[i]->port_type !=
3005 LPFC_PHYSICAL_PORT))
3006 vports[i]->vpi = 0;
3007 }
James Smart549e55c2007-08-02 11:09:51 -04003008 spin_unlock_irq(shost->host_lock);
3009 }
Arnd Bergmannaeb66412016-03-14 15:29:44 +01003010 }
3011 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05003012
James Smart46fa3112007-04-25 09:51:45 -04003013 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05003014 return 0;
3015}
3016
James Smarte59058c2008-08-24 21:49:00 -04003017/**
James Smart3621a712009-04-06 18:47:14 -04003018 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04003019 * @phba: pointer to lpfc hba data structure.
3020 *
3021 * This routine marks a HBA's management interface as not blocked. Once the
3022 * HBA's management interface is marked as not blocked, all the user space
3023 * access to the HBA, whether they are from sysfs interface or libdfc
3024 * interface will be allowed. The HBA is set to block the management interface
3025 * when the driver prepares the HBA interface for online or offline and then
3026 * set to unblock the management interface afterwards.
3027 **/
James Smart46fa3112007-04-25 09:51:45 -04003028void
James Smart46fa3112007-04-25 09:51:45 -04003029lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
3030{
3031 unsigned long iflag;
3032
James Smart2e0fef82007-06-17 19:56:36 -05003033 spin_lock_irqsave(&phba->hbalock, iflag);
3034 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
3035 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04003036}
3037
James Smarte59058c2008-08-24 21:49:00 -04003038/**
James Smart3621a712009-04-06 18:47:14 -04003039 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04003040 * @phba: pointer to lpfc hba data structure.
3041 *
3042 * This routine is invoked to prepare a HBA to be brought offline. It performs
3043 * unregistration login to all the nodes on all vports and flushes the mailbox
3044 * queue to make it ready to be brought offline.
3045 **/
James Smart46fa3112007-04-25 09:51:45 -04003046void
James Smart618a5232012-06-12 13:54:36 -04003047lpfc_offline_prep(struct lpfc_hba *phba, int mbx_action)
James Smart46fa3112007-04-25 09:51:45 -04003048{
James Smart2e0fef82007-06-17 19:56:36 -05003049 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04003050 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04003051 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05003052 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04003053 int i;
dea31012005-04-17 16:05:31 -05003054
James Smart2e0fef82007-06-17 19:56:36 -05003055 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04003056 return;
dea31012005-04-17 16:05:31 -05003057
James Smart618a5232012-06-12 13:54:36 -04003058 lpfc_block_mgmt_io(phba, mbx_action);
dea31012005-04-17 16:05:31 -05003059
3060 lpfc_linkdown(phba);
3061
James Smart87af33f2007-10-27 13:37:43 -04003062 /* Issue an unreg_login to all nodes on all vports */
3063 vports = lpfc_create_vport_work_array(phba);
3064 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04003065 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04003066 if (vports[i]->load_flag & FC_UNLOADING)
3067 continue;
James Smart72100cc2010-02-12 14:43:01 -05003068 shost = lpfc_shost_from_vport(vports[i]);
3069 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003070 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05003071 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
3072 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05003073 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05003074
James Smart87af33f2007-10-27 13:37:43 -04003075 shost = lpfc_shost_from_vport(vports[i]);
3076 list_for_each_entry_safe(ndlp, next_ndlp,
3077 &vports[i]->fc_nodes,
3078 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05003079 if (!NLP_CHK_NODE_ACT(ndlp))
3080 continue;
James Smart87af33f2007-10-27 13:37:43 -04003081 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
3082 continue;
3083 if (ndlp->nlp_type & NLP_FABRIC) {
3084 lpfc_disc_state_machine(vports[i], ndlp,
3085 NULL, NLP_EVT_DEVICE_RECOVERY);
3086 lpfc_disc_state_machine(vports[i], ndlp,
3087 NULL, NLP_EVT_DEVICE_RM);
3088 }
3089 spin_lock_irq(shost->host_lock);
3090 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart401ee0c2012-03-01 22:35:34 -05003091 spin_unlock_irq(shost->host_lock);
James Smart6b5151f2012-01-18 16:24:06 -05003092 /*
3093 * Whenever an SLI4 port goes offline, free the
James Smart401ee0c2012-03-01 22:35:34 -05003094 * RPI. Get a new RPI when the adapter port
3095 * comes back online.
James Smart6b5151f2012-01-18 16:24:06 -05003096 */
James Smartbe6bb942015-04-07 15:07:22 -04003097 if (phba->sli_rev == LPFC_SLI_REV4) {
3098 lpfc_printf_vlog(ndlp->vport,
3099 KERN_INFO, LOG_NODE,
3100 "0011 lpfc_offline: "
3101 "ndlp:x%p did %x "
3102 "usgmap:x%x rpi:%x\n",
3103 ndlp, ndlp->nlp_DID,
3104 ndlp->nlp_usg_map,
3105 ndlp->nlp_rpi);
3106
James Smart6b5151f2012-01-18 16:24:06 -05003107 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smartbe6bb942015-04-07 15:07:22 -04003108 }
James Smart87af33f2007-10-27 13:37:43 -04003109 lpfc_unreg_rpi(vports[i], ndlp);
3110 }
3111 }
3112 }
James Smart09372822008-01-11 01:52:54 -05003113 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05003114
James Smart618a5232012-06-12 13:54:36 -04003115 lpfc_sli_mbox_sys_shutdown(phba, mbx_action);
James Smart46fa3112007-04-25 09:51:45 -04003116}
3117
James Smarte59058c2008-08-24 21:49:00 -04003118/**
James Smart3621a712009-04-06 18:47:14 -04003119 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04003120 * @phba: pointer to lpfc hba data structure.
3121 *
3122 * This routine actually brings a HBA offline. It stops all the timers
3123 * associated with the HBA, brings down the SLI layer, and eventually
3124 * marks the HBA as in offline state for the upper layer protocol.
3125 **/
James Smart46fa3112007-04-25 09:51:45 -04003126void
James Smart2e0fef82007-06-17 19:56:36 -05003127lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04003128{
James Smart549e55c2007-08-02 11:09:51 -04003129 struct Scsi_Host *shost;
3130 struct lpfc_vport **vports;
3131 int i;
James Smart46fa3112007-04-25 09:51:45 -04003132
James Smart549e55c2007-08-02 11:09:51 -04003133 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04003134 return;
James Smart688a8862006-07-06 15:49:56 -04003135
James Smartda0436e2009-05-22 14:51:39 -04003136 /* stop port and all timers associated with this hba */
3137 lpfc_stop_port(phba);
James Smart51ef4c22007-08-02 11:10:31 -04003138 vports = lpfc_create_vport_work_array(phba);
3139 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04003140 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04003141 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05003142 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05003143 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003144 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05003145 /* Bring down the SLI Layer and cleanup. The HBA is offline
3146 now. */
3147 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003148 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04003149 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003150 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04003151 vports = lpfc_create_vport_work_array(phba);
3152 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04003153 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04003154 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04003155 spin_lock_irq(shost->host_lock);
3156 vports[i]->work_port_events = 0;
3157 vports[i]->fc_flag |= FC_OFFLINE_MODE;
3158 spin_unlock_irq(shost->host_lock);
3159 }
James Smart09372822008-01-11 01:52:54 -05003160 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05003161}
3162
James Smarte59058c2008-08-24 21:49:00 -04003163/**
James Smart3621a712009-04-06 18:47:14 -04003164 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04003165 * @phba: pointer to lpfc hba data structure.
3166 *
3167 * This routine is to free all the SCSI buffers and IOCBs from the driver
3168 * list back to kernel. It is called from lpfc_pci_remove_one to free
3169 * the internal resources before the device is removed from the system.
James Smarte59058c2008-08-24 21:49:00 -04003170 **/
James Smart8a9d2e82012-05-09 21:16:12 -04003171static void
James Smart2e0fef82007-06-17 19:56:36 -05003172lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003173{
3174 struct lpfc_scsi_buf *sb, *sb_next;
dea31012005-04-17 16:05:31 -05003175
James Smart895427b2017-02-12 13:52:30 -08003176 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
3177 return;
3178
James Smart2e0fef82007-06-17 19:56:36 -05003179 spin_lock_irq(&phba->hbalock);
James Smarta40fc5f2013-04-17 20:17:40 -04003180
dea31012005-04-17 16:05:31 -05003181 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smarta40fc5f2013-04-17 20:17:40 -04003182
3183 spin_lock(&phba->scsi_buf_list_put_lock);
3184 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_put,
3185 list) {
dea31012005-04-17 16:05:31 -05003186 list_del(&sb->list);
James Smart895427b2017-02-12 13:52:30 -08003187 pci_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05003188 sb->dma_handle);
dea31012005-04-17 16:05:31 -05003189 kfree(sb);
3190 phba->total_scsi_bufs--;
3191 }
James Smarta40fc5f2013-04-17 20:17:40 -04003192 spin_unlock(&phba->scsi_buf_list_put_lock);
3193
3194 spin_lock(&phba->scsi_buf_list_get_lock);
3195 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_get,
3196 list) {
3197 list_del(&sb->list);
James Smart895427b2017-02-12 13:52:30 -08003198 pci_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
James Smarta40fc5f2013-04-17 20:17:40 -04003199 sb->dma_handle);
3200 kfree(sb);
3201 phba->total_scsi_bufs--;
3202 }
3203 spin_unlock(&phba->scsi_buf_list_get_lock);
James Smart2e0fef82007-06-17 19:56:36 -05003204 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003205}
James Smart8a9d2e82012-05-09 21:16:12 -04003206/**
James Smart895427b2017-02-12 13:52:30 -08003207 * lpfc_nvme_free - Free all the NVME buffers and IOCBs from driver lists
3208 * @phba: pointer to lpfc hba data structure.
3209 *
3210 * This routine is to free all the NVME buffers and IOCBs from the driver
3211 * list back to kernel. It is called from lpfc_pci_remove_one to free
3212 * the internal resources before the device is removed from the system.
3213 **/
3214static void
3215lpfc_nvme_free(struct lpfc_hba *phba)
3216{
3217 struct lpfc_nvme_buf *lpfc_ncmd, *lpfc_ncmd_next;
James Smart895427b2017-02-12 13:52:30 -08003218
3219 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
3220 return;
3221
3222 spin_lock_irq(&phba->hbalock);
3223
3224 /* Release all the lpfc_nvme_bufs maintained by this host. */
3225 spin_lock(&phba->nvme_buf_list_put_lock);
3226 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3227 &phba->lpfc_nvme_buf_list_put, list) {
3228 list_del(&lpfc_ncmd->list);
3229 pci_pool_free(phba->lpfc_sg_dma_buf_pool, lpfc_ncmd->data,
3230 lpfc_ncmd->dma_handle);
3231 kfree(lpfc_ncmd);
3232 phba->total_nvme_bufs--;
3233 }
3234 spin_unlock(&phba->nvme_buf_list_put_lock);
3235
3236 spin_lock(&phba->nvme_buf_list_get_lock);
3237 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3238 &phba->lpfc_nvme_buf_list_get, list) {
3239 list_del(&lpfc_ncmd->list);
3240 pci_pool_free(phba->lpfc_sg_dma_buf_pool, lpfc_ncmd->data,
3241 lpfc_ncmd->dma_handle);
3242 kfree(lpfc_ncmd);
3243 phba->total_nvme_bufs--;
3244 }
3245 spin_unlock(&phba->nvme_buf_list_get_lock);
James Smart895427b2017-02-12 13:52:30 -08003246 spin_unlock_irq(&phba->hbalock);
3247}
3248/**
3249 * lpfc_sli4_els_sgl_update - update ELS xri-sgl sizing and mapping
James Smart8a9d2e82012-05-09 21:16:12 -04003250 * @phba: pointer to lpfc hba data structure.
3251 *
3252 * This routine first calculates the sizes of the current els and allocated
3253 * scsi sgl lists, and then goes through all sgls to updates the physical
3254 * XRIs assigned due to port function reset. During port initialization, the
3255 * current els and allocated scsi sgl lists are 0s.
3256 *
3257 * Return codes
3258 * 0 - successful (for now, it always returns 0)
3259 **/
3260int
James Smart895427b2017-02-12 13:52:30 -08003261lpfc_sli4_els_sgl_update(struct lpfc_hba *phba)
James Smart8a9d2e82012-05-09 21:16:12 -04003262{
3263 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
James Smart895427b2017-02-12 13:52:30 -08003264 uint16_t i, lxri, xri_cnt, els_xri_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04003265 LIST_HEAD(els_sgl_list);
James Smart8a9d2e82012-05-09 21:16:12 -04003266 int rc;
3267
3268 /*
3269 * update on pci function's els xri-sgl list
3270 */
3271 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
James Smart895427b2017-02-12 13:52:30 -08003272
James Smart8a9d2e82012-05-09 21:16:12 -04003273 if (els_xri_cnt > phba->sli4_hba.els_xri_cnt) {
3274 /* els xri-sgl expanded */
3275 xri_cnt = els_xri_cnt - phba->sli4_hba.els_xri_cnt;
3276 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3277 "3157 ELS xri-sgl count increased from "
3278 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3279 els_xri_cnt);
3280 /* allocate the additional els sgls */
3281 for (i = 0; i < xri_cnt; i++) {
3282 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3283 GFP_KERNEL);
3284 if (sglq_entry == NULL) {
3285 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3286 "2562 Failure to allocate an "
3287 "ELS sgl entry:%d\n", i);
3288 rc = -ENOMEM;
3289 goto out_free_mem;
3290 }
3291 sglq_entry->buff_type = GEN_BUFF_TYPE;
3292 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0,
3293 &sglq_entry->phys);
3294 if (sglq_entry->virt == NULL) {
3295 kfree(sglq_entry);
3296 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3297 "2563 Failure to allocate an "
3298 "ELS mbuf:%d\n", i);
3299 rc = -ENOMEM;
3300 goto out_free_mem;
3301 }
3302 sglq_entry->sgl = sglq_entry->virt;
3303 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
3304 sglq_entry->state = SGL_FREED;
3305 list_add_tail(&sglq_entry->list, &els_sgl_list);
3306 }
James Smart38c20672013-03-01 16:37:44 -05003307 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08003308 spin_lock(&phba->sli4_hba.sgl_list_lock);
3309 list_splice_init(&els_sgl_list,
3310 &phba->sli4_hba.lpfc_els_sgl_list);
3311 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05003312 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003313 } else if (els_xri_cnt < phba->sli4_hba.els_xri_cnt) {
3314 /* els xri-sgl shrinked */
3315 xri_cnt = phba->sli4_hba.els_xri_cnt - els_xri_cnt;
3316 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3317 "3158 ELS xri-sgl count decreased from "
3318 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3319 els_xri_cnt);
3320 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08003321 spin_lock(&phba->sli4_hba.sgl_list_lock);
3322 list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list,
3323 &els_sgl_list);
James Smart8a9d2e82012-05-09 21:16:12 -04003324 /* release extra els sgls from list */
3325 for (i = 0; i < xri_cnt; i++) {
3326 list_remove_head(&els_sgl_list,
3327 sglq_entry, struct lpfc_sglq, list);
3328 if (sglq_entry) {
James Smart895427b2017-02-12 13:52:30 -08003329 __lpfc_mbuf_free(phba, sglq_entry->virt,
3330 sglq_entry->phys);
James Smart8a9d2e82012-05-09 21:16:12 -04003331 kfree(sglq_entry);
3332 }
3333 }
James Smart895427b2017-02-12 13:52:30 -08003334 list_splice_init(&els_sgl_list,
3335 &phba->sli4_hba.lpfc_els_sgl_list);
3336 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003337 spin_unlock_irq(&phba->hbalock);
3338 } else
3339 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3340 "3163 ELS xri-sgl count unchanged: %d\n",
3341 els_xri_cnt);
3342 phba->sli4_hba.els_xri_cnt = els_xri_cnt;
3343
3344 /* update xris to els sgls on the list */
3345 sglq_entry = NULL;
3346 sglq_entry_next = NULL;
3347 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
James Smart895427b2017-02-12 13:52:30 -08003348 &phba->sli4_hba.lpfc_els_sgl_list, list) {
James Smart8a9d2e82012-05-09 21:16:12 -04003349 lxri = lpfc_sli4_next_xritag(phba);
3350 if (lxri == NO_XRI) {
3351 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3352 "2400 Failed to allocate xri for "
3353 "ELS sgl\n");
3354 rc = -ENOMEM;
3355 goto out_free_mem;
3356 }
3357 sglq_entry->sli4_lxritag = lxri;
3358 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3359 }
James Smart895427b2017-02-12 13:52:30 -08003360 return 0;
3361
3362out_free_mem:
3363 lpfc_free_els_sgl_list(phba);
3364 return rc;
3365}
3366
3367/**
James Smartf358dd02017-02-12 13:52:34 -08003368 * lpfc_sli4_nvmet_sgl_update - update xri-sgl sizing and mapping
3369 * @phba: pointer to lpfc hba data structure.
3370 *
3371 * This routine first calculates the sizes of the current els and allocated
3372 * scsi sgl lists, and then goes through all sgls to updates the physical
3373 * XRIs assigned due to port function reset. During port initialization, the
3374 * current els and allocated scsi sgl lists are 0s.
3375 *
3376 * Return codes
3377 * 0 - successful (for now, it always returns 0)
3378 **/
3379int
3380lpfc_sli4_nvmet_sgl_update(struct lpfc_hba *phba)
3381{
3382 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
3383 uint16_t i, lxri, xri_cnt, els_xri_cnt;
3384 uint16_t nvmet_xri_cnt, tot_cnt;
3385 LIST_HEAD(nvmet_sgl_list);
3386 int rc;
3387
3388 /*
3389 * update on pci function's nvmet xri-sgl list
3390 */
3391 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
James Smart2d7dbc42017-02-12 13:52:35 -08003392 nvmet_xri_cnt = phba->cfg_nvmet_mrq * phba->cfg_nvmet_mrq_post;
James Smartf358dd02017-02-12 13:52:34 -08003393 tot_cnt = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
James Smart2d7dbc42017-02-12 13:52:35 -08003394 if (nvmet_xri_cnt > tot_cnt) {
3395 phba->cfg_nvmet_mrq_post = tot_cnt / phba->cfg_nvmet_mrq;
3396 nvmet_xri_cnt = phba->cfg_nvmet_mrq * phba->cfg_nvmet_mrq_post;
3397 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3398 "6301 NVMET post-sgl count changed to %d\n",
3399 phba->cfg_nvmet_mrq_post);
3400 }
James Smartf358dd02017-02-12 13:52:34 -08003401
3402 if (nvmet_xri_cnt > phba->sli4_hba.nvmet_xri_cnt) {
3403 /* els xri-sgl expanded */
3404 xri_cnt = nvmet_xri_cnt - phba->sli4_hba.nvmet_xri_cnt;
3405 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3406 "6302 NVMET xri-sgl cnt grew from %d to %d\n",
3407 phba->sli4_hba.nvmet_xri_cnt, nvmet_xri_cnt);
3408 /* allocate the additional nvmet sgls */
3409 for (i = 0; i < xri_cnt; i++) {
3410 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3411 GFP_KERNEL);
3412 if (sglq_entry == NULL) {
3413 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3414 "6303 Failure to allocate an "
3415 "NVMET sgl entry:%d\n", i);
3416 rc = -ENOMEM;
3417 goto out_free_mem;
3418 }
3419 sglq_entry->buff_type = NVMET_BUFF_TYPE;
3420 sglq_entry->virt = lpfc_nvmet_buf_alloc(phba, 0,
3421 &sglq_entry->phys);
3422 if (sglq_entry->virt == NULL) {
3423 kfree(sglq_entry);
3424 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3425 "6304 Failure to allocate an "
3426 "NVMET buf:%d\n", i);
3427 rc = -ENOMEM;
3428 goto out_free_mem;
3429 }
3430 sglq_entry->sgl = sglq_entry->virt;
3431 memset(sglq_entry->sgl, 0,
3432 phba->cfg_sg_dma_buf_size);
3433 sglq_entry->state = SGL_FREED;
3434 list_add_tail(&sglq_entry->list, &nvmet_sgl_list);
3435 }
3436 spin_lock_irq(&phba->hbalock);
3437 spin_lock(&phba->sli4_hba.sgl_list_lock);
3438 list_splice_init(&nvmet_sgl_list,
3439 &phba->sli4_hba.lpfc_nvmet_sgl_list);
3440 spin_unlock(&phba->sli4_hba.sgl_list_lock);
3441 spin_unlock_irq(&phba->hbalock);
3442 } else if (nvmet_xri_cnt < phba->sli4_hba.nvmet_xri_cnt) {
3443 /* nvmet xri-sgl shrunk */
3444 xri_cnt = phba->sli4_hba.nvmet_xri_cnt - nvmet_xri_cnt;
3445 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3446 "6305 NVMET xri-sgl count decreased from "
3447 "%d to %d\n", phba->sli4_hba.nvmet_xri_cnt,
3448 nvmet_xri_cnt);
3449 spin_lock_irq(&phba->hbalock);
3450 spin_lock(&phba->sli4_hba.sgl_list_lock);
3451 list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list,
3452 &nvmet_sgl_list);
3453 /* release extra nvmet sgls from list */
3454 for (i = 0; i < xri_cnt; i++) {
3455 list_remove_head(&nvmet_sgl_list,
3456 sglq_entry, struct lpfc_sglq, list);
3457 if (sglq_entry) {
3458 lpfc_nvmet_buf_free(phba, sglq_entry->virt,
3459 sglq_entry->phys);
3460 kfree(sglq_entry);
3461 }
3462 }
3463 list_splice_init(&nvmet_sgl_list,
3464 &phba->sli4_hba.lpfc_nvmet_sgl_list);
3465 spin_unlock(&phba->sli4_hba.sgl_list_lock);
3466 spin_unlock_irq(&phba->hbalock);
3467 } else
3468 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3469 "6306 NVMET xri-sgl count unchanged: %d\n",
3470 nvmet_xri_cnt);
3471 phba->sli4_hba.nvmet_xri_cnt = nvmet_xri_cnt;
3472
3473 /* update xris to nvmet sgls on the list */
3474 sglq_entry = NULL;
3475 sglq_entry_next = NULL;
3476 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
3477 &phba->sli4_hba.lpfc_nvmet_sgl_list, list) {
3478 lxri = lpfc_sli4_next_xritag(phba);
3479 if (lxri == NO_XRI) {
3480 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3481 "6307 Failed to allocate xri for "
3482 "NVMET sgl\n");
3483 rc = -ENOMEM;
3484 goto out_free_mem;
3485 }
3486 sglq_entry->sli4_lxritag = lxri;
3487 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3488 }
3489 return 0;
3490
3491out_free_mem:
3492 lpfc_free_nvmet_sgl_list(phba);
3493 return rc;
3494}
3495
3496/**
James Smart895427b2017-02-12 13:52:30 -08003497 * lpfc_sli4_scsi_sgl_update - update xri-sgl sizing and mapping
3498 * @phba: pointer to lpfc hba data structure.
3499 *
3500 * This routine first calculates the sizes of the current els and allocated
3501 * scsi sgl lists, and then goes through all sgls to updates the physical
3502 * XRIs assigned due to port function reset. During port initialization, the
3503 * current els and allocated scsi sgl lists are 0s.
3504 *
3505 * Return codes
3506 * 0 - successful (for now, it always returns 0)
3507 **/
3508int
3509lpfc_sli4_scsi_sgl_update(struct lpfc_hba *phba)
3510{
3511 struct lpfc_scsi_buf *psb, *psb_next;
3512 uint16_t i, lxri, els_xri_cnt, scsi_xri_cnt;
3513 LIST_HEAD(scsi_sgl_list);
3514 int rc;
3515
3516 /*
3517 * update on pci function's els xri-sgl list
3518 */
3519 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3520 phba->total_scsi_bufs = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04003521
3522 /*
3523 * update on pci function's allocated scsi xri-sgl list
3524 */
James Smart8a9d2e82012-05-09 21:16:12 -04003525 /* maximum number of xris available for scsi buffers */
3526 phba->sli4_hba.scsi_xri_max = phba->sli4_hba.max_cfg_param.max_xri -
3527 els_xri_cnt;
3528
James Smart895427b2017-02-12 13:52:30 -08003529 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
3530 return 0;
3531
3532 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3533 phba->sli4_hba.scsi_xri_max = /* Split them up */
3534 (phba->sli4_hba.scsi_xri_max *
3535 phba->cfg_xri_split) / 100;
James Smart8a9d2e82012-05-09 21:16:12 -04003536
James Smarta40fc5f2013-04-17 20:17:40 -04003537 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart164cecd2013-09-06 12:22:38 -04003538 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003539 list_splice_init(&phba->lpfc_scsi_buf_list_get, &scsi_sgl_list);
3540 list_splice(&phba->lpfc_scsi_buf_list_put, &scsi_sgl_list);
James Smart164cecd2013-09-06 12:22:38 -04003541 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003542 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003543
James Smarte8c0a772017-04-21 16:04:49 -07003544 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3545 "6060 Current allocated SCSI xri-sgl count:%d, "
3546 "maximum SCSI xri count:%d (split:%d)\n",
3547 phba->sli4_hba.scsi_xri_cnt,
3548 phba->sli4_hba.scsi_xri_max, phba->cfg_xri_split);
3549
James Smart8a9d2e82012-05-09 21:16:12 -04003550 if (phba->sli4_hba.scsi_xri_cnt > phba->sli4_hba.scsi_xri_max) {
3551 /* max scsi xri shrinked below the allocated scsi buffers */
3552 scsi_xri_cnt = phba->sli4_hba.scsi_xri_cnt -
3553 phba->sli4_hba.scsi_xri_max;
3554 /* release the extra allocated scsi buffers */
3555 for (i = 0; i < scsi_xri_cnt; i++) {
3556 list_remove_head(&scsi_sgl_list, psb,
3557 struct lpfc_scsi_buf, list);
James Smarta2fc4aef2014-09-03 12:57:55 -04003558 if (psb) {
James Smart895427b2017-02-12 13:52:30 -08003559 pci_pool_free(phba->lpfc_sg_dma_buf_pool,
James Smarta2fc4aef2014-09-03 12:57:55 -04003560 psb->data, psb->dma_handle);
3561 kfree(psb);
3562 }
James Smart8a9d2e82012-05-09 21:16:12 -04003563 }
James Smarta40fc5f2013-04-17 20:17:40 -04003564 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003565 phba->sli4_hba.scsi_xri_cnt -= scsi_xri_cnt;
James Smarta40fc5f2013-04-17 20:17:40 -04003566 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003567 }
3568
3569 /* update xris associated to remaining allocated scsi buffers */
3570 psb = NULL;
3571 psb_next = NULL;
3572 list_for_each_entry_safe(psb, psb_next, &scsi_sgl_list, list) {
3573 lxri = lpfc_sli4_next_xritag(phba);
3574 if (lxri == NO_XRI) {
3575 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3576 "2560 Failed to allocate xri for "
3577 "scsi buffer\n");
3578 rc = -ENOMEM;
3579 goto out_free_mem;
3580 }
3581 psb->cur_iocbq.sli4_lxritag = lxri;
3582 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3583 }
James Smarta40fc5f2013-04-17 20:17:40 -04003584 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart164cecd2013-09-06 12:22:38 -04003585 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003586 list_splice_init(&scsi_sgl_list, &phba->lpfc_scsi_buf_list_get);
3587 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
James Smart164cecd2013-09-06 12:22:38 -04003588 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003589 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
dea31012005-04-17 16:05:31 -05003590 return 0;
James Smart8a9d2e82012-05-09 21:16:12 -04003591
3592out_free_mem:
James Smart8a9d2e82012-05-09 21:16:12 -04003593 lpfc_scsi_free(phba);
3594 return rc;
dea31012005-04-17 16:05:31 -05003595}
3596
James Smart96418b52017-03-04 09:30:31 -08003597static uint64_t
3598lpfc_get_wwpn(struct lpfc_hba *phba)
3599{
3600 uint64_t wwn;
3601 int rc;
3602 LPFC_MBOXQ_t *mboxq;
3603 MAILBOX_t *mb;
3604
James Smart4492b732017-04-27 15:08:26 -07003605 if (phba->sli_rev < LPFC_SLI_REV4) {
3606 /* Reset the port first */
3607 lpfc_sli_brdrestart(phba);
3608 rc = lpfc_sli_chipset_init(phba);
3609 if (rc)
3610 return (uint64_t)-1;
3611 }
James Smart96418b52017-03-04 09:30:31 -08003612
3613 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
3614 GFP_KERNEL);
3615 if (!mboxq)
3616 return (uint64_t)-1;
3617
3618 /* First get WWN of HBA instance */
3619 lpfc_read_nv(phba, mboxq);
3620 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
3621 if (rc != MBX_SUCCESS) {
3622 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3623 "6019 Mailbox failed , mbxCmd x%x "
3624 "READ_NV, mbxStatus x%x\n",
3625 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
3626 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
3627 mempool_free(mboxq, phba->mbox_mem_pool);
3628 return (uint64_t) -1;
3629 }
3630 mb = &mboxq->u.mb;
3631 memcpy(&wwn, (char *)mb->un.varRDnvp.portname, sizeof(uint64_t));
3632 /* wwn is WWPN of HBA instance */
3633 mempool_free(mboxq, phba->mbox_mem_pool);
3634 if (phba->sli_rev == LPFC_SLI_REV4)
3635 return be64_to_cpu(wwn);
3636 else
3637 return (((wwn & 0xffffffff00000000) >> 32) |
3638 ((wwn & 0x00000000ffffffff) << 32));
3639
3640}
3641
James Smarte59058c2008-08-24 21:49:00 -04003642/**
James Smart895427b2017-02-12 13:52:30 -08003643 * lpfc_sli4_nvme_sgl_update - update xri-sgl sizing and mapping
3644 * @phba: pointer to lpfc hba data structure.
3645 *
3646 * This routine first calculates the sizes of the current els and allocated
3647 * scsi sgl lists, and then goes through all sgls to updates the physical
3648 * XRIs assigned due to port function reset. During port initialization, the
3649 * current els and allocated scsi sgl lists are 0s.
3650 *
3651 * Return codes
3652 * 0 - successful (for now, it always returns 0)
3653 **/
3654int
3655lpfc_sli4_nvme_sgl_update(struct lpfc_hba *phba)
3656{
3657 struct lpfc_nvme_buf *lpfc_ncmd = NULL, *lpfc_ncmd_next = NULL;
3658 uint16_t i, lxri, els_xri_cnt;
3659 uint16_t nvme_xri_cnt, nvme_xri_max;
3660 LIST_HEAD(nvme_sgl_list);
3661 int rc;
3662
3663 phba->total_nvme_bufs = 0;
3664
3665 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
3666 return 0;
3667 /*
3668 * update on pci function's allocated nvme xri-sgl list
3669 */
3670
3671 /* maximum number of xris available for nvme buffers */
3672 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3673 nvme_xri_max = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
3674 phba->sli4_hba.nvme_xri_max = nvme_xri_max;
3675 phba->sli4_hba.nvme_xri_max -= phba->sli4_hba.scsi_xri_max;
3676
3677 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3678 "6074 Current allocated NVME xri-sgl count:%d, "
3679 "maximum NVME xri count:%d\n",
3680 phba->sli4_hba.nvme_xri_cnt,
3681 phba->sli4_hba.nvme_xri_max);
3682
3683 spin_lock_irq(&phba->nvme_buf_list_get_lock);
3684 spin_lock(&phba->nvme_buf_list_put_lock);
3685 list_splice_init(&phba->lpfc_nvme_buf_list_get, &nvme_sgl_list);
3686 list_splice(&phba->lpfc_nvme_buf_list_put, &nvme_sgl_list);
3687 spin_unlock(&phba->nvme_buf_list_put_lock);
3688 spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3689
3690 if (phba->sli4_hba.nvme_xri_cnt > phba->sli4_hba.nvme_xri_max) {
3691 /* max nvme xri shrunk below the allocated nvme buffers */
3692 spin_lock_irq(&phba->nvme_buf_list_get_lock);
3693 nvme_xri_cnt = phba->sli4_hba.nvme_xri_cnt -
3694 phba->sli4_hba.nvme_xri_max;
3695 spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3696 /* release the extra allocated nvme buffers */
3697 for (i = 0; i < nvme_xri_cnt; i++) {
3698 list_remove_head(&nvme_sgl_list, lpfc_ncmd,
3699 struct lpfc_nvme_buf, list);
3700 if (lpfc_ncmd) {
3701 pci_pool_free(phba->lpfc_sg_dma_buf_pool,
3702 lpfc_ncmd->data,
3703 lpfc_ncmd->dma_handle);
3704 kfree(lpfc_ncmd);
3705 }
3706 }
3707 spin_lock_irq(&phba->nvme_buf_list_get_lock);
3708 phba->sli4_hba.nvme_xri_cnt -= nvme_xri_cnt;
3709 spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3710 }
3711
3712 /* update xris associated to remaining allocated nvme buffers */
3713 lpfc_ncmd = NULL;
3714 lpfc_ncmd_next = NULL;
3715 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3716 &nvme_sgl_list, list) {
3717 lxri = lpfc_sli4_next_xritag(phba);
3718 if (lxri == NO_XRI) {
3719 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3720 "6075 Failed to allocate xri for "
3721 "nvme buffer\n");
3722 rc = -ENOMEM;
3723 goto out_free_mem;
3724 }
3725 lpfc_ncmd->cur_iocbq.sli4_lxritag = lxri;
3726 lpfc_ncmd->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3727 }
3728 spin_lock_irq(&phba->nvme_buf_list_get_lock);
3729 spin_lock(&phba->nvme_buf_list_put_lock);
3730 list_splice_init(&nvme_sgl_list, &phba->lpfc_nvme_buf_list_get);
3731 INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_put);
3732 spin_unlock(&phba->nvme_buf_list_put_lock);
3733 spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3734 return 0;
3735
3736out_free_mem:
3737 lpfc_nvme_free(phba);
3738 return rc;
3739}
3740
3741/**
James Smart3621a712009-04-06 18:47:14 -04003742 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04003743 * @phba: pointer to lpfc hba data structure.
3744 * @instance: a unique integer ID to this FC port.
3745 * @dev: pointer to the device data structure.
3746 *
3747 * This routine creates a FC port for the upper layer protocol. The FC port
3748 * can be created on top of either a physical port or a virtual port provided
3749 * by the HBA. This routine also allocates a SCSI host data structure (shost)
3750 * and associates the FC port created before adding the shost into the SCSI
3751 * layer.
3752 *
3753 * Return codes
3754 * @vport - pointer to the virtual N_Port data structure.
3755 * NULL - port create failed.
3756 **/
James Smart2e0fef82007-06-17 19:56:36 -05003757struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04003758lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04003759{
James Smart2e0fef82007-06-17 19:56:36 -05003760 struct lpfc_vport *vport;
James Smart895427b2017-02-12 13:52:30 -08003761 struct Scsi_Host *shost = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003762 int error = 0;
James Smart96418b52017-03-04 09:30:31 -08003763 int i;
3764 uint64_t wwn;
3765 bool use_no_reset_hba = false;
3766
3767 wwn = lpfc_get_wwpn(phba);
3768
3769 for (i = 0; i < lpfc_no_hba_reset_cnt; i++) {
3770 if (wwn == lpfc_no_hba_reset[i]) {
3771 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3772 "6020 Setting use_no_reset port=%llx\n",
3773 wwn);
3774 use_no_reset_hba = true;
3775 break;
3776 }
3777 }
James Smart47a86172007-04-25 09:53:22 -04003778
James Smart895427b2017-02-12 13:52:30 -08003779 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
3780 if (dev != &phba->pcidev->dev) {
3781 shost = scsi_host_alloc(&lpfc_vport_template,
3782 sizeof(struct lpfc_vport));
3783 } else {
James Smart96418b52017-03-04 09:30:31 -08003784 if (!use_no_reset_hba)
James Smart895427b2017-02-12 13:52:30 -08003785 shost = scsi_host_alloc(&lpfc_template,
3786 sizeof(struct lpfc_vport));
3787 else
James Smart96418b52017-03-04 09:30:31 -08003788 shost = scsi_host_alloc(&lpfc_template_no_hr,
James Smart895427b2017-02-12 13:52:30 -08003789 sizeof(struct lpfc_vport));
3790 }
3791 } else if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
3792 shost = scsi_host_alloc(&lpfc_template_nvme,
James Smartea4142f2015-04-07 15:07:13 -04003793 sizeof(struct lpfc_vport));
3794 }
James Smart2e0fef82007-06-17 19:56:36 -05003795 if (!shost)
3796 goto out;
James Smart47a86172007-04-25 09:53:22 -04003797
James Smart2e0fef82007-06-17 19:56:36 -05003798 vport = (struct lpfc_vport *) shost->hostdata;
3799 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05003800 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05003801 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05003802 vport->fc_rscn_flush = 0;
James Smart3de2a652007-08-02 11:09:59 -04003803 lpfc_get_vport_cfgparam(vport);
James Smart895427b2017-02-12 13:52:30 -08003804
James Smart2e0fef82007-06-17 19:56:36 -05003805 shost->unique_id = instance;
3806 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04003807 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05003808 shost->this_id = -1;
3809 shost->max_cmd_len = 16;
James Smart8b0dff12015-05-22 10:42:38 -04003810 shost->nr_hw_queues = phba->cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04003811 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart28baac72010-02-12 14:42:03 -05003812 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04003813 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smartda0436e2009-05-22 14:51:39 -04003814 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
3815 }
James Smart81301a92008-12-04 22:39:46 -05003816
James Smart47a86172007-04-25 09:53:22 -04003817 /*
James Smart2e0fef82007-06-17 19:56:36 -05003818 * Set initial can_queue value since 0 is no longer supported and
3819 * scsi_add_host will fail. This will be adjusted later based on the
3820 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04003821 */
James Smart2e0fef82007-06-17 19:56:36 -05003822 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04003823 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05003824 shost->transportt = lpfc_vport_transport_template;
3825 vport->port_type = LPFC_NPIV_PORT;
3826 } else {
3827 shost->transportt = lpfc_transport_template;
3828 vport->port_type = LPFC_PHYSICAL_PORT;
3829 }
James Smart47a86172007-04-25 09:53:22 -04003830
James Smart2e0fef82007-06-17 19:56:36 -05003831 /* Initialize all internally managed lists. */
3832 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04003833 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05003834 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04003835
Tomas Jasek33cc5592017-03-03 13:45:48 +01003836 setup_timer(&vport->fc_disctmo, lpfc_disc_timeout,
3837 (unsigned long)vport);
James Smart47a86172007-04-25 09:53:22 -04003838
Tomas Jasek33cc5592017-03-03 13:45:48 +01003839 setup_timer(&vport->els_tmofunc, lpfc_els_timeout,
3840 (unsigned long)vport);
James Smart92494142011-02-16 12:39:44 -05003841
Tomas Jasek33cc5592017-03-03 13:45:48 +01003842 setup_timer(&vport->delayed_disc_tmo, lpfc_delayed_disc_tmo,
3843 (unsigned long)vport);
James Smart92494142011-02-16 12:39:44 -05003844
James Bottomleyd139b9b2009-11-05 13:33:12 -06003845 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05003846 if (error)
3847 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04003848
James Smart549e55c2007-08-02 11:09:51 -04003849 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003850 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04003851 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003852 return vport;
James Smart47a86172007-04-25 09:53:22 -04003853
James Smart2e0fef82007-06-17 19:56:36 -05003854out_put_shost:
3855 scsi_host_put(shost);
3856out:
3857 return NULL;
James Smart47a86172007-04-25 09:53:22 -04003858}
3859
James Smarte59058c2008-08-24 21:49:00 -04003860/**
James Smart3621a712009-04-06 18:47:14 -04003861 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04003862 * @vport: pointer to an lpfc virtual N_Port data structure.
3863 *
3864 * This routine destroys a FC port from the upper layer protocol. All the
3865 * resources associated with the port are released.
3866 **/
James Smart2e0fef82007-06-17 19:56:36 -05003867void
3868destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04003869{
James Smart92d7f7b2007-06-17 19:56:38 -05003870 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3871 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003872
James Smart858c9f62007-06-17 19:56:39 -05003873 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05003874 fc_remove_host(shost);
3875 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04003876
James Smart92d7f7b2007-06-17 19:56:38 -05003877 spin_lock_irq(&phba->hbalock);
3878 list_del_init(&vport->listentry);
3879 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04003880
James Smart92d7f7b2007-06-17 19:56:38 -05003881 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04003882 return;
James Smart47a86172007-04-25 09:53:22 -04003883}
3884
James Smarte59058c2008-08-24 21:49:00 -04003885/**
James Smart3621a712009-04-06 18:47:14 -04003886 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04003887 *
3888 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
3889 * uses the kernel idr facility to perform the task.
3890 *
3891 * Return codes:
3892 * instance - a unique integer ID allocated as the new instance.
3893 * -1 - lpfc get instance failed.
3894 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003895int
3896lpfc_get_instance(void)
3897{
Tejun Heoab516032013-02-27 17:04:44 -08003898 int ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003899
Tejun Heoab516032013-02-27 17:04:44 -08003900 ret = idr_alloc(&lpfc_hba_index, NULL, 0, 0, GFP_KERNEL);
3901 return ret < 0 ? -1 : ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003902}
3903
James Smarte59058c2008-08-24 21:49:00 -04003904/**
James Smart3621a712009-04-06 18:47:14 -04003905 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04003906 * @shost: pointer to SCSI host data structure.
3907 * @time: elapsed time of the scan in jiffies.
3908 *
3909 * This routine is called by the SCSI layer with a SCSI host to determine
3910 * whether the scan host is finished.
3911 *
3912 * Note: there is no scan_start function as adapter initialization will have
3913 * asynchronously kicked off the link initialization.
3914 *
3915 * Return codes
3916 * 0 - SCSI host scan is not over yet.
3917 * 1 - SCSI host scan is over.
3918 **/
James Smart47a86172007-04-25 09:53:22 -04003919int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
3920{
James Smart2e0fef82007-06-17 19:56:36 -05003921 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3922 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05003923 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04003924
James Smart858c9f62007-06-17 19:56:39 -05003925 spin_lock_irq(shost->host_lock);
3926
James Smart51ef4c22007-08-02 11:10:31 -04003927 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05003928 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003929 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05003930 }
James Smart256ec0d2013-04-17 20:14:58 -04003931 if (time >= msecs_to_jiffies(30 * 1000)) {
James Smart2e0fef82007-06-17 19:56:36 -05003932 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003933 "0461 Scanning longer than 30 "
3934 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003935 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003936 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003937 }
James Smart256ec0d2013-04-17 20:14:58 -04003938 if (time >= msecs_to_jiffies(15 * 1000) &&
3939 phba->link_state <= LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -05003940 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003941 "0465 Link down longer than 15 "
3942 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003943 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05003944 goto finished;
James Smart47a86172007-04-25 09:53:22 -04003945 }
3946
James Smart2e0fef82007-06-17 19:56:36 -05003947 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05003948 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003949 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05003950 goto finished;
James Smart256ec0d2013-04-17 20:14:58 -04003951 if (vport->fc_map_cnt == 0 && time < msecs_to_jiffies(2 * 1000))
James Smart858c9f62007-06-17 19:56:39 -05003952 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003953 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05003954 goto finished;
3955
3956 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003957
3958finished:
James Smart858c9f62007-06-17 19:56:39 -05003959 spin_unlock_irq(shost->host_lock);
3960 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05003961}
3962
James Smarte59058c2008-08-24 21:49:00 -04003963/**
James Smart3621a712009-04-06 18:47:14 -04003964 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04003965 * @shost: pointer to SCSI host data structure.
3966 *
3967 * This routine initializes a given SCSI host attributes on a FC port. The
3968 * SCSI host can be either on top of a physical port or a virtual port.
3969 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003970void lpfc_host_attrib_init(struct Scsi_Host *shost)
3971{
3972 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3973 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003974 /*
James Smart2e0fef82007-06-17 19:56:36 -05003975 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04003976 */
3977
James Smart2e0fef82007-06-17 19:56:36 -05003978 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
3979 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04003980 fc_host_supported_classes(shost) = FC_COS_CLASS3;
3981
3982 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003983 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003984 fc_host_supported_fc4s(shost)[2] = 1;
3985 fc_host_supported_fc4s(shost)[7] = 1;
3986
James Smart92d7f7b2007-06-17 19:56:38 -05003987 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
3988 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04003989
3990 fc_host_supported_speeds(shost) = 0;
James Smartd38dd522015-08-31 16:48:17 -04003991 if (phba->lmt & LMT_32Gb)
3992 fc_host_supported_speeds(shost) |= FC_PORTSPEED_32GBIT;
James Smart88a2cfb2011-07-22 18:36:33 -04003993 if (phba->lmt & LMT_16Gb)
3994 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
James Smart47a86172007-04-25 09:53:22 -04003995 if (phba->lmt & LMT_10Gb)
3996 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04003997 if (phba->lmt & LMT_8Gb)
3998 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04003999 if (phba->lmt & LMT_4Gb)
4000 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
4001 if (phba->lmt & LMT_2Gb)
4002 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
4003 if (phba->lmt & LMT_1Gb)
4004 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
4005
4006 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05004007 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
4008 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04004009
Mike Christie0af5d702010-09-15 16:52:31 -05004010 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
4011
James Smart47a86172007-04-25 09:53:22 -04004012 /* This value is also unchanging */
4013 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05004014 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04004015 fc_host_active_fc4s(shost)[2] = 1;
4016 fc_host_active_fc4s(shost)[7] = 1;
4017
James Smart92d7f7b2007-06-17 19:56:38 -05004018 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04004019 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04004020 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04004021 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04004022}
dea31012005-04-17 16:05:31 -05004023
James Smarte59058c2008-08-24 21:49:00 -04004024/**
James Smartda0436e2009-05-22 14:51:39 -04004025 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04004026 * @phba: pointer to lpfc hba data structure.
4027 *
James Smartda0436e2009-05-22 14:51:39 -04004028 * This routine is invoked to stop an SLI3 device port, it stops the device
4029 * from generating interrupts and stops the device driver's timers for the
4030 * device.
James Smarte59058c2008-08-24 21:49:00 -04004031 **/
James Smartdb2378e2008-02-08 18:49:51 -05004032static void
James Smartda0436e2009-05-22 14:51:39 -04004033lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05004034{
James Smartda0436e2009-05-22 14:51:39 -04004035 /* Clear all interrupt enable conditions */
4036 writel(0, phba->HCregaddr);
4037 readl(phba->HCregaddr); /* flush */
4038 /* Clear all pending interrupts */
4039 writel(0xffffffff, phba->HAregaddr);
4040 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04004041
James Smartda0436e2009-05-22 14:51:39 -04004042 /* Reset some HBA SLI setup states */
4043 lpfc_stop_hba_timers(phba);
4044 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05004045}
4046
James Smarte59058c2008-08-24 21:49:00 -04004047/**
James Smartda0436e2009-05-22 14:51:39 -04004048 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05004049 * @phba: pointer to lpfc hba data structure.
4050 *
James Smartda0436e2009-05-22 14:51:39 -04004051 * This routine is invoked to stop an SLI4 device port, it stops the device
4052 * from generating interrupts and stops the device driver's timers for the
4053 * device.
4054 **/
4055static void
4056lpfc_stop_port_s4(struct lpfc_hba *phba)
4057{
4058 /* Reset some HBA SLI4 setup states */
4059 lpfc_stop_hba_timers(phba);
4060 phba->pport->work_port_events = 0;
4061 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04004062}
4063
4064/**
4065 * lpfc_stop_port - Wrapper function for stopping hba port
4066 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05004067 *
James Smartda0436e2009-05-22 14:51:39 -04004068 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
4069 * the API jump table function pointer from the lpfc_hba struct.
4070 **/
4071void
4072lpfc_stop_port(struct lpfc_hba *phba)
4073{
4074 phba->lpfc_stop_port(phba);
4075}
4076
4077/**
James Smartecfd03c2010-02-12 14:41:27 -05004078 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
4079 * @phba: Pointer to hba for which this call is being executed.
4080 *
4081 * This routine starts the timer waiting for the FCF rediscovery to complete.
4082 **/
4083void
4084lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
4085{
4086 unsigned long fcf_redisc_wait_tmo =
4087 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
4088 /* Start fcf rediscovery wait period timer */
4089 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
4090 spin_lock_irq(&phba->hbalock);
4091 /* Allow action to new fcf asynchronous event */
4092 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
4093 /* Mark the FCF rediscovery pending state */
4094 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
4095 spin_unlock_irq(&phba->hbalock);
4096}
4097
4098/**
4099 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
4100 * @ptr: Map to lpfc_hba data structure pointer.
4101 *
4102 * This routine is invoked when waiting for FCF table rediscover has been
4103 * timed out. If new FCF record(s) has (have) been discovered during the
4104 * wait period, a new FCF event shall be added to the FCOE async event
4105 * list, and then worker thread shall be waked up for processing from the
4106 * worker thread context.
4107 **/
Rashika Kheriae399b222014-09-03 12:55:28 -04004108static void
James Smartecfd03c2010-02-12 14:41:27 -05004109lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
4110{
4111 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
4112
4113 /* Don't send FCF rediscovery event if timer cancelled */
4114 spin_lock_irq(&phba->hbalock);
4115 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
4116 spin_unlock_irq(&phba->hbalock);
4117 return;
4118 }
4119 /* Clear FCF rediscovery timer pending flag */
4120 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
4121 /* FCF rediscovery event to worker thread */
4122 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
4123 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004124 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04004125 "2776 FCF rediscover quiescent timer expired\n");
James Smartecfd03c2010-02-12 14:41:27 -05004126 /* wake up worker thread */
4127 lpfc_worker_wake_up(phba);
4128}
4129
4130/**
James Smartda0436e2009-05-22 14:51:39 -04004131 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
4132 * @phba: pointer to lpfc hba data structure.
4133 * @acqe_link: pointer to the async link completion queue entry.
4134 *
4135 * This routine is to parse the SLI4 link-attention link fault code and
4136 * translate it into the base driver's read link attention mailbox command
4137 * status.
4138 *
4139 * Return: Link-attention status in terms of base driver's coding.
4140 **/
4141static uint16_t
4142lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
4143 struct lpfc_acqe_link *acqe_link)
4144{
4145 uint16_t latt_fault;
4146
4147 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
4148 case LPFC_ASYNC_LINK_FAULT_NONE:
4149 case LPFC_ASYNC_LINK_FAULT_LOCAL:
4150 case LPFC_ASYNC_LINK_FAULT_REMOTE:
4151 latt_fault = 0;
4152 break;
4153 default:
4154 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4155 "0398 Invalid link fault code: x%x\n",
4156 bf_get(lpfc_acqe_link_fault, acqe_link));
4157 latt_fault = MBXERR_ERROR;
4158 break;
4159 }
4160 return latt_fault;
4161}
4162
4163/**
4164 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
4165 * @phba: pointer to lpfc hba data structure.
4166 * @acqe_link: pointer to the async link completion queue entry.
4167 *
4168 * This routine is to parse the SLI4 link attention type and translate it
4169 * into the base driver's link attention type coding.
4170 *
4171 * Return: Link attention type in terms of base driver's coding.
4172 **/
4173static uint8_t
4174lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
4175 struct lpfc_acqe_link *acqe_link)
4176{
4177 uint8_t att_type;
4178
4179 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
4180 case LPFC_ASYNC_LINK_STATUS_DOWN:
4181 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
James Smart76a95d72010-11-20 23:11:48 -05004182 att_type = LPFC_ATT_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04004183 break;
4184 case LPFC_ASYNC_LINK_STATUS_UP:
4185 /* Ignore physical link up events - wait for logical link up */
James Smart76a95d72010-11-20 23:11:48 -05004186 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04004187 break;
4188 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
James Smart76a95d72010-11-20 23:11:48 -05004189 att_type = LPFC_ATT_LINK_UP;
James Smartda0436e2009-05-22 14:51:39 -04004190 break;
4191 default:
4192 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4193 "0399 Invalid link attention type: x%x\n",
4194 bf_get(lpfc_acqe_link_status, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05004195 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04004196 break;
4197 }
4198 return att_type;
4199}
4200
4201/**
James Smart8b68cd52012-09-29 11:32:37 -04004202 * lpfc_sli_port_speed_get - Get sli3 link speed code to link speed
4203 * @phba: pointer to lpfc hba data structure.
4204 *
4205 * This routine is to get an SLI3 FC port's link speed in Mbps.
4206 *
4207 * Return: link speed in terms of Mbps.
4208 **/
4209uint32_t
4210lpfc_sli_port_speed_get(struct lpfc_hba *phba)
4211{
4212 uint32_t link_speed;
4213
4214 if (!lpfc_is_link_up(phba))
4215 return 0;
4216
James Smarta085e872015-12-16 18:12:02 -05004217 if (phba->sli_rev <= LPFC_SLI_REV3) {
4218 switch (phba->fc_linkspeed) {
4219 case LPFC_LINK_SPEED_1GHZ:
4220 link_speed = 1000;
4221 break;
4222 case LPFC_LINK_SPEED_2GHZ:
4223 link_speed = 2000;
4224 break;
4225 case LPFC_LINK_SPEED_4GHZ:
4226 link_speed = 4000;
4227 break;
4228 case LPFC_LINK_SPEED_8GHZ:
4229 link_speed = 8000;
4230 break;
4231 case LPFC_LINK_SPEED_10GHZ:
4232 link_speed = 10000;
4233 break;
4234 case LPFC_LINK_SPEED_16GHZ:
4235 link_speed = 16000;
4236 break;
4237 default:
4238 link_speed = 0;
4239 }
4240 } else {
4241 if (phba->sli4_hba.link_state.logical_speed)
4242 link_speed =
4243 phba->sli4_hba.link_state.logical_speed;
4244 else
4245 link_speed = phba->sli4_hba.link_state.speed;
James Smart8b68cd52012-09-29 11:32:37 -04004246 }
4247 return link_speed;
4248}
4249
4250/**
4251 * lpfc_sli4_port_speed_parse - Parse async evt link speed code to link speed
4252 * @phba: pointer to lpfc hba data structure.
4253 * @evt_code: asynchronous event code.
4254 * @speed_code: asynchronous event link speed code.
4255 *
4256 * This routine is to parse the giving SLI4 async event link speed code into
4257 * value of Mbps for the link speed.
4258 *
4259 * Return: link speed in terms of Mbps.
4260 **/
4261static uint32_t
4262lpfc_sli4_port_speed_parse(struct lpfc_hba *phba, uint32_t evt_code,
4263 uint8_t speed_code)
4264{
4265 uint32_t port_speed;
4266
4267 switch (evt_code) {
4268 case LPFC_TRAILER_CODE_LINK:
4269 switch (speed_code) {
James Smart26d830e2015-04-07 15:07:17 -04004270 case LPFC_ASYNC_LINK_SPEED_ZERO:
James Smart8b68cd52012-09-29 11:32:37 -04004271 port_speed = 0;
4272 break;
James Smart26d830e2015-04-07 15:07:17 -04004273 case LPFC_ASYNC_LINK_SPEED_10MBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004274 port_speed = 10;
4275 break;
James Smart26d830e2015-04-07 15:07:17 -04004276 case LPFC_ASYNC_LINK_SPEED_100MBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004277 port_speed = 100;
4278 break;
James Smart26d830e2015-04-07 15:07:17 -04004279 case LPFC_ASYNC_LINK_SPEED_1GBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004280 port_speed = 1000;
4281 break;
James Smart26d830e2015-04-07 15:07:17 -04004282 case LPFC_ASYNC_LINK_SPEED_10GBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004283 port_speed = 10000;
4284 break;
James Smart26d830e2015-04-07 15:07:17 -04004285 case LPFC_ASYNC_LINK_SPEED_20GBPS:
4286 port_speed = 20000;
4287 break;
4288 case LPFC_ASYNC_LINK_SPEED_25GBPS:
4289 port_speed = 25000;
4290 break;
4291 case LPFC_ASYNC_LINK_SPEED_40GBPS:
4292 port_speed = 40000;
4293 break;
James Smart8b68cd52012-09-29 11:32:37 -04004294 default:
4295 port_speed = 0;
4296 }
4297 break;
4298 case LPFC_TRAILER_CODE_FC:
4299 switch (speed_code) {
James Smart26d830e2015-04-07 15:07:17 -04004300 case LPFC_FC_LA_SPEED_UNKNOWN:
James Smart8b68cd52012-09-29 11:32:37 -04004301 port_speed = 0;
4302 break;
James Smart26d830e2015-04-07 15:07:17 -04004303 case LPFC_FC_LA_SPEED_1G:
James Smart8b68cd52012-09-29 11:32:37 -04004304 port_speed = 1000;
4305 break;
James Smart26d830e2015-04-07 15:07:17 -04004306 case LPFC_FC_LA_SPEED_2G:
James Smart8b68cd52012-09-29 11:32:37 -04004307 port_speed = 2000;
4308 break;
James Smart26d830e2015-04-07 15:07:17 -04004309 case LPFC_FC_LA_SPEED_4G:
James Smart8b68cd52012-09-29 11:32:37 -04004310 port_speed = 4000;
4311 break;
James Smart26d830e2015-04-07 15:07:17 -04004312 case LPFC_FC_LA_SPEED_8G:
James Smart8b68cd52012-09-29 11:32:37 -04004313 port_speed = 8000;
4314 break;
James Smart26d830e2015-04-07 15:07:17 -04004315 case LPFC_FC_LA_SPEED_10G:
James Smart8b68cd52012-09-29 11:32:37 -04004316 port_speed = 10000;
4317 break;
James Smart26d830e2015-04-07 15:07:17 -04004318 case LPFC_FC_LA_SPEED_16G:
James Smart8b68cd52012-09-29 11:32:37 -04004319 port_speed = 16000;
4320 break;
James Smartd38dd522015-08-31 16:48:17 -04004321 case LPFC_FC_LA_SPEED_32G:
4322 port_speed = 32000;
4323 break;
James Smart8b68cd52012-09-29 11:32:37 -04004324 default:
4325 port_speed = 0;
4326 }
4327 break;
4328 default:
4329 port_speed = 0;
4330 }
4331 return port_speed;
4332}
4333
4334/**
James Smart70f3c072010-12-15 17:57:33 -05004335 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
James Smartda0436e2009-05-22 14:51:39 -04004336 * @phba: pointer to lpfc hba data structure.
4337 * @acqe_link: pointer to the async link completion queue entry.
4338 *
James Smart70f3c072010-12-15 17:57:33 -05004339 * This routine is to handle the SLI4 asynchronous FCoE link event.
James Smartda0436e2009-05-22 14:51:39 -04004340 **/
4341static void
4342lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
4343 struct lpfc_acqe_link *acqe_link)
4344{
4345 struct lpfc_dmabuf *mp;
4346 LPFC_MBOXQ_t *pmb;
4347 MAILBOX_t *mb;
James Smart76a95d72010-11-20 23:11:48 -05004348 struct lpfc_mbx_read_top *la;
James Smartda0436e2009-05-22 14:51:39 -04004349 uint8_t att_type;
James Smart76a95d72010-11-20 23:11:48 -05004350 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004351
4352 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05004353 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
James Smartda0436e2009-05-22 14:51:39 -04004354 return;
James Smart32b97932009-07-19 10:01:21 -04004355 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004356 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4357 if (!pmb) {
4358 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4359 "0395 The mboxq allocation failed\n");
4360 return;
4361 }
4362 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4363 if (!mp) {
4364 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4365 "0396 The lpfc_dmabuf allocation failed\n");
4366 goto out_free_pmb;
4367 }
4368 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
4369 if (!mp->virt) {
4370 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4371 "0397 The mbuf allocation failed\n");
4372 goto out_free_dmabuf;
4373 }
4374
4375 /* Cleanup any outstanding ELS commands */
4376 lpfc_els_flush_all_cmd(phba);
4377
4378 /* Block ELS IOCBs until we have done process link event */
James Smart895427b2017-02-12 13:52:30 -08004379 phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
James Smartda0436e2009-05-22 14:51:39 -04004380
4381 /* Update link event statistics */
4382 phba->sli.slistat.link_event++;
4383
James Smart76a95d72010-11-20 23:11:48 -05004384 /* Create lpfc_handle_latt mailbox command from link ACQE */
4385 lpfc_read_topology(phba, pmb, mp);
4386 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smartda0436e2009-05-22 14:51:39 -04004387 pmb->vport = phba->pport;
4388
James Smartda0436e2009-05-22 14:51:39 -04004389 /* Keep the link status for extra SLI4 state machine reference */
4390 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04004391 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_LINK,
4392 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smartda0436e2009-05-22 14:51:39 -04004393 phba->sli4_hba.link_state.duplex =
4394 bf_get(lpfc_acqe_link_duplex, acqe_link);
4395 phba->sli4_hba.link_state.status =
4396 bf_get(lpfc_acqe_link_status, acqe_link);
James Smart70f3c072010-12-15 17:57:33 -05004397 phba->sli4_hba.link_state.type =
4398 bf_get(lpfc_acqe_link_type, acqe_link);
4399 phba->sli4_hba.link_state.number =
4400 bf_get(lpfc_acqe_link_number, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04004401 phba->sli4_hba.link_state.fault =
4402 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05004403 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04004404 bf_get(lpfc_acqe_logical_link_speed, acqe_link) * 10;
4405
James Smart70f3c072010-12-15 17:57:33 -05004406 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartc31098c2011-04-16 11:03:33 -04004407 "2900 Async FC/FCoE Link event - Speed:%dGBit "
4408 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
4409 "Logical speed:%dMbps Fault:%d\n",
James Smart70f3c072010-12-15 17:57:33 -05004410 phba->sli4_hba.link_state.speed,
4411 phba->sli4_hba.link_state.topology,
4412 phba->sli4_hba.link_state.status,
4413 phba->sli4_hba.link_state.type,
4414 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04004415 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05004416 phba->sli4_hba.link_state.fault);
James Smart76a95d72010-11-20 23:11:48 -05004417 /*
4418 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
4419 * topology info. Note: Optional for non FC-AL ports.
4420 */
4421 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
4422 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4423 if (rc == MBX_NOT_FINISHED)
4424 goto out_free_dmabuf;
4425 return;
4426 }
4427 /*
4428 * For FCoE Mode: fill in all the topology information we need and call
4429 * the READ_TOPOLOGY completion routine to continue without actually
4430 * sending the READ_TOPOLOGY mailbox command to the port.
4431 */
4432 /* Parse and translate status field */
4433 mb = &pmb->u.mb;
4434 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
4435
4436 /* Parse and translate link attention fields */
4437 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
4438 la->eventTag = acqe_link->event_tag;
4439 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
4440 bf_set(lpfc_mbx_read_top_link_spd, la,
James Smarta085e872015-12-16 18:12:02 -05004441 (bf_get(lpfc_acqe_link_speed, acqe_link)));
James Smart76a95d72010-11-20 23:11:48 -05004442
4443 /* Fake the the following irrelvant fields */
4444 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
4445 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
4446 bf_set(lpfc_mbx_read_top_il, la, 0);
4447 bf_set(lpfc_mbx_read_top_pb, la, 0);
4448 bf_set(lpfc_mbx_read_top_fa, la, 0);
4449 bf_set(lpfc_mbx_read_top_mm, la, 0);
James Smartda0436e2009-05-22 14:51:39 -04004450
4451 /* Invoke the lpfc_handle_latt mailbox command callback function */
James Smart76a95d72010-11-20 23:11:48 -05004452 lpfc_mbx_cmpl_read_topology(phba, pmb);
James Smartda0436e2009-05-22 14:51:39 -04004453
4454 return;
4455
4456out_free_dmabuf:
4457 kfree(mp);
4458out_free_pmb:
4459 mempool_free(pmb, phba->mbox_mem_pool);
4460}
4461
4462/**
James Smart70f3c072010-12-15 17:57:33 -05004463 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
4464 * @phba: pointer to lpfc hba data structure.
4465 * @acqe_fc: pointer to the async fc completion queue entry.
4466 *
4467 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
4468 * that the event was received and then issue a read_topology mailbox command so
4469 * that the rest of the driver will treat it the same as SLI3.
4470 **/
4471static void
4472lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
4473{
4474 struct lpfc_dmabuf *mp;
4475 LPFC_MBOXQ_t *pmb;
James Smart7bdedb32016-07-06 12:36:00 -07004476 MAILBOX_t *mb;
4477 struct lpfc_mbx_read_top *la;
James Smart70f3c072010-12-15 17:57:33 -05004478 int rc;
4479
4480 if (bf_get(lpfc_trailer_type, acqe_fc) !=
4481 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
4482 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4483 "2895 Non FC link Event detected.(%d)\n",
4484 bf_get(lpfc_trailer_type, acqe_fc));
4485 return;
4486 }
4487 /* Keep the link status for extra SLI4 state machine reference */
4488 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04004489 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
4490 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
James Smart70f3c072010-12-15 17:57:33 -05004491 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
4492 phba->sli4_hba.link_state.topology =
4493 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
4494 phba->sli4_hba.link_state.status =
4495 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
4496 phba->sli4_hba.link_state.type =
4497 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
4498 phba->sli4_hba.link_state.number =
4499 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
4500 phba->sli4_hba.link_state.fault =
4501 bf_get(lpfc_acqe_link_fault, acqe_fc);
4502 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04004503 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
James Smart70f3c072010-12-15 17:57:33 -05004504 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4505 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
4506 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
4507 "%dMbps Fault:%d\n",
4508 phba->sli4_hba.link_state.speed,
4509 phba->sli4_hba.link_state.topology,
4510 phba->sli4_hba.link_state.status,
4511 phba->sli4_hba.link_state.type,
4512 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04004513 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05004514 phba->sli4_hba.link_state.fault);
4515 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4516 if (!pmb) {
4517 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4518 "2897 The mboxq allocation failed\n");
4519 return;
4520 }
4521 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4522 if (!mp) {
4523 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4524 "2898 The lpfc_dmabuf allocation failed\n");
4525 goto out_free_pmb;
4526 }
4527 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
4528 if (!mp->virt) {
4529 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4530 "2899 The mbuf allocation failed\n");
4531 goto out_free_dmabuf;
4532 }
4533
4534 /* Cleanup any outstanding ELS commands */
4535 lpfc_els_flush_all_cmd(phba);
4536
4537 /* Block ELS IOCBs until we have done process link event */
James Smart895427b2017-02-12 13:52:30 -08004538 phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
James Smart70f3c072010-12-15 17:57:33 -05004539
4540 /* Update link event statistics */
4541 phba->sli.slistat.link_event++;
4542
4543 /* Create lpfc_handle_latt mailbox command from link ACQE */
4544 lpfc_read_topology(phba, pmb, mp);
4545 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
4546 pmb->vport = phba->pport;
4547
James Smart7bdedb32016-07-06 12:36:00 -07004548 if (phba->sli4_hba.link_state.status != LPFC_FC_LA_TYPE_LINK_UP) {
4549 /* Parse and translate status field */
4550 mb = &pmb->u.mb;
4551 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba,
4552 (void *)acqe_fc);
4553
4554 /* Parse and translate link attention fields */
4555 la = (struct lpfc_mbx_read_top *)&pmb->u.mb.un.varReadTop;
4556 la->eventTag = acqe_fc->event_tag;
James Smart7bdedb32016-07-06 12:36:00 -07004557
James Smartaeb3c812017-04-21 16:05:02 -07004558 if (phba->sli4_hba.link_state.status ==
4559 LPFC_FC_LA_TYPE_UNEXP_WWPN) {
4560 bf_set(lpfc_mbx_read_top_att_type, la,
4561 LPFC_FC_LA_TYPE_UNEXP_WWPN);
4562 } else {
4563 bf_set(lpfc_mbx_read_top_att_type, la,
4564 LPFC_FC_LA_TYPE_LINK_DOWN);
4565 }
James Smart7bdedb32016-07-06 12:36:00 -07004566 /* Invoke the mailbox command callback function */
4567 lpfc_mbx_cmpl_read_topology(phba, pmb);
4568
4569 return;
4570 }
4571
James Smart70f3c072010-12-15 17:57:33 -05004572 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4573 if (rc == MBX_NOT_FINISHED)
4574 goto out_free_dmabuf;
4575 return;
4576
4577out_free_dmabuf:
4578 kfree(mp);
4579out_free_pmb:
4580 mempool_free(pmb, phba->mbox_mem_pool);
4581}
4582
4583/**
4584 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
4585 * @phba: pointer to lpfc hba data structure.
4586 * @acqe_fc: pointer to the async SLI completion queue entry.
4587 *
4588 * This routine is to handle the SLI4 asynchronous SLI events.
4589 **/
4590static void
4591lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
4592{
James Smart4b8bae02012-06-12 13:55:07 -04004593 char port_name;
James Smart8c1312e2012-10-31 14:45:09 -04004594 char message[128];
James Smart4b8bae02012-06-12 13:55:07 -04004595 uint8_t status;
James Smart946727d2015-04-07 15:07:09 -04004596 uint8_t evt_type;
James Smart448193b2015-12-16 18:12:05 -05004597 uint8_t operational = 0;
James Smart946727d2015-04-07 15:07:09 -04004598 struct temp_event temp_event_data;
James Smart4b8bae02012-06-12 13:55:07 -04004599 struct lpfc_acqe_misconfigured_event *misconfigured;
James Smart946727d2015-04-07 15:07:09 -04004600 struct Scsi_Host *shost;
James Smart4b8bae02012-06-12 13:55:07 -04004601
James Smart946727d2015-04-07 15:07:09 -04004602 evt_type = bf_get(lpfc_trailer_type, acqe_sli);
4603
James Smart448193b2015-12-16 18:12:05 -05004604 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4605 "2901 Async SLI event - Event Data1:x%08x Event Data2:"
4606 "x%08x SLI Event Type:%d\n",
4607 acqe_sli->event_data1, acqe_sli->event_data2,
4608 evt_type);
James Smart4b8bae02012-06-12 13:55:07 -04004609
4610 port_name = phba->Port[0];
4611 if (port_name == 0x00)
4612 port_name = '?'; /* get port name is empty */
4613
James Smart946727d2015-04-07 15:07:09 -04004614 switch (evt_type) {
4615 case LPFC_SLI_EVENT_TYPE_OVER_TEMP:
4616 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
4617 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
4618 temp_event_data.data = (uint32_t)acqe_sli->event_data1;
4619
4620 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4621 "3190 Over Temperature:%d Celsius- Port Name %c\n",
4622 acqe_sli->event_data1, port_name);
4623
James Smart310429e2016-07-06 12:35:54 -07004624 phba->sfp_warning |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
James Smart946727d2015-04-07 15:07:09 -04004625 shost = lpfc_shost_from_vport(phba->pport);
4626 fc_host_post_vendor_event(shost, fc_get_event_number(),
4627 sizeof(temp_event_data),
4628 (char *)&temp_event_data,
4629 SCSI_NL_VID_TYPE_PCI
4630 | PCI_VENDOR_ID_EMULEX);
4631 break;
4632 case LPFC_SLI_EVENT_TYPE_NORM_TEMP:
4633 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
4634 temp_event_data.event_code = LPFC_NORMAL_TEMP;
4635 temp_event_data.data = (uint32_t)acqe_sli->event_data1;
4636
4637 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4638 "3191 Normal Temperature:%d Celsius - Port Name %c\n",
4639 acqe_sli->event_data1, port_name);
4640
4641 shost = lpfc_shost_from_vport(phba->pport);
4642 fc_host_post_vendor_event(shost, fc_get_event_number(),
4643 sizeof(temp_event_data),
4644 (char *)&temp_event_data,
4645 SCSI_NL_VID_TYPE_PCI
4646 | PCI_VENDOR_ID_EMULEX);
4647 break;
4648 case LPFC_SLI_EVENT_TYPE_MISCONFIGURED:
4649 misconfigured = (struct lpfc_acqe_misconfigured_event *)
James Smart4b8bae02012-06-12 13:55:07 -04004650 &acqe_sli->event_data1;
4651
James Smart946727d2015-04-07 15:07:09 -04004652 /* fetch the status for this port */
4653 switch (phba->sli4_hba.lnk_info.lnk_no) {
4654 case LPFC_LINK_NUMBER_0:
James Smart448193b2015-12-16 18:12:05 -05004655 status = bf_get(lpfc_sli_misconfigured_port0_state,
4656 &misconfigured->theEvent);
4657 operational = bf_get(lpfc_sli_misconfigured_port0_op,
James Smart4b8bae02012-06-12 13:55:07 -04004658 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04004659 break;
4660 case LPFC_LINK_NUMBER_1:
James Smart448193b2015-12-16 18:12:05 -05004661 status = bf_get(lpfc_sli_misconfigured_port1_state,
4662 &misconfigured->theEvent);
4663 operational = bf_get(lpfc_sli_misconfigured_port1_op,
James Smart4b8bae02012-06-12 13:55:07 -04004664 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04004665 break;
4666 case LPFC_LINK_NUMBER_2:
James Smart448193b2015-12-16 18:12:05 -05004667 status = bf_get(lpfc_sli_misconfigured_port2_state,
4668 &misconfigured->theEvent);
4669 operational = bf_get(lpfc_sli_misconfigured_port2_op,
James Smart4b8bae02012-06-12 13:55:07 -04004670 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04004671 break;
4672 case LPFC_LINK_NUMBER_3:
James Smart448193b2015-12-16 18:12:05 -05004673 status = bf_get(lpfc_sli_misconfigured_port3_state,
4674 &misconfigured->theEvent);
4675 operational = bf_get(lpfc_sli_misconfigured_port3_op,
James Smart4b8bae02012-06-12 13:55:07 -04004676 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04004677 break;
4678 default:
James Smart448193b2015-12-16 18:12:05 -05004679 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4680 "3296 "
4681 "LPFC_SLI_EVENT_TYPE_MISCONFIGURED "
4682 "event: Invalid link %d",
4683 phba->sli4_hba.lnk_info.lnk_no);
4684 return;
James Smart946727d2015-04-07 15:07:09 -04004685 }
James Smart4b8bae02012-06-12 13:55:07 -04004686
James Smart448193b2015-12-16 18:12:05 -05004687 /* Skip if optic state unchanged */
4688 if (phba->sli4_hba.lnk_info.optic_state == status)
4689 return;
4690
James Smart946727d2015-04-07 15:07:09 -04004691 switch (status) {
4692 case LPFC_SLI_EVENT_STATUS_VALID:
James Smart448193b2015-12-16 18:12:05 -05004693 sprintf(message, "Physical Link is functional");
4694 break;
James Smart946727d2015-04-07 15:07:09 -04004695 case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
4696 sprintf(message, "Optics faulted/incorrectly "
4697 "installed/not installed - Reseat optics, "
4698 "if issue not resolved, replace.");
4699 break;
4700 case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
4701 sprintf(message,
4702 "Optics of two types installed - Remove one "
4703 "optic or install matching pair of optics.");
4704 break;
4705 case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
4706 sprintf(message, "Incompatible optics - Replace with "
James Smart292098b2012-09-29 11:31:41 -04004707 "compatible optics for card to function.");
James Smart946727d2015-04-07 15:07:09 -04004708 break;
James Smart448193b2015-12-16 18:12:05 -05004709 case LPFC_SLI_EVENT_STATUS_UNQUALIFIED:
4710 sprintf(message, "Unqualified optics - Replace with "
4711 "Avago optics for Warranty and Technical "
4712 "Support - Link is%s operational",
James Smart2ea259e2017-02-12 13:52:27 -08004713 (operational) ? " not" : "");
James Smart448193b2015-12-16 18:12:05 -05004714 break;
4715 case LPFC_SLI_EVENT_STATUS_UNCERTIFIED:
4716 sprintf(message, "Uncertified optics - Replace with "
4717 "Avago-certified optics to enable link "
4718 "operation - Link is%s operational",
James Smart2ea259e2017-02-12 13:52:27 -08004719 (operational) ? " not" : "");
James Smart448193b2015-12-16 18:12:05 -05004720 break;
James Smart946727d2015-04-07 15:07:09 -04004721 default:
4722 /* firmware is reporting a status we don't know about */
4723 sprintf(message, "Unknown event status x%02x", status);
4724 break;
4725 }
James Smart448193b2015-12-16 18:12:05 -05004726 phba->sli4_hba.lnk_info.optic_state = status;
James Smart946727d2015-04-07 15:07:09 -04004727 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart448193b2015-12-16 18:12:05 -05004728 "3176 Port Name %c %s\n", port_name, message);
James Smart946727d2015-04-07 15:07:09 -04004729 break;
4730 case LPFC_SLI_EVENT_TYPE_REMOTE_DPORT:
4731 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4732 "3192 Remote DPort Test Initiated - "
4733 "Event Data1:x%08x Event Data2: x%08x\n",
4734 acqe_sli->event_data1, acqe_sli->event_data2);
James Smart4b8bae02012-06-12 13:55:07 -04004735 break;
4736 default:
James Smart946727d2015-04-07 15:07:09 -04004737 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4738 "3193 Async SLI event - Event Data1:x%08x Event Data2:"
4739 "x%08x SLI Event Type:%d\n",
4740 acqe_sli->event_data1, acqe_sli->event_data2,
4741 evt_type);
James Smart4b8bae02012-06-12 13:55:07 -04004742 break;
4743 }
James Smart70f3c072010-12-15 17:57:33 -05004744}
4745
4746/**
James Smartfc2b9892010-02-26 14:15:29 -05004747 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
4748 * @vport: pointer to vport data structure.
4749 *
4750 * This routine is to perform Clear Virtual Link (CVL) on a vport in
4751 * response to a CVL event.
4752 *
4753 * Return the pointer to the ndlp with the vport if successful, otherwise
4754 * return NULL.
4755 **/
4756static struct lpfc_nodelist *
4757lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
4758{
4759 struct lpfc_nodelist *ndlp;
4760 struct Scsi_Host *shost;
4761 struct lpfc_hba *phba;
4762
4763 if (!vport)
4764 return NULL;
James Smartfc2b9892010-02-26 14:15:29 -05004765 phba = vport->phba;
4766 if (!phba)
4767 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04004768 ndlp = lpfc_findnode_did(vport, Fabric_DID);
4769 if (!ndlp) {
4770 /* Cannot find existing Fabric ndlp, so allocate a new one */
James Smart9d3d3402017-04-21 16:05:00 -07004771 ndlp = lpfc_nlp_init(vport, Fabric_DID);
James Smart78730cf2010-04-06 15:06:30 -04004772 if (!ndlp)
4773 return 0;
James Smart78730cf2010-04-06 15:06:30 -04004774 /* Set the node type */
4775 ndlp->nlp_type |= NLP_FABRIC;
4776 /* Put ndlp onto node list */
4777 lpfc_enqueue_node(vport, ndlp);
4778 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
4779 /* re-setup ndlp without removing from node list */
4780 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
4781 if (!ndlp)
4782 return 0;
4783 }
James Smart63e801c2010-11-20 23:14:19 -05004784 if ((phba->pport->port_state < LPFC_FLOGI) &&
4785 (phba->pport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05004786 return NULL;
4787 /* If virtual link is not yet instantiated ignore CVL */
James Smart63e801c2010-11-20 23:14:19 -05004788 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
4789 && (vport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05004790 return NULL;
4791 shost = lpfc_shost_from_vport(vport);
4792 if (!shost)
4793 return NULL;
4794 lpfc_linkdown_port(vport);
4795 lpfc_cleanup_pending_mbox(vport);
4796 spin_lock_irq(shost->host_lock);
4797 vport->fc_flag |= FC_VPORT_CVL_RCVD;
4798 spin_unlock_irq(shost->host_lock);
4799
4800 return ndlp;
4801}
4802
4803/**
4804 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
4805 * @vport: pointer to lpfc hba data structure.
4806 *
4807 * This routine is to perform Clear Virtual Link (CVL) on all vports in
4808 * response to a FCF dead event.
4809 **/
4810static void
4811lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
4812{
4813 struct lpfc_vport **vports;
4814 int i;
4815
4816 vports = lpfc_create_vport_work_array(phba);
4817 if (vports)
4818 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
4819 lpfc_sli4_perform_vport_cvl(vports[i]);
4820 lpfc_destroy_vport_work_array(phba, vports);
4821}
4822
4823/**
James Smart76a95d72010-11-20 23:11:48 -05004824 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
James Smartda0436e2009-05-22 14:51:39 -04004825 * @phba: pointer to lpfc hba data structure.
4826 * @acqe_link: pointer to the async fcoe completion queue entry.
4827 *
4828 * This routine is to handle the SLI4 asynchronous fcoe event.
4829 **/
4830static void
James Smart76a95d72010-11-20 23:11:48 -05004831lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
James Smart70f3c072010-12-15 17:57:33 -05004832 struct lpfc_acqe_fip *acqe_fip)
James Smartda0436e2009-05-22 14:51:39 -04004833{
James Smart70f3c072010-12-15 17:57:33 -05004834 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004835 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04004836 struct lpfc_vport *vport;
4837 struct lpfc_nodelist *ndlp;
4838 struct Scsi_Host *shost;
James Smart695a8142010-01-26 23:08:03 -05004839 int active_vlink_present;
4840 struct lpfc_vport **vports;
4841 int i;
James Smartda0436e2009-05-22 14:51:39 -04004842
James Smart70f3c072010-12-15 17:57:33 -05004843 phba->fc_eventTag = acqe_fip->event_tag;
4844 phba->fcoe_eventtag = acqe_fip->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004845 switch (event_type) {
James Smart70f3c072010-12-15 17:57:33 -05004846 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
4847 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
4848 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
James Smart999d8132010-03-15 11:24:56 -04004849 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4850 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004851 "2546 New FCF event, evt_tag:x%x, "
4852 "index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004853 acqe_fip->event_tag,
4854 acqe_fip->index);
James Smart999d8132010-03-15 11:24:56 -04004855 else
4856 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
4857 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004858 "2788 FCF param modified event, "
4859 "evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004860 acqe_fip->event_tag,
4861 acqe_fip->index);
James Smart38b92ef2010-08-04 16:11:39 -04004862 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004863 /*
4864 * During period of FCF discovery, read the FCF
4865 * table record indexed by the event to update
James Smarta93ff372010-10-22 11:06:08 -04004866 * FCF roundrobin failover eligible FCF bmask.
James Smart0c9ab6f2010-02-26 14:15:57 -05004867 */
4868 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4869 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004870 "2779 Read FCF (x%x) for updating "
4871 "roundrobin FCF failover bmask\n",
James Smart70f3c072010-12-15 17:57:33 -05004872 acqe_fip->index);
4873 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05004874 }
James Smart38b92ef2010-08-04 16:11:39 -04004875
4876 /* If the FCF discovery is in progress, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04004877 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04004878 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart38b92ef2010-08-04 16:11:39 -04004879 spin_unlock_irq(&phba->hbalock);
4880 break;
4881 }
4882 /* If fast FCF failover rescan event is pending, do nothing */
4883 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
4884 spin_unlock_irq(&phba->hbalock);
4885 break;
4886 }
4887
James Smartc2b97122013-05-31 17:05:36 -04004888 /* If the FCF has been in discovered state, do nothing. */
4889 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
James Smart3804dc82010-07-14 15:31:37 -04004890 spin_unlock_irq(&phba->hbalock);
4891 break;
4892 }
4893 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04004894
James Smart0c9ab6f2010-02-26 14:15:57 -05004895 /* Otherwise, scan the entire FCF table and re-discover SAN */
4896 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004897 "2770 Start FCF table scan per async FCF "
4898 "event, evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004899 acqe_fip->event_tag, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05004900 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
4901 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04004902 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05004903 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4904 "2547 Issue FCF scan read FCF mailbox "
James Smarta93ff372010-10-22 11:06:08 -04004905 "command failed (x%x)\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004906 break;
4907
James Smart70f3c072010-12-15 17:57:33 -05004908 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
James Smartda0436e2009-05-22 14:51:39 -04004909 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04004910 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004911 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
4912 acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04004913 break;
4914
James Smart70f3c072010-12-15 17:57:33 -05004915 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
James Smart80c17842012-03-01 22:35:45 -05004916 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05004917 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004918 "2549 FCF (x%x) disconnected from network, "
James Smart70f3c072010-12-15 17:57:33 -05004919 "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart38b92ef2010-08-04 16:11:39 -04004920 /*
4921 * If we are in the middle of FCF failover process, clear
4922 * the corresponding FCF bit in the roundrobin bitmap.
James Smartda0436e2009-05-22 14:51:39 -04004923 */
James Smartfc2b9892010-02-26 14:15:29 -05004924 spin_lock_irq(&phba->hbalock);
James Smarta1cadfe2016-07-06 12:36:02 -07004925 if ((phba->fcf.fcf_flag & FCF_DISCOVERY) &&
4926 (phba->fcf.current_rec.fcf_indx != acqe_fip->index)) {
James Smartfc2b9892010-02-26 14:15:29 -05004927 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004928 /* Update FLOGI FCF failover eligible FCF bmask */
James Smart70f3c072010-12-15 17:57:33 -05004929 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004930 break;
4931 }
James Smart38b92ef2010-08-04 16:11:39 -04004932 spin_unlock_irq(&phba->hbalock);
4933
4934 /* If the event is not for currently used fcf do nothing */
James Smart70f3c072010-12-15 17:57:33 -05004935 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
James Smart38b92ef2010-08-04 16:11:39 -04004936 break;
4937
4938 /*
4939 * Otherwise, request the port to rediscover the entire FCF
4940 * table for a fast recovery from case that the current FCF
4941 * is no longer valid as we are not in the middle of FCF
4942 * failover process already.
4943 */
James Smartc2b97122013-05-31 17:05:36 -04004944 spin_lock_irq(&phba->hbalock);
4945 /* Mark the fast failover process in progress */
4946 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
4947 spin_unlock_irq(&phba->hbalock);
4948
4949 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
4950 "2771 Start FCF fast failover process due to "
4951 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
4952 "\n", acqe_fip->event_tag, acqe_fip->index);
4953 rc = lpfc_sli4_redisc_fcf_table(phba);
4954 if (rc) {
4955 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4956 LOG_DISCOVERY,
4957 "2772 Issue FCF rediscover mabilbox "
4958 "command failed, fail through to FCF "
4959 "dead event\n");
4960 spin_lock_irq(&phba->hbalock);
4961 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
4962 spin_unlock_irq(&phba->hbalock);
4963 /*
4964 * Last resort will fail over by treating this
4965 * as a link down to FCF registration.
4966 */
4967 lpfc_sli4_fcf_dead_failthrough(phba);
4968 } else {
4969 /* Reset FCF roundrobin bmask for new discovery */
4970 lpfc_sli4_clear_fcf_rr_bmask(phba);
4971 /*
4972 * Handling fast FCF failover to a DEAD FCF event is
4973 * considered equalivant to receiving CVL to all vports.
4974 */
4975 lpfc_sli4_perform_all_vport_cvl(phba);
4976 }
James Smartda0436e2009-05-22 14:51:39 -04004977 break;
James Smart70f3c072010-12-15 17:57:33 -05004978 case LPFC_FIP_EVENT_TYPE_CVL:
James Smart80c17842012-03-01 22:35:45 -05004979 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05004980 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6669f9b2009-10-02 15:16:45 -04004981 "2718 Clear Virtual Link Received for VPI 0x%x"
James Smart70f3c072010-12-15 17:57:33 -05004982 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart6d368e52011-05-24 11:44:12 -04004983
James Smart6669f9b2009-10-02 15:16:45 -04004984 vport = lpfc_find_vport_by_vpid(phba,
James Smart5248a742011-07-22 18:37:06 -04004985 acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004986 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04004987 if (!ndlp)
4988 break;
James Smart695a8142010-01-26 23:08:03 -05004989 active_vlink_present = 0;
4990
4991 vports = lpfc_create_vport_work_array(phba);
4992 if (vports) {
4993 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
4994 i++) {
4995 if ((!(vports[i]->fc_flag &
4996 FC_VPORT_CVL_RCVD)) &&
4997 (vports[i]->port_state > LPFC_FDISC)) {
4998 active_vlink_present = 1;
4999 break;
5000 }
5001 }
5002 lpfc_destroy_vport_work_array(phba, vports);
5003 }
5004
James Smartcc823552015-05-21 13:55:26 -04005005 /*
5006 * Don't re-instantiate if vport is marked for deletion.
5007 * If we are here first then vport_delete is going to wait
5008 * for discovery to complete.
5009 */
5010 if (!(vport->load_flag & FC_UNLOADING) &&
5011 active_vlink_present) {
James Smart695a8142010-01-26 23:08:03 -05005012 /*
5013 * If there are other active VLinks present,
5014 * re-instantiate the Vlink using FDISC.
5015 */
James Smart256ec0d2013-04-17 20:14:58 -04005016 mod_timer(&ndlp->nlp_delayfunc,
5017 jiffies + msecs_to_jiffies(1000));
James Smartfc2b9892010-02-26 14:15:29 -05005018 shost = lpfc_shost_from_vport(vport);
James Smart6669f9b2009-10-02 15:16:45 -04005019 spin_lock_irq(shost->host_lock);
5020 ndlp->nlp_flag |= NLP_DELAY_TMO;
5021 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05005022 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
5023 vport->port_state = LPFC_FDISC;
5024 } else {
James Smartecfd03c2010-02-12 14:41:27 -05005025 /*
5026 * Otherwise, we request port to rediscover
5027 * the entire FCF table for a fast recovery
5028 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05005029 * is no longer valid if we are not already
5030 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05005031 */
James Smartfc2b9892010-02-26 14:15:29 -05005032 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005033 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05005034 spin_unlock_irq(&phba->hbalock);
5035 break;
5036 }
5037 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05005038 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05005039 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005040 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
5041 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005042 "2773 Start FCF failover per CVL, "
James Smart70f3c072010-12-15 17:57:33 -05005043 "evt_tag:x%x\n", acqe_fip->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05005044 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05005045 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05005046 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
5047 LOG_DISCOVERY,
5048 "2774 Issue FCF rediscover "
5049 "mabilbox command failed, "
5050 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05005051 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005052 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05005053 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05005054 /*
5055 * Last resort will be re-try on the
5056 * the current registered FCF entry.
5057 */
5058 lpfc_retry_pport_discovery(phba);
James Smart38b92ef2010-08-04 16:11:39 -04005059 } else
5060 /*
5061 * Reset FCF roundrobin bmask for new
5062 * discovery.
5063 */
James Smart7d791df2011-07-22 18:37:52 -04005064 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart6669f9b2009-10-02 15:16:45 -04005065 }
5066 break;
James Smartda0436e2009-05-22 14:51:39 -04005067 default:
5068 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5069 "0288 Unknown FCoE event type 0x%x event tag "
James Smart70f3c072010-12-15 17:57:33 -05005070 "0x%x\n", event_type, acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04005071 break;
5072 }
5073}
5074
5075/**
5076 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
5077 * @phba: pointer to lpfc hba data structure.
5078 * @acqe_link: pointer to the async dcbx completion queue entry.
5079 *
5080 * This routine is to handle the SLI4 asynchronous dcbx event.
5081 **/
5082static void
5083lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
5084 struct lpfc_acqe_dcbx *acqe_dcbx)
5085{
James Smart4d9ab992009-10-02 15:16:39 -04005086 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04005087 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5088 "0290 The SLI4 DCBX asynchronous event is not "
5089 "handled yet\n");
5090}
5091
5092/**
James Smartb19a0612010-04-06 14:48:51 -04005093 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
5094 * @phba: pointer to lpfc hba data structure.
5095 * @acqe_link: pointer to the async grp5 completion queue entry.
5096 *
5097 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
5098 * is an asynchronous notified of a logical link speed change. The Port
5099 * reports the logical link speed in units of 10Mbps.
5100 **/
5101static void
5102lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
5103 struct lpfc_acqe_grp5 *acqe_grp5)
5104{
5105 uint16_t prev_ll_spd;
5106
5107 phba->fc_eventTag = acqe_grp5->event_tag;
5108 phba->fcoe_eventtag = acqe_grp5->event_tag;
5109 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
5110 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04005111 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5)) * 10;
James Smartb19a0612010-04-06 14:48:51 -04005112 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5113 "2789 GRP5 Async Event: Updating logical link speed "
James Smart8b68cd52012-09-29 11:32:37 -04005114 "from %dMbps to %dMbps\n", prev_ll_spd,
5115 phba->sli4_hba.link_state.logical_speed);
James Smartb19a0612010-04-06 14:48:51 -04005116}
5117
5118/**
James Smartda0436e2009-05-22 14:51:39 -04005119 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
5120 * @phba: pointer to lpfc hba data structure.
5121 *
5122 * This routine is invoked by the worker thread to process all the pending
5123 * SLI4 asynchronous events.
5124 **/
5125void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
5126{
5127 struct lpfc_cq_event *cq_event;
5128
5129 /* First, declare the async event has been handled */
5130 spin_lock_irq(&phba->hbalock);
5131 phba->hba_flag &= ~ASYNC_EVENT;
5132 spin_unlock_irq(&phba->hbalock);
5133 /* Now, handle all the async events */
5134 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
5135 /* Get the first event from the head of the event queue */
5136 spin_lock_irq(&phba->hbalock);
5137 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
5138 cq_event, struct lpfc_cq_event, list);
5139 spin_unlock_irq(&phba->hbalock);
5140 /* Process the asynchronous event */
5141 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
5142 case LPFC_TRAILER_CODE_LINK:
5143 lpfc_sli4_async_link_evt(phba,
5144 &cq_event->cqe.acqe_link);
5145 break;
5146 case LPFC_TRAILER_CODE_FCOE:
James Smart70f3c072010-12-15 17:57:33 -05005147 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04005148 break;
5149 case LPFC_TRAILER_CODE_DCBX:
5150 lpfc_sli4_async_dcbx_evt(phba,
5151 &cq_event->cqe.acqe_dcbx);
5152 break;
James Smartb19a0612010-04-06 14:48:51 -04005153 case LPFC_TRAILER_CODE_GRP5:
5154 lpfc_sli4_async_grp5_evt(phba,
5155 &cq_event->cqe.acqe_grp5);
5156 break;
James Smart70f3c072010-12-15 17:57:33 -05005157 case LPFC_TRAILER_CODE_FC:
5158 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
5159 break;
5160 case LPFC_TRAILER_CODE_SLI:
5161 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
5162 break;
James Smartda0436e2009-05-22 14:51:39 -04005163 default:
5164 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5165 "1804 Invalid asynchrous event code: "
5166 "x%x\n", bf_get(lpfc_trailer_code,
5167 &cq_event->cqe.mcqe_cmpl));
5168 break;
5169 }
5170 /* Free the completion event processed to the free pool */
5171 lpfc_sli4_cq_event_release(phba, cq_event);
5172 }
5173}
5174
5175/**
James Smartecfd03c2010-02-12 14:41:27 -05005176 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
5177 * @phba: pointer to lpfc hba data structure.
5178 *
5179 * This routine is invoked by the worker thread to process FCF table
5180 * rediscovery pending completion event.
5181 **/
5182void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
5183{
5184 int rc;
5185
5186 spin_lock_irq(&phba->hbalock);
5187 /* Clear FCF rediscovery timeout event */
5188 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
5189 /* Clear driver fast failover FCF record flag */
5190 phba->fcf.failover_rec.flag = 0;
5191 /* Set state for FCF fast failover */
5192 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
5193 spin_unlock_irq(&phba->hbalock);
5194
5195 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05005196 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005197 "2777 Start post-quiescent FCF table scan\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05005198 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05005199 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05005200 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
5201 "2747 Issue FCF scan read FCF mailbox "
5202 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05005203}
5204
5205/**
James Smartda0436e2009-05-22 14:51:39 -04005206 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
5207 * @phba: pointer to lpfc hba data structure.
5208 * @dev_grp: The HBA PCI-Device group number.
5209 *
5210 * This routine is invoked to set up the per HBA PCI-Device group function
5211 * API jump table entries.
5212 *
5213 * Return: 0 if success, otherwise -ENODEV
5214 **/
5215int
5216lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05005217{
5218 int rc;
5219
James Smartda0436e2009-05-22 14:51:39 -04005220 /* Set up lpfc PCI-device group */
5221 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05005222
James Smartda0436e2009-05-22 14:51:39 -04005223 /* The LPFC_PCI_DEV_OC uses SLI4 */
5224 if (dev_grp == LPFC_PCI_DEV_OC)
5225 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05005226
James Smartda0436e2009-05-22 14:51:39 -04005227 /* Set up device INIT API function jump table */
5228 rc = lpfc_init_api_table_setup(phba, dev_grp);
5229 if (rc)
5230 return -ENODEV;
5231 /* Set up SCSI API function jump table */
5232 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
5233 if (rc)
5234 return -ENODEV;
5235 /* Set up SLI API function jump table */
5236 rc = lpfc_sli_api_table_setup(phba, dev_grp);
5237 if (rc)
5238 return -ENODEV;
5239 /* Set up MBOX API function jump table */
5240 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
5241 if (rc)
5242 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05005243
James Smartda0436e2009-05-22 14:51:39 -04005244 return 0;
James Smart5b75da22008-12-04 22:39:35 -05005245}
5246
5247/**
James Smart3621a712009-04-06 18:47:14 -04005248 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05005249 * @phba: pointer to lpfc hba data structure.
5250 * @intr_mode: active interrupt mode adopted.
5251 *
5252 * This routine it invoked to log the currently used active interrupt mode
5253 * to the device.
James Smart3772a992009-05-22 14:50:54 -04005254 **/
5255static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05005256{
5257 switch (intr_mode) {
5258 case 0:
5259 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5260 "0470 Enable INTx interrupt mode.\n");
5261 break;
5262 case 1:
5263 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5264 "0481 Enabled MSI interrupt mode.\n");
5265 break;
5266 case 2:
5267 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5268 "0480 Enabled MSI-X interrupt mode.\n");
5269 break;
5270 default:
5271 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5272 "0482 Illegal interrupt mode.\n");
5273 break;
5274 }
5275 return;
5276}
5277
James Smart5b75da22008-12-04 22:39:35 -05005278/**
James Smart3772a992009-05-22 14:50:54 -04005279 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05005280 * @phba: pointer to lpfc hba data structure.
5281 *
James Smart3772a992009-05-22 14:50:54 -04005282 * This routine is invoked to enable the PCI device that is common to all
5283 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05005284 *
5285 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005286 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005287 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05005288 **/
James Smart3772a992009-05-22 14:50:54 -04005289static int
5290lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05005291{
James Smart3772a992009-05-22 14:50:54 -04005292 struct pci_dev *pdev;
James Smart5b75da22008-12-04 22:39:35 -05005293
James Smart3772a992009-05-22 14:50:54 -04005294 /* Obtain PCI device reference */
5295 if (!phba->pcidev)
5296 goto out_error;
5297 else
5298 pdev = phba->pcidev;
James Smart3772a992009-05-22 14:50:54 -04005299 /* Enable PCI device */
5300 if (pci_enable_device_mem(pdev))
5301 goto out_error;
5302 /* Request PCI resource for the device */
Johannes Thumshirne0c04832016-06-07 09:44:03 +02005303 if (pci_request_mem_regions(pdev, LPFC_DRIVER_NAME))
James Smart3772a992009-05-22 14:50:54 -04005304 goto out_disable_device;
5305 /* Set up device as PCI master and save state for EEH */
5306 pci_set_master(pdev);
5307 pci_try_set_mwi(pdev);
5308 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05005309
James Smart05580562011-05-24 11:40:48 -04005310 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
Jon Mason453193e2013-09-11 15:38:13 -07005311 if (pci_is_pcie(pdev))
James Smart05580562011-05-24 11:40:48 -04005312 pdev->needs_freset = 1;
5313
James Smart3772a992009-05-22 14:50:54 -04005314 return 0;
James Smart5b75da22008-12-04 22:39:35 -05005315
James Smart3772a992009-05-22 14:50:54 -04005316out_disable_device:
5317 pci_disable_device(pdev);
5318out_error:
James Smart079b5c92011-08-21 21:48:49 -04005319 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
Johannes Thumshirne0c04832016-06-07 09:44:03 +02005320 "1401 Failed to enable pci device\n");
James Smart3772a992009-05-22 14:50:54 -04005321 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05005322}
5323
5324/**
James Smart3772a992009-05-22 14:50:54 -04005325 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05005326 * @phba: pointer to lpfc hba data structure.
5327 *
James Smart3772a992009-05-22 14:50:54 -04005328 * This routine is invoked to disable the PCI device that is common to all
5329 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05005330 **/
5331static void
James Smart3772a992009-05-22 14:50:54 -04005332lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05005333{
James Smart3772a992009-05-22 14:50:54 -04005334 struct pci_dev *pdev;
James Smart5b75da22008-12-04 22:39:35 -05005335
James Smart3772a992009-05-22 14:50:54 -04005336 /* Obtain PCI device reference */
5337 if (!phba->pcidev)
5338 return;
5339 else
5340 pdev = phba->pcidev;
James Smart3772a992009-05-22 14:50:54 -04005341 /* Release PCI resource and disable PCI device */
Johannes Thumshirne0c04832016-06-07 09:44:03 +02005342 pci_release_mem_regions(pdev);
James Smart3772a992009-05-22 14:50:54 -04005343 pci_disable_device(pdev);
James Smart5b75da22008-12-04 22:39:35 -05005344
5345 return;
5346}
5347
5348/**
James Smart3772a992009-05-22 14:50:54 -04005349 * lpfc_reset_hba - Reset a hba
5350 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04005351 *
James Smart3772a992009-05-22 14:50:54 -04005352 * This routine is invoked to reset a hba device. It brings the HBA
5353 * offline, performs a board restart, and then brings the board back
5354 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
5355 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04005356 **/
James Smart3772a992009-05-22 14:50:54 -04005357void
5358lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05005359{
James Smart3772a992009-05-22 14:50:54 -04005360 /* If resets are disabled then set error state and return. */
5361 if (!phba->cfg_enable_hba_reset) {
5362 phba->link_state = LPFC_HBA_ERROR;
5363 return;
5364 }
James Smartee620212014-04-04 13:51:53 -04005365 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
5366 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
5367 else
5368 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart3772a992009-05-22 14:50:54 -04005369 lpfc_offline(phba);
5370 lpfc_sli_brdrestart(phba);
5371 lpfc_online(phba);
5372 lpfc_unblock_mgmt_io(phba);
5373}
dea31012005-04-17 16:05:31 -05005374
James Smart3772a992009-05-22 14:50:54 -04005375/**
James Smart0a96e972011-07-22 18:37:28 -04005376 * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
5377 * @phba: pointer to lpfc hba data structure.
5378 *
5379 * This function enables the PCI SR-IOV virtual functions to a physical
5380 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
5381 * enable the number of virtual functions to the physical function. As
5382 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
5383 * API call does not considered as an error condition for most of the device.
5384 **/
5385uint16_t
5386lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
5387{
5388 struct pci_dev *pdev = phba->pcidev;
5389 uint16_t nr_virtfn;
5390 int pos;
5391
James Smart0a96e972011-07-22 18:37:28 -04005392 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
5393 if (pos == 0)
5394 return 0;
5395
5396 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
5397 return nr_virtfn;
5398}
5399
5400/**
James Smart912e3ac2011-05-24 11:42:11 -04005401 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
5402 * @phba: pointer to lpfc hba data structure.
5403 * @nr_vfn: number of virtual functions to be enabled.
5404 *
5405 * This function enables the PCI SR-IOV virtual functions to a physical
5406 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
5407 * enable the number of virtual functions to the physical function. As
5408 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
5409 * API call does not considered as an error condition for most of the device.
5410 **/
5411int
5412lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
5413{
5414 struct pci_dev *pdev = phba->pcidev;
James Smart0a96e972011-07-22 18:37:28 -04005415 uint16_t max_nr_vfn;
James Smart912e3ac2011-05-24 11:42:11 -04005416 int rc;
5417
James Smart0a96e972011-07-22 18:37:28 -04005418 max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
5419 if (nr_vfn > max_nr_vfn) {
5420 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5421 "3057 Requested vfs (%d) greater than "
5422 "supported vfs (%d)", nr_vfn, max_nr_vfn);
5423 return -EINVAL;
5424 }
5425
James Smart912e3ac2011-05-24 11:42:11 -04005426 rc = pci_enable_sriov(pdev, nr_vfn);
5427 if (rc) {
5428 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5429 "2806 Failed to enable sriov on this device "
5430 "with vfn number nr_vf:%d, rc:%d\n",
5431 nr_vfn, rc);
5432 } else
5433 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5434 "2807 Successful enable sriov on this device "
5435 "with vfn number nr_vf:%d\n", nr_vfn);
5436 return rc;
5437}
5438
5439/**
James Smart895427b2017-02-12 13:52:30 -08005440 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
James Smart3772a992009-05-22 14:50:54 -04005441 * @phba: pointer to lpfc hba data structure.
5442 *
James Smart895427b2017-02-12 13:52:30 -08005443 * This routine is invoked to set up the driver internal resources before the
5444 * device specific resource setup to support the HBA device it attached to.
James Smart3772a992009-05-22 14:50:54 -04005445 *
5446 * Return codes
James Smart895427b2017-02-12 13:52:30 -08005447 * 0 - successful
5448 * other values - error
James Smart3772a992009-05-22 14:50:54 -04005449 **/
5450static int
James Smart895427b2017-02-12 13:52:30 -08005451lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
James Smart3772a992009-05-22 14:50:54 -04005452{
James Smart895427b2017-02-12 13:52:30 -08005453 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05005454
James Smart3772a992009-05-22 14:50:54 -04005455 /*
James Smart895427b2017-02-12 13:52:30 -08005456 * Driver resources common to all SLI revisions
James Smart3772a992009-05-22 14:50:54 -04005457 */
James Smart895427b2017-02-12 13:52:30 -08005458 atomic_set(&phba->fast_event_count, 0);
5459 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005460
James Smart895427b2017-02-12 13:52:30 -08005461 /* Initialize ndlp management spinlock */
5462 spin_lock_init(&phba->ndlp_lock);
James Smart3772a992009-05-22 14:50:54 -04005463
James Smart895427b2017-02-12 13:52:30 -08005464 INIT_LIST_HEAD(&phba->port_list);
5465 INIT_LIST_HEAD(&phba->work_list);
5466 init_waitqueue_head(&phba->wait_4_mlo_m_q);
5467
5468 /* Initialize the wait queue head for the kernel thread */
5469 init_waitqueue_head(&phba->work_waitq);
5470
5471 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smartf358dd02017-02-12 13:52:34 -08005472 "1403 Protocols supported %s %s %s\n",
James Smart895427b2017-02-12 13:52:30 -08005473 ((phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) ?
5474 "SCSI" : " "),
5475 ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) ?
James Smartf358dd02017-02-12 13:52:34 -08005476 "NVME" : " "),
5477 (phba->nvmet_support ? "NVMET" : " "));
James Smart895427b2017-02-12 13:52:30 -08005478
5479 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
5480 /* Initialize the scsi buffer list used by driver for scsi IO */
5481 spin_lock_init(&phba->scsi_buf_list_get_lock);
5482 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_get);
5483 spin_lock_init(&phba->scsi_buf_list_put_lock);
5484 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
5485 }
5486
5487 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
5488 (phba->nvmet_support == 0)) {
5489 /* Initialize the NVME buffer list used by driver for NVME IO */
5490 spin_lock_init(&phba->nvme_buf_list_get_lock);
5491 INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_get);
5492 spin_lock_init(&phba->nvme_buf_list_put_lock);
5493 INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_put);
5494 }
5495
5496 /* Initialize the fabric iocb list */
5497 INIT_LIST_HEAD(&phba->fabric_iocb_list);
5498
5499 /* Initialize list to save ELS buffers */
5500 INIT_LIST_HEAD(&phba->elsbuf);
5501
5502 /* Initialize FCF connection rec list */
5503 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
5504
5505 /* Initialize OAS configuration list */
5506 spin_lock_init(&phba->devicelock);
5507 INIT_LIST_HEAD(&phba->luns);
5508
James Smart3772a992009-05-22 14:50:54 -04005509 /* MBOX heartbeat timer */
Tomas Jasek33cc5592017-03-03 13:45:48 +01005510 setup_timer(&psli->mbox_tmo, lpfc_mbox_timeout, (unsigned long)phba);
James Smart3772a992009-05-22 14:50:54 -04005511 /* Fabric block timer */
Tomas Jasek33cc5592017-03-03 13:45:48 +01005512 setup_timer(&phba->fabric_block_timer, lpfc_fabric_block_timeout,
5513 (unsigned long)phba);
James Smart3772a992009-05-22 14:50:54 -04005514 /* EA polling mode timer */
Tomas Jasek33cc5592017-03-03 13:45:48 +01005515 setup_timer(&phba->eratt_poll, lpfc_poll_eratt,
5516 (unsigned long)phba);
James Smart895427b2017-02-12 13:52:30 -08005517 /* Heartbeat timer */
Tomas Jasek33cc5592017-03-03 13:45:48 +01005518 setup_timer(&phba->hb_tmofunc, lpfc_hb_timeout, (unsigned long)phba);
James Smart895427b2017-02-12 13:52:30 -08005519
5520 return 0;
5521}
5522
5523/**
5524 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev
5525 * @phba: pointer to lpfc hba data structure.
5526 *
5527 * This routine is invoked to set up the driver internal resources specific to
5528 * support the SLI-3 HBA device it attached to.
5529 *
5530 * Return codes
5531 * 0 - successful
5532 * other values - error
5533 **/
5534static int
5535lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
5536{
5537 int rc;
5538
5539 /*
5540 * Initialize timers used by driver
5541 */
5542
5543 /* FCP polling mode timer */
Tomas Jasek33cc5592017-03-03 13:45:48 +01005544 setup_timer(&phba->fcp_poll_timer, lpfc_poll_timeout,
5545 (unsigned long)phba);
James Smart3772a992009-05-22 14:50:54 -04005546
5547 /* Host attention work mask setup */
5548 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
5549 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
5550
5551 /* Get all the module params for configuring this host */
5552 lpfc_get_cfgparam(phba);
James Smart895427b2017-02-12 13:52:30 -08005553 /* Set up phase-1 common device driver resources */
5554
5555 rc = lpfc_setup_driver_resource_phase1(phba);
5556 if (rc)
5557 return -ENODEV;
5558
James Smart49198b32010-04-06 15:04:33 -04005559 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
5560 phba->menlo_flag |= HBA_MENLO_SUPPORT;
5561 /* check for menlo minimum sg count */
5562 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
5563 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
5564 }
5565
James Smart895427b2017-02-12 13:52:30 -08005566 if (!phba->sli.sli3_ring)
5567 phba->sli.sli3_ring = kzalloc(LPFC_SLI3_MAX_RING *
James Smart2a76a282012-08-03 12:35:54 -04005568 sizeof(struct lpfc_sli_ring), GFP_KERNEL);
James Smart895427b2017-02-12 13:52:30 -08005569 if (!phba->sli.sli3_ring)
James Smart2a76a282012-08-03 12:35:54 -04005570 return -ENOMEM;
5571
James Smart3772a992009-05-22 14:50:54 -04005572 /*
James Smart96f70772013-04-17 20:16:15 -04005573 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smart3772a992009-05-22 14:50:54 -04005574 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smart3772a992009-05-22 14:50:54 -04005575 */
James Smart3772a992009-05-22 14:50:54 -04005576
James Smart96f70772013-04-17 20:16:15 -04005577 /* Initialize the host templates the configured values. */
James Smart3772a992009-05-22 14:50:54 -04005578 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
James Smart96418b52017-03-04 09:30:31 -08005579 lpfc_template_no_hr.sg_tablesize = phba->cfg_sg_seg_cnt;
5580 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
James Smart3772a992009-05-22 14:50:54 -04005581
James Smart96f70772013-04-17 20:16:15 -04005582 /* There are going to be 2 reserved BDEs: 1 FCP cmnd + 1 FCP rsp */
5583 if (phba->cfg_enable_bg) {
5584 /*
5585 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
5586 * the FCP rsp, and a BDE for each. Sice we have no control
5587 * over how many protection data segments the SCSI Layer
5588 * will hand us (ie: there could be one for every block
5589 * in the IO), we just allocate enough BDEs to accomidate
5590 * our max amount and we need to limit lpfc_sg_seg_cnt to
5591 * minimize the risk of running out.
5592 */
5593 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5594 sizeof(struct fcp_rsp) +
5595 (LPFC_MAX_SG_SEG_CNT * sizeof(struct ulp_bde64));
5596
5597 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SEG_CNT_DIF)
5598 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT_DIF;
5599
5600 /* Total BDEs in BPL for scsi_sg_list and scsi_sg_prot_list */
5601 phba->cfg_total_seg_cnt = LPFC_MAX_SG_SEG_CNT;
5602 } else {
5603 /*
5604 * The scsi_buf for a regular I/O will hold the FCP cmnd,
5605 * the FCP rsp, a BDE for each, and a BDE for up to
5606 * cfg_sg_seg_cnt data segments.
5607 */
5608 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5609 sizeof(struct fcp_rsp) +
5610 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
5611
5612 /* Total BDEs in BPL for scsi_sg_list */
5613 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
5614 }
5615
5616 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
5617 "9088 sg_tablesize:%d dmabuf_size:%d total_bde:%d\n",
5618 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
5619 phba->cfg_total_seg_cnt);
5620
James Smart3772a992009-05-22 14:50:54 -04005621 phba->max_vpi = LPFC_MAX_VPI;
5622 /* This will be set to correct value after config_port mbox */
5623 phba->max_vports = 0;
5624
5625 /*
5626 * Initialize the SLI Layer to run with lpfc HBAs.
5627 */
5628 lpfc_sli_setup(phba);
James Smart895427b2017-02-12 13:52:30 -08005629 lpfc_sli_queue_init(phba);
James Smart3772a992009-05-22 14:50:54 -04005630
5631 /* Allocate device driver memory */
5632 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
5633 return -ENOMEM;
5634
James Smart912e3ac2011-05-24 11:42:11 -04005635 /*
5636 * Enable sr-iov virtual functions if supported and configured
5637 * through the module parameter.
5638 */
5639 if (phba->cfg_sriov_nr_virtfn > 0) {
5640 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
5641 phba->cfg_sriov_nr_virtfn);
5642 if (rc) {
5643 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5644 "2808 Requested number of SR-IOV "
5645 "virtual functions (%d) is not "
5646 "supported\n",
5647 phba->cfg_sriov_nr_virtfn);
5648 phba->cfg_sriov_nr_virtfn = 0;
5649 }
5650 }
5651
James Smart3772a992009-05-22 14:50:54 -04005652 return 0;
5653}
5654
5655/**
5656 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
5657 * @phba: pointer to lpfc hba data structure.
5658 *
5659 * This routine is invoked to unset the driver internal resources set up
5660 * specific for supporting the SLI-3 HBA device it attached to.
5661 **/
5662static void
5663lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
5664{
5665 /* Free device driver memory allocated */
5666 lpfc_mem_free_all(phba);
5667
5668 return;
5669}
5670
5671/**
James Smartda0436e2009-05-22 14:51:39 -04005672 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
5673 * @phba: pointer to lpfc hba data structure.
5674 *
5675 * This routine is invoked to set up the driver internal resources specific to
5676 * support the SLI-4 HBA device it attached to.
5677 *
5678 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005679 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005680 * other values - error
5681 **/
5682static int
5683lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
5684{
James Smart28baac72010-02-12 14:42:03 -05005685 LPFC_MBOXQ_t *mboxq;
James Smartf358dd02017-02-12 13:52:34 -08005686 MAILBOX_t *mb;
James Smart895427b2017-02-12 13:52:30 -08005687 int rc, i, max_buf_size;
James Smart28baac72010-02-12 14:42:03 -05005688 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
5689 struct lpfc_mqe *mqe;
James Smart09294d42013-04-17 20:16:05 -04005690 int longs;
James Smart1ba981f2014-02-20 09:56:45 -05005691 int fof_vectors = 0;
James Smartf358dd02017-02-12 13:52:34 -08005692 uint64_t wwn;
James Smartda0436e2009-05-22 14:51:39 -04005693
James Smart895427b2017-02-12 13:52:30 -08005694 phba->sli4_hba.num_online_cpu = num_online_cpus();
5695 phba->sli4_hba.num_present_cpu = lpfc_present_cpu;
5696 phba->sli4_hba.curr_disp_cpu = 0;
5697
James Smart716d3bc2013-09-06 12:18:28 -04005698 /* Get all the module params for configuring this host */
5699 lpfc_get_cfgparam(phba);
5700
James Smart895427b2017-02-12 13:52:30 -08005701 /* Set up phase-1 common device driver resources */
5702 rc = lpfc_setup_driver_resource_phase1(phba);
5703 if (rc)
5704 return -ENODEV;
5705
James Smartda0436e2009-05-22 14:51:39 -04005706 /* Before proceed, wait for POST done and device ready */
5707 rc = lpfc_sli4_post_status_check(phba);
5708 if (rc)
5709 return -ENODEV;
5710
5711 /*
5712 * Initialize timers used by driver
5713 */
5714
Tomas Jasek33cc5592017-03-03 13:45:48 +01005715 setup_timer(&phba->rrq_tmr, lpfc_rrq_timeout, (unsigned long)phba);
James Smartda0436e2009-05-22 14:51:39 -04005716
James Smartecfd03c2010-02-12 14:41:27 -05005717 /* FCF rediscover timer */
Tomas Jasek33cc5592017-03-03 13:45:48 +01005718 setup_timer(&phba->fcf.redisc_wait, lpfc_sli4_fcf_redisc_wait_tmo,
5719 (unsigned long)phba);
James Smartecfd03c2010-02-12 14:41:27 -05005720
James Smartda0436e2009-05-22 14:51:39 -04005721 /*
James Smart7ad20aa2011-05-24 11:44:28 -04005722 * Control structure for handling external multi-buffer mailbox
5723 * command pass-through.
5724 */
5725 memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
5726 sizeof(struct lpfc_mbox_ext_buf_ctx));
5727 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
5728
James Smartda0436e2009-05-22 14:51:39 -04005729 phba->max_vpi = LPFC_MAX_VPI;
James Smart67d12732012-08-03 12:36:13 -04005730
James Smartda0436e2009-05-22 14:51:39 -04005731 /* This will be set to correct value after the read_config mbox */
5732 phba->max_vports = 0;
5733
5734 /* Program the default value of vlan_id and fc_map */
5735 phba->valid_vlan = 0;
5736 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5737 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5738 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5739
5740 /*
James Smart2a76a282012-08-03 12:35:54 -04005741 * For SLI4, instead of using ring 0 (LPFC_FCP_RING) for FCP commands
James Smart895427b2017-02-12 13:52:30 -08005742 * we will associate a new ring, for each EQ/CQ/WQ tuple.
5743 * The WQ create will allocate the ring.
James Smart2a76a282012-08-03 12:35:54 -04005744 */
James Smart085c6472010-11-20 23:11:37 -05005745
James Smart09294d42013-04-17 20:16:05 -04005746 /*
5747 * It doesn't matter what family our adapter is in, we are
5748 * limited to 2 Pages, 512 SGEs, for our SGL.
5749 * There are going to be 2 reserved SGEs: 1 FCP cmnd + 1 FCP rsp
5750 */
5751 max_buf_size = (2 * SLI4_PAGE_SIZE);
5752 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SGL_SEG_CNT - 2)
5753 phba->cfg_sg_seg_cnt = LPFC_MAX_SGL_SEG_CNT - 2;
James Smart09294d42013-04-17 20:16:05 -04005754
James Smartda0436e2009-05-22 14:51:39 -04005755 /*
James Smart895427b2017-02-12 13:52:30 -08005756 * Since lpfc_sg_seg_cnt is module param, the sg_dma_buf_size
5757 * used to create the sg_dma_buf_pool must be calculated.
James Smart28baac72010-02-12 14:42:03 -05005758 */
James Smart96f70772013-04-17 20:16:15 -04005759 if (phba->cfg_enable_bg) {
5760 /*
James Smart895427b2017-02-12 13:52:30 -08005761 * The scsi_buf for a T10-DIF I/O holds the FCP cmnd,
5762 * the FCP rsp, and a SGE. Sice we have no control
5763 * over how many protection segments the SCSI Layer
James Smart96f70772013-04-17 20:16:15 -04005764 * will hand us (ie: there could be one for every block
James Smart895427b2017-02-12 13:52:30 -08005765 * in the IO), just allocate enough SGEs to accomidate
5766 * our max amount and we need to limit lpfc_sg_seg_cnt
5767 * to minimize the risk of running out.
James Smart96f70772013-04-17 20:16:15 -04005768 */
5769 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
James Smart895427b2017-02-12 13:52:30 -08005770 sizeof(struct fcp_rsp) + max_buf_size;
James Smart96f70772013-04-17 20:16:15 -04005771
5772 /* Total SGEs for scsi_sg_list and scsi_sg_prot_list */
5773 phba->cfg_total_seg_cnt = LPFC_MAX_SGL_SEG_CNT;
5774
5775 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SLI4_SEG_CNT_DIF)
James Smart895427b2017-02-12 13:52:30 -08005776 phba->cfg_sg_seg_cnt =
5777 LPFC_MAX_SG_SLI4_SEG_CNT_DIF;
James Smart96f70772013-04-17 20:16:15 -04005778 } else {
5779 /*
James Smart895427b2017-02-12 13:52:30 -08005780 * The scsi_buf for a regular I/O holds the FCP cmnd,
James Smart96f70772013-04-17 20:16:15 -04005781 * the FCP rsp, a SGE for each, and a SGE for up to
5782 * cfg_sg_seg_cnt data segments.
5783 */
5784 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
James Smart895427b2017-02-12 13:52:30 -08005785 sizeof(struct fcp_rsp) +
5786 ((phba->cfg_sg_seg_cnt + 2) *
5787 sizeof(struct sli4_sge));
James Smart96f70772013-04-17 20:16:15 -04005788
5789 /* Total SGEs for scsi_sg_list */
5790 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
James Smart895427b2017-02-12 13:52:30 -08005791
James Smart96f70772013-04-17 20:16:15 -04005792 /*
James Smart895427b2017-02-12 13:52:30 -08005793 * NOTE: if (phba->cfg_sg_seg_cnt + 2) <= 256 we only
5794 * need to post 1 page for the SGL.
James Smart96f70772013-04-17 20:16:15 -04005795 */
James Smart085c6472010-11-20 23:11:37 -05005796 }
James Smartacd68592012-01-18 16:25:09 -05005797
James Smart96f70772013-04-17 20:16:15 -04005798 /* Initialize the host templates with the updated values. */
5799 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5800 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
James Smart96418b52017-03-04 09:30:31 -08005801 lpfc_template_no_hr.sg_tablesize = phba->cfg_sg_seg_cnt;
James Smart96f70772013-04-17 20:16:15 -04005802
5803 if (phba->cfg_sg_dma_buf_size <= LPFC_MIN_SG_SLI4_BUF_SZ)
5804 phba->cfg_sg_dma_buf_size = LPFC_MIN_SG_SLI4_BUF_SZ;
5805 else
5806 phba->cfg_sg_dma_buf_size =
5807 SLI4_PAGE_ALIGN(phba->cfg_sg_dma_buf_size);
5808
5809 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
5810 "9087 sg_tablesize:%d dmabuf_size:%d total_sge:%d\n",
5811 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
5812 phba->cfg_total_seg_cnt);
James Smartda0436e2009-05-22 14:51:39 -04005813
5814 /* Initialize buffer queue management fields */
James Smart895427b2017-02-12 13:52:30 -08005815 INIT_LIST_HEAD(&phba->hbqs[LPFC_ELS_HBQ].hbq_buffer_list);
James Smartda0436e2009-05-22 14:51:39 -04005816 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
5817 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
5818
5819 /*
5820 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
5821 */
James Smart895427b2017-02-12 13:52:30 -08005822 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
5823 /* Initialize the Abort scsi buffer list used by driver */
5824 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
5825 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
5826 }
5827
5828 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
5829 /* Initialize the Abort nvme buffer list used by driver */
5830 spin_lock_init(&phba->sli4_hba.abts_nvme_buf_list_lock);
5831 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvme_buf_list);
James Smart86c67372017-04-21 16:05:04 -07005832 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
James Smart318083a2017-03-04 09:30:30 -08005833 /* Fast-path XRI aborted CQ Event work queue list */
5834 INIT_LIST_HEAD(&phba->sli4_hba.sp_nvme_xri_aborted_work_queue);
James Smart895427b2017-02-12 13:52:30 -08005835 }
5836
James Smartda0436e2009-05-22 14:51:39 -04005837 /* This abort list used by worker thread */
James Smart895427b2017-02-12 13:52:30 -08005838 spin_lock_init(&phba->sli4_hba.sgl_list_lock);
James Smartf358dd02017-02-12 13:52:34 -08005839 spin_lock_init(&phba->sli4_hba.nvmet_io_lock);
James Smartda0436e2009-05-22 14:51:39 -04005840
5841 /*
James Smart6d368e52011-05-24 11:44:12 -04005842 * Initialize driver internal slow-path work queues
James Smartda0436e2009-05-22 14:51:39 -04005843 */
5844
5845 /* Driver internel slow-path CQ Event pool */
5846 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
5847 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04005848 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04005849 /* Asynchronous event CQ Event work queue list */
5850 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
5851 /* Fast-path XRI aborted CQ Event work queue list */
5852 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
5853 /* Slow-path XRI aborted CQ Event work queue list */
5854 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
5855 /* Receive queue CQ Event work queue list */
5856 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
5857
James Smart6d368e52011-05-24 11:44:12 -04005858 /* Initialize extent block lists. */
5859 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
5860 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
5861 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
5862 INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
5863
James Smartd1f525a2017-04-21 16:04:55 -07005864 /* Initialize mboxq lists. If the early init routines fail
5865 * these lists need to be correctly initialized.
5866 */
5867 INIT_LIST_HEAD(&phba->sli.mboxq);
5868 INIT_LIST_HEAD(&phba->sli.mboxq_cmpl);
5869
James Smart448193b2015-12-16 18:12:05 -05005870 /* initialize optic_state to 0xFF */
5871 phba->sli4_hba.lnk_info.optic_state = 0xff;
5872
James Smartda0436e2009-05-22 14:51:39 -04005873 /* Allocate device driver memory */
5874 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
5875 if (rc)
5876 return -ENOMEM;
5877
James Smart2fcee4b2010-12-15 17:57:46 -05005878 /* IF Type 2 ports get initialized now. */
5879 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
5880 LPFC_SLI_INTF_IF_TYPE_2) {
5881 rc = lpfc_pci_function_reset(phba);
James Smart895427b2017-02-12 13:52:30 -08005882 if (unlikely(rc)) {
5883 rc = -ENODEV;
5884 goto out_free_mem;
5885 }
James Smart946727d2015-04-07 15:07:09 -04005886 phba->temp_sensor_support = 1;
James Smart2fcee4b2010-12-15 17:57:46 -05005887 }
5888
James Smartda0436e2009-05-22 14:51:39 -04005889 /* Create the bootstrap mailbox command */
5890 rc = lpfc_create_bootstrap_mbox(phba);
5891 if (unlikely(rc))
5892 goto out_free_mem;
5893
5894 /* Set up the host's endian order with the device. */
5895 rc = lpfc_setup_endian_order(phba);
5896 if (unlikely(rc))
5897 goto out_free_bsmbx;
5898
5899 /* Set up the hba's configuration parameters. */
5900 rc = lpfc_sli4_read_config(phba);
5901 if (unlikely(rc))
5902 goto out_free_bsmbx;
James Smartcff261f2013-12-17 20:29:47 -05005903 rc = lpfc_mem_alloc_active_rrq_pool_s4(phba);
5904 if (unlikely(rc))
5905 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04005906
James Smart2fcee4b2010-12-15 17:57:46 -05005907 /* IF Type 0 ports get initialized now. */
5908 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
5909 LPFC_SLI_INTF_IF_TYPE_0) {
5910 rc = lpfc_pci_function_reset(phba);
5911 if (unlikely(rc))
5912 goto out_free_bsmbx;
5913 }
James Smartda0436e2009-05-22 14:51:39 -04005914
James Smartcb5172e2010-03-15 11:25:07 -04005915 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
5916 GFP_KERNEL);
5917 if (!mboxq) {
5918 rc = -ENOMEM;
5919 goto out_free_bsmbx;
5920 }
5921
James Smartf358dd02017-02-12 13:52:34 -08005922 /* Check for NVMET being configured */
James Smart895427b2017-02-12 13:52:30 -08005923 phba->nvmet_support = 0;
James Smartf358dd02017-02-12 13:52:34 -08005924 if (lpfc_enable_nvmet_cnt) {
5925
5926 /* First get WWN of HBA instance */
5927 lpfc_read_nv(phba, mboxq);
5928 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5929 if (rc != MBX_SUCCESS) {
5930 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5931 "6016 Mailbox failed , mbxCmd x%x "
5932 "READ_NV, mbxStatus x%x\n",
5933 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5934 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smartd1f525a2017-04-21 16:04:55 -07005935 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartf358dd02017-02-12 13:52:34 -08005936 rc = -EIO;
5937 goto out_free_bsmbx;
5938 }
5939 mb = &mboxq->u.mb;
5940 memcpy(&wwn, (char *)mb->un.varRDnvp.nodename,
5941 sizeof(uint64_t));
5942 wwn = cpu_to_be64(wwn);
5943 phba->sli4_hba.wwnn.u.name = wwn;
5944 memcpy(&wwn, (char *)mb->un.varRDnvp.portname,
5945 sizeof(uint64_t));
5946 /* wwn is WWPN of HBA instance */
5947 wwn = cpu_to_be64(wwn);
5948 phba->sli4_hba.wwpn.u.name = wwn;
5949
5950 /* Check to see if it matches any module parameter */
5951 for (i = 0; i < lpfc_enable_nvmet_cnt; i++) {
5952 if (wwn == lpfc_enable_nvmet[i]) {
James Smart7d708032017-03-08 14:36:01 -08005953#if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
James Smartf358dd02017-02-12 13:52:34 -08005954 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5955 "6017 NVME Target %016llx\n",
5956 wwn);
5957 phba->nvmet_support = 1; /* a match */
James Smart7d708032017-03-08 14:36:01 -08005958#else
5959 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5960 "6021 Can't enable NVME Target."
5961 " NVME_TARGET_FC infrastructure"
5962 " is not in kernel\n");
5963#endif
James Smartf358dd02017-02-12 13:52:34 -08005964 }
5965 }
5966 }
James Smart895427b2017-02-12 13:52:30 -08005967
5968 lpfc_nvme_mod_param_dep(phba);
5969
James Smartfedd3b72011-02-16 12:39:24 -05005970 /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
James Smartcb5172e2010-03-15 11:25:07 -04005971 lpfc_supported_pages(mboxq);
5972 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smartfedd3b72011-02-16 12:39:24 -05005973 if (!rc) {
5974 mqe = &mboxq->u.mqe;
5975 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
5976 LPFC_MAX_SUPPORTED_PAGES);
5977 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
5978 switch (pn_page[i]) {
5979 case LPFC_SLI4_PARAMETERS:
5980 phba->sli4_hba.pc_sli4_params.supported = 1;
5981 break;
5982 default:
5983 break;
5984 }
5985 }
5986 /* Read the port's SLI4 Parameters capabilities if supported. */
5987 if (phba->sli4_hba.pc_sli4_params.supported)
5988 rc = lpfc_pc_sli4_params_get(phba, mboxq);
5989 if (rc) {
5990 mempool_free(mboxq, phba->mbox_mem_pool);
5991 rc = -EIO;
5992 goto out_free_bsmbx;
James Smartcb5172e2010-03-15 11:25:07 -04005993 }
5994 }
James Smart65791f12016-07-06 12:35:56 -07005995
James Smartfedd3b72011-02-16 12:39:24 -05005996 /*
5997 * Get sli4 parameters that override parameters from Port capabilities.
James Smart6d368e52011-05-24 11:44:12 -04005998 * If this call fails, it isn't critical unless the SLI4 parameters come
5999 * back in conflict.
James Smartfedd3b72011-02-16 12:39:24 -05006000 */
James Smart6d368e52011-05-24 11:44:12 -04006001 rc = lpfc_get_sli4_parameters(phba, mboxq);
6002 if (rc) {
6003 if (phba->sli4_hba.extents_in_use &&
6004 phba->sli4_hba.rpi_hdrs_in_use) {
6005 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6006 "2999 Unsupported SLI4 Parameters "
6007 "Extents and RPI headers enabled.\n");
James Smart6d368e52011-05-24 11:44:12 -04006008 }
James Smart895427b2017-02-12 13:52:30 -08006009 mempool_free(mboxq, phba->mbox_mem_pool);
6010 goto out_free_bsmbx;
James Smart6d368e52011-05-24 11:44:12 -04006011 }
James Smart895427b2017-02-12 13:52:30 -08006012
James Smartcb5172e2010-03-15 11:25:07 -04006013 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1ba981f2014-02-20 09:56:45 -05006014
6015 /* Verify OAS is supported */
6016 lpfc_sli4_oas_verify(phba);
6017 if (phba->cfg_fof)
6018 fof_vectors = 1;
6019
James Smart5350d872011-10-10 21:33:49 -04006020 /* Verify all the SLI4 queues */
6021 rc = lpfc_sli4_queue_verify(phba);
James Smartda0436e2009-05-22 14:51:39 -04006022 if (rc)
6023 goto out_free_bsmbx;
6024
6025 /* Create driver internal CQE event pool */
6026 rc = lpfc_sli4_cq_event_pool_create(phba);
6027 if (rc)
James Smart5350d872011-10-10 21:33:49 -04006028 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04006029
James Smart8a9d2e82012-05-09 21:16:12 -04006030 /* Initialize sgl lists per host */
6031 lpfc_init_sgl_list(phba);
6032
6033 /* Allocate and initialize active sgl array */
James Smartda0436e2009-05-22 14:51:39 -04006034 rc = lpfc_init_active_sgl_array(phba);
6035 if (rc) {
6036 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6037 "1430 Failed to initialize sgl list.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04006038 goto out_destroy_cq_event_pool;
James Smartda0436e2009-05-22 14:51:39 -04006039 }
James Smartda0436e2009-05-22 14:51:39 -04006040 rc = lpfc_sli4_init_rpi_hdrs(phba);
6041 if (rc) {
6042 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6043 "1432 Failed to initialize rpi headers.\n");
6044 goto out_free_active_sgl;
6045 }
6046
James Smarta93ff372010-10-22 11:06:08 -04006047 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
James Smart0c9ab6f2010-02-26 14:15:57 -05006048 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
6049 phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
6050 GFP_KERNEL);
6051 if (!phba->fcf.fcf_rr_bmask) {
6052 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6053 "2759 Failed allocate memory for FCF round "
6054 "robin failover bmask\n");
James Smart05580562011-05-24 11:40:48 -04006055 rc = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -05006056 goto out_remove_rpi_hdrs;
6057 }
6058
James Smart895427b2017-02-12 13:52:30 -08006059 phba->sli4_hba.hba_eq_hdl = kcalloc(fof_vectors + phba->io_channel_irqs,
6060 sizeof(struct lpfc_hba_eq_hdl),
6061 GFP_KERNEL);
6062 if (!phba->sli4_hba.hba_eq_hdl) {
James Smart67d12732012-08-03 12:36:13 -04006063 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6064 "2572 Failed allocate memory for "
6065 "fast-path per-EQ handle array\n");
6066 rc = -ENOMEM;
6067 goto out_free_fcf_rr_bmask;
James Smartda0436e2009-05-22 14:51:39 -04006068 }
6069
James Smart895427b2017-02-12 13:52:30 -08006070 phba->sli4_hba.cpu_map = kcalloc(phba->sli4_hba.num_present_cpu,
6071 sizeof(struct lpfc_vector_map_info),
6072 GFP_KERNEL);
James Smart7bb03bb2013-04-17 20:19:16 -04006073 if (!phba->sli4_hba.cpu_map) {
6074 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6075 "3327 Failed allocate memory for msi-x "
6076 "interrupt vector mapping\n");
6077 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08006078 goto out_free_hba_eq_hdl;
James Smart7bb03bb2013-04-17 20:19:16 -04006079 }
James Smartb246de12013-05-31 17:03:07 -04006080 if (lpfc_used_cpu == NULL) {
James Smart895427b2017-02-12 13:52:30 -08006081 lpfc_used_cpu = kcalloc(lpfc_present_cpu, sizeof(uint16_t),
6082 GFP_KERNEL);
James Smartb246de12013-05-31 17:03:07 -04006083 if (!lpfc_used_cpu) {
6084 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6085 "3335 Failed allocate memory for msi-x "
6086 "interrupt vector mapping\n");
6087 kfree(phba->sli4_hba.cpu_map);
6088 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08006089 goto out_free_hba_eq_hdl;
James Smartb246de12013-05-31 17:03:07 -04006090 }
6091 for (i = 0; i < lpfc_present_cpu; i++)
6092 lpfc_used_cpu[i] = LPFC_VECTOR_MAP_EMPTY;
6093 }
6094
James Smart912e3ac2011-05-24 11:42:11 -04006095 /*
6096 * Enable sr-iov virtual functions if supported and configured
6097 * through the module parameter.
6098 */
6099 if (phba->cfg_sriov_nr_virtfn > 0) {
6100 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
6101 phba->cfg_sriov_nr_virtfn);
6102 if (rc) {
6103 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6104 "3020 Requested number of SR-IOV "
6105 "virtual functions (%d) is not "
6106 "supported\n",
6107 phba->cfg_sriov_nr_virtfn);
6108 phba->cfg_sriov_nr_virtfn = 0;
6109 }
6110 }
6111
James Smart5248a742011-07-22 18:37:06 -04006112 return 0;
James Smartda0436e2009-05-22 14:51:39 -04006113
James Smart895427b2017-02-12 13:52:30 -08006114out_free_hba_eq_hdl:
6115 kfree(phba->sli4_hba.hba_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05006116out_free_fcf_rr_bmask:
6117 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04006118out_remove_rpi_hdrs:
6119 lpfc_sli4_remove_rpi_hdrs(phba);
6120out_free_active_sgl:
6121 lpfc_free_active_sgl(phba);
James Smartda0436e2009-05-22 14:51:39 -04006122out_destroy_cq_event_pool:
6123 lpfc_sli4_cq_event_pool_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04006124out_free_bsmbx:
6125 lpfc_destroy_bootstrap_mbox(phba);
6126out_free_mem:
6127 lpfc_mem_free(phba);
6128 return rc;
6129}
6130
6131/**
6132 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
6133 * @phba: pointer to lpfc hba data structure.
6134 *
6135 * This routine is invoked to unset the driver internal resources set up
6136 * specific for supporting the SLI-4 HBA device it attached to.
6137 **/
6138static void
6139lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
6140{
6141 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6142
James Smart7bb03bb2013-04-17 20:19:16 -04006143 /* Free memory allocated for msi-x interrupt vector to CPU mapping */
6144 kfree(phba->sli4_hba.cpu_map);
6145 phba->sli4_hba.num_present_cpu = 0;
6146 phba->sli4_hba.num_online_cpu = 0;
James Smart76fd07a2014-02-20 09:57:18 -05006147 phba->sli4_hba.curr_disp_cpu = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04006148
James Smartda0436e2009-05-22 14:51:39 -04006149 /* Free memory allocated for fast-path work queue handles */
James Smart895427b2017-02-12 13:52:30 -08006150 kfree(phba->sli4_hba.hba_eq_hdl);
James Smartda0436e2009-05-22 14:51:39 -04006151
6152 /* Free the allocated rpi headers. */
6153 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04006154 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04006155
James Smart0c9ab6f2010-02-26 14:15:57 -05006156 /* Free eligible FCF index bmask */
6157 kfree(phba->fcf.fcf_rr_bmask);
6158
James Smartda0436e2009-05-22 14:51:39 -04006159 /* Free the ELS sgl list */
6160 lpfc_free_active_sgl(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04006161 lpfc_free_els_sgl_list(phba);
James Smartf358dd02017-02-12 13:52:34 -08006162 lpfc_free_nvmet_sgl_list(phba);
James Smartda0436e2009-05-22 14:51:39 -04006163
James Smartda0436e2009-05-22 14:51:39 -04006164 /* Free the completion queue EQ event pool */
6165 lpfc_sli4_cq_event_release_all(phba);
6166 lpfc_sli4_cq_event_pool_destroy(phba);
6167
James Smart6d368e52011-05-24 11:44:12 -04006168 /* Release resource identifiers. */
6169 lpfc_sli4_dealloc_resource_identifiers(phba);
6170
James Smartda0436e2009-05-22 14:51:39 -04006171 /* Free the bsmbx region. */
6172 lpfc_destroy_bootstrap_mbox(phba);
6173
6174 /* Free the SLI Layer memory with SLI4 HBAs */
6175 lpfc_mem_free_all(phba);
6176
6177 /* Free the current connect table */
6178 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04006179 &phba->fcf_conn_rec_list, list) {
6180 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04006181 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04006182 }
James Smartda0436e2009-05-22 14:51:39 -04006183
6184 return;
6185}
6186
6187/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006188 * lpfc_init_api_table_setup - Set up init api function jump table
James Smart3772a992009-05-22 14:50:54 -04006189 * @phba: The hba struct for which this call is being executed.
6190 * @dev_grp: The HBA PCI-Device group number.
6191 *
6192 * This routine sets up the device INIT interface API function jump table
6193 * in @phba struct.
6194 *
6195 * Returns: 0 - success, -ENODEV - failure.
6196 **/
6197int
6198lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6199{
James Smart84d1b002010-02-12 14:42:33 -05006200 phba->lpfc_hba_init_link = lpfc_hba_init_link;
6201 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart7f860592011-03-11 16:05:52 -05006202 phba->lpfc_selective_reset = lpfc_selective_reset;
James Smart3772a992009-05-22 14:50:54 -04006203 switch (dev_grp) {
6204 case LPFC_PCI_DEV_LP:
6205 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
6206 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
6207 phba->lpfc_stop_port = lpfc_stop_port_s3;
6208 break;
James Smartda0436e2009-05-22 14:51:39 -04006209 case LPFC_PCI_DEV_OC:
6210 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
6211 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
6212 phba->lpfc_stop_port = lpfc_stop_port_s4;
6213 break;
James Smart3772a992009-05-22 14:50:54 -04006214 default:
6215 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6216 "1431 Invalid HBA PCI-device group: 0x%x\n",
6217 dev_grp);
6218 return -ENODEV;
6219 break;
6220 }
6221 return 0;
6222}
6223
6224/**
James Smart3772a992009-05-22 14:50:54 -04006225 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
6226 * @phba: pointer to lpfc hba data structure.
6227 *
6228 * This routine is invoked to set up the driver internal resources after the
6229 * device specific resource setup to support the HBA device it attached to.
6230 *
6231 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006232 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006233 * other values - error
6234 **/
6235static int
6236lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
6237{
6238 int error;
6239
6240 /* Startup the kernel thread for this host adapter. */
6241 phba->worker_thread = kthread_run(lpfc_do_work, phba,
6242 "lpfc_worker_%d", phba->brd_no);
6243 if (IS_ERR(phba->worker_thread)) {
6244 error = PTR_ERR(phba->worker_thread);
6245 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05006246 }
6247
James Smart3772a992009-05-22 14:50:54 -04006248 return 0;
6249}
6250
6251/**
6252 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
6253 * @phba: pointer to lpfc hba data structure.
6254 *
6255 * This routine is invoked to unset the driver internal resources set up after
6256 * the device specific resource setup for supporting the HBA device it
6257 * attached to.
6258 **/
6259static void
6260lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
6261{
6262 /* Stop kernel worker thread */
6263 kthread_stop(phba->worker_thread);
6264}
6265
6266/**
6267 * lpfc_free_iocb_list - Free iocb list.
6268 * @phba: pointer to lpfc hba data structure.
6269 *
6270 * This routine is invoked to free the driver's IOCB list and memory.
6271 **/
6272static void
6273lpfc_free_iocb_list(struct lpfc_hba *phba)
6274{
6275 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
6276
6277 spin_lock_irq(&phba->hbalock);
6278 list_for_each_entry_safe(iocbq_entry, iocbq_next,
6279 &phba->lpfc_iocb_list, list) {
6280 list_del(&iocbq_entry->list);
6281 kfree(iocbq_entry);
6282 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05006283 }
James Smart3772a992009-05-22 14:50:54 -04006284 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006285
James Smart3772a992009-05-22 14:50:54 -04006286 return;
6287}
dea31012005-04-17 16:05:31 -05006288
James Smart3772a992009-05-22 14:50:54 -04006289/**
6290 * lpfc_init_iocb_list - Allocate and initialize iocb list.
6291 * @phba: pointer to lpfc hba data structure.
6292 *
6293 * This routine is invoked to allocate and initizlize the driver's IOCB
6294 * list and set up the IOCB tag array accordingly.
6295 *
6296 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006297 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006298 * other values - error
6299 **/
6300static int
6301lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
6302{
6303 struct lpfc_iocbq *iocbq_entry = NULL;
6304 uint16_t iotag;
6305 int i;
dea31012005-04-17 16:05:31 -05006306
6307 /* Initialize and populate the iocb list per host. */
6308 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04006309 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07006310 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05006311 if (iocbq_entry == NULL) {
6312 printk(KERN_ERR "%s: only allocated %d iocbs of "
6313 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07006314 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05006315 goto out_free_iocbq;
6316 }
6317
James Bottomley604a3e32005-10-29 10:28:33 -05006318 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
6319 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04006320 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05006321 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04006322 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05006323 goto out_free_iocbq;
6324 }
James Smart6d368e52011-05-24 11:44:12 -04006325 iocbq_entry->sli4_lxritag = NO_XRI;
James Smart3772a992009-05-22 14:50:54 -04006326 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05006327
6328 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006329 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
6330 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05006331 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006332 }
6333
James Smart3772a992009-05-22 14:50:54 -04006334 return 0;
6335
6336out_free_iocbq:
6337 lpfc_free_iocb_list(phba);
6338
6339 return -ENOMEM;
6340}
6341
6342/**
James Smart8a9d2e82012-05-09 21:16:12 -04006343 * lpfc_free_sgl_list - Free a given sgl list.
James Smartda0436e2009-05-22 14:51:39 -04006344 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -04006345 * @sglq_list: pointer to the head of sgl list.
James Smartda0436e2009-05-22 14:51:39 -04006346 *
James Smart8a9d2e82012-05-09 21:16:12 -04006347 * This routine is invoked to free a give sgl list and memory.
James Smartda0436e2009-05-22 14:51:39 -04006348 **/
James Smart8a9d2e82012-05-09 21:16:12 -04006349void
6350lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
James Smartda0436e2009-05-22 14:51:39 -04006351{
6352 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart8a9d2e82012-05-09 21:16:12 -04006353
6354 list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
6355 list_del(&sglq_entry->list);
6356 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
6357 kfree(sglq_entry);
6358 }
6359}
6360
6361/**
6362 * lpfc_free_els_sgl_list - Free els sgl list.
6363 * @phba: pointer to lpfc hba data structure.
6364 *
6365 * This routine is invoked to free the driver's els sgl list and memory.
6366 **/
6367static void
6368lpfc_free_els_sgl_list(struct lpfc_hba *phba)
6369{
James Smartda0436e2009-05-22 14:51:39 -04006370 LIST_HEAD(sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04006371
James Smart8a9d2e82012-05-09 21:16:12 -04006372 /* Retrieve all els sgls from driver list */
James Smartda0436e2009-05-22 14:51:39 -04006373 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006374 spin_lock(&phba->sli4_hba.sgl_list_lock);
6375 list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list, &sglq_list);
6376 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04006377 spin_unlock_irq(&phba->hbalock);
6378
James Smart8a9d2e82012-05-09 21:16:12 -04006379 /* Now free the sgl list */
6380 lpfc_free_sgl_list(phba, &sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04006381}
6382
6383/**
James Smartf358dd02017-02-12 13:52:34 -08006384 * lpfc_free_nvmet_sgl_list - Free nvmet sgl list.
6385 * @phba: pointer to lpfc hba data structure.
6386 *
6387 * This routine is invoked to free the driver's nvmet sgl list and memory.
6388 **/
6389static void
6390lpfc_free_nvmet_sgl_list(struct lpfc_hba *phba)
6391{
6392 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
6393 LIST_HEAD(sglq_list);
6394
6395 /* Retrieve all nvmet sgls from driver list */
6396 spin_lock_irq(&phba->hbalock);
6397 spin_lock(&phba->sli4_hba.sgl_list_lock);
6398 list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list, &sglq_list);
6399 spin_unlock(&phba->sli4_hba.sgl_list_lock);
6400 spin_unlock_irq(&phba->hbalock);
6401
6402 /* Now free the sgl list */
6403 list_for_each_entry_safe(sglq_entry, sglq_next, &sglq_list, list) {
6404 list_del(&sglq_entry->list);
6405 lpfc_nvmet_buf_free(phba, sglq_entry->virt, sglq_entry->phys);
6406 kfree(sglq_entry);
6407 }
6408}
6409
6410/**
James Smartda0436e2009-05-22 14:51:39 -04006411 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
6412 * @phba: pointer to lpfc hba data structure.
6413 *
6414 * This routine is invoked to allocate the driver's active sgl memory.
6415 * This array will hold the sglq_entry's for active IOs.
6416 **/
6417static int
6418lpfc_init_active_sgl_array(struct lpfc_hba *phba)
6419{
6420 int size;
6421 size = sizeof(struct lpfc_sglq *);
6422 size *= phba->sli4_hba.max_cfg_param.max_xri;
6423
6424 phba->sli4_hba.lpfc_sglq_active_list =
6425 kzalloc(size, GFP_KERNEL);
6426 if (!phba->sli4_hba.lpfc_sglq_active_list)
6427 return -ENOMEM;
6428 return 0;
6429}
6430
6431/**
6432 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
6433 * @phba: pointer to lpfc hba data structure.
6434 *
6435 * This routine is invoked to walk through the array of active sglq entries
6436 * and free all of the resources.
6437 * This is just a place holder for now.
6438 **/
6439static void
6440lpfc_free_active_sgl(struct lpfc_hba *phba)
6441{
6442 kfree(phba->sli4_hba.lpfc_sglq_active_list);
6443}
6444
6445/**
6446 * lpfc_init_sgl_list - Allocate and initialize sgl list.
6447 * @phba: pointer to lpfc hba data structure.
6448 *
6449 * This routine is invoked to allocate and initizlize the driver's sgl
6450 * list and set up the sgl xritag tag array accordingly.
6451 *
James Smartda0436e2009-05-22 14:51:39 -04006452 **/
James Smart8a9d2e82012-05-09 21:16:12 -04006453static void
James Smartda0436e2009-05-22 14:51:39 -04006454lpfc_init_sgl_list(struct lpfc_hba *phba)
6455{
James Smartda0436e2009-05-22 14:51:39 -04006456 /* Initialize and populate the sglq list per host/VF. */
James Smart895427b2017-02-12 13:52:30 -08006457 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_els_sgl_list);
James Smartda0436e2009-05-22 14:51:39 -04006458 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smartf358dd02017-02-12 13:52:34 -08006459 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_sgl_list);
James Smart86c67372017-04-21 16:05:04 -07006460 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
James Smartda0436e2009-05-22 14:51:39 -04006461
James Smart8a9d2e82012-05-09 21:16:12 -04006462 /* els xri-sgl book keeping */
6463 phba->sli4_hba.els_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04006464
James Smart8a9d2e82012-05-09 21:16:12 -04006465 /* scsi xri-buffer book keeping */
James Smartda0436e2009-05-22 14:51:39 -04006466 phba->sli4_hba.scsi_xri_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -08006467
6468 /* nvme xri-buffer book keeping */
6469 phba->sli4_hba.nvme_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04006470}
6471
6472/**
6473 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
6474 * @phba: pointer to lpfc hba data structure.
6475 *
6476 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -04006477 * port for those SLI4 ports that do not support extents. This routine
James Smartda0436e2009-05-22 14:51:39 -04006478 * posts a PAGE_SIZE memory region to the port to hold up to
James Smart88a2cfb2011-07-22 18:36:33 -04006479 * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
6480 * and should be called only when interrupts are disabled.
James Smartda0436e2009-05-22 14:51:39 -04006481 *
6482 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006483 * 0 - successful
James Smart88a2cfb2011-07-22 18:36:33 -04006484 * -ERROR - otherwise.
James Smartda0436e2009-05-22 14:51:39 -04006485 **/
6486int
6487lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
6488{
6489 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04006490 struct lpfc_rpi_hdr *rpi_hdr;
6491
6492 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
James Smartff78d8f2011-12-13 13:21:35 -05006493 if (!phba->sli4_hba.rpi_hdrs_in_use)
James Smart6d368e52011-05-24 11:44:12 -04006494 return rc;
James Smart6d368e52011-05-24 11:44:12 -04006495 if (phba->sli4_hba.extents_in_use)
6496 return -EIO;
James Smartda0436e2009-05-22 14:51:39 -04006497
6498 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
6499 if (!rpi_hdr) {
6500 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6501 "0391 Error during rpi post operation\n");
6502 lpfc_sli4_remove_rpis(phba);
6503 rc = -ENODEV;
6504 }
6505
6506 return rc;
6507}
6508
6509/**
6510 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
6511 * @phba: pointer to lpfc hba data structure.
6512 *
6513 * This routine is invoked to allocate a single 4KB memory region to
6514 * support rpis and stores them in the phba. This single region
6515 * provides support for up to 64 rpis. The region is used globally
6516 * by the device.
6517 *
6518 * Returns:
6519 * A valid rpi hdr on success.
6520 * A NULL pointer on any failure.
6521 **/
6522struct lpfc_rpi_hdr *
6523lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
6524{
6525 uint16_t rpi_limit, curr_rpi_range;
6526 struct lpfc_dmabuf *dmabuf;
6527 struct lpfc_rpi_hdr *rpi_hdr;
James Smart9589b0622011-04-16 11:03:17 -04006528 uint32_t rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04006529
James Smart6d368e52011-05-24 11:44:12 -04006530 /*
6531 * If the SLI4 port supports extents, posting the rpi header isn't
6532 * required. Set the expected maximum count and let the actual value
6533 * get set when extents are fully allocated.
6534 */
6535 if (!phba->sli4_hba.rpi_hdrs_in_use)
6536 return NULL;
6537 if (phba->sli4_hba.extents_in_use)
6538 return NULL;
6539
6540 /* The limit on the logical index is just the max_rpi count. */
James Smartda0436e2009-05-22 14:51:39 -04006541 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
James Smart6d368e52011-05-24 11:44:12 -04006542 phba->sli4_hba.max_cfg_param.max_rpi - 1;
James Smartda0436e2009-05-22 14:51:39 -04006543
6544 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04006545 /*
6546 * Establish the starting RPI in this header block. The starting
6547 * rpi is normalized to a zero base because the physical rpi is
6548 * port based.
6549 */
James Smart97f2ecf2012-03-01 22:35:23 -05006550 curr_rpi_range = phba->sli4_hba.next_rpi;
James Smartda0436e2009-05-22 14:51:39 -04006551 spin_unlock_irq(&phba->hbalock);
6552
6553 /*
6554 * The port has a limited number of rpis. The increment here
6555 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
6556 * and to allow the full max_rpi range per port.
6557 */
6558 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
James Smart9589b0622011-04-16 11:03:17 -04006559 rpi_count = rpi_limit - curr_rpi_range;
6560 else
6561 rpi_count = LPFC_RPI_HDR_COUNT;
James Smartda0436e2009-05-22 14:51:39 -04006562
James Smart6d368e52011-05-24 11:44:12 -04006563 if (!rpi_count)
6564 return NULL;
James Smartda0436e2009-05-22 14:51:39 -04006565 /*
6566 * First allocate the protocol header region for the port. The
6567 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
6568 */
6569 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
6570 if (!dmabuf)
6571 return NULL;
6572
Joe Perches1aee3832014-09-03 12:56:12 -04006573 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
6574 LPFC_HDR_TEMPLATE_SIZE,
6575 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04006576 if (!dmabuf->virt) {
6577 rpi_hdr = NULL;
6578 goto err_free_dmabuf;
6579 }
6580
James Smartda0436e2009-05-22 14:51:39 -04006581 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
6582 rpi_hdr = NULL;
6583 goto err_free_coherent;
6584 }
6585
6586 /* Save the rpi header data for cleanup later. */
6587 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
6588 if (!rpi_hdr)
6589 goto err_free_coherent;
6590
6591 rpi_hdr->dmabuf = dmabuf;
6592 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
6593 rpi_hdr->page_count = 1;
6594 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04006595
6596 /* The rpi_hdr stores the logical index only. */
6597 rpi_hdr->start_rpi = curr_rpi_range;
James Smartda0436e2009-05-22 14:51:39 -04006598 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
6599
6600 /*
James Smart6d368e52011-05-24 11:44:12 -04006601 * The next_rpi stores the next logical module-64 rpi value used
6602 * to post physical rpis in subsequent rpi postings.
James Smartda0436e2009-05-22 14:51:39 -04006603 */
James Smart9589b0622011-04-16 11:03:17 -04006604 phba->sli4_hba.next_rpi += rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04006605 spin_unlock_irq(&phba->hbalock);
6606 return rpi_hdr;
6607
6608 err_free_coherent:
6609 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
6610 dmabuf->virt, dmabuf->phys);
6611 err_free_dmabuf:
6612 kfree(dmabuf);
6613 return NULL;
6614}
6615
6616/**
6617 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
6618 * @phba: pointer to lpfc hba data structure.
6619 *
6620 * This routine is invoked to remove all memory resources allocated
James Smart6d368e52011-05-24 11:44:12 -04006621 * to support rpis for SLI4 ports not supporting extents. This routine
6622 * presumes the caller has released all rpis consumed by fabric or port
6623 * logins and is prepared to have the header pages removed.
James Smartda0436e2009-05-22 14:51:39 -04006624 **/
6625void
6626lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
6627{
6628 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
6629
James Smart6d368e52011-05-24 11:44:12 -04006630 if (!phba->sli4_hba.rpi_hdrs_in_use)
6631 goto exit;
6632
James Smartda0436e2009-05-22 14:51:39 -04006633 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
6634 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
6635 list_del(&rpi_hdr->list);
6636 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
6637 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
6638 kfree(rpi_hdr->dmabuf);
6639 kfree(rpi_hdr);
6640 }
James Smart6d368e52011-05-24 11:44:12 -04006641 exit:
6642 /* There are no rpis available to the port now. */
6643 phba->sli4_hba.next_rpi = 0;
James Smartda0436e2009-05-22 14:51:39 -04006644}
6645
6646/**
James Smart3772a992009-05-22 14:50:54 -04006647 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
6648 * @pdev: pointer to pci device data structure.
6649 *
6650 * This routine is invoked to allocate the driver hba data structure for an
6651 * HBA device. If the allocation is successful, the phba reference to the
6652 * PCI device data structure is set.
6653 *
6654 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006655 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04006656 * NULL - error
6657 **/
6658static struct lpfc_hba *
6659lpfc_hba_alloc(struct pci_dev *pdev)
6660{
6661 struct lpfc_hba *phba;
6662
6663 /* Allocate memory for HBA structure */
6664 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
6665 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02006666 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04006667 return NULL;
6668 }
6669
6670 /* Set reference to PCI device in HBA structure */
6671 phba->pcidev = pdev;
6672
6673 /* Assign an unused board number */
6674 phba->brd_no = lpfc_get_instance();
6675 if (phba->brd_no < 0) {
6676 kfree(phba);
6677 return NULL;
6678 }
James Smart65791f12016-07-06 12:35:56 -07006679 phba->eratt_poll_interval = LPFC_ERATT_POLL_INTERVAL;
James Smart3772a992009-05-22 14:50:54 -04006680
James Smart4fede782010-01-26 23:08:55 -05006681 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04006682 INIT_LIST_HEAD(&phba->ct_ev_waiters);
6683
James Smart3772a992009-05-22 14:50:54 -04006684 return phba;
6685}
6686
6687/**
6688 * lpfc_hba_free - Free driver hba data structure with a device.
6689 * @phba: pointer to lpfc hba data structure.
6690 *
6691 * This routine is invoked to free the driver hba data structure with an
6692 * HBA device.
6693 **/
6694static void
6695lpfc_hba_free(struct lpfc_hba *phba)
6696{
6697 /* Release the driver assigned board number */
6698 idr_remove(&lpfc_hba_index, phba->brd_no);
6699
James Smart895427b2017-02-12 13:52:30 -08006700 /* Free memory allocated with sli3 rings */
6701 kfree(phba->sli.sli3_ring);
6702 phba->sli.sli3_ring = NULL;
James Smart2a76a282012-08-03 12:35:54 -04006703
James Smart3772a992009-05-22 14:50:54 -04006704 kfree(phba);
6705 return;
6706}
6707
6708/**
6709 * lpfc_create_shost - Create hba physical port with associated scsi host.
6710 * @phba: pointer to lpfc hba data structure.
6711 *
6712 * This routine is invoked to create HBA physical port and associate a SCSI
6713 * host with it.
6714 *
6715 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006716 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006717 * other values - error
6718 **/
6719static int
6720lpfc_create_shost(struct lpfc_hba *phba)
6721{
6722 struct lpfc_vport *vport;
6723 struct Scsi_Host *shost;
6724
6725 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05006726 phba->fc_edtov = FF_DEF_EDTOV;
6727 phba->fc_ratov = FF_DEF_RATOV;
6728 phba->fc_altov = FF_DEF_ALTOV;
6729 phba->fc_arbtov = FF_DEF_ARBTOV;
6730
James Smartd7c47992010-06-08 18:31:54 -04006731 atomic_set(&phba->sdev_cnt, 0);
James Smart3de2a652007-08-02 11:09:59 -04006732 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05006733 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04006734 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05006735
6736 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05006737 phba->pport = vport;
James Smart2ea259e2017-02-12 13:52:27 -08006738
James Smartf358dd02017-02-12 13:52:34 -08006739 if (phba->nvmet_support) {
6740 /* Only 1 vport (pport) will support NVME target */
6741 if (phba->txrdy_payload_pool == NULL) {
6742 phba->txrdy_payload_pool = pci_pool_create(
6743 "txrdy_pool", phba->pcidev,
6744 TXRDY_PAYLOAD_LEN, 16, 0);
6745 if (phba->txrdy_payload_pool) {
6746 phba->targetport = NULL;
6747 phba->cfg_enable_fc4_type = LPFC_ENABLE_NVME;
6748 lpfc_printf_log(phba, KERN_INFO,
6749 LOG_INIT | LOG_NVME_DISC,
6750 "6076 NVME Target Found\n");
6751 }
6752 }
6753 }
6754
James Smart858c9f62007-06-17 19:56:39 -05006755 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04006756 /* Put reference to SCSI host to driver's device private data */
6757 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05006758
James Smart4258e982015-12-16 18:11:58 -05006759 /*
6760 * At this point we are fully registered with PSA. In addition,
6761 * any initial discovery should be completed.
6762 */
6763 vport->load_flag |= FC_ALLOW_FDMI;
James Smart8663cbb2016-03-31 14:12:33 -07006764 if (phba->cfg_enable_SmartSAN ||
6765 (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
James Smart4258e982015-12-16 18:11:58 -05006766
6767 /* Setup appropriate attribute masks */
6768 vport->fdmi_hba_mask = LPFC_FDMI2_HBA_ATTR;
James Smart8663cbb2016-03-31 14:12:33 -07006769 if (phba->cfg_enable_SmartSAN)
James Smart4258e982015-12-16 18:11:58 -05006770 vport->fdmi_port_mask = LPFC_FDMI2_SMART_ATTR;
6771 else
6772 vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
6773 }
James Smart3772a992009-05-22 14:50:54 -04006774 return 0;
6775}
dea31012005-04-17 16:05:31 -05006776
James Smart3772a992009-05-22 14:50:54 -04006777/**
6778 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
6779 * @phba: pointer to lpfc hba data structure.
6780 *
6781 * This routine is invoked to destroy HBA physical port and the associated
6782 * SCSI host.
6783 **/
6784static void
6785lpfc_destroy_shost(struct lpfc_hba *phba)
6786{
6787 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04006788
James Smart3772a992009-05-22 14:50:54 -04006789 /* Destroy physical port that associated with the SCSI host */
6790 destroy_port(vport);
6791
6792 return;
6793}
6794
6795/**
6796 * lpfc_setup_bg - Setup Block guard structures and debug areas.
6797 * @phba: pointer to lpfc hba data structure.
6798 * @shost: the shost to be used to detect Block guard settings.
6799 *
6800 * This routine sets up the local Block guard protocol settings for @shost.
6801 * This routine also allocates memory for debugging bg buffers.
6802 **/
6803static void
6804lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
6805{
James Smartbbeb79b2012-06-12 13:54:27 -04006806 uint32_t old_mask;
6807 uint32_t old_guard;
6808
James Smart3772a992009-05-22 14:50:54 -04006809 int pagecnt = 10;
James Smartb3b98b72016-10-13 15:06:06 -07006810 if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
James Smart3772a992009-05-22 14:50:54 -04006811 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6812 "1478 Registering BlockGuard with the "
6813 "SCSI layer\n");
James Smartbbeb79b2012-06-12 13:54:27 -04006814
James Smartb3b98b72016-10-13 15:06:06 -07006815 old_mask = phba->cfg_prot_mask;
6816 old_guard = phba->cfg_prot_guard;
James Smartbbeb79b2012-06-12 13:54:27 -04006817
6818 /* Only allow supported values */
James Smartb3b98b72016-10-13 15:06:06 -07006819 phba->cfg_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
James Smartbbeb79b2012-06-12 13:54:27 -04006820 SHOST_DIX_TYPE0_PROTECTION |
6821 SHOST_DIX_TYPE1_PROTECTION);
James Smartb3b98b72016-10-13 15:06:06 -07006822 phba->cfg_prot_guard &= (SHOST_DIX_GUARD_IP |
6823 SHOST_DIX_GUARD_CRC);
James Smartbbeb79b2012-06-12 13:54:27 -04006824
6825 /* DIF Type 1 protection for profiles AST1/C1 is end to end */
James Smartb3b98b72016-10-13 15:06:06 -07006826 if (phba->cfg_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
6827 phba->cfg_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
James Smartbbeb79b2012-06-12 13:54:27 -04006828
James Smartb3b98b72016-10-13 15:06:06 -07006829 if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
6830 if ((old_mask != phba->cfg_prot_mask) ||
6831 (old_guard != phba->cfg_prot_guard))
James Smartbbeb79b2012-06-12 13:54:27 -04006832 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6833 "1475 Registering BlockGuard with the "
6834 "SCSI layer: mask %d guard %d\n",
James Smartb3b98b72016-10-13 15:06:06 -07006835 phba->cfg_prot_mask,
6836 phba->cfg_prot_guard);
James Smartbbeb79b2012-06-12 13:54:27 -04006837
James Smartb3b98b72016-10-13 15:06:06 -07006838 scsi_host_set_prot(shost, phba->cfg_prot_mask);
6839 scsi_host_set_guard(shost, phba->cfg_prot_guard);
James Smartbbeb79b2012-06-12 13:54:27 -04006840 } else
6841 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6842 "1479 Not Registering BlockGuard with the SCSI "
6843 "layer, Bad protection parameters: %d %d\n",
6844 old_mask, old_guard);
James Smart98c9ea52007-10-27 13:37:33 -04006845 }
James Smartbbeb79b2012-06-12 13:54:27 -04006846
James Smart81301a92008-12-04 22:39:46 -05006847 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05006848 while (pagecnt) {
6849 spin_lock_init(&_dump_buf_lock);
6850 _dump_buf_data =
6851 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
6852 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04006853 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6854 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04006855 "_dump_buf_data at 0x%p\n",
6856 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05006857 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04006858 memset(_dump_buf_data, 0,
6859 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05006860 break;
James Smart3772a992009-05-22 14:50:54 -04006861 } else
James Smart81301a92008-12-04 22:39:46 -05006862 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05006863 }
James Smart81301a92008-12-04 22:39:46 -05006864 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04006865 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6866 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04006867 "memory for hexdump\n");
6868 } else
James Smart6a9c52c2009-10-02 15:16:51 -04006869 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6870 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05006871 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05006872 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05006873 while (pagecnt) {
6874 _dump_buf_dif =
6875 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
6876 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04006877 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6878 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04006879 "_dump_buf_dif at 0x%p\n",
6880 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05006881 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04006882 memset(_dump_buf_dif, 0,
6883 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05006884 break;
James Smart3772a992009-05-22 14:50:54 -04006885 } else
James Smart81301a92008-12-04 22:39:46 -05006886 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05006887 }
James Smart81301a92008-12-04 22:39:46 -05006888 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04006889 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6890 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04006891 "memory for hexdump\n");
6892 } else
James Smart6a9c52c2009-10-02 15:16:51 -04006893 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6894 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04006895 _dump_buf_dif);
6896}
6897
6898/**
6899 * lpfc_post_init_setup - Perform necessary device post initialization setup.
6900 * @phba: pointer to lpfc hba data structure.
6901 *
6902 * This routine is invoked to perform all the necessary post initialization
6903 * setup for the device.
6904 **/
6905static void
6906lpfc_post_init_setup(struct lpfc_hba *phba)
6907{
6908 struct Scsi_Host *shost;
6909 struct lpfc_adapter_event_header adapter_event;
6910
6911 /* Get the default values for Model Name and Description */
6912 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
6913
6914 /*
6915 * hba setup may have changed the hba_queue_depth so we need to
6916 * adjust the value of can_queue.
6917 */
6918 shost = pci_get_drvdata(phba->pcidev);
6919 shost->can_queue = phba->cfg_hba_queue_depth - 10;
6920 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
6921 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05006922
6923 lpfc_host_attrib_init(shost);
6924
James Smart2e0fef82007-06-17 19:56:36 -05006925 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
6926 spin_lock_irq(shost->host_lock);
6927 lpfc_poll_start_timer(phba);
6928 spin_unlock_irq(shost->host_lock);
6929 }
James Smart8f6d98d2006-08-01 07:34:00 -04006930
James Smart93996272008-08-24 21:50:30 -04006931 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6932 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04006933 /* Send board arrival event to upper layer */
6934 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
6935 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
6936 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04006937 sizeof(adapter_event),
6938 (char *) &adapter_event,
6939 LPFC_NL_VENDOR_ID);
6940 return;
6941}
6942
6943/**
6944 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
6945 * @phba: pointer to lpfc hba data structure.
6946 *
6947 * This routine is invoked to set up the PCI device memory space for device
6948 * with SLI-3 interface spec.
6949 *
6950 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006951 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006952 * other values - error
6953 **/
6954static int
6955lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
6956{
6957 struct pci_dev *pdev;
6958 unsigned long bar0map_len, bar2map_len;
6959 int i, hbq_count;
6960 void *ptr;
6961 int error = -ENODEV;
6962
6963 /* Obtain PCI device reference */
6964 if (!phba->pcidev)
6965 return error;
6966 else
6967 pdev = phba->pcidev;
6968
6969 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05006970 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
6971 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
6972 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
6973 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smart3772a992009-05-22 14:50:54 -04006974 return error;
Michael Reed8e685972009-09-18 12:02:05 -05006975 }
6976 }
James Smart3772a992009-05-22 14:50:54 -04006977
6978 /* Get the bus address of Bar0 and Bar2 and the number of bytes
6979 * required by each mapping.
6980 */
6981 phba->pci_bar0_map = pci_resource_start(pdev, 0);
6982 bar0map_len = pci_resource_len(pdev, 0);
6983
6984 phba->pci_bar2_map = pci_resource_start(pdev, 2);
6985 bar2map_len = pci_resource_len(pdev, 2);
6986
6987 /* Map HBA SLIM to a kernel virtual address. */
6988 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
6989 if (!phba->slim_memmap_p) {
6990 dev_printk(KERN_ERR, &pdev->dev,
6991 "ioremap failed for SLIM memory.\n");
6992 goto out;
6993 }
6994
6995 /* Map HBA Control Registers to a kernel virtual address. */
6996 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
6997 if (!phba->ctrl_regs_memmap_p) {
6998 dev_printk(KERN_ERR, &pdev->dev,
6999 "ioremap failed for HBA control registers.\n");
7000 goto out_iounmap_slim;
7001 }
7002
7003 /* Allocate memory for SLI-2 structures */
Joe Perches1aee3832014-09-03 12:56:12 -04007004 phba->slim2p.virt = dma_zalloc_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7005 &phba->slim2p.phys, GFP_KERNEL);
James Smart3772a992009-05-22 14:50:54 -04007006 if (!phba->slim2p.virt)
7007 goto out_iounmap;
7008
James Smart3772a992009-05-22 14:50:54 -04007009 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04007010 phba->mbox_ext = (phba->slim2p.virt +
7011 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04007012 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
7013 phba->IOCBs = (phba->slim2p.virt +
7014 offsetof(struct lpfc_sli2_slim, IOCBs));
7015
7016 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
7017 lpfc_sli_hbq_size(),
7018 &phba->hbqslimp.phys,
7019 GFP_KERNEL);
7020 if (!phba->hbqslimp.virt)
7021 goto out_free_slim;
7022
7023 hbq_count = lpfc_sli_hbq_count();
7024 ptr = phba->hbqslimp.virt;
7025 for (i = 0; i < hbq_count; ++i) {
7026 phba->hbqs[i].hbq_virt = ptr;
7027 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
7028 ptr += (lpfc_hbq_defs[i]->entry_count *
7029 sizeof(struct lpfc_hbq_entry));
7030 }
7031 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
7032 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
7033
7034 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
7035
James Smart3772a992009-05-22 14:50:54 -04007036 phba->MBslimaddr = phba->slim_memmap_p;
7037 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
7038 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
7039 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
7040 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04007041
dea31012005-04-17 16:05:31 -05007042 return 0;
7043
dea31012005-04-17 16:05:31 -05007044out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04007045 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7046 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05007047out_iounmap:
7048 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05007049out_iounmap_slim:
dea31012005-04-17 16:05:31 -05007050 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05007051out:
7052 return error;
7053}
7054
James Smarte59058c2008-08-24 21:49:00 -04007055/**
James Smart3772a992009-05-22 14:50:54 -04007056 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
7057 * @phba: pointer to lpfc hba data structure.
7058 *
7059 * This routine is invoked to unset the PCI device memory space for device
7060 * with SLI-3 interface spec.
7061 **/
7062static void
7063lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
7064{
7065 struct pci_dev *pdev;
7066
7067 /* Obtain PCI device reference */
7068 if (!phba->pcidev)
7069 return;
7070 else
7071 pdev = phba->pcidev;
7072
7073 /* Free coherent DMA memory allocated */
7074 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
7075 phba->hbqslimp.virt, phba->hbqslimp.phys);
7076 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7077 phba->slim2p.virt, phba->slim2p.phys);
7078
7079 /* I/O memory unmap */
7080 iounmap(phba->ctrl_regs_memmap_p);
7081 iounmap(phba->slim_memmap_p);
7082
7083 return;
7084}
7085
7086/**
James Smartda0436e2009-05-22 14:51:39 -04007087 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
7088 * @phba: pointer to lpfc hba data structure.
7089 *
7090 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
7091 * done and check status.
7092 *
7093 * Return 0 if successful, otherwise -ENODEV.
7094 **/
7095int
7096lpfc_sli4_post_status_check(struct lpfc_hba *phba)
7097{
James Smart2fcee4b2010-12-15 17:57:46 -05007098 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
7099 struct lpfc_register reg_data;
7100 int i, port_error = 0;
7101 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04007102
James Smart9940b972011-03-11 16:06:12 -05007103 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
7104 memset(&reg_data, 0, sizeof(reg_data));
James Smart2fcee4b2010-12-15 17:57:46 -05007105 if (!phba->sli4_hba.PSMPHRregaddr)
James Smartda0436e2009-05-22 14:51:39 -04007106 return -ENODEV;
7107
James Smartda0436e2009-05-22 14:51:39 -04007108 /* Wait up to 30 seconds for the SLI Port POST done and ready */
7109 for (i = 0; i < 3000; i++) {
James Smart9940b972011-03-11 16:06:12 -05007110 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
7111 &portsmphr_reg.word0) ||
7112 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007113 /* Port has a fatal POST error, break out */
James Smartda0436e2009-05-22 14:51:39 -04007114 port_error = -ENODEV;
7115 break;
7116 }
James Smart2fcee4b2010-12-15 17:57:46 -05007117 if (LPFC_POST_STAGE_PORT_READY ==
7118 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
James Smartda0436e2009-05-22 14:51:39 -04007119 break;
James Smartda0436e2009-05-22 14:51:39 -04007120 msleep(10);
7121 }
7122
James Smart2fcee4b2010-12-15 17:57:46 -05007123 /*
7124 * If there was a port error during POST, then don't proceed with
7125 * other register reads as the data may not be valid. Just exit.
7126 */
7127 if (port_error) {
James Smartda0436e2009-05-22 14:51:39 -04007128 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05007129 "1408 Port Failed POST - portsmphr=0x%x, "
7130 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
7131 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
7132 portsmphr_reg.word0,
7133 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
7134 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
7135 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
7136 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
7137 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
7138 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
7139 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
7140 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
7141 } else {
James Smart28baac72010-02-12 14:42:03 -05007142 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05007143 "2534 Device Info: SLIFamily=0x%x, "
7144 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
7145 "SLIHint_2=0x%x, FT=0x%x\n",
James Smart28baac72010-02-12 14:42:03 -05007146 bf_get(lpfc_sli_intf_sli_family,
7147 &phba->sli4_hba.sli_intf),
7148 bf_get(lpfc_sli_intf_slirev,
7149 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05007150 bf_get(lpfc_sli_intf_if_type,
James Smart28baac72010-02-12 14:42:03 -05007151 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05007152 bf_get(lpfc_sli_intf_sli_hint1,
7153 &phba->sli4_hba.sli_intf),
7154 bf_get(lpfc_sli_intf_sli_hint2,
7155 &phba->sli4_hba.sli_intf),
7156 bf_get(lpfc_sli_intf_func_type,
James Smart28baac72010-02-12 14:42:03 -05007157 &phba->sli4_hba.sli_intf));
James Smart2fcee4b2010-12-15 17:57:46 -05007158 /*
7159 * Check for other Port errors during the initialization
7160 * process. Fail the load if the port did not come up
7161 * correctly.
7162 */
7163 if_type = bf_get(lpfc_sli_intf_if_type,
7164 &phba->sli4_hba.sli_intf);
7165 switch (if_type) {
7166 case LPFC_SLI_INTF_IF_TYPE_0:
7167 phba->sli4_hba.ue_mask_lo =
7168 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
7169 phba->sli4_hba.ue_mask_hi =
7170 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
7171 uerrlo_reg.word0 =
7172 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
7173 uerrhi_reg.word0 =
7174 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
7175 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
7176 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
7177 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7178 "1422 Unrecoverable Error "
7179 "Detected during POST "
7180 "uerr_lo_reg=0x%x, "
7181 "uerr_hi_reg=0x%x, "
7182 "ue_mask_lo_reg=0x%x, "
7183 "ue_mask_hi_reg=0x%x\n",
7184 uerrlo_reg.word0,
7185 uerrhi_reg.word0,
7186 phba->sli4_hba.ue_mask_lo,
7187 phba->sli4_hba.ue_mask_hi);
7188 port_error = -ENODEV;
7189 }
7190 break;
7191 case LPFC_SLI_INTF_IF_TYPE_2:
7192 /* Final checks. The port status should be clean. */
James Smart9940b972011-03-11 16:06:12 -05007193 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
7194 &reg_data.word0) ||
James Smart05580562011-05-24 11:40:48 -04007195 (bf_get(lpfc_sliport_status_err, &reg_data) &&
7196 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007197 phba->work_status[0] =
7198 readl(phba->sli4_hba.u.if_type2.
7199 ERR1regaddr);
7200 phba->work_status[1] =
7201 readl(phba->sli4_hba.u.if_type2.
7202 ERR2regaddr);
7203 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05007204 "2888 Unrecoverable port error "
7205 "following POST: port status reg "
7206 "0x%x, port_smphr reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05007207 "error 1=0x%x, error 2=0x%x\n",
7208 reg_data.word0,
7209 portsmphr_reg.word0,
7210 phba->work_status[0],
7211 phba->work_status[1]);
7212 port_error = -ENODEV;
7213 }
7214 break;
7215 case LPFC_SLI_INTF_IF_TYPE_1:
7216 default:
7217 break;
7218 }
James Smart28baac72010-02-12 14:42:03 -05007219 }
James Smartda0436e2009-05-22 14:51:39 -04007220 return port_error;
7221}
7222
7223/**
7224 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
7225 * @phba: pointer to lpfc hba data structure.
James Smart2fcee4b2010-12-15 17:57:46 -05007226 * @if_type: The SLI4 interface type getting configured.
James Smartda0436e2009-05-22 14:51:39 -04007227 *
7228 * This routine is invoked to set up SLI4 BAR0 PCI config space register
7229 * memory map.
7230 **/
7231static void
James Smart2fcee4b2010-12-15 17:57:46 -05007232lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04007233{
James Smart2fcee4b2010-12-15 17:57:46 -05007234 switch (if_type) {
7235 case LPFC_SLI_INTF_IF_TYPE_0:
7236 phba->sli4_hba.u.if_type0.UERRLOregaddr =
7237 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
7238 phba->sli4_hba.u.if_type0.UERRHIregaddr =
7239 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
7240 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
7241 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
7242 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
7243 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
7244 phba->sli4_hba.SLIINTFregaddr =
7245 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
7246 break;
7247 case LPFC_SLI_INTF_IF_TYPE_2:
7248 phba->sli4_hba.u.if_type2.ERR1regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04007249 phba->sli4_hba.conf_regs_memmap_p +
7250 LPFC_CTL_PORT_ER1_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05007251 phba->sli4_hba.u.if_type2.ERR2regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04007252 phba->sli4_hba.conf_regs_memmap_p +
7253 LPFC_CTL_PORT_ER2_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05007254 phba->sli4_hba.u.if_type2.CTRLregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04007255 phba->sli4_hba.conf_regs_memmap_p +
7256 LPFC_CTL_PORT_CTL_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05007257 phba->sli4_hba.u.if_type2.STATUSregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04007258 phba->sli4_hba.conf_regs_memmap_p +
7259 LPFC_CTL_PORT_STA_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05007260 phba->sli4_hba.SLIINTFregaddr =
7261 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
7262 phba->sli4_hba.PSMPHRregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04007263 phba->sli4_hba.conf_regs_memmap_p +
7264 LPFC_CTL_PORT_SEM_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05007265 phba->sli4_hba.RQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05007266 phba->sli4_hba.conf_regs_memmap_p +
7267 LPFC_ULP0_RQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05007268 phba->sli4_hba.WQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05007269 phba->sli4_hba.conf_regs_memmap_p +
7270 LPFC_ULP0_WQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05007271 phba->sli4_hba.EQCQDBregaddr =
7272 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
7273 phba->sli4_hba.MQDBregaddr =
7274 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
7275 phba->sli4_hba.BMBXregaddr =
7276 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
7277 break;
7278 case LPFC_SLI_INTF_IF_TYPE_1:
7279 default:
7280 dev_printk(KERN_ERR, &phba->pcidev->dev,
7281 "FATAL - unsupported SLI4 interface type - %d\n",
7282 if_type);
7283 break;
7284 }
James Smartda0436e2009-05-22 14:51:39 -04007285}
7286
7287/**
7288 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
7289 * @phba: pointer to lpfc hba data structure.
7290 *
7291 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
7292 * memory map.
7293 **/
7294static void
7295lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
7296{
James Smart2fcee4b2010-12-15 17:57:46 -05007297 phba->sli4_hba.PSMPHRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
7298 LPFC_SLIPORT_IF0_SMPHR;
James Smartda0436e2009-05-22 14:51:39 -04007299 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05007300 LPFC_HST_ISR0;
James Smartda0436e2009-05-22 14:51:39 -04007301 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05007302 LPFC_HST_IMR0;
James Smartda0436e2009-05-22 14:51:39 -04007303 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05007304 LPFC_HST_ISCR0;
James Smartda0436e2009-05-22 14:51:39 -04007305}
7306
7307/**
7308 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
7309 * @phba: pointer to lpfc hba data structure.
7310 * @vf: virtual function number
7311 *
7312 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
7313 * based on the given viftual function number, @vf.
7314 *
7315 * Return 0 if successful, otherwise -ENODEV.
7316 **/
7317static int
7318lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
7319{
7320 if (vf > LPFC_VIR_FUNC_MAX)
7321 return -ENODEV;
7322
7323 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05007324 vf * LPFC_VFR_PAGE_SIZE +
7325 LPFC_ULP0_RQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04007326 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05007327 vf * LPFC_VFR_PAGE_SIZE +
7328 LPFC_ULP0_WQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04007329 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7330 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
7331 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7332 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
7333 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7334 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
7335 return 0;
7336}
7337
7338/**
7339 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
7340 * @phba: pointer to lpfc hba data structure.
7341 *
7342 * This routine is invoked to create the bootstrap mailbox
7343 * region consistent with the SLI-4 interface spec. This
7344 * routine allocates all memory necessary to communicate
7345 * mailbox commands to the port and sets up all alignment
7346 * needs. No locks are expected to be held when calling
7347 * this routine.
7348 *
7349 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007350 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04007351 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04007352 **/
7353static int
7354lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
7355{
7356 uint32_t bmbx_size;
7357 struct lpfc_dmabuf *dmabuf;
7358 struct dma_address *dma_address;
7359 uint32_t pa_addr;
7360 uint64_t phys_addr;
7361
7362 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
7363 if (!dmabuf)
7364 return -ENOMEM;
7365
7366 /*
7367 * The bootstrap mailbox region is comprised of 2 parts
7368 * plus an alignment restriction of 16 bytes.
7369 */
7370 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
Joe Perches1aee3832014-09-03 12:56:12 -04007371 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, bmbx_size,
7372 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04007373 if (!dmabuf->virt) {
7374 kfree(dmabuf);
7375 return -ENOMEM;
7376 }
James Smartda0436e2009-05-22 14:51:39 -04007377
7378 /*
7379 * Initialize the bootstrap mailbox pointers now so that the register
7380 * operations are simple later. The mailbox dma address is required
7381 * to be 16-byte aligned. Also align the virtual memory as each
7382 * maibox is copied into the bmbx mailbox region before issuing the
7383 * command to the port.
7384 */
7385 phba->sli4_hba.bmbx.dmabuf = dmabuf;
7386 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
7387
7388 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
7389 LPFC_ALIGN_16_BYTE);
7390 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
7391 LPFC_ALIGN_16_BYTE);
7392
7393 /*
7394 * Set the high and low physical addresses now. The SLI4 alignment
7395 * requirement is 16 bytes and the mailbox is posted to the port
7396 * as two 30-bit addresses. The other data is a bit marking whether
7397 * the 30-bit address is the high or low address.
7398 * Upcast bmbx aphys to 64bits so shift instruction compiles
7399 * clean on 32 bit machines.
7400 */
7401 dma_address = &phba->sli4_hba.bmbx.dma_address;
7402 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
7403 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
7404 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
7405 LPFC_BMBX_BIT1_ADDR_HI);
7406
7407 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
7408 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
7409 LPFC_BMBX_BIT1_ADDR_LO);
7410 return 0;
7411}
7412
7413/**
7414 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
7415 * @phba: pointer to lpfc hba data structure.
7416 *
7417 * This routine is invoked to teardown the bootstrap mailbox
7418 * region and release all host resources. This routine requires
7419 * the caller to ensure all mailbox commands recovered, no
7420 * additional mailbox comands are sent, and interrupts are disabled
7421 * before calling this routine.
7422 *
7423 **/
7424static void
7425lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
7426{
7427 dma_free_coherent(&phba->pcidev->dev,
7428 phba->sli4_hba.bmbx.bmbx_size,
7429 phba->sli4_hba.bmbx.dmabuf->virt,
7430 phba->sli4_hba.bmbx.dmabuf->phys);
7431
7432 kfree(phba->sli4_hba.bmbx.dmabuf);
7433 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
7434}
7435
7436/**
7437 * lpfc_sli4_read_config - Get the config parameters.
7438 * @phba: pointer to lpfc hba data structure.
7439 *
7440 * This routine is invoked to read the configuration parameters from the HBA.
7441 * The configuration parameters are used to set the base and maximum values
7442 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
7443 * allocation for the port.
7444 *
7445 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007446 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007447 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007448 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007449 **/
James Smartff78d8f2011-12-13 13:21:35 -05007450int
James Smartda0436e2009-05-22 14:51:39 -04007451lpfc_sli4_read_config(struct lpfc_hba *phba)
7452{
7453 LPFC_MBOXQ_t *pmb;
7454 struct lpfc_mbx_read_config *rd_config;
James Smart912e3ac2011-05-24 11:42:11 -04007455 union lpfc_sli4_cfg_shdr *shdr;
7456 uint32_t shdr_status, shdr_add_status;
7457 struct lpfc_mbx_get_func_cfg *get_func_cfg;
7458 struct lpfc_rsrc_desc_fcfcoe *desc;
James Smart8aa134a2012-08-14 14:25:29 -04007459 char *pdesc_0;
James Smartc6918162016-10-13 15:06:16 -07007460 uint16_t forced_link_speed;
7461 uint32_t if_type;
James Smart8aa134a2012-08-14 14:25:29 -04007462 int length, i, rc = 0, rc2;
James Smartda0436e2009-05-22 14:51:39 -04007463
7464 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7465 if (!pmb) {
7466 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7467 "2011 Unable to allocate memory for issuing "
7468 "SLI_CONFIG_SPECIAL mailbox command\n");
7469 return -ENOMEM;
7470 }
7471
7472 lpfc_read_config(phba, pmb);
7473
7474 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
7475 if (rc != MBX_SUCCESS) {
7476 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7477 "2012 Mailbox failed , mbxCmd x%x "
7478 "READ_CONFIG, mbxStatus x%x\n",
7479 bf_get(lpfc_mqe_command, &pmb->u.mqe),
7480 bf_get(lpfc_mqe_status, &pmb->u.mqe));
7481 rc = -EIO;
7482 } else {
7483 rd_config = &pmb->u.mqe.un.rd_config;
James Smartff78d8f2011-12-13 13:21:35 -05007484 if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
7485 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
7486 phba->sli4_hba.lnk_info.lnk_tp =
7487 bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
7488 phba->sli4_hba.lnk_info.lnk_no =
7489 bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
7490 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7491 "3081 lnk_type:%d, lnk_numb:%d\n",
7492 phba->sli4_hba.lnk_info.lnk_tp,
7493 phba->sli4_hba.lnk_info.lnk_no);
7494 } else
7495 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
7496 "3082 Mailbox (x%x) returned ldv:x0\n",
7497 bf_get(lpfc_mqe_command, &pmb->u.mqe));
James Smart6d368e52011-05-24 11:44:12 -04007498 phba->sli4_hba.extents_in_use =
7499 bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04007500 phba->sli4_hba.max_cfg_param.max_xri =
7501 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
7502 phba->sli4_hba.max_cfg_param.xri_base =
7503 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
7504 phba->sli4_hba.max_cfg_param.max_vpi =
7505 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
7506 phba->sli4_hba.max_cfg_param.vpi_base =
7507 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
7508 phba->sli4_hba.max_cfg_param.max_rpi =
7509 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
7510 phba->sli4_hba.max_cfg_param.rpi_base =
7511 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
7512 phba->sli4_hba.max_cfg_param.max_vfi =
7513 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
7514 phba->sli4_hba.max_cfg_param.vfi_base =
7515 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
7516 phba->sli4_hba.max_cfg_param.max_fcfi =
7517 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04007518 phba->sli4_hba.max_cfg_param.max_eq =
7519 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
7520 phba->sli4_hba.max_cfg_param.max_rq =
7521 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
7522 phba->sli4_hba.max_cfg_param.max_wq =
7523 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
7524 phba->sli4_hba.max_cfg_param.max_cq =
7525 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
7526 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
7527 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
7528 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
7529 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
James Smart5ffc2662009-11-18 15:39:44 -05007530 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
7531 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04007532 phba->max_vports = phba->max_vpi;
7533 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart6d368e52011-05-24 11:44:12 -04007534 "2003 cfg params Extents? %d "
7535 "XRI(B:%d M:%d), "
James Smartda0436e2009-05-22 14:51:39 -04007536 "VPI(B:%d M:%d) "
7537 "VFI(B:%d M:%d) "
7538 "RPI(B:%d M:%d) "
James Smart2ea259e2017-02-12 13:52:27 -08007539 "FCFI:%d EQ:%d CQ:%d WQ:%d RQ:%d\n",
James Smart6d368e52011-05-24 11:44:12 -04007540 phba->sli4_hba.extents_in_use,
James Smartda0436e2009-05-22 14:51:39 -04007541 phba->sli4_hba.max_cfg_param.xri_base,
7542 phba->sli4_hba.max_cfg_param.max_xri,
7543 phba->sli4_hba.max_cfg_param.vpi_base,
7544 phba->sli4_hba.max_cfg_param.max_vpi,
7545 phba->sli4_hba.max_cfg_param.vfi_base,
7546 phba->sli4_hba.max_cfg_param.max_vfi,
7547 phba->sli4_hba.max_cfg_param.rpi_base,
7548 phba->sli4_hba.max_cfg_param.max_rpi,
James Smart2ea259e2017-02-12 13:52:27 -08007549 phba->sli4_hba.max_cfg_param.max_fcfi,
7550 phba->sli4_hba.max_cfg_param.max_eq,
7551 phba->sli4_hba.max_cfg_param.max_cq,
7552 phba->sli4_hba.max_cfg_param.max_wq,
7553 phba->sli4_hba.max_cfg_param.max_rq);
7554
James Smartda0436e2009-05-22 14:51:39 -04007555 }
James Smart912e3ac2011-05-24 11:42:11 -04007556
7557 if (rc)
7558 goto read_cfg_out;
James Smartda0436e2009-05-22 14:51:39 -04007559
James Smartc6918162016-10-13 15:06:16 -07007560 /* Update link speed if forced link speed is supported */
7561 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7562 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
7563 forced_link_speed =
7564 bf_get(lpfc_mbx_rd_conf_link_speed, rd_config);
7565 if (forced_link_speed) {
7566 phba->hba_flag |= HBA_FORCED_LINK_SPEED;
7567
7568 switch (forced_link_speed) {
7569 case LINK_SPEED_1G:
7570 phba->cfg_link_speed =
7571 LPFC_USER_LINK_SPEED_1G;
7572 break;
7573 case LINK_SPEED_2G:
7574 phba->cfg_link_speed =
7575 LPFC_USER_LINK_SPEED_2G;
7576 break;
7577 case LINK_SPEED_4G:
7578 phba->cfg_link_speed =
7579 LPFC_USER_LINK_SPEED_4G;
7580 break;
7581 case LINK_SPEED_8G:
7582 phba->cfg_link_speed =
7583 LPFC_USER_LINK_SPEED_8G;
7584 break;
7585 case LINK_SPEED_10G:
7586 phba->cfg_link_speed =
7587 LPFC_USER_LINK_SPEED_10G;
7588 break;
7589 case LINK_SPEED_16G:
7590 phba->cfg_link_speed =
7591 LPFC_USER_LINK_SPEED_16G;
7592 break;
7593 case LINK_SPEED_32G:
7594 phba->cfg_link_speed =
7595 LPFC_USER_LINK_SPEED_32G;
7596 break;
7597 case 0xffff:
7598 phba->cfg_link_speed =
7599 LPFC_USER_LINK_SPEED_AUTO;
7600 break;
7601 default:
7602 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7603 "0047 Unrecognized link "
7604 "speed : %d\n",
7605 forced_link_speed);
7606 phba->cfg_link_speed =
7607 LPFC_USER_LINK_SPEED_AUTO;
7608 }
7609 }
7610 }
7611
James Smartda0436e2009-05-22 14:51:39 -04007612 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -04007613 length = phba->sli4_hba.max_cfg_param.max_xri -
7614 lpfc_sli4_get_els_iocb_cnt(phba);
7615 if (phba->cfg_hba_queue_depth > length) {
7616 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7617 "3361 HBA queue depth changed from %d to %d\n",
7618 phba->cfg_hba_queue_depth, length);
7619 phba->cfg_hba_queue_depth = length;
7620 }
James Smart912e3ac2011-05-24 11:42:11 -04007621
7622 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
7623 LPFC_SLI_INTF_IF_TYPE_2)
7624 goto read_cfg_out;
7625
7626 /* get the pf# and vf# for SLI4 if_type 2 port */
7627 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
7628 sizeof(struct lpfc_sli4_cfg_mhdr));
7629 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
7630 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
7631 length, LPFC_SLI4_MBX_EMBED);
7632
James Smart8aa134a2012-08-14 14:25:29 -04007633 rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart912e3ac2011-05-24 11:42:11 -04007634 shdr = (union lpfc_sli4_cfg_shdr *)
7635 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
7636 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7637 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart8aa134a2012-08-14 14:25:29 -04007638 if (rc2 || shdr_status || shdr_add_status) {
James Smart912e3ac2011-05-24 11:42:11 -04007639 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7640 "3026 Mailbox failed , mbxCmd x%x "
7641 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
7642 bf_get(lpfc_mqe_command, &pmb->u.mqe),
7643 bf_get(lpfc_mqe_status, &pmb->u.mqe));
James Smart912e3ac2011-05-24 11:42:11 -04007644 goto read_cfg_out;
7645 }
7646
7647 /* search for fc_fcoe resrouce descriptor */
7648 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
James Smart912e3ac2011-05-24 11:42:11 -04007649
James Smart8aa134a2012-08-14 14:25:29 -04007650 pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
7651 desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
7652 length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
7653 if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
7654 length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
7655 else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
7656 goto read_cfg_out;
7657
James Smart912e3ac2011-05-24 11:42:11 -04007658 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
James Smart8aa134a2012-08-14 14:25:29 -04007659 desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
James Smart912e3ac2011-05-24 11:42:11 -04007660 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
James Smart8aa134a2012-08-14 14:25:29 -04007661 bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
James Smart912e3ac2011-05-24 11:42:11 -04007662 phba->sli4_hba.iov.pf_number =
7663 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
7664 phba->sli4_hba.iov.vf_number =
7665 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
7666 break;
7667 }
7668 }
7669
7670 if (i < LPFC_RSRC_DESC_MAX_NUM)
7671 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7672 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
7673 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
7674 phba->sli4_hba.iov.vf_number);
James Smart8aa134a2012-08-14 14:25:29 -04007675 else
James Smart912e3ac2011-05-24 11:42:11 -04007676 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7677 "3028 GET_FUNCTION_CONFIG: failed to find "
7678 "Resrouce Descriptor:x%x\n",
7679 LPFC_RSRC_DESC_TYPE_FCFCOE);
James Smart912e3ac2011-05-24 11:42:11 -04007680
7681read_cfg_out:
7682 mempool_free(pmb, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04007683 return rc;
7684}
7685
7686/**
James Smart2fcee4b2010-12-15 17:57:46 -05007687 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
James Smartda0436e2009-05-22 14:51:39 -04007688 * @phba: pointer to lpfc hba data structure.
7689 *
James Smart2fcee4b2010-12-15 17:57:46 -05007690 * This routine is invoked to setup the port-side endian order when
7691 * the port if_type is 0. This routine has no function for other
7692 * if_types.
James Smartda0436e2009-05-22 14:51:39 -04007693 *
7694 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007695 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007696 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007697 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007698 **/
7699static int
7700lpfc_setup_endian_order(struct lpfc_hba *phba)
7701{
7702 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05007703 uint32_t if_type, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04007704 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
7705 HOST_ENDIAN_HIGH_WORD1};
7706
James Smart2fcee4b2010-12-15 17:57:46 -05007707 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7708 switch (if_type) {
7709 case LPFC_SLI_INTF_IF_TYPE_0:
7710 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
7711 GFP_KERNEL);
7712 if (!mboxq) {
7713 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7714 "0492 Unable to allocate memory for "
7715 "issuing SLI_CONFIG_SPECIAL mailbox "
7716 "command\n");
7717 return -ENOMEM;
7718 }
James Smartda0436e2009-05-22 14:51:39 -04007719
James Smart2fcee4b2010-12-15 17:57:46 -05007720 /*
7721 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
7722 * two words to contain special data values and no other data.
7723 */
7724 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
7725 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
7726 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7727 if (rc != MBX_SUCCESS) {
7728 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7729 "0493 SLI_CONFIG_SPECIAL mailbox "
7730 "failed with status x%x\n",
7731 rc);
7732 rc = -EIO;
7733 }
7734 mempool_free(mboxq, phba->mbox_mem_pool);
7735 break;
7736 case LPFC_SLI_INTF_IF_TYPE_2:
7737 case LPFC_SLI_INTF_IF_TYPE_1:
7738 default:
7739 break;
James Smartda0436e2009-05-22 14:51:39 -04007740 }
James Smartda0436e2009-05-22 14:51:39 -04007741 return rc;
7742}
7743
7744/**
James Smart895427b2017-02-12 13:52:30 -08007745 * lpfc_sli4_queue_verify - Verify and update EQ counts
James Smartda0436e2009-05-22 14:51:39 -04007746 * @phba: pointer to lpfc hba data structure.
7747 *
James Smart895427b2017-02-12 13:52:30 -08007748 * This routine is invoked to check the user settable queue counts for EQs.
7749 * After this routine is called the counts will be set to valid values that
James Smart5350d872011-10-10 21:33:49 -04007750 * adhere to the constraints of the system's interrupt vectors and the port's
7751 * queue resources.
James Smartda0436e2009-05-22 14:51:39 -04007752 *
7753 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007754 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007755 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04007756 **/
7757static int
James Smart5350d872011-10-10 21:33:49 -04007758lpfc_sli4_queue_verify(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04007759{
James Smart895427b2017-02-12 13:52:30 -08007760 int io_channel;
James Smart1ba981f2014-02-20 09:56:45 -05007761 int fof_vectors = phba->cfg_fof ? 1 : 0;
James Smartda0436e2009-05-22 14:51:39 -04007762
7763 /*
James Smart67d12732012-08-03 12:36:13 -04007764 * Sanity check for configured queue parameters against the run-time
James Smartda0436e2009-05-22 14:51:39 -04007765 * device parameters
7766 */
7767
James Smart67d12732012-08-03 12:36:13 -04007768 /* Sanity check on HBA EQ parameters */
James Smart895427b2017-02-12 13:52:30 -08007769 io_channel = phba->io_channel_irqs;
James Smartda0436e2009-05-22 14:51:39 -04007770
James Smart895427b2017-02-12 13:52:30 -08007771 if (phba->sli4_hba.num_online_cpu < io_channel) {
James Smart82c3e9b2012-09-29 11:29:50 -04007772 lpfc_printf_log(phba,
7773 KERN_ERR, LOG_INIT,
James Smart90695ee2012-08-14 14:25:36 -04007774 "3188 Reducing IO channels to match number of "
James Smart7bb03bb2013-04-17 20:19:16 -04007775 "online CPUs: from %d to %d\n",
James Smart895427b2017-02-12 13:52:30 -08007776 io_channel, phba->sli4_hba.num_online_cpu);
7777 io_channel = phba->sli4_hba.num_online_cpu;
James Smart90695ee2012-08-14 14:25:36 -04007778 }
7779
James Smart895427b2017-02-12 13:52:30 -08007780 if (io_channel + fof_vectors > phba->sli4_hba.max_cfg_param.max_eq) {
James Smart82c3e9b2012-09-29 11:29:50 -04007781 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7782 "2575 Reducing IO channels to match number of "
7783 "available EQs: from %d to %d\n",
James Smart895427b2017-02-12 13:52:30 -08007784 io_channel,
James Smart82c3e9b2012-09-29 11:29:50 -04007785 phba->sli4_hba.max_cfg_param.max_eq);
James Smart895427b2017-02-12 13:52:30 -08007786 io_channel = phba->sli4_hba.max_cfg_param.max_eq - fof_vectors;
James Smartda0436e2009-05-22 14:51:39 -04007787 }
James Smart67d12732012-08-03 12:36:13 -04007788
James Smart895427b2017-02-12 13:52:30 -08007789 /* The actual number of FCP / NVME event queues adopted */
7790 if (io_channel != phba->io_channel_irqs)
7791 phba->io_channel_irqs = io_channel;
7792 if (phba->cfg_fcp_io_channel > io_channel)
7793 phba->cfg_fcp_io_channel = io_channel;
7794 if (phba->cfg_nvme_io_channel > io_channel)
7795 phba->cfg_nvme_io_channel = io_channel;
James Smart2d7dbc42017-02-12 13:52:35 -08007796 if (phba->cfg_nvme_io_channel < phba->cfg_nvmet_mrq)
7797 phba->cfg_nvmet_mrq = phba->cfg_nvme_io_channel;
James Smart895427b2017-02-12 13:52:30 -08007798
7799 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2d7dbc42017-02-12 13:52:35 -08007800 "2574 IO channels: irqs %d fcp %d nvme %d MRQ: %d\n",
James Smart895427b2017-02-12 13:52:30 -08007801 phba->io_channel_irqs, phba->cfg_fcp_io_channel,
James Smart2d7dbc42017-02-12 13:52:35 -08007802 phba->cfg_nvme_io_channel, phba->cfg_nvmet_mrq);
James Smartda0436e2009-05-22 14:51:39 -04007803
James Smartda0436e2009-05-22 14:51:39 -04007804 /* Get EQ depth from module parameter, fake the default for now */
7805 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
7806 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
7807
James Smart5350d872011-10-10 21:33:49 -04007808 /* Get CQ depth from module parameter, fake the default for now */
7809 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
7810 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
James Smart5350d872011-10-10 21:33:49 -04007811 return 0;
James Smart895427b2017-02-12 13:52:30 -08007812}
7813
7814static int
7815lpfc_alloc_nvme_wq_cq(struct lpfc_hba *phba, int wqidx)
7816{
7817 struct lpfc_queue *qdesc;
7818 int cnt;
7819
7820 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7821 phba->sli4_hba.cq_ecount);
7822 if (!qdesc) {
7823 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7824 "0508 Failed allocate fast-path NVME CQ (%d)\n",
7825 wqidx);
7826 return 1;
7827 }
7828 phba->sli4_hba.nvme_cq[wqidx] = qdesc;
7829
7830 cnt = LPFC_NVME_WQSIZE;
7831 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_WQE128_SIZE, cnt);
7832 if (!qdesc) {
7833 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7834 "0509 Failed allocate fast-path NVME WQ (%d)\n",
7835 wqidx);
7836 return 1;
7837 }
7838 phba->sli4_hba.nvme_wq[wqidx] = qdesc;
7839 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
7840 return 0;
7841}
7842
7843static int
7844lpfc_alloc_fcp_wq_cq(struct lpfc_hba *phba, int wqidx)
7845{
7846 struct lpfc_queue *qdesc;
7847 uint32_t wqesize;
7848
7849 /* Create Fast Path FCP CQs */
7850 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7851 phba->sli4_hba.cq_ecount);
7852 if (!qdesc) {
7853 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7854 "0499 Failed allocate fast-path FCP CQ (%d)\n", wqidx);
7855 return 1;
7856 }
7857 phba->sli4_hba.fcp_cq[wqidx] = qdesc;
7858
7859 /* Create Fast Path FCP WQs */
7860 wqesize = (phba->fcp_embed_io) ?
James Smartd1f525a2017-04-21 16:04:55 -07007861 LPFC_WQE128_SIZE : phba->sli4_hba.wq_esize;
James Smart895427b2017-02-12 13:52:30 -08007862 qdesc = lpfc_sli4_queue_alloc(phba, wqesize, phba->sli4_hba.wq_ecount);
7863 if (!qdesc) {
7864 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7865 "0503 Failed allocate fast-path FCP WQ (%d)\n",
7866 wqidx);
7867 return 1;
7868 }
7869 phba->sli4_hba.fcp_wq[wqidx] = qdesc;
7870 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
7871 return 0;
James Smart5350d872011-10-10 21:33:49 -04007872}
7873
7874/**
7875 * lpfc_sli4_queue_create - Create all the SLI4 queues
7876 * @phba: pointer to lpfc hba data structure.
7877 *
7878 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
7879 * operation. For each SLI4 queue type, the parameters such as queue entry
7880 * count (queue depth) shall be taken from the module parameter. For now,
7881 * we just use some constant number as place holder.
7882 *
7883 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07007884 * 0 - successful
James Smart5350d872011-10-10 21:33:49 -04007885 * -ENOMEM - No availble memory
7886 * -EIO - The mailbox failed to complete successfully.
7887 **/
7888int
7889lpfc_sli4_queue_create(struct lpfc_hba *phba)
7890{
7891 struct lpfc_queue *qdesc;
James Smartd1f525a2017-04-21 16:04:55 -07007892 int idx, io_channel;
James Smart5350d872011-10-10 21:33:49 -04007893
7894 /*
James Smart67d12732012-08-03 12:36:13 -04007895 * Create HBA Record arrays.
James Smart895427b2017-02-12 13:52:30 -08007896 * Both NVME and FCP will share that same vectors / EQs
James Smart5350d872011-10-10 21:33:49 -04007897 */
James Smart895427b2017-02-12 13:52:30 -08007898 io_channel = phba->io_channel_irqs;
7899 if (!io_channel)
James Smart67d12732012-08-03 12:36:13 -04007900 return -ERANGE;
James Smart5350d872011-10-10 21:33:49 -04007901
James Smart67d12732012-08-03 12:36:13 -04007902 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
7903 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
7904 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
7905 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
7906 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
7907 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
James Smart895427b2017-02-12 13:52:30 -08007908 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
7909 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
7910 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
7911 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
James Smart67d12732012-08-03 12:36:13 -04007912
James Smart895427b2017-02-12 13:52:30 -08007913 phba->sli4_hba.hba_eq = kcalloc(io_channel,
7914 sizeof(struct lpfc_queue *),
7915 GFP_KERNEL);
James Smart67d12732012-08-03 12:36:13 -04007916 if (!phba->sli4_hba.hba_eq) {
James Smartda0436e2009-05-22 14:51:39 -04007917 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04007918 "2576 Failed allocate memory for "
7919 "fast-path EQ record array\n");
James Smartda0436e2009-05-22 14:51:39 -04007920 goto out_error;
7921 }
James Smart67d12732012-08-03 12:36:13 -04007922
James Smart895427b2017-02-12 13:52:30 -08007923 if (phba->cfg_fcp_io_channel) {
7924 phba->sli4_hba.fcp_cq = kcalloc(phba->cfg_fcp_io_channel,
7925 sizeof(struct lpfc_queue *),
7926 GFP_KERNEL);
7927 if (!phba->sli4_hba.fcp_cq) {
7928 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7929 "2577 Failed allocate memory for "
7930 "fast-path CQ record array\n");
7931 goto out_error;
7932 }
7933 phba->sli4_hba.fcp_wq = kcalloc(phba->cfg_fcp_io_channel,
7934 sizeof(struct lpfc_queue *),
7935 GFP_KERNEL);
7936 if (!phba->sli4_hba.fcp_wq) {
7937 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7938 "2578 Failed allocate memory for "
7939 "fast-path FCP WQ record array\n");
7940 goto out_error;
7941 }
7942 /*
7943 * Since the first EQ can have multiple CQs associated with it,
7944 * this array is used to quickly see if we have a FCP fast-path
7945 * CQ match.
7946 */
7947 phba->sli4_hba.fcp_cq_map = kcalloc(phba->cfg_fcp_io_channel,
7948 sizeof(uint16_t),
7949 GFP_KERNEL);
7950 if (!phba->sli4_hba.fcp_cq_map) {
7951 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7952 "2545 Failed allocate memory for "
7953 "fast-path CQ map\n");
7954 goto out_error;
7955 }
James Smart67d12732012-08-03 12:36:13 -04007956 }
7957
James Smart895427b2017-02-12 13:52:30 -08007958 if (phba->cfg_nvme_io_channel) {
7959 phba->sli4_hba.nvme_cq = kcalloc(phba->cfg_nvme_io_channel,
7960 sizeof(struct lpfc_queue *),
7961 GFP_KERNEL);
7962 if (!phba->sli4_hba.nvme_cq) {
7963 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7964 "6077 Failed allocate memory for "
7965 "fast-path CQ record array\n");
7966 goto out_error;
7967 }
7968
James Smart895427b2017-02-12 13:52:30 -08007969 phba->sli4_hba.nvme_wq = kcalloc(phba->cfg_nvme_io_channel,
7970 sizeof(struct lpfc_queue *),
7971 GFP_KERNEL);
7972 if (!phba->sli4_hba.nvme_wq) {
7973 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7974 "2581 Failed allocate memory for "
7975 "fast-path NVME WQ record array\n");
7976 goto out_error;
7977 }
7978
7979 /*
7980 * Since the first EQ can have multiple CQs associated with it,
7981 * this array is used to quickly see if we have a NVME fast-path
7982 * CQ match.
7983 */
7984 phba->sli4_hba.nvme_cq_map = kcalloc(phba->cfg_nvme_io_channel,
7985 sizeof(uint16_t),
7986 GFP_KERNEL);
7987 if (!phba->sli4_hba.nvme_cq_map) {
7988 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7989 "6078 Failed allocate memory for "
7990 "fast-path CQ map\n");
7991 goto out_error;
7992 }
James Smart2d7dbc42017-02-12 13:52:35 -08007993
7994 if (phba->nvmet_support) {
7995 phba->sli4_hba.nvmet_cqset = kcalloc(
7996 phba->cfg_nvmet_mrq,
7997 sizeof(struct lpfc_queue *),
7998 GFP_KERNEL);
7999 if (!phba->sli4_hba.nvmet_cqset) {
8000 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8001 "3121 Fail allocate memory for "
8002 "fast-path CQ set array\n");
8003 goto out_error;
8004 }
8005 phba->sli4_hba.nvmet_mrq_hdr = kcalloc(
8006 phba->cfg_nvmet_mrq,
8007 sizeof(struct lpfc_queue *),
8008 GFP_KERNEL);
8009 if (!phba->sli4_hba.nvmet_mrq_hdr) {
8010 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8011 "3122 Fail allocate memory for "
8012 "fast-path RQ set hdr array\n");
8013 goto out_error;
8014 }
8015 phba->sli4_hba.nvmet_mrq_data = kcalloc(
8016 phba->cfg_nvmet_mrq,
8017 sizeof(struct lpfc_queue *),
8018 GFP_KERNEL);
8019 if (!phba->sli4_hba.nvmet_mrq_data) {
8020 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8021 "3124 Fail allocate memory for "
8022 "fast-path RQ set data array\n");
8023 goto out_error;
8024 }
8025 }
James Smart67d12732012-08-03 12:36:13 -04008026 }
James Smartda0436e2009-05-22 14:51:39 -04008027
James Smart895427b2017-02-12 13:52:30 -08008028 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
James Smart67d12732012-08-03 12:36:13 -04008029
James Smart895427b2017-02-12 13:52:30 -08008030 /* Create HBA Event Queues (EQs) */
8031 for (idx = 0; idx < io_channel; idx++) {
James Smart67d12732012-08-03 12:36:13 -04008032 /* Create EQs */
James Smartda0436e2009-05-22 14:51:39 -04008033 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
8034 phba->sli4_hba.eq_ecount);
8035 if (!qdesc) {
8036 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04008037 "0497 Failed allocate EQ (%d)\n", idx);
8038 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008039 }
James Smart67d12732012-08-03 12:36:13 -04008040 phba->sli4_hba.hba_eq[idx] = qdesc;
James Smartda0436e2009-05-22 14:51:39 -04008041 }
8042
James Smart895427b2017-02-12 13:52:30 -08008043 /* FCP and NVME io channels are not required to be balanced */
8044
8045 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++)
8046 if (lpfc_alloc_fcp_wq_cq(phba, idx))
8047 goto out_error;
8048
8049 for (idx = 0; idx < phba->cfg_nvme_io_channel; idx++)
8050 if (lpfc_alloc_nvme_wq_cq(phba, idx))
8051 goto out_error;
James Smart67d12732012-08-03 12:36:13 -04008052
James Smart2d7dbc42017-02-12 13:52:35 -08008053 if (phba->nvmet_support) {
8054 for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
8055 qdesc = lpfc_sli4_queue_alloc(phba,
8056 phba->sli4_hba.cq_esize,
8057 phba->sli4_hba.cq_ecount);
8058 if (!qdesc) {
8059 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8060 "3142 Failed allocate NVME "
8061 "CQ Set (%d)\n", idx);
8062 goto out_error;
8063 }
8064 phba->sli4_hba.nvmet_cqset[idx] = qdesc;
8065 }
8066 }
8067
James Smartda0436e2009-05-22 14:51:39 -04008068 /*
James Smart67d12732012-08-03 12:36:13 -04008069 * Create Slow Path Completion Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04008070 */
8071
James Smartda0436e2009-05-22 14:51:39 -04008072 /* Create slow-path Mailbox Command Complete Queue */
8073 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
8074 phba->sli4_hba.cq_ecount);
8075 if (!qdesc) {
8076 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8077 "0500 Failed allocate slow-path mailbox CQ\n");
James Smart67d12732012-08-03 12:36:13 -04008078 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008079 }
8080 phba->sli4_hba.mbx_cq = qdesc;
8081
8082 /* Create slow-path ELS Complete Queue */
8083 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
8084 phba->sli4_hba.cq_ecount);
8085 if (!qdesc) {
8086 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8087 "0501 Failed allocate slow-path ELS CQ\n");
James Smart67d12732012-08-03 12:36:13 -04008088 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008089 }
8090 phba->sli4_hba.els_cq = qdesc;
8091
James Smartda0436e2009-05-22 14:51:39 -04008092
James Smart5350d872011-10-10 21:33:49 -04008093 /*
James Smart67d12732012-08-03 12:36:13 -04008094 * Create Slow Path Work Queues (WQs)
James Smart5350d872011-10-10 21:33:49 -04008095 */
James Smartda0436e2009-05-22 14:51:39 -04008096
8097 /* Create Mailbox Command Queue */
James Smartda0436e2009-05-22 14:51:39 -04008098
8099 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
8100 phba->sli4_hba.mq_ecount);
8101 if (!qdesc) {
8102 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8103 "0505 Failed allocate slow-path MQ\n");
James Smart67d12732012-08-03 12:36:13 -04008104 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008105 }
8106 phba->sli4_hba.mbx_wq = qdesc;
8107
8108 /*
James Smart67d12732012-08-03 12:36:13 -04008109 * Create ELS Work Queues
James Smartda0436e2009-05-22 14:51:39 -04008110 */
James Smartda0436e2009-05-22 14:51:39 -04008111
8112 /* Create slow-path ELS Work Queue */
8113 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
8114 phba->sli4_hba.wq_ecount);
8115 if (!qdesc) {
8116 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8117 "0504 Failed allocate slow-path ELS WQ\n");
James Smart67d12732012-08-03 12:36:13 -04008118 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008119 }
8120 phba->sli4_hba.els_wq = qdesc;
James Smart895427b2017-02-12 13:52:30 -08008121 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8122
8123 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
8124 /* Create NVME LS Complete Queue */
8125 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
8126 phba->sli4_hba.cq_ecount);
8127 if (!qdesc) {
8128 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8129 "6079 Failed allocate NVME LS CQ\n");
8130 goto out_error;
8131 }
8132 phba->sli4_hba.nvmels_cq = qdesc;
8133
8134 /* Create NVME LS Work Queue */
8135 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
8136 phba->sli4_hba.wq_ecount);
8137 if (!qdesc) {
8138 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8139 "6080 Failed allocate NVME LS WQ\n");
8140 goto out_error;
8141 }
8142 phba->sli4_hba.nvmels_wq = qdesc;
8143 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8144 }
James Smartda0436e2009-05-22 14:51:39 -04008145
James Smartda0436e2009-05-22 14:51:39 -04008146 /*
8147 * Create Receive Queue (RQ)
8148 */
James Smartda0436e2009-05-22 14:51:39 -04008149
8150 /* Create Receive Queue for header */
8151 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
8152 phba->sli4_hba.rq_ecount);
8153 if (!qdesc) {
8154 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8155 "0506 Failed allocate receive HRQ\n");
James Smart67d12732012-08-03 12:36:13 -04008156 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008157 }
8158 phba->sli4_hba.hdr_rq = qdesc;
8159
8160 /* Create Receive Queue for data */
8161 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
8162 phba->sli4_hba.rq_ecount);
8163 if (!qdesc) {
8164 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8165 "0507 Failed allocate receive DRQ\n");
James Smart67d12732012-08-03 12:36:13 -04008166 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008167 }
8168 phba->sli4_hba.dat_rq = qdesc;
8169
James Smart2d7dbc42017-02-12 13:52:35 -08008170 if (phba->nvmet_support) {
8171 for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
8172 /* Create NVMET Receive Queue for header */
8173 qdesc = lpfc_sli4_queue_alloc(phba,
8174 phba->sli4_hba.rq_esize,
8175 phba->sli4_hba.rq_ecount);
8176 if (!qdesc) {
8177 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8178 "3146 Failed allocate "
8179 "receive HRQ\n");
8180 goto out_error;
8181 }
8182 phba->sli4_hba.nvmet_mrq_hdr[idx] = qdesc;
8183
8184 /* Only needed for header of RQ pair */
8185 qdesc->rqbp = kzalloc(sizeof(struct lpfc_rqb),
8186 GFP_KERNEL);
8187 if (qdesc->rqbp == NULL) {
8188 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8189 "6131 Failed allocate "
8190 "Header RQBP\n");
8191 goto out_error;
8192 }
8193
8194 /* Create NVMET Receive Queue for data */
8195 qdesc = lpfc_sli4_queue_alloc(phba,
8196 phba->sli4_hba.rq_esize,
8197 phba->sli4_hba.rq_ecount);
8198 if (!qdesc) {
8199 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8200 "3156 Failed allocate "
8201 "receive DRQ\n");
8202 goto out_error;
8203 }
8204 phba->sli4_hba.nvmet_mrq_data[idx] = qdesc;
8205 }
8206 }
8207
James Smart1ba981f2014-02-20 09:56:45 -05008208 /* Create the Queues needed for Flash Optimized Fabric operations */
8209 if (phba->cfg_fof)
8210 lpfc_fof_queue_create(phba);
James Smartda0436e2009-05-22 14:51:39 -04008211 return 0;
8212
James Smartda0436e2009-05-22 14:51:39 -04008213out_error:
James Smart67d12732012-08-03 12:36:13 -04008214 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04008215 return -ENOMEM;
8216}
8217
James Smart895427b2017-02-12 13:52:30 -08008218static inline void
8219__lpfc_sli4_release_queue(struct lpfc_queue **qp)
8220{
8221 if (*qp != NULL) {
8222 lpfc_sli4_queue_free(*qp);
8223 *qp = NULL;
8224 }
8225}
8226
8227static inline void
8228lpfc_sli4_release_queues(struct lpfc_queue ***qs, int max)
8229{
8230 int idx;
8231
8232 if (*qs == NULL)
8233 return;
8234
8235 for (idx = 0; idx < max; idx++)
8236 __lpfc_sli4_release_queue(&(*qs)[idx]);
8237
8238 kfree(*qs);
8239 *qs = NULL;
8240}
8241
8242static inline void
8243lpfc_sli4_release_queue_map(uint16_t **qmap)
8244{
8245 if (*qmap != NULL) {
8246 kfree(*qmap);
8247 *qmap = NULL;
8248 }
8249}
8250
James Smartda0436e2009-05-22 14:51:39 -04008251/**
8252 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
8253 * @phba: pointer to lpfc hba data structure.
8254 *
8255 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
8256 * operation.
8257 *
8258 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008259 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008260 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04008261 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04008262 **/
James Smart5350d872011-10-10 21:33:49 -04008263void
James Smartda0436e2009-05-22 14:51:39 -04008264lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
8265{
James Smart1ba981f2014-02-20 09:56:45 -05008266 if (phba->cfg_fof)
8267 lpfc_fof_queue_destroy(phba);
8268
James Smart895427b2017-02-12 13:52:30 -08008269 /* Release HBA eqs */
8270 lpfc_sli4_release_queues(&phba->sli4_hba.hba_eq, phba->io_channel_irqs);
James Smart67d12732012-08-03 12:36:13 -04008271
James Smart895427b2017-02-12 13:52:30 -08008272 /* Release FCP cqs */
8273 lpfc_sli4_release_queues(&phba->sli4_hba.fcp_cq,
James Smartd1f525a2017-04-21 16:04:55 -07008274 phba->cfg_fcp_io_channel);
James Smart67d12732012-08-03 12:36:13 -04008275
James Smart895427b2017-02-12 13:52:30 -08008276 /* Release FCP wqs */
8277 lpfc_sli4_release_queues(&phba->sli4_hba.fcp_wq,
James Smartd1f525a2017-04-21 16:04:55 -07008278 phba->cfg_fcp_io_channel);
James Smart67d12732012-08-03 12:36:13 -04008279
8280 /* Release FCP CQ mapping array */
James Smart895427b2017-02-12 13:52:30 -08008281 lpfc_sli4_release_queue_map(&phba->sli4_hba.fcp_cq_map);
8282
8283 /* Release NVME cqs */
8284 lpfc_sli4_release_queues(&phba->sli4_hba.nvme_cq,
8285 phba->cfg_nvme_io_channel);
8286
8287 /* Release NVME wqs */
8288 lpfc_sli4_release_queues(&phba->sli4_hba.nvme_wq,
8289 phba->cfg_nvme_io_channel);
8290
8291 /* Release NVME CQ mapping array */
8292 lpfc_sli4_release_queue_map(&phba->sli4_hba.nvme_cq_map);
James Smartda0436e2009-05-22 14:51:39 -04008293
James Smart2d7dbc42017-02-12 13:52:35 -08008294 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_cqset,
8295 phba->cfg_nvmet_mrq);
8296
8297 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_hdr,
8298 phba->cfg_nvmet_mrq);
8299 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_data,
8300 phba->cfg_nvmet_mrq);
8301
James Smartda0436e2009-05-22 14:51:39 -04008302 /* Release mailbox command work queue */
James Smart895427b2017-02-12 13:52:30 -08008303 __lpfc_sli4_release_queue(&phba->sli4_hba.mbx_wq);
James Smartda0436e2009-05-22 14:51:39 -04008304
8305 /* Release ELS work queue */
James Smart895427b2017-02-12 13:52:30 -08008306 __lpfc_sli4_release_queue(&phba->sli4_hba.els_wq);
8307
8308 /* Release ELS work queue */
8309 __lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_wq);
James Smartda0436e2009-05-22 14:51:39 -04008310
8311 /* Release unsolicited receive queue */
James Smart895427b2017-02-12 13:52:30 -08008312 __lpfc_sli4_release_queue(&phba->sli4_hba.hdr_rq);
8313 __lpfc_sli4_release_queue(&phba->sli4_hba.dat_rq);
James Smartda0436e2009-05-22 14:51:39 -04008314
James Smartda0436e2009-05-22 14:51:39 -04008315 /* Release ELS complete queue */
James Smart895427b2017-02-12 13:52:30 -08008316 __lpfc_sli4_release_queue(&phba->sli4_hba.els_cq);
8317
8318 /* Release NVME LS complete queue */
8319 __lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_cq);
James Smartda0436e2009-05-22 14:51:39 -04008320
8321 /* Release mailbox command complete queue */
James Smart895427b2017-02-12 13:52:30 -08008322 __lpfc_sli4_release_queue(&phba->sli4_hba.mbx_cq);
8323
8324 /* Everything on this list has been freed */
8325 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
8326}
8327
8328int
8329lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
8330 struct lpfc_queue *drq, int count)
8331{
8332 int rc, i;
8333 struct lpfc_rqe hrqe;
8334 struct lpfc_rqe drqe;
8335 struct lpfc_rqb *rqbp;
8336 struct rqb_dmabuf *rqb_buffer;
8337 LIST_HEAD(rqb_buf_list);
8338
8339 rqbp = hrq->rqbp;
8340 for (i = 0; i < count; i++) {
8341 rqb_buffer = (rqbp->rqb_alloc_buffer)(phba);
8342 if (!rqb_buffer)
8343 break;
8344 rqb_buffer->hrq = hrq;
8345 rqb_buffer->drq = drq;
8346 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
8347 }
8348 while (!list_empty(&rqb_buf_list)) {
8349 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
8350 hbuf.list);
8351
8352 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
8353 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
8354 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
8355 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
8356 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
8357 if (rc < 0) {
8358 (rqbp->rqb_free_buffer)(phba, rqb_buffer);
8359 } else {
8360 list_add_tail(&rqb_buffer->hbuf.list,
8361 &rqbp->rqb_buffer_list);
8362 rqbp->buffer_count++;
8363 }
8364 }
8365 return 1;
8366}
8367
8368int
8369lpfc_free_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *rq)
8370{
8371 struct lpfc_rqb *rqbp;
8372 struct lpfc_dmabuf *h_buf;
8373 struct rqb_dmabuf *rqb_buffer;
8374
8375 rqbp = rq->rqbp;
8376 while (!list_empty(&rqbp->rqb_buffer_list)) {
8377 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
8378 struct lpfc_dmabuf, list);
8379
8380 rqb_buffer = container_of(h_buf, struct rqb_dmabuf, hbuf);
8381 (rqbp->rqb_free_buffer)(phba, rqb_buffer);
8382 rqbp->buffer_count--;
8383 }
8384 return 1;
8385}
8386
8387static int
8388lpfc_create_wq_cq(struct lpfc_hba *phba, struct lpfc_queue *eq,
8389 struct lpfc_queue *cq, struct lpfc_queue *wq, uint16_t *cq_map,
8390 int qidx, uint32_t qtype)
8391{
8392 struct lpfc_sli_ring *pring;
8393 int rc;
8394
8395 if (!eq || !cq || !wq) {
8396 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8397 "6085 Fast-path %s (%d) not allocated\n",
8398 ((eq) ? ((cq) ? "WQ" : "CQ") : "EQ"), qidx);
8399 return -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04008400 }
James Smartda0436e2009-05-22 14:51:39 -04008401
James Smart895427b2017-02-12 13:52:30 -08008402 /* create the Cq first */
8403 rc = lpfc_cq_create(phba, cq, eq,
8404 (qtype == LPFC_MBOX) ? LPFC_MCQ : LPFC_WCQ, qtype);
8405 if (rc) {
8406 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8407 "6086 Failed setup of CQ (%d), rc = 0x%x\n",
8408 qidx, (uint32_t)rc);
8409 return rc;
8410 }
8411
8412 if (qtype != LPFC_MBOX) {
8413 /* Setup nvme_cq_map for fast lookup */
8414 if (cq_map)
8415 *cq_map = cq->queue_id;
8416
8417 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8418 "6087 CQ setup: cq[%d]-id=%d, parent eq[%d]-id=%d\n",
8419 qidx, cq->queue_id, qidx, eq->queue_id);
8420
8421 /* create the wq */
8422 rc = lpfc_wq_create(phba, wq, cq, qtype);
8423 if (rc) {
8424 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8425 "6123 Fail setup fastpath WQ (%d), rc = 0x%x\n",
8426 qidx, (uint32_t)rc);
8427 /* no need to tear down cq - caller will do so */
8428 return rc;
8429 }
8430
8431 /* Bind this CQ/WQ to the NVME ring */
8432 pring = wq->pring;
8433 pring->sli.sli4.wqp = (void *)wq;
8434 cq->pring = pring;
8435
8436 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8437 "2593 WQ setup: wq[%d]-id=%d assoc=%d, cq[%d]-id=%d\n",
8438 qidx, wq->queue_id, wq->assoc_qid, qidx, cq->queue_id);
8439 } else {
8440 rc = lpfc_mq_create(phba, wq, cq, LPFC_MBOX);
8441 if (rc) {
8442 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8443 "0539 Failed setup of slow-path MQ: "
8444 "rc = 0x%x\n", rc);
8445 /* no need to tear down cq - caller will do so */
8446 return rc;
8447 }
8448
8449 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8450 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
8451 phba->sli4_hba.mbx_wq->queue_id,
8452 phba->sli4_hba.mbx_cq->queue_id);
8453 }
8454
8455 return 0;
James Smartda0436e2009-05-22 14:51:39 -04008456}
8457
8458/**
8459 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
8460 * @phba: pointer to lpfc hba data structure.
8461 *
8462 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
8463 * operation.
8464 *
8465 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008466 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008467 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04008468 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04008469 **/
8470int
8471lpfc_sli4_queue_setup(struct lpfc_hba *phba)
8472{
James Smart962bc512013-01-03 15:44:00 -05008473 uint32_t shdr_status, shdr_add_status;
8474 union lpfc_sli4_cfg_shdr *shdr;
8475 LPFC_MBOXQ_t *mboxq;
James Smart895427b2017-02-12 13:52:30 -08008476 int qidx;
8477 uint32_t length, io_channel;
8478 int rc = -ENOMEM;
James Smart962bc512013-01-03 15:44:00 -05008479
8480 /* Check for dual-ULP support */
8481 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8482 if (!mboxq) {
8483 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8484 "3249 Unable to allocate memory for "
8485 "QUERY_FW_CFG mailbox command\n");
8486 return -ENOMEM;
8487 }
8488 length = (sizeof(struct lpfc_mbx_query_fw_config) -
8489 sizeof(struct lpfc_sli4_cfg_mhdr));
8490 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
8491 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
8492 length, LPFC_SLI4_MBX_EMBED);
8493
8494 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8495
8496 shdr = (union lpfc_sli4_cfg_shdr *)
8497 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
8498 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
8499 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
8500 if (shdr_status || shdr_add_status || rc) {
8501 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8502 "3250 QUERY_FW_CFG mailbox failed with status "
8503 "x%x add_status x%x, mbx status x%x\n",
8504 shdr_status, shdr_add_status, rc);
8505 if (rc != MBX_TIMEOUT)
8506 mempool_free(mboxq, phba->mbox_mem_pool);
8507 rc = -ENXIO;
8508 goto out_error;
8509 }
8510
8511 phba->sli4_hba.fw_func_mode =
8512 mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
8513 phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
8514 phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
James Smart8b017a32015-05-21 13:55:18 -04008515 phba->sli4_hba.physical_port =
8516 mboxq->u.mqe.un.query_fw_cfg.rsp.physical_port;
James Smart962bc512013-01-03 15:44:00 -05008517 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8518 "3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
8519 "ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
8520 phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
8521
8522 if (rc != MBX_TIMEOUT)
8523 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04008524
8525 /*
James Smart67d12732012-08-03 12:36:13 -04008526 * Set up HBA Event Queues (EQs)
James Smartda0436e2009-05-22 14:51:39 -04008527 */
James Smart895427b2017-02-12 13:52:30 -08008528 io_channel = phba->io_channel_irqs;
James Smartda0436e2009-05-22 14:51:39 -04008529
James Smart67d12732012-08-03 12:36:13 -04008530 /* Set up HBA event queue */
James Smart895427b2017-02-12 13:52:30 -08008531 if (io_channel && !phba->sli4_hba.hba_eq) {
James Smart2e90f4b2011-12-13 13:22:37 -05008532 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8533 "3147 Fast-path EQs not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05008534 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04008535 goto out_error;
James Smart2e90f4b2011-12-13 13:22:37 -05008536 }
James Smart895427b2017-02-12 13:52:30 -08008537 for (qidx = 0; qidx < io_channel; qidx++) {
8538 if (!phba->sli4_hba.hba_eq[qidx]) {
James Smartda0436e2009-05-22 14:51:39 -04008539 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8540 "0522 Fast-path EQ (%d) not "
James Smart895427b2017-02-12 13:52:30 -08008541 "allocated\n", qidx);
James Smart1b511972011-12-13 13:23:09 -05008542 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08008543 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008544 }
James Smart895427b2017-02-12 13:52:30 -08008545 rc = lpfc_eq_create(phba, phba->sli4_hba.hba_eq[qidx],
8546 phba->cfg_fcp_imax);
James Smartda0436e2009-05-22 14:51:39 -04008547 if (rc) {
8548 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8549 "0523 Failed setup of fast-path EQ "
James Smart895427b2017-02-12 13:52:30 -08008550 "(%d), rc = 0x%x\n", qidx,
James Smarta2fc4aef2014-09-03 12:57:55 -04008551 (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -08008552 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008553 }
8554 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008555 "2584 HBA EQ setup: queue[%d]-id=%d\n",
8556 qidx, phba->sli4_hba.hba_eq[qidx]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04008557 }
8558
James Smart895427b2017-02-12 13:52:30 -08008559 if (phba->cfg_nvme_io_channel) {
8560 if (!phba->sli4_hba.nvme_cq || !phba->sli4_hba.nvme_wq) {
James Smart67d12732012-08-03 12:36:13 -04008561 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008562 "6084 Fast-path NVME %s array not allocated\n",
8563 (phba->sli4_hba.nvme_cq) ? "CQ" : "WQ");
James Smart67d12732012-08-03 12:36:13 -04008564 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08008565 goto out_destroy;
James Smart67d12732012-08-03 12:36:13 -04008566 }
8567
James Smart895427b2017-02-12 13:52:30 -08008568 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++) {
8569 rc = lpfc_create_wq_cq(phba,
8570 phba->sli4_hba.hba_eq[
8571 qidx % io_channel],
8572 phba->sli4_hba.nvme_cq[qidx],
8573 phba->sli4_hba.nvme_wq[qidx],
8574 &phba->sli4_hba.nvme_cq_map[qidx],
8575 qidx, LPFC_NVME);
8576 if (rc) {
8577 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8578 "6123 Failed to setup fastpath "
8579 "NVME WQ/CQ (%d), rc = 0x%x\n",
8580 qidx, (uint32_t)rc);
8581 goto out_destroy;
8582 }
8583 }
James Smart67d12732012-08-03 12:36:13 -04008584 }
8585
James Smart895427b2017-02-12 13:52:30 -08008586 if (phba->cfg_fcp_io_channel) {
8587 /* Set up fast-path FCP Response Complete Queue */
8588 if (!phba->sli4_hba.fcp_cq || !phba->sli4_hba.fcp_wq) {
James Smart67d12732012-08-03 12:36:13 -04008589 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008590 "3148 Fast-path FCP %s array not allocated\n",
8591 phba->sli4_hba.fcp_cq ? "WQ" : "CQ");
James Smart67d12732012-08-03 12:36:13 -04008592 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08008593 goto out_destroy;
James Smart67d12732012-08-03 12:36:13 -04008594 }
8595
James Smart895427b2017-02-12 13:52:30 -08008596 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++) {
8597 rc = lpfc_create_wq_cq(phba,
8598 phba->sli4_hba.hba_eq[
8599 qidx % io_channel],
8600 phba->sli4_hba.fcp_cq[qidx],
8601 phba->sli4_hba.fcp_wq[qidx],
8602 &phba->sli4_hba.fcp_cq_map[qidx],
8603 qidx, LPFC_FCP);
8604 if (rc) {
8605 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8606 "0535 Failed to setup fastpath "
8607 "FCP WQ/CQ (%d), rc = 0x%x\n",
8608 qidx, (uint32_t)rc);
8609 goto out_destroy;
8610 }
8611 }
James Smart67d12732012-08-03 12:36:13 -04008612 }
James Smartda0436e2009-05-22 14:51:39 -04008613
8614 /*
James Smart895427b2017-02-12 13:52:30 -08008615 * Set up Slow Path Complete Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04008616 */
8617
James Smart895427b2017-02-12 13:52:30 -08008618 /* Set up slow-path MBOX CQ/MQ */
8619
8620 if (!phba->sli4_hba.mbx_cq || !phba->sli4_hba.mbx_wq) {
James Smartda0436e2009-05-22 14:51:39 -04008621 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008622 "0528 %s not allocated\n",
8623 phba->sli4_hba.mbx_cq ?
James Smartd1f525a2017-04-21 16:04:55 -07008624 "Mailbox WQ" : "Mailbox CQ");
James Smart1b511972011-12-13 13:23:09 -05008625 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08008626 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008627 }
James Smart895427b2017-02-12 13:52:30 -08008628
8629 rc = lpfc_create_wq_cq(phba, phba->sli4_hba.hba_eq[0],
James Smartd1f525a2017-04-21 16:04:55 -07008630 phba->sli4_hba.mbx_cq,
8631 phba->sli4_hba.mbx_wq,
8632 NULL, 0, LPFC_MBOX);
James Smartda0436e2009-05-22 14:51:39 -04008633 if (rc) {
8634 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008635 "0529 Failed setup of mailbox WQ/CQ: rc = 0x%x\n",
8636 (uint32_t)rc);
8637 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008638 }
James Smart2d7dbc42017-02-12 13:52:35 -08008639 if (phba->nvmet_support) {
8640 if (!phba->sli4_hba.nvmet_cqset) {
8641 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8642 "3165 Fast-path NVME CQ Set "
8643 "array not allocated\n");
8644 rc = -ENOMEM;
8645 goto out_destroy;
8646 }
8647 if (phba->cfg_nvmet_mrq > 1) {
8648 rc = lpfc_cq_create_set(phba,
8649 phba->sli4_hba.nvmet_cqset,
8650 phba->sli4_hba.hba_eq,
8651 LPFC_WCQ, LPFC_NVMET);
8652 if (rc) {
8653 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8654 "3164 Failed setup of NVME CQ "
8655 "Set, rc = 0x%x\n",
8656 (uint32_t)rc);
8657 goto out_destroy;
8658 }
8659 } else {
8660 /* Set up NVMET Receive Complete Queue */
8661 rc = lpfc_cq_create(phba, phba->sli4_hba.nvmet_cqset[0],
8662 phba->sli4_hba.hba_eq[0],
8663 LPFC_WCQ, LPFC_NVMET);
8664 if (rc) {
8665 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8666 "6089 Failed setup NVMET CQ: "
8667 "rc = 0x%x\n", (uint32_t)rc);
8668 goto out_destroy;
8669 }
8670 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8671 "6090 NVMET CQ setup: cq-id=%d, "
8672 "parent eq-id=%d\n",
8673 phba->sli4_hba.nvmet_cqset[0]->queue_id,
8674 phba->sli4_hba.hba_eq[0]->queue_id);
8675 }
8676 }
James Smartda0436e2009-05-22 14:51:39 -04008677
James Smart895427b2017-02-12 13:52:30 -08008678 /* Set up slow-path ELS WQ/CQ */
8679 if (!phba->sli4_hba.els_cq || !phba->sli4_hba.els_wq) {
James Smartda0436e2009-05-22 14:51:39 -04008680 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008681 "0530 ELS %s not allocated\n",
8682 phba->sli4_hba.els_cq ? "WQ" : "CQ");
James Smart1b511972011-12-13 13:23:09 -05008683 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08008684 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008685 }
James Smart895427b2017-02-12 13:52:30 -08008686 rc = lpfc_create_wq_cq(phba, phba->sli4_hba.hba_eq[0],
8687 phba->sli4_hba.els_cq,
8688 phba->sli4_hba.els_wq,
8689 NULL, 0, LPFC_ELS);
James Smartda0436e2009-05-22 14:51:39 -04008690 if (rc) {
8691 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008692 "0529 Failed setup of ELS WQ/CQ: rc = 0x%x\n",
8693 (uint32_t)rc);
8694 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008695 }
8696 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8697 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
8698 phba->sli4_hba.els_wq->queue_id,
8699 phba->sli4_hba.els_cq->queue_id);
8700
James Smart895427b2017-02-12 13:52:30 -08008701 if (phba->cfg_nvme_io_channel) {
8702 /* Set up NVME LS Complete Queue */
8703 if (!phba->sli4_hba.nvmels_cq || !phba->sli4_hba.nvmels_wq) {
8704 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8705 "6091 LS %s not allocated\n",
8706 phba->sli4_hba.nvmels_cq ? "WQ" : "CQ");
8707 rc = -ENOMEM;
8708 goto out_destroy;
8709 }
8710 rc = lpfc_create_wq_cq(phba, phba->sli4_hba.hba_eq[0],
8711 phba->sli4_hba.nvmels_cq,
8712 phba->sli4_hba.nvmels_wq,
8713 NULL, 0, LPFC_NVME_LS);
8714 if (rc) {
8715 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8716 "0529 Failed setup of NVVME LS WQ/CQ: "
8717 "rc = 0x%x\n", (uint32_t)rc);
8718 goto out_destroy;
8719 }
8720
8721 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8722 "6096 ELS WQ setup: wq-id=%d, "
8723 "parent cq-id=%d\n",
8724 phba->sli4_hba.nvmels_wq->queue_id,
8725 phba->sli4_hba.nvmels_cq->queue_id);
8726 }
8727
James Smart2d7dbc42017-02-12 13:52:35 -08008728 /*
8729 * Create NVMET Receive Queue (RQ)
8730 */
8731 if (phba->nvmet_support) {
8732 if ((!phba->sli4_hba.nvmet_cqset) ||
8733 (!phba->sli4_hba.nvmet_mrq_hdr) ||
8734 (!phba->sli4_hba.nvmet_mrq_data)) {
8735 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8736 "6130 MRQ CQ Queues not "
8737 "allocated\n");
8738 rc = -ENOMEM;
8739 goto out_destroy;
8740 }
8741 if (phba->cfg_nvmet_mrq > 1) {
8742 rc = lpfc_mrq_create(phba,
8743 phba->sli4_hba.nvmet_mrq_hdr,
8744 phba->sli4_hba.nvmet_mrq_data,
8745 phba->sli4_hba.nvmet_cqset,
8746 LPFC_NVMET);
8747 if (rc) {
8748 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8749 "6098 Failed setup of NVMET "
8750 "MRQ: rc = 0x%x\n",
8751 (uint32_t)rc);
8752 goto out_destroy;
8753 }
8754
8755 } else {
8756 rc = lpfc_rq_create(phba,
8757 phba->sli4_hba.nvmet_mrq_hdr[0],
8758 phba->sli4_hba.nvmet_mrq_data[0],
8759 phba->sli4_hba.nvmet_cqset[0],
8760 LPFC_NVMET);
8761 if (rc) {
8762 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8763 "6057 Failed setup of NVMET "
8764 "Receive Queue: rc = 0x%x\n",
8765 (uint32_t)rc);
8766 goto out_destroy;
8767 }
8768
8769 lpfc_printf_log(
8770 phba, KERN_INFO, LOG_INIT,
8771 "6099 NVMET RQ setup: hdr-rq-id=%d, "
8772 "dat-rq-id=%d parent cq-id=%d\n",
8773 phba->sli4_hba.nvmet_mrq_hdr[0]->queue_id,
8774 phba->sli4_hba.nvmet_mrq_data[0]->queue_id,
8775 phba->sli4_hba.nvmet_cqset[0]->queue_id);
8776
8777 }
8778 }
8779
James Smartda0436e2009-05-22 14:51:39 -04008780 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
8781 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8782 "0540 Receive Queue not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05008783 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08008784 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008785 }
James Smart73d91e52011-10-10 21:32:10 -04008786
8787 lpfc_rq_adjust_repost(phba, phba->sli4_hba.hdr_rq, LPFC_ELS_HBQ);
8788 lpfc_rq_adjust_repost(phba, phba->sli4_hba.dat_rq, LPFC_ELS_HBQ);
8789
James Smartda0436e2009-05-22 14:51:39 -04008790 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04008791 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04008792 if (rc) {
8793 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8794 "0541 Failed setup of Receive Queue: "
James Smarta2fc4aef2014-09-03 12:57:55 -04008795 "rc = 0x%x\n", (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -08008796 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008797 }
James Smart73d91e52011-10-10 21:32:10 -04008798
James Smartda0436e2009-05-22 14:51:39 -04008799 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8800 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
8801 "parent cq-id=%d\n",
8802 phba->sli4_hba.hdr_rq->queue_id,
8803 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04008804 phba->sli4_hba.els_cq->queue_id);
James Smart1ba981f2014-02-20 09:56:45 -05008805
8806 if (phba->cfg_fof) {
8807 rc = lpfc_fof_queue_setup(phba);
8808 if (rc) {
8809 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8810 "0549 Failed setup of FOF Queues: "
8811 "rc = 0x%x\n", rc);
James Smart895427b2017-02-12 13:52:30 -08008812 goto out_destroy;
James Smart1ba981f2014-02-20 09:56:45 -05008813 }
8814 }
James Smart2c9c5a02015-04-07 15:07:15 -04008815
James Smart43140ca2017-03-04 09:30:34 -08008816 for (qidx = 0; qidx < io_channel; qidx += LPFC_MAX_EQ_DELAY_EQID_CNT)
James Smart895427b2017-02-12 13:52:30 -08008817 lpfc_modify_hba_eq_delay(phba, qidx);
James Smart43140ca2017-03-04 09:30:34 -08008818
James Smartda0436e2009-05-22 14:51:39 -04008819 return 0;
8820
James Smart895427b2017-02-12 13:52:30 -08008821out_destroy:
8822 lpfc_sli4_queue_unset(phba);
James Smartda0436e2009-05-22 14:51:39 -04008823out_error:
8824 return rc;
8825}
8826
8827/**
8828 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
8829 * @phba: pointer to lpfc hba data structure.
8830 *
8831 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
8832 * operation.
8833 *
8834 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008835 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008836 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04008837 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04008838 **/
8839void
8840lpfc_sli4_queue_unset(struct lpfc_hba *phba)
8841{
James Smart895427b2017-02-12 13:52:30 -08008842 int qidx;
James Smartda0436e2009-05-22 14:51:39 -04008843
James Smart1ba981f2014-02-20 09:56:45 -05008844 /* Unset the queues created for Flash Optimized Fabric operations */
8845 if (phba->cfg_fof)
8846 lpfc_fof_queue_destroy(phba);
James Smart895427b2017-02-12 13:52:30 -08008847
James Smartda0436e2009-05-22 14:51:39 -04008848 /* Unset mailbox command work queue */
James Smart895427b2017-02-12 13:52:30 -08008849 if (phba->sli4_hba.mbx_wq)
8850 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
8851
8852 /* Unset NVME LS work queue */
8853 if (phba->sli4_hba.nvmels_wq)
8854 lpfc_wq_destroy(phba, phba->sli4_hba.nvmels_wq);
8855
James Smartda0436e2009-05-22 14:51:39 -04008856 /* Unset ELS work queue */
Colin Ian King019c0d62017-05-06 23:13:55 +01008857 if (phba->sli4_hba.els_wq)
James Smart895427b2017-02-12 13:52:30 -08008858 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
8859
James Smartda0436e2009-05-22 14:51:39 -04008860 /* Unset unsolicited receive queue */
James Smart895427b2017-02-12 13:52:30 -08008861 if (phba->sli4_hba.hdr_rq)
8862 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq,
8863 phba->sli4_hba.dat_rq);
8864
James Smartda0436e2009-05-22 14:51:39 -04008865 /* Unset FCP work queue */
James Smart895427b2017-02-12 13:52:30 -08008866 if (phba->sli4_hba.fcp_wq)
8867 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
8868 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[qidx]);
8869
8870 /* Unset NVME work queue */
8871 if (phba->sli4_hba.nvme_wq) {
8872 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
8873 lpfc_wq_destroy(phba, phba->sli4_hba.nvme_wq[qidx]);
James Smart67d12732012-08-03 12:36:13 -04008874 }
James Smart895427b2017-02-12 13:52:30 -08008875
James Smartda0436e2009-05-22 14:51:39 -04008876 /* Unset mailbox command complete queue */
James Smart895427b2017-02-12 13:52:30 -08008877 if (phba->sli4_hba.mbx_cq)
8878 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
8879
James Smartda0436e2009-05-22 14:51:39 -04008880 /* Unset ELS complete queue */
James Smart895427b2017-02-12 13:52:30 -08008881 if (phba->sli4_hba.els_cq)
8882 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
8883
8884 /* Unset NVME LS complete queue */
8885 if (phba->sli4_hba.nvmels_cq)
8886 lpfc_cq_destroy(phba, phba->sli4_hba.nvmels_cq);
8887
8888 /* Unset NVME response complete queue */
8889 if (phba->sli4_hba.nvme_cq)
8890 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
8891 lpfc_cq_destroy(phba, phba->sli4_hba.nvme_cq[qidx]);
8892
James Smart2d7dbc42017-02-12 13:52:35 -08008893 /* Unset NVMET MRQ queue */
8894 if (phba->sli4_hba.nvmet_mrq_hdr) {
8895 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
8896 lpfc_rq_destroy(phba,
8897 phba->sli4_hba.nvmet_mrq_hdr[qidx],
8898 phba->sli4_hba.nvmet_mrq_data[qidx]);
8899 }
8900
8901 /* Unset NVMET CQ Set complete queue */
8902 if (phba->sli4_hba.nvmet_cqset) {
8903 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
8904 lpfc_cq_destroy(phba,
8905 phba->sli4_hba.nvmet_cqset[qidx]);
8906 }
8907
James Smartda0436e2009-05-22 14:51:39 -04008908 /* Unset FCP response complete queue */
James Smart895427b2017-02-12 13:52:30 -08008909 if (phba->sli4_hba.fcp_cq)
8910 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
8911 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[qidx]);
8912
James Smartda0436e2009-05-22 14:51:39 -04008913 /* Unset fast-path event queue */
James Smart895427b2017-02-12 13:52:30 -08008914 if (phba->sli4_hba.hba_eq)
8915 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
8916 lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[qidx]);
James Smartda0436e2009-05-22 14:51:39 -04008917}
8918
8919/**
8920 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
8921 * @phba: pointer to lpfc hba data structure.
8922 *
8923 * This routine is invoked to allocate and set up a pool of completion queue
8924 * events. The body of the completion queue event is a completion queue entry
8925 * CQE. For now, this pool is used for the interrupt service routine to queue
8926 * the following HBA completion queue events for the worker thread to process:
8927 * - Mailbox asynchronous events
8928 * - Receive queue completion unsolicited events
8929 * Later, this can be used for all the slow-path events.
8930 *
8931 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008932 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008933 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04008934 **/
8935static int
8936lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
8937{
8938 struct lpfc_cq_event *cq_event;
8939 int i;
8940
8941 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
8942 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
8943 if (!cq_event)
8944 goto out_pool_create_fail;
8945 list_add_tail(&cq_event->list,
8946 &phba->sli4_hba.sp_cqe_event_pool);
8947 }
8948 return 0;
8949
8950out_pool_create_fail:
8951 lpfc_sli4_cq_event_pool_destroy(phba);
8952 return -ENOMEM;
8953}
8954
8955/**
8956 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
8957 * @phba: pointer to lpfc hba data structure.
8958 *
8959 * This routine is invoked to free the pool of completion queue events at
8960 * driver unload time. Note that, it is the responsibility of the driver
8961 * cleanup routine to free all the outstanding completion-queue events
8962 * allocated from this pool back into the pool before invoking this routine
8963 * to destroy the pool.
8964 **/
8965static void
8966lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
8967{
8968 struct lpfc_cq_event *cq_event, *next_cq_event;
8969
8970 list_for_each_entry_safe(cq_event, next_cq_event,
8971 &phba->sli4_hba.sp_cqe_event_pool, list) {
8972 list_del(&cq_event->list);
8973 kfree(cq_event);
8974 }
8975}
8976
8977/**
8978 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
8979 * @phba: pointer to lpfc hba data structure.
8980 *
8981 * This routine is the lock free version of the API invoked to allocate a
8982 * completion-queue event from the free pool.
8983 *
8984 * Return: Pointer to the newly allocated completion-queue event if successful
8985 * NULL otherwise.
8986 **/
8987struct lpfc_cq_event *
8988__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
8989{
8990 struct lpfc_cq_event *cq_event = NULL;
8991
8992 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
8993 struct lpfc_cq_event, list);
8994 return cq_event;
8995}
8996
8997/**
8998 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
8999 * @phba: pointer to lpfc hba data structure.
9000 *
9001 * This routine is the lock version of the API invoked to allocate a
9002 * completion-queue event from the free pool.
9003 *
9004 * Return: Pointer to the newly allocated completion-queue event if successful
9005 * NULL otherwise.
9006 **/
9007struct lpfc_cq_event *
9008lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
9009{
9010 struct lpfc_cq_event *cq_event;
9011 unsigned long iflags;
9012
9013 spin_lock_irqsave(&phba->hbalock, iflags);
9014 cq_event = __lpfc_sli4_cq_event_alloc(phba);
9015 spin_unlock_irqrestore(&phba->hbalock, iflags);
9016 return cq_event;
9017}
9018
9019/**
9020 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
9021 * @phba: pointer to lpfc hba data structure.
9022 * @cq_event: pointer to the completion queue event to be freed.
9023 *
9024 * This routine is the lock free version of the API invoked to release a
9025 * completion-queue event back into the free pool.
9026 **/
9027void
9028__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
9029 struct lpfc_cq_event *cq_event)
9030{
9031 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
9032}
9033
9034/**
9035 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
9036 * @phba: pointer to lpfc hba data structure.
9037 * @cq_event: pointer to the completion queue event to be freed.
9038 *
9039 * This routine is the lock version of the API invoked to release a
9040 * completion-queue event back into the free pool.
9041 **/
9042void
9043lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
9044 struct lpfc_cq_event *cq_event)
9045{
9046 unsigned long iflags;
9047 spin_lock_irqsave(&phba->hbalock, iflags);
9048 __lpfc_sli4_cq_event_release(phba, cq_event);
9049 spin_unlock_irqrestore(&phba->hbalock, iflags);
9050}
9051
9052/**
9053 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
9054 * @phba: pointer to lpfc hba data structure.
9055 *
9056 * This routine is to free all the pending completion-queue events to the
9057 * back into the free pool for device reset.
9058 **/
9059static void
9060lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
9061{
9062 LIST_HEAD(cqelist);
9063 struct lpfc_cq_event *cqe;
9064 unsigned long iflags;
9065
9066 /* Retrieve all the pending WCQEs from pending WCQE lists */
9067 spin_lock_irqsave(&phba->hbalock, iflags);
9068 /* Pending FCP XRI abort events */
9069 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
9070 &cqelist);
9071 /* Pending ELS XRI abort events */
9072 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
9073 &cqelist);
James Smart318083a2017-03-04 09:30:30 -08009074 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
9075 /* Pending NVME XRI abort events */
9076 list_splice_init(&phba->sli4_hba.sp_nvme_xri_aborted_work_queue,
9077 &cqelist);
9078 }
James Smartda0436e2009-05-22 14:51:39 -04009079 /* Pending asynnc events */
9080 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
9081 &cqelist);
9082 spin_unlock_irqrestore(&phba->hbalock, iflags);
9083
9084 while (!list_empty(&cqelist)) {
9085 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
9086 lpfc_sli4_cq_event_release(phba, cqe);
9087 }
9088}
9089
9090/**
9091 * lpfc_pci_function_reset - Reset pci function.
9092 * @phba: pointer to lpfc hba data structure.
9093 *
9094 * This routine is invoked to request a PCI function reset. It will destroys
9095 * all resources assigned to the PCI function which originates this request.
9096 *
9097 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009098 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009099 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04009100 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04009101 **/
9102int
9103lpfc_pci_function_reset(struct lpfc_hba *phba)
9104{
9105 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05009106 uint32_t rc = 0, if_type;
James Smartda0436e2009-05-22 14:51:39 -04009107 uint32_t shdr_status, shdr_add_status;
James Smart2f6fa2c2014-09-03 12:57:08 -04009108 uint32_t rdy_chk;
9109 uint32_t port_reset = 0;
James Smartda0436e2009-05-22 14:51:39 -04009110 union lpfc_sli4_cfg_shdr *shdr;
James Smart2fcee4b2010-12-15 17:57:46 -05009111 struct lpfc_register reg_data;
James Smart2b81f942012-03-01 22:37:18 -05009112 uint16_t devid;
James Smartda0436e2009-05-22 14:51:39 -04009113
James Smart2fcee4b2010-12-15 17:57:46 -05009114 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9115 switch (if_type) {
9116 case LPFC_SLI_INTF_IF_TYPE_0:
9117 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
9118 GFP_KERNEL);
9119 if (!mboxq) {
9120 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9121 "0494 Unable to allocate memory for "
9122 "issuing SLI_FUNCTION_RESET mailbox "
9123 "command\n");
9124 return -ENOMEM;
9125 }
9126
9127 /* Setup PCI function reset mailbox-ioctl command */
9128 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
9129 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
9130 LPFC_SLI4_MBX_EMBED);
9131 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9132 shdr = (union lpfc_sli4_cfg_shdr *)
9133 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
9134 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9135 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
9136 &shdr->response);
9137 if (rc != MBX_TIMEOUT)
9138 mempool_free(mboxq, phba->mbox_mem_pool);
9139 if (shdr_status || shdr_add_status || rc) {
9140 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9141 "0495 SLI_FUNCTION_RESET mailbox "
9142 "failed with status x%x add_status x%x,"
9143 " mbx status x%x\n",
9144 shdr_status, shdr_add_status, rc);
9145 rc = -ENXIO;
9146 }
9147 break;
9148 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart2f6fa2c2014-09-03 12:57:08 -04009149wait:
9150 /*
9151 * Poll the Port Status Register and wait for RDY for
9152 * up to 30 seconds. If the port doesn't respond, treat
9153 * it as an error.
9154 */
James Smart77d093f2015-04-07 15:07:08 -04009155 for (rdy_chk = 0; rdy_chk < 1500; rdy_chk++) {
James Smart2f6fa2c2014-09-03 12:57:08 -04009156 if (lpfc_readl(phba->sli4_hba.u.if_type2.
9157 STATUSregaddr, &reg_data.word0)) {
9158 rc = -ENODEV;
9159 goto out;
9160 }
9161 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
9162 break;
9163 msleep(20);
9164 }
9165
9166 if (!bf_get(lpfc_sliport_status_rdy, &reg_data)) {
9167 phba->work_status[0] = readl(
9168 phba->sli4_hba.u.if_type2.ERR1regaddr);
9169 phba->work_status[1] = readl(
9170 phba->sli4_hba.u.if_type2.ERR2regaddr);
9171 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9172 "2890 Port not ready, port status reg "
9173 "0x%x error 1=0x%x, error 2=0x%x\n",
9174 reg_data.word0,
9175 phba->work_status[0],
9176 phba->work_status[1]);
9177 rc = -ENODEV;
9178 goto out;
9179 }
9180
9181 if (!port_reset) {
9182 /*
9183 * Reset the port now
9184 */
James Smart2fcee4b2010-12-15 17:57:46 -05009185 reg_data.word0 = 0;
9186 bf_set(lpfc_sliport_ctrl_end, &reg_data,
9187 LPFC_SLIPORT_LITTLE_ENDIAN);
9188 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
9189 LPFC_SLIPORT_INIT_PORT);
9190 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
9191 CTRLregaddr);
James Smart8fcb8ac2012-03-01 22:35:58 -05009192 /* flush */
James Smart2b81f942012-03-01 22:37:18 -05009193 pci_read_config_word(phba->pcidev,
9194 PCI_DEVICE_ID, &devid);
James Smart2fcee4b2010-12-15 17:57:46 -05009195
James Smart2f6fa2c2014-09-03 12:57:08 -04009196 port_reset = 1;
9197 msleep(20);
9198 goto wait;
9199 } else if (bf_get(lpfc_sliport_status_rn, &reg_data)) {
9200 rc = -ENODEV;
9201 goto out;
James Smart2fcee4b2010-12-15 17:57:46 -05009202 }
9203 break;
James Smart2f6fa2c2014-09-03 12:57:08 -04009204
James Smart2fcee4b2010-12-15 17:57:46 -05009205 case LPFC_SLI_INTF_IF_TYPE_1:
9206 default:
9207 break;
James Smartda0436e2009-05-22 14:51:39 -04009208 }
9209
James Smart73d91e52011-10-10 21:32:10 -04009210out:
James Smart2fcee4b2010-12-15 17:57:46 -05009211 /* Catch the not-ready port failure after a port reset. */
James Smart2f6fa2c2014-09-03 12:57:08 -04009212 if (rc) {
James Smart229adb02013-04-17 20:16:51 -04009213 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9214 "3317 HBA not functional: IP Reset Failed "
James Smart2f6fa2c2014-09-03 12:57:08 -04009215 "try: echo fw_reset > board_mode\n");
James Smart2fcee4b2010-12-15 17:57:46 -05009216 rc = -ENODEV;
James Smart229adb02013-04-17 20:16:51 -04009217 }
James Smart2fcee4b2010-12-15 17:57:46 -05009218
James Smartda0436e2009-05-22 14:51:39 -04009219 return rc;
9220}
9221
9222/**
James Smartda0436e2009-05-22 14:51:39 -04009223 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
9224 * @phba: pointer to lpfc hba data structure.
9225 *
9226 * This routine is invoked to set up the PCI device memory space for device
9227 * with SLI-4 interface spec.
9228 *
9229 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009230 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04009231 * other values - error
9232 **/
9233static int
9234lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
9235{
9236 struct pci_dev *pdev;
9237 unsigned long bar0map_len, bar1map_len, bar2map_len;
9238 int error = -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -05009239 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04009240
9241 /* Obtain PCI device reference */
9242 if (!phba->pcidev)
9243 return error;
9244 else
9245 pdev = phba->pcidev;
9246
9247 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05009248 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
9249 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
9250 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
9251 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smartda0436e2009-05-22 14:51:39 -04009252 return error;
Michael Reed8e685972009-09-18 12:02:05 -05009253 }
9254 }
James Smartda0436e2009-05-22 14:51:39 -04009255
James Smart2fcee4b2010-12-15 17:57:46 -05009256 /*
9257 * The BARs and register set definitions and offset locations are
9258 * dependent on the if_type.
9259 */
9260 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
9261 &phba->sli4_hba.sli_intf.word0)) {
9262 return error;
9263 }
9264
9265 /* There is no SLI3 failback for SLI4 devices. */
9266 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
9267 LPFC_SLI_INTF_VALID) {
9268 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9269 "2894 SLI_INTF reg contents invalid "
9270 "sli_intf reg 0x%x\n",
9271 phba->sli4_hba.sli_intf.word0);
9272 return error;
9273 }
9274
9275 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9276 /*
9277 * Get the bus address of SLI4 device Bar regions and the
9278 * number of bytes required by each mapping. The mapping of the
9279 * particular PCI BARs regions is dependent on the type of
9280 * SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -04009281 */
James Smartf5ca6f22013-09-06 12:21:09 -04009282 if (pci_resource_start(pdev, PCI_64BIT_BAR0)) {
9283 phba->pci_bar0_map = pci_resource_start(pdev, PCI_64BIT_BAR0);
9284 bar0map_len = pci_resource_len(pdev, PCI_64BIT_BAR0);
James Smart2fcee4b2010-12-15 17:57:46 -05009285
9286 /*
9287 * Map SLI4 PCI Config Space Register base to a kernel virtual
9288 * addr
9289 */
9290 phba->sli4_hba.conf_regs_memmap_p =
9291 ioremap(phba->pci_bar0_map, bar0map_len);
9292 if (!phba->sli4_hba.conf_regs_memmap_p) {
9293 dev_printk(KERN_ERR, &pdev->dev,
9294 "ioremap failed for SLI4 PCI config "
9295 "registers.\n");
9296 goto out;
9297 }
James Smartf5ca6f22013-09-06 12:21:09 -04009298 phba->pci_bar0_memmap_p = phba->sli4_hba.conf_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05009299 /* Set up BAR0 PCI config space register memory map */
9300 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smart1dfb5a42010-02-12 14:40:50 -05009301 } else {
9302 phba->pci_bar0_map = pci_resource_start(pdev, 1);
9303 bar0map_len = pci_resource_len(pdev, 1);
James Smart2fcee4b2010-12-15 17:57:46 -05009304 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
9305 dev_printk(KERN_ERR, &pdev->dev,
9306 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
9307 goto out;
9308 }
9309 phba->sli4_hba.conf_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04009310 ioremap(phba->pci_bar0_map, bar0map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05009311 if (!phba->sli4_hba.conf_regs_memmap_p) {
9312 dev_printk(KERN_ERR, &pdev->dev,
9313 "ioremap failed for SLI4 PCI config "
9314 "registers.\n");
9315 goto out;
9316 }
9317 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smartda0436e2009-05-22 14:51:39 -04009318 }
9319
James Smartc31098c2011-04-16 11:03:33 -04009320 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
James Smartf5ca6f22013-09-06 12:21:09 -04009321 (pci_resource_start(pdev, PCI_64BIT_BAR2))) {
James Smart2fcee4b2010-12-15 17:57:46 -05009322 /*
9323 * Map SLI4 if type 0 HBA Control Register base to a kernel
9324 * virtual address and setup the registers.
9325 */
James Smartf5ca6f22013-09-06 12:21:09 -04009326 phba->pci_bar1_map = pci_resource_start(pdev, PCI_64BIT_BAR2);
9327 bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
James Smart2fcee4b2010-12-15 17:57:46 -05009328 phba->sli4_hba.ctrl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04009329 ioremap(phba->pci_bar1_map, bar1map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05009330 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
9331 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04009332 "ioremap failed for SLI4 HBA control registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05009333 goto out_iounmap_conf;
9334 }
James Smartf5ca6f22013-09-06 12:21:09 -04009335 phba->pci_bar2_memmap_p = phba->sli4_hba.ctrl_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05009336 lpfc_sli4_bar1_register_memmap(phba);
James Smartda0436e2009-05-22 14:51:39 -04009337 }
9338
James Smartc31098c2011-04-16 11:03:33 -04009339 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
James Smartf5ca6f22013-09-06 12:21:09 -04009340 (pci_resource_start(pdev, PCI_64BIT_BAR4))) {
James Smart2fcee4b2010-12-15 17:57:46 -05009341 /*
9342 * Map SLI4 if type 0 HBA Doorbell Register base to a kernel
9343 * virtual address and setup the registers.
9344 */
James Smartf5ca6f22013-09-06 12:21:09 -04009345 phba->pci_bar2_map = pci_resource_start(pdev, PCI_64BIT_BAR4);
9346 bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
James Smart2fcee4b2010-12-15 17:57:46 -05009347 phba->sli4_hba.drbl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04009348 ioremap(phba->pci_bar2_map, bar2map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05009349 if (!phba->sli4_hba.drbl_regs_memmap_p) {
9350 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04009351 "ioremap failed for SLI4 HBA doorbell registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05009352 goto out_iounmap_ctrl;
9353 }
James Smartf5ca6f22013-09-06 12:21:09 -04009354 phba->pci_bar4_memmap_p = phba->sli4_hba.drbl_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05009355 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
9356 if (error)
9357 goto out_iounmap_all;
James Smartda0436e2009-05-22 14:51:39 -04009358 }
9359
James Smartda0436e2009-05-22 14:51:39 -04009360 return 0;
9361
9362out_iounmap_all:
9363 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
9364out_iounmap_ctrl:
9365 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
9366out_iounmap_conf:
9367 iounmap(phba->sli4_hba.conf_regs_memmap_p);
9368out:
9369 return error;
9370}
9371
9372/**
9373 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
9374 * @phba: pointer to lpfc hba data structure.
9375 *
9376 * This routine is invoked to unset the PCI device memory space for device
9377 * with SLI-4 interface spec.
9378 **/
9379static void
9380lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
9381{
James Smart2e90f4b2011-12-13 13:22:37 -05009382 uint32_t if_type;
9383 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smartda0436e2009-05-22 14:51:39 -04009384
James Smart2e90f4b2011-12-13 13:22:37 -05009385 switch (if_type) {
9386 case LPFC_SLI_INTF_IF_TYPE_0:
9387 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
9388 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
9389 iounmap(phba->sli4_hba.conf_regs_memmap_p);
9390 break;
9391 case LPFC_SLI_INTF_IF_TYPE_2:
9392 iounmap(phba->sli4_hba.conf_regs_memmap_p);
9393 break;
9394 case LPFC_SLI_INTF_IF_TYPE_1:
9395 default:
9396 dev_printk(KERN_ERR, &phba->pcidev->dev,
9397 "FATAL - unsupported SLI4 interface type - %d\n",
9398 if_type);
9399 break;
9400 }
James Smartda0436e2009-05-22 14:51:39 -04009401}
9402
9403/**
James Smart3772a992009-05-22 14:50:54 -04009404 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
9405 * @phba: pointer to lpfc hba data structure.
9406 *
9407 * This routine is invoked to enable the MSI-X interrupt vectors to device
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009408 * with SLI-3 interface specs.
James Smart3772a992009-05-22 14:50:54 -04009409 *
9410 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009411 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04009412 * other values - error
9413 **/
9414static int
9415lpfc_sli_enable_msix(struct lpfc_hba *phba)
9416{
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009417 int rc;
James Smart3772a992009-05-22 14:50:54 -04009418 LPFC_MBOXQ_t *pmb;
9419
9420 /* Set up MSI-X multi-message vectors */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009421 rc = pci_alloc_irq_vectors(phba->pcidev,
9422 LPFC_MSIX_VECTORS, LPFC_MSIX_VECTORS, PCI_IRQ_MSIX);
9423 if (rc < 0) {
James Smart3772a992009-05-22 14:50:54 -04009424 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9425 "0420 PCI enable MSI-X failed (%d)\n", rc);
Alexander Gordeev029165a2014-07-16 20:05:15 +02009426 goto vec_fail_out;
James Smart3772a992009-05-22 14:50:54 -04009427 }
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009428
James Smart3772a992009-05-22 14:50:54 -04009429 /*
9430 * Assign MSI-X vectors to interrupt handlers
9431 */
9432
9433 /* vector-0 is associated to slow-path handler */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009434 rc = request_irq(pci_irq_vector(phba->pcidev, 0),
James Smarted243d32015-05-21 13:55:25 -04009435 &lpfc_sli_sp_intr_handler, 0,
James Smart3772a992009-05-22 14:50:54 -04009436 LPFC_SP_DRIVER_HANDLER_NAME, phba);
9437 if (rc) {
9438 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9439 "0421 MSI-X slow-path request_irq failed "
9440 "(%d)\n", rc);
9441 goto msi_fail_out;
9442 }
9443
9444 /* vector-1 is associated to fast-path handler */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009445 rc = request_irq(pci_irq_vector(phba->pcidev, 1),
James Smarted243d32015-05-21 13:55:25 -04009446 &lpfc_sli_fp_intr_handler, 0,
James Smart3772a992009-05-22 14:50:54 -04009447 LPFC_FP_DRIVER_HANDLER_NAME, phba);
9448
9449 if (rc) {
9450 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9451 "0429 MSI-X fast-path request_irq failed "
9452 "(%d)\n", rc);
9453 goto irq_fail_out;
9454 }
9455
9456 /*
9457 * Configure HBA MSI-X attention conditions to messages
9458 */
9459 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9460
9461 if (!pmb) {
9462 rc = -ENOMEM;
9463 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9464 "0474 Unable to allocate memory for issuing "
9465 "MBOX_CONFIG_MSI command\n");
9466 goto mem_fail_out;
9467 }
9468 rc = lpfc_config_msi(phba, pmb);
9469 if (rc)
9470 goto mbx_fail_out;
9471 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
9472 if (rc != MBX_SUCCESS) {
9473 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
9474 "0351 Config MSI mailbox command failed, "
9475 "mbxCmd x%x, mbxStatus x%x\n",
9476 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
9477 goto mbx_fail_out;
9478 }
9479
9480 /* Free memory allocated for mailbox command */
9481 mempool_free(pmb, phba->mbox_mem_pool);
9482 return rc;
9483
9484mbx_fail_out:
9485 /* Free memory allocated for mailbox command */
9486 mempool_free(pmb, phba->mbox_mem_pool);
9487
9488mem_fail_out:
9489 /* free the irq already requested */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009490 free_irq(pci_irq_vector(phba->pcidev, 1), phba);
James Smart3772a992009-05-22 14:50:54 -04009491
9492irq_fail_out:
9493 /* free the irq already requested */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009494 free_irq(pci_irq_vector(phba->pcidev, 0), phba);
James Smart3772a992009-05-22 14:50:54 -04009495
9496msi_fail_out:
9497 /* Unconfigure MSI-X capability structure */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009498 pci_free_irq_vectors(phba->pcidev);
Alexander Gordeev029165a2014-07-16 20:05:15 +02009499
9500vec_fail_out:
James Smart3772a992009-05-22 14:50:54 -04009501 return rc;
9502}
9503
9504/**
James Smart3772a992009-05-22 14:50:54 -04009505 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
9506 * @phba: pointer to lpfc hba data structure.
9507 *
9508 * This routine is invoked to enable the MSI interrupt mode to device with
9509 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
9510 * enable the MSI vector. The device driver is responsible for calling the
9511 * request_irq() to register MSI vector with a interrupt the handler, which
9512 * is done in this function.
9513 *
9514 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009515 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04009516 * other values - error
9517 */
9518static int
9519lpfc_sli_enable_msi(struct lpfc_hba *phba)
9520{
9521 int rc;
9522
9523 rc = pci_enable_msi(phba->pcidev);
9524 if (!rc)
9525 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9526 "0462 PCI enable MSI mode success.\n");
9527 else {
9528 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9529 "0471 PCI enable MSI mode failed (%d)\n", rc);
9530 return rc;
9531 }
9532
9533 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
James Smarted243d32015-05-21 13:55:25 -04009534 0, LPFC_DRIVER_NAME, phba);
James Smart3772a992009-05-22 14:50:54 -04009535 if (rc) {
9536 pci_disable_msi(phba->pcidev);
9537 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9538 "0478 MSI request_irq failed (%d)\n", rc);
9539 }
9540 return rc;
9541}
9542
9543/**
James Smart3772a992009-05-22 14:50:54 -04009544 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
9545 * @phba: pointer to lpfc hba data structure.
9546 *
9547 * This routine is invoked to enable device interrupt and associate driver's
9548 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
9549 * spec. Depends on the interrupt mode configured to the driver, the driver
9550 * will try to fallback from the configured interrupt mode to an interrupt
9551 * mode which is supported by the platform, kernel, and device in the order
9552 * of:
9553 * MSI-X -> MSI -> IRQ.
9554 *
9555 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009556 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04009557 * other values - error
9558 **/
9559static uint32_t
9560lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
9561{
9562 uint32_t intr_mode = LPFC_INTR_ERROR;
9563 int retval;
9564
9565 if (cfg_mode == 2) {
9566 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
9567 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
9568 if (!retval) {
9569 /* Now, try to enable MSI-X interrupt mode */
9570 retval = lpfc_sli_enable_msix(phba);
9571 if (!retval) {
9572 /* Indicate initialization to MSI-X mode */
9573 phba->intr_type = MSIX;
9574 intr_mode = 2;
9575 }
9576 }
9577 }
9578
9579 /* Fallback to MSI if MSI-X initialization failed */
9580 if (cfg_mode >= 1 && phba->intr_type == NONE) {
9581 retval = lpfc_sli_enable_msi(phba);
9582 if (!retval) {
9583 /* Indicate initialization to MSI mode */
9584 phba->intr_type = MSI;
9585 intr_mode = 1;
9586 }
9587 }
9588
9589 /* Fallback to INTx if both MSI-X/MSI initalization failed */
9590 if (phba->intr_type == NONE) {
9591 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
9592 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
9593 if (!retval) {
9594 /* Indicate initialization to INTx mode */
9595 phba->intr_type = INTx;
9596 intr_mode = 0;
9597 }
9598 }
9599 return intr_mode;
9600}
9601
9602/**
9603 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
9604 * @phba: pointer to lpfc hba data structure.
9605 *
9606 * This routine is invoked to disable device interrupt and disassociate the
9607 * driver's interrupt handler(s) from interrupt vector(s) to device with
9608 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
9609 * release the interrupt vector(s) for the message signaled interrupt.
9610 **/
9611static void
9612lpfc_sli_disable_intr(struct lpfc_hba *phba)
9613{
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009614 int nr_irqs, i;
9615
James Smart3772a992009-05-22 14:50:54 -04009616 if (phba->intr_type == MSIX)
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009617 nr_irqs = LPFC_MSIX_VECTORS;
9618 else
9619 nr_irqs = 1;
9620
9621 for (i = 0; i < nr_irqs; i++)
9622 free_irq(pci_irq_vector(phba->pcidev, i), phba);
9623 pci_free_irq_vectors(phba->pcidev);
James Smart3772a992009-05-22 14:50:54 -04009624
9625 /* Reset interrupt management states */
9626 phba->intr_type = NONE;
9627 phba->sli.slistat.sli_intr = 0;
James Smart3772a992009-05-22 14:50:54 -04009628}
9629
9630/**
James Smart895427b2017-02-12 13:52:30 -08009631 * lpfc_cpu_affinity_check - Check vector CPU affinity mappings
James Smart7bb03bb2013-04-17 20:19:16 -04009632 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -08009633 * @vectors: number of msix vectors allocated.
James Smart7bb03bb2013-04-17 20:19:16 -04009634 *
James Smart895427b2017-02-12 13:52:30 -08009635 * The routine will figure out the CPU affinity assignment for every
9636 * MSI-X vector allocated for the HBA. The hba_eq_hdl will be updated
9637 * with a pointer to the CPU mask that defines ALL the CPUs this vector
9638 * can be associated with. If the vector can be unquely associated with
9639 * a single CPU, that CPU will be recorded in hba_eq_hdl[index].cpu.
9640 * In addition, the CPU to IO channel mapping will be calculated
9641 * and the phba->sli4_hba.cpu_map array will reflect this.
James Smart7bb03bb2013-04-17 20:19:16 -04009642 */
James Smart895427b2017-02-12 13:52:30 -08009643static void
9644lpfc_cpu_affinity_check(struct lpfc_hba *phba, int vectors)
James Smart7bb03bb2013-04-17 20:19:16 -04009645{
9646 struct lpfc_vector_map_info *cpup;
James Smart895427b2017-02-12 13:52:30 -08009647 int index = 0;
9648 int vec = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04009649 int cpu;
James Smart7bb03bb2013-04-17 20:19:16 -04009650#ifdef CONFIG_X86
9651 struct cpuinfo_x86 *cpuinfo;
9652#endif
James Smart7bb03bb2013-04-17 20:19:16 -04009653
9654 /* Init cpu_map array */
9655 memset(phba->sli4_hba.cpu_map, 0xff,
9656 (sizeof(struct lpfc_vector_map_info) *
James Smart895427b2017-02-12 13:52:30 -08009657 phba->sli4_hba.num_present_cpu));
James Smart7bb03bb2013-04-17 20:19:16 -04009658
9659 /* Update CPU map with physical id and core id of each CPU */
9660 cpup = phba->sli4_hba.cpu_map;
9661 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
9662#ifdef CONFIG_X86
9663 cpuinfo = &cpu_data(cpu);
9664 cpup->phys_id = cpuinfo->phys_proc_id;
9665 cpup->core_id = cpuinfo->cpu_core_id;
9666#else
9667 /* No distinction between CPUs for other platforms */
9668 cpup->phys_id = 0;
9669 cpup->core_id = 0;
9670#endif
James Smart895427b2017-02-12 13:52:30 -08009671 cpup->channel_id = index; /* For now round robin */
9672 cpup->irq = pci_irq_vector(phba->pcidev, vec);
9673 vec++;
9674 if (vec >= vectors)
9675 vec = 0;
9676 index++;
9677 if (index >= phba->cfg_fcp_io_channel)
9678 index = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04009679 cpup++;
9680 }
James Smart7bb03bb2013-04-17 20:19:16 -04009681}
9682
9683
9684/**
James Smartda0436e2009-05-22 14:51:39 -04009685 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
9686 * @phba: pointer to lpfc hba data structure.
9687 *
9688 * This routine is invoked to enable the MSI-X interrupt vectors to device
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009689 * with SLI-4 interface spec.
James Smartda0436e2009-05-22 14:51:39 -04009690 *
9691 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009692 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04009693 * other values - error
9694 **/
9695static int
9696lpfc_sli4_enable_msix(struct lpfc_hba *phba)
9697{
James Smart75baf692010-06-08 18:31:21 -04009698 int vectors, rc, index;
James Smartda0436e2009-05-22 14:51:39 -04009699
9700 /* Set up MSI-X multi-message vectors */
James Smart895427b2017-02-12 13:52:30 -08009701 vectors = phba->io_channel_irqs;
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009702 if (phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05009703 vectors++;
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009704
James Smartf358dd02017-02-12 13:52:34 -08009705 rc = pci_alloc_irq_vectors(phba->pcidev,
9706 (phba->nvmet_support) ? 1 : 2,
9707 vectors, PCI_IRQ_MSIX | PCI_IRQ_AFFINITY);
Alexander Gordeev4f871e12014-09-03 12:56:29 -04009708 if (rc < 0) {
James Smartda0436e2009-05-22 14:51:39 -04009709 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9710 "0484 PCI enable MSI-X failed (%d)\n", rc);
Alexander Gordeev029165a2014-07-16 20:05:15 +02009711 goto vec_fail_out;
James Smartda0436e2009-05-22 14:51:39 -04009712 }
Alexander Gordeev4f871e12014-09-03 12:56:29 -04009713 vectors = rc;
James Smart75baf692010-06-08 18:31:21 -04009714
James Smart7bb03bb2013-04-17 20:19:16 -04009715 /* Assign MSI-X vectors to interrupt handlers */
James Smart67d12732012-08-03 12:36:13 -04009716 for (index = 0; index < vectors; index++) {
James Smart4305f182012-08-03 12:36:33 -04009717 memset(&phba->sli4_hba.handler_name[index], 0, 16);
James Smarta2fc4aef2014-09-03 12:57:55 -04009718 snprintf((char *)&phba->sli4_hba.handler_name[index],
9719 LPFC_SLI4_HANDLER_NAME_SZ,
James Smart4305f182012-08-03 12:36:33 -04009720 LPFC_DRIVER_HANDLER_NAME"%d", index);
James Smartda0436e2009-05-22 14:51:39 -04009721
James Smart895427b2017-02-12 13:52:30 -08009722 phba->sli4_hba.hba_eq_hdl[index].idx = index;
9723 phba->sli4_hba.hba_eq_hdl[index].phba = phba;
9724 atomic_set(&phba->sli4_hba.hba_eq_hdl[index].hba_eq_in_use, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009725 if (phba->cfg_fof && (index == (vectors - 1)))
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009726 rc = request_irq(pci_irq_vector(phba->pcidev, index),
James Smarted243d32015-05-21 13:55:25 -04009727 &lpfc_sli4_fof_intr_handler, 0,
James Smart1ba981f2014-02-20 09:56:45 -05009728 (char *)&phba->sli4_hba.handler_name[index],
James Smart895427b2017-02-12 13:52:30 -08009729 &phba->sli4_hba.hba_eq_hdl[index]);
James Smart1ba981f2014-02-20 09:56:45 -05009730 else
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009731 rc = request_irq(pci_irq_vector(phba->pcidev, index),
James Smarted243d32015-05-21 13:55:25 -04009732 &lpfc_sli4_hba_intr_handler, 0,
James Smart4305f182012-08-03 12:36:33 -04009733 (char *)&phba->sli4_hba.handler_name[index],
James Smart895427b2017-02-12 13:52:30 -08009734 &phba->sli4_hba.hba_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -04009735 if (rc) {
9736 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9737 "0486 MSI-X fast-path (%d) "
9738 "request_irq failed (%d)\n", index, rc);
9739 goto cfg_fail_out;
9740 }
9741 }
9742
James Smart1ba981f2014-02-20 09:56:45 -05009743 if (phba->cfg_fof)
9744 vectors--;
9745
James Smart895427b2017-02-12 13:52:30 -08009746 if (vectors != phba->io_channel_irqs) {
James Smart82c3e9b2012-09-29 11:29:50 -04009747 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9748 "3238 Reducing IO channels to match number of "
9749 "MSI-X vectors, requested %d got %d\n",
James Smart895427b2017-02-12 13:52:30 -08009750 phba->io_channel_irqs, vectors);
9751 if (phba->cfg_fcp_io_channel > vectors)
9752 phba->cfg_fcp_io_channel = vectors;
9753 if (phba->cfg_nvme_io_channel > vectors)
9754 phba->cfg_nvme_io_channel = vectors;
9755 if (phba->cfg_fcp_io_channel > phba->cfg_nvme_io_channel)
9756 phba->io_channel_irqs = phba->cfg_fcp_io_channel;
9757 else
9758 phba->io_channel_irqs = phba->cfg_nvme_io_channel;
James Smart82c3e9b2012-09-29 11:29:50 -04009759 }
James Smart895427b2017-02-12 13:52:30 -08009760 lpfc_cpu_affinity_check(phba, vectors);
James Smart7bb03bb2013-04-17 20:19:16 -04009761
James Smartda0436e2009-05-22 14:51:39 -04009762 return rc;
9763
9764cfg_fail_out:
9765 /* free the irq already requested */
James Smart895427b2017-02-12 13:52:30 -08009766 for (--index; index >= 0; index--)
9767 free_irq(pci_irq_vector(phba->pcidev, index),
9768 &phba->sli4_hba.hba_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -04009769
James Smartda0436e2009-05-22 14:51:39 -04009770 /* Unconfigure MSI-X capability structure */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009771 pci_free_irq_vectors(phba->pcidev);
Alexander Gordeev029165a2014-07-16 20:05:15 +02009772
9773vec_fail_out:
James Smartda0436e2009-05-22 14:51:39 -04009774 return rc;
9775}
9776
9777/**
James Smartda0436e2009-05-22 14:51:39 -04009778 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
9779 * @phba: pointer to lpfc hba data structure.
9780 *
9781 * This routine is invoked to enable the MSI interrupt mode to device with
9782 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
9783 * to enable the MSI vector. The device driver is responsible for calling
9784 * the request_irq() to register MSI vector with a interrupt the handler,
9785 * which is done in this function.
9786 *
9787 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009788 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04009789 * other values - error
9790 **/
9791static int
9792lpfc_sli4_enable_msi(struct lpfc_hba *phba)
9793{
9794 int rc, index;
9795
9796 rc = pci_enable_msi(phba->pcidev);
9797 if (!rc)
9798 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9799 "0487 PCI enable MSI mode success.\n");
9800 else {
9801 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9802 "0488 PCI enable MSI mode failed (%d)\n", rc);
9803 return rc;
9804 }
9805
9806 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
James Smarted243d32015-05-21 13:55:25 -04009807 0, LPFC_DRIVER_NAME, phba);
James Smartda0436e2009-05-22 14:51:39 -04009808 if (rc) {
9809 pci_disable_msi(phba->pcidev);
9810 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9811 "0490 MSI request_irq failed (%d)\n", rc);
James Smart75baf692010-06-08 18:31:21 -04009812 return rc;
James Smartda0436e2009-05-22 14:51:39 -04009813 }
9814
James Smart895427b2017-02-12 13:52:30 -08009815 for (index = 0; index < phba->io_channel_irqs; index++) {
9816 phba->sli4_hba.hba_eq_hdl[index].idx = index;
9817 phba->sli4_hba.hba_eq_hdl[index].phba = phba;
James Smartda0436e2009-05-22 14:51:39 -04009818 }
9819
James Smart1ba981f2014-02-20 09:56:45 -05009820 if (phba->cfg_fof) {
James Smart895427b2017-02-12 13:52:30 -08009821 phba->sli4_hba.hba_eq_hdl[index].idx = index;
9822 phba->sli4_hba.hba_eq_hdl[index].phba = phba;
James Smart1ba981f2014-02-20 09:56:45 -05009823 }
James Smart75baf692010-06-08 18:31:21 -04009824 return 0;
James Smartda0436e2009-05-22 14:51:39 -04009825}
9826
9827/**
James Smartda0436e2009-05-22 14:51:39 -04009828 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
9829 * @phba: pointer to lpfc hba data structure.
9830 *
9831 * This routine is invoked to enable device interrupt and associate driver's
9832 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
9833 * interface spec. Depends on the interrupt mode configured to the driver,
9834 * the driver will try to fallback from the configured interrupt mode to an
9835 * interrupt mode which is supported by the platform, kernel, and device in
9836 * the order of:
9837 * MSI-X -> MSI -> IRQ.
9838 *
9839 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009840 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04009841 * other values - error
9842 **/
9843static uint32_t
9844lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
9845{
9846 uint32_t intr_mode = LPFC_INTR_ERROR;
James Smart895427b2017-02-12 13:52:30 -08009847 int retval, idx;
James Smartda0436e2009-05-22 14:51:39 -04009848
9849 if (cfg_mode == 2) {
9850 /* Preparation before conf_msi mbox cmd */
9851 retval = 0;
9852 if (!retval) {
9853 /* Now, try to enable MSI-X interrupt mode */
9854 retval = lpfc_sli4_enable_msix(phba);
9855 if (!retval) {
9856 /* Indicate initialization to MSI-X mode */
9857 phba->intr_type = MSIX;
9858 intr_mode = 2;
9859 }
9860 }
9861 }
9862
9863 /* Fallback to MSI if MSI-X initialization failed */
9864 if (cfg_mode >= 1 && phba->intr_type == NONE) {
9865 retval = lpfc_sli4_enable_msi(phba);
9866 if (!retval) {
9867 /* Indicate initialization to MSI mode */
9868 phba->intr_type = MSI;
9869 intr_mode = 1;
9870 }
9871 }
9872
9873 /* Fallback to INTx if both MSI-X/MSI initalization failed */
9874 if (phba->intr_type == NONE) {
9875 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
9876 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
9877 if (!retval) {
James Smart895427b2017-02-12 13:52:30 -08009878 struct lpfc_hba_eq_hdl *eqhdl;
9879
James Smartda0436e2009-05-22 14:51:39 -04009880 /* Indicate initialization to INTx mode */
9881 phba->intr_type = INTx;
9882 intr_mode = 0;
James Smart895427b2017-02-12 13:52:30 -08009883
9884 for (idx = 0; idx < phba->io_channel_irqs; idx++) {
9885 eqhdl = &phba->sli4_hba.hba_eq_hdl[idx];
9886 eqhdl->idx = idx;
9887 eqhdl->phba = phba;
9888 atomic_set(&eqhdl->hba_eq_in_use, 1);
James Smartda0436e2009-05-22 14:51:39 -04009889 }
James Smart1ba981f2014-02-20 09:56:45 -05009890 if (phba->cfg_fof) {
James Smart895427b2017-02-12 13:52:30 -08009891 eqhdl = &phba->sli4_hba.hba_eq_hdl[idx];
9892 eqhdl->idx = idx;
9893 eqhdl->phba = phba;
9894 atomic_set(&eqhdl->hba_eq_in_use, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009895 }
James Smartda0436e2009-05-22 14:51:39 -04009896 }
9897 }
9898 return intr_mode;
9899}
9900
9901/**
9902 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
9903 * @phba: pointer to lpfc hba data structure.
9904 *
9905 * This routine is invoked to disable device interrupt and disassociate
9906 * the driver's interrupt handler(s) from interrupt vector(s) to device
9907 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
9908 * will release the interrupt vector(s) for the message signaled interrupt.
9909 **/
9910static void
9911lpfc_sli4_disable_intr(struct lpfc_hba *phba)
9912{
9913 /* Disable the currently initialized interrupt mode */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009914 if (phba->intr_type == MSIX) {
9915 int index;
9916
9917 /* Free up MSI-X multi-message vectors */
James Smart895427b2017-02-12 13:52:30 -08009918 for (index = 0; index < phba->io_channel_irqs; index++)
9919 free_irq(pci_irq_vector(phba->pcidev, index),
9920 &phba->sli4_hba.hba_eq_hdl[index]);
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009921
9922 if (phba->cfg_fof)
James Smart895427b2017-02-12 13:52:30 -08009923 free_irq(pci_irq_vector(phba->pcidev, index),
9924 &phba->sli4_hba.hba_eq_hdl[index]);
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009925 } else {
James Smartda0436e2009-05-22 14:51:39 -04009926 free_irq(phba->pcidev->irq, phba);
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009927 }
9928
9929 pci_free_irq_vectors(phba->pcidev);
James Smartda0436e2009-05-22 14:51:39 -04009930
9931 /* Reset interrupt management states */
9932 phba->intr_type = NONE;
9933 phba->sli.slistat.sli_intr = 0;
James Smartda0436e2009-05-22 14:51:39 -04009934}
9935
9936/**
James Smart3772a992009-05-22 14:50:54 -04009937 * lpfc_unset_hba - Unset SLI3 hba device initialization
9938 * @phba: pointer to lpfc hba data structure.
9939 *
9940 * This routine is invoked to unset the HBA device initialization steps to
9941 * a device with SLI-3 interface spec.
9942 **/
9943static void
9944lpfc_unset_hba(struct lpfc_hba *phba)
9945{
9946 struct lpfc_vport *vport = phba->pport;
9947 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
9948
9949 spin_lock_irq(shost->host_lock);
9950 vport->load_flag |= FC_UNLOADING;
9951 spin_unlock_irq(shost->host_lock);
9952
James Smart72859902012-01-18 16:25:38 -05009953 kfree(phba->vpi_bmask);
9954 kfree(phba->vpi_ids);
9955
James Smart3772a992009-05-22 14:50:54 -04009956 lpfc_stop_hba_timers(phba);
9957
9958 phba->pport->work_port_events = 0;
9959
9960 lpfc_sli_hba_down(phba);
9961
9962 lpfc_sli_brdrestart(phba);
9963
9964 lpfc_sli_disable_intr(phba);
9965
9966 return;
9967}
9968
9969/**
James Smart5af5eee2010-10-22 11:06:38 -04009970 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
9971 * @phba: Pointer to HBA context object.
9972 *
9973 * This function is called in the SLI4 code path to wait for completion
9974 * of device's XRIs exchange busy. It will check the XRI exchange busy
9975 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
9976 * that, it will check the XRI exchange busy on outstanding FCP and ELS
9977 * I/Os every 30 seconds, log error message, and wait forever. Only when
9978 * all XRI exchange busy complete, the driver unload shall proceed with
9979 * invoking the function reset ioctl mailbox command to the CNA and the
9980 * the rest of the driver unload resource release.
9981 **/
9982static void
9983lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
9984{
9985 int wait_time = 0;
James Smart895427b2017-02-12 13:52:30 -08009986 int nvme_xri_cmpl = 1;
James Smart86c67372017-04-21 16:05:04 -07009987 int nvmet_xri_cmpl = 1;
James Smart895427b2017-02-12 13:52:30 -08009988 int fcp_xri_cmpl = 1;
James Smart5af5eee2010-10-22 11:06:38 -04009989 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
9990
James Smart895427b2017-02-12 13:52:30 -08009991 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP)
9992 fcp_xri_cmpl =
9993 list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
James Smart86c67372017-04-21 16:05:04 -07009994 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart895427b2017-02-12 13:52:30 -08009995 nvme_xri_cmpl =
9996 list_empty(&phba->sli4_hba.lpfc_abts_nvme_buf_list);
James Smart86c67372017-04-21 16:05:04 -07009997 nvmet_xri_cmpl =
9998 list_empty(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
9999 }
James Smart895427b2017-02-12 13:52:30 -080010000
James Smartf358dd02017-02-12 13:52:34 -080010001 while (!fcp_xri_cmpl || !els_xri_cmpl || !nvme_xri_cmpl ||
10002 !nvmet_xri_cmpl) {
James Smart5af5eee2010-10-22 11:06:38 -040010003 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
James Smart895427b2017-02-12 13:52:30 -080010004 if (!nvme_xri_cmpl)
10005 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10006 "6100 NVME XRI exchange busy "
10007 "wait time: %d seconds.\n",
10008 wait_time/1000);
James Smart5af5eee2010-10-22 11:06:38 -040010009 if (!fcp_xri_cmpl)
10010 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10011 "2877 FCP XRI exchange busy "
10012 "wait time: %d seconds.\n",
10013 wait_time/1000);
10014 if (!els_xri_cmpl)
10015 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10016 "2878 ELS XRI exchange busy "
10017 "wait time: %d seconds.\n",
10018 wait_time/1000);
10019 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
10020 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
10021 } else {
10022 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
10023 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
10024 }
James Smart86c67372017-04-21 16:05:04 -070010025 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart895427b2017-02-12 13:52:30 -080010026 nvme_xri_cmpl = list_empty(
10027 &phba->sli4_hba.lpfc_abts_nvme_buf_list);
James Smart86c67372017-04-21 16:05:04 -070010028 nvmet_xri_cmpl = list_empty(
10029 &phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
10030 }
James Smart895427b2017-02-12 13:52:30 -080010031
10032 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP)
10033 fcp_xri_cmpl = list_empty(
10034 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
10035
James Smart5af5eee2010-10-22 11:06:38 -040010036 els_xri_cmpl =
10037 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smartf358dd02017-02-12 13:52:34 -080010038
James Smart5af5eee2010-10-22 11:06:38 -040010039 }
10040}
10041
10042/**
James Smartda0436e2009-05-22 14:51:39 -040010043 * lpfc_sli4_hba_unset - Unset the fcoe hba
10044 * @phba: Pointer to HBA context object.
10045 *
10046 * This function is called in the SLI4 code path to reset the HBA's FCoE
10047 * function. The caller is not required to hold any lock. This routine
10048 * issues PCI function reset mailbox command to reset the FCoE function.
10049 * At the end of the function, it calls lpfc_hba_down_post function to
10050 * free any pending commands.
10051 **/
10052static void
10053lpfc_sli4_hba_unset(struct lpfc_hba *phba)
10054{
10055 int wait_cnt = 0;
10056 LPFC_MBOXQ_t *mboxq;
James Smart912e3ac2011-05-24 11:42:11 -040010057 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -040010058
10059 lpfc_stop_hba_timers(phba);
10060 phba->sli4_hba.intr_enable = 0;
10061
10062 /*
10063 * Gracefully wait out the potential current outstanding asynchronous
10064 * mailbox command.
10065 */
10066
10067 /* First, block any pending async mailbox command from posted */
10068 spin_lock_irq(&phba->hbalock);
10069 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
10070 spin_unlock_irq(&phba->hbalock);
10071 /* Now, trying to wait it out if we can */
10072 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
10073 msleep(10);
10074 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
10075 break;
10076 }
10077 /* Forcefully release the outstanding mailbox command if timed out */
10078 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
10079 spin_lock_irq(&phba->hbalock);
10080 mboxq = phba->sli.mbox_active;
10081 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
10082 __lpfc_mbox_cmpl_put(phba, mboxq);
10083 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10084 phba->sli.mbox_active = NULL;
10085 spin_unlock_irq(&phba->hbalock);
10086 }
10087
James Smart5af5eee2010-10-22 11:06:38 -040010088 /* Abort all iocbs associated with the hba */
10089 lpfc_sli_hba_iocb_abort(phba);
10090
10091 /* Wait for completion of device XRI exchange busy */
10092 lpfc_sli4_xri_exchange_busy_wait(phba);
10093
James Smartda0436e2009-05-22 14:51:39 -040010094 /* Disable PCI subsystem interrupt */
10095 lpfc_sli4_disable_intr(phba);
10096
James Smart912e3ac2011-05-24 11:42:11 -040010097 /* Disable SR-IOV if enabled */
10098 if (phba->cfg_sriov_nr_virtfn)
10099 pci_disable_sriov(pdev);
10100
James Smartda0436e2009-05-22 14:51:39 -040010101 /* Stop kthread signal shall trigger work_done one more time */
10102 kthread_stop(phba->worker_thread);
10103
James Smartd1f525a2017-04-21 16:04:55 -070010104 /* Unset the queues shared with the hardware then release all
10105 * allocated resources.
10106 */
10107 lpfc_sli4_queue_unset(phba);
10108 lpfc_sli4_queue_destroy(phba);
10109
James Smart3677a3a2010-09-29 11:19:14 -040010110 /* Reset SLI4 HBA FCoE function */
10111 lpfc_pci_function_reset(phba);
10112
James Smartda0436e2009-05-22 14:51:39 -040010113 /* Stop the SLI4 device port */
10114 phba->pport->work_port_events = 0;
10115}
10116
James Smart28baac72010-02-12 14:42:03 -050010117 /**
10118 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
10119 * @phba: Pointer to HBA context object.
10120 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
10121 *
10122 * This function is called in the SLI4 code path to read the port's
10123 * sli4 capabilities.
10124 *
10125 * This function may be be called from any context that can block-wait
10126 * for the completion. The expectation is that this routine is called
10127 * typically from probe_one or from the online routine.
10128 **/
10129int
10130lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
10131{
10132 int rc;
10133 struct lpfc_mqe *mqe;
10134 struct lpfc_pc_sli4_params *sli4_params;
10135 uint32_t mbox_tmo;
10136
10137 rc = 0;
10138 mqe = &mboxq->u.mqe;
10139
10140 /* Read the port's SLI4 Parameters port capabilities */
James Smartfedd3b72011-02-16 12:39:24 -050010141 lpfc_pc_sli4_params(mboxq);
James Smart28baac72010-02-12 14:42:03 -050010142 if (!phba->sli4_hba.intr_enable)
10143 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
10144 else {
James Smarta183a152011-10-10 21:32:43 -040010145 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart28baac72010-02-12 14:42:03 -050010146 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
10147 }
10148
10149 if (unlikely(rc))
10150 return 1;
10151
10152 sli4_params = &phba->sli4_hba.pc_sli4_params;
10153 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
10154 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
10155 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
10156 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
10157 &mqe->un.sli4_params);
10158 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
10159 &mqe->un.sli4_params);
10160 sli4_params->proto_types = mqe->un.sli4_params.word3;
10161 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
10162 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
10163 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
10164 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
10165 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
10166 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
10167 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
10168 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
10169 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
10170 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
10171 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
10172 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
10173 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
10174 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
10175 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
10176 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
10177 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
10178 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
10179 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
10180 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
James Smart05580562011-05-24 11:40:48 -040010181
10182 /* Make sure that sge_supp_len can be handled by the driver */
10183 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
10184 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
10185
James Smart28baac72010-02-12 14:42:03 -050010186 return rc;
10187}
10188
James Smartda0436e2009-05-22 14:51:39 -040010189/**
James Smartfedd3b72011-02-16 12:39:24 -050010190 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
10191 * @phba: Pointer to HBA context object.
10192 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
10193 *
10194 * This function is called in the SLI4 code path to read the port's
10195 * sli4 capabilities.
10196 *
10197 * This function may be be called from any context that can block-wait
10198 * for the completion. The expectation is that this routine is called
10199 * typically from probe_one or from the online routine.
10200 **/
10201int
10202lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
10203{
10204 int rc;
10205 struct lpfc_mqe *mqe = &mboxq->u.mqe;
10206 struct lpfc_pc_sli4_params *sli4_params;
James Smarta183a152011-10-10 21:32:43 -040010207 uint32_t mbox_tmo;
James Smartfedd3b72011-02-16 12:39:24 -050010208 int length;
10209 struct lpfc_sli4_parameters *mbx_sli4_parameters;
10210
James Smart6d368e52011-05-24 11:44:12 -040010211 /*
10212 * By default, the driver assumes the SLI4 port requires RPI
10213 * header postings. The SLI4_PARAM response will correct this
10214 * assumption.
10215 */
10216 phba->sli4_hba.rpi_hdrs_in_use = 1;
10217
James Smartfedd3b72011-02-16 12:39:24 -050010218 /* Read the port's SLI4 Config Parameters */
10219 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
10220 sizeof(struct lpfc_sli4_cfg_mhdr));
10221 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
10222 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
10223 length, LPFC_SLI4_MBX_EMBED);
10224 if (!phba->sli4_hba.intr_enable)
10225 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -040010226 else {
10227 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
10228 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
10229 }
James Smartfedd3b72011-02-16 12:39:24 -050010230 if (unlikely(rc))
10231 return rc;
10232 sli4_params = &phba->sli4_hba.pc_sli4_params;
10233 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
10234 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
10235 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
10236 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
10237 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
10238 mbx_sli4_parameters);
10239 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
10240 mbx_sli4_parameters);
10241 if (bf_get(cfg_phwq, mbx_sli4_parameters))
10242 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
10243 else
10244 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
10245 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
10246 sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
James Smart1ba981f2014-02-20 09:56:45 -050010247 sli4_params->oas_supported = bf_get(cfg_oas, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -050010248 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
10249 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
10250 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
10251 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
James Smart0c651872013-07-15 18:33:23 -040010252 sli4_params->wqsize = bf_get(cfg_wqsize, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -050010253 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
10254 mbx_sli4_parameters);
James Smart895427b2017-02-12 13:52:30 -080010255 sli4_params->wqpcnt = bf_get(cfg_wqpcnt, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -050010256 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
10257 mbx_sli4_parameters);
James Smart6d368e52011-05-24 11:44:12 -040010258 phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
10259 phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
James Smart895427b2017-02-12 13:52:30 -080010260 phba->nvme_support = (bf_get(cfg_nvme, mbx_sli4_parameters) &&
10261 bf_get(cfg_xib, mbx_sli4_parameters));
10262
10263 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP) ||
10264 !phba->nvme_support) {
10265 phba->nvme_support = 0;
10266 phba->nvmet_support = 0;
James Smart2d7dbc42017-02-12 13:52:35 -080010267 phba->cfg_nvmet_mrq = 0;
James Smart895427b2017-02-12 13:52:30 -080010268 phba->cfg_nvme_io_channel = 0;
10269 phba->io_channel_irqs = phba->cfg_fcp_io_channel;
10270 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_NVME,
10271 "6101 Disabling NVME support: "
10272 "Not supported by firmware: %d %d\n",
10273 bf_get(cfg_nvme, mbx_sli4_parameters),
10274 bf_get(cfg_xib, mbx_sli4_parameters));
10275
10276 /* If firmware doesn't support NVME, just use SCSI support */
10277 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
10278 return -ENODEV;
10279 phba->cfg_enable_fc4_type = LPFC_ENABLE_FCP;
10280 }
James Smart05580562011-05-24 11:40:48 -040010281
James Smartf358dd02017-02-12 13:52:34 -080010282 if (bf_get(cfg_xib, mbx_sli4_parameters) && phba->cfg_suppress_rsp)
10283 phba->sli.sli_flag |= LPFC_SLI_SUPPRESS_RSP;
10284
James Smart05580562011-05-24 11:40:48 -040010285 /* Make sure that sge_supp_len can be handled by the driver */
10286 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
10287 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
10288
James Smartb5c53952016-03-31 14:12:30 -070010289 /*
10290 * Issue IOs with CDB embedded in WQE to minimized the number
10291 * of DMAs the firmware has to do. Setting this to 1 also forces
10292 * the driver to use 128 bytes WQEs for FCP IOs.
10293 */
10294 if (bf_get(cfg_ext_embed_cb, mbx_sli4_parameters))
10295 phba->fcp_embed_io = 1;
10296 else
10297 phba->fcp_embed_io = 0;
James Smart7bdedb32016-07-06 12:36:00 -070010298
10299 /*
10300 * Check if the SLI port supports MDS Diagnostics
10301 */
10302 if (bf_get(cfg_mds_diags, mbx_sli4_parameters))
10303 phba->mds_diags_support = 1;
10304 else
10305 phba->mds_diags_support = 0;
James Smartfedd3b72011-02-16 12:39:24 -050010306 return 0;
10307}
10308
10309/**
James Smart3772a992009-05-22 14:50:54 -040010310 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
10311 * @pdev: pointer to PCI device
10312 * @pid: pointer to PCI device identifier
10313 *
10314 * This routine is to be called to attach a device with SLI-3 interface spec
10315 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
10316 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
10317 * information of the device and driver to see if the driver state that it can
10318 * support this kind of device. If the match is successful, the driver core
10319 * invokes this routine. If this routine determines it can claim the HBA, it
10320 * does all the initialization that it needs to do to handle the HBA properly.
10321 *
10322 * Return code
10323 * 0 - driver can claim the device
10324 * negative value - driver can not claim the device
10325 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010326static int
James Smart3772a992009-05-22 14:50:54 -040010327lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
10328{
10329 struct lpfc_hba *phba;
10330 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -040010331 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -040010332 int error;
10333 uint32_t cfg_mode, intr_mode;
10334
10335 /* Allocate memory for HBA structure */
10336 phba = lpfc_hba_alloc(pdev);
10337 if (!phba)
10338 return -ENOMEM;
10339
10340 /* Perform generic PCI device enabling operation */
10341 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -040010342 if (error)
James Smart3772a992009-05-22 14:50:54 -040010343 goto out_free_phba;
James Smart3772a992009-05-22 14:50:54 -040010344
10345 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
10346 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
10347 if (error)
10348 goto out_disable_pci_dev;
10349
10350 /* Set up SLI-3 specific device PCI memory space */
10351 error = lpfc_sli_pci_mem_setup(phba);
10352 if (error) {
10353 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10354 "1402 Failed to set up pci memory space.\n");
10355 goto out_disable_pci_dev;
10356 }
10357
James Smart3772a992009-05-22 14:50:54 -040010358 /* Set up SLI-3 specific device driver resources */
10359 error = lpfc_sli_driver_resource_setup(phba);
10360 if (error) {
10361 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10362 "1404 Failed to set up driver resource.\n");
10363 goto out_unset_pci_mem_s3;
10364 }
10365
10366 /* Initialize and populate the iocb list per host */
James Smartd1f525a2017-04-21 16:04:55 -070010367
James Smart3772a992009-05-22 14:50:54 -040010368 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
10369 if (error) {
10370 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10371 "1405 Failed to initialize iocb list.\n");
10372 goto out_unset_driver_resource_s3;
10373 }
10374
10375 /* Set up common device driver resources */
10376 error = lpfc_setup_driver_resource_phase2(phba);
10377 if (error) {
10378 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10379 "1406 Failed to set up driver resource.\n");
10380 goto out_free_iocb_list;
10381 }
10382
James Smart079b5c92011-08-21 21:48:49 -040010383 /* Get the default values for Model Name and Description */
10384 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
10385
James Smart3772a992009-05-22 14:50:54 -040010386 /* Create SCSI host to the physical port */
10387 error = lpfc_create_shost(phba);
10388 if (error) {
10389 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10390 "1407 Failed to create scsi host.\n");
10391 goto out_unset_driver_resource;
10392 }
10393
10394 /* Configure sysfs attributes */
10395 vport = phba->pport;
10396 error = lpfc_alloc_sysfs_attr(vport);
10397 if (error) {
10398 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10399 "1476 Failed to allocate sysfs attr\n");
10400 goto out_destroy_shost;
10401 }
10402
James Smart6669f9b2009-10-02 15:16:45 -040010403 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -040010404 /* Now, trying to enable interrupt and bring up the device */
10405 cfg_mode = phba->cfg_use_msi;
10406 while (true) {
10407 /* Put device to a known state before enabling interrupt */
10408 lpfc_stop_port(phba);
10409 /* Configure and enable interrupt */
10410 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
10411 if (intr_mode == LPFC_INTR_ERROR) {
10412 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10413 "0431 Failed to enable interrupt.\n");
10414 error = -ENODEV;
10415 goto out_free_sysfs_attr;
10416 }
10417 /* SLI-3 HBA setup */
10418 if (lpfc_sli_hba_setup(phba)) {
10419 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10420 "1477 Failed to set up hba\n");
10421 error = -ENODEV;
10422 goto out_remove_device;
10423 }
10424
10425 /* Wait 50ms for the interrupts of previous mailbox commands */
10426 msleep(50);
10427 /* Check active interrupts on message signaled interrupts */
10428 if (intr_mode == 0 ||
10429 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
10430 /* Log the current active interrupt mode */
10431 phba->intr_mode = intr_mode;
10432 lpfc_log_intr_mode(phba, intr_mode);
10433 break;
10434 } else {
10435 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10436 "0447 Configure interrupt mode (%d) "
10437 "failed active interrupt test.\n",
10438 intr_mode);
10439 /* Disable the current interrupt mode */
10440 lpfc_sli_disable_intr(phba);
10441 /* Try next level of interrupt mode */
10442 cfg_mode = --intr_mode;
10443 }
10444 }
10445
10446 /* Perform post initialization setup */
10447 lpfc_post_init_setup(phba);
10448
10449 /* Check if there are static vports to be created. */
10450 lpfc_create_static_vport(phba);
10451
10452 return 0;
10453
10454out_remove_device:
10455 lpfc_unset_hba(phba);
10456out_free_sysfs_attr:
10457 lpfc_free_sysfs_attr(vport);
10458out_destroy_shost:
10459 lpfc_destroy_shost(phba);
10460out_unset_driver_resource:
10461 lpfc_unset_driver_resource_phase2(phba);
10462out_free_iocb_list:
10463 lpfc_free_iocb_list(phba);
10464out_unset_driver_resource_s3:
10465 lpfc_sli_driver_resource_unset(phba);
10466out_unset_pci_mem_s3:
10467 lpfc_sli_pci_mem_unset(phba);
10468out_disable_pci_dev:
10469 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040010470 if (shost)
10471 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -040010472out_free_phba:
10473 lpfc_hba_free(phba);
10474 return error;
10475}
10476
10477/**
10478 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -040010479 * @pdev: pointer to PCI device
10480 *
James Smart3772a992009-05-22 14:50:54 -040010481 * This routine is to be called to disattach a device with SLI-3 interface
10482 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
10483 * removed from PCI bus, it performs all the necessary cleanup for the HBA
10484 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -040010485 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010486static void
James Smart3772a992009-05-22 14:50:54 -040010487lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -050010488{
James Smart2e0fef82007-06-17 19:56:36 -050010489 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10490 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -050010491 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -050010492 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -050010493 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -050010494
James Smart549e55c2007-08-02 11:09:51 -040010495 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -040010496 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -040010497 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010498
James Smart858c9f62007-06-17 19:56:39 -050010499 lpfc_free_sysfs_attr(vport);
10500
James Smarteada2722008-12-04 22:39:13 -050010501 /* Release all the vports against this physical port */
10502 vports = lpfc_create_vport_work_array(phba);
10503 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040010504 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
10505 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
10506 continue;
James Smarteada2722008-12-04 22:39:13 -050010507 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040010508 }
James Smarteada2722008-12-04 22:39:13 -050010509 lpfc_destroy_vport_work_array(phba, vports);
10510
10511 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -050010512 fc_remove_host(shost);
10513 scsi_remove_host(shost);
James Smartd613b6a2017-02-12 13:52:37 -080010514
James Smart87af33f2007-10-27 13:37:43 -040010515 lpfc_cleanup(vport);
10516
James Smart2e0fef82007-06-17 19:56:36 -050010517 /*
10518 * Bring down the SLI Layer. This step disable all interrupts,
10519 * clears the rings, discards all mailbox commands, and resets
10520 * the HBA.
10521 */
James Smarta257bf92009-04-06 18:48:10 -040010522
Justin P. Mattock48e34d02010-12-30 15:07:58 -080010523 /* HBA interrupt will be disabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -050010524 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -040010525 /* Stop kthread signal shall trigger work_done one more time */
10526 kthread_stop(phba->worker_thread);
10527 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -050010528 lpfc_sli_brdrestart(phba);
10529
James Smart72859902012-01-18 16:25:38 -050010530 kfree(phba->vpi_bmask);
10531 kfree(phba->vpi_ids);
10532
James Smart3772a992009-05-22 14:50:54 -040010533 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -050010534 spin_lock_irq(&phba->hbalock);
10535 list_del_init(&vport->listentry);
10536 spin_unlock_irq(&phba->hbalock);
10537
James Smart858c9f62007-06-17 19:56:39 -050010538 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -050010539
James Smart912e3ac2011-05-24 11:42:11 -040010540 /* Disable SR-IOV if enabled */
10541 if (phba->cfg_sriov_nr_virtfn)
10542 pci_disable_sriov(pdev);
10543
James Smart5b75da22008-12-04 22:39:35 -050010544 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -040010545 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -050010546
James Smart858c9f62007-06-17 19:56:39 -050010547 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -050010548
10549 /*
10550 * Call scsi_free before mem_free since scsi bufs are released to their
10551 * corresponding pools here.
10552 */
10553 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -040010554 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -050010555
James Smart34b02dc2008-08-24 21:49:55 -040010556 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
10557 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -050010558
James Smart2e0fef82007-06-17 19:56:36 -050010559 /* Free resources associated with SLI2 interface */
10560 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -040010561 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -050010562
10563 /* unmap adapter SLIM and Control Registers */
10564 iounmap(phba->ctrl_regs_memmap_p);
10565 iounmap(phba->slim_memmap_p);
10566
James Smart3772a992009-05-22 14:50:54 -040010567 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -050010568
Johannes Thumshirne0c04832016-06-07 09:44:03 +020010569 pci_release_mem_regions(pdev);
James Smart2e0fef82007-06-17 19:56:36 -050010570 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -050010571}
10572
Linas Vepstas8d63f372007-02-14 14:28:36 -060010573/**
James Smart3772a992009-05-22 14:50:54 -040010574 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -050010575 * @pdev: pointer to PCI device
10576 * @msg: power management message
10577 *
James Smart3772a992009-05-22 14:50:54 -040010578 * This routine is to be called from the kernel's PCI subsystem to support
10579 * system Power Management (PM) to device with SLI-3 interface spec. When
10580 * PM invokes this method, it quiesces the device by stopping the driver's
10581 * worker thread for the device, turning off device's interrupt and DMA,
10582 * and bring the device offline. Note that as the driver implements the
10583 * minimum PM requirements to a power-aware driver's PM support for the
10584 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
10585 * to the suspend() method call will be treated as SUSPEND and the driver will
10586 * fully reinitialize its device during resume() method call, the driver will
10587 * set device to PCI_D3hot state in PCI config space instead of setting it
10588 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -050010589 *
10590 * Return code
James Smart3772a992009-05-22 14:50:54 -040010591 * 0 - driver suspended the device
10592 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -050010593 **/
10594static int
James Smart3772a992009-05-22 14:50:54 -040010595lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -050010596{
10597 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10598 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10599
10600 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10601 "0473 PCI device Power Management suspend.\n");
10602
10603 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040010604 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart3a55b532008-12-04 22:38:54 -050010605 lpfc_offline(phba);
10606 kthread_stop(phba->worker_thread);
10607
10608 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -040010609 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -050010610
10611 /* Save device state to PCI config space */
10612 pci_save_state(pdev);
10613 pci_set_power_state(pdev, PCI_D3hot);
10614
10615 return 0;
10616}
10617
10618/**
James Smart3772a992009-05-22 14:50:54 -040010619 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -050010620 * @pdev: pointer to PCI device
10621 *
James Smart3772a992009-05-22 14:50:54 -040010622 * This routine is to be called from the kernel's PCI subsystem to support
10623 * system Power Management (PM) to device with SLI-3 interface spec. When PM
10624 * invokes this method, it restores the device's PCI config space state and
10625 * fully reinitializes the device and brings it online. Note that as the
10626 * driver implements the minimum PM requirements to a power-aware driver's
10627 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
10628 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
10629 * driver will fully reinitialize its device during resume() method call,
10630 * the device will be set to PCI_D0 directly in PCI config space before
10631 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -050010632 *
10633 * Return code
James Smart3772a992009-05-22 14:50:54 -040010634 * 0 - driver suspended the device
10635 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -050010636 **/
10637static int
James Smart3772a992009-05-22 14:50:54 -040010638lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -050010639{
10640 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10641 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -050010642 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -050010643 int error;
10644
10645 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10646 "0452 PCI device Power Management resume.\n");
10647
10648 /* Restore device state from PCI config space */
10649 pci_set_power_state(pdev, PCI_D0);
10650 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -040010651
James Smart1dfb5a42010-02-12 14:40:50 -050010652 /*
10653 * As the new kernel behavior of pci_restore_state() API call clears
10654 * device saved_state flag, need to save the restored state again.
10655 */
10656 pci_save_state(pdev);
10657
James Smart3a55b532008-12-04 22:38:54 -050010658 if (pdev->is_busmaster)
10659 pci_set_master(pdev);
10660
10661 /* Startup the kernel thread for this host adapter. */
10662 phba->worker_thread = kthread_run(lpfc_do_work, phba,
10663 "lpfc_worker_%d", phba->brd_no);
10664 if (IS_ERR(phba->worker_thread)) {
10665 error = PTR_ERR(phba->worker_thread);
10666 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10667 "0434 PM resume failed to start worker "
10668 "thread: error=x%x.\n", error);
10669 return error;
10670 }
10671
James Smart5b75da22008-12-04 22:39:35 -050010672 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -040010673 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -050010674 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -050010675 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -050010676 "0430 PM resume Failed to enable interrupt\n");
10677 return -EIO;
10678 } else
10679 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -050010680
10681 /* Restart HBA and bring it online */
10682 lpfc_sli_brdrestart(phba);
10683 lpfc_online(phba);
10684
James Smart5b75da22008-12-04 22:39:35 -050010685 /* Log the current active interrupt mode */
10686 lpfc_log_intr_mode(phba, phba->intr_mode);
10687
James Smart3a55b532008-12-04 22:38:54 -050010688 return 0;
10689}
10690
10691/**
James Smart891478a2009-11-18 15:40:23 -050010692 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
10693 * @phba: pointer to lpfc hba data structure.
10694 *
10695 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -040010696 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -050010697 **/
10698static void
10699lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
10700{
10701 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10702 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -040010703
10704 /*
10705 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
10706 * and let the SCSI mid-layer to retry them to recover.
10707 */
James Smartdb55fba2014-04-04 13:52:02 -040010708 lpfc_sli_abort_fcp_rings(phba);
James Smart891478a2009-11-18 15:40:23 -050010709}
10710
10711/**
James Smart0d878412009-10-02 15:16:56 -040010712 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
10713 * @phba: pointer to lpfc hba data structure.
10714 *
10715 * This routine is called to prepare the SLI3 device for PCI slot reset. It
10716 * disables the device interrupt and pci device, and aborts the internal FCP
10717 * pending I/Os.
10718 **/
10719static void
10720lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
10721{
James Smart0d878412009-10-02 15:16:56 -040010722 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -050010723 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -040010724
James Smart75baf692010-06-08 18:31:21 -040010725 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040010726 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040010727
James Smarte2af0d22010-03-15 11:25:32 -040010728 /* Block all SCSI devices' I/Os on the host */
10729 lpfc_scsi_dev_block(phba);
10730
James Smartea714f32013-04-17 20:18:47 -040010731 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
10732 lpfc_sli_flush_fcp_rings(phba);
10733
James Smarte2af0d22010-03-15 11:25:32 -040010734 /* stop all timers */
10735 lpfc_stop_hba_timers(phba);
10736
James Smart0d878412009-10-02 15:16:56 -040010737 /* Disable interrupt and pci device */
10738 lpfc_sli_disable_intr(phba);
10739 pci_disable_device(phba->pcidev);
James Smart0d878412009-10-02 15:16:56 -040010740}
10741
10742/**
10743 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
10744 * @phba: pointer to lpfc hba data structure.
10745 *
10746 * This routine is called to prepare the SLI3 device for PCI slot permanently
10747 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
10748 * pending I/Os.
10749 **/
10750static void
James Smart75baf692010-06-08 18:31:21 -040010751lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
James Smart0d878412009-10-02 15:16:56 -040010752{
10753 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -050010754 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -040010755 /* Block all SCSI devices' I/Os on the host */
10756 lpfc_scsi_dev_block(phba);
10757
10758 /* stop all timers */
10759 lpfc_stop_hba_timers(phba);
10760
James Smart0d878412009-10-02 15:16:56 -040010761 /* Clean up all driver's outstanding SCSI I/Os */
10762 lpfc_sli_flush_fcp_rings(phba);
10763}
10764
10765/**
James Smart3772a992009-05-22 14:50:54 -040010766 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -040010767 * @pdev: pointer to PCI device.
10768 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -060010769 *
James Smart3772a992009-05-22 14:50:54 -040010770 * This routine is called from the PCI subsystem for I/O error handling to
10771 * device with SLI-3 interface spec. This function is called by the PCI
10772 * subsystem after a PCI bus error affecting this device has been detected.
10773 * When this function is invoked, it will need to stop all the I/Os and
10774 * interrupt(s) to the device. Once that is done, it will return
10775 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
10776 * as desired.
James Smarte59058c2008-08-24 21:49:00 -040010777 *
10778 * Return codes
James Smart0d878412009-10-02 15:16:56 -040010779 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -040010780 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10781 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -040010782 **/
James Smart3772a992009-05-22 14:50:54 -040010783static pci_ers_result_t
10784lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -060010785{
James Smart51ef4c22007-08-02 11:10:31 -040010786 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10787 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060010788
James Smart0d878412009-10-02 15:16:56 -040010789 switch (state) {
10790 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -050010791 /* Non-fatal error, prepare for recovery */
10792 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -040010793 return PCI_ERS_RESULT_CAN_RECOVER;
10794 case pci_channel_io_frozen:
10795 /* Fatal error, prepare for slot reset */
10796 lpfc_sli_prep_dev_for_reset(phba);
10797 return PCI_ERS_RESULT_NEED_RESET;
10798 case pci_channel_io_perm_failure:
10799 /* Permanent failure, prepare for device down */
James Smart75baf692010-06-08 18:31:21 -040010800 lpfc_sli_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -060010801 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -040010802 default:
10803 /* Unknown state, prepare and request slot reset */
10804 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10805 "0472 Unknown PCI error state: x%x\n", state);
10806 lpfc_sli_prep_dev_for_reset(phba);
10807 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -040010808 }
Linas Vepstas8d63f372007-02-14 14:28:36 -060010809}
10810
10811/**
James Smart3772a992009-05-22 14:50:54 -040010812 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -040010813 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -060010814 *
James Smart3772a992009-05-22 14:50:54 -040010815 * This routine is called from the PCI subsystem for error handling to
10816 * device with SLI-3 interface spec. This is called after PCI bus has been
10817 * reset to restart the PCI card from scratch, as if from a cold-boot.
10818 * During the PCI subsystem error recovery, after driver returns
10819 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
10820 * recovery and then call this routine before calling the .resume method
10821 * to recover the device. This function will initialize the HBA device,
10822 * enable the interrupt, but it will just put the HBA to offline state
10823 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -040010824 *
10825 * Return codes
James Smart3772a992009-05-22 14:50:54 -040010826 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10827 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -060010828 */
James Smart3772a992009-05-22 14:50:54 -040010829static pci_ers_result_t
10830lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -060010831{
James Smart51ef4c22007-08-02 11:10:31 -040010832 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10833 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060010834 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -050010835 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -060010836
10837 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +110010838 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -060010839 printk(KERN_ERR "lpfc: Cannot re-enable "
10840 "PCI device after reset.\n");
10841 return PCI_ERS_RESULT_DISCONNECT;
10842 }
10843
James Smart97207482008-12-04 22:39:19 -050010844 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050010845
10846 /*
10847 * As the new kernel behavior of pci_restore_state() API call clears
10848 * device saved_state flag, need to save the restored state again.
10849 */
10850 pci_save_state(pdev);
10851
James Smart97207482008-12-04 22:39:19 -050010852 if (pdev->is_busmaster)
10853 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -060010854
James Smart92d7f7b2007-06-17 19:56:38 -050010855 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010856 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -050010857 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -060010858
James Smart5b75da22008-12-04 22:39:35 -050010859 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -040010860 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -050010861 if (intr_mode == LPFC_INTR_ERROR) {
10862 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10863 "0427 Cannot re-enable interrupt after "
10864 "slot reset.\n");
10865 return PCI_ERS_RESULT_DISCONNECT;
10866 } else
10867 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -060010868
James Smart75baf692010-06-08 18:31:21 -040010869 /* Take device offline, it will perform cleanup */
James Smart618a5232012-06-12 13:54:36 -040010870 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
Linas Vepstas8d63f372007-02-14 14:28:36 -060010871 lpfc_offline(phba);
10872 lpfc_sli_brdrestart(phba);
10873
James Smart5b75da22008-12-04 22:39:35 -050010874 /* Log the current active interrupt mode */
10875 lpfc_log_intr_mode(phba, phba->intr_mode);
10876
Linas Vepstas8d63f372007-02-14 14:28:36 -060010877 return PCI_ERS_RESULT_RECOVERED;
10878}
10879
10880/**
James Smart3772a992009-05-22 14:50:54 -040010881 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -040010882 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -060010883 *
James Smart3772a992009-05-22 14:50:54 -040010884 * This routine is called from the PCI subsystem for error handling to device
10885 * with SLI-3 interface spec. It is called when kernel error recovery tells
10886 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
10887 * error recovery. After this call, traffic can start to flow from this device
10888 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -060010889 */
James Smart3772a992009-05-22 14:50:54 -040010890static void
10891lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -060010892{
James Smart51ef4c22007-08-02 11:10:31 -040010893 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10894 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060010895
James Smarte2af0d22010-03-15 11:25:32 -040010896 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -040010897 lpfc_online(phba);
James Smart0d878412009-10-02 15:16:56 -040010898
10899 /* Clean up Advanced Error Reporting (AER) if needed */
10900 if (phba->hba_flag & HBA_AER_ENABLED)
10901 pci_cleanup_aer_uncorrect_error_status(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -060010902}
10903
James Smart3772a992009-05-22 14:50:54 -040010904/**
James Smartda0436e2009-05-22 14:51:39 -040010905 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
10906 * @phba: pointer to lpfc hba data structure.
10907 *
10908 * returns the number of ELS/CT IOCBs to reserve
10909 **/
10910int
10911lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
10912{
10913 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
10914
James Smartf1126682009-06-10 17:22:44 -040010915 if (phba->sli_rev == LPFC_SLI_REV4) {
10916 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -040010917 return 10;
James Smartf1126682009-06-10 17:22:44 -040010918 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -040010919 return 25;
James Smartf1126682009-06-10 17:22:44 -040010920 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -040010921 return 50;
James Smartf1126682009-06-10 17:22:44 -040010922 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -040010923 return 100;
James Smart8a9d2e82012-05-09 21:16:12 -040010924 else if (max_xri <= 1536)
James Smart6a9c52c2009-10-02 15:16:51 -040010925 return 150;
James Smart8a9d2e82012-05-09 21:16:12 -040010926 else if (max_xri <= 2048)
10927 return 200;
10928 else
10929 return 250;
James Smartf1126682009-06-10 17:22:44 -040010930 } else
10931 return 0;
James Smartda0436e2009-05-22 14:51:39 -040010932}
10933
10934/**
James Smart895427b2017-02-12 13:52:30 -080010935 * lpfc_sli4_get_iocb_cnt - Calculate the # of total IOCBs to reserve
10936 * @phba: pointer to lpfc hba data structure.
10937 *
James Smartf358dd02017-02-12 13:52:34 -080010938 * returns the number of ELS/CT + NVMET IOCBs to reserve
James Smart895427b2017-02-12 13:52:30 -080010939 **/
10940int
10941lpfc_sli4_get_iocb_cnt(struct lpfc_hba *phba)
10942{
10943 int max_xri = lpfc_sli4_get_els_iocb_cnt(phba);
10944
James Smartf358dd02017-02-12 13:52:34 -080010945 if (phba->nvmet_support)
10946 max_xri += LPFC_NVMET_BUF_POST;
James Smart895427b2017-02-12 13:52:30 -080010947 return max_xri;
10948}
10949
10950
10951/**
James Smart52d52442011-05-24 11:42:45 -040010952 * lpfc_write_firmware - attempt to write a firmware image to the port
James Smart52d52442011-05-24 11:42:45 -040010953 * @fw: pointer to firmware image returned from request_firmware.
James Smartce396282012-09-29 11:30:56 -040010954 * @phba: pointer to lpfc hba data structure.
James Smart52d52442011-05-24 11:42:45 -040010955 *
James Smart52d52442011-05-24 11:42:45 -040010956 **/
James Smartce396282012-09-29 11:30:56 -040010957static void
10958lpfc_write_firmware(const struct firmware *fw, void *context)
James Smart52d52442011-05-24 11:42:45 -040010959{
James Smartce396282012-09-29 11:30:56 -040010960 struct lpfc_hba *phba = (struct lpfc_hba *)context;
James Smart6b5151f2012-01-18 16:24:06 -050010961 char fwrev[FW_REV_STR_SIZE];
James Smartce396282012-09-29 11:30:56 -040010962 struct lpfc_grp_hdr *image;
James Smart52d52442011-05-24 11:42:45 -040010963 struct list_head dma_buffer_list;
10964 int i, rc = 0;
10965 struct lpfc_dmabuf *dmabuf, *next;
10966 uint32_t offset = 0, temp_offset = 0;
James Smart6b6ef5d2016-10-13 15:06:17 -070010967 uint32_t magic_number, ftype, fid, fsize;
James Smart52d52442011-05-24 11:42:45 -040010968
James Smartc71ab862012-10-31 14:44:33 -040010969 /* It can be null in no-wait mode, sanity check */
James Smartce396282012-09-29 11:30:56 -040010970 if (!fw) {
10971 rc = -ENXIO;
10972 goto out;
10973 }
10974 image = (struct lpfc_grp_hdr *)fw->data;
10975
James Smart6b6ef5d2016-10-13 15:06:17 -070010976 magic_number = be32_to_cpu(image->magic_number);
10977 ftype = bf_get_be32(lpfc_grp_hdr_file_type, image);
10978 fid = bf_get_be32(lpfc_grp_hdr_id, image),
10979 fsize = be32_to_cpu(image->size);
10980
James Smart52d52442011-05-24 11:42:45 -040010981 INIT_LIST_HEAD(&dma_buffer_list);
James Smart6b6ef5d2016-10-13 15:06:17 -070010982 if ((magic_number != LPFC_GROUP_OJECT_MAGIC_G5 &&
10983 magic_number != LPFC_GROUP_OJECT_MAGIC_G6) ||
10984 ftype != LPFC_FILE_TYPE_GROUP || fsize != fw->size) {
James Smart52d52442011-05-24 11:42:45 -040010985 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10986 "3022 Invalid FW image found. "
Arnd Bergmannefe583c2016-10-17 14:35:46 +020010987 "Magic:%x Type:%x ID:%x Size %d %zd\n",
James Smart6b6ef5d2016-10-13 15:06:17 -070010988 magic_number, ftype, fid, fsize, fw->size);
James Smartce396282012-09-29 11:30:56 -040010989 rc = -EINVAL;
10990 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010991 }
10992 lpfc_decode_firmware_rev(phba, fwrev, 1);
James Smart88a2cfb2011-07-22 18:36:33 -040010993 if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
James Smart52d52442011-05-24 11:42:45 -040010994 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartce396282012-09-29 11:30:56 -040010995 "3023 Updating Firmware, Current Version:%s "
James Smart52d52442011-05-24 11:42:45 -040010996 "New Version:%s\n",
James Smart88a2cfb2011-07-22 18:36:33 -040010997 fwrev, image->revision);
James Smart52d52442011-05-24 11:42:45 -040010998 for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
10999 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
11000 GFP_KERNEL);
11001 if (!dmabuf) {
11002 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040011003 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040011004 }
11005 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
11006 SLI4_PAGE_SIZE,
11007 &dmabuf->phys,
11008 GFP_KERNEL);
11009 if (!dmabuf->virt) {
11010 kfree(dmabuf);
11011 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040011012 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040011013 }
11014 list_add_tail(&dmabuf->list, &dma_buffer_list);
11015 }
11016 while (offset < fw->size) {
11017 temp_offset = offset;
11018 list_for_each_entry(dmabuf, &dma_buffer_list, list) {
James Smart079b5c92011-08-21 21:48:49 -040011019 if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
James Smart52d52442011-05-24 11:42:45 -040011020 memcpy(dmabuf->virt,
11021 fw->data + temp_offset,
James Smart079b5c92011-08-21 21:48:49 -040011022 fw->size - temp_offset);
11023 temp_offset = fw->size;
James Smart52d52442011-05-24 11:42:45 -040011024 break;
11025 }
James Smart52d52442011-05-24 11:42:45 -040011026 memcpy(dmabuf->virt, fw->data + temp_offset,
11027 SLI4_PAGE_SIZE);
James Smart88a2cfb2011-07-22 18:36:33 -040011028 temp_offset += SLI4_PAGE_SIZE;
James Smart52d52442011-05-24 11:42:45 -040011029 }
11030 rc = lpfc_wr_object(phba, &dma_buffer_list,
11031 (fw->size - offset), &offset);
James Smartce396282012-09-29 11:30:56 -040011032 if (rc)
11033 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040011034 }
11035 rc = offset;
11036 }
James Smartce396282012-09-29 11:30:56 -040011037
11038release_out:
James Smart52d52442011-05-24 11:42:45 -040011039 list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
11040 list_del(&dmabuf->list);
11041 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
11042 dmabuf->virt, dmabuf->phys);
11043 kfree(dmabuf);
11044 }
James Smartce396282012-09-29 11:30:56 -040011045 release_firmware(fw);
11046out:
11047 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartc71ab862012-10-31 14:44:33 -040011048 "3024 Firmware update done: %d.\n", rc);
James Smartce396282012-09-29 11:30:56 -040011049 return;
James Smart52d52442011-05-24 11:42:45 -040011050}
11051
11052/**
James Smartc71ab862012-10-31 14:44:33 -040011053 * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
11054 * @phba: pointer to lpfc hba data structure.
11055 *
11056 * This routine is called to perform Linux generic firmware upgrade on device
11057 * that supports such feature.
11058 **/
11059int
11060lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
11061{
11062 uint8_t file_name[ELX_MODEL_NAME_SIZE];
11063 int ret;
11064 const struct firmware *fw;
11065
11066 /* Only supported on SLI4 interface type 2 for now */
11067 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
11068 LPFC_SLI_INTF_IF_TYPE_2)
11069 return -EPERM;
11070
11071 snprintf(file_name, ELX_MODEL_NAME_SIZE, "%s.grp", phba->ModelName);
11072
11073 if (fw_upgrade == INT_FW_UPGRADE) {
11074 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
11075 file_name, &phba->pcidev->dev,
11076 GFP_KERNEL, (void *)phba,
11077 lpfc_write_firmware);
11078 } else if (fw_upgrade == RUN_FW_UPGRADE) {
11079 ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
11080 if (!ret)
11081 lpfc_write_firmware(fw, (void *)phba);
11082 } else {
11083 ret = -EINVAL;
11084 }
11085
11086 return ret;
11087}
11088
11089/**
James Smartda0436e2009-05-22 14:51:39 -040011090 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
11091 * @pdev: pointer to PCI device
11092 * @pid: pointer to PCI device identifier
11093 *
11094 * This routine is called from the kernel's PCI subsystem to device with
11095 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
11096 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
11097 * information of the device and driver to see if the driver state that it
11098 * can support this kind of device. If the match is successful, the driver
11099 * core invokes this routine. If this routine determines it can claim the HBA,
11100 * it does all the initialization that it needs to do to handle the HBA
11101 * properly.
11102 *
11103 * Return code
11104 * 0 - driver can claim the device
11105 * negative value - driver can not claim the device
11106 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011107static int
James Smartda0436e2009-05-22 14:51:39 -040011108lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
11109{
11110 struct lpfc_hba *phba;
11111 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -040011112 struct Scsi_Host *shost = NULL;
James Smart2b7824d2017-04-21 16:04:59 -070011113 int error, cnt;
James Smartda0436e2009-05-22 14:51:39 -040011114 uint32_t cfg_mode, intr_mode;
James Smartda0436e2009-05-22 14:51:39 -040011115
11116 /* Allocate memory for HBA structure */
11117 phba = lpfc_hba_alloc(pdev);
11118 if (!phba)
11119 return -ENOMEM;
11120
11121 /* Perform generic PCI device enabling operation */
11122 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -040011123 if (error)
James Smartda0436e2009-05-22 14:51:39 -040011124 goto out_free_phba;
James Smartda0436e2009-05-22 14:51:39 -040011125
11126 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
11127 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
11128 if (error)
11129 goto out_disable_pci_dev;
11130
11131 /* Set up SLI-4 specific device PCI memory space */
11132 error = lpfc_sli4_pci_mem_setup(phba);
11133 if (error) {
11134 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11135 "1410 Failed to set up pci memory space.\n");
11136 goto out_disable_pci_dev;
11137 }
11138
James Smartda0436e2009-05-22 14:51:39 -040011139 /* Set up SLI-4 Specific device driver resources */
11140 error = lpfc_sli4_driver_resource_setup(phba);
11141 if (error) {
11142 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11143 "1412 Failed to set up driver resource.\n");
11144 goto out_unset_pci_mem_s4;
11145 }
11146
James Smart2b7824d2017-04-21 16:04:59 -070011147 cnt = phba->cfg_iocb_cnt * 1024;
11148 if (phba->nvmet_support)
11149 cnt += phba->cfg_nvmet_mrq_post * phba->cfg_nvmet_mrq;
11150
James Smartda0436e2009-05-22 14:51:39 -040011151 /* Initialize and populate the iocb list per host */
James Smart2a9bf3d2010-06-07 15:24:45 -040011152 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart2b7824d2017-04-21 16:04:59 -070011153 "2821 initialize iocb list %d total %d\n",
11154 phba->cfg_iocb_cnt, cnt);
11155 error = lpfc_init_iocb_list(phba, cnt);
James Smart2a9bf3d2010-06-07 15:24:45 -040011156
James Smartda0436e2009-05-22 14:51:39 -040011157 if (error) {
11158 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11159 "1413 Failed to initialize iocb list.\n");
11160 goto out_unset_driver_resource_s4;
11161 }
11162
James Smart19ca7602010-11-20 23:11:55 -050011163 INIT_LIST_HEAD(&phba->active_rrq_list);
James Smart7d791df2011-07-22 18:37:52 -040011164 INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
James Smart19ca7602010-11-20 23:11:55 -050011165
James Smartda0436e2009-05-22 14:51:39 -040011166 /* Set up common device driver resources */
11167 error = lpfc_setup_driver_resource_phase2(phba);
11168 if (error) {
11169 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11170 "1414 Failed to set up driver resource.\n");
11171 goto out_free_iocb_list;
11172 }
11173
James Smart079b5c92011-08-21 21:48:49 -040011174 /* Get the default values for Model Name and Description */
11175 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
11176
James Smartda0436e2009-05-22 14:51:39 -040011177 /* Create SCSI host to the physical port */
11178 error = lpfc_create_shost(phba);
11179 if (error) {
11180 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11181 "1415 Failed to create scsi host.\n");
11182 goto out_unset_driver_resource;
11183 }
11184
11185 /* Configure sysfs attributes */
11186 vport = phba->pport;
11187 error = lpfc_alloc_sysfs_attr(vport);
11188 if (error) {
11189 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11190 "1416 Failed to allocate sysfs attr\n");
11191 goto out_destroy_shost;
11192 }
11193
James Smart6669f9b2009-10-02 15:16:45 -040011194 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -040011195 /* Now, trying to enable interrupt and bring up the device */
11196 cfg_mode = phba->cfg_use_msi;
James Smartda0436e2009-05-22 14:51:39 -040011197
James Smart7b15db32013-01-03 15:43:29 -050011198 /* Put device to a known state before enabling interrupt */
11199 lpfc_stop_port(phba);
James Smart895427b2017-02-12 13:52:30 -080011200
James Smart7b15db32013-01-03 15:43:29 -050011201 /* Configure and enable interrupt */
11202 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
11203 if (intr_mode == LPFC_INTR_ERROR) {
11204 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11205 "0426 Failed to enable interrupt.\n");
11206 error = -ENODEV;
11207 goto out_free_sysfs_attr;
James Smartda0436e2009-05-22 14:51:39 -040011208 }
James Smart7b15db32013-01-03 15:43:29 -050011209 /* Default to single EQ for non-MSI-X */
James Smart895427b2017-02-12 13:52:30 -080011210 if (phba->intr_type != MSIX) {
11211 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP)
11212 phba->cfg_fcp_io_channel = 1;
James Smart2d7dbc42017-02-12 13:52:35 -080011213 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart895427b2017-02-12 13:52:30 -080011214 phba->cfg_nvme_io_channel = 1;
James Smart2d7dbc42017-02-12 13:52:35 -080011215 if (phba->nvmet_support)
11216 phba->cfg_nvmet_mrq = 1;
11217 }
James Smart895427b2017-02-12 13:52:30 -080011218 phba->io_channel_irqs = 1;
11219 }
11220
James Smart7b15db32013-01-03 15:43:29 -050011221 /* Set up SLI-4 HBA */
11222 if (lpfc_sli4_hba_setup(phba)) {
11223 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11224 "1421 Failed to set up hba\n");
11225 error = -ENODEV;
11226 goto out_disable_intr;
11227 }
11228
11229 /* Log the current active interrupt mode */
11230 phba->intr_mode = intr_mode;
11231 lpfc_log_intr_mode(phba, intr_mode);
James Smartda0436e2009-05-22 14:51:39 -040011232
11233 /* Perform post initialization setup */
11234 lpfc_post_init_setup(phba);
11235
James Smart01649562017-02-12 13:52:32 -080011236 /* NVME support in FW earlier in the driver load corrects the
11237 * FC4 type making a check for nvme_support unnecessary.
11238 */
11239 if ((phba->nvmet_support == 0) &&
11240 (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)) {
11241 /* Create NVME binding with nvme_fc_transport. This
James Smartd1f525a2017-04-21 16:04:55 -070011242 * ensures the vport is initialized. If the localport
11243 * create fails, it should not unload the driver to
11244 * support field issues.
James Smart01649562017-02-12 13:52:32 -080011245 */
11246 error = lpfc_nvme_create_localport(vport);
11247 if (error) {
11248 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11249 "6004 NVME registration failed, "
11250 "error x%x\n",
11251 error);
James Smart01649562017-02-12 13:52:32 -080011252 }
11253 }
James Smart895427b2017-02-12 13:52:30 -080011254
James Smartc71ab862012-10-31 14:44:33 -040011255 /* check for firmware upgrade or downgrade */
11256 if (phba->cfg_request_firmware_upgrade)
Sebastian Herbsztdb6f1c22015-08-31 16:48:14 -040011257 lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
James Smart52d52442011-05-24 11:42:45 -040011258
James Smart1c6834a2009-07-19 10:01:26 -040011259 /* Check if there are static vports to be created. */
11260 lpfc_create_static_vport(phba);
James Smartda0436e2009-05-22 14:51:39 -040011261 return 0;
11262
11263out_disable_intr:
11264 lpfc_sli4_disable_intr(phba);
11265out_free_sysfs_attr:
11266 lpfc_free_sysfs_attr(vport);
11267out_destroy_shost:
11268 lpfc_destroy_shost(phba);
11269out_unset_driver_resource:
11270 lpfc_unset_driver_resource_phase2(phba);
11271out_free_iocb_list:
11272 lpfc_free_iocb_list(phba);
11273out_unset_driver_resource_s4:
11274 lpfc_sli4_driver_resource_unset(phba);
11275out_unset_pci_mem_s4:
11276 lpfc_sli4_pci_mem_unset(phba);
11277out_disable_pci_dev:
11278 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040011279 if (shost)
11280 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -040011281out_free_phba:
11282 lpfc_hba_free(phba);
11283 return error;
11284}
11285
11286/**
11287 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
11288 * @pdev: pointer to PCI device
11289 *
11290 * This routine is called from the kernel's PCI subsystem to device with
11291 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
11292 * removed from PCI bus, it performs all the necessary cleanup for the HBA
11293 * device to be removed from the PCI subsystem properly.
11294 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011295static void
James Smartda0436e2009-05-22 14:51:39 -040011296lpfc_pci_remove_one_s4(struct pci_dev *pdev)
11297{
11298 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11299 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
11300 struct lpfc_vport **vports;
11301 struct lpfc_hba *phba = vport->phba;
11302 int i;
11303
11304 /* Mark the device unloading flag */
11305 spin_lock_irq(&phba->hbalock);
11306 vport->load_flag |= FC_UNLOADING;
11307 spin_unlock_irq(&phba->hbalock);
11308
11309 /* Free the HBA sysfs attributes */
11310 lpfc_free_sysfs_attr(vport);
11311
11312 /* Release all the vports against this physical port */
11313 vports = lpfc_create_vport_work_array(phba);
11314 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040011315 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
11316 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
11317 continue;
James Smartda0436e2009-05-22 14:51:39 -040011318 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040011319 }
James Smartda0436e2009-05-22 14:51:39 -040011320 lpfc_destroy_vport_work_array(phba, vports);
11321
11322 /* Remove FC host and then SCSI host with the physical port */
11323 fc_remove_host(shost);
11324 scsi_remove_host(shost);
11325
James Smartd613b6a2017-02-12 13:52:37 -080011326 /* Perform ndlp cleanup on the physical port. The nvme and nvmet
11327 * localports are destroyed after to cleanup all transport memory.
James Smart895427b2017-02-12 13:52:30 -080011328 */
James Smartda0436e2009-05-22 14:51:39 -040011329 lpfc_cleanup(vport);
James Smartd613b6a2017-02-12 13:52:37 -080011330 lpfc_nvmet_destroy_targetport(phba);
11331 lpfc_nvme_destroy_localport(vport);
James Smartda0436e2009-05-22 14:51:39 -040011332
11333 /*
11334 * Bring down the SLI Layer. This step disables all interrupts,
11335 * clears the rings, discards all mailbox commands, and resets
11336 * the HBA FCoE function.
11337 */
11338 lpfc_debugfs_terminate(vport);
11339 lpfc_sli4_hba_unset(phba);
11340
11341 spin_lock_irq(&phba->hbalock);
11342 list_del_init(&vport->listentry);
11343 spin_unlock_irq(&phba->hbalock);
11344
James Smart3677a3a2010-09-29 11:19:14 -040011345 /* Perform scsi free before driver resource_unset since scsi
James Smartda0436e2009-05-22 14:51:39 -040011346 * buffers are released to their corresponding pools here.
11347 */
11348 lpfc_scsi_free(phba);
James Smart895427b2017-02-12 13:52:30 -080011349 lpfc_nvme_free(phba);
James Smart01649562017-02-12 13:52:32 -080011350 lpfc_free_iocb_list(phba);
James Smart67d12732012-08-03 12:36:13 -040011351
James Smartda0436e2009-05-22 14:51:39 -040011352 lpfc_sli4_driver_resource_unset(phba);
11353
11354 /* Unmap adapter Control and Doorbell registers */
11355 lpfc_sli4_pci_mem_unset(phba);
11356
11357 /* Release PCI resources and disable device's PCI function */
11358 scsi_host_put(shost);
11359 lpfc_disable_pci_dev(phba);
11360
11361 /* Finally, free the driver's device data structure */
11362 lpfc_hba_free(phba);
11363
11364 return;
11365}
11366
11367/**
11368 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
11369 * @pdev: pointer to PCI device
11370 * @msg: power management message
11371 *
11372 * This routine is called from the kernel's PCI subsystem to support system
11373 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
11374 * this method, it quiesces the device by stopping the driver's worker
11375 * thread for the device, turning off device's interrupt and DMA, and bring
11376 * the device offline. Note that as the driver implements the minimum PM
11377 * requirements to a power-aware driver's PM support for suspend/resume -- all
11378 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
11379 * method call will be treated as SUSPEND and the driver will fully
11380 * reinitialize its device during resume() method call, the driver will set
11381 * device to PCI_D3hot state in PCI config space instead of setting it
11382 * according to the @msg provided by the PM.
11383 *
11384 * Return code
11385 * 0 - driver suspended the device
11386 * Error otherwise
11387 **/
11388static int
11389lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
11390{
11391 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11392 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11393
11394 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart75baf692010-06-08 18:31:21 -040011395 "2843 PCI device Power Management suspend.\n");
James Smartda0436e2009-05-22 14:51:39 -040011396
11397 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040011398 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smartda0436e2009-05-22 14:51:39 -040011399 lpfc_offline(phba);
11400 kthread_stop(phba->worker_thread);
11401
11402 /* Disable interrupt from device */
11403 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040011404 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -040011405
11406 /* Save device state to PCI config space */
11407 pci_save_state(pdev);
11408 pci_set_power_state(pdev, PCI_D3hot);
11409
11410 return 0;
11411}
11412
11413/**
11414 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
11415 * @pdev: pointer to PCI device
11416 *
11417 * This routine is called from the kernel's PCI subsystem to support system
11418 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
11419 * this method, it restores the device's PCI config space state and fully
11420 * reinitializes the device and brings it online. Note that as the driver
11421 * implements the minimum PM requirements to a power-aware driver's PM for
11422 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
11423 * to the suspend() method call will be treated as SUSPEND and the driver
11424 * will fully reinitialize its device during resume() method call, the device
11425 * will be set to PCI_D0 directly in PCI config space before restoring the
11426 * state.
11427 *
11428 * Return code
11429 * 0 - driver suspended the device
11430 * Error otherwise
11431 **/
11432static int
11433lpfc_pci_resume_one_s4(struct pci_dev *pdev)
11434{
11435 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11436 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11437 uint32_t intr_mode;
11438 int error;
11439
11440 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11441 "0292 PCI device Power Management resume.\n");
11442
11443 /* Restore device state from PCI config space */
11444 pci_set_power_state(pdev, PCI_D0);
11445 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050011446
11447 /*
11448 * As the new kernel behavior of pci_restore_state() API call clears
11449 * device saved_state flag, need to save the restored state again.
11450 */
11451 pci_save_state(pdev);
11452
James Smartda0436e2009-05-22 14:51:39 -040011453 if (pdev->is_busmaster)
11454 pci_set_master(pdev);
11455
11456 /* Startup the kernel thread for this host adapter. */
11457 phba->worker_thread = kthread_run(lpfc_do_work, phba,
11458 "lpfc_worker_%d", phba->brd_no);
11459 if (IS_ERR(phba->worker_thread)) {
11460 error = PTR_ERR(phba->worker_thread);
11461 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11462 "0293 PM resume failed to start worker "
11463 "thread: error=x%x.\n", error);
11464 return error;
11465 }
11466
11467 /* Configure and enable interrupt */
11468 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
11469 if (intr_mode == LPFC_INTR_ERROR) {
11470 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11471 "0294 PM resume Failed to enable interrupt\n");
11472 return -EIO;
11473 } else
11474 phba->intr_mode = intr_mode;
11475
11476 /* Restart HBA and bring it online */
11477 lpfc_sli_brdrestart(phba);
11478 lpfc_online(phba);
11479
11480 /* Log the current active interrupt mode */
11481 lpfc_log_intr_mode(phba, phba->intr_mode);
11482
11483 return 0;
11484}
11485
11486/**
James Smart75baf692010-06-08 18:31:21 -040011487 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
11488 * @phba: pointer to lpfc hba data structure.
11489 *
11490 * This routine is called to prepare the SLI4 device for PCI slot recover. It
11491 * aborts all the outstanding SCSI I/Os to the pci device.
11492 **/
11493static void
11494lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
11495{
James Smart75baf692010-06-08 18:31:21 -040011496 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11497 "2828 PCI channel I/O abort preparing for recovery\n");
11498 /*
11499 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
11500 * and let the SCSI mid-layer to retry them to recover.
11501 */
James Smartdb55fba2014-04-04 13:52:02 -040011502 lpfc_sli_abort_fcp_rings(phba);
James Smart75baf692010-06-08 18:31:21 -040011503}
11504
11505/**
11506 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
11507 * @phba: pointer to lpfc hba data structure.
11508 *
11509 * This routine is called to prepare the SLI4 device for PCI slot reset. It
11510 * disables the device interrupt and pci device, and aborts the internal FCP
11511 * pending I/Os.
11512 **/
11513static void
11514lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
11515{
11516 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11517 "2826 PCI channel disable preparing for reset\n");
11518
11519 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040011520 lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
James Smart75baf692010-06-08 18:31:21 -040011521
11522 /* Block all SCSI devices' I/Os on the host */
11523 lpfc_scsi_dev_block(phba);
11524
James Smartea714f32013-04-17 20:18:47 -040011525 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
11526 lpfc_sli_flush_fcp_rings(phba);
11527
James Smart75baf692010-06-08 18:31:21 -040011528 /* stop all timers */
11529 lpfc_stop_hba_timers(phba);
11530
11531 /* Disable interrupt and pci device */
11532 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040011533 lpfc_sli4_queue_destroy(phba);
James Smart75baf692010-06-08 18:31:21 -040011534 pci_disable_device(phba->pcidev);
James Smart75baf692010-06-08 18:31:21 -040011535}
11536
11537/**
11538 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
11539 * @phba: pointer to lpfc hba data structure.
11540 *
11541 * This routine is called to prepare the SLI4 device for PCI slot permanently
11542 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
11543 * pending I/Os.
11544 **/
11545static void
11546lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
11547{
11548 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11549 "2827 PCI channel permanent disable for failure\n");
11550
11551 /* Block all SCSI devices' I/Os on the host */
11552 lpfc_scsi_dev_block(phba);
11553
11554 /* stop all timers */
11555 lpfc_stop_hba_timers(phba);
11556
11557 /* Clean up all driver's outstanding SCSI I/Os */
11558 lpfc_sli_flush_fcp_rings(phba);
11559}
11560
11561/**
James Smartda0436e2009-05-22 14:51:39 -040011562 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
11563 * @pdev: pointer to PCI device.
11564 * @state: the current PCI connection state.
11565 *
11566 * This routine is called from the PCI subsystem for error handling to device
11567 * with SLI-4 interface spec. This function is called by the PCI subsystem
11568 * after a PCI bus error affecting this device has been detected. When this
11569 * function is invoked, it will need to stop all the I/Os and interrupt(s)
11570 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
11571 * for the PCI subsystem to perform proper recovery as desired.
11572 *
11573 * Return codes
11574 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
11575 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11576 **/
11577static pci_ers_result_t
11578lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
11579{
James Smart75baf692010-06-08 18:31:21 -040011580 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11581 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11582
11583 switch (state) {
11584 case pci_channel_io_normal:
11585 /* Non-fatal error, prepare for recovery */
11586 lpfc_sli4_prep_dev_for_recover(phba);
11587 return PCI_ERS_RESULT_CAN_RECOVER;
11588 case pci_channel_io_frozen:
11589 /* Fatal error, prepare for slot reset */
11590 lpfc_sli4_prep_dev_for_reset(phba);
11591 return PCI_ERS_RESULT_NEED_RESET;
11592 case pci_channel_io_perm_failure:
11593 /* Permanent failure, prepare for device down */
11594 lpfc_sli4_prep_dev_for_perm_failure(phba);
11595 return PCI_ERS_RESULT_DISCONNECT;
11596 default:
11597 /* Unknown state, prepare and request slot reset */
11598 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11599 "2825 Unknown PCI error state: x%x\n", state);
11600 lpfc_sli4_prep_dev_for_reset(phba);
11601 return PCI_ERS_RESULT_NEED_RESET;
11602 }
James Smartda0436e2009-05-22 14:51:39 -040011603}
11604
11605/**
11606 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
11607 * @pdev: pointer to PCI device.
11608 *
11609 * This routine is called from the PCI subsystem for error handling to device
11610 * with SLI-4 interface spec. It is called after PCI bus has been reset to
11611 * restart the PCI card from scratch, as if from a cold-boot. During the
11612 * PCI subsystem error recovery, after the driver returns
11613 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
11614 * recovery and then call this routine before calling the .resume method to
11615 * recover the device. This function will initialize the HBA device, enable
11616 * the interrupt, but it will just put the HBA to offline state without
11617 * passing any I/O traffic.
11618 *
11619 * Return codes
11620 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
11621 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11622 */
11623static pci_ers_result_t
11624lpfc_io_slot_reset_s4(struct pci_dev *pdev)
11625{
James Smart75baf692010-06-08 18:31:21 -040011626 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11627 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11628 struct lpfc_sli *psli = &phba->sli;
11629 uint32_t intr_mode;
11630
11631 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
11632 if (pci_enable_device_mem(pdev)) {
11633 printk(KERN_ERR "lpfc: Cannot re-enable "
11634 "PCI device after reset.\n");
11635 return PCI_ERS_RESULT_DISCONNECT;
11636 }
11637
11638 pci_restore_state(pdev);
James Smart0a96e972011-07-22 18:37:28 -040011639
11640 /*
11641 * As the new kernel behavior of pci_restore_state() API call clears
11642 * device saved_state flag, need to save the restored state again.
11643 */
11644 pci_save_state(pdev);
11645
James Smart75baf692010-06-08 18:31:21 -040011646 if (pdev->is_busmaster)
11647 pci_set_master(pdev);
11648
11649 spin_lock_irq(&phba->hbalock);
11650 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
11651 spin_unlock_irq(&phba->hbalock);
11652
11653 /* Configure and enable interrupt */
11654 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
11655 if (intr_mode == LPFC_INTR_ERROR) {
11656 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11657 "2824 Cannot re-enable interrupt after "
11658 "slot reset.\n");
11659 return PCI_ERS_RESULT_DISCONNECT;
11660 } else
11661 phba->intr_mode = intr_mode;
11662
11663 /* Log the current active interrupt mode */
11664 lpfc_log_intr_mode(phba, phba->intr_mode);
11665
James Smartda0436e2009-05-22 14:51:39 -040011666 return PCI_ERS_RESULT_RECOVERED;
11667}
11668
11669/**
11670 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
11671 * @pdev: pointer to PCI device
11672 *
11673 * This routine is called from the PCI subsystem for error handling to device
11674 * with SLI-4 interface spec. It is called when kernel error recovery tells
11675 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
11676 * error recovery. After this call, traffic can start to flow from this device
11677 * again.
11678 **/
11679static void
11680lpfc_io_resume_s4(struct pci_dev *pdev)
11681{
James Smart75baf692010-06-08 18:31:21 -040011682 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11683 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11684
11685 /*
11686 * In case of slot reset, as function reset is performed through
11687 * mailbox command which needs DMA to be enabled, this operation
11688 * has to be moved to the io resume phase. Taking device offline
11689 * will perform the necessary cleanup.
11690 */
11691 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
11692 /* Perform device reset */
James Smart618a5232012-06-12 13:54:36 -040011693 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040011694 lpfc_offline(phba);
11695 lpfc_sli_brdrestart(phba);
11696 /* Bring the device back online */
11697 lpfc_online(phba);
11698 }
11699
11700 /* Clean up Advanced Error Reporting (AER) if needed */
11701 if (phba->hba_flag & HBA_AER_ENABLED)
11702 pci_cleanup_aer_uncorrect_error_status(pdev);
James Smartda0436e2009-05-22 14:51:39 -040011703}
11704
11705/**
James Smart3772a992009-05-22 14:50:54 -040011706 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
11707 * @pdev: pointer to PCI device
11708 * @pid: pointer to PCI device identifier
11709 *
11710 * This routine is to be registered to the kernel's PCI subsystem. When an
11711 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
11712 * at PCI device-specific information of the device and driver to see if the
11713 * driver state that it can support this kind of device. If the match is
11714 * successful, the driver core invokes this routine. This routine dispatches
11715 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
11716 * do all the initialization that it needs to do to handle the HBA device
11717 * properly.
11718 *
11719 * Return code
11720 * 0 - driver can claim the device
11721 * negative value - driver can not claim the device
11722 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011723static int
James Smart3772a992009-05-22 14:50:54 -040011724lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
11725{
11726 int rc;
James Smart8fa38512009-07-19 10:01:03 -040011727 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -040011728
James Smart28baac72010-02-12 14:42:03 -050011729 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -040011730 return -ENODEV;
11731
James Smart8fa38512009-07-19 10:01:03 -040011732 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -050011733 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -040011734 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040011735 else
James Smart3772a992009-05-22 14:50:54 -040011736 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040011737
James Smart3772a992009-05-22 14:50:54 -040011738 return rc;
11739}
11740
11741/**
11742 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
11743 * @pdev: pointer to PCI device
11744 *
11745 * This routine is to be registered to the kernel's PCI subsystem. When an
11746 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
11747 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
11748 * remove routine, which will perform all the necessary cleanup for the
11749 * device to be removed from the PCI subsystem properly.
11750 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011751static void
James Smart3772a992009-05-22 14:50:54 -040011752lpfc_pci_remove_one(struct pci_dev *pdev)
11753{
11754 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11755 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11756
11757 switch (phba->pci_dev_grp) {
11758 case LPFC_PCI_DEV_LP:
11759 lpfc_pci_remove_one_s3(pdev);
11760 break;
James Smartda0436e2009-05-22 14:51:39 -040011761 case LPFC_PCI_DEV_OC:
11762 lpfc_pci_remove_one_s4(pdev);
11763 break;
James Smart3772a992009-05-22 14:50:54 -040011764 default:
11765 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11766 "1424 Invalid PCI device group: 0x%x\n",
11767 phba->pci_dev_grp);
11768 break;
11769 }
11770 return;
11771}
11772
11773/**
11774 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
11775 * @pdev: pointer to PCI device
11776 * @msg: power management message
11777 *
11778 * This routine is to be registered to the kernel's PCI subsystem to support
11779 * system Power Management (PM). When PM invokes this method, it dispatches
11780 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
11781 * suspend the device.
11782 *
11783 * Return code
11784 * 0 - driver suspended the device
11785 * Error otherwise
11786 **/
11787static int
11788lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
11789{
11790 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11791 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11792 int rc = -ENODEV;
11793
11794 switch (phba->pci_dev_grp) {
11795 case LPFC_PCI_DEV_LP:
11796 rc = lpfc_pci_suspend_one_s3(pdev, msg);
11797 break;
James Smartda0436e2009-05-22 14:51:39 -040011798 case LPFC_PCI_DEV_OC:
11799 rc = lpfc_pci_suspend_one_s4(pdev, msg);
11800 break;
James Smart3772a992009-05-22 14:50:54 -040011801 default:
11802 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11803 "1425 Invalid PCI device group: 0x%x\n",
11804 phba->pci_dev_grp);
11805 break;
11806 }
11807 return rc;
11808}
11809
11810/**
11811 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
11812 * @pdev: pointer to PCI device
11813 *
11814 * This routine is to be registered to the kernel's PCI subsystem to support
11815 * system Power Management (PM). When PM invokes this method, it dispatches
11816 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
11817 * resume the device.
11818 *
11819 * Return code
11820 * 0 - driver suspended the device
11821 * Error otherwise
11822 **/
11823static int
11824lpfc_pci_resume_one(struct pci_dev *pdev)
11825{
11826 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11827 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11828 int rc = -ENODEV;
11829
11830 switch (phba->pci_dev_grp) {
11831 case LPFC_PCI_DEV_LP:
11832 rc = lpfc_pci_resume_one_s3(pdev);
11833 break;
James Smartda0436e2009-05-22 14:51:39 -040011834 case LPFC_PCI_DEV_OC:
11835 rc = lpfc_pci_resume_one_s4(pdev);
11836 break;
James Smart3772a992009-05-22 14:50:54 -040011837 default:
11838 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11839 "1426 Invalid PCI device group: 0x%x\n",
11840 phba->pci_dev_grp);
11841 break;
11842 }
11843 return rc;
11844}
11845
11846/**
11847 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
11848 * @pdev: pointer to PCI device.
11849 * @state: the current PCI connection state.
11850 *
11851 * This routine is registered to the PCI subsystem for error handling. This
11852 * function is called by the PCI subsystem after a PCI bus error affecting
11853 * this device has been detected. When this routine is invoked, it dispatches
11854 * the action to the proper SLI-3 or SLI-4 device error detected handling
11855 * routine, which will perform the proper error detected operation.
11856 *
11857 * Return codes
11858 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
11859 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11860 **/
11861static pci_ers_result_t
11862lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
11863{
11864 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11865 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11866 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
11867
11868 switch (phba->pci_dev_grp) {
11869 case LPFC_PCI_DEV_LP:
11870 rc = lpfc_io_error_detected_s3(pdev, state);
11871 break;
James Smartda0436e2009-05-22 14:51:39 -040011872 case LPFC_PCI_DEV_OC:
11873 rc = lpfc_io_error_detected_s4(pdev, state);
11874 break;
James Smart3772a992009-05-22 14:50:54 -040011875 default:
11876 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11877 "1427 Invalid PCI device group: 0x%x\n",
11878 phba->pci_dev_grp);
11879 break;
11880 }
11881 return rc;
11882}
11883
11884/**
11885 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
11886 * @pdev: pointer to PCI device.
11887 *
11888 * This routine is registered to the PCI subsystem for error handling. This
11889 * function is called after PCI bus has been reset to restart the PCI card
11890 * from scratch, as if from a cold-boot. When this routine is invoked, it
11891 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
11892 * routine, which will perform the proper device reset.
11893 *
11894 * Return codes
11895 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
11896 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11897 **/
11898static pci_ers_result_t
11899lpfc_io_slot_reset(struct pci_dev *pdev)
11900{
11901 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11902 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11903 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
11904
11905 switch (phba->pci_dev_grp) {
11906 case LPFC_PCI_DEV_LP:
11907 rc = lpfc_io_slot_reset_s3(pdev);
11908 break;
James Smartda0436e2009-05-22 14:51:39 -040011909 case LPFC_PCI_DEV_OC:
11910 rc = lpfc_io_slot_reset_s4(pdev);
11911 break;
James Smart3772a992009-05-22 14:50:54 -040011912 default:
11913 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11914 "1428 Invalid PCI device group: 0x%x\n",
11915 phba->pci_dev_grp);
11916 break;
11917 }
11918 return rc;
11919}
11920
11921/**
11922 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
11923 * @pdev: pointer to PCI device
11924 *
11925 * This routine is registered to the PCI subsystem for error handling. It
11926 * is called when kernel error recovery tells the lpfc driver that it is
11927 * OK to resume normal PCI operation after PCI bus error recovery. When
11928 * this routine is invoked, it dispatches the action to the proper SLI-3
11929 * or SLI-4 device io_resume routine, which will resume the device operation.
11930 **/
11931static void
11932lpfc_io_resume(struct pci_dev *pdev)
11933{
11934 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11935 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11936
11937 switch (phba->pci_dev_grp) {
11938 case LPFC_PCI_DEV_LP:
11939 lpfc_io_resume_s3(pdev);
11940 break;
James Smartda0436e2009-05-22 14:51:39 -040011941 case LPFC_PCI_DEV_OC:
11942 lpfc_io_resume_s4(pdev);
11943 break;
James Smart3772a992009-05-22 14:50:54 -040011944 default:
11945 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11946 "1429 Invalid PCI device group: 0x%x\n",
11947 phba->pci_dev_grp);
11948 break;
11949 }
11950 return;
11951}
11952
James Smart1ba981f2014-02-20 09:56:45 -050011953/**
11954 * lpfc_sli4_oas_verify - Verify OAS is supported by this adapter
11955 * @phba: pointer to lpfc hba data structure.
11956 *
11957 * This routine checks to see if OAS is supported for this adapter. If
11958 * supported, the configure Flash Optimized Fabric flag is set. Otherwise,
11959 * the enable oas flag is cleared and the pool created for OAS device data
11960 * is destroyed.
11961 *
11962 **/
11963void
11964lpfc_sli4_oas_verify(struct lpfc_hba *phba)
11965{
11966
11967 if (!phba->cfg_EnableXLane)
11968 return;
11969
11970 if (phba->sli4_hba.pc_sli4_params.oas_supported) {
11971 phba->cfg_fof = 1;
11972 } else {
James Smartf38fa0b2014-04-04 13:52:21 -040011973 phba->cfg_fof = 0;
James Smart1ba981f2014-02-20 09:56:45 -050011974 if (phba->device_data_mem_pool)
11975 mempool_destroy(phba->device_data_mem_pool);
11976 phba->device_data_mem_pool = NULL;
11977 }
11978
11979 return;
11980}
11981
11982/**
11983 * lpfc_fof_queue_setup - Set up all the fof queues
11984 * @phba: pointer to lpfc hba data structure.
11985 *
11986 * This routine is invoked to set up all the fof queues for the FC HBA
11987 * operation.
11988 *
11989 * Return codes
11990 * 0 - successful
11991 * -ENOMEM - No available memory
11992 **/
11993int
11994lpfc_fof_queue_setup(struct lpfc_hba *phba)
11995{
James Smart895427b2017-02-12 13:52:30 -080011996 struct lpfc_sli_ring *pring;
James Smart1ba981f2014-02-20 09:56:45 -050011997 int rc;
11998
11999 rc = lpfc_eq_create(phba, phba->sli4_hba.fof_eq, LPFC_MAX_IMAX);
12000 if (rc)
12001 return -ENOMEM;
12002
James Smartf38fa0b2014-04-04 13:52:21 -040012003 if (phba->cfg_fof) {
James Smart1ba981f2014-02-20 09:56:45 -050012004
12005 rc = lpfc_cq_create(phba, phba->sli4_hba.oas_cq,
12006 phba->sli4_hba.fof_eq, LPFC_WCQ, LPFC_FCP);
12007 if (rc)
12008 goto out_oas_cq;
12009
12010 rc = lpfc_wq_create(phba, phba->sli4_hba.oas_wq,
12011 phba->sli4_hba.oas_cq, LPFC_FCP);
12012 if (rc)
12013 goto out_oas_wq;
12014
James Smart895427b2017-02-12 13:52:30 -080012015 /* Bind this CQ/WQ to the NVME ring */
12016 pring = phba->sli4_hba.oas_wq->pring;
12017 pring->sli.sli4.wqp =
12018 (void *)phba->sli4_hba.oas_wq;
12019 phba->sli4_hba.oas_cq->pring = pring;
James Smart1ba981f2014-02-20 09:56:45 -050012020 }
12021
12022 return 0;
12023
12024out_oas_wq:
James Smartf38fa0b2014-04-04 13:52:21 -040012025 lpfc_cq_destroy(phba, phba->sli4_hba.oas_cq);
James Smart1ba981f2014-02-20 09:56:45 -050012026out_oas_cq:
12027 lpfc_eq_destroy(phba, phba->sli4_hba.fof_eq);
12028 return rc;
12029
12030}
12031
12032/**
12033 * lpfc_fof_queue_create - Create all the fof queues
12034 * @phba: pointer to lpfc hba data structure.
12035 *
12036 * This routine is invoked to allocate all the fof queues for the FC HBA
12037 * operation. For each SLI4 queue type, the parameters such as queue entry
12038 * count (queue depth) shall be taken from the module parameter. For now,
12039 * we just use some constant number as place holder.
12040 *
12041 * Return codes
12042 * 0 - successful
12043 * -ENOMEM - No availble memory
12044 * -EIO - The mailbox failed to complete successfully.
12045 **/
12046int
12047lpfc_fof_queue_create(struct lpfc_hba *phba)
12048{
12049 struct lpfc_queue *qdesc;
James Smart7e04e212017-04-21 16:05:05 -070012050 uint32_t wqesize;
James Smart1ba981f2014-02-20 09:56:45 -050012051
12052 /* Create FOF EQ */
12053 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
12054 phba->sli4_hba.eq_ecount);
12055 if (!qdesc)
12056 goto out_error;
12057
12058 phba->sli4_hba.fof_eq = qdesc;
12059
James Smartf38fa0b2014-04-04 13:52:21 -040012060 if (phba->cfg_fof) {
James Smart1ba981f2014-02-20 09:56:45 -050012061
12062 /* Create OAS CQ */
12063 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
12064 phba->sli4_hba.cq_ecount);
12065 if (!qdesc)
12066 goto out_error;
12067
12068 phba->sli4_hba.oas_cq = qdesc;
12069
12070 /* Create OAS WQ */
James Smart7e04e212017-04-21 16:05:05 -070012071 wqesize = (phba->fcp_embed_io) ?
12072 LPFC_WQE128_SIZE : phba->sli4_hba.wq_esize;
12073 qdesc = lpfc_sli4_queue_alloc(phba, wqesize,
James Smart1ba981f2014-02-20 09:56:45 -050012074 phba->sli4_hba.wq_ecount);
James Smart7e04e212017-04-21 16:05:05 -070012075
James Smart1ba981f2014-02-20 09:56:45 -050012076 if (!qdesc)
12077 goto out_error;
12078
12079 phba->sli4_hba.oas_wq = qdesc;
James Smart895427b2017-02-12 13:52:30 -080012080 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
James Smart1ba981f2014-02-20 09:56:45 -050012081
12082 }
12083 return 0;
12084
12085out_error:
12086 lpfc_fof_queue_destroy(phba);
12087 return -ENOMEM;
12088}
12089
12090/**
12091 * lpfc_fof_queue_destroy - Destroy all the fof queues
12092 * @phba: pointer to lpfc hba data structure.
12093 *
12094 * This routine is invoked to release all the SLI4 queues with the FC HBA
12095 * operation.
12096 *
12097 * Return codes
12098 * 0 - successful
12099 **/
12100int
12101lpfc_fof_queue_destroy(struct lpfc_hba *phba)
12102{
12103 /* Release FOF Event queue */
12104 if (phba->sli4_hba.fof_eq != NULL) {
12105 lpfc_sli4_queue_free(phba->sli4_hba.fof_eq);
12106 phba->sli4_hba.fof_eq = NULL;
12107 }
12108
12109 /* Release OAS Completion queue */
12110 if (phba->sli4_hba.oas_cq != NULL) {
12111 lpfc_sli4_queue_free(phba->sli4_hba.oas_cq);
12112 phba->sli4_hba.oas_cq = NULL;
12113 }
12114
12115 /* Release OAS Work queue */
12116 if (phba->sli4_hba.oas_wq != NULL) {
12117 lpfc_sli4_queue_free(phba->sli4_hba.oas_wq);
12118 phba->sli4_hba.oas_wq = NULL;
12119 }
12120 return 0;
12121}
12122
dea31012005-04-17 16:05:31 -050012123MODULE_DEVICE_TABLE(pci, lpfc_id_table);
12124
Stephen Hemmingera55b2d22012-09-07 09:33:16 -070012125static const struct pci_error_handlers lpfc_err_handler = {
Linas Vepstas8d63f372007-02-14 14:28:36 -060012126 .error_detected = lpfc_io_error_detected,
12127 .slot_reset = lpfc_io_slot_reset,
12128 .resume = lpfc_io_resume,
12129};
12130
dea31012005-04-17 16:05:31 -050012131static struct pci_driver lpfc_driver = {
12132 .name = LPFC_DRIVER_NAME,
12133 .id_table = lpfc_id_table,
12134 .probe = lpfc_pci_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080012135 .remove = lpfc_pci_remove_one,
Anton Blanchard85e8a232017-02-13 08:49:20 +110012136 .shutdown = lpfc_pci_remove_one,
James Smart3a55b532008-12-04 22:38:54 -050012137 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -040012138 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -050012139 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -050012140};
12141
James Smart3ef6d242012-01-18 16:23:48 -050012142static const struct file_operations lpfc_mgmt_fop = {
Al Viro858feac2013-04-14 22:39:37 -040012143 .owner = THIS_MODULE,
James Smart3ef6d242012-01-18 16:23:48 -050012144};
12145
12146static struct miscdevice lpfc_mgmt_dev = {
12147 .minor = MISC_DYNAMIC_MINOR,
12148 .name = "lpfcmgmt",
12149 .fops = &lpfc_mgmt_fop,
12150};
12151
James Smarte59058c2008-08-24 21:49:00 -040012152/**
James Smart3621a712009-04-06 18:47:14 -040012153 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -040012154 *
12155 * This routine is to be invoked when the lpfc module is loaded into the
12156 * kernel. The special kernel macro module_init() is used to indicate the
12157 * role of this routine to the kernel as lpfc module entry point.
12158 *
12159 * Return codes
12160 * 0 - successful
12161 * -ENOMEM - FC attach transport failed
12162 * all others - failed
12163 */
dea31012005-04-17 16:05:31 -050012164static int __init
12165lpfc_init(void)
12166{
12167 int error = 0;
12168
12169 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040012170 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -050012171
James Smart3ef6d242012-01-18 16:23:48 -050012172 error = misc_register(&lpfc_mgmt_dev);
12173 if (error)
12174 printk(KERN_ERR "Could not register lpfcmgmt device, "
12175 "misc_register returned with status %d", error);
12176
James Smart458c0832016-07-06 12:36:07 -070012177 lpfc_transport_functions.vport_create = lpfc_vport_create;
12178 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
dea31012005-04-17 16:05:31 -050012179 lpfc_transport_template =
12180 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -040012181 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -050012182 return -ENOMEM;
James Smart458c0832016-07-06 12:36:07 -070012183 lpfc_vport_transport_template =
12184 fc_attach_transport(&lpfc_vport_transport_functions);
12185 if (lpfc_vport_transport_template == NULL) {
12186 fc_release_transport(lpfc_transport_template);
12187 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -040012188 }
James Smart7bb03bb2013-04-17 20:19:16 -040012189
12190 /* Initialize in case vector mapping is needed */
James Smartb246de12013-05-31 17:03:07 -040012191 lpfc_used_cpu = NULL;
James Smart2ea259e2017-02-12 13:52:27 -080012192 lpfc_present_cpu = num_present_cpus();
James Smart7bb03bb2013-04-17 20:19:16 -040012193
dea31012005-04-17 16:05:31 -050012194 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -050012195 if (error) {
dea31012005-04-17 16:05:31 -050012196 fc_release_transport(lpfc_transport_template);
James Smart458c0832016-07-06 12:36:07 -070012197 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -050012198 }
dea31012005-04-17 16:05:31 -050012199
12200 return error;
12201}
12202
James Smarte59058c2008-08-24 21:49:00 -040012203/**
James Smart3621a712009-04-06 18:47:14 -040012204 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -040012205 *
12206 * This routine is invoked when the lpfc module is removed from the kernel.
12207 * The special kernel macro module_exit() is used to indicate the role of
12208 * this routine to the kernel as lpfc module exit point.
12209 */
dea31012005-04-17 16:05:31 -050012210static void __exit
12211lpfc_exit(void)
12212{
James Smart3ef6d242012-01-18 16:23:48 -050012213 misc_deregister(&lpfc_mgmt_dev);
dea31012005-04-17 16:05:31 -050012214 pci_unregister_driver(&lpfc_driver);
12215 fc_release_transport(lpfc_transport_template);
James Smart458c0832016-07-06 12:36:07 -070012216 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -050012217 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040012218 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
12219 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050012220 (1L << _dump_buf_data_order), _dump_buf_data);
12221 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
12222 }
12223
12224 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -040012225 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
12226 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050012227 (1L << _dump_buf_dif_order), _dump_buf_dif);
12228 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
12229 }
James Smartb246de12013-05-31 17:03:07 -040012230 kfree(lpfc_used_cpu);
Johannes Thumshirn79739672015-08-31 16:48:11 -040012231 idr_destroy(&lpfc_hba_index);
dea31012005-04-17 16:05:31 -050012232}
12233
12234module_init(lpfc_init);
12235module_exit(lpfc_exit);
12236MODULE_LICENSE("GPL");
12237MODULE_DESCRIPTION(LPFC_MODULE_DESC);
James Smartd080abe2017-02-12 13:52:39 -080012238MODULE_AUTHOR("Broadcom");
dea31012005-04-17 16:05:31 -050012239MODULE_VERSION("0:" LPFC_DRIVER_VERSION);