blob: e24dca2b3f2fb06786c51f6d54c2c5556cd020f9 [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 Smart128bdda2018-01-30 15:59:03 -08004 * Copyright (C) 2017-2018 Broadcom. All Rights Reserved. The term *
James Smartd080abe2017-02-12 13:52:39 -08005 * “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>
Maurizio Lombardi286871a2017-08-23 16:55:48 -070040#include <linux/bitops.h>
dea31012005-04-17 16:05:31 -050041
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040042#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050043#include <scsi/scsi_device.h>
44#include <scsi/scsi_host.h>
45#include <scsi/scsi_transport_fc.h>
James Smart86c67372017-04-21 16:05:04 -070046#include <scsi/scsi_tcq.h>
47#include <scsi/fc/fc_fs.h>
48
49#include <linux/nvme-fc-driver.h>
dea31012005-04-17 16:05:31 -050050
James Smartda0436e2009-05-22 14:51:39 -040051#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050052#include "lpfc_hw.h"
53#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040054#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040055#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050056#include "lpfc_disc.h"
dea31012005-04-17 16:05:31 -050057#include "lpfc.h"
James Smart895427b2017-02-12 13:52:30 -080058#include "lpfc_scsi.h"
59#include "lpfc_nvme.h"
James Smart86c67372017-04-21 16:05:04 -070060#include "lpfc_nvmet.h"
dea31012005-04-17 16:05:31 -050061#include "lpfc_logmsg.h"
62#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050063#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050064#include "lpfc_version.h"
James Smart12f44452016-07-06 12:36:03 -070065#include "lpfc_ids.h"
dea31012005-04-17 16:05:31 -050066
James Smart81301a92008-12-04 22:39:46 -050067char *_dump_buf_data;
68unsigned long _dump_buf_data_order;
69char *_dump_buf_dif;
70unsigned long _dump_buf_dif_order;
71spinlock_t _dump_buf_lock;
72
James Smart7bb03bb2013-04-17 20:19:16 -040073/* Used when mapping IRQ vectors in a driver centric manner */
James Smartb246de12013-05-31 17:03:07 -040074uint16_t *lpfc_used_cpu;
75uint32_t lpfc_present_cpu;
James Smart7bb03bb2013-04-17 20:19:16 -040076
dea31012005-04-17 16:05:31 -050077static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
78static int lpfc_post_rcv_buf(struct lpfc_hba *);
James Smart5350d872011-10-10 21:33:49 -040079static int lpfc_sli4_queue_verify(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040080static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
81static int lpfc_setup_endian_order(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040082static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
James Smart8a9d2e82012-05-09 21:16:12 -040083static void lpfc_free_els_sgl_list(struct lpfc_hba *);
James Smartf358dd02017-02-12 13:52:34 -080084static void lpfc_free_nvmet_sgl_list(struct lpfc_hba *);
James Smart8a9d2e82012-05-09 21:16:12 -040085static void lpfc_init_sgl_list(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040086static int lpfc_init_active_sgl_array(struct lpfc_hba *);
87static void lpfc_free_active_sgl(struct lpfc_hba *);
88static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
89static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
90static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
91static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
92static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
James Smart618a5232012-06-12 13:54:36 -040093static void lpfc_sli4_disable_intr(struct lpfc_hba *);
94static uint32_t lpfc_sli4_enable_intr(struct lpfc_hba *, uint32_t);
James Smart1ba981f2014-02-20 09:56:45 -050095static void lpfc_sli4_oas_verify(struct lpfc_hba *phba);
dea31012005-04-17 16:05:31 -050096
97static struct scsi_transport_template *lpfc_transport_template = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050098static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea31012005-04-17 16:05:31 -050099static DEFINE_IDR(lpfc_hba_index);
James Smartf358dd02017-02-12 13:52:34 -0800100#define LPFC_NVMET_BUF_POST 254
dea31012005-04-17 16:05:31 -0500101
James Smarte59058c2008-08-24 21:49:00 -0400102/**
James Smart3621a712009-04-06 18:47:14 -0400103 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
James Smarte59058c2008-08-24 21:49:00 -0400104 * @phba: pointer to lpfc hba data structure.
105 *
106 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
107 * mailbox command. It retrieves the revision information from the HBA and
108 * collects the Vital Product Data (VPD) about the HBA for preparing the
109 * configuration of the HBA.
110 *
111 * Return codes:
112 * 0 - success.
113 * -ERESTART - requests the SLI layer to reset the HBA and try again.
114 * Any other value - indicates an error.
115 **/
dea31012005-04-17 16:05:31 -0500116int
James Smart2e0fef82007-06-17 19:56:36 -0500117lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500118{
119 lpfc_vpd_t *vp = &phba->vpd;
120 int i = 0, rc;
121 LPFC_MBOXQ_t *pmb;
122 MAILBOX_t *mb;
123 char *lpfc_vpd_data = NULL;
124 uint16_t offset = 0;
125 static char licensed[56] =
126 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -0400127 static int init_key = 1;
dea31012005-04-17 16:05:31 -0500128
129 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
130 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500131 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500132 return -ENOMEM;
133 }
134
James Smart04c68492009-05-22 14:52:52 -0400135 mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -0500136 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500137
138 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -0400139 if (init_key) {
140 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -0500141
James Smart65a29c12006-07-06 15:50:50 -0400142 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
143 *ptext = cpu_to_be32(*ptext);
144 init_key = 0;
145 }
dea31012005-04-17 16:05:31 -0500146
147 lpfc_read_nv(phba, pmb);
148 memset((char*)mb->un.varRDnvp.rsvd3, 0,
149 sizeof (mb->un.varRDnvp.rsvd3));
150 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
151 sizeof (licensed));
152
153 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
154
155 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500156 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smarte8b62012007-08-02 11:10:09 -0400157 "0324 Config Port initialization "
dea31012005-04-17 16:05:31 -0500158 "error, mbxCmd x%x READ_NVPARM, "
159 "mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500160 mb->mbxCommand, mb->mbxStatus);
161 mempool_free(pmb, phba->mbox_mem_pool);
162 return -ERESTART;
163 }
164 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
James Smart2e0fef82007-06-17 19:56:36 -0500165 sizeof(phba->wwnn));
166 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
167 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500168 }
169
James Smart92d7f7b2007-06-17 19:56:38 -0500170 phba->sli3_options = 0x0;
171
dea31012005-04-17 16:05:31 -0500172 /* Setup and issue mailbox READ REV command */
173 lpfc_read_rev(phba, pmb);
174 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
175 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500176 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400177 "0439 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500178 "READ_REV, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500179 mb->mbxCommand, mb->mbxStatus);
180 mempool_free( pmb, phba->mbox_mem_pool);
181 return -ERESTART;
182 }
183
James Smart92d7f7b2007-06-17 19:56:38 -0500184
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500185 /*
186 * The value of rr must be 1 since the driver set the cv field to 1.
187 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500188 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500189 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500190 vp->rev.rBit = 0;
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500191 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400192 "0440 Adapter failed to init, READ_REV has "
193 "missing revision information.\n");
dea31012005-04-17 16:05:31 -0500194 mempool_free(pmb, phba->mbox_mem_pool);
195 return -ERESTART;
dea31012005-04-17 16:05:31 -0500196 }
197
James Smart495a7142008-06-14 22:52:59 -0400198 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
199 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -0500200 return -EINVAL;
James Smart495a7142008-06-14 22:52:59 -0400201 }
James Smarted957682007-06-17 19:56:37 -0500202
dea31012005-04-17 16:05:31 -0500203 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500204 vp->rev.rBit = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500205 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500206 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
207 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
208 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
209 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500210 vp->rev.biuRev = mb->un.varRdRev.biuRev;
211 vp->rev.smRev = mb->un.varRdRev.smRev;
212 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
213 vp->rev.endecRev = mb->un.varRdRev.endecRev;
214 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
215 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
216 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
217 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
218 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
219 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
220
James Smart92d7f7b2007-06-17 19:56:38 -0500221 /* If the sli feature level is less then 9, we must
222 * tear down all RPIs and VPIs on link down if NPIV
223 * is enabled.
224 */
225 if (vp->rev.feaLevelHigh < 9)
226 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
227
dea31012005-04-17 16:05:31 -0500228 if (lpfc_is_LC_HBA(phba->pcidev->device))
229 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
230 sizeof (phba->RandomData));
231
dea31012005-04-17 16:05:31 -0500232 /* Get adapter VPD information */
dea31012005-04-17 16:05:31 -0500233 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
234 if (!lpfc_vpd_data)
James Smartd7c255b2008-08-24 21:50:00 -0400235 goto out_free_mbox;
dea31012005-04-17 16:05:31 -0500236 do {
James Smarta0c87cb2009-07-19 10:01:10 -0400237 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
dea31012005-04-17 16:05:31 -0500238 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
239
240 if (rc != MBX_SUCCESS) {
241 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400242 "0441 VPD not present on adapter, "
dea31012005-04-17 16:05:31 -0500243 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500244 mb->mbxCommand, mb->mbxStatus);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500245 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500246 }
James Smart04c68492009-05-22 14:52:52 -0400247 /* dump mem may return a zero when finished or we got a
248 * mailbox error, either way we are done.
249 */
250 if (mb->un.varDmp.word_cnt == 0)
251 break;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500252 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
253 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
James Smartd7c255b2008-08-24 21:50:00 -0400254 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
255 lpfc_vpd_data + offset,
James Smart92d7f7b2007-06-17 19:56:38 -0500256 mb->un.varDmp.word_cnt);
dea31012005-04-17 16:05:31 -0500257 offset += mb->un.varDmp.word_cnt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500258 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
259 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500260
261 kfree(lpfc_vpd_data);
dea31012005-04-17 16:05:31 -0500262out_free_mbox:
263 mempool_free(pmb, phba->mbox_mem_pool);
264 return 0;
265}
266
James Smarte59058c2008-08-24 21:49:00 -0400267/**
James Smart3621a712009-04-06 18:47:14 -0400268 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
James Smarte59058c2008-08-24 21:49:00 -0400269 * @phba: pointer to lpfc hba data structure.
270 * @pmboxq: pointer to the driver internal queue element for mailbox command.
271 *
272 * This is the completion handler for driver's configuring asynchronous event
273 * mailbox command to the device. If the mailbox command returns successfully,
274 * it will set internal async event support flag to 1; otherwise, it will
275 * set internal async event support flag to 0.
276 **/
James Smart57127f12007-10-27 13:37:05 -0400277static void
278lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
279{
James Smart04c68492009-05-22 14:52:52 -0400280 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
James Smart57127f12007-10-27 13:37:05 -0400281 phba->temp_sensor_support = 1;
282 else
283 phba->temp_sensor_support = 0;
284 mempool_free(pmboxq, phba->mbox_mem_pool);
285 return;
286}
287
James Smarte59058c2008-08-24 21:49:00 -0400288/**
James Smart3621a712009-04-06 18:47:14 -0400289 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
James Smart97207482008-12-04 22:39:19 -0500290 * @phba: pointer to lpfc hba data structure.
291 * @pmboxq: pointer to the driver internal queue element for mailbox command.
292 *
293 * This is the completion handler for dump mailbox command for getting
294 * wake up parameters. When this command complete, the response contain
295 * Option rom version of the HBA. This function translate the version number
296 * into a human readable string and store it in OptionROMVersion.
297 **/
298static void
299lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
300{
301 struct prog_id *prg;
302 uint32_t prog_id_word;
303 char dist = ' ';
304 /* character array used for decoding dist type. */
305 char dist_char[] = "nabx";
306
James Smart04c68492009-05-22 14:52:52 -0400307 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
James Smart9f1e1b52008-12-04 22:39:40 -0500308 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500309 return;
James Smart9f1e1b52008-12-04 22:39:40 -0500310 }
James Smart97207482008-12-04 22:39:19 -0500311
312 prg = (struct prog_id *) &prog_id_word;
313
314 /* word 7 contain option rom version */
James Smart04c68492009-05-22 14:52:52 -0400315 prog_id_word = pmboxq->u.mb.un.varWords[7];
James Smart97207482008-12-04 22:39:19 -0500316
317 /* Decode the Option rom version word to a readable string */
318 if (prg->dist < 4)
319 dist = dist_char[prg->dist];
320
321 if ((prg->dist == 3) && (prg->num == 0))
James Smarta2fc4aef2014-09-03 12:57:55 -0400322 snprintf(phba->OptionROMVersion, 32, "%d.%d%d",
James Smart97207482008-12-04 22:39:19 -0500323 prg->ver, prg->rev, prg->lev);
324 else
James Smarta2fc4aef2014-09-03 12:57:55 -0400325 snprintf(phba->OptionROMVersion, 32, "%d.%d%d%c%d",
James Smart97207482008-12-04 22:39:19 -0500326 prg->ver, prg->rev, prg->lev,
327 dist, prg->num);
James Smart9f1e1b52008-12-04 22:39:40 -0500328 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500329 return;
330}
331
332/**
James Smart05580562011-05-24 11:40:48 -0400333 * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
334 * cfg_soft_wwnn, cfg_soft_wwpn
335 * @vport: pointer to lpfc vport data structure.
336 *
337 *
338 * Return codes
339 * None.
340 **/
341void
342lpfc_update_vport_wwn(struct lpfc_vport *vport)
343{
James Smartaeb3c812017-04-21 16:05:02 -0700344 uint8_t vvvl = vport->fc_sparam.cmn.valid_vendor_ver_level;
345 u32 *fawwpn_key = (u32 *)&vport->fc_sparam.un.vendorVersion[0];
346
James Smart05580562011-05-24 11:40:48 -0400347 /* If the soft name exists then update it using the service params */
348 if (vport->phba->cfg_soft_wwnn)
349 u64_to_wwn(vport->phba->cfg_soft_wwnn,
350 vport->fc_sparam.nodeName.u.wwn);
351 if (vport->phba->cfg_soft_wwpn)
352 u64_to_wwn(vport->phba->cfg_soft_wwpn,
353 vport->fc_sparam.portName.u.wwn);
354
355 /*
356 * If the name is empty or there exists a soft name
357 * then copy the service params name, otherwise use the fc name
358 */
359 if (vport->fc_nodename.u.wwn[0] == 0 || vport->phba->cfg_soft_wwnn)
360 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
361 sizeof(struct lpfc_name));
362 else
363 memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
364 sizeof(struct lpfc_name));
365
James Smartaeb3c812017-04-21 16:05:02 -0700366 /*
367 * If the port name has changed, then set the Param changes flag
368 * to unreg the login
369 */
370 if (vport->fc_portname.u.wwn[0] != 0 &&
371 memcmp(&vport->fc_portname, &vport->fc_sparam.portName,
372 sizeof(struct lpfc_name)))
373 vport->vport_flag |= FAWWPN_PARAM_CHG;
374
375 if (vport->fc_portname.u.wwn[0] == 0 ||
376 vport->phba->cfg_soft_wwpn ||
377 (vvvl == 1 && cpu_to_be32(*fawwpn_key) == FAPWWN_KEY_VENDOR) ||
378 vport->vport_flag & FAWWPN_SET) {
James Smart05580562011-05-24 11:40:48 -0400379 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
380 sizeof(struct lpfc_name));
James Smartaeb3c812017-04-21 16:05:02 -0700381 vport->vport_flag &= ~FAWWPN_SET;
382 if (vvvl == 1 && cpu_to_be32(*fawwpn_key) == FAPWWN_KEY_VENDOR)
383 vport->vport_flag |= FAWWPN_SET;
384 }
James Smart05580562011-05-24 11:40:48 -0400385 else
386 memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
387 sizeof(struct lpfc_name));
388}
389
390/**
James Smart3621a712009-04-06 18:47:14 -0400391 * lpfc_config_port_post - Perform lpfc initialization after config port
James Smarte59058c2008-08-24 21:49:00 -0400392 * @phba: pointer to lpfc hba data structure.
393 *
394 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
395 * command call. It performs all internal resource and state setups on the
396 * port: post IOCB buffers, enable appropriate host interrupt attentions,
397 * ELS ring timers, etc.
398 *
399 * Return codes
400 * 0 - success.
401 * Any other value - error.
402 **/
dea31012005-04-17 16:05:31 -0500403int
James Smart2e0fef82007-06-17 19:56:36 -0500404lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500405{
James Smart2e0fef82007-06-17 19:56:36 -0500406 struct lpfc_vport *vport = phba->pport;
James Smarta257bf92009-04-06 18:48:10 -0400407 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -0500408 LPFC_MBOXQ_t *pmb;
409 MAILBOX_t *mb;
410 struct lpfc_dmabuf *mp;
411 struct lpfc_sli *psli = &phba->sli;
412 uint32_t status, timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500413 int i, j;
414 int rc;
dea31012005-04-17 16:05:31 -0500415
James Smart7af67052007-10-27 13:38:11 -0400416 spin_lock_irq(&phba->hbalock);
417 /*
418 * If the Config port completed correctly the HBA is not
419 * over heated any more.
420 */
421 if (phba->over_temp_state == HBA_OVER_TEMP)
422 phba->over_temp_state = HBA_NORMAL_TEMP;
423 spin_unlock_irq(&phba->hbalock);
424
dea31012005-04-17 16:05:31 -0500425 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
426 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500427 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500428 return -ENOMEM;
429 }
James Smart04c68492009-05-22 14:52:52 -0400430 mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500431
dea31012005-04-17 16:05:31 -0500432 /* Get login parameters for NID. */
James Smart9f1177a2010-02-26 14:12:57 -0500433 rc = lpfc_read_sparam(phba, pmb, 0);
434 if (rc) {
435 mempool_free(pmb, phba->mbox_mem_pool);
436 return -ENOMEM;
437 }
438
James Smarted957682007-06-17 19:56:37 -0500439 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500440 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500441 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400442 "0448 Adapter failed init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500443 "READ_SPARM mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500444 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500445 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500446 mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart9f1177a2010-02-26 14:12:57 -0500447 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500448 lpfc_mbuf_free(phba, mp->virt, mp->phys);
449 kfree(mp);
450 return -EIO;
451 }
452
453 mp = (struct lpfc_dmabuf *) pmb->context1;
454
James Smart2e0fef82007-06-17 19:56:36 -0500455 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500456 lpfc_mbuf_free(phba, mp->virt, mp->phys);
457 kfree(mp);
458 pmb->context1 = NULL;
James Smart05580562011-05-24 11:40:48 -0400459 lpfc_update_vport_wwn(vport);
James Smarta257bf92009-04-06 18:48:10 -0400460
461 /* Update the fc_host data structures with new wwn. */
462 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
463 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart21e9a0a2009-05-22 14:53:21 -0400464 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smarta257bf92009-04-06 18:48:10 -0400465
dea31012005-04-17 16:05:31 -0500466 /* If no serial number in VPD data, use low 6 bytes of WWNN */
467 /* This should be consolidated into parse_vpd ? - mr */
468 if (phba->SerialNumber[0] == 0) {
469 uint8_t *outptr;
470
James Smart2e0fef82007-06-17 19:56:36 -0500471 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500472 for (i = 0; i < 12; i++) {
473 status = *outptr++;
474 j = ((status & 0xf0) >> 4);
475 if (j <= 9)
476 phba->SerialNumber[i] =
477 (char)((uint8_t) 0x30 + (uint8_t) j);
478 else
479 phba->SerialNumber[i] =
480 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
481 i++;
482 j = (status & 0xf);
483 if (j <= 9)
484 phba->SerialNumber[i] =
485 (char)((uint8_t) 0x30 + (uint8_t) j);
486 else
487 phba->SerialNumber[i] =
488 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
489 }
490 }
491
dea31012005-04-17 16:05:31 -0500492 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500493 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500494 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500495 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400496 "0453 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500497 "READ_CONFIG, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500498 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500499 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500500 mempool_free( pmb, phba->mbox_mem_pool);
501 return -EIO;
502 }
503
James Smarta0c87cb2009-07-19 10:01:10 -0400504 /* Check if the port is disabled */
505 lpfc_sli_read_link_ste(phba);
506
dea31012005-04-17 16:05:31 -0500507 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -0400508 i = (mb->un.varRdConfig.max_xri + 1);
509 if (phba->cfg_hba_queue_depth > i) {
510 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
511 "3359 HBA queue depth changed from %d to %d\n",
512 phba->cfg_hba_queue_depth, i);
513 phba->cfg_hba_queue_depth = i;
514 }
515
516 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
517 i = (mb->un.varRdConfig.max_xri >> 3);
518 if (phba->pport->cfg_lun_queue_depth > i) {
519 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
520 "3360 LUN queue depth changed from %d to %d\n",
521 phba->pport->cfg_lun_queue_depth, i);
522 phba->pport->cfg_lun_queue_depth = i;
523 }
dea31012005-04-17 16:05:31 -0500524
525 phba->lmt = mb->un.varRdConfig.lmt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500526
527 /* Get the default values for Model Name and Description */
528 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
529
James Smart2e0fef82007-06-17 19:56:36 -0500530 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500531
James Smart0b727fe2007-10-27 13:37:25 -0400532 /* Only process IOCBs on ELS ring till hba_state is READY */
James Smart895427b2017-02-12 13:52:30 -0800533 if (psli->sli3_ring[LPFC_EXTRA_RING].sli.sli3.cmdringaddr)
534 psli->sli3_ring[LPFC_EXTRA_RING].flag |= LPFC_STOP_IOCB_EVENT;
535 if (psli->sli3_ring[LPFC_FCP_RING].sli.sli3.cmdringaddr)
536 psli->sli3_ring[LPFC_FCP_RING].flag |= LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500537
538 /* Post receive buffers for desired rings */
James Smarted957682007-06-17 19:56:37 -0500539 if (phba->sli_rev != 3)
540 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500541
James Smart93996272008-08-24 21:50:30 -0400542 /*
543 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
544 */
545 if (phba->intr_type == MSIX) {
546 rc = lpfc_config_msi(phba, pmb);
547 if (rc) {
548 mempool_free(pmb, phba->mbox_mem_pool);
549 return -EIO;
550 }
551 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
552 if (rc != MBX_SUCCESS) {
553 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
554 "0352 Config MSI mailbox command "
555 "failed, mbxCmd x%x, mbxStatus x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400556 pmb->u.mb.mbxCommand,
557 pmb->u.mb.mbxStatus);
James Smart93996272008-08-24 21:50:30 -0400558 mempool_free(pmb, phba->mbox_mem_pool);
559 return -EIO;
560 }
561 }
562
James Smart04c68492009-05-22 14:52:52 -0400563 spin_lock_irq(&phba->hbalock);
James Smart93996272008-08-24 21:50:30 -0400564 /* Initialize ERATT handling flag */
565 phba->hba_flag &= ~HBA_ERATT_HANDLED;
566
dea31012005-04-17 16:05:31 -0500567 /* Enable appropriate host interrupts */
James Smart9940b972011-03-11 16:06:12 -0500568 if (lpfc_readl(phba->HCregaddr, &status)) {
569 spin_unlock_irq(&phba->hbalock);
570 return -EIO;
571 }
dea31012005-04-17 16:05:31 -0500572 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
573 if (psli->num_rings > 0)
574 status |= HC_R0INT_ENA;
575 if (psli->num_rings > 1)
576 status |= HC_R1INT_ENA;
577 if (psli->num_rings > 2)
578 status |= HC_R2INT_ENA;
579 if (psli->num_rings > 3)
580 status |= HC_R3INT_ENA;
581
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500582 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
583 (phba->cfg_poll & DISABLE_FCP_RING_INT))
James Smart93996272008-08-24 21:50:30 -0400584 status &= ~(HC_R0INT_ENA);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500585
dea31012005-04-17 16:05:31 -0500586 writel(status, phba->HCregaddr);
587 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500588 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500589
James Smart93996272008-08-24 21:50:30 -0400590 /* Set up ring-0 (ELS) timer */
591 timeout = phba->fc_ratov * 2;
James Smart256ec0d2013-04-17 20:14:58 -0400592 mod_timer(&vport->els_tmofunc,
593 jiffies + msecs_to_jiffies(1000 * timeout));
James Smart93996272008-08-24 21:50:30 -0400594 /* Set up heart beat (HB) timer */
James Smart256ec0d2013-04-17 20:14:58 -0400595 mod_timer(&phba->hb_tmofunc,
596 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -0500597 phba->hb_outstanding = 0;
598 phba->last_completion_time = jiffies;
James Smart93996272008-08-24 21:50:30 -0400599 /* Set up error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -0400600 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -0700601 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
dea31012005-04-17 16:05:31 -0500602
James Smarta0c87cb2009-07-19 10:01:10 -0400603 if (phba->hba_flag & LINK_DISABLED) {
604 lpfc_printf_log(phba,
605 KERN_ERR, LOG_INIT,
606 "2598 Adapter Link is disabled.\n");
607 lpfc_down_link(phba, pmb);
608 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
609 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
610 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
611 lpfc_printf_log(phba,
612 KERN_ERR, LOG_INIT,
613 "2599 Adapter failed to issue DOWN_LINK"
614 " mbox command rc 0x%x\n", rc);
615
616 mempool_free(pmb, phba->mbox_mem_pool);
617 return -EIO;
618 }
James Smarte40a02c2010-02-26 14:13:54 -0500619 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart026abb82011-12-13 13:20:45 -0500620 mempool_free(pmb, phba->mbox_mem_pool);
621 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
622 if (rc)
623 return rc;
dea31012005-04-17 16:05:31 -0500624 }
625 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400626 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500627 if (!pmb) {
628 phba->link_state = LPFC_HBA_ERROR;
629 return -ENOMEM;
630 }
631
James Smart57127f12007-10-27 13:37:05 -0400632 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
633 pmb->mbox_cmpl = lpfc_config_async_cmpl;
634 pmb->vport = phba->pport;
635 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500636
James Smart57127f12007-10-27 13:37:05 -0400637 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
638 lpfc_printf_log(phba,
639 KERN_ERR,
640 LOG_INIT,
641 "0456 Adapter failed to issue "
James Smarte4e74272009-07-19 10:01:38 -0400642 "ASYNCEVT_ENABLE mbox status x%x\n",
James Smart57127f12007-10-27 13:37:05 -0400643 rc);
644 mempool_free(pmb, phba->mbox_mem_pool);
645 }
James Smart97207482008-12-04 22:39:19 -0500646
647 /* Get Option rom version */
648 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500649 if (!pmb) {
650 phba->link_state = LPFC_HBA_ERROR;
651 return -ENOMEM;
652 }
653
James Smart97207482008-12-04 22:39:19 -0500654 lpfc_dump_wakeup_param(phba, pmb);
655 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
656 pmb->vport = phba->pport;
657 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
658
659 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
660 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
James Smarte4e74272009-07-19 10:01:38 -0400661 "to get Option ROM version status x%x\n", rc);
James Smart97207482008-12-04 22:39:19 -0500662 mempool_free(pmb, phba->mbox_mem_pool);
663 }
664
James Smartd7c255b2008-08-24 21:50:00 -0400665 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400666}
667
James Smarte59058c2008-08-24 21:49:00 -0400668/**
James Smart84d1b002010-02-12 14:42:33 -0500669 * lpfc_hba_init_link - Initialize the FC link
670 * @phba: pointer to lpfc hba data structure.
James Smart6e7288d2010-06-07 15:23:35 -0400671 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500672 *
673 * This routine will issue the INIT_LINK mailbox command call.
674 * It is available to other drivers through the lpfc_hba data
675 * structure for use as a delayed link up mechanism with the
676 * module parameter lpfc_suppress_link_up.
677 *
678 * Return code
679 * 0 - success
680 * Any other value - error
681 **/
Rashika Kheriae399b222014-09-03 12:55:28 -0400682static int
James Smart6e7288d2010-06-07 15:23:35 -0400683lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500684{
James Smart1b511972011-12-13 13:23:09 -0500685 return lpfc_hba_init_link_fc_topology(phba, phba->cfg_topology, flag);
686}
687
688/**
689 * lpfc_hba_init_link_fc_topology - Initialize FC link with desired topology
690 * @phba: pointer to lpfc hba data structure.
691 * @fc_topology: desired fc topology.
692 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
693 *
694 * This routine will issue the INIT_LINK mailbox command call.
695 * It is available to other drivers through the lpfc_hba data
696 * structure for use as a delayed link up mechanism with the
697 * module parameter lpfc_suppress_link_up.
698 *
699 * Return code
700 * 0 - success
701 * Any other value - error
702 **/
703int
704lpfc_hba_init_link_fc_topology(struct lpfc_hba *phba, uint32_t fc_topology,
705 uint32_t flag)
706{
James Smart84d1b002010-02-12 14:42:33 -0500707 struct lpfc_vport *vport = phba->pport;
708 LPFC_MBOXQ_t *pmb;
709 MAILBOX_t *mb;
710 int rc;
711
712 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
713 if (!pmb) {
714 phba->link_state = LPFC_HBA_ERROR;
715 return -ENOMEM;
716 }
717 mb = &pmb->u.mb;
718 pmb->vport = vport;
719
James Smart026abb82011-12-13 13:20:45 -0500720 if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_MAX) ||
721 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G) &&
722 !(phba->lmt & LMT_1Gb)) ||
723 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G) &&
724 !(phba->lmt & LMT_2Gb)) ||
725 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G) &&
726 !(phba->lmt & LMT_4Gb)) ||
727 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G) &&
728 !(phba->lmt & LMT_8Gb)) ||
729 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G) &&
730 !(phba->lmt & LMT_10Gb)) ||
731 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G) &&
James Smartd38dd522015-08-31 16:48:17 -0400732 !(phba->lmt & LMT_16Gb)) ||
733 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_32G) &&
734 !(phba->lmt & LMT_32Gb))) {
James Smart026abb82011-12-13 13:20:45 -0500735 /* Reset link speed to auto */
736 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
737 "1302 Invalid speed for this board:%d "
738 "Reset link speed to auto.\n",
739 phba->cfg_link_speed);
740 phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
741 }
James Smart1b511972011-12-13 13:23:09 -0500742 lpfc_init_link(phba, pmb, fc_topology, phba->cfg_link_speed);
James Smart84d1b002010-02-12 14:42:33 -0500743 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart1b511972011-12-13 13:23:09 -0500744 if (phba->sli_rev < LPFC_SLI_REV4)
745 lpfc_set_loopback_flag(phba);
James Smart6e7288d2010-06-07 15:23:35 -0400746 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart76a95d72010-11-20 23:11:48 -0500747 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
James Smart84d1b002010-02-12 14:42:33 -0500748 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
749 "0498 Adapter failed to init, mbxCmd x%x "
750 "INIT_LINK, mbxStatus x%x\n",
751 mb->mbxCommand, mb->mbxStatus);
James Smart76a95d72010-11-20 23:11:48 -0500752 if (phba->sli_rev <= LPFC_SLI_REV3) {
753 /* Clear all interrupt enable conditions */
754 writel(0, phba->HCregaddr);
755 readl(phba->HCregaddr); /* flush */
756 /* Clear all pending interrupts */
757 writel(0xffffffff, phba->HAregaddr);
758 readl(phba->HAregaddr); /* flush */
759 }
James Smart84d1b002010-02-12 14:42:33 -0500760 phba->link_state = LPFC_HBA_ERROR;
James Smart6e7288d2010-06-07 15:23:35 -0400761 if (rc != MBX_BUSY || flag == MBX_POLL)
James Smart84d1b002010-02-12 14:42:33 -0500762 mempool_free(pmb, phba->mbox_mem_pool);
763 return -EIO;
764 }
James Smarte40a02c2010-02-26 14:13:54 -0500765 phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
James Smart6e7288d2010-06-07 15:23:35 -0400766 if (flag == MBX_POLL)
767 mempool_free(pmb, phba->mbox_mem_pool);
James Smart84d1b002010-02-12 14:42:33 -0500768
769 return 0;
770}
771
772/**
773 * lpfc_hba_down_link - this routine downs the FC link
James Smart6e7288d2010-06-07 15:23:35 -0400774 * @phba: pointer to lpfc hba data structure.
775 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500776 *
777 * This routine will issue the DOWN_LINK mailbox command call.
778 * It is available to other drivers through the lpfc_hba data
779 * structure for use to stop the link.
780 *
781 * Return code
782 * 0 - success
783 * Any other value - error
784 **/
Rashika Kheriae399b222014-09-03 12:55:28 -0400785static int
James Smart6e7288d2010-06-07 15:23:35 -0400786lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500787{
788 LPFC_MBOXQ_t *pmb;
789 int rc;
790
791 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
792 if (!pmb) {
793 phba->link_state = LPFC_HBA_ERROR;
794 return -ENOMEM;
795 }
796
797 lpfc_printf_log(phba,
798 KERN_ERR, LOG_INIT,
799 "0491 Adapter Link is disabled.\n");
800 lpfc_down_link(phba, pmb);
801 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6e7288d2010-06-07 15:23:35 -0400802 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart84d1b002010-02-12 14:42:33 -0500803 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
804 lpfc_printf_log(phba,
805 KERN_ERR, LOG_INIT,
806 "2522 Adapter failed to issue DOWN_LINK"
807 " mbox command rc 0x%x\n", rc);
808
809 mempool_free(pmb, phba->mbox_mem_pool);
810 return -EIO;
811 }
James Smart6e7288d2010-06-07 15:23:35 -0400812 if (flag == MBX_POLL)
813 mempool_free(pmb, phba->mbox_mem_pool);
814
James Smart84d1b002010-02-12 14:42:33 -0500815 return 0;
816}
817
818/**
James Smart3621a712009-04-06 18:47:14 -0400819 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400820 * @phba: pointer to lpfc HBA data structure.
821 *
822 * This routine will do LPFC uninitialization before the HBA is reset when
823 * bringing down the SLI Layer.
824 *
825 * Return codes
826 * 0 - success.
827 * Any other value - error.
828 **/
dea31012005-04-17 16:05:31 -0500829int
James Smart2e0fef82007-06-17 19:56:36 -0500830lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500831{
James Smart1b32f6a2008-02-08 18:49:39 -0500832 struct lpfc_vport **vports;
833 int i;
James Smart3772a992009-05-22 14:50:54 -0400834
835 if (phba->sli_rev <= LPFC_SLI_REV3) {
836 /* Disable interrupts */
837 writel(0, phba->HCregaddr);
838 readl(phba->HCregaddr); /* flush */
839 }
dea31012005-04-17 16:05:31 -0500840
James Smart1b32f6a2008-02-08 18:49:39 -0500841 if (phba->pport->load_flag & FC_UNLOADING)
842 lpfc_cleanup_discovery_resources(phba->pport);
843 else {
844 vports = lpfc_create_vport_work_array(phba);
845 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400846 for (i = 0; i <= phba->max_vports &&
847 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500848 lpfc_cleanup_discovery_resources(vports[i]);
849 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500850 }
851 return 0;
dea31012005-04-17 16:05:31 -0500852}
853
James Smarte59058c2008-08-24 21:49:00 -0400854/**
James Smart68e814f2014-05-21 08:04:59 -0400855 * lpfc_sli4_free_sp_events - Cleanup sp_queue_events to free
856 * rspiocb which got deferred
857 *
858 * @phba: pointer to lpfc HBA data structure.
859 *
860 * This routine will cleanup completed slow path events after HBA is reset
861 * when bringing down the SLI Layer.
862 *
863 *
864 * Return codes
865 * void.
866 **/
867static void
868lpfc_sli4_free_sp_events(struct lpfc_hba *phba)
869{
870 struct lpfc_iocbq *rspiocbq;
871 struct hbq_dmabuf *dmabuf;
872 struct lpfc_cq_event *cq_event;
873
874 spin_lock_irq(&phba->hbalock);
875 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
876 spin_unlock_irq(&phba->hbalock);
877
878 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
879 /* Get the response iocb from the head of work queue */
880 spin_lock_irq(&phba->hbalock);
881 list_remove_head(&phba->sli4_hba.sp_queue_event,
882 cq_event, struct lpfc_cq_event, list);
883 spin_unlock_irq(&phba->hbalock);
884
885 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
886 case CQE_CODE_COMPL_WQE:
887 rspiocbq = container_of(cq_event, struct lpfc_iocbq,
888 cq_event);
889 lpfc_sli_release_iocbq(phba, rspiocbq);
890 break;
891 case CQE_CODE_RECEIVE:
892 case CQE_CODE_RECEIVE_V1:
893 dmabuf = container_of(cq_event, struct hbq_dmabuf,
894 cq_event);
895 lpfc_in_buf_free(phba, &dmabuf->dbuf);
896 }
897 }
898}
899
900/**
James Smartbcece5f2014-04-04 13:51:34 -0400901 * lpfc_hba_free_post_buf - Perform lpfc uninitialization after HBA reset
902 * @phba: pointer to lpfc HBA data structure.
903 *
904 * This routine will cleanup posted ELS buffers after the HBA is reset
905 * when bringing down the SLI Layer.
906 *
907 *
908 * Return codes
909 * void.
910 **/
911static void
912lpfc_hba_free_post_buf(struct lpfc_hba *phba)
913{
914 struct lpfc_sli *psli = &phba->sli;
915 struct lpfc_sli_ring *pring;
916 struct lpfc_dmabuf *mp, *next_mp;
James Smart07eab622014-04-04 13:51:44 -0400917 LIST_HEAD(buflist);
918 int count;
James Smartbcece5f2014-04-04 13:51:34 -0400919
920 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
921 lpfc_sli_hbqbuf_free_all(phba);
922 else {
923 /* Cleanup preposted buffers on the ELS ring */
James Smart895427b2017-02-12 13:52:30 -0800924 pring = &psli->sli3_ring[LPFC_ELS_RING];
James Smart07eab622014-04-04 13:51:44 -0400925 spin_lock_irq(&phba->hbalock);
926 list_splice_init(&pring->postbufq, &buflist);
927 spin_unlock_irq(&phba->hbalock);
928
929 count = 0;
930 list_for_each_entry_safe(mp, next_mp, &buflist, list) {
James Smartbcece5f2014-04-04 13:51:34 -0400931 list_del(&mp->list);
James Smart07eab622014-04-04 13:51:44 -0400932 count++;
James Smartbcece5f2014-04-04 13:51:34 -0400933 lpfc_mbuf_free(phba, mp->virt, mp->phys);
934 kfree(mp);
935 }
James Smart07eab622014-04-04 13:51:44 -0400936
937 spin_lock_irq(&phba->hbalock);
938 pring->postbufq_cnt -= count;
James Smartbcece5f2014-04-04 13:51:34 -0400939 spin_unlock_irq(&phba->hbalock);
940 }
941}
942
943/**
944 * lpfc_hba_clean_txcmplq - Perform lpfc uninitialization after HBA reset
945 * @phba: pointer to lpfc HBA data structure.
946 *
947 * This routine will cleanup the txcmplq after the HBA is reset when bringing
948 * down the SLI Layer.
949 *
950 * Return codes
951 * void
952 **/
953static void
954lpfc_hba_clean_txcmplq(struct lpfc_hba *phba)
955{
956 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -0800957 struct lpfc_queue *qp = NULL;
James Smartbcece5f2014-04-04 13:51:34 -0400958 struct lpfc_sli_ring *pring;
959 LIST_HEAD(completions);
960 int i;
James Smartc1dd9112018-01-30 15:58:57 -0800961 struct lpfc_iocbq *piocb, *next_iocb;
James Smartbcece5f2014-04-04 13:51:34 -0400962
James Smart895427b2017-02-12 13:52:30 -0800963 if (phba->sli_rev != LPFC_SLI_REV4) {
964 for (i = 0; i < psli->num_rings; i++) {
965 pring = &psli->sli3_ring[i];
James Smartbcece5f2014-04-04 13:51:34 -0400966 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -0800967 /* At this point in time the HBA is either reset or DOA
968 * Nothing should be on txcmplq as it will
969 * NEVER complete.
970 */
971 list_splice_init(&pring->txcmplq, &completions);
972 pring->txcmplq_cnt = 0;
James Smartbcece5f2014-04-04 13:51:34 -0400973 spin_unlock_irq(&phba->hbalock);
974
James Smart895427b2017-02-12 13:52:30 -0800975 lpfc_sli_abort_iocb_ring(phba, pring);
976 }
James Smartbcece5f2014-04-04 13:51:34 -0400977 /* Cancel all the IOCBs from the completions list */
James Smart895427b2017-02-12 13:52:30 -0800978 lpfc_sli_cancel_iocbs(phba, &completions,
979 IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
980 return;
981 }
982 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
983 pring = qp->pring;
984 if (!pring)
985 continue;
986 spin_lock_irq(&pring->ring_lock);
James Smartc1dd9112018-01-30 15:58:57 -0800987 list_for_each_entry_safe(piocb, next_iocb,
988 &pring->txcmplq, list)
989 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart895427b2017-02-12 13:52:30 -0800990 list_splice_init(&pring->txcmplq, &completions);
991 pring->txcmplq_cnt = 0;
992 spin_unlock_irq(&pring->ring_lock);
James Smartbcece5f2014-04-04 13:51:34 -0400993 lpfc_sli_abort_iocb_ring(phba, pring);
994 }
James Smart895427b2017-02-12 13:52:30 -0800995 /* Cancel all the IOCBs from the completions list */
996 lpfc_sli_cancel_iocbs(phba, &completions,
997 IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
James Smartbcece5f2014-04-04 13:51:34 -0400998}
999
1000/**
James Smart3772a992009-05-22 14:50:54 -04001001 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smartbcece5f2014-04-04 13:51:34 -04001002 int i;
James Smarte59058c2008-08-24 21:49:00 -04001003 * @phba: pointer to lpfc HBA data structure.
1004 *
1005 * This routine will do uninitialization after the HBA is reset when bring
1006 * down the SLI Layer.
1007 *
1008 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001009 * 0 - success.
James Smarte59058c2008-08-24 21:49:00 -04001010 * Any other value - error.
1011 **/
James Smart3772a992009-05-22 14:50:54 -04001012static int
1013lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05001014{
James Smartbcece5f2014-04-04 13:51:34 -04001015 lpfc_hba_free_post_buf(phba);
1016 lpfc_hba_clean_txcmplq(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001017 return 0;
1018}
James Smart5af5eee2010-10-22 11:06:38 -04001019
James Smartda0436e2009-05-22 14:51:39 -04001020/**
1021 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
1022 * @phba: pointer to lpfc HBA data structure.
1023 *
1024 * This routine will do uninitialization after the HBA is reset when bring
1025 * down the SLI Layer.
1026 *
1027 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001028 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001029 * Any other value - error.
1030 **/
1031static int
1032lpfc_hba_down_post_s4(struct lpfc_hba *phba)
1033{
1034 struct lpfc_scsi_buf *psb, *psb_next;
James Smart86c67372017-04-21 16:05:04 -07001035 struct lpfc_nvmet_rcv_ctx *ctxp, *ctxp_next;
James Smartda0436e2009-05-22 14:51:39 -04001036 LIST_HEAD(aborts);
James Smart895427b2017-02-12 13:52:30 -08001037 LIST_HEAD(nvme_aborts);
James Smart86c67372017-04-21 16:05:04 -07001038 LIST_HEAD(nvmet_aborts);
James Smartda0436e2009-05-22 14:51:39 -04001039 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -05001040 struct lpfc_sglq *sglq_entry = NULL;
James Smartcf1a1d32017-12-08 17:18:03 -08001041 int cnt;
James Smart0f65ff62010-02-26 14:14:23 -05001042
James Smart895427b2017-02-12 13:52:30 -08001043
1044 lpfc_sli_hbqbuf_free_all(phba);
James Smartbcece5f2014-04-04 13:51:34 -04001045 lpfc_hba_clean_txcmplq(phba);
1046
James Smartda0436e2009-05-22 14:51:39 -04001047 /* At this point in time the HBA is either reset or DOA. Either
1048 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
James Smart895427b2017-02-12 13:52:30 -08001049 * on the lpfc_els_sgl_list so that it can either be freed if the
James Smartda0436e2009-05-22 14:51:39 -04001050 * driver is unloading or reposted if the driver is restarting
1051 * the port.
1052 */
James Smart895427b2017-02-12 13:52:30 -08001053 spin_lock_irq(&phba->hbalock); /* required for lpfc_els_sgl_list and */
James Smartda0436e2009-05-22 14:51:39 -04001054 /* scsl_buf_list */
James Smart895427b2017-02-12 13:52:30 -08001055 /* sgl_list_lock required because worker thread uses this
James Smartda0436e2009-05-22 14:51:39 -04001056 * list.
1057 */
James Smart895427b2017-02-12 13:52:30 -08001058 spin_lock(&phba->sli4_hba.sgl_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -05001059 list_for_each_entry(sglq_entry,
1060 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
1061 sglq_entry->state = SGL_FREED;
1062
James Smartda0436e2009-05-22 14:51:39 -04001063 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
James Smart895427b2017-02-12 13:52:30 -08001064 &phba->sli4_hba.lpfc_els_sgl_list);
1065
James Smartf358dd02017-02-12 13:52:34 -08001066
James Smart895427b2017-02-12 13:52:30 -08001067 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04001068 /* abts_scsi_buf_list_lock required because worker thread uses this
1069 * list.
1070 */
James Smart895427b2017-02-12 13:52:30 -08001071 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
1072 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
1073 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
1074 &aborts);
1075 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
1076 }
1077
1078 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
1079 spin_lock(&phba->sli4_hba.abts_nvme_buf_list_lock);
1080 list_splice_init(&phba->sli4_hba.lpfc_abts_nvme_buf_list,
1081 &nvme_aborts);
James Smart86c67372017-04-21 16:05:04 -07001082 list_splice_init(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
1083 &nvmet_aborts);
James Smart895427b2017-02-12 13:52:30 -08001084 spin_unlock(&phba->sli4_hba.abts_nvme_buf_list_lock);
1085 }
1086
James Smartda0436e2009-05-22 14:51:39 -04001087 spin_unlock_irq(&phba->hbalock);
1088
1089 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
1090 psb->pCmd = NULL;
1091 psb->status = IOSTAT_SUCCESS;
1092 }
James Smarta40fc5f2013-04-17 20:17:40 -04001093 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
1094 list_splice(&aborts, &phba->lpfc_scsi_buf_list_put);
1095 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
James Smart68e814f2014-05-21 08:04:59 -04001096
James Smart86c67372017-04-21 16:05:04 -07001097 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smartcf1a1d32017-12-08 17:18:03 -08001098 cnt = 0;
James Smart86c67372017-04-21 16:05:04 -07001099 list_for_each_entry_safe(psb, psb_next, &nvme_aborts, list) {
1100 psb->pCmd = NULL;
1101 psb->status = IOSTAT_SUCCESS;
James Smartcf1a1d32017-12-08 17:18:03 -08001102 cnt++;
James Smart86c67372017-04-21 16:05:04 -07001103 }
1104 spin_lock_irqsave(&phba->nvme_buf_list_put_lock, iflag);
James Smartcf1a1d32017-12-08 17:18:03 -08001105 phba->put_nvme_bufs += cnt;
James Smart86c67372017-04-21 16:05:04 -07001106 list_splice(&nvme_aborts, &phba->lpfc_nvme_buf_list_put);
1107 spin_unlock_irqrestore(&phba->nvme_buf_list_put_lock, iflag);
1108
1109 list_for_each_entry_safe(ctxp, ctxp_next, &nvmet_aborts, list) {
1110 ctxp->flag &= ~(LPFC_NVMET_XBUSY | LPFC_NVMET_ABORT_OP);
James Smart6c621a22017-05-15 15:20:45 -07001111 lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
James Smart86c67372017-04-21 16:05:04 -07001112 }
James Smart895427b2017-02-12 13:52:30 -08001113 }
James Smart895427b2017-02-12 13:52:30 -08001114
James Smart68e814f2014-05-21 08:04:59 -04001115 lpfc_sli4_free_sp_events(phba);
James Smartda0436e2009-05-22 14:51:39 -04001116 return 0;
1117}
1118
1119/**
1120 * lpfc_hba_down_post - Wrapper func for hba down post routine
1121 * @phba: pointer to lpfc HBA data structure.
1122 *
1123 * This routine wraps the actual SLI3 or SLI4 routine for performing
1124 * uninitialization after the HBA is reset when bring down the SLI Layer.
1125 *
1126 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001127 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001128 * Any other value - error.
1129 **/
1130int
1131lpfc_hba_down_post(struct lpfc_hba *phba)
1132{
1133 return (*phba->lpfc_hba_down_post)(phba);
1134}
Jamie Wellnitz41415862006-02-28 19:25:27 -05001135
James Smarte59058c2008-08-24 21:49:00 -04001136/**
James Smart3621a712009-04-06 18:47:14 -04001137 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001138 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1139 *
1140 * This is the HBA-timer timeout handler registered to the lpfc driver. When
1141 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
1142 * work-port-events bitmap and the worker thread is notified. This timeout
1143 * event will be used by the worker thread to invoke the actual timeout
1144 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
1145 * be performed in the timeout handler and the HBA timeout event bit shall
1146 * be cleared by the worker thread after it has taken the event bitmap out.
1147 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001148static void
Kees Cookf22eb4d2017-09-06 20:24:26 -07001149lpfc_hb_timeout(struct timer_list *t)
James Smart858c9f62007-06-17 19:56:39 -05001150{
1151 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -04001152 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -05001153 unsigned long iflag;
1154
Kees Cookf22eb4d2017-09-06 20:24:26 -07001155 phba = from_timer(phba, t, hb_tmofunc);
James Smart93996272008-08-24 21:50:30 -04001156
1157 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -05001158 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04001159 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
1160 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -05001161 phba->pport->work_port_events |= WORKER_HB_TMO;
1162 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
1163
James Smart93996272008-08-24 21:50:30 -04001164 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -04001165 if (!tmo_posted)
1166 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -05001167 return;
1168}
1169
James Smarte59058c2008-08-24 21:49:00 -04001170/**
James Smart19ca7602010-11-20 23:11:55 -05001171 * lpfc_rrq_timeout - The RRQ-timer timeout handler
1172 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1173 *
1174 * This is the RRQ-timer timeout handler registered to the lpfc driver. When
1175 * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
1176 * work-port-events bitmap and the worker thread is notified. This timeout
1177 * event will be used by the worker thread to invoke the actual timeout
1178 * handler routine, lpfc_rrq_handler. Any periodical operations will
1179 * be performed in the timeout handler and the RRQ timeout event bit shall
1180 * be cleared by the worker thread after it has taken the event bitmap out.
1181 **/
1182static void
Kees Cookf22eb4d2017-09-06 20:24:26 -07001183lpfc_rrq_timeout(struct timer_list *t)
James Smart19ca7602010-11-20 23:11:55 -05001184{
1185 struct lpfc_hba *phba;
James Smart19ca7602010-11-20 23:11:55 -05001186 unsigned long iflag;
1187
Kees Cookf22eb4d2017-09-06 20:24:26 -07001188 phba = from_timer(phba, t, rrq_tmr);
James Smart19ca7602010-11-20 23:11:55 -05001189 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001190 if (!(phba->pport->load_flag & FC_UNLOADING))
1191 phba->hba_flag |= HBA_RRQ_ACTIVE;
1192 else
1193 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart19ca7602010-11-20 23:11:55 -05001194 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001195
1196 if (!(phba->pport->load_flag & FC_UNLOADING))
1197 lpfc_worker_wake_up(phba);
James Smart19ca7602010-11-20 23:11:55 -05001198}
1199
1200/**
James Smart3621a712009-04-06 18:47:14 -04001201 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -04001202 * @phba: pointer to lpfc hba data structure.
1203 * @pmboxq: pointer to the driver internal queue element for mailbox command.
1204 *
1205 * This is the callback function to the lpfc heart-beat mailbox command.
1206 * If configured, the lpfc driver issues the heart-beat mailbox command to
1207 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
1208 * heart-beat mailbox command is issued, the driver shall set up heart-beat
1209 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
1210 * heart-beat outstanding state. Once the mailbox command comes back and
1211 * no error conditions detected, the heart-beat mailbox command timer is
1212 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1213 * state is cleared for the next heart-beat. If the timer expired with the
1214 * heart-beat outstanding state set, the driver will put the HBA offline.
1215 **/
James Smart858c9f62007-06-17 19:56:39 -05001216static void
1217lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1218{
1219 unsigned long drvr_flag;
1220
1221 spin_lock_irqsave(&phba->hbalock, drvr_flag);
1222 phba->hb_outstanding = 0;
1223 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1224
James Smart93996272008-08-24 21:50:30 -04001225 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -05001226 mempool_free(pmboxq, phba->mbox_mem_pool);
1227 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1228 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -04001229 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -05001230 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001231 jiffies +
1232 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001233 return;
1234}
1235
James Smarte59058c2008-08-24 21:49:00 -04001236/**
James Smart3621a712009-04-06 18:47:14 -04001237 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001238 * @phba: pointer to lpfc hba data structure.
1239 *
1240 * This is the actual HBA-timer timeout handler to be invoked by the worker
1241 * thread whenever the HBA timer fired and HBA-timeout event posted. This
1242 * handler performs any periodic operations needed for the device. If such
1243 * periodic event has already been attended to either in the interrupt handler
1244 * or by processing slow-ring or fast-ring events within the HBA-timer
1245 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1246 * the timer for the next timeout period. If lpfc heart-beat mailbox command
1247 * is configured and there is no heart-beat mailbox command outstanding, a
1248 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1249 * has been a heart-beat mailbox command outstanding, the HBA shall be put
1250 * to offline.
1251 **/
James Smart858c9f62007-06-17 19:56:39 -05001252void
1253lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1254{
James Smart45ed1192009-10-02 15:17:02 -04001255 struct lpfc_vport **vports;
James Smart858c9f62007-06-17 19:56:39 -05001256 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -05001257 struct lpfc_dmabuf *buf_ptr;
James Smart45ed1192009-10-02 15:17:02 -04001258 int retval, i;
James Smart858c9f62007-06-17 19:56:39 -05001259 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -05001260 LIST_HEAD(completions);
James Smart0cf07f842017-06-01 21:07:10 -07001261 struct lpfc_queue *qp;
1262 unsigned long time_elapsed;
1263 uint32_t tick_cqe, max_cqe, val;
1264 uint64_t tot, data1, data2, data3;
Dick Kennedy66d7ce92017-08-23 16:55:42 -07001265 struct lpfc_nvmet_tgtport *tgtp;
James Smart0cf07f842017-06-01 21:07:10 -07001266 struct lpfc_register reg_data;
1267 void __iomem *eqdreg = phba->sli4_hba.u.if_type2.EQDregaddr;
James Smart858c9f62007-06-17 19:56:39 -05001268
James Smart45ed1192009-10-02 15:17:02 -04001269 vports = lpfc_create_vport_work_array(phba);
1270 if (vports != NULL)
James Smart4258e982015-12-16 18:11:58 -05001271 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart45ed1192009-10-02 15:17:02 -04001272 lpfc_rcv_seq_check_edtov(vports[i]);
James Smart4258e982015-12-16 18:11:58 -05001273 lpfc_fdmi_num_disc_check(vports[i]);
1274 }
James Smart45ed1192009-10-02 15:17:02 -04001275 lpfc_destroy_vport_work_array(phba, vports);
1276
James Smart858c9f62007-06-17 19:56:39 -05001277 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -04001278 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -05001279 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1280 return;
1281
James Smart0cf07f842017-06-01 21:07:10 -07001282 if (phba->cfg_auto_imax) {
1283 if (!phba->last_eqdelay_time) {
1284 phba->last_eqdelay_time = jiffies;
1285 goto skip_eqdelay;
1286 }
1287 time_elapsed = jiffies - phba->last_eqdelay_time;
1288 phba->last_eqdelay_time = jiffies;
1289
1290 tot = 0xffff;
1291 /* Check outstanding IO count */
1292 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
1293 if (phba->nvmet_support) {
Dick Kennedy66d7ce92017-08-23 16:55:42 -07001294 tgtp = phba->targetport->private;
1295 /* Calculate outstanding IOs */
1296 tot = atomic_read(&tgtp->rcv_fcp_cmd_drop);
1297 tot += atomic_read(&tgtp->xmt_fcp_release);
1298 tot = atomic_read(&tgtp->rcv_fcp_cmd_in) - tot;
James Smart0cf07f842017-06-01 21:07:10 -07001299 } else {
1300 tot = atomic_read(&phba->fc4NvmeIoCmpls);
1301 data1 = atomic_read(
1302 &phba->fc4NvmeInputRequests);
1303 data2 = atomic_read(
1304 &phba->fc4NvmeOutputRequests);
1305 data3 = atomic_read(
1306 &phba->fc4NvmeControlRequests);
1307 tot = (data1 + data2 + data3) - tot;
1308 }
1309 }
1310
1311 /* Interrupts per sec per EQ */
1312 val = phba->cfg_fcp_imax / phba->io_channel_irqs;
1313 tick_cqe = val / CONFIG_HZ; /* Per tick per EQ */
1314
1315 /* Assume 1 CQE/ISR, calc max CQEs allowed for time duration */
1316 max_cqe = time_elapsed * tick_cqe;
1317
1318 for (i = 0; i < phba->io_channel_irqs; i++) {
1319 /* Fast-path EQ */
1320 qp = phba->sli4_hba.hba_eq[i];
1321 if (!qp)
1322 continue;
1323
1324 /* Use no EQ delay if we don't have many outstanding
1325 * IOs, or if we are only processing 1 CQE/ISR or less.
1326 * Otherwise, assume we can process up to lpfc_fcp_imax
1327 * interrupts per HBA.
1328 */
1329 if (tot < LPFC_NODELAY_MAX_IO ||
1330 qp->EQ_cqe_cnt <= max_cqe)
1331 val = 0;
1332 else
1333 val = phba->cfg_fcp_imax;
1334
1335 if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
1336 /* Use EQ Delay Register method */
1337
1338 /* Convert for EQ Delay register */
1339 if (val) {
1340 /* First, interrupts per sec per EQ */
1341 val = phba->cfg_fcp_imax /
1342 phba->io_channel_irqs;
1343
1344 /* us delay between each interrupt */
1345 val = LPFC_SEC_TO_USEC / val;
1346 }
1347 if (val != qp->q_mode) {
1348 reg_data.word0 = 0;
1349 bf_set(lpfc_sliport_eqdelay_id,
1350 &reg_data, qp->queue_id);
1351 bf_set(lpfc_sliport_eqdelay_delay,
1352 &reg_data, val);
1353 writel(reg_data.word0, eqdreg);
1354 }
1355 } else {
1356 /* Use mbox command method */
1357 if (val != qp->q_mode)
1358 lpfc_modify_hba_eq_delay(phba, i,
1359 1, val);
1360 }
1361
1362 /*
1363 * val is cfg_fcp_imax or 0 for mbox delay or us delay
1364 * between interrupts for EQDR.
1365 */
1366 qp->q_mode = val;
1367 qp->EQ_cqe_cnt = 0;
1368 }
1369 }
1370
1371skip_eqdelay:
James Smart858c9f62007-06-17 19:56:39 -05001372 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -05001373
James Smart256ec0d2013-04-17 20:14:58 -04001374 if (time_after(phba->last_completion_time +
1375 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL),
1376 jiffies)) {
James Smart858c9f62007-06-17 19:56:39 -05001377 spin_unlock_irq(&phba->pport->work_port_lock);
1378 if (!phba->hb_outstanding)
1379 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001380 jiffies +
1381 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001382 else
1383 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001384 jiffies +
1385 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001386 return;
1387 }
1388 spin_unlock_irq(&phba->pport->work_port_lock);
1389
James Smart0ff10d42008-01-11 01:52:36 -05001390 if (phba->elsbuf_cnt &&
1391 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1392 spin_lock_irq(&phba->hbalock);
1393 list_splice_init(&phba->elsbuf, &completions);
1394 phba->elsbuf_cnt = 0;
1395 phba->elsbuf_prev_cnt = 0;
1396 spin_unlock_irq(&phba->hbalock);
1397
1398 while (!list_empty(&completions)) {
1399 list_remove_head(&completions, buf_ptr,
1400 struct lpfc_dmabuf, list);
1401 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1402 kfree(buf_ptr);
1403 }
1404 }
1405 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1406
James Smart858c9f62007-06-17 19:56:39 -05001407 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -05001408 if (phba->cfg_enable_hba_heartbeat) {
1409 if (!phba->hb_outstanding) {
James Smartbc739052010-08-04 16:11:18 -04001410 if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1411 (list_empty(&psli->mboxq))) {
1412 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1413 GFP_KERNEL);
1414 if (!pmboxq) {
1415 mod_timer(&phba->hb_tmofunc,
1416 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001417 msecs_to_jiffies(1000 *
1418 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001419 return;
1420 }
James Smart13815c82008-01-11 01:52:48 -05001421
James Smartbc739052010-08-04 16:11:18 -04001422 lpfc_heart_beat(phba, pmboxq);
1423 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1424 pmboxq->vport = phba->pport;
1425 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1426 MBX_NOWAIT);
James Smart13815c82008-01-11 01:52:48 -05001427
James Smartbc739052010-08-04 16:11:18 -04001428 if (retval != MBX_BUSY &&
1429 retval != MBX_SUCCESS) {
1430 mempool_free(pmboxq,
1431 phba->mbox_mem_pool);
1432 mod_timer(&phba->hb_tmofunc,
1433 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001434 msecs_to_jiffies(1000 *
1435 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001436 return;
1437 }
1438 phba->skipped_hb = 0;
1439 phba->hb_outstanding = 1;
1440 } else if (time_before_eq(phba->last_completion_time,
1441 phba->skipped_hb)) {
1442 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1443 "2857 Last completion time not "
1444 " updated in %d ms\n",
1445 jiffies_to_msecs(jiffies
1446 - phba->last_completion_time));
1447 } else
1448 phba->skipped_hb = jiffies;
1449
James Smart858c9f62007-06-17 19:56:39 -05001450 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001451 jiffies +
1452 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001453 return;
James Smart13815c82008-01-11 01:52:48 -05001454 } else {
1455 /*
1456 * If heart beat timeout called with hb_outstanding set
James Smartdcf2a4e2010-09-29 11:18:53 -04001457 * we need to give the hb mailbox cmd a chance to
1458 * complete or TMO.
James Smart13815c82008-01-11 01:52:48 -05001459 */
James Smartdcf2a4e2010-09-29 11:18:53 -04001460 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1461 "0459 Adapter heartbeat still out"
1462 "standing:last compl time was %d ms.\n",
1463 jiffies_to_msecs(jiffies
1464 - phba->last_completion_time));
1465 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001466 jiffies +
1467 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001468 }
James Smart4258e982015-12-16 18:11:58 -05001469 } else {
1470 mod_timer(&phba->hb_tmofunc,
1471 jiffies +
1472 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001473 }
1474}
1475
James Smarte59058c2008-08-24 21:49:00 -04001476/**
James Smart3621a712009-04-06 18:47:14 -04001477 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -04001478 * @phba: pointer to lpfc hba data structure.
1479 *
1480 * This routine is called to bring the HBA offline when HBA hardware error
1481 * other than Port Error 6 has been detected.
1482 **/
James Smart09372822008-01-11 01:52:54 -05001483static void
1484lpfc_offline_eratt(struct lpfc_hba *phba)
1485{
1486 struct lpfc_sli *psli = &phba->sli;
1487
1488 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001489 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -05001490 spin_unlock_irq(&phba->hbalock);
James Smart618a5232012-06-12 13:54:36 -04001491 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart09372822008-01-11 01:52:54 -05001492
1493 lpfc_offline(phba);
1494 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001495 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001496 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001497 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001498 lpfc_hba_down_post(phba);
1499 lpfc_sli_brdready(phba, HS_MBRDY);
1500 lpfc_unblock_mgmt_io(phba);
1501 phba->link_state = LPFC_HBA_ERROR;
1502 return;
1503}
1504
James Smarte59058c2008-08-24 21:49:00 -04001505/**
James Smartda0436e2009-05-22 14:51:39 -04001506 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1507 * @phba: pointer to lpfc hba data structure.
1508 *
1509 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1510 * other than Port Error 6 has been detected.
1511 **/
James Smarta88dbb62013-04-17 20:18:39 -04001512void
James Smartda0436e2009-05-22 14:51:39 -04001513lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1514{
James Smart946727d2015-04-07 15:07:09 -04001515 spin_lock_irq(&phba->hbalock);
1516 phba->link_state = LPFC_HBA_ERROR;
1517 spin_unlock_irq(&phba->hbalock);
1518
James Smart618a5232012-06-12 13:54:36 -04001519 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smartda0436e2009-05-22 14:51:39 -04001520 lpfc_offline(phba);
James Smartda0436e2009-05-22 14:51:39 -04001521 lpfc_hba_down_post(phba);
James Smartda0436e2009-05-22 14:51:39 -04001522 lpfc_unblock_mgmt_io(phba);
James Smartda0436e2009-05-22 14:51:39 -04001523}
1524
1525/**
James Smarta257bf92009-04-06 18:48:10 -04001526 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1527 * @phba: pointer to lpfc hba data structure.
1528 *
1529 * This routine is invoked to handle the deferred HBA hardware error
1530 * conditions. This type of error is indicated by HBA by setting ER1
1531 * and another ER bit in the host status register. The driver will
1532 * wait until the ER1 bit clears before handling the error condition.
1533 **/
1534static void
1535lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1536{
1537 uint32_t old_host_status = phba->work_hs;
James Smarta257bf92009-04-06 18:48:10 -04001538 struct lpfc_sli *psli = &phba->sli;
1539
James Smartf4b4c682009-05-22 14:53:12 -04001540 /* If the pci channel is offline, ignore possible errors,
1541 * since we cannot communicate with the pci card anyway.
1542 */
1543 if (pci_channel_offline(phba->pcidev)) {
1544 spin_lock_irq(&phba->hbalock);
1545 phba->hba_flag &= ~DEFER_ERATT;
1546 spin_unlock_irq(&phba->hbalock);
1547 return;
1548 }
1549
James Smarta257bf92009-04-06 18:48:10 -04001550 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1551 "0479 Deferred Adapter Hardware Error "
1552 "Data: x%x x%x x%x\n",
1553 phba->work_hs,
1554 phba->work_status[0], phba->work_status[1]);
1555
1556 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001557 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001558 spin_unlock_irq(&phba->hbalock);
1559
1560
1561 /*
1562 * Firmware stops when it triggred erratt. That could cause the I/Os
1563 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1564 * SCSI layer retry it after re-establishing link.
1565 */
James Smartdb55fba2014-04-04 13:52:02 -04001566 lpfc_sli_abort_fcp_rings(phba);
James Smarta257bf92009-04-06 18:48:10 -04001567
1568 /*
1569 * There was a firmware error. Take the hba offline and then
1570 * attempt to restart it.
1571 */
James Smart618a5232012-06-12 13:54:36 -04001572 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smarta257bf92009-04-06 18:48:10 -04001573 lpfc_offline(phba);
1574
1575 /* Wait for the ER1 bit to clear.*/
1576 while (phba->work_hs & HS_FFER1) {
1577 msleep(100);
James Smart9940b972011-03-11 16:06:12 -05001578 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1579 phba->work_hs = UNPLUG_ERR ;
1580 break;
1581 }
James Smarta257bf92009-04-06 18:48:10 -04001582 /* If driver is unloading let the worker thread continue */
1583 if (phba->pport->load_flag & FC_UNLOADING) {
1584 phba->work_hs = 0;
1585 break;
1586 }
1587 }
1588
1589 /*
1590 * This is to ptrotect against a race condition in which
1591 * first write to the host attention register clear the
1592 * host status register.
1593 */
1594 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1595 phba->work_hs = old_host_status & ~HS_FFER1;
1596
James Smart3772a992009-05-22 14:50:54 -04001597 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001598 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001599 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001600 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1601 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1602}
1603
James Smart3772a992009-05-22 14:50:54 -04001604static void
1605lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1606{
1607 struct lpfc_board_event_header board_event;
1608 struct Scsi_Host *shost;
1609
1610 board_event.event_type = FC_REG_BOARD_EVENT;
1611 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1612 shost = lpfc_shost_from_vport(phba->pport);
1613 fc_host_post_vendor_event(shost, fc_get_event_number(),
1614 sizeof(board_event),
1615 (char *) &board_event,
1616 LPFC_NL_VENDOR_ID);
1617}
1618
James Smarta257bf92009-04-06 18:48:10 -04001619/**
James Smart3772a992009-05-22 14:50:54 -04001620 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001621 * @phba: pointer to lpfc hba data structure.
1622 *
1623 * This routine is invoked to handle the following HBA hardware error
1624 * conditions:
1625 * 1 - HBA error attention interrupt
1626 * 2 - DMA ring index out of range
1627 * 3 - Mailbox command came back as unknown
1628 **/
James Smart3772a992009-05-22 14:50:54 -04001629static void
1630lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001631{
James Smart2e0fef82007-06-17 19:56:36 -05001632 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001633 struct lpfc_sli *psli = &phba->sli;
James Smartd2873e42006-08-18 17:46:43 -04001634 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001635 unsigned long temperature;
1636 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001637 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001638
Linas Vepstas8d63f372007-02-14 14:28:36 -06001639 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001640 * since we cannot communicate with the pci card anyway.
1641 */
1642 if (pci_channel_offline(phba->pcidev)) {
1643 spin_lock_irq(&phba->hbalock);
1644 phba->hba_flag &= ~DEFER_ERATT;
1645 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001646 return;
James Smart3772a992009-05-22 14:50:54 -04001647 }
1648
James Smart13815c82008-01-11 01:52:48 -05001649 /* If resets are disabled then leave the HBA alone and return */
1650 if (!phba->cfg_enable_hba_reset)
1651 return;
dea31012005-04-17 16:05:31 -05001652
James Smartea2151b2008-09-07 11:52:10 -04001653 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001654 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001655
James Smarta257bf92009-04-06 18:48:10 -04001656 if (phba->hba_flag & DEFER_ERATT)
1657 lpfc_handle_deferred_eratt(phba);
1658
James Smartdcf2a4e2010-09-29 11:18:53 -04001659 if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1660 if (phba->work_hs & HS_FFER6)
1661 /* Re-establishing Link */
1662 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1663 "1301 Re-establishing Link "
1664 "Data: x%x x%x x%x\n",
1665 phba->work_hs, phba->work_status[0],
1666 phba->work_status[1]);
1667 if (phba->work_hs & HS_FFER8)
1668 /* Device Zeroization */
1669 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1670 "2861 Host Authentication device "
1671 "zeroization Data:x%x x%x x%x\n",
1672 phba->work_hs, phba->work_status[0],
1673 phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001674
James Smart92d7f7b2007-06-17 19:56:38 -05001675 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001676 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001677 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001678
1679 /*
1680 * Firmware stops when it triggled erratt with HS_FFER6.
1681 * That could cause the I/Os dropped by the firmware.
1682 * Error iocb (I/O) on txcmplq and let the SCSI layer
1683 * retry it after re-establishing link.
1684 */
James Smartdb55fba2014-04-04 13:52:02 -04001685 lpfc_sli_abort_fcp_rings(phba);
dea31012005-04-17 16:05:31 -05001686
dea31012005-04-17 16:05:31 -05001687 /*
1688 * There was a firmware error. Take the hba offline and then
1689 * attempt to restart it.
1690 */
James Smart618a5232012-06-12 13:54:36 -04001691 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
dea31012005-04-17 16:05:31 -05001692 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001693 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001694 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001695 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001696 return;
1697 }
James Smart46fa3112007-04-25 09:51:45 -04001698 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001699 } else if (phba->work_hs & HS_CRIT_TEMP) {
1700 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1701 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1702 temp_event_data.event_code = LPFC_CRIT_TEMP;
1703 temp_event_data.data = (uint32_t)temperature;
1704
1705 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001706 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001707 "(%ld), taking this port offline "
1708 "Data: x%x x%x x%x\n",
1709 temperature, phba->work_hs,
1710 phba->work_status[0], phba->work_status[1]);
1711
1712 shost = lpfc_shost_from_vport(phba->pport);
1713 fc_host_post_vendor_event(shost, fc_get_event_number(),
1714 sizeof(temp_event_data),
1715 (char *) &temp_event_data,
1716 SCSI_NL_VID_TYPE_PCI
1717 | PCI_VENDOR_ID_EMULEX);
1718
James Smart7af67052007-10-27 13:38:11 -04001719 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001720 phba->over_temp_state = HBA_OVER_TEMP;
1721 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001722 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001723
dea31012005-04-17 16:05:31 -05001724 } else {
1725 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001726 * failure is a value other than FFER6. Do not call the offline
1727 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001728 */
1729 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001730 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001731 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001732 phba->work_hs,
dea31012005-04-17 16:05:31 -05001733 phba->work_status[0], phba->work_status[1]);
1734
James Smartd2873e42006-08-18 17:46:43 -04001735 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001736 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001737 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001738 sizeof(event_data), (char *) &event_data,
1739 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1740
James Smart09372822008-01-11 01:52:54 -05001741 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001742 }
James Smart93996272008-08-24 21:50:30 -04001743 return;
dea31012005-04-17 16:05:31 -05001744}
1745
James Smarte59058c2008-08-24 21:49:00 -04001746/**
James Smart618a5232012-06-12 13:54:36 -04001747 * lpfc_sli4_port_sta_fn_reset - The SLI4 function reset due to port status reg
1748 * @phba: pointer to lpfc hba data structure.
1749 * @mbx_action: flag for mailbox shutdown action.
1750 *
1751 * This routine is invoked to perform an SLI4 port PCI function reset in
1752 * response to port status register polling attention. It waits for port
1753 * status register (ERR, RDY, RN) bits before proceeding with function reset.
1754 * During this process, interrupt vectors are freed and later requested
1755 * for handling possible port resource change.
1756 **/
1757static int
James Smarte10b2022014-02-20 09:57:43 -05001758lpfc_sli4_port_sta_fn_reset(struct lpfc_hba *phba, int mbx_action,
1759 bool en_rn_msg)
James Smart618a5232012-06-12 13:54:36 -04001760{
1761 int rc;
1762 uint32_t intr_mode;
1763
James Smart65791f12016-07-06 12:35:56 -07001764 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
1765 LPFC_SLI_INTF_IF_TYPE_2) {
1766 /*
1767 * On error status condition, driver need to wait for port
1768 * ready before performing reset.
1769 */
1770 rc = lpfc_sli4_pdev_status_reg_wait(phba);
James Smart0e916ee2016-07-06 12:36:06 -07001771 if (rc)
James Smart65791f12016-07-06 12:35:56 -07001772 return rc;
James Smart618a5232012-06-12 13:54:36 -04001773 }
James Smart0e916ee2016-07-06 12:36:06 -07001774
James Smart65791f12016-07-06 12:35:56 -07001775 /* need reset: attempt for port recovery */
1776 if (en_rn_msg)
1777 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1778 "2887 Reset Needed: Attempting Port "
1779 "Recovery...\n");
1780 lpfc_offline_prep(phba, mbx_action);
1781 lpfc_offline(phba);
1782 /* release interrupt for possible resource change */
1783 lpfc_sli4_disable_intr(phba);
1784 lpfc_sli_brdrestart(phba);
1785 /* request and enable interrupt */
1786 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
1787 if (intr_mode == LPFC_INTR_ERROR) {
1788 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1789 "3175 Failed to enable interrupt\n");
1790 return -EIO;
1791 }
1792 phba->intr_mode = intr_mode;
1793 rc = lpfc_online(phba);
1794 if (rc == 0)
1795 lpfc_unblock_mgmt_io(phba);
1796
James Smart618a5232012-06-12 13:54:36 -04001797 return rc;
1798}
1799
1800/**
James Smartda0436e2009-05-22 14:51:39 -04001801 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1802 * @phba: pointer to lpfc hba data structure.
1803 *
1804 * This routine is invoked to handle the SLI4 HBA hardware error attention
1805 * conditions.
1806 **/
1807static void
1808lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1809{
1810 struct lpfc_vport *vport = phba->pport;
1811 uint32_t event_data;
1812 struct Scsi_Host *shost;
James Smart2fcee4b2010-12-15 17:57:46 -05001813 uint32_t if_type;
James Smart2e90f4b2011-12-13 13:22:37 -05001814 struct lpfc_register portstat_reg = {0};
1815 uint32_t reg_err1, reg_err2;
1816 uint32_t uerrlo_reg, uemasklo_reg;
James Smart65791f12016-07-06 12:35:56 -07001817 uint32_t smphr_port_status = 0, pci_rd_rc1, pci_rd_rc2;
James Smarte10b2022014-02-20 09:57:43 -05001818 bool en_rn_msg = true;
James Smart946727d2015-04-07 15:07:09 -04001819 struct temp_event temp_event_data;
James Smart65791f12016-07-06 12:35:56 -07001820 struct lpfc_register portsmphr_reg;
1821 int rc, i;
James Smartda0436e2009-05-22 14:51:39 -04001822
1823 /* If the pci channel is offline, ignore possible errors, since
1824 * we cannot communicate with the pci card anyway.
1825 */
1826 if (pci_channel_offline(phba->pcidev))
1827 return;
James Smartda0436e2009-05-22 14:51:39 -04001828
James Smart65791f12016-07-06 12:35:56 -07001829 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
James Smart2fcee4b2010-12-15 17:57:46 -05001830 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1831 switch (if_type) {
1832 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart2e90f4b2011-12-13 13:22:37 -05001833 pci_rd_rc1 = lpfc_readl(
1834 phba->sli4_hba.u.if_type0.UERRLOregaddr,
1835 &uerrlo_reg);
1836 pci_rd_rc2 = lpfc_readl(
1837 phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
1838 &uemasklo_reg);
1839 /* consider PCI bus read error as pci_channel_offline */
1840 if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
1841 return;
James Smart65791f12016-07-06 12:35:56 -07001842 if (!(phba->hba_flag & HBA_RECOVERABLE_UE)) {
1843 lpfc_sli4_offline_eratt(phba);
1844 return;
1845 }
1846 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1847 "7623 Checking UE recoverable");
1848
1849 for (i = 0; i < phba->sli4_hba.ue_to_sr / 1000; i++) {
1850 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1851 &portsmphr_reg.word0))
1852 continue;
1853
1854 smphr_port_status = bf_get(lpfc_port_smphr_port_status,
1855 &portsmphr_reg);
1856 if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1857 LPFC_PORT_SEM_UE_RECOVERABLE)
1858 break;
1859 /*Sleep for 1Sec, before checking SEMAPHORE */
1860 msleep(1000);
1861 }
1862
1863 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1864 "4827 smphr_port_status x%x : Waited %dSec",
1865 smphr_port_status, i);
1866
1867 /* Recoverable UE, reset the HBA device */
1868 if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1869 LPFC_PORT_SEM_UE_RECOVERABLE) {
1870 for (i = 0; i < 20; i++) {
1871 msleep(1000);
1872 if (!lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1873 &portsmphr_reg.word0) &&
1874 (LPFC_POST_STAGE_PORT_READY ==
1875 bf_get(lpfc_port_smphr_port_status,
1876 &portsmphr_reg))) {
1877 rc = lpfc_sli4_port_sta_fn_reset(phba,
1878 LPFC_MBX_NO_WAIT, en_rn_msg);
1879 if (rc == 0)
1880 return;
1881 lpfc_printf_log(phba,
1882 KERN_ERR, LOG_INIT,
1883 "4215 Failed to recover UE");
1884 break;
1885 }
1886 }
1887 }
1888 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1889 "7624 Firmware not ready: Failing UE recovery,"
1890 " waited %dSec", i);
James Smart2fcee4b2010-12-15 17:57:46 -05001891 lpfc_sli4_offline_eratt(phba);
1892 break;
James Smart946727d2015-04-07 15:07:09 -04001893
James Smart2fcee4b2010-12-15 17:57:46 -05001894 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart2e90f4b2011-12-13 13:22:37 -05001895 pci_rd_rc1 = lpfc_readl(
1896 phba->sli4_hba.u.if_type2.STATUSregaddr,
1897 &portstat_reg.word0);
1898 /* consider PCI bus read error as pci_channel_offline */
James Smart6b5151f2012-01-18 16:24:06 -05001899 if (pci_rd_rc1 == -EIO) {
1900 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1901 "3151 PCI bus read access failure: x%x\n",
1902 readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
James Smart2e90f4b2011-12-13 13:22:37 -05001903 return;
James Smart6b5151f2012-01-18 16:24:06 -05001904 }
James Smart2e90f4b2011-12-13 13:22:37 -05001905 reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
1906 reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
James Smart2fcee4b2010-12-15 17:57:46 -05001907 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
James Smart2fcee4b2010-12-15 17:57:46 -05001908 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1909 "2889 Port Overtemperature event, "
James Smart946727d2015-04-07 15:07:09 -04001910 "taking port offline Data: x%x x%x\n",
1911 reg_err1, reg_err2);
1912
James Smart310429e2016-07-06 12:35:54 -07001913 phba->sfp_alarm |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
James Smart946727d2015-04-07 15:07:09 -04001914 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1915 temp_event_data.event_code = LPFC_CRIT_TEMP;
1916 temp_event_data.data = 0xFFFFFFFF;
1917
1918 shost = lpfc_shost_from_vport(phba->pport);
1919 fc_host_post_vendor_event(shost, fc_get_event_number(),
1920 sizeof(temp_event_data),
1921 (char *)&temp_event_data,
1922 SCSI_NL_VID_TYPE_PCI
1923 | PCI_VENDOR_ID_EMULEX);
1924
James Smart2fcee4b2010-12-15 17:57:46 -05001925 spin_lock_irq(&phba->hbalock);
1926 phba->over_temp_state = HBA_OVER_TEMP;
1927 spin_unlock_irq(&phba->hbalock);
1928 lpfc_sli4_offline_eratt(phba);
James Smart946727d2015-04-07 15:07:09 -04001929 return;
James Smart2fcee4b2010-12-15 17:57:46 -05001930 }
James Smart2e90f4b2011-12-13 13:22:37 -05001931 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smarte10b2022014-02-20 09:57:43 -05001932 reg_err2 == SLIPORT_ERR2_REG_FW_RESTART) {
James Smart2e90f4b2011-12-13 13:22:37 -05001933 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte10b2022014-02-20 09:57:43 -05001934 "3143 Port Down: Firmware Update "
1935 "Detected\n");
1936 en_rn_msg = false;
1937 } else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smart2e90f4b2011-12-13 13:22:37 -05001938 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1939 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1940 "3144 Port Down: Debug Dump\n");
1941 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1942 reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
1943 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1944 "3145 Port Down: Provisioning\n");
James Smart618a5232012-06-12 13:54:36 -04001945
James Smart946727d2015-04-07 15:07:09 -04001946 /* If resets are disabled then leave the HBA alone and return */
1947 if (!phba->cfg_enable_hba_reset)
1948 return;
1949
James Smart618a5232012-06-12 13:54:36 -04001950 /* Check port status register for function reset */
James Smarte10b2022014-02-20 09:57:43 -05001951 rc = lpfc_sli4_port_sta_fn_reset(phba, LPFC_MBX_NO_WAIT,
1952 en_rn_msg);
James Smart618a5232012-06-12 13:54:36 -04001953 if (rc == 0) {
1954 /* don't report event on forced debug dump */
1955 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1956 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1957 return;
1958 else
1959 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001960 }
James Smart618a5232012-06-12 13:54:36 -04001961 /* fall through for not able to recover */
James Smart6b5151f2012-01-18 16:24:06 -05001962 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1963 "3152 Unrecoverable error, bring the port "
1964 "offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001965 lpfc_sli4_offline_eratt(phba);
1966 break;
1967 case LPFC_SLI_INTF_IF_TYPE_1:
1968 default:
1969 break;
1970 }
James Smart2e90f4b2011-12-13 13:22:37 -05001971 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1972 "3123 Report dump event to upper layer\n");
1973 /* Send an internal error event to mgmt application */
1974 lpfc_board_errevt_to_mgmt(phba);
1975
1976 event_data = FC_REG_DUMP_EVENT;
1977 shost = lpfc_shost_from_vport(vport);
1978 fc_host_post_vendor_event(shost, fc_get_event_number(),
1979 sizeof(event_data), (char *) &event_data,
1980 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
James Smartda0436e2009-05-22 14:51:39 -04001981}
1982
1983/**
1984 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1985 * @phba: pointer to lpfc HBA data structure.
1986 *
1987 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1988 * routine from the API jump table function pointer from the lpfc_hba struct.
1989 *
1990 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001991 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001992 * Any other value - error.
1993 **/
1994void
1995lpfc_handle_eratt(struct lpfc_hba *phba)
1996{
1997 (*phba->lpfc_handle_eratt)(phba);
1998}
1999
2000/**
James Smart3621a712009-04-06 18:47:14 -04002001 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04002002 * @phba: pointer to lpfc hba data structure.
2003 *
2004 * This routine is invoked from the worker thread to handle a HBA host
James Smart895427b2017-02-12 13:52:30 -08002005 * attention link event. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04002006 **/
dea31012005-04-17 16:05:31 -05002007void
James Smart2e0fef82007-06-17 19:56:36 -05002008lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002009{
James Smart2e0fef82007-06-17 19:56:36 -05002010 struct lpfc_vport *vport = phba->pport;
2011 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05002012 LPFC_MBOXQ_t *pmb;
2013 volatile uint32_t control;
2014 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05002015 int rc = 0;
dea31012005-04-17 16:05:31 -05002016
2017 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05002018 if (!pmb) {
2019 rc = 1;
dea31012005-04-17 16:05:31 -05002020 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05002021 }
dea31012005-04-17 16:05:31 -05002022
2023 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05002024 if (!mp) {
2025 rc = 2;
dea31012005-04-17 16:05:31 -05002026 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05002027 }
dea31012005-04-17 16:05:31 -05002028
2029 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05002030 if (!mp->virt) {
2031 rc = 3;
dea31012005-04-17 16:05:31 -05002032 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05002033 }
dea31012005-04-17 16:05:31 -05002034
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05002035 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04002036 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05002037
2038 psli->slistat.link_event++;
James Smart76a95d72010-11-20 23:11:48 -05002039 lpfc_read_topology(phba, pmb, mp);
2040 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smart2e0fef82007-06-17 19:56:36 -05002041 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04002042 /* Block ELS IOCBs until we have processed this mbox command */
James Smart895427b2017-02-12 13:52:30 -08002043 phba->sli.sli3_ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04002044 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05002045 if (rc == MBX_NOT_FINISHED) {
2046 rc = 4;
James Smart14691152006-12-02 13:34:28 -05002047 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05002048 }
dea31012005-04-17 16:05:31 -05002049
2050 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05002051 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002052 writel(HA_LATT, phba->HAregaddr);
2053 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002054 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002055
2056 return;
2057
James Smart14691152006-12-02 13:34:28 -05002058lpfc_handle_latt_free_mbuf:
James Smart895427b2017-02-12 13:52:30 -08002059 phba->sli.sli3_ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05002060 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05002061lpfc_handle_latt_free_mp:
2062 kfree(mp);
2063lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04002064 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002065lpfc_handle_latt_err_exit:
2066 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05002067 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002068 psli->sli_flag |= LPFC_PROCESS_LA;
2069 control = readl(phba->HCregaddr);
2070 control |= HC_LAINT_ENA;
2071 writel(control, phba->HCregaddr);
2072 readl(phba->HCregaddr); /* flush */
2073
2074 /* Clear Link Attention in HA REG */
2075 writel(HA_LATT, phba->HAregaddr);
2076 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002077 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002078 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002079 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002080
James Smart09372822008-01-11 01:52:54 -05002081 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
2082 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05002083
2084 return;
2085}
2086
James Smarte59058c2008-08-24 21:49:00 -04002087/**
James Smart3621a712009-04-06 18:47:14 -04002088 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04002089 * @phba: pointer to lpfc hba data structure.
2090 * @vpd: pointer to the vital product data.
2091 * @len: length of the vital product data in bytes.
2092 *
2093 * This routine parses the Vital Product Data (VPD). The VPD is treated as
2094 * an array of characters. In this routine, the ModelName, ProgramType, and
2095 * ModelDesc, etc. fields of the phba data structure will be populated.
2096 *
2097 * Return codes
2098 * 0 - pointer to the VPD passed in is NULL
2099 * 1 - success
2100 **/
James Smart3772a992009-05-22 14:50:54 -04002101int
James Smart2e0fef82007-06-17 19:56:36 -05002102lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05002103{
2104 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05002105 int Length;
dea31012005-04-17 16:05:31 -05002106 int i, j;
2107 int finished = 0;
2108 int index = 0;
2109
2110 if (!vpd)
2111 return 0;
2112
2113 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05002114 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002115 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05002116 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
2117 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002118 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05002119 switch (vpd[index]) {
2120 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002121 case 0x91:
dea31012005-04-17 16:05:31 -05002122 index += 1;
2123 lenlo = vpd[index];
2124 index += 1;
2125 lenhi = vpd[index];
2126 index += 1;
2127 i = ((((unsigned short)lenhi) << 8) + lenlo);
2128 index += i;
2129 break;
2130 case 0x90:
2131 index += 1;
2132 lenlo = vpd[index];
2133 index += 1;
2134 lenhi = vpd[index];
2135 index += 1;
2136 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002137 if (Length > len - index)
2138 Length = len - index;
dea31012005-04-17 16:05:31 -05002139 while (Length > 0) {
2140 /* Look for Serial Number */
2141 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
2142 index += 2;
2143 i = vpd[index];
2144 index += 1;
2145 j = 0;
2146 Length -= (3+i);
2147 while(i--) {
2148 phba->SerialNumber[j++] = vpd[index++];
2149 if (j == 31)
2150 break;
2151 }
2152 phba->SerialNumber[j] = 0;
2153 continue;
2154 }
2155 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
2156 phba->vpd_flag |= VPD_MODEL_DESC;
2157 index += 2;
2158 i = vpd[index];
2159 index += 1;
2160 j = 0;
2161 Length -= (3+i);
2162 while(i--) {
2163 phba->ModelDesc[j++] = vpd[index++];
2164 if (j == 255)
2165 break;
2166 }
2167 phba->ModelDesc[j] = 0;
2168 continue;
2169 }
2170 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
2171 phba->vpd_flag |= VPD_MODEL_NAME;
2172 index += 2;
2173 i = vpd[index];
2174 index += 1;
2175 j = 0;
2176 Length -= (3+i);
2177 while(i--) {
2178 phba->ModelName[j++] = vpd[index++];
2179 if (j == 79)
2180 break;
2181 }
2182 phba->ModelName[j] = 0;
2183 continue;
2184 }
2185 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
2186 phba->vpd_flag |= VPD_PROGRAM_TYPE;
2187 index += 2;
2188 i = vpd[index];
2189 index += 1;
2190 j = 0;
2191 Length -= (3+i);
2192 while(i--) {
2193 phba->ProgramType[j++] = vpd[index++];
2194 if (j == 255)
2195 break;
2196 }
2197 phba->ProgramType[j] = 0;
2198 continue;
2199 }
2200 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
2201 phba->vpd_flag |= VPD_PORT;
2202 index += 2;
2203 i = vpd[index];
2204 index += 1;
2205 j = 0;
2206 Length -= (3+i);
2207 while(i--) {
James Smartcd1c8302011-10-10 21:33:25 -04002208 if ((phba->sli_rev == LPFC_SLI_REV4) &&
2209 (phba->sli4_hba.pport_name_sta ==
2210 LPFC_SLI4_PPNAME_GET)) {
2211 j++;
2212 index++;
2213 } else
2214 phba->Port[j++] = vpd[index++];
2215 if (j == 19)
2216 break;
dea31012005-04-17 16:05:31 -05002217 }
James Smartcd1c8302011-10-10 21:33:25 -04002218 if ((phba->sli_rev != LPFC_SLI_REV4) ||
2219 (phba->sli4_hba.pport_name_sta ==
2220 LPFC_SLI4_PPNAME_NON))
2221 phba->Port[j] = 0;
dea31012005-04-17 16:05:31 -05002222 continue;
2223 }
2224 else {
2225 index += 2;
2226 i = vpd[index];
2227 index += 1;
2228 index += i;
2229 Length -= (3 + i);
2230 }
2231 }
2232 finished = 0;
2233 break;
2234 case 0x78:
2235 finished = 1;
2236 break;
2237 default:
2238 index ++;
2239 break;
2240 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002241 }
dea31012005-04-17 16:05:31 -05002242
2243 return(1);
2244}
2245
James Smarte59058c2008-08-24 21:49:00 -04002246/**
James Smart3621a712009-04-06 18:47:14 -04002247 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04002248 * @phba: pointer to lpfc hba data structure.
2249 * @mdp: pointer to the data structure to hold the derived model name.
2250 * @descp: pointer to the data structure to hold the derived description.
2251 *
2252 * This routine retrieves HBA's description based on its registered PCI device
2253 * ID. The @descp passed into this function points to an array of 256 chars. It
2254 * shall be returned with the model name, maximum speed, and the host bus type.
2255 * The @mdp passed into this function points to an array of 80 chars. When the
2256 * function returns, the @mdp will be filled with the model name.
2257 **/
dea31012005-04-17 16:05:31 -05002258static void
James Smart2e0fef82007-06-17 19:56:36 -05002259lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05002260{
2261 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05002262 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002263 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04002264 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04002265 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002266 struct {
James Smarta747c9c2009-11-18 15:41:10 -05002267 char *name;
2268 char *bus;
2269 char *function;
2270 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002271
2272 if (mdp && mdp[0] != '\0'
2273 && descp && descp[0] != '\0')
2274 return;
2275
James Smartd38dd522015-08-31 16:48:17 -04002276 if (phba->lmt & LMT_32Gb)
2277 max_speed = 32;
2278 else if (phba->lmt & LMT_16Gb)
James Smartc0c11512011-05-24 11:41:34 -04002279 max_speed = 16;
2280 else if (phba->lmt & LMT_10Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002281 max_speed = 10;
2282 else if (phba->lmt & LMT_8Gb)
2283 max_speed = 8;
2284 else if (phba->lmt & LMT_4Gb)
2285 max_speed = 4;
2286 else if (phba->lmt & LMT_2Gb)
2287 max_speed = 2;
James Smart4169d862012-09-29 11:32:09 -04002288 else if (phba->lmt & LMT_1Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002289 max_speed = 1;
James Smart4169d862012-09-29 11:32:09 -04002290 else
2291 max_speed = 0;
dea31012005-04-17 16:05:31 -05002292
2293 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05002294
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002295 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002296 case PCI_DEVICE_ID_FIREFLY:
James Smart12222f42014-05-21 08:05:19 -04002297 m = (typeof(m)){"LP6000", "PCI",
2298 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002299 break;
dea31012005-04-17 16:05:31 -05002300 case PCI_DEVICE_ID_SUPERFLY:
2301 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smart12222f42014-05-21 08:05:19 -04002302 m = (typeof(m)){"LP7000", "PCI", ""};
dea31012005-04-17 16:05:31 -05002303 else
James Smart12222f42014-05-21 08:05:19 -04002304 m = (typeof(m)){"LP7000E", "PCI", ""};
2305 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
dea31012005-04-17 16:05:31 -05002306 break;
2307 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002308 m = (typeof(m)){"LP8000", "PCI",
James Smart12222f42014-05-21 08:05:19 -04002309 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002310 break;
2311 case PCI_DEVICE_ID_CENTAUR:
2312 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smart12222f42014-05-21 08:05:19 -04002313 m = (typeof(m)){"LP9002", "PCI", ""};
dea31012005-04-17 16:05:31 -05002314 else
James Smart12222f42014-05-21 08:05:19 -04002315 m = (typeof(m)){"LP9000", "PCI", ""};
2316 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
dea31012005-04-17 16:05:31 -05002317 break;
2318 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002319 m = (typeof(m)){"LP952", "PCI",
James Smart12222f42014-05-21 08:05:19 -04002320 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002321 break;
2322 case PCI_DEVICE_ID_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05002323 m = (typeof(m)){"LP9802", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002324 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002325 break;
2326 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05002327 m = (typeof(m)){"LP10000", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002328 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002329 break;
2330 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05002331 m = (typeof(m)){"LPX1000", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002332 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002333 break;
2334 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002335 m = (typeof(m)){"LP982", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002336 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002337 break;
2338 case PCI_DEVICE_ID_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002339 m = (typeof(m)){"LP1050", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002340 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002341 break;
2342 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05002343 m = (typeof(m)){"LP11000", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002344 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002345 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002346 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002347 m = (typeof(m)){"LP11000-SP", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002348 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002349 break;
2350 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002351 m = (typeof(m)){"LP11002-SP", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002352 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002353 break;
2354 case PCI_DEVICE_ID_NEPTUNE:
James Smart12222f42014-05-21 08:05:19 -04002355 m = (typeof(m)){"LPe1000", "PCIe",
2356 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002357 break;
2358 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smart12222f42014-05-21 08:05:19 -04002359 m = (typeof(m)){"LPe1000-SP", "PCIe",
2360 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002361 break;
2362 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smart12222f42014-05-21 08:05:19 -04002363 m = (typeof(m)){"LPe1002-SP", "PCIe",
2364 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002365 break;
dea31012005-04-17 16:05:31 -05002366 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05002367 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002368 break;
2369 case PCI_DEVICE_ID_BSMB:
James Smart12222f42014-05-21 08:05:19 -04002370 m = (typeof(m)){"LP111", "PCI-X2",
2371 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002372 break;
2373 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05002374 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002375 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002376 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002377 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002378 break;
2379 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002380 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04002381 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002382 break;
dea31012005-04-17 16:05:31 -05002383 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05002384 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002385 break;
2386 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05002387 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002388 break;
2389 case PCI_DEVICE_ID_LP101:
James Smart12222f42014-05-21 08:05:19 -04002390 m = (typeof(m)){"LP101", "PCI-X",
2391 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002392 break;
2393 case PCI_DEVICE_ID_LP10000S:
James Smart12222f42014-05-21 08:05:19 -04002394 m = (typeof(m)){"LP10000-S", "PCI",
2395 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002396 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002397 case PCI_DEVICE_ID_LP11000S:
James Smart12222f42014-05-21 08:05:19 -04002398 m = (typeof(m)){"LP11000-S", "PCI-X2",
2399 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05002400 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002401 case PCI_DEVICE_ID_LPE11000S:
James Smart12222f42014-05-21 08:05:19 -04002402 m = (typeof(m)){"LPe11000-S", "PCIe",
2403 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002404 break;
James Smartb87eab32007-04-25 09:53:28 -04002405 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05002406 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002407 break;
2408 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05002409 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002410 break;
2411 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05002412 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002413 break;
2414 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002415 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002416 break;
2417 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002418 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002419 break;
2420 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05002421 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002422 break;
James Smart84774a42008-08-24 21:50:06 -04002423 case PCI_DEVICE_ID_HORNET:
James Smart12222f42014-05-21 08:05:19 -04002424 m = (typeof(m)){"LP21000", "PCIe",
2425 "Obsolete, Unsupported FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002426 GE = 1;
2427 break;
2428 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05002429 m = (typeof(m)){"LPev12000", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002430 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002431 break;
2432 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05002433 m = (typeof(m)){"LPev12000", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002434 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002435 break;
2436 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05002437 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002438 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002439 break;
James Smartda0436e2009-05-22 14:51:39 -04002440 case PCI_DEVICE_ID_TIGERSHARK:
2441 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002442 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04002443 break;
James Smarta747c9c2009-11-18 15:41:10 -05002444 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04002445 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002446 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
2447 break;
2448 case PCI_DEVICE_ID_FALCON:
2449 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
2450 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04002451 break;
James Smart98fc5dd2010-06-07 15:24:29 -04002452 case PCI_DEVICE_ID_BALIUS:
2453 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
James Smart12222f42014-05-21 08:05:19 -04002454 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart98fc5dd2010-06-07 15:24:29 -04002455 break;
James Smart085c6472010-11-20 23:11:37 -05002456 case PCI_DEVICE_ID_LANCER_FC:
James Smartc0c11512011-05-24 11:41:34 -04002457 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
James Smart085c6472010-11-20 23:11:37 -05002458 break;
James Smart12222f42014-05-21 08:05:19 -04002459 case PCI_DEVICE_ID_LANCER_FC_VF:
2460 m = (typeof(m)){"LPe16000", "PCIe",
2461 "Obsolete, Unsupported Fibre Channel Adapter"};
2462 break;
James Smart085c6472010-11-20 23:11:37 -05002463 case PCI_DEVICE_ID_LANCER_FCOE:
2464 oneConnect = 1;
James Smart079b5c92011-08-21 21:48:49 -04002465 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
James Smart085c6472010-11-20 23:11:37 -05002466 break;
James Smart12222f42014-05-21 08:05:19 -04002467 case PCI_DEVICE_ID_LANCER_FCOE_VF:
2468 oneConnect = 1;
2469 m = (typeof(m)){"OCe15100", "PCIe",
2470 "Obsolete, Unsupported FCoE"};
2471 break;
James Smartd38dd522015-08-31 16:48:17 -04002472 case PCI_DEVICE_ID_LANCER_G6_FC:
2473 m = (typeof(m)){"LPe32000", "PCIe", "Fibre Channel Adapter"};
2474 break;
James Smartf8cafd32012-08-03 12:42:51 -04002475 case PCI_DEVICE_ID_SKYHAWK:
2476 case PCI_DEVICE_ID_SKYHAWK_VF:
2477 oneConnect = 1;
2478 m = (typeof(m)){"OCe14000", "PCIe", "FCoE"};
2479 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002480 default:
James Smarta747c9c2009-11-18 15:41:10 -05002481 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002482 break;
dea31012005-04-17 16:05:31 -05002483 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002484
2485 if (mdp && mdp[0] == '\0')
2486 snprintf(mdp, 79,"%s", m.name);
James Smartc0c11512011-05-24 11:41:34 -04002487 /*
2488 * oneConnect hba requires special processing, they are all initiators
James Smartda0436e2009-05-22 14:51:39 -04002489 * and we put the port number on the end
2490 */
2491 if (descp && descp[0] == '\0') {
2492 if (oneConnect)
2493 snprintf(descp, 255,
James Smart4169d862012-09-29 11:32:09 -04002494 "Emulex OneConnect %s, %s Initiator %s",
James Smarta747c9c2009-11-18 15:41:10 -05002495 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04002496 phba->Port);
James Smart4169d862012-09-29 11:32:09 -04002497 else if (max_speed == 0)
2498 snprintf(descp, 255,
Sebastian Herbszt290237d2015-08-31 16:48:08 -04002499 "Emulex %s %s %s",
James Smart4169d862012-09-29 11:32:09 -04002500 m.name, m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002501 else
2502 snprintf(descp, 255,
2503 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05002504 m.name, max_speed, (GE) ? "GE" : "Gb",
2505 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002506 }
dea31012005-04-17 16:05:31 -05002507}
2508
James Smarte59058c2008-08-24 21:49:00 -04002509/**
James Smart3621a712009-04-06 18:47:14 -04002510 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04002511 * @phba: pointer to lpfc hba data structure.
2512 * @pring: pointer to a IOCB ring.
2513 * @cnt: the number of IOCBs to be posted to the IOCB ring.
2514 *
2515 * This routine posts a given number of IOCBs with the associated DMA buffer
2516 * descriptors specified by the cnt argument to the given IOCB ring.
2517 *
2518 * Return codes
2519 * The number of IOCBs NOT able to be posted to the IOCB ring.
2520 **/
dea31012005-04-17 16:05:31 -05002521int
James Smart495a7142008-06-14 22:52:59 -04002522lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05002523{
2524 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002525 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05002526 struct lpfc_dmabuf *mp1, *mp2;
2527
2528 cnt += pring->missbufcnt;
2529
2530 /* While there are buffers to post */
2531 while (cnt > 0) {
2532 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002533 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002534 if (iocb == NULL) {
2535 pring->missbufcnt = cnt;
2536 return cnt;
2537 }
dea31012005-04-17 16:05:31 -05002538 icmd = &iocb->iocb;
2539
2540 /* 2 buffers can be posted per command */
2541 /* Allocate buffer to post */
2542 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2543 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04002544 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2545 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002546 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002547 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002548 pring->missbufcnt = cnt;
2549 return cnt;
2550 }
2551
2552 INIT_LIST_HEAD(&mp1->list);
2553 /* Allocate buffer to post */
2554 if (cnt > 1) {
2555 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2556 if (mp2)
2557 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2558 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04002559 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002560 kfree(mp2);
dea31012005-04-17 16:05:31 -05002561 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2562 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002563 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002564 pring->missbufcnt = cnt;
2565 return cnt;
2566 }
2567
2568 INIT_LIST_HEAD(&mp2->list);
2569 } else {
2570 mp2 = NULL;
2571 }
2572
2573 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2574 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2575 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2576 icmd->ulpBdeCount = 1;
2577 cnt--;
2578 if (mp2) {
2579 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2580 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2581 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2582 cnt--;
2583 icmd->ulpBdeCount = 2;
2584 }
2585
2586 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2587 icmd->ulpLe = 1;
2588
James Smart3772a992009-05-22 14:50:54 -04002589 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2590 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05002591 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2592 kfree(mp1);
2593 cnt++;
2594 if (mp2) {
2595 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2596 kfree(mp2);
2597 cnt++;
2598 }
James Bottomley604a3e32005-10-29 10:28:33 -05002599 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002600 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05002601 return cnt;
2602 }
dea31012005-04-17 16:05:31 -05002603 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05002604 if (mp2)
dea31012005-04-17 16:05:31 -05002605 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05002606 }
2607 pring->missbufcnt = 0;
2608 return 0;
2609}
2610
James Smarte59058c2008-08-24 21:49:00 -04002611/**
James Smart3621a712009-04-06 18:47:14 -04002612 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04002613 * @phba: pointer to lpfc hba data structure.
2614 *
2615 * This routine posts initial receive IOCB buffers to the ELS ring. The
2616 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
James Smart895427b2017-02-12 13:52:30 -08002617 * set to 64 IOCBs. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04002618 *
2619 * Return codes
2620 * 0 - success (currently always success)
2621 **/
dea31012005-04-17 16:05:31 -05002622static int
James Smart2e0fef82007-06-17 19:56:36 -05002623lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002624{
2625 struct lpfc_sli *psli = &phba->sli;
2626
2627 /* Ring 0, ELS / CT buffers */
James Smart895427b2017-02-12 13:52:30 -08002628 lpfc_post_buffer(phba, &psli->sli3_ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05002629 /* Ring 2 - FCP no buffers needed */
2630
2631 return 0;
2632}
2633
2634#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2635
James Smarte59058c2008-08-24 21:49:00 -04002636/**
James Smart3621a712009-04-06 18:47:14 -04002637 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04002638 * @HashResultPointer: pointer to an array as hash table.
2639 *
2640 * This routine sets up the initial values to the array of hash table entries
2641 * for the LC HBAs.
2642 **/
dea31012005-04-17 16:05:31 -05002643static void
2644lpfc_sha_init(uint32_t * HashResultPointer)
2645{
2646 HashResultPointer[0] = 0x67452301;
2647 HashResultPointer[1] = 0xEFCDAB89;
2648 HashResultPointer[2] = 0x98BADCFE;
2649 HashResultPointer[3] = 0x10325476;
2650 HashResultPointer[4] = 0xC3D2E1F0;
2651}
2652
James Smarte59058c2008-08-24 21:49:00 -04002653/**
James Smart3621a712009-04-06 18:47:14 -04002654 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04002655 * @HashResultPointer: pointer to an initial/result hash table.
2656 * @HashWorkingPointer: pointer to an working hash table.
2657 *
2658 * This routine iterates an initial hash table pointed by @HashResultPointer
2659 * with the values from the working hash table pointeed by @HashWorkingPointer.
2660 * The results are putting back to the initial hash table, returned through
2661 * the @HashResultPointer as the result hash table.
2662 **/
dea31012005-04-17 16:05:31 -05002663static void
2664lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2665{
2666 int t;
2667 uint32_t TEMP;
2668 uint32_t A, B, C, D, E;
2669 t = 16;
2670 do {
2671 HashWorkingPointer[t] =
2672 S(1,
2673 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2674 8] ^
2675 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2676 } while (++t <= 79);
2677 t = 0;
2678 A = HashResultPointer[0];
2679 B = HashResultPointer[1];
2680 C = HashResultPointer[2];
2681 D = HashResultPointer[3];
2682 E = HashResultPointer[4];
2683
2684 do {
2685 if (t < 20) {
2686 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2687 } else if (t < 40) {
2688 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2689 } else if (t < 60) {
2690 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2691 } else {
2692 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2693 }
2694 TEMP += S(5, A) + E + HashWorkingPointer[t];
2695 E = D;
2696 D = C;
2697 C = S(30, B);
2698 B = A;
2699 A = TEMP;
2700 } while (++t <= 79);
2701
2702 HashResultPointer[0] += A;
2703 HashResultPointer[1] += B;
2704 HashResultPointer[2] += C;
2705 HashResultPointer[3] += D;
2706 HashResultPointer[4] += E;
2707
2708}
2709
James Smarte59058c2008-08-24 21:49:00 -04002710/**
James Smart3621a712009-04-06 18:47:14 -04002711 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04002712 * @RandomChallenge: pointer to the entry of host challenge random number array.
2713 * @HashWorking: pointer to the entry of the working hash array.
2714 *
2715 * This routine calculates the working hash array referred by @HashWorking
2716 * from the challenge random numbers associated with the host, referred by
2717 * @RandomChallenge. The result is put into the entry of the working hash
2718 * array and returned by reference through @HashWorking.
2719 **/
dea31012005-04-17 16:05:31 -05002720static void
2721lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2722{
2723 *HashWorking = (*RandomChallenge ^ *HashWorking);
2724}
2725
James Smarte59058c2008-08-24 21:49:00 -04002726/**
James Smart3621a712009-04-06 18:47:14 -04002727 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04002728 * @phba: pointer to lpfc hba data structure.
2729 * @hbainit: pointer to an array of unsigned 32-bit integers.
2730 *
2731 * This routine performs the special handling for LC HBA initialization.
2732 **/
dea31012005-04-17 16:05:31 -05002733void
2734lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2735{
2736 int t;
2737 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05002738 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05002739
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002740 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002741 if (!HashWorking)
2742 return;
2743
dea31012005-04-17 16:05:31 -05002744 HashWorking[0] = HashWorking[78] = *pwwnn++;
2745 HashWorking[1] = HashWorking[79] = *pwwnn;
2746
2747 for (t = 0; t < 7; t++)
2748 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2749
2750 lpfc_sha_init(hbainit);
2751 lpfc_sha_iterate(hbainit, HashWorking);
2752 kfree(HashWorking);
2753}
2754
James Smarte59058c2008-08-24 21:49:00 -04002755/**
James Smart3621a712009-04-06 18:47:14 -04002756 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04002757 * @vport: pointer to a virtual N_Port data structure.
2758 *
2759 * This routine performs the necessary cleanups before deleting the @vport.
2760 * It invokes the discovery state machine to perform necessary state
2761 * transitions and to release the ndlps associated with the @vport. Note,
2762 * the physical port is treated as @vport 0.
2763 **/
James Smart87af33f2007-10-27 13:37:43 -04002764void
James Smart2e0fef82007-06-17 19:56:36 -05002765lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002766{
James Smart87af33f2007-10-27 13:37:43 -04002767 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002768 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04002769 int i = 0;
dea31012005-04-17 16:05:31 -05002770
James Smart87af33f2007-10-27 13:37:43 -04002771 if (phba->link_state > LPFC_LINK_DOWN)
2772 lpfc_port_link_failure(vport);
2773
2774 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002775 if (!NLP_CHK_NODE_ACT(ndlp)) {
2776 ndlp = lpfc_enable_node(vport, ndlp,
2777 NLP_STE_UNUSED_NODE);
2778 if (!ndlp)
2779 continue;
2780 spin_lock_irq(&phba->ndlp_lock);
2781 NLP_SET_FREE_REQ(ndlp);
2782 spin_unlock_irq(&phba->ndlp_lock);
2783 /* Trigger the release of the ndlp memory */
2784 lpfc_nlp_put(ndlp);
2785 continue;
2786 }
2787 spin_lock_irq(&phba->ndlp_lock);
2788 if (NLP_CHK_FREE_REQ(ndlp)) {
2789 /* The ndlp should not be in memory free mode already */
2790 spin_unlock_irq(&phba->ndlp_lock);
2791 continue;
2792 } else
2793 /* Indicate request for freeing ndlp memory */
2794 NLP_SET_FREE_REQ(ndlp);
2795 spin_unlock_irq(&phba->ndlp_lock);
2796
James Smart58da1ff2008-04-07 10:15:56 -04002797 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2798 ndlp->nlp_DID == Fabric_DID) {
2799 /* Just free up ndlp with Fabric_DID for vports */
2800 lpfc_nlp_put(ndlp);
2801 continue;
2802 }
2803
James Smarteff4a012012-01-18 16:25:25 -05002804 /* take care of nodes in unused state before the state
2805 * machine taking action.
2806 */
2807 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
2808 lpfc_nlp_put(ndlp);
2809 continue;
2810 }
2811
James Smart87af33f2007-10-27 13:37:43 -04002812 if (ndlp->nlp_type & NLP_FABRIC)
2813 lpfc_disc_state_machine(vport, ndlp, NULL,
2814 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002815
James Smart87af33f2007-10-27 13:37:43 -04002816 lpfc_disc_state_machine(vport, ndlp, NULL,
2817 NLP_EVT_DEVICE_RM);
2818 }
2819
James Smarta8adb832007-10-27 13:37:53 -04002820 /* At this point, ALL ndlp's should be gone
2821 * because of the previous NLP_EVT_DEVICE_RM.
2822 * Lets wait for this to happen, if needed.
2823 */
James Smart87af33f2007-10-27 13:37:43 -04002824 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002825 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002826 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002827 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002828 list_for_each_entry_safe(ndlp, next_ndlp,
2829 &vport->fc_nodes, nlp_listp) {
2830 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2831 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002832 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002833 "usgmap:x%x refcnt:%d\n",
2834 ndlp->nlp_DID, (void *)ndlp,
2835 ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01002836 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05002837 }
James Smarta8adb832007-10-27 13:37:53 -04002838 break;
James Smart87af33f2007-10-27 13:37:43 -04002839 }
James Smarta8adb832007-10-27 13:37:53 -04002840
2841 /* Wait for any activity on ndlps to settle */
2842 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002843 }
James Smart1151e3e2011-02-16 12:39:35 -05002844 lpfc_cleanup_vports_rrqs(vport, NULL);
dea31012005-04-17 16:05:31 -05002845}
2846
James Smarte59058c2008-08-24 21:49:00 -04002847/**
James Smart3621a712009-04-06 18:47:14 -04002848 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002849 * @vport: pointer to a virtual N_Port data structure.
2850 *
2851 * This routine stops all the timers associated with a @vport. This function
2852 * is invoked before disabling or deleting a @vport. Note that the physical
2853 * port is treated as @vport 0.
2854 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002855void
2856lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002857{
James Smart92d7f7b2007-06-17 19:56:38 -05002858 del_timer_sync(&vport->els_tmofunc);
James Smart92494142011-02-16 12:39:44 -05002859 del_timer_sync(&vport->delayed_disc_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002860 lpfc_can_disctmo(vport);
2861 return;
dea31012005-04-17 16:05:31 -05002862}
2863
James Smarte59058c2008-08-24 21:49:00 -04002864/**
James Smartecfd03c2010-02-12 14:41:27 -05002865 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2866 * @phba: pointer to lpfc hba data structure.
2867 *
2868 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2869 * caller of this routine should already hold the host lock.
2870 **/
2871void
2872__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2873{
James Smart5ac6b302010-10-22 11:05:36 -04002874 /* Clear pending FCF rediscovery wait flag */
2875 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2876
James Smartecfd03c2010-02-12 14:41:27 -05002877 /* Now, try to stop the timer */
2878 del_timer(&phba->fcf.redisc_wait);
2879}
2880
2881/**
2882 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2883 * @phba: pointer to lpfc hba data structure.
2884 *
2885 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2886 * checks whether the FCF rediscovery wait timer is pending with the host
2887 * lock held before proceeding with disabling the timer and clearing the
2888 * wait timer pendig flag.
2889 **/
2890void
2891lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2892{
2893 spin_lock_irq(&phba->hbalock);
2894 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2895 /* FCF rediscovery timer already fired or stopped */
2896 spin_unlock_irq(&phba->hbalock);
2897 return;
2898 }
2899 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart5ac6b302010-10-22 11:05:36 -04002900 /* Clear failover in progress flags */
2901 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
James Smartecfd03c2010-02-12 14:41:27 -05002902 spin_unlock_irq(&phba->hbalock);
2903}
2904
2905/**
James Smart3772a992009-05-22 14:50:54 -04002906 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002907 * @phba: pointer to lpfc hba data structure.
2908 *
2909 * This routine stops all the timers associated with a HBA. This function is
2910 * invoked before either putting a HBA offline or unloading the driver.
2911 **/
James Smart3772a992009-05-22 14:50:54 -04002912void
2913lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002914{
James Smart51ef4c22007-08-02 11:10:31 -04002915 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05002916 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002917 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002918 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002919 del_timer_sync(&phba->hb_tmofunc);
James Smart1151e3e2011-02-16 12:39:35 -05002920 if (phba->sli_rev == LPFC_SLI_REV4) {
2921 del_timer_sync(&phba->rrq_tmr);
2922 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2923 }
James Smart3772a992009-05-22 14:50:54 -04002924 phba->hb_outstanding = 0;
2925
2926 switch (phba->pci_dev_grp) {
2927 case LPFC_PCI_DEV_LP:
2928 /* Stop any LightPulse device specific driver timers */
2929 del_timer_sync(&phba->fcp_poll_timer);
2930 break;
2931 case LPFC_PCI_DEV_OC:
2932 /* Stop any OneConnect device sepcific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05002933 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04002934 break;
2935 default:
2936 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2937 "0297 Invalid device group (x%x)\n",
2938 phba->pci_dev_grp);
2939 break;
2940 }
James Smart2e0fef82007-06-17 19:56:36 -05002941 return;
dea31012005-04-17 16:05:31 -05002942}
2943
James Smarte59058c2008-08-24 21:49:00 -04002944/**
James Smart3621a712009-04-06 18:47:14 -04002945 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002946 * @phba: pointer to lpfc hba data structure.
2947 *
2948 * This routine marks a HBA's management interface as blocked. Once the HBA's
2949 * management interface is marked as blocked, all the user space access to
2950 * the HBA, whether they are from sysfs interface or libdfc interface will
2951 * all be blocked. The HBA is set to block the management interface when the
2952 * driver prepares the HBA interface for online or offline.
2953 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002954static void
James Smart618a5232012-06-12 13:54:36 -04002955lpfc_block_mgmt_io(struct lpfc_hba *phba, int mbx_action)
Adrian Bunka6ababd2007-11-05 18:07:33 +01002956{
2957 unsigned long iflag;
James Smart6e7288d2010-06-07 15:23:35 -04002958 uint8_t actcmd = MBX_HEARTBEAT;
2959 unsigned long timeout;
2960
Adrian Bunka6ababd2007-11-05 18:07:33 +01002961 spin_lock_irqsave(&phba->hbalock, iflag);
2962 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
James Smart618a5232012-06-12 13:54:36 -04002963 spin_unlock_irqrestore(&phba->hbalock, iflag);
2964 if (mbx_action == LPFC_MBX_NO_WAIT)
2965 return;
2966 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
2967 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002968 if (phba->sli.mbox_active) {
James Smart6e7288d2010-06-07 15:23:35 -04002969 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
James Smarta183a152011-10-10 21:32:43 -04002970 /* Determine how long we might wait for the active mailbox
2971 * command to be gracefully completed by firmware.
2972 */
2973 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
2974 phba->sli.mbox_active) * 1000) + jiffies;
2975 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002976 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002977
James Smart6e7288d2010-06-07 15:23:35 -04002978 /* Wait for the outstnading mailbox command to complete */
2979 while (phba->sli.mbox_active) {
2980 /* Check active mailbox complete status every 2ms */
2981 msleep(2);
2982 if (time_after(jiffies, timeout)) {
2983 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2984 "2813 Mgmt IO is Blocked %x "
2985 "- mbox cmd %x still active\n",
2986 phba->sli.sli_flag, actcmd);
2987 break;
2988 }
2989 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002990}
2991
James Smarte59058c2008-08-24 21:49:00 -04002992/**
James Smart6b5151f2012-01-18 16:24:06 -05002993 * lpfc_sli4_node_prep - Assign RPIs for active nodes.
2994 * @phba: pointer to lpfc hba data structure.
2995 *
2996 * Allocate RPIs for all active remote nodes. This is needed whenever
2997 * an SLI4 adapter is reset and the driver is not unloading. Its purpose
2998 * is to fixup the temporary rpi assignments.
2999 **/
3000void
3001lpfc_sli4_node_prep(struct lpfc_hba *phba)
3002{
3003 struct lpfc_nodelist *ndlp, *next_ndlp;
3004 struct lpfc_vport **vports;
James Smart9d3d3402017-04-21 16:05:00 -07003005 int i, rpi;
3006 unsigned long flags;
James Smart6b5151f2012-01-18 16:24:06 -05003007
3008 if (phba->sli_rev != LPFC_SLI_REV4)
3009 return;
3010
3011 vports = lpfc_create_vport_work_array(phba);
James Smart9d3d3402017-04-21 16:05:00 -07003012 if (vports == NULL)
3013 return;
James Smart6b5151f2012-01-18 16:24:06 -05003014
James Smart9d3d3402017-04-21 16:05:00 -07003015 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3016 if (vports[i]->load_flag & FC_UNLOADING)
3017 continue;
3018
3019 list_for_each_entry_safe(ndlp, next_ndlp,
3020 &vports[i]->fc_nodes,
3021 nlp_listp) {
3022 if (!NLP_CHK_NODE_ACT(ndlp))
3023 continue;
3024 rpi = lpfc_sli4_alloc_rpi(phba);
3025 if (rpi == LPFC_RPI_ALLOC_ERROR) {
3026 spin_lock_irqsave(&phba->ndlp_lock, flags);
3027 NLP_CLR_NODE_ACT(ndlp);
3028 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3029 continue;
James Smart6b5151f2012-01-18 16:24:06 -05003030 }
James Smart9d3d3402017-04-21 16:05:00 -07003031 ndlp->nlp_rpi = rpi;
3032 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3033 "0009 rpi:%x DID:%x "
3034 "flg:%x map:%x %p\n", ndlp->nlp_rpi,
3035 ndlp->nlp_DID, ndlp->nlp_flag,
3036 ndlp->nlp_usg_map, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -05003037 }
3038 }
3039 lpfc_destroy_vport_work_array(phba, vports);
3040}
3041
3042/**
James Smart3621a712009-04-06 18:47:14 -04003043 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04003044 * @phba: pointer to lpfc hba data structure.
3045 *
3046 * This routine initializes the HBA and brings a HBA online. During this
3047 * process, the management interface is blocked to prevent user space access
3048 * to the HBA interfering with the driver initialization.
3049 *
3050 * Return codes
3051 * 0 - successful
3052 * 1 - failed
3053 **/
dea31012005-04-17 16:05:31 -05003054int
James Smart2e0fef82007-06-17 19:56:36 -05003055lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003056{
Julia Lawall372bd282008-12-16 16:15:08 +01003057 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04003058 struct lpfc_vport **vports;
Dick Kennedya145fda2017-08-23 16:55:44 -07003059 int i, error = 0;
James Smart16a3a202013-04-17 20:14:38 -04003060 bool vpis_cleared = false;
James Smart2e0fef82007-06-17 19:56:36 -05003061
dea31012005-04-17 16:05:31 -05003062 if (!phba)
3063 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01003064 vport = phba->pport;
dea31012005-04-17 16:05:31 -05003065
James Smart2e0fef82007-06-17 19:56:36 -05003066 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05003067 return 0;
3068
James Smarted957682007-06-17 19:56:37 -05003069 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003070 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05003071
James Smart618a5232012-06-12 13:54:36 -04003072 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05003073
James Smartda0436e2009-05-22 14:51:39 -04003074 if (phba->sli_rev == LPFC_SLI_REV4) {
3075 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
3076 lpfc_unblock_mgmt_io(phba);
3077 return 1;
3078 }
James Smart16a3a202013-04-17 20:14:38 -04003079 spin_lock_irq(&phba->hbalock);
3080 if (!phba->sli4_hba.max_cfg_param.vpi_used)
3081 vpis_cleared = true;
3082 spin_unlock_irq(&phba->hbalock);
Dick Kennedya145fda2017-08-23 16:55:44 -07003083
3084 /* Reestablish the local initiator port.
3085 * The offline process destroyed the previous lport.
3086 */
3087 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME &&
3088 !phba->nvmet_support) {
3089 error = lpfc_nvme_create_localport(phba->pport);
3090 if (error)
3091 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3092 "6132 NVME restore reg failed "
3093 "on nvmei error x%x\n", error);
3094 }
James Smartda0436e2009-05-22 14:51:39 -04003095 } else {
James Smart895427b2017-02-12 13:52:30 -08003096 lpfc_sli_queue_init(phba);
James Smartda0436e2009-05-22 14:51:39 -04003097 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
3098 lpfc_unblock_mgmt_io(phba);
3099 return 1;
3100 }
James Smart46fa3112007-04-25 09:51:45 -04003101 }
dea31012005-04-17 16:05:31 -05003102
James Smart549e55c2007-08-02 11:09:51 -04003103 vports = lpfc_create_vport_work_array(phba);
Arnd Bergmannaeb66412016-03-14 15:29:44 +01003104 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04003105 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04003106 struct Scsi_Host *shost;
3107 shost = lpfc_shost_from_vport(vports[i]);
3108 spin_lock_irq(shost->host_lock);
3109 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
3110 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
3111 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart16a3a202013-04-17 20:14:38 -04003112 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart1c6834a2009-07-19 10:01:26 -04003113 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart16a3a202013-04-17 20:14:38 -04003114 if ((vpis_cleared) &&
3115 (vports[i]->port_type !=
3116 LPFC_PHYSICAL_PORT))
3117 vports[i]->vpi = 0;
3118 }
James Smart549e55c2007-08-02 11:09:51 -04003119 spin_unlock_irq(shost->host_lock);
3120 }
Arnd Bergmannaeb66412016-03-14 15:29:44 +01003121 }
3122 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05003123
James Smart46fa3112007-04-25 09:51:45 -04003124 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05003125 return 0;
3126}
3127
James Smarte59058c2008-08-24 21:49:00 -04003128/**
James Smart3621a712009-04-06 18:47:14 -04003129 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04003130 * @phba: pointer to lpfc hba data structure.
3131 *
3132 * This routine marks a HBA's management interface as not blocked. Once the
3133 * HBA's management interface is marked as not blocked, all the user space
3134 * access to the HBA, whether they are from sysfs interface or libdfc
3135 * interface will be allowed. The HBA is set to block the management interface
3136 * when the driver prepares the HBA interface for online or offline and then
3137 * set to unblock the management interface afterwards.
3138 **/
James Smart46fa3112007-04-25 09:51:45 -04003139void
James Smart46fa3112007-04-25 09:51:45 -04003140lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
3141{
3142 unsigned long iflag;
3143
James Smart2e0fef82007-06-17 19:56:36 -05003144 spin_lock_irqsave(&phba->hbalock, iflag);
3145 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
3146 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04003147}
3148
James Smarte59058c2008-08-24 21:49:00 -04003149/**
James Smart3621a712009-04-06 18:47:14 -04003150 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04003151 * @phba: pointer to lpfc hba data structure.
3152 *
3153 * This routine is invoked to prepare a HBA to be brought offline. It performs
3154 * unregistration login to all the nodes on all vports and flushes the mailbox
3155 * queue to make it ready to be brought offline.
3156 **/
James Smart46fa3112007-04-25 09:51:45 -04003157void
James Smart618a5232012-06-12 13:54:36 -04003158lpfc_offline_prep(struct lpfc_hba *phba, int mbx_action)
James Smart46fa3112007-04-25 09:51:45 -04003159{
James Smart2e0fef82007-06-17 19:56:36 -05003160 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04003161 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04003162 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05003163 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04003164 int i;
dea31012005-04-17 16:05:31 -05003165
James Smart2e0fef82007-06-17 19:56:36 -05003166 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04003167 return;
dea31012005-04-17 16:05:31 -05003168
James Smart618a5232012-06-12 13:54:36 -04003169 lpfc_block_mgmt_io(phba, mbx_action);
dea31012005-04-17 16:05:31 -05003170
3171 lpfc_linkdown(phba);
3172
James Smart87af33f2007-10-27 13:37:43 -04003173 /* Issue an unreg_login to all nodes on all vports */
3174 vports = lpfc_create_vport_work_array(phba);
3175 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04003176 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04003177 if (vports[i]->load_flag & FC_UNLOADING)
3178 continue;
James Smart72100cc2010-02-12 14:43:01 -05003179 shost = lpfc_shost_from_vport(vports[i]);
3180 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003181 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05003182 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
3183 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05003184 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05003185
James Smart87af33f2007-10-27 13:37:43 -04003186 shost = lpfc_shost_from_vport(vports[i]);
3187 list_for_each_entry_safe(ndlp, next_ndlp,
3188 &vports[i]->fc_nodes,
3189 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05003190 if (!NLP_CHK_NODE_ACT(ndlp))
3191 continue;
James Smart87af33f2007-10-27 13:37:43 -04003192 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
3193 continue;
3194 if (ndlp->nlp_type & NLP_FABRIC) {
3195 lpfc_disc_state_machine(vports[i], ndlp,
3196 NULL, NLP_EVT_DEVICE_RECOVERY);
3197 lpfc_disc_state_machine(vports[i], ndlp,
3198 NULL, NLP_EVT_DEVICE_RM);
3199 }
3200 spin_lock_irq(shost->host_lock);
3201 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart401ee0c2012-03-01 22:35:34 -05003202 spin_unlock_irq(shost->host_lock);
James Smart6b5151f2012-01-18 16:24:06 -05003203 /*
3204 * Whenever an SLI4 port goes offline, free the
James Smart401ee0c2012-03-01 22:35:34 -05003205 * RPI. Get a new RPI when the adapter port
3206 * comes back online.
James Smart6b5151f2012-01-18 16:24:06 -05003207 */
James Smartbe6bb942015-04-07 15:07:22 -04003208 if (phba->sli_rev == LPFC_SLI_REV4) {
3209 lpfc_printf_vlog(ndlp->vport,
3210 KERN_INFO, LOG_NODE,
3211 "0011 lpfc_offline: "
3212 "ndlp:x%p did %x "
3213 "usgmap:x%x rpi:%x\n",
3214 ndlp, ndlp->nlp_DID,
3215 ndlp->nlp_usg_map,
3216 ndlp->nlp_rpi);
3217
James Smart6b5151f2012-01-18 16:24:06 -05003218 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smartbe6bb942015-04-07 15:07:22 -04003219 }
James Smart87af33f2007-10-27 13:37:43 -04003220 lpfc_unreg_rpi(vports[i], ndlp);
3221 }
3222 }
3223 }
James Smart09372822008-01-11 01:52:54 -05003224 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05003225
James Smart618a5232012-06-12 13:54:36 -04003226 lpfc_sli_mbox_sys_shutdown(phba, mbx_action);
Dick Kennedyf485c182017-09-29 17:34:34 -07003227
3228 if (phba->wq)
3229 flush_workqueue(phba->wq);
James Smart46fa3112007-04-25 09:51:45 -04003230}
3231
James Smarte59058c2008-08-24 21:49:00 -04003232/**
James Smart3621a712009-04-06 18:47:14 -04003233 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04003234 * @phba: pointer to lpfc hba data structure.
3235 *
3236 * This routine actually brings a HBA offline. It stops all the timers
3237 * associated with the HBA, brings down the SLI layer, and eventually
3238 * marks the HBA as in offline state for the upper layer protocol.
3239 **/
James Smart46fa3112007-04-25 09:51:45 -04003240void
James Smart2e0fef82007-06-17 19:56:36 -05003241lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04003242{
James Smart549e55c2007-08-02 11:09:51 -04003243 struct Scsi_Host *shost;
3244 struct lpfc_vport **vports;
3245 int i;
James Smart46fa3112007-04-25 09:51:45 -04003246
James Smart549e55c2007-08-02 11:09:51 -04003247 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04003248 return;
James Smart688a8862006-07-06 15:49:56 -04003249
James Smartda0436e2009-05-22 14:51:39 -04003250 /* stop port and all timers associated with this hba */
3251 lpfc_stop_port(phba);
Dick Kennedy4b40d022017-08-23 16:55:38 -07003252
3253 /* Tear down the local and target port registrations. The
3254 * nvme transports need to cleanup.
3255 */
3256 lpfc_nvmet_destroy_targetport(phba);
3257 lpfc_nvme_destroy_localport(phba->pport);
3258
James Smart51ef4c22007-08-02 11:10:31 -04003259 vports = lpfc_create_vport_work_array(phba);
3260 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04003261 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04003262 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05003263 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05003264 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003265 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05003266 /* Bring down the SLI Layer and cleanup. The HBA is offline
3267 now. */
3268 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003269 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04003270 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003271 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04003272 vports = lpfc_create_vport_work_array(phba);
3273 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04003274 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04003275 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04003276 spin_lock_irq(shost->host_lock);
3277 vports[i]->work_port_events = 0;
3278 vports[i]->fc_flag |= FC_OFFLINE_MODE;
3279 spin_unlock_irq(shost->host_lock);
3280 }
James Smart09372822008-01-11 01:52:54 -05003281 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05003282}
3283
James Smarte59058c2008-08-24 21:49:00 -04003284/**
James Smart3621a712009-04-06 18:47:14 -04003285 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04003286 * @phba: pointer to lpfc hba data structure.
3287 *
3288 * This routine is to free all the SCSI buffers and IOCBs from the driver
3289 * list back to kernel. It is called from lpfc_pci_remove_one to free
3290 * the internal resources before the device is removed from the system.
James Smarte59058c2008-08-24 21:49:00 -04003291 **/
James Smart8a9d2e82012-05-09 21:16:12 -04003292static void
James Smart2e0fef82007-06-17 19:56:36 -05003293lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003294{
3295 struct lpfc_scsi_buf *sb, *sb_next;
dea31012005-04-17 16:05:31 -05003296
James Smart895427b2017-02-12 13:52:30 -08003297 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
3298 return;
3299
James Smart2e0fef82007-06-17 19:56:36 -05003300 spin_lock_irq(&phba->hbalock);
James Smarta40fc5f2013-04-17 20:17:40 -04003301
dea31012005-04-17 16:05:31 -05003302 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smarta40fc5f2013-04-17 20:17:40 -04003303
3304 spin_lock(&phba->scsi_buf_list_put_lock);
3305 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_put,
3306 list) {
dea31012005-04-17 16:05:31 -05003307 list_del(&sb->list);
Romain Perier771db5c2017-07-06 10:13:05 +02003308 dma_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05003309 sb->dma_handle);
dea31012005-04-17 16:05:31 -05003310 kfree(sb);
3311 phba->total_scsi_bufs--;
3312 }
James Smarta40fc5f2013-04-17 20:17:40 -04003313 spin_unlock(&phba->scsi_buf_list_put_lock);
3314
3315 spin_lock(&phba->scsi_buf_list_get_lock);
3316 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_get,
3317 list) {
3318 list_del(&sb->list);
Romain Perier771db5c2017-07-06 10:13:05 +02003319 dma_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
James Smarta40fc5f2013-04-17 20:17:40 -04003320 sb->dma_handle);
3321 kfree(sb);
3322 phba->total_scsi_bufs--;
3323 }
3324 spin_unlock(&phba->scsi_buf_list_get_lock);
James Smart2e0fef82007-06-17 19:56:36 -05003325 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003326}
James Smart8a9d2e82012-05-09 21:16:12 -04003327/**
James Smart895427b2017-02-12 13:52:30 -08003328 * lpfc_nvme_free - Free all the NVME buffers and IOCBs from driver lists
3329 * @phba: pointer to lpfc hba data structure.
3330 *
3331 * This routine is to free all the NVME buffers and IOCBs from the driver
3332 * list back to kernel. It is called from lpfc_pci_remove_one to free
3333 * the internal resources before the device is removed from the system.
3334 **/
3335static void
3336lpfc_nvme_free(struct lpfc_hba *phba)
3337{
3338 struct lpfc_nvme_buf *lpfc_ncmd, *lpfc_ncmd_next;
James Smart895427b2017-02-12 13:52:30 -08003339
3340 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
3341 return;
3342
3343 spin_lock_irq(&phba->hbalock);
3344
3345 /* Release all the lpfc_nvme_bufs maintained by this host. */
3346 spin_lock(&phba->nvme_buf_list_put_lock);
3347 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3348 &phba->lpfc_nvme_buf_list_put, list) {
3349 list_del(&lpfc_ncmd->list);
James Smartcf1a1d32017-12-08 17:18:03 -08003350 phba->put_nvme_bufs--;
Romain Perier771db5c2017-07-06 10:13:05 +02003351 dma_pool_free(phba->lpfc_sg_dma_buf_pool, lpfc_ncmd->data,
James Smart895427b2017-02-12 13:52:30 -08003352 lpfc_ncmd->dma_handle);
3353 kfree(lpfc_ncmd);
3354 phba->total_nvme_bufs--;
3355 }
3356 spin_unlock(&phba->nvme_buf_list_put_lock);
3357
3358 spin_lock(&phba->nvme_buf_list_get_lock);
3359 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3360 &phba->lpfc_nvme_buf_list_get, list) {
3361 list_del(&lpfc_ncmd->list);
James Smartcf1a1d32017-12-08 17:18:03 -08003362 phba->get_nvme_bufs--;
Romain Perier771db5c2017-07-06 10:13:05 +02003363 dma_pool_free(phba->lpfc_sg_dma_buf_pool, lpfc_ncmd->data,
James Smart895427b2017-02-12 13:52:30 -08003364 lpfc_ncmd->dma_handle);
3365 kfree(lpfc_ncmd);
3366 phba->total_nvme_bufs--;
3367 }
3368 spin_unlock(&phba->nvme_buf_list_get_lock);
James Smart895427b2017-02-12 13:52:30 -08003369 spin_unlock_irq(&phba->hbalock);
3370}
3371/**
3372 * lpfc_sli4_els_sgl_update - update ELS xri-sgl sizing and mapping
James Smart8a9d2e82012-05-09 21:16:12 -04003373 * @phba: pointer to lpfc hba data structure.
3374 *
3375 * This routine first calculates the sizes of the current els and allocated
3376 * scsi sgl lists, and then goes through all sgls to updates the physical
3377 * XRIs assigned due to port function reset. During port initialization, the
3378 * current els and allocated scsi sgl lists are 0s.
3379 *
3380 * Return codes
3381 * 0 - successful (for now, it always returns 0)
3382 **/
3383int
James Smart895427b2017-02-12 13:52:30 -08003384lpfc_sli4_els_sgl_update(struct lpfc_hba *phba)
James Smart8a9d2e82012-05-09 21:16:12 -04003385{
3386 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
James Smart895427b2017-02-12 13:52:30 -08003387 uint16_t i, lxri, xri_cnt, els_xri_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04003388 LIST_HEAD(els_sgl_list);
James Smart8a9d2e82012-05-09 21:16:12 -04003389 int rc;
3390
3391 /*
3392 * update on pci function's els xri-sgl list
3393 */
3394 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
James Smart895427b2017-02-12 13:52:30 -08003395
James Smart8a9d2e82012-05-09 21:16:12 -04003396 if (els_xri_cnt > phba->sli4_hba.els_xri_cnt) {
3397 /* els xri-sgl expanded */
3398 xri_cnt = els_xri_cnt - phba->sli4_hba.els_xri_cnt;
3399 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3400 "3157 ELS xri-sgl count increased from "
3401 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3402 els_xri_cnt);
3403 /* allocate the additional els sgls */
3404 for (i = 0; i < xri_cnt; i++) {
3405 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3406 GFP_KERNEL);
3407 if (sglq_entry == NULL) {
3408 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3409 "2562 Failure to allocate an "
3410 "ELS sgl entry:%d\n", i);
3411 rc = -ENOMEM;
3412 goto out_free_mem;
3413 }
3414 sglq_entry->buff_type = GEN_BUFF_TYPE;
3415 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0,
3416 &sglq_entry->phys);
3417 if (sglq_entry->virt == NULL) {
3418 kfree(sglq_entry);
3419 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3420 "2563 Failure to allocate an "
3421 "ELS mbuf:%d\n", i);
3422 rc = -ENOMEM;
3423 goto out_free_mem;
3424 }
3425 sglq_entry->sgl = sglq_entry->virt;
3426 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
3427 sglq_entry->state = SGL_FREED;
3428 list_add_tail(&sglq_entry->list, &els_sgl_list);
3429 }
James Smart38c20672013-03-01 16:37:44 -05003430 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08003431 spin_lock(&phba->sli4_hba.sgl_list_lock);
3432 list_splice_init(&els_sgl_list,
3433 &phba->sli4_hba.lpfc_els_sgl_list);
3434 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05003435 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003436 } else if (els_xri_cnt < phba->sli4_hba.els_xri_cnt) {
3437 /* els xri-sgl shrinked */
3438 xri_cnt = phba->sli4_hba.els_xri_cnt - els_xri_cnt;
3439 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3440 "3158 ELS xri-sgl count decreased from "
3441 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3442 els_xri_cnt);
3443 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08003444 spin_lock(&phba->sli4_hba.sgl_list_lock);
3445 list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list,
3446 &els_sgl_list);
James Smart8a9d2e82012-05-09 21:16:12 -04003447 /* release extra els sgls from list */
3448 for (i = 0; i < xri_cnt; i++) {
3449 list_remove_head(&els_sgl_list,
3450 sglq_entry, struct lpfc_sglq, list);
3451 if (sglq_entry) {
James Smart895427b2017-02-12 13:52:30 -08003452 __lpfc_mbuf_free(phba, sglq_entry->virt,
3453 sglq_entry->phys);
James Smart8a9d2e82012-05-09 21:16:12 -04003454 kfree(sglq_entry);
3455 }
3456 }
James Smart895427b2017-02-12 13:52:30 -08003457 list_splice_init(&els_sgl_list,
3458 &phba->sli4_hba.lpfc_els_sgl_list);
3459 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003460 spin_unlock_irq(&phba->hbalock);
3461 } else
3462 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3463 "3163 ELS xri-sgl count unchanged: %d\n",
3464 els_xri_cnt);
3465 phba->sli4_hba.els_xri_cnt = els_xri_cnt;
3466
3467 /* update xris to els sgls on the list */
3468 sglq_entry = NULL;
3469 sglq_entry_next = NULL;
3470 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
James Smart895427b2017-02-12 13:52:30 -08003471 &phba->sli4_hba.lpfc_els_sgl_list, list) {
James Smart8a9d2e82012-05-09 21:16:12 -04003472 lxri = lpfc_sli4_next_xritag(phba);
3473 if (lxri == NO_XRI) {
3474 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3475 "2400 Failed to allocate xri for "
3476 "ELS sgl\n");
3477 rc = -ENOMEM;
3478 goto out_free_mem;
3479 }
3480 sglq_entry->sli4_lxritag = lxri;
3481 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3482 }
James Smart895427b2017-02-12 13:52:30 -08003483 return 0;
3484
3485out_free_mem:
3486 lpfc_free_els_sgl_list(phba);
3487 return rc;
3488}
3489
3490/**
James Smartf358dd02017-02-12 13:52:34 -08003491 * lpfc_sli4_nvmet_sgl_update - update xri-sgl sizing and mapping
3492 * @phba: pointer to lpfc hba data structure.
3493 *
3494 * This routine first calculates the sizes of the current els and allocated
3495 * scsi sgl lists, and then goes through all sgls to updates the physical
3496 * XRIs assigned due to port function reset. During port initialization, the
3497 * current els and allocated scsi sgl lists are 0s.
3498 *
3499 * Return codes
3500 * 0 - successful (for now, it always returns 0)
3501 **/
3502int
3503lpfc_sli4_nvmet_sgl_update(struct lpfc_hba *phba)
3504{
3505 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
3506 uint16_t i, lxri, xri_cnt, els_xri_cnt;
James Smart6c621a22017-05-15 15:20:45 -07003507 uint16_t nvmet_xri_cnt;
James Smartf358dd02017-02-12 13:52:34 -08003508 LIST_HEAD(nvmet_sgl_list);
3509 int rc;
3510
3511 /*
3512 * update on pci function's nvmet xri-sgl list
3513 */
3514 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
James Smart61f3d4b2017-05-15 15:20:41 -07003515
James Smart6c621a22017-05-15 15:20:45 -07003516 /* For NVMET, ALL remaining XRIs are dedicated for IO processing */
3517 nvmet_xri_cnt = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
James Smartf358dd02017-02-12 13:52:34 -08003518 if (nvmet_xri_cnt > phba->sli4_hba.nvmet_xri_cnt) {
3519 /* els xri-sgl expanded */
3520 xri_cnt = nvmet_xri_cnt - phba->sli4_hba.nvmet_xri_cnt;
3521 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3522 "6302 NVMET xri-sgl cnt grew from %d to %d\n",
3523 phba->sli4_hba.nvmet_xri_cnt, nvmet_xri_cnt);
3524 /* allocate the additional nvmet sgls */
3525 for (i = 0; i < xri_cnt; i++) {
3526 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3527 GFP_KERNEL);
3528 if (sglq_entry == NULL) {
3529 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3530 "6303 Failure to allocate an "
3531 "NVMET sgl entry:%d\n", i);
3532 rc = -ENOMEM;
3533 goto out_free_mem;
3534 }
3535 sglq_entry->buff_type = NVMET_BUFF_TYPE;
3536 sglq_entry->virt = lpfc_nvmet_buf_alloc(phba, 0,
3537 &sglq_entry->phys);
3538 if (sglq_entry->virt == NULL) {
3539 kfree(sglq_entry);
3540 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3541 "6304 Failure to allocate an "
3542 "NVMET buf:%d\n", i);
3543 rc = -ENOMEM;
3544 goto out_free_mem;
3545 }
3546 sglq_entry->sgl = sglq_entry->virt;
3547 memset(sglq_entry->sgl, 0,
3548 phba->cfg_sg_dma_buf_size);
3549 sglq_entry->state = SGL_FREED;
3550 list_add_tail(&sglq_entry->list, &nvmet_sgl_list);
3551 }
3552 spin_lock_irq(&phba->hbalock);
3553 spin_lock(&phba->sli4_hba.sgl_list_lock);
3554 list_splice_init(&nvmet_sgl_list,
3555 &phba->sli4_hba.lpfc_nvmet_sgl_list);
3556 spin_unlock(&phba->sli4_hba.sgl_list_lock);
3557 spin_unlock_irq(&phba->hbalock);
3558 } else if (nvmet_xri_cnt < phba->sli4_hba.nvmet_xri_cnt) {
3559 /* nvmet xri-sgl shrunk */
3560 xri_cnt = phba->sli4_hba.nvmet_xri_cnt - nvmet_xri_cnt;
3561 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3562 "6305 NVMET xri-sgl count decreased from "
3563 "%d to %d\n", phba->sli4_hba.nvmet_xri_cnt,
3564 nvmet_xri_cnt);
3565 spin_lock_irq(&phba->hbalock);
3566 spin_lock(&phba->sli4_hba.sgl_list_lock);
3567 list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list,
3568 &nvmet_sgl_list);
3569 /* release extra nvmet sgls from list */
3570 for (i = 0; i < xri_cnt; i++) {
3571 list_remove_head(&nvmet_sgl_list,
3572 sglq_entry, struct lpfc_sglq, list);
3573 if (sglq_entry) {
3574 lpfc_nvmet_buf_free(phba, sglq_entry->virt,
3575 sglq_entry->phys);
3576 kfree(sglq_entry);
3577 }
3578 }
3579 list_splice_init(&nvmet_sgl_list,
3580 &phba->sli4_hba.lpfc_nvmet_sgl_list);
3581 spin_unlock(&phba->sli4_hba.sgl_list_lock);
3582 spin_unlock_irq(&phba->hbalock);
3583 } else
3584 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3585 "6306 NVMET xri-sgl count unchanged: %d\n",
3586 nvmet_xri_cnt);
3587 phba->sli4_hba.nvmet_xri_cnt = nvmet_xri_cnt;
3588
3589 /* update xris to nvmet sgls on the list */
3590 sglq_entry = NULL;
3591 sglq_entry_next = NULL;
3592 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
3593 &phba->sli4_hba.lpfc_nvmet_sgl_list, list) {
3594 lxri = lpfc_sli4_next_xritag(phba);
3595 if (lxri == NO_XRI) {
3596 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3597 "6307 Failed to allocate xri for "
3598 "NVMET sgl\n");
3599 rc = -ENOMEM;
3600 goto out_free_mem;
3601 }
3602 sglq_entry->sli4_lxritag = lxri;
3603 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3604 }
3605 return 0;
3606
3607out_free_mem:
3608 lpfc_free_nvmet_sgl_list(phba);
3609 return rc;
3610}
3611
3612/**
James Smart895427b2017-02-12 13:52:30 -08003613 * lpfc_sli4_scsi_sgl_update - update xri-sgl sizing and mapping
3614 * @phba: pointer to lpfc hba data structure.
3615 *
3616 * This routine first calculates the sizes of the current els and allocated
3617 * scsi sgl lists, and then goes through all sgls to updates the physical
3618 * XRIs assigned due to port function reset. During port initialization, the
3619 * current els and allocated scsi sgl lists are 0s.
3620 *
3621 * Return codes
3622 * 0 - successful (for now, it always returns 0)
3623 **/
3624int
3625lpfc_sli4_scsi_sgl_update(struct lpfc_hba *phba)
3626{
3627 struct lpfc_scsi_buf *psb, *psb_next;
3628 uint16_t i, lxri, els_xri_cnt, scsi_xri_cnt;
3629 LIST_HEAD(scsi_sgl_list);
3630 int rc;
3631
3632 /*
3633 * update on pci function's els xri-sgl list
3634 */
3635 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3636 phba->total_scsi_bufs = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04003637
3638 /*
3639 * update on pci function's allocated scsi xri-sgl list
3640 */
James Smart8a9d2e82012-05-09 21:16:12 -04003641 /* maximum number of xris available for scsi buffers */
3642 phba->sli4_hba.scsi_xri_max = phba->sli4_hba.max_cfg_param.max_xri -
3643 els_xri_cnt;
3644
James Smart895427b2017-02-12 13:52:30 -08003645 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
3646 return 0;
3647
3648 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3649 phba->sli4_hba.scsi_xri_max = /* Split them up */
3650 (phba->sli4_hba.scsi_xri_max *
3651 phba->cfg_xri_split) / 100;
James Smart8a9d2e82012-05-09 21:16:12 -04003652
James Smarta40fc5f2013-04-17 20:17:40 -04003653 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart164cecd2013-09-06 12:22:38 -04003654 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003655 list_splice_init(&phba->lpfc_scsi_buf_list_get, &scsi_sgl_list);
3656 list_splice(&phba->lpfc_scsi_buf_list_put, &scsi_sgl_list);
James Smart164cecd2013-09-06 12:22:38 -04003657 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003658 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003659
James Smarte8c0a772017-04-21 16:04:49 -07003660 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3661 "6060 Current allocated SCSI xri-sgl count:%d, "
3662 "maximum SCSI xri count:%d (split:%d)\n",
3663 phba->sli4_hba.scsi_xri_cnt,
3664 phba->sli4_hba.scsi_xri_max, phba->cfg_xri_split);
3665
James Smart8a9d2e82012-05-09 21:16:12 -04003666 if (phba->sli4_hba.scsi_xri_cnt > phba->sli4_hba.scsi_xri_max) {
3667 /* max scsi xri shrinked below the allocated scsi buffers */
3668 scsi_xri_cnt = phba->sli4_hba.scsi_xri_cnt -
3669 phba->sli4_hba.scsi_xri_max;
3670 /* release the extra allocated scsi buffers */
3671 for (i = 0; i < scsi_xri_cnt; i++) {
3672 list_remove_head(&scsi_sgl_list, psb,
3673 struct lpfc_scsi_buf, list);
James Smarta2fc4aef2014-09-03 12:57:55 -04003674 if (psb) {
Romain Perier771db5c2017-07-06 10:13:05 +02003675 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
James Smarta2fc4aef2014-09-03 12:57:55 -04003676 psb->data, psb->dma_handle);
3677 kfree(psb);
3678 }
James Smart8a9d2e82012-05-09 21:16:12 -04003679 }
James Smarta40fc5f2013-04-17 20:17:40 -04003680 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003681 phba->sli4_hba.scsi_xri_cnt -= scsi_xri_cnt;
James Smarta40fc5f2013-04-17 20:17:40 -04003682 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003683 }
3684
3685 /* update xris associated to remaining allocated scsi buffers */
3686 psb = NULL;
3687 psb_next = NULL;
3688 list_for_each_entry_safe(psb, psb_next, &scsi_sgl_list, list) {
3689 lxri = lpfc_sli4_next_xritag(phba);
3690 if (lxri == NO_XRI) {
3691 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3692 "2560 Failed to allocate xri for "
3693 "scsi buffer\n");
3694 rc = -ENOMEM;
3695 goto out_free_mem;
3696 }
3697 psb->cur_iocbq.sli4_lxritag = lxri;
3698 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3699 }
James Smarta40fc5f2013-04-17 20:17:40 -04003700 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart164cecd2013-09-06 12:22:38 -04003701 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003702 list_splice_init(&scsi_sgl_list, &phba->lpfc_scsi_buf_list_get);
3703 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
James Smart164cecd2013-09-06 12:22:38 -04003704 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003705 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
dea31012005-04-17 16:05:31 -05003706 return 0;
James Smart8a9d2e82012-05-09 21:16:12 -04003707
3708out_free_mem:
James Smart8a9d2e82012-05-09 21:16:12 -04003709 lpfc_scsi_free(phba);
3710 return rc;
dea31012005-04-17 16:05:31 -05003711}
3712
James Smart96418b52017-03-04 09:30:31 -08003713static uint64_t
3714lpfc_get_wwpn(struct lpfc_hba *phba)
3715{
3716 uint64_t wwn;
3717 int rc;
3718 LPFC_MBOXQ_t *mboxq;
3719 MAILBOX_t *mb;
3720
James Smart96418b52017-03-04 09:30:31 -08003721 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
3722 GFP_KERNEL);
3723 if (!mboxq)
3724 return (uint64_t)-1;
3725
3726 /* First get WWN of HBA instance */
3727 lpfc_read_nv(phba, mboxq);
3728 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
3729 if (rc != MBX_SUCCESS) {
3730 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3731 "6019 Mailbox failed , mbxCmd x%x "
3732 "READ_NV, mbxStatus x%x\n",
3733 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
3734 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
3735 mempool_free(mboxq, phba->mbox_mem_pool);
3736 return (uint64_t) -1;
3737 }
3738 mb = &mboxq->u.mb;
3739 memcpy(&wwn, (char *)mb->un.varRDnvp.portname, sizeof(uint64_t));
3740 /* wwn is WWPN of HBA instance */
3741 mempool_free(mboxq, phba->mbox_mem_pool);
3742 if (phba->sli_rev == LPFC_SLI_REV4)
3743 return be64_to_cpu(wwn);
3744 else
Maurizio Lombardi286871a2017-08-23 16:55:48 -07003745 return rol64(wwn, 32);
James Smart96418b52017-03-04 09:30:31 -08003746}
3747
James Smarte59058c2008-08-24 21:49:00 -04003748/**
James Smart895427b2017-02-12 13:52:30 -08003749 * lpfc_sli4_nvme_sgl_update - update xri-sgl sizing and mapping
3750 * @phba: pointer to lpfc hba data structure.
3751 *
3752 * This routine first calculates the sizes of the current els and allocated
3753 * scsi sgl lists, and then goes through all sgls to updates the physical
3754 * XRIs assigned due to port function reset. During port initialization, the
3755 * current els and allocated scsi sgl lists are 0s.
3756 *
3757 * Return codes
3758 * 0 - successful (for now, it always returns 0)
3759 **/
3760int
3761lpfc_sli4_nvme_sgl_update(struct lpfc_hba *phba)
3762{
3763 struct lpfc_nvme_buf *lpfc_ncmd = NULL, *lpfc_ncmd_next = NULL;
3764 uint16_t i, lxri, els_xri_cnt;
3765 uint16_t nvme_xri_cnt, nvme_xri_max;
3766 LIST_HEAD(nvme_sgl_list);
James Smartcf1a1d32017-12-08 17:18:03 -08003767 int rc, cnt;
James Smart895427b2017-02-12 13:52:30 -08003768
3769 phba->total_nvme_bufs = 0;
James Smartcf1a1d32017-12-08 17:18:03 -08003770 phba->get_nvme_bufs = 0;
3771 phba->put_nvme_bufs = 0;
James Smart895427b2017-02-12 13:52:30 -08003772
3773 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
3774 return 0;
3775 /*
3776 * update on pci function's allocated nvme xri-sgl list
3777 */
3778
3779 /* maximum number of xris available for nvme buffers */
3780 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3781 nvme_xri_max = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
3782 phba->sli4_hba.nvme_xri_max = nvme_xri_max;
3783 phba->sli4_hba.nvme_xri_max -= phba->sli4_hba.scsi_xri_max;
3784
3785 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3786 "6074 Current allocated NVME xri-sgl count:%d, "
3787 "maximum NVME xri count:%d\n",
3788 phba->sli4_hba.nvme_xri_cnt,
3789 phba->sli4_hba.nvme_xri_max);
3790
3791 spin_lock_irq(&phba->nvme_buf_list_get_lock);
3792 spin_lock(&phba->nvme_buf_list_put_lock);
3793 list_splice_init(&phba->lpfc_nvme_buf_list_get, &nvme_sgl_list);
3794 list_splice(&phba->lpfc_nvme_buf_list_put, &nvme_sgl_list);
James Smartcf1a1d32017-12-08 17:18:03 -08003795 cnt = phba->get_nvme_bufs + phba->put_nvme_bufs;
3796 phba->get_nvme_bufs = 0;
3797 phba->put_nvme_bufs = 0;
James Smart895427b2017-02-12 13:52:30 -08003798 spin_unlock(&phba->nvme_buf_list_put_lock);
3799 spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3800
3801 if (phba->sli4_hba.nvme_xri_cnt > phba->sli4_hba.nvme_xri_max) {
3802 /* max nvme xri shrunk below the allocated nvme buffers */
3803 spin_lock_irq(&phba->nvme_buf_list_get_lock);
3804 nvme_xri_cnt = phba->sli4_hba.nvme_xri_cnt -
3805 phba->sli4_hba.nvme_xri_max;
3806 spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3807 /* release the extra allocated nvme buffers */
3808 for (i = 0; i < nvme_xri_cnt; i++) {
3809 list_remove_head(&nvme_sgl_list, lpfc_ncmd,
3810 struct lpfc_nvme_buf, list);
3811 if (lpfc_ncmd) {
Romain Perier771db5c2017-07-06 10:13:05 +02003812 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
James Smart895427b2017-02-12 13:52:30 -08003813 lpfc_ncmd->data,
3814 lpfc_ncmd->dma_handle);
3815 kfree(lpfc_ncmd);
3816 }
3817 }
3818 spin_lock_irq(&phba->nvme_buf_list_get_lock);
3819 phba->sli4_hba.nvme_xri_cnt -= nvme_xri_cnt;
3820 spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3821 }
3822
3823 /* update xris associated to remaining allocated nvme buffers */
3824 lpfc_ncmd = NULL;
3825 lpfc_ncmd_next = NULL;
3826 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3827 &nvme_sgl_list, list) {
3828 lxri = lpfc_sli4_next_xritag(phba);
3829 if (lxri == NO_XRI) {
3830 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3831 "6075 Failed to allocate xri for "
3832 "nvme buffer\n");
3833 rc = -ENOMEM;
3834 goto out_free_mem;
3835 }
3836 lpfc_ncmd->cur_iocbq.sli4_lxritag = lxri;
3837 lpfc_ncmd->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3838 }
3839 spin_lock_irq(&phba->nvme_buf_list_get_lock);
3840 spin_lock(&phba->nvme_buf_list_put_lock);
3841 list_splice_init(&nvme_sgl_list, &phba->lpfc_nvme_buf_list_get);
James Smartcf1a1d32017-12-08 17:18:03 -08003842 phba->get_nvme_bufs = cnt;
James Smart895427b2017-02-12 13:52:30 -08003843 INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_put);
3844 spin_unlock(&phba->nvme_buf_list_put_lock);
3845 spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3846 return 0;
3847
3848out_free_mem:
3849 lpfc_nvme_free(phba);
3850 return rc;
3851}
3852
3853/**
James Smart3621a712009-04-06 18:47:14 -04003854 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04003855 * @phba: pointer to lpfc hba data structure.
3856 * @instance: a unique integer ID to this FC port.
3857 * @dev: pointer to the device data structure.
3858 *
3859 * This routine creates a FC port for the upper layer protocol. The FC port
3860 * can be created on top of either a physical port or a virtual port provided
3861 * by the HBA. This routine also allocates a SCSI host data structure (shost)
3862 * and associates the FC port created before adding the shost into the SCSI
3863 * layer.
3864 *
3865 * Return codes
3866 * @vport - pointer to the virtual N_Port data structure.
3867 * NULL - port create failed.
3868 **/
James Smart2e0fef82007-06-17 19:56:36 -05003869struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04003870lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04003871{
James Smart2e0fef82007-06-17 19:56:36 -05003872 struct lpfc_vport *vport;
James Smart895427b2017-02-12 13:52:30 -08003873 struct Scsi_Host *shost = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003874 int error = 0;
James Smart96418b52017-03-04 09:30:31 -08003875 int i;
3876 uint64_t wwn;
3877 bool use_no_reset_hba = false;
James Smart56bc8022017-06-15 22:56:43 -07003878 int rc;
James Smart96418b52017-03-04 09:30:31 -08003879
James Smart56bc8022017-06-15 22:56:43 -07003880 if (lpfc_no_hba_reset_cnt) {
3881 if (phba->sli_rev < LPFC_SLI_REV4 &&
3882 dev == &phba->pcidev->dev) {
3883 /* Reset the port first */
3884 lpfc_sli_brdrestart(phba);
3885 rc = lpfc_sli_chipset_init(phba);
3886 if (rc)
3887 return NULL;
3888 }
3889 wwn = lpfc_get_wwpn(phba);
3890 }
James Smart96418b52017-03-04 09:30:31 -08003891
3892 for (i = 0; i < lpfc_no_hba_reset_cnt; i++) {
3893 if (wwn == lpfc_no_hba_reset[i]) {
3894 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3895 "6020 Setting use_no_reset port=%llx\n",
3896 wwn);
3897 use_no_reset_hba = true;
3898 break;
3899 }
3900 }
James Smart47a86172007-04-25 09:53:22 -04003901
James Smart895427b2017-02-12 13:52:30 -08003902 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
3903 if (dev != &phba->pcidev->dev) {
3904 shost = scsi_host_alloc(&lpfc_vport_template,
3905 sizeof(struct lpfc_vport));
3906 } else {
James Smart96418b52017-03-04 09:30:31 -08003907 if (!use_no_reset_hba)
James Smart895427b2017-02-12 13:52:30 -08003908 shost = scsi_host_alloc(&lpfc_template,
3909 sizeof(struct lpfc_vport));
3910 else
James Smart96418b52017-03-04 09:30:31 -08003911 shost = scsi_host_alloc(&lpfc_template_no_hr,
James Smart895427b2017-02-12 13:52:30 -08003912 sizeof(struct lpfc_vport));
3913 }
3914 } else if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
3915 shost = scsi_host_alloc(&lpfc_template_nvme,
James Smartea4142f2015-04-07 15:07:13 -04003916 sizeof(struct lpfc_vport));
3917 }
James Smart2e0fef82007-06-17 19:56:36 -05003918 if (!shost)
3919 goto out;
James Smart47a86172007-04-25 09:53:22 -04003920
James Smart2e0fef82007-06-17 19:56:36 -05003921 vport = (struct lpfc_vport *) shost->hostdata;
3922 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05003923 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05003924 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05003925 vport->fc_rscn_flush = 0;
James Smart3de2a652007-08-02 11:09:59 -04003926 lpfc_get_vport_cfgparam(vport);
James Smart895427b2017-02-12 13:52:30 -08003927
James Smart2e0fef82007-06-17 19:56:36 -05003928 shost->unique_id = instance;
3929 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04003930 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05003931 shost->this_id = -1;
3932 shost->max_cmd_len = 16;
James Smart8b0dff12015-05-22 10:42:38 -04003933 shost->nr_hw_queues = phba->cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04003934 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart28baac72010-02-12 14:42:03 -05003935 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04003936 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smartda0436e2009-05-22 14:51:39 -04003937 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
3938 }
James Smart81301a92008-12-04 22:39:46 -05003939
James Smart47a86172007-04-25 09:53:22 -04003940 /*
James Smart2e0fef82007-06-17 19:56:36 -05003941 * Set initial can_queue value since 0 is no longer supported and
3942 * scsi_add_host will fail. This will be adjusted later based on the
3943 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04003944 */
James Smart2e0fef82007-06-17 19:56:36 -05003945 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04003946 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05003947 shost->transportt = lpfc_vport_transport_template;
3948 vport->port_type = LPFC_NPIV_PORT;
3949 } else {
3950 shost->transportt = lpfc_transport_template;
3951 vport->port_type = LPFC_PHYSICAL_PORT;
3952 }
James Smart47a86172007-04-25 09:53:22 -04003953
James Smart2e0fef82007-06-17 19:56:36 -05003954 /* Initialize all internally managed lists. */
3955 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04003956 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05003957 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04003958
Kees Cookf22eb4d2017-09-06 20:24:26 -07003959 timer_setup(&vport->fc_disctmo, lpfc_disc_timeout, 0);
James Smart47a86172007-04-25 09:53:22 -04003960
Kees Cookf22eb4d2017-09-06 20:24:26 -07003961 timer_setup(&vport->els_tmofunc, lpfc_els_timeout, 0);
James Smart92494142011-02-16 12:39:44 -05003962
Kees Cookf22eb4d2017-09-06 20:24:26 -07003963 timer_setup(&vport->delayed_disc_tmo, lpfc_delayed_disc_tmo, 0);
James Smart92494142011-02-16 12:39:44 -05003964
James Bottomleyd139b9b2009-11-05 13:33:12 -06003965 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05003966 if (error)
3967 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04003968
James Smart549e55c2007-08-02 11:09:51 -04003969 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003970 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04003971 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003972 return vport;
James Smart47a86172007-04-25 09:53:22 -04003973
James Smart2e0fef82007-06-17 19:56:36 -05003974out_put_shost:
3975 scsi_host_put(shost);
3976out:
3977 return NULL;
James Smart47a86172007-04-25 09:53:22 -04003978}
3979
James Smarte59058c2008-08-24 21:49:00 -04003980/**
James Smart3621a712009-04-06 18:47:14 -04003981 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04003982 * @vport: pointer to an lpfc virtual N_Port data structure.
3983 *
3984 * This routine destroys a FC port from the upper layer protocol. All the
3985 * resources associated with the port are released.
3986 **/
James Smart2e0fef82007-06-17 19:56:36 -05003987void
3988destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04003989{
James Smart92d7f7b2007-06-17 19:56:38 -05003990 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3991 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003992
James Smart858c9f62007-06-17 19:56:39 -05003993 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05003994 fc_remove_host(shost);
3995 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04003996
James Smart92d7f7b2007-06-17 19:56:38 -05003997 spin_lock_irq(&phba->hbalock);
3998 list_del_init(&vport->listentry);
3999 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04004000
James Smart92d7f7b2007-06-17 19:56:38 -05004001 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04004002 return;
James Smart47a86172007-04-25 09:53:22 -04004003}
4004
James Smarte59058c2008-08-24 21:49:00 -04004005/**
James Smart3621a712009-04-06 18:47:14 -04004006 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04004007 *
4008 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
4009 * uses the kernel idr facility to perform the task.
4010 *
4011 * Return codes:
4012 * instance - a unique integer ID allocated as the new instance.
4013 * -1 - lpfc get instance failed.
4014 **/
James Smart92d7f7b2007-06-17 19:56:38 -05004015int
4016lpfc_get_instance(void)
4017{
Tejun Heoab516032013-02-27 17:04:44 -08004018 int ret;
James Smart92d7f7b2007-06-17 19:56:38 -05004019
Tejun Heoab516032013-02-27 17:04:44 -08004020 ret = idr_alloc(&lpfc_hba_index, NULL, 0, 0, GFP_KERNEL);
4021 return ret < 0 ? -1 : ret;
James Smart92d7f7b2007-06-17 19:56:38 -05004022}
4023
James Smarte59058c2008-08-24 21:49:00 -04004024/**
James Smart3621a712009-04-06 18:47:14 -04004025 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04004026 * @shost: pointer to SCSI host data structure.
4027 * @time: elapsed time of the scan in jiffies.
4028 *
4029 * This routine is called by the SCSI layer with a SCSI host to determine
4030 * whether the scan host is finished.
4031 *
4032 * Note: there is no scan_start function as adapter initialization will have
4033 * asynchronously kicked off the link initialization.
4034 *
4035 * Return codes
4036 * 0 - SCSI host scan is not over yet.
4037 * 1 - SCSI host scan is over.
4038 **/
James Smart47a86172007-04-25 09:53:22 -04004039int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
4040{
James Smart2e0fef82007-06-17 19:56:36 -05004041 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4042 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05004043 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04004044
James Smart858c9f62007-06-17 19:56:39 -05004045 spin_lock_irq(shost->host_lock);
4046
James Smart51ef4c22007-08-02 11:10:31 -04004047 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05004048 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04004049 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05004050 }
James Smart256ec0d2013-04-17 20:14:58 -04004051 if (time >= msecs_to_jiffies(30 * 1000)) {
James Smart2e0fef82007-06-17 19:56:36 -05004052 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004053 "0461 Scanning longer than 30 "
4054 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05004055 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04004056 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05004057 }
James Smart256ec0d2013-04-17 20:14:58 -04004058 if (time >= msecs_to_jiffies(15 * 1000) &&
4059 phba->link_state <= LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -05004060 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004061 "0465 Link down longer than 15 "
4062 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05004063 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05004064 goto finished;
James Smart47a86172007-04-25 09:53:22 -04004065 }
4066
James Smart2e0fef82007-06-17 19:56:36 -05004067 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05004068 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05004069 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05004070 goto finished;
James Smart256ec0d2013-04-17 20:14:58 -04004071 if (vport->fc_map_cnt == 0 && time < msecs_to_jiffies(2 * 1000))
James Smart858c9f62007-06-17 19:56:39 -05004072 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05004073 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05004074 goto finished;
4075
4076 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04004077
4078finished:
James Smart858c9f62007-06-17 19:56:39 -05004079 spin_unlock_irq(shost->host_lock);
4080 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05004081}
4082
James Smarte59058c2008-08-24 21:49:00 -04004083/**
James Smart3621a712009-04-06 18:47:14 -04004084 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04004085 * @shost: pointer to SCSI host data structure.
4086 *
4087 * This routine initializes a given SCSI host attributes on a FC port. The
4088 * SCSI host can be either on top of a physical port or a virtual port.
4089 **/
James Smart92d7f7b2007-06-17 19:56:38 -05004090void lpfc_host_attrib_init(struct Scsi_Host *shost)
4091{
4092 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4093 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04004094 /*
James Smart2e0fef82007-06-17 19:56:36 -05004095 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04004096 */
4097
James Smart2e0fef82007-06-17 19:56:36 -05004098 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4099 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04004100 fc_host_supported_classes(shost) = FC_COS_CLASS3;
4101
4102 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05004103 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04004104 fc_host_supported_fc4s(shost)[2] = 1;
4105 fc_host_supported_fc4s(shost)[7] = 1;
4106
James Smart92d7f7b2007-06-17 19:56:38 -05004107 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
4108 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04004109
4110 fc_host_supported_speeds(shost) = 0;
James Smartd38dd522015-08-31 16:48:17 -04004111 if (phba->lmt & LMT_32Gb)
4112 fc_host_supported_speeds(shost) |= FC_PORTSPEED_32GBIT;
James Smart88a2cfb2011-07-22 18:36:33 -04004113 if (phba->lmt & LMT_16Gb)
4114 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
James Smart47a86172007-04-25 09:53:22 -04004115 if (phba->lmt & LMT_10Gb)
4116 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04004117 if (phba->lmt & LMT_8Gb)
4118 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04004119 if (phba->lmt & LMT_4Gb)
4120 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
4121 if (phba->lmt & LMT_2Gb)
4122 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
4123 if (phba->lmt & LMT_1Gb)
4124 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
4125
4126 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05004127 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
4128 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04004129
Mike Christie0af5d702010-09-15 16:52:31 -05004130 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
4131
James Smart47a86172007-04-25 09:53:22 -04004132 /* This value is also unchanging */
4133 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05004134 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04004135 fc_host_active_fc4s(shost)[2] = 1;
4136 fc_host_active_fc4s(shost)[7] = 1;
4137
James Smart92d7f7b2007-06-17 19:56:38 -05004138 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04004139 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04004140 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04004141 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04004142}
dea31012005-04-17 16:05:31 -05004143
James Smarte59058c2008-08-24 21:49:00 -04004144/**
James Smartda0436e2009-05-22 14:51:39 -04004145 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04004146 * @phba: pointer to lpfc hba data structure.
4147 *
James Smartda0436e2009-05-22 14:51:39 -04004148 * This routine is invoked to stop an SLI3 device port, it stops the device
4149 * from generating interrupts and stops the device driver's timers for the
4150 * device.
James Smarte59058c2008-08-24 21:49:00 -04004151 **/
James Smartdb2378e2008-02-08 18:49:51 -05004152static void
James Smartda0436e2009-05-22 14:51:39 -04004153lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05004154{
James Smartda0436e2009-05-22 14:51:39 -04004155 /* Clear all interrupt enable conditions */
4156 writel(0, phba->HCregaddr);
4157 readl(phba->HCregaddr); /* flush */
4158 /* Clear all pending interrupts */
4159 writel(0xffffffff, phba->HAregaddr);
4160 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04004161
James Smartda0436e2009-05-22 14:51:39 -04004162 /* Reset some HBA SLI setup states */
4163 lpfc_stop_hba_timers(phba);
4164 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05004165}
4166
James Smarte59058c2008-08-24 21:49:00 -04004167/**
James Smartda0436e2009-05-22 14:51:39 -04004168 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05004169 * @phba: pointer to lpfc hba data structure.
4170 *
James Smartda0436e2009-05-22 14:51:39 -04004171 * This routine is invoked to stop an SLI4 device port, it stops the device
4172 * from generating interrupts and stops the device driver's timers for the
4173 * device.
4174 **/
4175static void
4176lpfc_stop_port_s4(struct lpfc_hba *phba)
4177{
4178 /* Reset some HBA SLI4 setup states */
4179 lpfc_stop_hba_timers(phba);
4180 phba->pport->work_port_events = 0;
4181 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04004182}
4183
4184/**
4185 * lpfc_stop_port - Wrapper function for stopping hba port
4186 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05004187 *
James Smartda0436e2009-05-22 14:51:39 -04004188 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
4189 * the API jump table function pointer from the lpfc_hba struct.
4190 **/
4191void
4192lpfc_stop_port(struct lpfc_hba *phba)
4193{
4194 phba->lpfc_stop_port(phba);
Dick Kennedyf485c182017-09-29 17:34:34 -07004195
4196 if (phba->wq)
4197 flush_workqueue(phba->wq);
James Smartda0436e2009-05-22 14:51:39 -04004198}
4199
4200/**
James Smartecfd03c2010-02-12 14:41:27 -05004201 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
4202 * @phba: Pointer to hba for which this call is being executed.
4203 *
4204 * This routine starts the timer waiting for the FCF rediscovery to complete.
4205 **/
4206void
4207lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
4208{
4209 unsigned long fcf_redisc_wait_tmo =
4210 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
4211 /* Start fcf rediscovery wait period timer */
4212 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
4213 spin_lock_irq(&phba->hbalock);
4214 /* Allow action to new fcf asynchronous event */
4215 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
4216 /* Mark the FCF rediscovery pending state */
4217 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
4218 spin_unlock_irq(&phba->hbalock);
4219}
4220
4221/**
4222 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
4223 * @ptr: Map to lpfc_hba data structure pointer.
4224 *
4225 * This routine is invoked when waiting for FCF table rediscover has been
4226 * timed out. If new FCF record(s) has (have) been discovered during the
4227 * wait period, a new FCF event shall be added to the FCOE async event
4228 * list, and then worker thread shall be waked up for processing from the
4229 * worker thread context.
4230 **/
Rashika Kheriae399b222014-09-03 12:55:28 -04004231static void
Kees Cookf22eb4d2017-09-06 20:24:26 -07004232lpfc_sli4_fcf_redisc_wait_tmo(struct timer_list *t)
James Smartecfd03c2010-02-12 14:41:27 -05004233{
Kees Cookf22eb4d2017-09-06 20:24:26 -07004234 struct lpfc_hba *phba = from_timer(phba, t, fcf.redisc_wait);
James Smartecfd03c2010-02-12 14:41:27 -05004235
4236 /* Don't send FCF rediscovery event if timer cancelled */
4237 spin_lock_irq(&phba->hbalock);
4238 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
4239 spin_unlock_irq(&phba->hbalock);
4240 return;
4241 }
4242 /* Clear FCF rediscovery timer pending flag */
4243 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
4244 /* FCF rediscovery event to worker thread */
4245 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
4246 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004247 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04004248 "2776 FCF rediscover quiescent timer expired\n");
James Smartecfd03c2010-02-12 14:41:27 -05004249 /* wake up worker thread */
4250 lpfc_worker_wake_up(phba);
4251}
4252
4253/**
James Smartda0436e2009-05-22 14:51:39 -04004254 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
4255 * @phba: pointer to lpfc hba data structure.
4256 * @acqe_link: pointer to the async link completion queue entry.
4257 *
4258 * This routine is to parse the SLI4 link-attention link fault code and
4259 * translate it into the base driver's read link attention mailbox command
4260 * status.
4261 *
4262 * Return: Link-attention status in terms of base driver's coding.
4263 **/
4264static uint16_t
4265lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
4266 struct lpfc_acqe_link *acqe_link)
4267{
4268 uint16_t latt_fault;
4269
4270 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
4271 case LPFC_ASYNC_LINK_FAULT_NONE:
4272 case LPFC_ASYNC_LINK_FAULT_LOCAL:
4273 case LPFC_ASYNC_LINK_FAULT_REMOTE:
4274 latt_fault = 0;
4275 break;
4276 default:
4277 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4278 "0398 Invalid link fault code: x%x\n",
4279 bf_get(lpfc_acqe_link_fault, acqe_link));
4280 latt_fault = MBXERR_ERROR;
4281 break;
4282 }
4283 return latt_fault;
4284}
4285
4286/**
4287 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
4288 * @phba: pointer to lpfc hba data structure.
4289 * @acqe_link: pointer to the async link completion queue entry.
4290 *
4291 * This routine is to parse the SLI4 link attention type and translate it
4292 * into the base driver's link attention type coding.
4293 *
4294 * Return: Link attention type in terms of base driver's coding.
4295 **/
4296static uint8_t
4297lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
4298 struct lpfc_acqe_link *acqe_link)
4299{
4300 uint8_t att_type;
4301
4302 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
4303 case LPFC_ASYNC_LINK_STATUS_DOWN:
4304 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
James Smart76a95d72010-11-20 23:11:48 -05004305 att_type = LPFC_ATT_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04004306 break;
4307 case LPFC_ASYNC_LINK_STATUS_UP:
4308 /* Ignore physical link up events - wait for logical link up */
James Smart76a95d72010-11-20 23:11:48 -05004309 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04004310 break;
4311 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
James Smart76a95d72010-11-20 23:11:48 -05004312 att_type = LPFC_ATT_LINK_UP;
James Smartda0436e2009-05-22 14:51:39 -04004313 break;
4314 default:
4315 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4316 "0399 Invalid link attention type: x%x\n",
4317 bf_get(lpfc_acqe_link_status, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05004318 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04004319 break;
4320 }
4321 return att_type;
4322}
4323
4324/**
James Smart8b68cd52012-09-29 11:32:37 -04004325 * lpfc_sli_port_speed_get - Get sli3 link speed code to link speed
4326 * @phba: pointer to lpfc hba data structure.
4327 *
4328 * This routine is to get an SLI3 FC port's link speed in Mbps.
4329 *
4330 * Return: link speed in terms of Mbps.
4331 **/
4332uint32_t
4333lpfc_sli_port_speed_get(struct lpfc_hba *phba)
4334{
4335 uint32_t link_speed;
4336
4337 if (!lpfc_is_link_up(phba))
4338 return 0;
4339
James Smarta085e872015-12-16 18:12:02 -05004340 if (phba->sli_rev <= LPFC_SLI_REV3) {
4341 switch (phba->fc_linkspeed) {
4342 case LPFC_LINK_SPEED_1GHZ:
4343 link_speed = 1000;
4344 break;
4345 case LPFC_LINK_SPEED_2GHZ:
4346 link_speed = 2000;
4347 break;
4348 case LPFC_LINK_SPEED_4GHZ:
4349 link_speed = 4000;
4350 break;
4351 case LPFC_LINK_SPEED_8GHZ:
4352 link_speed = 8000;
4353 break;
4354 case LPFC_LINK_SPEED_10GHZ:
4355 link_speed = 10000;
4356 break;
4357 case LPFC_LINK_SPEED_16GHZ:
4358 link_speed = 16000;
4359 break;
4360 default:
4361 link_speed = 0;
4362 }
4363 } else {
4364 if (phba->sli4_hba.link_state.logical_speed)
4365 link_speed =
4366 phba->sli4_hba.link_state.logical_speed;
4367 else
4368 link_speed = phba->sli4_hba.link_state.speed;
James Smart8b68cd52012-09-29 11:32:37 -04004369 }
4370 return link_speed;
4371}
4372
4373/**
4374 * lpfc_sli4_port_speed_parse - Parse async evt link speed code to link speed
4375 * @phba: pointer to lpfc hba data structure.
4376 * @evt_code: asynchronous event code.
4377 * @speed_code: asynchronous event link speed code.
4378 *
4379 * This routine is to parse the giving SLI4 async event link speed code into
4380 * value of Mbps for the link speed.
4381 *
4382 * Return: link speed in terms of Mbps.
4383 **/
4384static uint32_t
4385lpfc_sli4_port_speed_parse(struct lpfc_hba *phba, uint32_t evt_code,
4386 uint8_t speed_code)
4387{
4388 uint32_t port_speed;
4389
4390 switch (evt_code) {
4391 case LPFC_TRAILER_CODE_LINK:
4392 switch (speed_code) {
James Smart26d830e2015-04-07 15:07:17 -04004393 case LPFC_ASYNC_LINK_SPEED_ZERO:
James Smart8b68cd52012-09-29 11:32:37 -04004394 port_speed = 0;
4395 break;
James Smart26d830e2015-04-07 15:07:17 -04004396 case LPFC_ASYNC_LINK_SPEED_10MBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004397 port_speed = 10;
4398 break;
James Smart26d830e2015-04-07 15:07:17 -04004399 case LPFC_ASYNC_LINK_SPEED_100MBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004400 port_speed = 100;
4401 break;
James Smart26d830e2015-04-07 15:07:17 -04004402 case LPFC_ASYNC_LINK_SPEED_1GBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004403 port_speed = 1000;
4404 break;
James Smart26d830e2015-04-07 15:07:17 -04004405 case LPFC_ASYNC_LINK_SPEED_10GBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004406 port_speed = 10000;
4407 break;
James Smart26d830e2015-04-07 15:07:17 -04004408 case LPFC_ASYNC_LINK_SPEED_20GBPS:
4409 port_speed = 20000;
4410 break;
4411 case LPFC_ASYNC_LINK_SPEED_25GBPS:
4412 port_speed = 25000;
4413 break;
4414 case LPFC_ASYNC_LINK_SPEED_40GBPS:
4415 port_speed = 40000;
4416 break;
James Smart8b68cd52012-09-29 11:32:37 -04004417 default:
4418 port_speed = 0;
4419 }
4420 break;
4421 case LPFC_TRAILER_CODE_FC:
4422 switch (speed_code) {
James Smart26d830e2015-04-07 15:07:17 -04004423 case LPFC_FC_LA_SPEED_UNKNOWN:
James Smart8b68cd52012-09-29 11:32:37 -04004424 port_speed = 0;
4425 break;
James Smart26d830e2015-04-07 15:07:17 -04004426 case LPFC_FC_LA_SPEED_1G:
James Smart8b68cd52012-09-29 11:32:37 -04004427 port_speed = 1000;
4428 break;
James Smart26d830e2015-04-07 15:07:17 -04004429 case LPFC_FC_LA_SPEED_2G:
James Smart8b68cd52012-09-29 11:32:37 -04004430 port_speed = 2000;
4431 break;
James Smart26d830e2015-04-07 15:07:17 -04004432 case LPFC_FC_LA_SPEED_4G:
James Smart8b68cd52012-09-29 11:32:37 -04004433 port_speed = 4000;
4434 break;
James Smart26d830e2015-04-07 15:07:17 -04004435 case LPFC_FC_LA_SPEED_8G:
James Smart8b68cd52012-09-29 11:32:37 -04004436 port_speed = 8000;
4437 break;
James Smart26d830e2015-04-07 15:07:17 -04004438 case LPFC_FC_LA_SPEED_10G:
James Smart8b68cd52012-09-29 11:32:37 -04004439 port_speed = 10000;
4440 break;
James Smart26d830e2015-04-07 15:07:17 -04004441 case LPFC_FC_LA_SPEED_16G:
James Smart8b68cd52012-09-29 11:32:37 -04004442 port_speed = 16000;
4443 break;
James Smartd38dd522015-08-31 16:48:17 -04004444 case LPFC_FC_LA_SPEED_32G:
4445 port_speed = 32000;
4446 break;
James Smart8b68cd52012-09-29 11:32:37 -04004447 default:
4448 port_speed = 0;
4449 }
4450 break;
4451 default:
4452 port_speed = 0;
4453 }
4454 return port_speed;
4455}
4456
4457/**
James Smart70f3c072010-12-15 17:57:33 -05004458 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
James Smartda0436e2009-05-22 14:51:39 -04004459 * @phba: pointer to lpfc hba data structure.
4460 * @acqe_link: pointer to the async link completion queue entry.
4461 *
James Smart70f3c072010-12-15 17:57:33 -05004462 * This routine is to handle the SLI4 asynchronous FCoE link event.
James Smartda0436e2009-05-22 14:51:39 -04004463 **/
4464static void
4465lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
4466 struct lpfc_acqe_link *acqe_link)
4467{
4468 struct lpfc_dmabuf *mp;
4469 LPFC_MBOXQ_t *pmb;
4470 MAILBOX_t *mb;
James Smart76a95d72010-11-20 23:11:48 -05004471 struct lpfc_mbx_read_top *la;
James Smartda0436e2009-05-22 14:51:39 -04004472 uint8_t att_type;
James Smart76a95d72010-11-20 23:11:48 -05004473 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004474
4475 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05004476 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
James Smartda0436e2009-05-22 14:51:39 -04004477 return;
James Smart32b97932009-07-19 10:01:21 -04004478 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004479 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4480 if (!pmb) {
4481 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4482 "0395 The mboxq allocation failed\n");
4483 return;
4484 }
4485 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4486 if (!mp) {
4487 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4488 "0396 The lpfc_dmabuf allocation failed\n");
4489 goto out_free_pmb;
4490 }
4491 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
4492 if (!mp->virt) {
4493 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4494 "0397 The mbuf allocation failed\n");
4495 goto out_free_dmabuf;
4496 }
4497
4498 /* Cleanup any outstanding ELS commands */
4499 lpfc_els_flush_all_cmd(phba);
4500
4501 /* Block ELS IOCBs until we have done process link event */
James Smart895427b2017-02-12 13:52:30 -08004502 phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
James Smartda0436e2009-05-22 14:51:39 -04004503
4504 /* Update link event statistics */
4505 phba->sli.slistat.link_event++;
4506
James Smart76a95d72010-11-20 23:11:48 -05004507 /* Create lpfc_handle_latt mailbox command from link ACQE */
4508 lpfc_read_topology(phba, pmb, mp);
4509 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smartda0436e2009-05-22 14:51:39 -04004510 pmb->vport = phba->pport;
4511
James Smartda0436e2009-05-22 14:51:39 -04004512 /* Keep the link status for extra SLI4 state machine reference */
4513 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04004514 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_LINK,
4515 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smartda0436e2009-05-22 14:51:39 -04004516 phba->sli4_hba.link_state.duplex =
4517 bf_get(lpfc_acqe_link_duplex, acqe_link);
4518 phba->sli4_hba.link_state.status =
4519 bf_get(lpfc_acqe_link_status, acqe_link);
James Smart70f3c072010-12-15 17:57:33 -05004520 phba->sli4_hba.link_state.type =
4521 bf_get(lpfc_acqe_link_type, acqe_link);
4522 phba->sli4_hba.link_state.number =
4523 bf_get(lpfc_acqe_link_number, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04004524 phba->sli4_hba.link_state.fault =
4525 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05004526 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04004527 bf_get(lpfc_acqe_logical_link_speed, acqe_link) * 10;
4528
James Smart70f3c072010-12-15 17:57:33 -05004529 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartc31098c2011-04-16 11:03:33 -04004530 "2900 Async FC/FCoE Link event - Speed:%dGBit "
4531 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
4532 "Logical speed:%dMbps Fault:%d\n",
James Smart70f3c072010-12-15 17:57:33 -05004533 phba->sli4_hba.link_state.speed,
4534 phba->sli4_hba.link_state.topology,
4535 phba->sli4_hba.link_state.status,
4536 phba->sli4_hba.link_state.type,
4537 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04004538 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05004539 phba->sli4_hba.link_state.fault);
James Smart76a95d72010-11-20 23:11:48 -05004540 /*
4541 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
4542 * topology info. Note: Optional for non FC-AL ports.
4543 */
4544 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
4545 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4546 if (rc == MBX_NOT_FINISHED)
4547 goto out_free_dmabuf;
4548 return;
4549 }
4550 /*
4551 * For FCoE Mode: fill in all the topology information we need and call
4552 * the READ_TOPOLOGY completion routine to continue without actually
4553 * sending the READ_TOPOLOGY mailbox command to the port.
4554 */
4555 /* Parse and translate status field */
4556 mb = &pmb->u.mb;
4557 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
4558
4559 /* Parse and translate link attention fields */
4560 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
4561 la->eventTag = acqe_link->event_tag;
4562 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
4563 bf_set(lpfc_mbx_read_top_link_spd, la,
James Smarta085e872015-12-16 18:12:02 -05004564 (bf_get(lpfc_acqe_link_speed, acqe_link)));
James Smart76a95d72010-11-20 23:11:48 -05004565
4566 /* Fake the the following irrelvant fields */
4567 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
4568 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
4569 bf_set(lpfc_mbx_read_top_il, la, 0);
4570 bf_set(lpfc_mbx_read_top_pb, la, 0);
4571 bf_set(lpfc_mbx_read_top_fa, la, 0);
4572 bf_set(lpfc_mbx_read_top_mm, la, 0);
James Smartda0436e2009-05-22 14:51:39 -04004573
4574 /* Invoke the lpfc_handle_latt mailbox command callback function */
James Smart76a95d72010-11-20 23:11:48 -05004575 lpfc_mbx_cmpl_read_topology(phba, pmb);
James Smartda0436e2009-05-22 14:51:39 -04004576
4577 return;
4578
4579out_free_dmabuf:
4580 kfree(mp);
4581out_free_pmb:
4582 mempool_free(pmb, phba->mbox_mem_pool);
4583}
4584
4585/**
James Smart70f3c072010-12-15 17:57:33 -05004586 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
4587 * @phba: pointer to lpfc hba data structure.
4588 * @acqe_fc: pointer to the async fc completion queue entry.
4589 *
4590 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
4591 * that the event was received and then issue a read_topology mailbox command so
4592 * that the rest of the driver will treat it the same as SLI3.
4593 **/
4594static void
4595lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
4596{
4597 struct lpfc_dmabuf *mp;
4598 LPFC_MBOXQ_t *pmb;
James Smart7bdedb32016-07-06 12:36:00 -07004599 MAILBOX_t *mb;
4600 struct lpfc_mbx_read_top *la;
James Smart70f3c072010-12-15 17:57:33 -05004601 int rc;
4602
4603 if (bf_get(lpfc_trailer_type, acqe_fc) !=
4604 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
4605 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4606 "2895 Non FC link Event detected.(%d)\n",
4607 bf_get(lpfc_trailer_type, acqe_fc));
4608 return;
4609 }
4610 /* Keep the link status for extra SLI4 state machine reference */
4611 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04004612 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
4613 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
James Smart70f3c072010-12-15 17:57:33 -05004614 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
4615 phba->sli4_hba.link_state.topology =
4616 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
4617 phba->sli4_hba.link_state.status =
4618 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
4619 phba->sli4_hba.link_state.type =
4620 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
4621 phba->sli4_hba.link_state.number =
4622 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
4623 phba->sli4_hba.link_state.fault =
4624 bf_get(lpfc_acqe_link_fault, acqe_fc);
4625 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04004626 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
James Smart70f3c072010-12-15 17:57:33 -05004627 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4628 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
4629 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
4630 "%dMbps Fault:%d\n",
4631 phba->sli4_hba.link_state.speed,
4632 phba->sli4_hba.link_state.topology,
4633 phba->sli4_hba.link_state.status,
4634 phba->sli4_hba.link_state.type,
4635 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04004636 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05004637 phba->sli4_hba.link_state.fault);
4638 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4639 if (!pmb) {
4640 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4641 "2897 The mboxq allocation failed\n");
4642 return;
4643 }
4644 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4645 if (!mp) {
4646 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4647 "2898 The lpfc_dmabuf allocation failed\n");
4648 goto out_free_pmb;
4649 }
4650 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
4651 if (!mp->virt) {
4652 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4653 "2899 The mbuf allocation failed\n");
4654 goto out_free_dmabuf;
4655 }
4656
4657 /* Cleanup any outstanding ELS commands */
4658 lpfc_els_flush_all_cmd(phba);
4659
4660 /* Block ELS IOCBs until we have done process link event */
James Smart895427b2017-02-12 13:52:30 -08004661 phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
James Smart70f3c072010-12-15 17:57:33 -05004662
4663 /* Update link event statistics */
4664 phba->sli.slistat.link_event++;
4665
4666 /* Create lpfc_handle_latt mailbox command from link ACQE */
4667 lpfc_read_topology(phba, pmb, mp);
4668 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
4669 pmb->vport = phba->pport;
4670
James Smart7bdedb32016-07-06 12:36:00 -07004671 if (phba->sli4_hba.link_state.status != LPFC_FC_LA_TYPE_LINK_UP) {
James Smartae9e28f2017-05-15 15:20:51 -07004672 phba->link_flag &= ~(LS_MDS_LINK_DOWN | LS_MDS_LOOPBACK);
4673
4674 switch (phba->sli4_hba.link_state.status) {
4675 case LPFC_FC_LA_TYPE_MDS_LINK_DOWN:
4676 phba->link_flag |= LS_MDS_LINK_DOWN;
4677 break;
4678 case LPFC_FC_LA_TYPE_MDS_LOOPBACK:
4679 phba->link_flag |= LS_MDS_LOOPBACK;
4680 break;
4681 default:
4682 break;
4683 }
4684
James Smart7bdedb32016-07-06 12:36:00 -07004685 /* Parse and translate status field */
4686 mb = &pmb->u.mb;
4687 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba,
4688 (void *)acqe_fc);
4689
4690 /* Parse and translate link attention fields */
4691 la = (struct lpfc_mbx_read_top *)&pmb->u.mb.un.varReadTop;
4692 la->eventTag = acqe_fc->event_tag;
James Smart7bdedb32016-07-06 12:36:00 -07004693
James Smartaeb3c812017-04-21 16:05:02 -07004694 if (phba->sli4_hba.link_state.status ==
4695 LPFC_FC_LA_TYPE_UNEXP_WWPN) {
4696 bf_set(lpfc_mbx_read_top_att_type, la,
4697 LPFC_FC_LA_TYPE_UNEXP_WWPN);
4698 } else {
4699 bf_set(lpfc_mbx_read_top_att_type, la,
4700 LPFC_FC_LA_TYPE_LINK_DOWN);
4701 }
James Smart7bdedb32016-07-06 12:36:00 -07004702 /* Invoke the mailbox command callback function */
4703 lpfc_mbx_cmpl_read_topology(phba, pmb);
4704
4705 return;
4706 }
4707
James Smart70f3c072010-12-15 17:57:33 -05004708 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4709 if (rc == MBX_NOT_FINISHED)
4710 goto out_free_dmabuf;
4711 return;
4712
4713out_free_dmabuf:
4714 kfree(mp);
4715out_free_pmb:
4716 mempool_free(pmb, phba->mbox_mem_pool);
4717}
4718
4719/**
4720 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
4721 * @phba: pointer to lpfc hba data structure.
4722 * @acqe_fc: pointer to the async SLI completion queue entry.
4723 *
4724 * This routine is to handle the SLI4 asynchronous SLI events.
4725 **/
4726static void
4727lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
4728{
James Smart4b8bae02012-06-12 13:55:07 -04004729 char port_name;
James Smart8c1312e2012-10-31 14:45:09 -04004730 char message[128];
James Smart4b8bae02012-06-12 13:55:07 -04004731 uint8_t status;
James Smart946727d2015-04-07 15:07:09 -04004732 uint8_t evt_type;
James Smart448193b2015-12-16 18:12:05 -05004733 uint8_t operational = 0;
James Smart946727d2015-04-07 15:07:09 -04004734 struct temp_event temp_event_data;
James Smart4b8bae02012-06-12 13:55:07 -04004735 struct lpfc_acqe_misconfigured_event *misconfigured;
James Smart946727d2015-04-07 15:07:09 -04004736 struct Scsi_Host *shost;
James Smart4b8bae02012-06-12 13:55:07 -04004737
James Smart946727d2015-04-07 15:07:09 -04004738 evt_type = bf_get(lpfc_trailer_type, acqe_sli);
4739
James Smart448193b2015-12-16 18:12:05 -05004740 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4741 "2901 Async SLI event - Event Data1:x%08x Event Data2:"
4742 "x%08x SLI Event Type:%d\n",
4743 acqe_sli->event_data1, acqe_sli->event_data2,
4744 evt_type);
James Smart4b8bae02012-06-12 13:55:07 -04004745
4746 port_name = phba->Port[0];
4747 if (port_name == 0x00)
4748 port_name = '?'; /* get port name is empty */
4749
James Smart946727d2015-04-07 15:07:09 -04004750 switch (evt_type) {
4751 case LPFC_SLI_EVENT_TYPE_OVER_TEMP:
4752 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
4753 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
4754 temp_event_data.data = (uint32_t)acqe_sli->event_data1;
4755
4756 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4757 "3190 Over Temperature:%d Celsius- Port Name %c\n",
4758 acqe_sli->event_data1, port_name);
4759
James Smart310429e2016-07-06 12:35:54 -07004760 phba->sfp_warning |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
James Smart946727d2015-04-07 15:07:09 -04004761 shost = lpfc_shost_from_vport(phba->pport);
4762 fc_host_post_vendor_event(shost, fc_get_event_number(),
4763 sizeof(temp_event_data),
4764 (char *)&temp_event_data,
4765 SCSI_NL_VID_TYPE_PCI
4766 | PCI_VENDOR_ID_EMULEX);
4767 break;
4768 case LPFC_SLI_EVENT_TYPE_NORM_TEMP:
4769 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
4770 temp_event_data.event_code = LPFC_NORMAL_TEMP;
4771 temp_event_data.data = (uint32_t)acqe_sli->event_data1;
4772
4773 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4774 "3191 Normal Temperature:%d Celsius - Port Name %c\n",
4775 acqe_sli->event_data1, port_name);
4776
4777 shost = lpfc_shost_from_vport(phba->pport);
4778 fc_host_post_vendor_event(shost, fc_get_event_number(),
4779 sizeof(temp_event_data),
4780 (char *)&temp_event_data,
4781 SCSI_NL_VID_TYPE_PCI
4782 | PCI_VENDOR_ID_EMULEX);
4783 break;
4784 case LPFC_SLI_EVENT_TYPE_MISCONFIGURED:
4785 misconfigured = (struct lpfc_acqe_misconfigured_event *)
James Smart4b8bae02012-06-12 13:55:07 -04004786 &acqe_sli->event_data1;
4787
James Smart946727d2015-04-07 15:07:09 -04004788 /* fetch the status for this port */
4789 switch (phba->sli4_hba.lnk_info.lnk_no) {
4790 case LPFC_LINK_NUMBER_0:
James Smart448193b2015-12-16 18:12:05 -05004791 status = bf_get(lpfc_sli_misconfigured_port0_state,
4792 &misconfigured->theEvent);
4793 operational = bf_get(lpfc_sli_misconfigured_port0_op,
James Smart4b8bae02012-06-12 13:55:07 -04004794 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04004795 break;
4796 case LPFC_LINK_NUMBER_1:
James Smart448193b2015-12-16 18:12:05 -05004797 status = bf_get(lpfc_sli_misconfigured_port1_state,
4798 &misconfigured->theEvent);
4799 operational = bf_get(lpfc_sli_misconfigured_port1_op,
James Smart4b8bae02012-06-12 13:55:07 -04004800 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04004801 break;
4802 case LPFC_LINK_NUMBER_2:
James Smart448193b2015-12-16 18:12:05 -05004803 status = bf_get(lpfc_sli_misconfigured_port2_state,
4804 &misconfigured->theEvent);
4805 operational = bf_get(lpfc_sli_misconfigured_port2_op,
James Smart4b8bae02012-06-12 13:55:07 -04004806 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04004807 break;
4808 case LPFC_LINK_NUMBER_3:
James Smart448193b2015-12-16 18:12:05 -05004809 status = bf_get(lpfc_sli_misconfigured_port3_state,
4810 &misconfigured->theEvent);
4811 operational = bf_get(lpfc_sli_misconfigured_port3_op,
James Smart4b8bae02012-06-12 13:55:07 -04004812 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04004813 break;
4814 default:
James Smart448193b2015-12-16 18:12:05 -05004815 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4816 "3296 "
4817 "LPFC_SLI_EVENT_TYPE_MISCONFIGURED "
4818 "event: Invalid link %d",
4819 phba->sli4_hba.lnk_info.lnk_no);
4820 return;
James Smart946727d2015-04-07 15:07:09 -04004821 }
James Smart4b8bae02012-06-12 13:55:07 -04004822
James Smart448193b2015-12-16 18:12:05 -05004823 /* Skip if optic state unchanged */
4824 if (phba->sli4_hba.lnk_info.optic_state == status)
4825 return;
4826
James Smart946727d2015-04-07 15:07:09 -04004827 switch (status) {
4828 case LPFC_SLI_EVENT_STATUS_VALID:
James Smart448193b2015-12-16 18:12:05 -05004829 sprintf(message, "Physical Link is functional");
4830 break;
James Smart946727d2015-04-07 15:07:09 -04004831 case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
4832 sprintf(message, "Optics faulted/incorrectly "
4833 "installed/not installed - Reseat optics, "
4834 "if issue not resolved, replace.");
4835 break;
4836 case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
4837 sprintf(message,
4838 "Optics of two types installed - Remove one "
4839 "optic or install matching pair of optics.");
4840 break;
4841 case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
4842 sprintf(message, "Incompatible optics - Replace with "
James Smart292098b2012-09-29 11:31:41 -04004843 "compatible optics for card to function.");
James Smart946727d2015-04-07 15:07:09 -04004844 break;
James Smart448193b2015-12-16 18:12:05 -05004845 case LPFC_SLI_EVENT_STATUS_UNQUALIFIED:
4846 sprintf(message, "Unqualified optics - Replace with "
4847 "Avago optics for Warranty and Technical "
4848 "Support - Link is%s operational",
James Smart2ea259e2017-02-12 13:52:27 -08004849 (operational) ? " not" : "");
James Smart448193b2015-12-16 18:12:05 -05004850 break;
4851 case LPFC_SLI_EVENT_STATUS_UNCERTIFIED:
4852 sprintf(message, "Uncertified optics - Replace with "
4853 "Avago-certified optics to enable link "
4854 "operation - Link is%s operational",
James Smart2ea259e2017-02-12 13:52:27 -08004855 (operational) ? " not" : "");
James Smart448193b2015-12-16 18:12:05 -05004856 break;
James Smart946727d2015-04-07 15:07:09 -04004857 default:
4858 /* firmware is reporting a status we don't know about */
4859 sprintf(message, "Unknown event status x%02x", status);
4860 break;
4861 }
James Smart448193b2015-12-16 18:12:05 -05004862 phba->sli4_hba.lnk_info.optic_state = status;
James Smart946727d2015-04-07 15:07:09 -04004863 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart448193b2015-12-16 18:12:05 -05004864 "3176 Port Name %c %s\n", port_name, message);
James Smart946727d2015-04-07 15:07:09 -04004865 break;
4866 case LPFC_SLI_EVENT_TYPE_REMOTE_DPORT:
4867 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4868 "3192 Remote DPort Test Initiated - "
4869 "Event Data1:x%08x Event Data2: x%08x\n",
4870 acqe_sli->event_data1, acqe_sli->event_data2);
James Smart4b8bae02012-06-12 13:55:07 -04004871 break;
4872 default:
James Smart946727d2015-04-07 15:07:09 -04004873 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4874 "3193 Async SLI event - Event Data1:x%08x Event Data2:"
4875 "x%08x SLI Event Type:%d\n",
4876 acqe_sli->event_data1, acqe_sli->event_data2,
4877 evt_type);
James Smart4b8bae02012-06-12 13:55:07 -04004878 break;
4879 }
James Smart70f3c072010-12-15 17:57:33 -05004880}
4881
4882/**
James Smartfc2b9892010-02-26 14:15:29 -05004883 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
4884 * @vport: pointer to vport data structure.
4885 *
4886 * This routine is to perform Clear Virtual Link (CVL) on a vport in
4887 * response to a CVL event.
4888 *
4889 * Return the pointer to the ndlp with the vport if successful, otherwise
4890 * return NULL.
4891 **/
4892static struct lpfc_nodelist *
4893lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
4894{
4895 struct lpfc_nodelist *ndlp;
4896 struct Scsi_Host *shost;
4897 struct lpfc_hba *phba;
4898
4899 if (!vport)
4900 return NULL;
James Smartfc2b9892010-02-26 14:15:29 -05004901 phba = vport->phba;
4902 if (!phba)
4903 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04004904 ndlp = lpfc_findnode_did(vport, Fabric_DID);
4905 if (!ndlp) {
4906 /* Cannot find existing Fabric ndlp, so allocate a new one */
James Smart9d3d3402017-04-21 16:05:00 -07004907 ndlp = lpfc_nlp_init(vport, Fabric_DID);
James Smart78730cf2010-04-06 15:06:30 -04004908 if (!ndlp)
4909 return 0;
James Smart78730cf2010-04-06 15:06:30 -04004910 /* Set the node type */
4911 ndlp->nlp_type |= NLP_FABRIC;
4912 /* Put ndlp onto node list */
4913 lpfc_enqueue_node(vport, ndlp);
4914 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
4915 /* re-setup ndlp without removing from node list */
4916 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
4917 if (!ndlp)
4918 return 0;
4919 }
James Smart63e801c2010-11-20 23:14:19 -05004920 if ((phba->pport->port_state < LPFC_FLOGI) &&
4921 (phba->pport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05004922 return NULL;
4923 /* If virtual link is not yet instantiated ignore CVL */
James Smart63e801c2010-11-20 23:14:19 -05004924 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
4925 && (vport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05004926 return NULL;
4927 shost = lpfc_shost_from_vport(vport);
4928 if (!shost)
4929 return NULL;
4930 lpfc_linkdown_port(vport);
4931 lpfc_cleanup_pending_mbox(vport);
4932 spin_lock_irq(shost->host_lock);
4933 vport->fc_flag |= FC_VPORT_CVL_RCVD;
4934 spin_unlock_irq(shost->host_lock);
4935
4936 return ndlp;
4937}
4938
4939/**
4940 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
4941 * @vport: pointer to lpfc hba data structure.
4942 *
4943 * This routine is to perform Clear Virtual Link (CVL) on all vports in
4944 * response to a FCF dead event.
4945 **/
4946static void
4947lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
4948{
4949 struct lpfc_vport **vports;
4950 int i;
4951
4952 vports = lpfc_create_vport_work_array(phba);
4953 if (vports)
4954 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
4955 lpfc_sli4_perform_vport_cvl(vports[i]);
4956 lpfc_destroy_vport_work_array(phba, vports);
4957}
4958
4959/**
James Smart76a95d72010-11-20 23:11:48 -05004960 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
James Smartda0436e2009-05-22 14:51:39 -04004961 * @phba: pointer to lpfc hba data structure.
4962 * @acqe_link: pointer to the async fcoe completion queue entry.
4963 *
4964 * This routine is to handle the SLI4 asynchronous fcoe event.
4965 **/
4966static void
James Smart76a95d72010-11-20 23:11:48 -05004967lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
James Smart70f3c072010-12-15 17:57:33 -05004968 struct lpfc_acqe_fip *acqe_fip)
James Smartda0436e2009-05-22 14:51:39 -04004969{
James Smart70f3c072010-12-15 17:57:33 -05004970 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004971 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04004972 struct lpfc_vport *vport;
4973 struct lpfc_nodelist *ndlp;
4974 struct Scsi_Host *shost;
James Smart695a8142010-01-26 23:08:03 -05004975 int active_vlink_present;
4976 struct lpfc_vport **vports;
4977 int i;
James Smartda0436e2009-05-22 14:51:39 -04004978
James Smart70f3c072010-12-15 17:57:33 -05004979 phba->fc_eventTag = acqe_fip->event_tag;
4980 phba->fcoe_eventtag = acqe_fip->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004981 switch (event_type) {
James Smart70f3c072010-12-15 17:57:33 -05004982 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
4983 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
4984 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
James Smart999d8132010-03-15 11:24:56 -04004985 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4986 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004987 "2546 New FCF event, evt_tag:x%x, "
4988 "index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004989 acqe_fip->event_tag,
4990 acqe_fip->index);
James Smart999d8132010-03-15 11:24:56 -04004991 else
4992 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
4993 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004994 "2788 FCF param modified event, "
4995 "evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004996 acqe_fip->event_tag,
4997 acqe_fip->index);
James Smart38b92ef2010-08-04 16:11:39 -04004998 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004999 /*
5000 * During period of FCF discovery, read the FCF
5001 * table record indexed by the event to update
James Smarta93ff372010-10-22 11:06:08 -04005002 * FCF roundrobin failover eligible FCF bmask.
James Smart0c9ab6f2010-02-26 14:15:57 -05005003 */
5004 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
5005 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005006 "2779 Read FCF (x%x) for updating "
5007 "roundrobin FCF failover bmask\n",
James Smart70f3c072010-12-15 17:57:33 -05005008 acqe_fip->index);
5009 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05005010 }
James Smart38b92ef2010-08-04 16:11:39 -04005011
5012 /* If the FCF discovery is in progress, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04005013 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04005014 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart38b92ef2010-08-04 16:11:39 -04005015 spin_unlock_irq(&phba->hbalock);
5016 break;
5017 }
5018 /* If fast FCF failover rescan event is pending, do nothing */
5019 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
5020 spin_unlock_irq(&phba->hbalock);
5021 break;
5022 }
5023
James Smartc2b97122013-05-31 17:05:36 -04005024 /* If the FCF has been in discovered state, do nothing. */
5025 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
James Smart3804dc82010-07-14 15:31:37 -04005026 spin_unlock_irq(&phba->hbalock);
5027 break;
5028 }
5029 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04005030
James Smart0c9ab6f2010-02-26 14:15:57 -05005031 /* Otherwise, scan the entire FCF table and re-discover SAN */
5032 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005033 "2770 Start FCF table scan per async FCF "
5034 "event, evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05005035 acqe_fip->event_tag, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05005036 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
5037 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04005038 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05005039 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
5040 "2547 Issue FCF scan read FCF mailbox "
James Smarta93ff372010-10-22 11:06:08 -04005041 "command failed (x%x)\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04005042 break;
5043
James Smart70f3c072010-12-15 17:57:33 -05005044 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
James Smartda0436e2009-05-22 14:51:39 -04005045 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04005046 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05005047 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
5048 acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04005049 break;
5050
James Smart70f3c072010-12-15 17:57:33 -05005051 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
James Smart80c17842012-03-01 22:35:45 -05005052 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05005053 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005054 "2549 FCF (x%x) disconnected from network, "
James Smart70f3c072010-12-15 17:57:33 -05005055 "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart38b92ef2010-08-04 16:11:39 -04005056 /*
5057 * If we are in the middle of FCF failover process, clear
5058 * the corresponding FCF bit in the roundrobin bitmap.
James Smartda0436e2009-05-22 14:51:39 -04005059 */
James Smartfc2b9892010-02-26 14:15:29 -05005060 spin_lock_irq(&phba->hbalock);
James Smarta1cadfe2016-07-06 12:36:02 -07005061 if ((phba->fcf.fcf_flag & FCF_DISCOVERY) &&
5062 (phba->fcf.current_rec.fcf_indx != acqe_fip->index)) {
James Smartfc2b9892010-02-26 14:15:29 -05005063 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005064 /* Update FLOGI FCF failover eligible FCF bmask */
James Smart70f3c072010-12-15 17:57:33 -05005065 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05005066 break;
5067 }
James Smart38b92ef2010-08-04 16:11:39 -04005068 spin_unlock_irq(&phba->hbalock);
5069
5070 /* If the event is not for currently used fcf do nothing */
James Smart70f3c072010-12-15 17:57:33 -05005071 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
James Smart38b92ef2010-08-04 16:11:39 -04005072 break;
5073
5074 /*
5075 * Otherwise, request the port to rediscover the entire FCF
5076 * table for a fast recovery from case that the current FCF
5077 * is no longer valid as we are not in the middle of FCF
5078 * failover process already.
5079 */
James Smartc2b97122013-05-31 17:05:36 -04005080 spin_lock_irq(&phba->hbalock);
5081 /* Mark the fast failover process in progress */
5082 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
5083 spin_unlock_irq(&phba->hbalock);
5084
5085 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
5086 "2771 Start FCF fast failover process due to "
5087 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
5088 "\n", acqe_fip->event_tag, acqe_fip->index);
5089 rc = lpfc_sli4_redisc_fcf_table(phba);
5090 if (rc) {
5091 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
5092 LOG_DISCOVERY,
5093 "2772 Issue FCF rediscover mabilbox "
5094 "command failed, fail through to FCF "
5095 "dead event\n");
5096 spin_lock_irq(&phba->hbalock);
5097 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
5098 spin_unlock_irq(&phba->hbalock);
5099 /*
5100 * Last resort will fail over by treating this
5101 * as a link down to FCF registration.
5102 */
5103 lpfc_sli4_fcf_dead_failthrough(phba);
5104 } else {
5105 /* Reset FCF roundrobin bmask for new discovery */
5106 lpfc_sli4_clear_fcf_rr_bmask(phba);
5107 /*
5108 * Handling fast FCF failover to a DEAD FCF event is
5109 * considered equalivant to receiving CVL to all vports.
5110 */
5111 lpfc_sli4_perform_all_vport_cvl(phba);
5112 }
James Smartda0436e2009-05-22 14:51:39 -04005113 break;
James Smart70f3c072010-12-15 17:57:33 -05005114 case LPFC_FIP_EVENT_TYPE_CVL:
James Smart80c17842012-03-01 22:35:45 -05005115 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05005116 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6669f9b2009-10-02 15:16:45 -04005117 "2718 Clear Virtual Link Received for VPI 0x%x"
James Smart70f3c072010-12-15 17:57:33 -05005118 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart6d368e52011-05-24 11:44:12 -04005119
James Smart6669f9b2009-10-02 15:16:45 -04005120 vport = lpfc_find_vport_by_vpid(phba,
James Smart5248a742011-07-22 18:37:06 -04005121 acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05005122 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04005123 if (!ndlp)
5124 break;
James Smart695a8142010-01-26 23:08:03 -05005125 active_vlink_present = 0;
5126
5127 vports = lpfc_create_vport_work_array(phba);
5128 if (vports) {
5129 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
5130 i++) {
5131 if ((!(vports[i]->fc_flag &
5132 FC_VPORT_CVL_RCVD)) &&
5133 (vports[i]->port_state > LPFC_FDISC)) {
5134 active_vlink_present = 1;
5135 break;
5136 }
5137 }
5138 lpfc_destroy_vport_work_array(phba, vports);
5139 }
5140
James Smartcc823552015-05-21 13:55:26 -04005141 /*
5142 * Don't re-instantiate if vport is marked for deletion.
5143 * If we are here first then vport_delete is going to wait
5144 * for discovery to complete.
5145 */
5146 if (!(vport->load_flag & FC_UNLOADING) &&
5147 active_vlink_present) {
James Smart695a8142010-01-26 23:08:03 -05005148 /*
5149 * If there are other active VLinks present,
5150 * re-instantiate the Vlink using FDISC.
5151 */
James Smart256ec0d2013-04-17 20:14:58 -04005152 mod_timer(&ndlp->nlp_delayfunc,
5153 jiffies + msecs_to_jiffies(1000));
James Smartfc2b9892010-02-26 14:15:29 -05005154 shost = lpfc_shost_from_vport(vport);
James Smart6669f9b2009-10-02 15:16:45 -04005155 spin_lock_irq(shost->host_lock);
5156 ndlp->nlp_flag |= NLP_DELAY_TMO;
5157 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05005158 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
5159 vport->port_state = LPFC_FDISC;
5160 } else {
James Smartecfd03c2010-02-12 14:41:27 -05005161 /*
5162 * Otherwise, we request port to rediscover
5163 * the entire FCF table for a fast recovery
5164 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05005165 * is no longer valid if we are not already
5166 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05005167 */
James Smartfc2b9892010-02-26 14:15:29 -05005168 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005169 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05005170 spin_unlock_irq(&phba->hbalock);
5171 break;
5172 }
5173 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05005174 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05005175 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005176 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
5177 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005178 "2773 Start FCF failover per CVL, "
James Smart70f3c072010-12-15 17:57:33 -05005179 "evt_tag:x%x\n", acqe_fip->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05005180 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05005181 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05005182 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
5183 LOG_DISCOVERY,
5184 "2774 Issue FCF rediscover "
5185 "mabilbox command failed, "
5186 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05005187 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005188 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05005189 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05005190 /*
5191 * Last resort will be re-try on the
5192 * the current registered FCF entry.
5193 */
5194 lpfc_retry_pport_discovery(phba);
James Smart38b92ef2010-08-04 16:11:39 -04005195 } else
5196 /*
5197 * Reset FCF roundrobin bmask for new
5198 * discovery.
5199 */
James Smart7d791df2011-07-22 18:37:52 -04005200 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart6669f9b2009-10-02 15:16:45 -04005201 }
5202 break;
James Smartda0436e2009-05-22 14:51:39 -04005203 default:
5204 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5205 "0288 Unknown FCoE event type 0x%x event tag "
James Smart70f3c072010-12-15 17:57:33 -05005206 "0x%x\n", event_type, acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04005207 break;
5208 }
5209}
5210
5211/**
5212 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
5213 * @phba: pointer to lpfc hba data structure.
5214 * @acqe_link: pointer to the async dcbx completion queue entry.
5215 *
5216 * This routine is to handle the SLI4 asynchronous dcbx event.
5217 **/
5218static void
5219lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
5220 struct lpfc_acqe_dcbx *acqe_dcbx)
5221{
James Smart4d9ab992009-10-02 15:16:39 -04005222 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04005223 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5224 "0290 The SLI4 DCBX asynchronous event is not "
5225 "handled yet\n");
5226}
5227
5228/**
James Smartb19a0612010-04-06 14:48:51 -04005229 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
5230 * @phba: pointer to lpfc hba data structure.
5231 * @acqe_link: pointer to the async grp5 completion queue entry.
5232 *
5233 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
5234 * is an asynchronous notified of a logical link speed change. The Port
5235 * reports the logical link speed in units of 10Mbps.
5236 **/
5237static void
5238lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
5239 struct lpfc_acqe_grp5 *acqe_grp5)
5240{
5241 uint16_t prev_ll_spd;
5242
5243 phba->fc_eventTag = acqe_grp5->event_tag;
5244 phba->fcoe_eventtag = acqe_grp5->event_tag;
5245 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
5246 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04005247 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5)) * 10;
James Smartb19a0612010-04-06 14:48:51 -04005248 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5249 "2789 GRP5 Async Event: Updating logical link speed "
James Smart8b68cd52012-09-29 11:32:37 -04005250 "from %dMbps to %dMbps\n", prev_ll_spd,
5251 phba->sli4_hba.link_state.logical_speed);
James Smartb19a0612010-04-06 14:48:51 -04005252}
5253
5254/**
James Smartda0436e2009-05-22 14:51:39 -04005255 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
5256 * @phba: pointer to lpfc hba data structure.
5257 *
5258 * This routine is invoked by the worker thread to process all the pending
5259 * SLI4 asynchronous events.
5260 **/
5261void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
5262{
5263 struct lpfc_cq_event *cq_event;
5264
5265 /* First, declare the async event has been handled */
5266 spin_lock_irq(&phba->hbalock);
5267 phba->hba_flag &= ~ASYNC_EVENT;
5268 spin_unlock_irq(&phba->hbalock);
5269 /* Now, handle all the async events */
5270 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
5271 /* Get the first event from the head of the event queue */
5272 spin_lock_irq(&phba->hbalock);
5273 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
5274 cq_event, struct lpfc_cq_event, list);
5275 spin_unlock_irq(&phba->hbalock);
5276 /* Process the asynchronous event */
5277 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
5278 case LPFC_TRAILER_CODE_LINK:
5279 lpfc_sli4_async_link_evt(phba,
5280 &cq_event->cqe.acqe_link);
5281 break;
5282 case LPFC_TRAILER_CODE_FCOE:
James Smart70f3c072010-12-15 17:57:33 -05005283 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04005284 break;
5285 case LPFC_TRAILER_CODE_DCBX:
5286 lpfc_sli4_async_dcbx_evt(phba,
5287 &cq_event->cqe.acqe_dcbx);
5288 break;
James Smartb19a0612010-04-06 14:48:51 -04005289 case LPFC_TRAILER_CODE_GRP5:
5290 lpfc_sli4_async_grp5_evt(phba,
5291 &cq_event->cqe.acqe_grp5);
5292 break;
James Smart70f3c072010-12-15 17:57:33 -05005293 case LPFC_TRAILER_CODE_FC:
5294 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
5295 break;
5296 case LPFC_TRAILER_CODE_SLI:
5297 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
5298 break;
James Smartda0436e2009-05-22 14:51:39 -04005299 default:
5300 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5301 "1804 Invalid asynchrous event code: "
5302 "x%x\n", bf_get(lpfc_trailer_code,
5303 &cq_event->cqe.mcqe_cmpl));
5304 break;
5305 }
5306 /* Free the completion event processed to the free pool */
5307 lpfc_sli4_cq_event_release(phba, cq_event);
5308 }
5309}
5310
5311/**
James Smartecfd03c2010-02-12 14:41:27 -05005312 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
5313 * @phba: pointer to lpfc hba data structure.
5314 *
5315 * This routine is invoked by the worker thread to process FCF table
5316 * rediscovery pending completion event.
5317 **/
5318void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
5319{
5320 int rc;
5321
5322 spin_lock_irq(&phba->hbalock);
5323 /* Clear FCF rediscovery timeout event */
5324 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
5325 /* Clear driver fast failover FCF record flag */
5326 phba->fcf.failover_rec.flag = 0;
5327 /* Set state for FCF fast failover */
5328 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
5329 spin_unlock_irq(&phba->hbalock);
5330
5331 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05005332 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005333 "2777 Start post-quiescent FCF table scan\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05005334 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05005335 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05005336 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
5337 "2747 Issue FCF scan read FCF mailbox "
5338 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05005339}
5340
5341/**
James Smartda0436e2009-05-22 14:51:39 -04005342 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
5343 * @phba: pointer to lpfc hba data structure.
5344 * @dev_grp: The HBA PCI-Device group number.
5345 *
5346 * This routine is invoked to set up the per HBA PCI-Device group function
5347 * API jump table entries.
5348 *
5349 * Return: 0 if success, otherwise -ENODEV
5350 **/
5351int
5352lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05005353{
5354 int rc;
5355
James Smartda0436e2009-05-22 14:51:39 -04005356 /* Set up lpfc PCI-device group */
5357 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05005358
James Smartda0436e2009-05-22 14:51:39 -04005359 /* The LPFC_PCI_DEV_OC uses SLI4 */
5360 if (dev_grp == LPFC_PCI_DEV_OC)
5361 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05005362
James Smartda0436e2009-05-22 14:51:39 -04005363 /* Set up device INIT API function jump table */
5364 rc = lpfc_init_api_table_setup(phba, dev_grp);
5365 if (rc)
5366 return -ENODEV;
5367 /* Set up SCSI API function jump table */
5368 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
5369 if (rc)
5370 return -ENODEV;
5371 /* Set up SLI API function jump table */
5372 rc = lpfc_sli_api_table_setup(phba, dev_grp);
5373 if (rc)
5374 return -ENODEV;
5375 /* Set up MBOX API function jump table */
5376 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
5377 if (rc)
5378 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05005379
James Smartda0436e2009-05-22 14:51:39 -04005380 return 0;
James Smart5b75da22008-12-04 22:39:35 -05005381}
5382
5383/**
James Smart3621a712009-04-06 18:47:14 -04005384 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05005385 * @phba: pointer to lpfc hba data structure.
5386 * @intr_mode: active interrupt mode adopted.
5387 *
5388 * This routine it invoked to log the currently used active interrupt mode
5389 * to the device.
James Smart3772a992009-05-22 14:50:54 -04005390 **/
5391static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05005392{
5393 switch (intr_mode) {
5394 case 0:
5395 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5396 "0470 Enable INTx interrupt mode.\n");
5397 break;
5398 case 1:
5399 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5400 "0481 Enabled MSI interrupt mode.\n");
5401 break;
5402 case 2:
5403 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5404 "0480 Enabled MSI-X interrupt mode.\n");
5405 break;
5406 default:
5407 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5408 "0482 Illegal interrupt mode.\n");
5409 break;
5410 }
5411 return;
5412}
5413
James Smart5b75da22008-12-04 22:39:35 -05005414/**
James Smart3772a992009-05-22 14:50:54 -04005415 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05005416 * @phba: pointer to lpfc hba data structure.
5417 *
James Smart3772a992009-05-22 14:50:54 -04005418 * This routine is invoked to enable the PCI device that is common to all
5419 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05005420 *
5421 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005422 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005423 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05005424 **/
James Smart3772a992009-05-22 14:50:54 -04005425static int
5426lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05005427{
James Smart3772a992009-05-22 14:50:54 -04005428 struct pci_dev *pdev;
James Smart5b75da22008-12-04 22:39:35 -05005429
James Smart3772a992009-05-22 14:50:54 -04005430 /* Obtain PCI device reference */
5431 if (!phba->pcidev)
5432 goto out_error;
5433 else
5434 pdev = phba->pcidev;
James Smart3772a992009-05-22 14:50:54 -04005435 /* Enable PCI device */
5436 if (pci_enable_device_mem(pdev))
5437 goto out_error;
5438 /* Request PCI resource for the device */
Johannes Thumshirne0c04832016-06-07 09:44:03 +02005439 if (pci_request_mem_regions(pdev, LPFC_DRIVER_NAME))
James Smart3772a992009-05-22 14:50:54 -04005440 goto out_disable_device;
5441 /* Set up device as PCI master and save state for EEH */
5442 pci_set_master(pdev);
5443 pci_try_set_mwi(pdev);
5444 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05005445
James Smart05580562011-05-24 11:40:48 -04005446 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
Jon Mason453193e2013-09-11 15:38:13 -07005447 if (pci_is_pcie(pdev))
James Smart05580562011-05-24 11:40:48 -04005448 pdev->needs_freset = 1;
5449
James Smart3772a992009-05-22 14:50:54 -04005450 return 0;
James Smart5b75da22008-12-04 22:39:35 -05005451
James Smart3772a992009-05-22 14:50:54 -04005452out_disable_device:
5453 pci_disable_device(pdev);
5454out_error:
James Smart079b5c92011-08-21 21:48:49 -04005455 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
Johannes Thumshirne0c04832016-06-07 09:44:03 +02005456 "1401 Failed to enable pci device\n");
James Smart3772a992009-05-22 14:50:54 -04005457 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05005458}
5459
5460/**
James Smart3772a992009-05-22 14:50:54 -04005461 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05005462 * @phba: pointer to lpfc hba data structure.
5463 *
James Smart3772a992009-05-22 14:50:54 -04005464 * This routine is invoked to disable the PCI device that is common to all
5465 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05005466 **/
5467static void
James Smart3772a992009-05-22 14:50:54 -04005468lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05005469{
James Smart3772a992009-05-22 14:50:54 -04005470 struct pci_dev *pdev;
James Smart5b75da22008-12-04 22:39:35 -05005471
James Smart3772a992009-05-22 14:50:54 -04005472 /* Obtain PCI device reference */
5473 if (!phba->pcidev)
5474 return;
5475 else
5476 pdev = phba->pcidev;
James Smart3772a992009-05-22 14:50:54 -04005477 /* Release PCI resource and disable PCI device */
Johannes Thumshirne0c04832016-06-07 09:44:03 +02005478 pci_release_mem_regions(pdev);
James Smart3772a992009-05-22 14:50:54 -04005479 pci_disable_device(pdev);
James Smart5b75da22008-12-04 22:39:35 -05005480
5481 return;
5482}
5483
5484/**
James Smart3772a992009-05-22 14:50:54 -04005485 * lpfc_reset_hba - Reset a hba
5486 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04005487 *
James Smart3772a992009-05-22 14:50:54 -04005488 * This routine is invoked to reset a hba device. It brings the HBA
5489 * offline, performs a board restart, and then brings the board back
5490 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
5491 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04005492 **/
James Smart3772a992009-05-22 14:50:54 -04005493void
5494lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05005495{
James Smart3772a992009-05-22 14:50:54 -04005496 /* If resets are disabled then set error state and return. */
5497 if (!phba->cfg_enable_hba_reset) {
5498 phba->link_state = LPFC_HBA_ERROR;
5499 return;
5500 }
James Smartee620212014-04-04 13:51:53 -04005501 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
5502 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
5503 else
5504 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart3772a992009-05-22 14:50:54 -04005505 lpfc_offline(phba);
5506 lpfc_sli_brdrestart(phba);
5507 lpfc_online(phba);
5508 lpfc_unblock_mgmt_io(phba);
5509}
dea31012005-04-17 16:05:31 -05005510
James Smart3772a992009-05-22 14:50:54 -04005511/**
James Smart0a96e972011-07-22 18:37:28 -04005512 * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
5513 * @phba: pointer to lpfc hba data structure.
5514 *
5515 * This function enables the PCI SR-IOV virtual functions to a physical
5516 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
5517 * enable the number of virtual functions to the physical function. As
5518 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
5519 * API call does not considered as an error condition for most of the device.
5520 **/
5521uint16_t
5522lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
5523{
5524 struct pci_dev *pdev = phba->pcidev;
5525 uint16_t nr_virtfn;
5526 int pos;
5527
James Smart0a96e972011-07-22 18:37:28 -04005528 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
5529 if (pos == 0)
5530 return 0;
5531
5532 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
5533 return nr_virtfn;
5534}
5535
5536/**
James Smart912e3ac2011-05-24 11:42:11 -04005537 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
5538 * @phba: pointer to lpfc hba data structure.
5539 * @nr_vfn: number of virtual functions to be enabled.
5540 *
5541 * This function enables the PCI SR-IOV virtual functions to a physical
5542 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
5543 * enable the number of virtual functions to the physical function. As
5544 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
5545 * API call does not considered as an error condition for most of the device.
5546 **/
5547int
5548lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
5549{
5550 struct pci_dev *pdev = phba->pcidev;
James Smart0a96e972011-07-22 18:37:28 -04005551 uint16_t max_nr_vfn;
James Smart912e3ac2011-05-24 11:42:11 -04005552 int rc;
5553
James Smart0a96e972011-07-22 18:37:28 -04005554 max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
5555 if (nr_vfn > max_nr_vfn) {
5556 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5557 "3057 Requested vfs (%d) greater than "
5558 "supported vfs (%d)", nr_vfn, max_nr_vfn);
5559 return -EINVAL;
5560 }
5561
James Smart912e3ac2011-05-24 11:42:11 -04005562 rc = pci_enable_sriov(pdev, nr_vfn);
5563 if (rc) {
5564 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5565 "2806 Failed to enable sriov on this device "
5566 "with vfn number nr_vf:%d, rc:%d\n",
5567 nr_vfn, rc);
5568 } else
5569 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5570 "2807 Successful enable sriov on this device "
5571 "with vfn number nr_vf:%d\n", nr_vfn);
5572 return rc;
5573}
5574
5575/**
James Smart895427b2017-02-12 13:52:30 -08005576 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
James Smart3772a992009-05-22 14:50:54 -04005577 * @phba: pointer to lpfc hba data structure.
5578 *
James Smart895427b2017-02-12 13:52:30 -08005579 * This routine is invoked to set up the driver internal resources before the
5580 * device specific resource setup to support the HBA device it attached to.
James Smart3772a992009-05-22 14:50:54 -04005581 *
5582 * Return codes
James Smart895427b2017-02-12 13:52:30 -08005583 * 0 - successful
5584 * other values - error
James Smart3772a992009-05-22 14:50:54 -04005585 **/
5586static int
James Smart895427b2017-02-12 13:52:30 -08005587lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
James Smart3772a992009-05-22 14:50:54 -04005588{
James Smart895427b2017-02-12 13:52:30 -08005589 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05005590
James Smart3772a992009-05-22 14:50:54 -04005591 /*
James Smart895427b2017-02-12 13:52:30 -08005592 * Driver resources common to all SLI revisions
James Smart3772a992009-05-22 14:50:54 -04005593 */
James Smart895427b2017-02-12 13:52:30 -08005594 atomic_set(&phba->fast_event_count, 0);
5595 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005596
James Smart895427b2017-02-12 13:52:30 -08005597 /* Initialize ndlp management spinlock */
5598 spin_lock_init(&phba->ndlp_lock);
James Smart3772a992009-05-22 14:50:54 -04005599
James Smart895427b2017-02-12 13:52:30 -08005600 INIT_LIST_HEAD(&phba->port_list);
5601 INIT_LIST_HEAD(&phba->work_list);
5602 init_waitqueue_head(&phba->wait_4_mlo_m_q);
5603
5604 /* Initialize the wait queue head for the kernel thread */
5605 init_waitqueue_head(&phba->work_waitq);
5606
5607 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smartf358dd02017-02-12 13:52:34 -08005608 "1403 Protocols supported %s %s %s\n",
James Smart895427b2017-02-12 13:52:30 -08005609 ((phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) ?
5610 "SCSI" : " "),
5611 ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) ?
James Smartf358dd02017-02-12 13:52:34 -08005612 "NVME" : " "),
5613 (phba->nvmet_support ? "NVMET" : " "));
James Smart895427b2017-02-12 13:52:30 -08005614
5615 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
5616 /* Initialize the scsi buffer list used by driver for scsi IO */
5617 spin_lock_init(&phba->scsi_buf_list_get_lock);
5618 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_get);
5619 spin_lock_init(&phba->scsi_buf_list_put_lock);
5620 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
5621 }
5622
5623 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
5624 (phba->nvmet_support == 0)) {
5625 /* Initialize the NVME buffer list used by driver for NVME IO */
5626 spin_lock_init(&phba->nvme_buf_list_get_lock);
5627 INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_get);
James Smartcf1a1d32017-12-08 17:18:03 -08005628 phba->get_nvme_bufs = 0;
James Smart895427b2017-02-12 13:52:30 -08005629 spin_lock_init(&phba->nvme_buf_list_put_lock);
5630 INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_put);
James Smartcf1a1d32017-12-08 17:18:03 -08005631 phba->put_nvme_bufs = 0;
James Smart895427b2017-02-12 13:52:30 -08005632 }
5633
5634 /* Initialize the fabric iocb list */
5635 INIT_LIST_HEAD(&phba->fabric_iocb_list);
5636
5637 /* Initialize list to save ELS buffers */
5638 INIT_LIST_HEAD(&phba->elsbuf);
5639
5640 /* Initialize FCF connection rec list */
5641 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
5642
5643 /* Initialize OAS configuration list */
5644 spin_lock_init(&phba->devicelock);
5645 INIT_LIST_HEAD(&phba->luns);
5646
James Smart3772a992009-05-22 14:50:54 -04005647 /* MBOX heartbeat timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07005648 timer_setup(&psli->mbox_tmo, lpfc_mbox_timeout, 0);
James Smart3772a992009-05-22 14:50:54 -04005649 /* Fabric block timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07005650 timer_setup(&phba->fabric_block_timer, lpfc_fabric_block_timeout, 0);
James Smart3772a992009-05-22 14:50:54 -04005651 /* EA polling mode timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07005652 timer_setup(&phba->eratt_poll, lpfc_poll_eratt, 0);
James Smart895427b2017-02-12 13:52:30 -08005653 /* Heartbeat timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07005654 timer_setup(&phba->hb_tmofunc, lpfc_hb_timeout, 0);
James Smart895427b2017-02-12 13:52:30 -08005655
5656 return 0;
5657}
5658
5659/**
5660 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev
5661 * @phba: pointer to lpfc hba data structure.
5662 *
5663 * This routine is invoked to set up the driver internal resources specific to
5664 * support the SLI-3 HBA device it attached to.
5665 *
5666 * Return codes
5667 * 0 - successful
5668 * other values - error
5669 **/
5670static int
5671lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
5672{
5673 int rc;
5674
5675 /*
5676 * Initialize timers used by driver
5677 */
5678
5679 /* FCP polling mode timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07005680 timer_setup(&phba->fcp_poll_timer, lpfc_poll_timeout, 0);
James Smart3772a992009-05-22 14:50:54 -04005681
5682 /* Host attention work mask setup */
5683 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
5684 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
5685
5686 /* Get all the module params for configuring this host */
5687 lpfc_get_cfgparam(phba);
James Smart895427b2017-02-12 13:52:30 -08005688 /* Set up phase-1 common device driver resources */
5689
5690 rc = lpfc_setup_driver_resource_phase1(phba);
5691 if (rc)
5692 return -ENODEV;
5693
James Smart49198b32010-04-06 15:04:33 -04005694 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
5695 phba->menlo_flag |= HBA_MENLO_SUPPORT;
5696 /* check for menlo minimum sg count */
5697 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
5698 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
5699 }
5700
James Smart895427b2017-02-12 13:52:30 -08005701 if (!phba->sli.sli3_ring)
5702 phba->sli.sli3_ring = kzalloc(LPFC_SLI3_MAX_RING *
James Smart2a76a282012-08-03 12:35:54 -04005703 sizeof(struct lpfc_sli_ring), GFP_KERNEL);
James Smart895427b2017-02-12 13:52:30 -08005704 if (!phba->sli.sli3_ring)
James Smart2a76a282012-08-03 12:35:54 -04005705 return -ENOMEM;
5706
James Smart3772a992009-05-22 14:50:54 -04005707 /*
James Smart96f70772013-04-17 20:16:15 -04005708 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smart3772a992009-05-22 14:50:54 -04005709 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smart3772a992009-05-22 14:50:54 -04005710 */
James Smart3772a992009-05-22 14:50:54 -04005711
James Smart96f70772013-04-17 20:16:15 -04005712 /* Initialize the host templates the configured values. */
James Smart3772a992009-05-22 14:50:54 -04005713 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
James Smart96418b52017-03-04 09:30:31 -08005714 lpfc_template_no_hr.sg_tablesize = phba->cfg_sg_seg_cnt;
5715 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
James Smart3772a992009-05-22 14:50:54 -04005716
James Smart96f70772013-04-17 20:16:15 -04005717 /* There are going to be 2 reserved BDEs: 1 FCP cmnd + 1 FCP rsp */
5718 if (phba->cfg_enable_bg) {
5719 /*
5720 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
5721 * the FCP rsp, and a BDE for each. Sice we have no control
5722 * over how many protection data segments the SCSI Layer
5723 * will hand us (ie: there could be one for every block
5724 * in the IO), we just allocate enough BDEs to accomidate
5725 * our max amount and we need to limit lpfc_sg_seg_cnt to
5726 * minimize the risk of running out.
5727 */
5728 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5729 sizeof(struct fcp_rsp) +
5730 (LPFC_MAX_SG_SEG_CNT * sizeof(struct ulp_bde64));
5731
5732 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SEG_CNT_DIF)
5733 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT_DIF;
5734
5735 /* Total BDEs in BPL for scsi_sg_list and scsi_sg_prot_list */
5736 phba->cfg_total_seg_cnt = LPFC_MAX_SG_SEG_CNT;
5737 } else {
5738 /*
5739 * The scsi_buf for a regular I/O will hold the FCP cmnd,
5740 * the FCP rsp, a BDE for each, and a BDE for up to
5741 * cfg_sg_seg_cnt data segments.
5742 */
5743 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5744 sizeof(struct fcp_rsp) +
5745 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
5746
5747 /* Total BDEs in BPL for scsi_sg_list */
5748 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
5749 }
5750
5751 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
5752 "9088 sg_tablesize:%d dmabuf_size:%d total_bde:%d\n",
5753 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
5754 phba->cfg_total_seg_cnt);
5755
James Smart3772a992009-05-22 14:50:54 -04005756 phba->max_vpi = LPFC_MAX_VPI;
5757 /* This will be set to correct value after config_port mbox */
5758 phba->max_vports = 0;
5759
5760 /*
5761 * Initialize the SLI Layer to run with lpfc HBAs.
5762 */
5763 lpfc_sli_setup(phba);
James Smart895427b2017-02-12 13:52:30 -08005764 lpfc_sli_queue_init(phba);
James Smart3772a992009-05-22 14:50:54 -04005765
5766 /* Allocate device driver memory */
5767 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
5768 return -ENOMEM;
5769
James Smart912e3ac2011-05-24 11:42:11 -04005770 /*
5771 * Enable sr-iov virtual functions if supported and configured
5772 * through the module parameter.
5773 */
5774 if (phba->cfg_sriov_nr_virtfn > 0) {
5775 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
5776 phba->cfg_sriov_nr_virtfn);
5777 if (rc) {
5778 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5779 "2808 Requested number of SR-IOV "
5780 "virtual functions (%d) is not "
5781 "supported\n",
5782 phba->cfg_sriov_nr_virtfn);
5783 phba->cfg_sriov_nr_virtfn = 0;
5784 }
5785 }
5786
James Smart3772a992009-05-22 14:50:54 -04005787 return 0;
5788}
5789
5790/**
5791 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
5792 * @phba: pointer to lpfc hba data structure.
5793 *
5794 * This routine is invoked to unset the driver internal resources set up
5795 * specific for supporting the SLI-3 HBA device it attached to.
5796 **/
5797static void
5798lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
5799{
5800 /* Free device driver memory allocated */
5801 lpfc_mem_free_all(phba);
5802
5803 return;
5804}
5805
5806/**
James Smartda0436e2009-05-22 14:51:39 -04005807 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
5808 * @phba: pointer to lpfc hba data structure.
5809 *
5810 * This routine is invoked to set up the driver internal resources specific to
5811 * support the SLI-4 HBA device it attached to.
5812 *
5813 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005814 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005815 * other values - error
5816 **/
5817static int
5818lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
5819{
James Smart28baac72010-02-12 14:42:03 -05005820 LPFC_MBOXQ_t *mboxq;
James Smartf358dd02017-02-12 13:52:34 -08005821 MAILBOX_t *mb;
James Smart895427b2017-02-12 13:52:30 -08005822 int rc, i, max_buf_size;
James Smart28baac72010-02-12 14:42:03 -05005823 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
5824 struct lpfc_mqe *mqe;
James Smart09294d42013-04-17 20:16:05 -04005825 int longs;
James Smart1ba981f2014-02-20 09:56:45 -05005826 int fof_vectors = 0;
James Smart81e6a632017-11-20 16:00:43 -08005827 int extra;
James Smartf358dd02017-02-12 13:52:34 -08005828 uint64_t wwn;
James Smartda0436e2009-05-22 14:51:39 -04005829
James Smart895427b2017-02-12 13:52:30 -08005830 phba->sli4_hba.num_online_cpu = num_online_cpus();
5831 phba->sli4_hba.num_present_cpu = lpfc_present_cpu;
5832 phba->sli4_hba.curr_disp_cpu = 0;
5833
James Smart716d3bc2013-09-06 12:18:28 -04005834 /* Get all the module params for configuring this host */
5835 lpfc_get_cfgparam(phba);
5836
James Smart895427b2017-02-12 13:52:30 -08005837 /* Set up phase-1 common device driver resources */
5838 rc = lpfc_setup_driver_resource_phase1(phba);
5839 if (rc)
5840 return -ENODEV;
5841
James Smartda0436e2009-05-22 14:51:39 -04005842 /* Before proceed, wait for POST done and device ready */
5843 rc = lpfc_sli4_post_status_check(phba);
5844 if (rc)
5845 return -ENODEV;
5846
5847 /*
5848 * Initialize timers used by driver
5849 */
5850
Kees Cookf22eb4d2017-09-06 20:24:26 -07005851 timer_setup(&phba->rrq_tmr, lpfc_rrq_timeout, 0);
James Smartda0436e2009-05-22 14:51:39 -04005852
James Smartecfd03c2010-02-12 14:41:27 -05005853 /* FCF rediscover timer */
Kees Cookf22eb4d2017-09-06 20:24:26 -07005854 timer_setup(&phba->fcf.redisc_wait, lpfc_sli4_fcf_redisc_wait_tmo, 0);
James Smartecfd03c2010-02-12 14:41:27 -05005855
James Smartda0436e2009-05-22 14:51:39 -04005856 /*
James Smart7ad20aa2011-05-24 11:44:28 -04005857 * Control structure for handling external multi-buffer mailbox
5858 * command pass-through.
5859 */
5860 memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
5861 sizeof(struct lpfc_mbox_ext_buf_ctx));
5862 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
5863
James Smartda0436e2009-05-22 14:51:39 -04005864 phba->max_vpi = LPFC_MAX_VPI;
James Smart67d12732012-08-03 12:36:13 -04005865
James Smartda0436e2009-05-22 14:51:39 -04005866 /* This will be set to correct value after the read_config mbox */
5867 phba->max_vports = 0;
5868
5869 /* Program the default value of vlan_id and fc_map */
5870 phba->valid_vlan = 0;
5871 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5872 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5873 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5874
5875 /*
James Smart2a76a282012-08-03 12:35:54 -04005876 * For SLI4, instead of using ring 0 (LPFC_FCP_RING) for FCP commands
James Smart895427b2017-02-12 13:52:30 -08005877 * we will associate a new ring, for each EQ/CQ/WQ tuple.
5878 * The WQ create will allocate the ring.
James Smart2a76a282012-08-03 12:35:54 -04005879 */
James Smart085c6472010-11-20 23:11:37 -05005880
James Smart09294d42013-04-17 20:16:05 -04005881 /*
James Smart81e6a632017-11-20 16:00:43 -08005882 * 1 for cmd, 1 for rsp, NVME adds an extra one
5883 * for boundary conditions in its max_sgl_segment template.
5884 */
5885 extra = 2;
5886 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
5887 extra++;
5888
5889 /*
James Smart09294d42013-04-17 20:16:05 -04005890 * It doesn't matter what family our adapter is in, we are
5891 * limited to 2 Pages, 512 SGEs, for our SGL.
5892 * There are going to be 2 reserved SGEs: 1 FCP cmnd + 1 FCP rsp
5893 */
5894 max_buf_size = (2 * SLI4_PAGE_SIZE);
James Smart81e6a632017-11-20 16:00:43 -08005895 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SGL_SEG_CNT - extra)
5896 phba->cfg_sg_seg_cnt = LPFC_MAX_SGL_SEG_CNT - extra;
James Smart09294d42013-04-17 20:16:05 -04005897
James Smartda0436e2009-05-22 14:51:39 -04005898 /*
James Smart895427b2017-02-12 13:52:30 -08005899 * Since lpfc_sg_seg_cnt is module param, the sg_dma_buf_size
5900 * used to create the sg_dma_buf_pool must be calculated.
James Smart28baac72010-02-12 14:42:03 -05005901 */
James Smart96f70772013-04-17 20:16:15 -04005902 if (phba->cfg_enable_bg) {
5903 /*
James Smart895427b2017-02-12 13:52:30 -08005904 * The scsi_buf for a T10-DIF I/O holds the FCP cmnd,
5905 * the FCP rsp, and a SGE. Sice we have no control
5906 * over how many protection segments the SCSI Layer
James Smart96f70772013-04-17 20:16:15 -04005907 * will hand us (ie: there could be one for every block
James Smart895427b2017-02-12 13:52:30 -08005908 * in the IO), just allocate enough SGEs to accomidate
5909 * our max amount and we need to limit lpfc_sg_seg_cnt
5910 * to minimize the risk of running out.
James Smart96f70772013-04-17 20:16:15 -04005911 */
5912 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
James Smart895427b2017-02-12 13:52:30 -08005913 sizeof(struct fcp_rsp) + max_buf_size;
James Smart96f70772013-04-17 20:16:15 -04005914
5915 /* Total SGEs for scsi_sg_list and scsi_sg_prot_list */
5916 phba->cfg_total_seg_cnt = LPFC_MAX_SGL_SEG_CNT;
5917
5918 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SLI4_SEG_CNT_DIF)
James Smart895427b2017-02-12 13:52:30 -08005919 phba->cfg_sg_seg_cnt =
5920 LPFC_MAX_SG_SLI4_SEG_CNT_DIF;
James Smart96f70772013-04-17 20:16:15 -04005921 } else {
5922 /*
James Smart895427b2017-02-12 13:52:30 -08005923 * The scsi_buf for a regular I/O holds the FCP cmnd,
James Smart96f70772013-04-17 20:16:15 -04005924 * the FCP rsp, a SGE for each, and a SGE for up to
5925 * cfg_sg_seg_cnt data segments.
5926 */
5927 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
James Smart895427b2017-02-12 13:52:30 -08005928 sizeof(struct fcp_rsp) +
James Smart81e6a632017-11-20 16:00:43 -08005929 ((phba->cfg_sg_seg_cnt + extra) *
James Smart895427b2017-02-12 13:52:30 -08005930 sizeof(struct sli4_sge));
James Smart96f70772013-04-17 20:16:15 -04005931
5932 /* Total SGEs for scsi_sg_list */
James Smart81e6a632017-11-20 16:00:43 -08005933 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + extra;
James Smart895427b2017-02-12 13:52:30 -08005934
James Smart96f70772013-04-17 20:16:15 -04005935 /*
James Smart81e6a632017-11-20 16:00:43 -08005936 * NOTE: if (phba->cfg_sg_seg_cnt + extra) <= 256 we only
James Smart895427b2017-02-12 13:52:30 -08005937 * need to post 1 page for the SGL.
James Smart96f70772013-04-17 20:16:15 -04005938 */
James Smart085c6472010-11-20 23:11:37 -05005939 }
James Smartacd68592012-01-18 16:25:09 -05005940
James Smart96f70772013-04-17 20:16:15 -04005941 /* Initialize the host templates with the updated values. */
5942 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5943 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
James Smart96418b52017-03-04 09:30:31 -08005944 lpfc_template_no_hr.sg_tablesize = phba->cfg_sg_seg_cnt;
James Smart96f70772013-04-17 20:16:15 -04005945
5946 if (phba->cfg_sg_dma_buf_size <= LPFC_MIN_SG_SLI4_BUF_SZ)
5947 phba->cfg_sg_dma_buf_size = LPFC_MIN_SG_SLI4_BUF_SZ;
5948 else
5949 phba->cfg_sg_dma_buf_size =
5950 SLI4_PAGE_ALIGN(phba->cfg_sg_dma_buf_size);
5951
5952 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
5953 "9087 sg_tablesize:%d dmabuf_size:%d total_sge:%d\n",
5954 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
5955 phba->cfg_total_seg_cnt);
James Smartda0436e2009-05-22 14:51:39 -04005956
5957 /* Initialize buffer queue management fields */
James Smart895427b2017-02-12 13:52:30 -08005958 INIT_LIST_HEAD(&phba->hbqs[LPFC_ELS_HBQ].hbq_buffer_list);
James Smartda0436e2009-05-22 14:51:39 -04005959 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
5960 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
5961
5962 /*
5963 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
5964 */
James Smart895427b2017-02-12 13:52:30 -08005965 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
5966 /* Initialize the Abort scsi buffer list used by driver */
5967 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
5968 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
5969 }
5970
5971 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
5972 /* Initialize the Abort nvme buffer list used by driver */
5973 spin_lock_init(&phba->sli4_hba.abts_nvme_buf_list_lock);
5974 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvme_buf_list);
James Smart86c67372017-04-21 16:05:04 -07005975 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
James Smarta8cf5df2017-05-15 15:20:46 -07005976 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_io_wait_list);
James Smart895427b2017-02-12 13:52:30 -08005977 }
5978
James Smartda0436e2009-05-22 14:51:39 -04005979 /* This abort list used by worker thread */
James Smart895427b2017-02-12 13:52:30 -08005980 spin_lock_init(&phba->sli4_hba.sgl_list_lock);
James Smarta8cf5df2017-05-15 15:20:46 -07005981 spin_lock_init(&phba->sli4_hba.nvmet_io_wait_lock);
James Smartda0436e2009-05-22 14:51:39 -04005982
5983 /*
James Smart6d368e52011-05-24 11:44:12 -04005984 * Initialize driver internal slow-path work queues
James Smartda0436e2009-05-22 14:51:39 -04005985 */
5986
5987 /* Driver internel slow-path CQ Event pool */
5988 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
5989 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04005990 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04005991 /* Asynchronous event CQ Event work queue list */
5992 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
5993 /* Fast-path XRI aborted CQ Event work queue list */
5994 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
5995 /* Slow-path XRI aborted CQ Event work queue list */
5996 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
5997 /* Receive queue CQ Event work queue list */
5998 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
5999
James Smart6d368e52011-05-24 11:44:12 -04006000 /* Initialize extent block lists. */
6001 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
6002 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
6003 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
6004 INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
6005
James Smartd1f525a2017-04-21 16:04:55 -07006006 /* Initialize mboxq lists. If the early init routines fail
6007 * these lists need to be correctly initialized.
6008 */
6009 INIT_LIST_HEAD(&phba->sli.mboxq);
6010 INIT_LIST_HEAD(&phba->sli.mboxq_cmpl);
6011
James Smart448193b2015-12-16 18:12:05 -05006012 /* initialize optic_state to 0xFF */
6013 phba->sli4_hba.lnk_info.optic_state = 0xff;
6014
James Smartda0436e2009-05-22 14:51:39 -04006015 /* Allocate device driver memory */
6016 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
6017 if (rc)
6018 return -ENOMEM;
6019
James Smart2fcee4b2010-12-15 17:57:46 -05006020 /* IF Type 2 ports get initialized now. */
6021 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
6022 LPFC_SLI_INTF_IF_TYPE_2) {
6023 rc = lpfc_pci_function_reset(phba);
James Smart895427b2017-02-12 13:52:30 -08006024 if (unlikely(rc)) {
6025 rc = -ENODEV;
6026 goto out_free_mem;
6027 }
James Smart946727d2015-04-07 15:07:09 -04006028 phba->temp_sensor_support = 1;
James Smart2fcee4b2010-12-15 17:57:46 -05006029 }
6030
James Smartda0436e2009-05-22 14:51:39 -04006031 /* Create the bootstrap mailbox command */
6032 rc = lpfc_create_bootstrap_mbox(phba);
6033 if (unlikely(rc))
6034 goto out_free_mem;
6035
6036 /* Set up the host's endian order with the device. */
6037 rc = lpfc_setup_endian_order(phba);
6038 if (unlikely(rc))
6039 goto out_free_bsmbx;
6040
6041 /* Set up the hba's configuration parameters. */
6042 rc = lpfc_sli4_read_config(phba);
6043 if (unlikely(rc))
6044 goto out_free_bsmbx;
James Smartcff261f2013-12-17 20:29:47 -05006045 rc = lpfc_mem_alloc_active_rrq_pool_s4(phba);
6046 if (unlikely(rc))
6047 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04006048
James Smart2fcee4b2010-12-15 17:57:46 -05006049 /* IF Type 0 ports get initialized now. */
6050 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
6051 LPFC_SLI_INTF_IF_TYPE_0) {
6052 rc = lpfc_pci_function_reset(phba);
6053 if (unlikely(rc))
6054 goto out_free_bsmbx;
6055 }
James Smartda0436e2009-05-22 14:51:39 -04006056
James Smartcb5172e2010-03-15 11:25:07 -04006057 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6058 GFP_KERNEL);
6059 if (!mboxq) {
6060 rc = -ENOMEM;
6061 goto out_free_bsmbx;
6062 }
6063
James Smartf358dd02017-02-12 13:52:34 -08006064 /* Check for NVMET being configured */
James Smart895427b2017-02-12 13:52:30 -08006065 phba->nvmet_support = 0;
James Smartf358dd02017-02-12 13:52:34 -08006066 if (lpfc_enable_nvmet_cnt) {
6067
6068 /* First get WWN of HBA instance */
6069 lpfc_read_nv(phba, mboxq);
6070 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6071 if (rc != MBX_SUCCESS) {
6072 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6073 "6016 Mailbox failed , mbxCmd x%x "
6074 "READ_NV, mbxStatus x%x\n",
6075 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
6076 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smartd1f525a2017-04-21 16:04:55 -07006077 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartf358dd02017-02-12 13:52:34 -08006078 rc = -EIO;
6079 goto out_free_bsmbx;
6080 }
6081 mb = &mboxq->u.mb;
6082 memcpy(&wwn, (char *)mb->un.varRDnvp.nodename,
6083 sizeof(uint64_t));
6084 wwn = cpu_to_be64(wwn);
6085 phba->sli4_hba.wwnn.u.name = wwn;
6086 memcpy(&wwn, (char *)mb->un.varRDnvp.portname,
6087 sizeof(uint64_t));
6088 /* wwn is WWPN of HBA instance */
6089 wwn = cpu_to_be64(wwn);
6090 phba->sli4_hba.wwpn.u.name = wwn;
6091
6092 /* Check to see if it matches any module parameter */
6093 for (i = 0; i < lpfc_enable_nvmet_cnt; i++) {
6094 if (wwn == lpfc_enable_nvmet[i]) {
James Smart7d708032017-03-08 14:36:01 -08006095#if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
James Smart3c603be2017-05-15 15:20:44 -07006096 if (lpfc_nvmet_mem_alloc(phba))
6097 break;
6098
6099 phba->nvmet_support = 1; /* a match */
6100
James Smartf358dd02017-02-12 13:52:34 -08006101 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6102 "6017 NVME Target %016llx\n",
6103 wwn);
James Smart7d708032017-03-08 14:36:01 -08006104#else
6105 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6106 "6021 Can't enable NVME Target."
6107 " NVME_TARGET_FC infrastructure"
6108 " is not in kernel\n");
6109#endif
James Smart3c603be2017-05-15 15:20:44 -07006110 break;
James Smartf358dd02017-02-12 13:52:34 -08006111 }
6112 }
6113 }
James Smart895427b2017-02-12 13:52:30 -08006114
6115 lpfc_nvme_mod_param_dep(phba);
6116
James Smartfedd3b72011-02-16 12:39:24 -05006117 /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
James Smartcb5172e2010-03-15 11:25:07 -04006118 lpfc_supported_pages(mboxq);
6119 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smartfedd3b72011-02-16 12:39:24 -05006120 if (!rc) {
6121 mqe = &mboxq->u.mqe;
6122 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
6123 LPFC_MAX_SUPPORTED_PAGES);
6124 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
6125 switch (pn_page[i]) {
6126 case LPFC_SLI4_PARAMETERS:
6127 phba->sli4_hba.pc_sli4_params.supported = 1;
6128 break;
6129 default:
6130 break;
6131 }
6132 }
6133 /* Read the port's SLI4 Parameters capabilities if supported. */
6134 if (phba->sli4_hba.pc_sli4_params.supported)
6135 rc = lpfc_pc_sli4_params_get(phba, mboxq);
6136 if (rc) {
6137 mempool_free(mboxq, phba->mbox_mem_pool);
6138 rc = -EIO;
6139 goto out_free_bsmbx;
James Smartcb5172e2010-03-15 11:25:07 -04006140 }
6141 }
James Smart65791f12016-07-06 12:35:56 -07006142
James Smartfedd3b72011-02-16 12:39:24 -05006143 /*
6144 * Get sli4 parameters that override parameters from Port capabilities.
James Smart6d368e52011-05-24 11:44:12 -04006145 * If this call fails, it isn't critical unless the SLI4 parameters come
6146 * back in conflict.
James Smartfedd3b72011-02-16 12:39:24 -05006147 */
James Smart6d368e52011-05-24 11:44:12 -04006148 rc = lpfc_get_sli4_parameters(phba, mboxq);
6149 if (rc) {
6150 if (phba->sli4_hba.extents_in_use &&
6151 phba->sli4_hba.rpi_hdrs_in_use) {
6152 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6153 "2999 Unsupported SLI4 Parameters "
6154 "Extents and RPI headers enabled.\n");
James Smart6d368e52011-05-24 11:44:12 -04006155 }
James Smart895427b2017-02-12 13:52:30 -08006156 mempool_free(mboxq, phba->mbox_mem_pool);
Stefano Brivio5c756062017-09-06 11:02:56 +02006157 rc = -EIO;
James Smart895427b2017-02-12 13:52:30 -08006158 goto out_free_bsmbx;
James Smart6d368e52011-05-24 11:44:12 -04006159 }
James Smart895427b2017-02-12 13:52:30 -08006160
James Smartcb5172e2010-03-15 11:25:07 -04006161 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1ba981f2014-02-20 09:56:45 -05006162
6163 /* Verify OAS is supported */
6164 lpfc_sli4_oas_verify(phba);
6165 if (phba->cfg_fof)
6166 fof_vectors = 1;
6167
James Smart5350d872011-10-10 21:33:49 -04006168 /* Verify all the SLI4 queues */
6169 rc = lpfc_sli4_queue_verify(phba);
James Smartda0436e2009-05-22 14:51:39 -04006170 if (rc)
6171 goto out_free_bsmbx;
6172
6173 /* Create driver internal CQE event pool */
6174 rc = lpfc_sli4_cq_event_pool_create(phba);
6175 if (rc)
James Smart5350d872011-10-10 21:33:49 -04006176 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04006177
James Smart8a9d2e82012-05-09 21:16:12 -04006178 /* Initialize sgl lists per host */
6179 lpfc_init_sgl_list(phba);
6180
6181 /* Allocate and initialize active sgl array */
James Smartda0436e2009-05-22 14:51:39 -04006182 rc = lpfc_init_active_sgl_array(phba);
6183 if (rc) {
6184 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6185 "1430 Failed to initialize sgl list.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04006186 goto out_destroy_cq_event_pool;
James Smartda0436e2009-05-22 14:51:39 -04006187 }
James Smartda0436e2009-05-22 14:51:39 -04006188 rc = lpfc_sli4_init_rpi_hdrs(phba);
6189 if (rc) {
6190 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6191 "1432 Failed to initialize rpi headers.\n");
6192 goto out_free_active_sgl;
6193 }
6194
James Smarta93ff372010-10-22 11:06:08 -04006195 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
James Smart0c9ab6f2010-02-26 14:15:57 -05006196 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
6197 phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
6198 GFP_KERNEL);
6199 if (!phba->fcf.fcf_rr_bmask) {
6200 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6201 "2759 Failed allocate memory for FCF round "
6202 "robin failover bmask\n");
James Smart05580562011-05-24 11:40:48 -04006203 rc = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -05006204 goto out_remove_rpi_hdrs;
6205 }
6206
James Smart895427b2017-02-12 13:52:30 -08006207 phba->sli4_hba.hba_eq_hdl = kcalloc(fof_vectors + phba->io_channel_irqs,
6208 sizeof(struct lpfc_hba_eq_hdl),
6209 GFP_KERNEL);
6210 if (!phba->sli4_hba.hba_eq_hdl) {
James Smart67d12732012-08-03 12:36:13 -04006211 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6212 "2572 Failed allocate memory for "
6213 "fast-path per-EQ handle array\n");
6214 rc = -ENOMEM;
6215 goto out_free_fcf_rr_bmask;
James Smartda0436e2009-05-22 14:51:39 -04006216 }
6217
James Smart895427b2017-02-12 13:52:30 -08006218 phba->sli4_hba.cpu_map = kcalloc(phba->sli4_hba.num_present_cpu,
6219 sizeof(struct lpfc_vector_map_info),
6220 GFP_KERNEL);
James Smart7bb03bb2013-04-17 20:19:16 -04006221 if (!phba->sli4_hba.cpu_map) {
6222 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6223 "3327 Failed allocate memory for msi-x "
6224 "interrupt vector mapping\n");
6225 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08006226 goto out_free_hba_eq_hdl;
James Smart7bb03bb2013-04-17 20:19:16 -04006227 }
James Smartb246de12013-05-31 17:03:07 -04006228 if (lpfc_used_cpu == NULL) {
James Smart895427b2017-02-12 13:52:30 -08006229 lpfc_used_cpu = kcalloc(lpfc_present_cpu, sizeof(uint16_t),
6230 GFP_KERNEL);
James Smartb246de12013-05-31 17:03:07 -04006231 if (!lpfc_used_cpu) {
6232 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6233 "3335 Failed allocate memory for msi-x "
6234 "interrupt vector mapping\n");
6235 kfree(phba->sli4_hba.cpu_map);
6236 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08006237 goto out_free_hba_eq_hdl;
James Smartb246de12013-05-31 17:03:07 -04006238 }
6239 for (i = 0; i < lpfc_present_cpu; i++)
6240 lpfc_used_cpu[i] = LPFC_VECTOR_MAP_EMPTY;
6241 }
6242
James Smart912e3ac2011-05-24 11:42:11 -04006243 /*
6244 * Enable sr-iov virtual functions if supported and configured
6245 * through the module parameter.
6246 */
6247 if (phba->cfg_sriov_nr_virtfn > 0) {
6248 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
6249 phba->cfg_sriov_nr_virtfn);
6250 if (rc) {
6251 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6252 "3020 Requested number of SR-IOV "
6253 "virtual functions (%d) is not "
6254 "supported\n",
6255 phba->cfg_sriov_nr_virtfn);
6256 phba->cfg_sriov_nr_virtfn = 0;
6257 }
6258 }
6259
James Smart5248a742011-07-22 18:37:06 -04006260 return 0;
James Smartda0436e2009-05-22 14:51:39 -04006261
James Smart895427b2017-02-12 13:52:30 -08006262out_free_hba_eq_hdl:
6263 kfree(phba->sli4_hba.hba_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05006264out_free_fcf_rr_bmask:
6265 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04006266out_remove_rpi_hdrs:
6267 lpfc_sli4_remove_rpi_hdrs(phba);
6268out_free_active_sgl:
6269 lpfc_free_active_sgl(phba);
James Smartda0436e2009-05-22 14:51:39 -04006270out_destroy_cq_event_pool:
6271 lpfc_sli4_cq_event_pool_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04006272out_free_bsmbx:
6273 lpfc_destroy_bootstrap_mbox(phba);
6274out_free_mem:
6275 lpfc_mem_free(phba);
6276 return rc;
6277}
6278
6279/**
6280 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
6281 * @phba: pointer to lpfc hba data structure.
6282 *
6283 * This routine is invoked to unset the driver internal resources set up
6284 * specific for supporting the SLI-4 HBA device it attached to.
6285 **/
6286static void
6287lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
6288{
6289 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6290
James Smart7bb03bb2013-04-17 20:19:16 -04006291 /* Free memory allocated for msi-x interrupt vector to CPU mapping */
6292 kfree(phba->sli4_hba.cpu_map);
6293 phba->sli4_hba.num_present_cpu = 0;
6294 phba->sli4_hba.num_online_cpu = 0;
James Smart76fd07a2014-02-20 09:57:18 -05006295 phba->sli4_hba.curr_disp_cpu = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04006296
James Smartda0436e2009-05-22 14:51:39 -04006297 /* Free memory allocated for fast-path work queue handles */
James Smart895427b2017-02-12 13:52:30 -08006298 kfree(phba->sli4_hba.hba_eq_hdl);
James Smartda0436e2009-05-22 14:51:39 -04006299
6300 /* Free the allocated rpi headers. */
6301 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04006302 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04006303
James Smart0c9ab6f2010-02-26 14:15:57 -05006304 /* Free eligible FCF index bmask */
6305 kfree(phba->fcf.fcf_rr_bmask);
6306
James Smartda0436e2009-05-22 14:51:39 -04006307 /* Free the ELS sgl list */
6308 lpfc_free_active_sgl(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04006309 lpfc_free_els_sgl_list(phba);
James Smartf358dd02017-02-12 13:52:34 -08006310 lpfc_free_nvmet_sgl_list(phba);
James Smartda0436e2009-05-22 14:51:39 -04006311
James Smartda0436e2009-05-22 14:51:39 -04006312 /* Free the completion queue EQ event pool */
6313 lpfc_sli4_cq_event_release_all(phba);
6314 lpfc_sli4_cq_event_pool_destroy(phba);
6315
James Smart6d368e52011-05-24 11:44:12 -04006316 /* Release resource identifiers. */
6317 lpfc_sli4_dealloc_resource_identifiers(phba);
6318
James Smartda0436e2009-05-22 14:51:39 -04006319 /* Free the bsmbx region. */
6320 lpfc_destroy_bootstrap_mbox(phba);
6321
6322 /* Free the SLI Layer memory with SLI4 HBAs */
6323 lpfc_mem_free_all(phba);
6324
6325 /* Free the current connect table */
6326 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04006327 &phba->fcf_conn_rec_list, list) {
6328 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04006329 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04006330 }
James Smartda0436e2009-05-22 14:51:39 -04006331
6332 return;
6333}
6334
6335/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006336 * lpfc_init_api_table_setup - Set up init api function jump table
James Smart3772a992009-05-22 14:50:54 -04006337 * @phba: The hba struct for which this call is being executed.
6338 * @dev_grp: The HBA PCI-Device group number.
6339 *
6340 * This routine sets up the device INIT interface API function jump table
6341 * in @phba struct.
6342 *
6343 * Returns: 0 - success, -ENODEV - failure.
6344 **/
6345int
6346lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6347{
James Smart84d1b002010-02-12 14:42:33 -05006348 phba->lpfc_hba_init_link = lpfc_hba_init_link;
6349 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart7f860592011-03-11 16:05:52 -05006350 phba->lpfc_selective_reset = lpfc_selective_reset;
James Smart3772a992009-05-22 14:50:54 -04006351 switch (dev_grp) {
6352 case LPFC_PCI_DEV_LP:
6353 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
6354 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
6355 phba->lpfc_stop_port = lpfc_stop_port_s3;
6356 break;
James Smartda0436e2009-05-22 14:51:39 -04006357 case LPFC_PCI_DEV_OC:
6358 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
6359 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
6360 phba->lpfc_stop_port = lpfc_stop_port_s4;
6361 break;
James Smart3772a992009-05-22 14:50:54 -04006362 default:
6363 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6364 "1431 Invalid HBA PCI-device group: 0x%x\n",
6365 dev_grp);
6366 return -ENODEV;
6367 break;
6368 }
6369 return 0;
6370}
6371
6372/**
James Smart3772a992009-05-22 14:50:54 -04006373 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
6374 * @phba: pointer to lpfc hba data structure.
6375 *
6376 * This routine is invoked to set up the driver internal resources after the
6377 * device specific resource setup to support the HBA device it attached to.
6378 *
6379 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006380 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006381 * other values - error
6382 **/
6383static int
6384lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
6385{
6386 int error;
6387
6388 /* Startup the kernel thread for this host adapter. */
6389 phba->worker_thread = kthread_run(lpfc_do_work, phba,
6390 "lpfc_worker_%d", phba->brd_no);
6391 if (IS_ERR(phba->worker_thread)) {
6392 error = PTR_ERR(phba->worker_thread);
6393 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05006394 }
6395
Dick Kennedyf485c182017-09-29 17:34:34 -07006396 /* workqueue for deferred irq use */
6397 phba->wq = alloc_workqueue("lpfc_wq", WQ_MEM_RECLAIM, 0);
6398
James Smart3772a992009-05-22 14:50:54 -04006399 return 0;
6400}
6401
6402/**
6403 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
6404 * @phba: pointer to lpfc hba data structure.
6405 *
6406 * This routine is invoked to unset the driver internal resources set up after
6407 * the device specific resource setup for supporting the HBA device it
6408 * attached to.
6409 **/
6410static void
6411lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
6412{
Dick Kennedyf485c182017-09-29 17:34:34 -07006413 if (phba->wq) {
6414 flush_workqueue(phba->wq);
6415 destroy_workqueue(phba->wq);
6416 phba->wq = NULL;
6417 }
6418
James Smart3772a992009-05-22 14:50:54 -04006419 /* Stop kernel worker thread */
6420 kthread_stop(phba->worker_thread);
6421}
6422
6423/**
6424 * lpfc_free_iocb_list - Free iocb list.
6425 * @phba: pointer to lpfc hba data structure.
6426 *
6427 * This routine is invoked to free the driver's IOCB list and memory.
6428 **/
James Smart6c621a22017-05-15 15:20:45 -07006429void
James Smart3772a992009-05-22 14:50:54 -04006430lpfc_free_iocb_list(struct lpfc_hba *phba)
6431{
6432 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
6433
6434 spin_lock_irq(&phba->hbalock);
6435 list_for_each_entry_safe(iocbq_entry, iocbq_next,
6436 &phba->lpfc_iocb_list, list) {
6437 list_del(&iocbq_entry->list);
6438 kfree(iocbq_entry);
6439 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05006440 }
James Smart3772a992009-05-22 14:50:54 -04006441 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006442
James Smart3772a992009-05-22 14:50:54 -04006443 return;
6444}
dea31012005-04-17 16:05:31 -05006445
James Smart3772a992009-05-22 14:50:54 -04006446/**
6447 * lpfc_init_iocb_list - Allocate and initialize iocb list.
6448 * @phba: pointer to lpfc hba data structure.
6449 *
6450 * This routine is invoked to allocate and initizlize the driver's IOCB
6451 * list and set up the IOCB tag array accordingly.
6452 *
6453 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006454 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006455 * other values - error
6456 **/
James Smart6c621a22017-05-15 15:20:45 -07006457int
James Smart3772a992009-05-22 14:50:54 -04006458lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
6459{
6460 struct lpfc_iocbq *iocbq_entry = NULL;
6461 uint16_t iotag;
6462 int i;
dea31012005-04-17 16:05:31 -05006463
6464 /* Initialize and populate the iocb list per host. */
6465 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04006466 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07006467 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05006468 if (iocbq_entry == NULL) {
6469 printk(KERN_ERR "%s: only allocated %d iocbs of "
6470 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07006471 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05006472 goto out_free_iocbq;
6473 }
6474
James Bottomley604a3e32005-10-29 10:28:33 -05006475 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
6476 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04006477 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05006478 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04006479 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05006480 goto out_free_iocbq;
6481 }
James Smart6d368e52011-05-24 11:44:12 -04006482 iocbq_entry->sli4_lxritag = NO_XRI;
James Smart3772a992009-05-22 14:50:54 -04006483 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05006484
6485 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006486 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
6487 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05006488 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006489 }
6490
James Smart3772a992009-05-22 14:50:54 -04006491 return 0;
6492
6493out_free_iocbq:
6494 lpfc_free_iocb_list(phba);
6495
6496 return -ENOMEM;
6497}
6498
6499/**
James Smart8a9d2e82012-05-09 21:16:12 -04006500 * lpfc_free_sgl_list - Free a given sgl list.
James Smartda0436e2009-05-22 14:51:39 -04006501 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -04006502 * @sglq_list: pointer to the head of sgl list.
James Smartda0436e2009-05-22 14:51:39 -04006503 *
James Smart8a9d2e82012-05-09 21:16:12 -04006504 * This routine is invoked to free a give sgl list and memory.
James Smartda0436e2009-05-22 14:51:39 -04006505 **/
James Smart8a9d2e82012-05-09 21:16:12 -04006506void
6507lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
James Smartda0436e2009-05-22 14:51:39 -04006508{
6509 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart8a9d2e82012-05-09 21:16:12 -04006510
6511 list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
6512 list_del(&sglq_entry->list);
6513 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
6514 kfree(sglq_entry);
6515 }
6516}
6517
6518/**
6519 * lpfc_free_els_sgl_list - Free els sgl list.
6520 * @phba: pointer to lpfc hba data structure.
6521 *
6522 * This routine is invoked to free the driver's els sgl list and memory.
6523 **/
6524static void
6525lpfc_free_els_sgl_list(struct lpfc_hba *phba)
6526{
James Smartda0436e2009-05-22 14:51:39 -04006527 LIST_HEAD(sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04006528
James Smart8a9d2e82012-05-09 21:16:12 -04006529 /* Retrieve all els sgls from driver list */
James Smartda0436e2009-05-22 14:51:39 -04006530 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006531 spin_lock(&phba->sli4_hba.sgl_list_lock);
6532 list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list, &sglq_list);
6533 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04006534 spin_unlock_irq(&phba->hbalock);
6535
James Smart8a9d2e82012-05-09 21:16:12 -04006536 /* Now free the sgl list */
6537 lpfc_free_sgl_list(phba, &sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04006538}
6539
6540/**
James Smartf358dd02017-02-12 13:52:34 -08006541 * lpfc_free_nvmet_sgl_list - Free nvmet sgl list.
6542 * @phba: pointer to lpfc hba data structure.
6543 *
6544 * This routine is invoked to free the driver's nvmet sgl list and memory.
6545 **/
6546static void
6547lpfc_free_nvmet_sgl_list(struct lpfc_hba *phba)
6548{
6549 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
6550 LIST_HEAD(sglq_list);
6551
6552 /* Retrieve all nvmet sgls from driver list */
6553 spin_lock_irq(&phba->hbalock);
6554 spin_lock(&phba->sli4_hba.sgl_list_lock);
6555 list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list, &sglq_list);
6556 spin_unlock(&phba->sli4_hba.sgl_list_lock);
6557 spin_unlock_irq(&phba->hbalock);
6558
6559 /* Now free the sgl list */
6560 list_for_each_entry_safe(sglq_entry, sglq_next, &sglq_list, list) {
6561 list_del(&sglq_entry->list);
6562 lpfc_nvmet_buf_free(phba, sglq_entry->virt, sglq_entry->phys);
6563 kfree(sglq_entry);
6564 }
Dick Kennedy4b40d022017-08-23 16:55:38 -07006565
6566 /* Update the nvmet_xri_cnt to reflect no current sgls.
6567 * The next initialization cycle sets the count and allocates
6568 * the sgls over again.
6569 */
6570 phba->sli4_hba.nvmet_xri_cnt = 0;
James Smartf358dd02017-02-12 13:52:34 -08006571}
6572
6573/**
James Smartda0436e2009-05-22 14:51:39 -04006574 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
6575 * @phba: pointer to lpfc hba data structure.
6576 *
6577 * This routine is invoked to allocate the driver's active sgl memory.
6578 * This array will hold the sglq_entry's for active IOs.
6579 **/
6580static int
6581lpfc_init_active_sgl_array(struct lpfc_hba *phba)
6582{
6583 int size;
6584 size = sizeof(struct lpfc_sglq *);
6585 size *= phba->sli4_hba.max_cfg_param.max_xri;
6586
6587 phba->sli4_hba.lpfc_sglq_active_list =
6588 kzalloc(size, GFP_KERNEL);
6589 if (!phba->sli4_hba.lpfc_sglq_active_list)
6590 return -ENOMEM;
6591 return 0;
6592}
6593
6594/**
6595 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
6596 * @phba: pointer to lpfc hba data structure.
6597 *
6598 * This routine is invoked to walk through the array of active sglq entries
6599 * and free all of the resources.
6600 * This is just a place holder for now.
6601 **/
6602static void
6603lpfc_free_active_sgl(struct lpfc_hba *phba)
6604{
6605 kfree(phba->sli4_hba.lpfc_sglq_active_list);
6606}
6607
6608/**
6609 * lpfc_init_sgl_list - Allocate and initialize sgl list.
6610 * @phba: pointer to lpfc hba data structure.
6611 *
6612 * This routine is invoked to allocate and initizlize the driver's sgl
6613 * list and set up the sgl xritag tag array accordingly.
6614 *
James Smartda0436e2009-05-22 14:51:39 -04006615 **/
James Smart8a9d2e82012-05-09 21:16:12 -04006616static void
James Smartda0436e2009-05-22 14:51:39 -04006617lpfc_init_sgl_list(struct lpfc_hba *phba)
6618{
James Smartda0436e2009-05-22 14:51:39 -04006619 /* Initialize and populate the sglq list per host/VF. */
James Smart895427b2017-02-12 13:52:30 -08006620 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_els_sgl_list);
James Smartda0436e2009-05-22 14:51:39 -04006621 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smartf358dd02017-02-12 13:52:34 -08006622 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_sgl_list);
James Smart86c67372017-04-21 16:05:04 -07006623 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
James Smartda0436e2009-05-22 14:51:39 -04006624
James Smart8a9d2e82012-05-09 21:16:12 -04006625 /* els xri-sgl book keeping */
6626 phba->sli4_hba.els_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04006627
James Smart8a9d2e82012-05-09 21:16:12 -04006628 /* scsi xri-buffer book keeping */
James Smartda0436e2009-05-22 14:51:39 -04006629 phba->sli4_hba.scsi_xri_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -08006630
6631 /* nvme xri-buffer book keeping */
6632 phba->sli4_hba.nvme_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04006633}
6634
6635/**
6636 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
6637 * @phba: pointer to lpfc hba data structure.
6638 *
6639 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -04006640 * port for those SLI4 ports that do not support extents. This routine
James Smartda0436e2009-05-22 14:51:39 -04006641 * posts a PAGE_SIZE memory region to the port to hold up to
James Smart88a2cfb2011-07-22 18:36:33 -04006642 * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
6643 * and should be called only when interrupts are disabled.
James Smartda0436e2009-05-22 14:51:39 -04006644 *
6645 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006646 * 0 - successful
James Smart88a2cfb2011-07-22 18:36:33 -04006647 * -ERROR - otherwise.
James Smartda0436e2009-05-22 14:51:39 -04006648 **/
6649int
6650lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
6651{
6652 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04006653 struct lpfc_rpi_hdr *rpi_hdr;
6654
6655 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
James Smartff78d8f2011-12-13 13:21:35 -05006656 if (!phba->sli4_hba.rpi_hdrs_in_use)
James Smart6d368e52011-05-24 11:44:12 -04006657 return rc;
James Smart6d368e52011-05-24 11:44:12 -04006658 if (phba->sli4_hba.extents_in_use)
6659 return -EIO;
James Smartda0436e2009-05-22 14:51:39 -04006660
6661 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
6662 if (!rpi_hdr) {
6663 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6664 "0391 Error during rpi post operation\n");
6665 lpfc_sli4_remove_rpis(phba);
6666 rc = -ENODEV;
6667 }
6668
6669 return rc;
6670}
6671
6672/**
6673 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
6674 * @phba: pointer to lpfc hba data structure.
6675 *
6676 * This routine is invoked to allocate a single 4KB memory region to
6677 * support rpis and stores them in the phba. This single region
6678 * provides support for up to 64 rpis. The region is used globally
6679 * by the device.
6680 *
6681 * Returns:
6682 * A valid rpi hdr on success.
6683 * A NULL pointer on any failure.
6684 **/
6685struct lpfc_rpi_hdr *
6686lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
6687{
6688 uint16_t rpi_limit, curr_rpi_range;
6689 struct lpfc_dmabuf *dmabuf;
6690 struct lpfc_rpi_hdr *rpi_hdr;
6691
James Smart6d368e52011-05-24 11:44:12 -04006692 /*
6693 * If the SLI4 port supports extents, posting the rpi header isn't
6694 * required. Set the expected maximum count and let the actual value
6695 * get set when extents are fully allocated.
6696 */
6697 if (!phba->sli4_hba.rpi_hdrs_in_use)
6698 return NULL;
6699 if (phba->sli4_hba.extents_in_use)
6700 return NULL;
6701
6702 /* The limit on the logical index is just the max_rpi count. */
James Smart845d9e82017-05-15 15:20:38 -07006703 rpi_limit = phba->sli4_hba.max_cfg_param.max_rpi;
James Smartda0436e2009-05-22 14:51:39 -04006704
6705 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04006706 /*
6707 * Establish the starting RPI in this header block. The starting
6708 * rpi is normalized to a zero base because the physical rpi is
6709 * port based.
6710 */
James Smart97f2ecf2012-03-01 22:35:23 -05006711 curr_rpi_range = phba->sli4_hba.next_rpi;
James Smartda0436e2009-05-22 14:51:39 -04006712 spin_unlock_irq(&phba->hbalock);
6713
James Smart845d9e82017-05-15 15:20:38 -07006714 /* Reached full RPI range */
6715 if (curr_rpi_range == rpi_limit)
James Smart6d368e52011-05-24 11:44:12 -04006716 return NULL;
James Smart845d9e82017-05-15 15:20:38 -07006717
James Smartda0436e2009-05-22 14:51:39 -04006718 /*
6719 * First allocate the protocol header region for the port. The
6720 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
6721 */
6722 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
6723 if (!dmabuf)
6724 return NULL;
6725
Joe Perches1aee3832014-09-03 12:56:12 -04006726 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
6727 LPFC_HDR_TEMPLATE_SIZE,
6728 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04006729 if (!dmabuf->virt) {
6730 rpi_hdr = NULL;
6731 goto err_free_dmabuf;
6732 }
6733
James Smartda0436e2009-05-22 14:51:39 -04006734 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
6735 rpi_hdr = NULL;
6736 goto err_free_coherent;
6737 }
6738
6739 /* Save the rpi header data for cleanup later. */
6740 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
6741 if (!rpi_hdr)
6742 goto err_free_coherent;
6743
6744 rpi_hdr->dmabuf = dmabuf;
6745 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
6746 rpi_hdr->page_count = 1;
6747 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04006748
6749 /* The rpi_hdr stores the logical index only. */
6750 rpi_hdr->start_rpi = curr_rpi_range;
James Smart845d9e82017-05-15 15:20:38 -07006751 rpi_hdr->next_rpi = phba->sli4_hba.next_rpi + LPFC_RPI_HDR_COUNT;
James Smartda0436e2009-05-22 14:51:39 -04006752 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
6753
James Smartda0436e2009-05-22 14:51:39 -04006754 spin_unlock_irq(&phba->hbalock);
6755 return rpi_hdr;
6756
6757 err_free_coherent:
6758 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
6759 dmabuf->virt, dmabuf->phys);
6760 err_free_dmabuf:
6761 kfree(dmabuf);
6762 return NULL;
6763}
6764
6765/**
6766 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
6767 * @phba: pointer to lpfc hba data structure.
6768 *
6769 * This routine is invoked to remove all memory resources allocated
James Smart6d368e52011-05-24 11:44:12 -04006770 * to support rpis for SLI4 ports not supporting extents. This routine
6771 * presumes the caller has released all rpis consumed by fabric or port
6772 * logins and is prepared to have the header pages removed.
James Smartda0436e2009-05-22 14:51:39 -04006773 **/
6774void
6775lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
6776{
6777 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
6778
James Smart6d368e52011-05-24 11:44:12 -04006779 if (!phba->sli4_hba.rpi_hdrs_in_use)
6780 goto exit;
6781
James Smartda0436e2009-05-22 14:51:39 -04006782 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
6783 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
6784 list_del(&rpi_hdr->list);
6785 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
6786 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
6787 kfree(rpi_hdr->dmabuf);
6788 kfree(rpi_hdr);
6789 }
James Smart6d368e52011-05-24 11:44:12 -04006790 exit:
6791 /* There are no rpis available to the port now. */
6792 phba->sli4_hba.next_rpi = 0;
James Smartda0436e2009-05-22 14:51:39 -04006793}
6794
6795/**
James Smart3772a992009-05-22 14:50:54 -04006796 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
6797 * @pdev: pointer to pci device data structure.
6798 *
6799 * This routine is invoked to allocate the driver hba data structure for an
6800 * HBA device. If the allocation is successful, the phba reference to the
6801 * PCI device data structure is set.
6802 *
6803 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006804 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04006805 * NULL - error
6806 **/
6807static struct lpfc_hba *
6808lpfc_hba_alloc(struct pci_dev *pdev)
6809{
6810 struct lpfc_hba *phba;
6811
6812 /* Allocate memory for HBA structure */
6813 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
6814 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02006815 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04006816 return NULL;
6817 }
6818
6819 /* Set reference to PCI device in HBA structure */
6820 phba->pcidev = pdev;
6821
6822 /* Assign an unused board number */
6823 phba->brd_no = lpfc_get_instance();
6824 if (phba->brd_no < 0) {
6825 kfree(phba);
6826 return NULL;
6827 }
James Smart65791f12016-07-06 12:35:56 -07006828 phba->eratt_poll_interval = LPFC_ERATT_POLL_INTERVAL;
James Smart3772a992009-05-22 14:50:54 -04006829
James Smart4fede782010-01-26 23:08:55 -05006830 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04006831 INIT_LIST_HEAD(&phba->ct_ev_waiters);
6832
James Smart3772a992009-05-22 14:50:54 -04006833 return phba;
6834}
6835
6836/**
6837 * lpfc_hba_free - Free driver hba data structure with a device.
6838 * @phba: pointer to lpfc hba data structure.
6839 *
6840 * This routine is invoked to free the driver hba data structure with an
6841 * HBA device.
6842 **/
6843static void
6844lpfc_hba_free(struct lpfc_hba *phba)
6845{
6846 /* Release the driver assigned board number */
6847 idr_remove(&lpfc_hba_index, phba->brd_no);
6848
James Smart895427b2017-02-12 13:52:30 -08006849 /* Free memory allocated with sli3 rings */
6850 kfree(phba->sli.sli3_ring);
6851 phba->sli.sli3_ring = NULL;
James Smart2a76a282012-08-03 12:35:54 -04006852
James Smart3772a992009-05-22 14:50:54 -04006853 kfree(phba);
6854 return;
6855}
6856
6857/**
6858 * lpfc_create_shost - Create hba physical port with associated scsi host.
6859 * @phba: pointer to lpfc hba data structure.
6860 *
6861 * This routine is invoked to create HBA physical port and associate a SCSI
6862 * host with it.
6863 *
6864 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006865 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006866 * other values - error
6867 **/
6868static int
6869lpfc_create_shost(struct lpfc_hba *phba)
6870{
6871 struct lpfc_vport *vport;
6872 struct Scsi_Host *shost;
6873
6874 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05006875 phba->fc_edtov = FF_DEF_EDTOV;
6876 phba->fc_ratov = FF_DEF_RATOV;
6877 phba->fc_altov = FF_DEF_ALTOV;
6878 phba->fc_arbtov = FF_DEF_ARBTOV;
6879
James Smartd7c47992010-06-08 18:31:54 -04006880 atomic_set(&phba->sdev_cnt, 0);
James Smart2cee7802017-06-01 21:07:02 -07006881 atomic_set(&phba->fc4ScsiInputRequests, 0);
6882 atomic_set(&phba->fc4ScsiOutputRequests, 0);
6883 atomic_set(&phba->fc4ScsiControlRequests, 0);
6884 atomic_set(&phba->fc4ScsiIoCmpls, 0);
6885 atomic_set(&phba->fc4NvmeInputRequests, 0);
6886 atomic_set(&phba->fc4NvmeOutputRequests, 0);
6887 atomic_set(&phba->fc4NvmeControlRequests, 0);
6888 atomic_set(&phba->fc4NvmeIoCmpls, 0);
6889 atomic_set(&phba->fc4NvmeLsRequests, 0);
6890 atomic_set(&phba->fc4NvmeLsCmpls, 0);
James Smart3de2a652007-08-02 11:09:59 -04006891 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05006892 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04006893 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05006894
6895 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05006896 phba->pport = vport;
James Smart2ea259e2017-02-12 13:52:27 -08006897
James Smartf358dd02017-02-12 13:52:34 -08006898 if (phba->nvmet_support) {
6899 /* Only 1 vport (pport) will support NVME target */
6900 if (phba->txrdy_payload_pool == NULL) {
Romain Perier771db5c2017-07-06 10:13:05 +02006901 phba->txrdy_payload_pool = dma_pool_create(
6902 "txrdy_pool", &phba->pcidev->dev,
James Smartf358dd02017-02-12 13:52:34 -08006903 TXRDY_PAYLOAD_LEN, 16, 0);
6904 if (phba->txrdy_payload_pool) {
6905 phba->targetport = NULL;
6906 phba->cfg_enable_fc4_type = LPFC_ENABLE_NVME;
6907 lpfc_printf_log(phba, KERN_INFO,
6908 LOG_INIT | LOG_NVME_DISC,
6909 "6076 NVME Target Found\n");
6910 }
6911 }
6912 }
6913
James Smart858c9f62007-06-17 19:56:39 -05006914 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04006915 /* Put reference to SCSI host to driver's device private data */
6916 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05006917
James Smart4258e982015-12-16 18:11:58 -05006918 /*
6919 * At this point we are fully registered with PSA. In addition,
6920 * any initial discovery should be completed.
6921 */
6922 vport->load_flag |= FC_ALLOW_FDMI;
James Smart8663cbb2016-03-31 14:12:33 -07006923 if (phba->cfg_enable_SmartSAN ||
6924 (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
James Smart4258e982015-12-16 18:11:58 -05006925
6926 /* Setup appropriate attribute masks */
6927 vport->fdmi_hba_mask = LPFC_FDMI2_HBA_ATTR;
James Smart8663cbb2016-03-31 14:12:33 -07006928 if (phba->cfg_enable_SmartSAN)
James Smart4258e982015-12-16 18:11:58 -05006929 vport->fdmi_port_mask = LPFC_FDMI2_SMART_ATTR;
6930 else
6931 vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
6932 }
James Smart3772a992009-05-22 14:50:54 -04006933 return 0;
6934}
dea31012005-04-17 16:05:31 -05006935
James Smart3772a992009-05-22 14:50:54 -04006936/**
6937 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
6938 * @phba: pointer to lpfc hba data structure.
6939 *
6940 * This routine is invoked to destroy HBA physical port and the associated
6941 * SCSI host.
6942 **/
6943static void
6944lpfc_destroy_shost(struct lpfc_hba *phba)
6945{
6946 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04006947
James Smart3772a992009-05-22 14:50:54 -04006948 /* Destroy physical port that associated with the SCSI host */
6949 destroy_port(vport);
6950
6951 return;
6952}
6953
6954/**
6955 * lpfc_setup_bg - Setup Block guard structures and debug areas.
6956 * @phba: pointer to lpfc hba data structure.
6957 * @shost: the shost to be used to detect Block guard settings.
6958 *
6959 * This routine sets up the local Block guard protocol settings for @shost.
6960 * This routine also allocates memory for debugging bg buffers.
6961 **/
6962static void
6963lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
6964{
James Smartbbeb79b2012-06-12 13:54:27 -04006965 uint32_t old_mask;
6966 uint32_t old_guard;
6967
James Smart3772a992009-05-22 14:50:54 -04006968 int pagecnt = 10;
James Smartb3b98b72016-10-13 15:06:06 -07006969 if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
James Smart3772a992009-05-22 14:50:54 -04006970 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6971 "1478 Registering BlockGuard with the "
6972 "SCSI layer\n");
James Smartbbeb79b2012-06-12 13:54:27 -04006973
James Smartb3b98b72016-10-13 15:06:06 -07006974 old_mask = phba->cfg_prot_mask;
6975 old_guard = phba->cfg_prot_guard;
James Smartbbeb79b2012-06-12 13:54:27 -04006976
6977 /* Only allow supported values */
James Smartb3b98b72016-10-13 15:06:06 -07006978 phba->cfg_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
James Smartbbeb79b2012-06-12 13:54:27 -04006979 SHOST_DIX_TYPE0_PROTECTION |
6980 SHOST_DIX_TYPE1_PROTECTION);
James Smartb3b98b72016-10-13 15:06:06 -07006981 phba->cfg_prot_guard &= (SHOST_DIX_GUARD_IP |
6982 SHOST_DIX_GUARD_CRC);
James Smartbbeb79b2012-06-12 13:54:27 -04006983
6984 /* DIF Type 1 protection for profiles AST1/C1 is end to end */
James Smartb3b98b72016-10-13 15:06:06 -07006985 if (phba->cfg_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
6986 phba->cfg_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
James Smartbbeb79b2012-06-12 13:54:27 -04006987
James Smartb3b98b72016-10-13 15:06:06 -07006988 if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
6989 if ((old_mask != phba->cfg_prot_mask) ||
6990 (old_guard != phba->cfg_prot_guard))
James Smartbbeb79b2012-06-12 13:54:27 -04006991 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6992 "1475 Registering BlockGuard with the "
6993 "SCSI layer: mask %d guard %d\n",
James Smartb3b98b72016-10-13 15:06:06 -07006994 phba->cfg_prot_mask,
6995 phba->cfg_prot_guard);
James Smartbbeb79b2012-06-12 13:54:27 -04006996
James Smartb3b98b72016-10-13 15:06:06 -07006997 scsi_host_set_prot(shost, phba->cfg_prot_mask);
6998 scsi_host_set_guard(shost, phba->cfg_prot_guard);
James Smartbbeb79b2012-06-12 13:54:27 -04006999 } else
7000 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7001 "1479 Not Registering BlockGuard with the SCSI "
7002 "layer, Bad protection parameters: %d %d\n",
7003 old_mask, old_guard);
James Smart98c9ea52007-10-27 13:37:33 -04007004 }
James Smartbbeb79b2012-06-12 13:54:27 -04007005
James Smart81301a92008-12-04 22:39:46 -05007006 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05007007 while (pagecnt) {
7008 spin_lock_init(&_dump_buf_lock);
7009 _dump_buf_data =
7010 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
7011 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04007012 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7013 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04007014 "_dump_buf_data at 0x%p\n",
7015 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05007016 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04007017 memset(_dump_buf_data, 0,
7018 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05007019 break;
James Smart3772a992009-05-22 14:50:54 -04007020 } else
James Smart81301a92008-12-04 22:39:46 -05007021 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05007022 }
James Smart81301a92008-12-04 22:39:46 -05007023 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04007024 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7025 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04007026 "memory for hexdump\n");
7027 } else
James Smart6a9c52c2009-10-02 15:16:51 -04007028 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7029 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05007030 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05007031 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05007032 while (pagecnt) {
7033 _dump_buf_dif =
7034 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
7035 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04007036 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7037 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04007038 "_dump_buf_dif at 0x%p\n",
7039 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05007040 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04007041 memset(_dump_buf_dif, 0,
7042 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05007043 break;
James Smart3772a992009-05-22 14:50:54 -04007044 } else
James Smart81301a92008-12-04 22:39:46 -05007045 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05007046 }
James Smart81301a92008-12-04 22:39:46 -05007047 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04007048 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7049 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04007050 "memory for hexdump\n");
7051 } else
James Smart6a9c52c2009-10-02 15:16:51 -04007052 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7053 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04007054 _dump_buf_dif);
7055}
7056
7057/**
7058 * lpfc_post_init_setup - Perform necessary device post initialization setup.
7059 * @phba: pointer to lpfc hba data structure.
7060 *
7061 * This routine is invoked to perform all the necessary post initialization
7062 * setup for the device.
7063 **/
7064static void
7065lpfc_post_init_setup(struct lpfc_hba *phba)
7066{
7067 struct Scsi_Host *shost;
7068 struct lpfc_adapter_event_header adapter_event;
7069
7070 /* Get the default values for Model Name and Description */
7071 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
7072
7073 /*
7074 * hba setup may have changed the hba_queue_depth so we need to
7075 * adjust the value of can_queue.
7076 */
7077 shost = pci_get_drvdata(phba->pcidev);
7078 shost->can_queue = phba->cfg_hba_queue_depth - 10;
7079 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
7080 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05007081
7082 lpfc_host_attrib_init(shost);
7083
James Smart2e0fef82007-06-17 19:56:36 -05007084 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7085 spin_lock_irq(shost->host_lock);
7086 lpfc_poll_start_timer(phba);
7087 spin_unlock_irq(shost->host_lock);
7088 }
James Smart8f6d98d2006-08-01 07:34:00 -04007089
James Smart93996272008-08-24 21:50:30 -04007090 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7091 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04007092 /* Send board arrival event to upper layer */
7093 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
7094 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
7095 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04007096 sizeof(adapter_event),
7097 (char *) &adapter_event,
7098 LPFC_NL_VENDOR_ID);
7099 return;
7100}
7101
7102/**
7103 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
7104 * @phba: pointer to lpfc hba data structure.
7105 *
7106 * This routine is invoked to set up the PCI device memory space for device
7107 * with SLI-3 interface spec.
7108 *
7109 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007110 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007111 * other values - error
7112 **/
7113static int
7114lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
7115{
7116 struct pci_dev *pdev;
7117 unsigned long bar0map_len, bar2map_len;
7118 int i, hbq_count;
7119 void *ptr;
7120 int error = -ENODEV;
7121
7122 /* Obtain PCI device reference */
7123 if (!phba->pcidev)
7124 return error;
7125 else
7126 pdev = phba->pcidev;
7127
7128 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05007129 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
7130 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
7131 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
7132 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smart3772a992009-05-22 14:50:54 -04007133 return error;
Michael Reed8e685972009-09-18 12:02:05 -05007134 }
7135 }
James Smart3772a992009-05-22 14:50:54 -04007136
7137 /* Get the bus address of Bar0 and Bar2 and the number of bytes
7138 * required by each mapping.
7139 */
7140 phba->pci_bar0_map = pci_resource_start(pdev, 0);
7141 bar0map_len = pci_resource_len(pdev, 0);
7142
7143 phba->pci_bar2_map = pci_resource_start(pdev, 2);
7144 bar2map_len = pci_resource_len(pdev, 2);
7145
7146 /* Map HBA SLIM to a kernel virtual address. */
7147 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
7148 if (!phba->slim_memmap_p) {
7149 dev_printk(KERN_ERR, &pdev->dev,
7150 "ioremap failed for SLIM memory.\n");
7151 goto out;
7152 }
7153
7154 /* Map HBA Control Registers to a kernel virtual address. */
7155 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
7156 if (!phba->ctrl_regs_memmap_p) {
7157 dev_printk(KERN_ERR, &pdev->dev,
7158 "ioremap failed for HBA control registers.\n");
7159 goto out_iounmap_slim;
7160 }
7161
7162 /* Allocate memory for SLI-2 structures */
Joe Perches1aee3832014-09-03 12:56:12 -04007163 phba->slim2p.virt = dma_zalloc_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7164 &phba->slim2p.phys, GFP_KERNEL);
James Smart3772a992009-05-22 14:50:54 -04007165 if (!phba->slim2p.virt)
7166 goto out_iounmap;
7167
James Smart3772a992009-05-22 14:50:54 -04007168 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04007169 phba->mbox_ext = (phba->slim2p.virt +
7170 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04007171 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
7172 phba->IOCBs = (phba->slim2p.virt +
7173 offsetof(struct lpfc_sli2_slim, IOCBs));
7174
7175 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
7176 lpfc_sli_hbq_size(),
7177 &phba->hbqslimp.phys,
7178 GFP_KERNEL);
7179 if (!phba->hbqslimp.virt)
7180 goto out_free_slim;
7181
7182 hbq_count = lpfc_sli_hbq_count();
7183 ptr = phba->hbqslimp.virt;
7184 for (i = 0; i < hbq_count; ++i) {
7185 phba->hbqs[i].hbq_virt = ptr;
7186 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
7187 ptr += (lpfc_hbq_defs[i]->entry_count *
7188 sizeof(struct lpfc_hbq_entry));
7189 }
7190 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
7191 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
7192
7193 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
7194
James Smart3772a992009-05-22 14:50:54 -04007195 phba->MBslimaddr = phba->slim_memmap_p;
7196 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
7197 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
7198 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
7199 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04007200
dea31012005-04-17 16:05:31 -05007201 return 0;
7202
dea31012005-04-17 16:05:31 -05007203out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04007204 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7205 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05007206out_iounmap:
7207 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05007208out_iounmap_slim:
dea31012005-04-17 16:05:31 -05007209 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05007210out:
7211 return error;
7212}
7213
James Smarte59058c2008-08-24 21:49:00 -04007214/**
James Smart3772a992009-05-22 14:50:54 -04007215 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
7216 * @phba: pointer to lpfc hba data structure.
7217 *
7218 * This routine is invoked to unset the PCI device memory space for device
7219 * with SLI-3 interface spec.
7220 **/
7221static void
7222lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
7223{
7224 struct pci_dev *pdev;
7225
7226 /* Obtain PCI device reference */
7227 if (!phba->pcidev)
7228 return;
7229 else
7230 pdev = phba->pcidev;
7231
7232 /* Free coherent DMA memory allocated */
7233 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
7234 phba->hbqslimp.virt, phba->hbqslimp.phys);
7235 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7236 phba->slim2p.virt, phba->slim2p.phys);
7237
7238 /* I/O memory unmap */
7239 iounmap(phba->ctrl_regs_memmap_p);
7240 iounmap(phba->slim_memmap_p);
7241
7242 return;
7243}
7244
7245/**
James Smartda0436e2009-05-22 14:51:39 -04007246 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
7247 * @phba: pointer to lpfc hba data structure.
7248 *
7249 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
7250 * done and check status.
7251 *
7252 * Return 0 if successful, otherwise -ENODEV.
7253 **/
7254int
7255lpfc_sli4_post_status_check(struct lpfc_hba *phba)
7256{
James Smart2fcee4b2010-12-15 17:57:46 -05007257 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
7258 struct lpfc_register reg_data;
7259 int i, port_error = 0;
7260 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04007261
James Smart9940b972011-03-11 16:06:12 -05007262 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
7263 memset(&reg_data, 0, sizeof(reg_data));
James Smart2fcee4b2010-12-15 17:57:46 -05007264 if (!phba->sli4_hba.PSMPHRregaddr)
James Smartda0436e2009-05-22 14:51:39 -04007265 return -ENODEV;
7266
James Smartda0436e2009-05-22 14:51:39 -04007267 /* Wait up to 30 seconds for the SLI Port POST done and ready */
7268 for (i = 0; i < 3000; i++) {
James Smart9940b972011-03-11 16:06:12 -05007269 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
7270 &portsmphr_reg.word0) ||
7271 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007272 /* Port has a fatal POST error, break out */
James Smartda0436e2009-05-22 14:51:39 -04007273 port_error = -ENODEV;
7274 break;
7275 }
James Smart2fcee4b2010-12-15 17:57:46 -05007276 if (LPFC_POST_STAGE_PORT_READY ==
7277 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
James Smartda0436e2009-05-22 14:51:39 -04007278 break;
James Smartda0436e2009-05-22 14:51:39 -04007279 msleep(10);
7280 }
7281
James Smart2fcee4b2010-12-15 17:57:46 -05007282 /*
7283 * If there was a port error during POST, then don't proceed with
7284 * other register reads as the data may not be valid. Just exit.
7285 */
7286 if (port_error) {
James Smartda0436e2009-05-22 14:51:39 -04007287 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05007288 "1408 Port Failed POST - portsmphr=0x%x, "
7289 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
7290 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
7291 portsmphr_reg.word0,
7292 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
7293 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
7294 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
7295 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
7296 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
7297 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
7298 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
7299 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
7300 } else {
James Smart28baac72010-02-12 14:42:03 -05007301 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05007302 "2534 Device Info: SLIFamily=0x%x, "
7303 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
7304 "SLIHint_2=0x%x, FT=0x%x\n",
James Smart28baac72010-02-12 14:42:03 -05007305 bf_get(lpfc_sli_intf_sli_family,
7306 &phba->sli4_hba.sli_intf),
7307 bf_get(lpfc_sli_intf_slirev,
7308 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05007309 bf_get(lpfc_sli_intf_if_type,
James Smart28baac72010-02-12 14:42:03 -05007310 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05007311 bf_get(lpfc_sli_intf_sli_hint1,
7312 &phba->sli4_hba.sli_intf),
7313 bf_get(lpfc_sli_intf_sli_hint2,
7314 &phba->sli4_hba.sli_intf),
7315 bf_get(lpfc_sli_intf_func_type,
James Smart28baac72010-02-12 14:42:03 -05007316 &phba->sli4_hba.sli_intf));
James Smart2fcee4b2010-12-15 17:57:46 -05007317 /*
7318 * Check for other Port errors during the initialization
7319 * process. Fail the load if the port did not come up
7320 * correctly.
7321 */
7322 if_type = bf_get(lpfc_sli_intf_if_type,
7323 &phba->sli4_hba.sli_intf);
7324 switch (if_type) {
7325 case LPFC_SLI_INTF_IF_TYPE_0:
7326 phba->sli4_hba.ue_mask_lo =
7327 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
7328 phba->sli4_hba.ue_mask_hi =
7329 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
7330 uerrlo_reg.word0 =
7331 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
7332 uerrhi_reg.word0 =
7333 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
7334 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
7335 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
7336 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7337 "1422 Unrecoverable Error "
7338 "Detected during POST "
7339 "uerr_lo_reg=0x%x, "
7340 "uerr_hi_reg=0x%x, "
7341 "ue_mask_lo_reg=0x%x, "
7342 "ue_mask_hi_reg=0x%x\n",
7343 uerrlo_reg.word0,
7344 uerrhi_reg.word0,
7345 phba->sli4_hba.ue_mask_lo,
7346 phba->sli4_hba.ue_mask_hi);
7347 port_error = -ENODEV;
7348 }
7349 break;
7350 case LPFC_SLI_INTF_IF_TYPE_2:
7351 /* Final checks. The port status should be clean. */
James Smart9940b972011-03-11 16:06:12 -05007352 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
7353 &reg_data.word0) ||
James Smart05580562011-05-24 11:40:48 -04007354 (bf_get(lpfc_sliport_status_err, &reg_data) &&
7355 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007356 phba->work_status[0] =
7357 readl(phba->sli4_hba.u.if_type2.
7358 ERR1regaddr);
7359 phba->work_status[1] =
7360 readl(phba->sli4_hba.u.if_type2.
7361 ERR2regaddr);
7362 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05007363 "2888 Unrecoverable port error "
7364 "following POST: port status reg "
7365 "0x%x, port_smphr reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05007366 "error 1=0x%x, error 2=0x%x\n",
7367 reg_data.word0,
7368 portsmphr_reg.word0,
7369 phba->work_status[0],
7370 phba->work_status[1]);
7371 port_error = -ENODEV;
7372 }
7373 break;
7374 case LPFC_SLI_INTF_IF_TYPE_1:
7375 default:
7376 break;
7377 }
James Smart28baac72010-02-12 14:42:03 -05007378 }
James Smartda0436e2009-05-22 14:51:39 -04007379 return port_error;
7380}
7381
7382/**
7383 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
7384 * @phba: pointer to lpfc hba data structure.
James Smart2fcee4b2010-12-15 17:57:46 -05007385 * @if_type: The SLI4 interface type getting configured.
James Smartda0436e2009-05-22 14:51:39 -04007386 *
7387 * This routine is invoked to set up SLI4 BAR0 PCI config space register
7388 * memory map.
7389 **/
7390static void
James Smart2fcee4b2010-12-15 17:57:46 -05007391lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04007392{
James Smart2fcee4b2010-12-15 17:57:46 -05007393 switch (if_type) {
7394 case LPFC_SLI_INTF_IF_TYPE_0:
7395 phba->sli4_hba.u.if_type0.UERRLOregaddr =
7396 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
7397 phba->sli4_hba.u.if_type0.UERRHIregaddr =
7398 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
7399 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
7400 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
7401 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
7402 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
7403 phba->sli4_hba.SLIINTFregaddr =
7404 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
7405 break;
7406 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart0cf07f842017-06-01 21:07:10 -07007407 phba->sli4_hba.u.if_type2.EQDregaddr =
7408 phba->sli4_hba.conf_regs_memmap_p +
7409 LPFC_CTL_PORT_EQ_DELAY_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05007410 phba->sli4_hba.u.if_type2.ERR1regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04007411 phba->sli4_hba.conf_regs_memmap_p +
7412 LPFC_CTL_PORT_ER1_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05007413 phba->sli4_hba.u.if_type2.ERR2regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04007414 phba->sli4_hba.conf_regs_memmap_p +
7415 LPFC_CTL_PORT_ER2_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05007416 phba->sli4_hba.u.if_type2.CTRLregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04007417 phba->sli4_hba.conf_regs_memmap_p +
7418 LPFC_CTL_PORT_CTL_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05007419 phba->sli4_hba.u.if_type2.STATUSregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04007420 phba->sli4_hba.conf_regs_memmap_p +
7421 LPFC_CTL_PORT_STA_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05007422 phba->sli4_hba.SLIINTFregaddr =
7423 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
7424 phba->sli4_hba.PSMPHRregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04007425 phba->sli4_hba.conf_regs_memmap_p +
7426 LPFC_CTL_PORT_SEM_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05007427 phba->sli4_hba.RQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05007428 phba->sli4_hba.conf_regs_memmap_p +
7429 LPFC_ULP0_RQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05007430 phba->sli4_hba.WQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05007431 phba->sli4_hba.conf_regs_memmap_p +
7432 LPFC_ULP0_WQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05007433 phba->sli4_hba.EQCQDBregaddr =
7434 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
7435 phba->sli4_hba.MQDBregaddr =
7436 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
7437 phba->sli4_hba.BMBXregaddr =
7438 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
7439 break;
7440 case LPFC_SLI_INTF_IF_TYPE_1:
7441 default:
7442 dev_printk(KERN_ERR, &phba->pcidev->dev,
7443 "FATAL - unsupported SLI4 interface type - %d\n",
7444 if_type);
7445 break;
7446 }
James Smartda0436e2009-05-22 14:51:39 -04007447}
7448
7449/**
7450 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
7451 * @phba: pointer to lpfc hba data structure.
7452 *
7453 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
7454 * memory map.
7455 **/
7456static void
7457lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
7458{
James Smart2fcee4b2010-12-15 17:57:46 -05007459 phba->sli4_hba.PSMPHRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
7460 LPFC_SLIPORT_IF0_SMPHR;
James Smartda0436e2009-05-22 14:51:39 -04007461 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05007462 LPFC_HST_ISR0;
James Smartda0436e2009-05-22 14:51:39 -04007463 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05007464 LPFC_HST_IMR0;
James Smartda0436e2009-05-22 14:51:39 -04007465 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05007466 LPFC_HST_ISCR0;
James Smartda0436e2009-05-22 14:51:39 -04007467}
7468
7469/**
7470 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
7471 * @phba: pointer to lpfc hba data structure.
7472 * @vf: virtual function number
7473 *
7474 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
7475 * based on the given viftual function number, @vf.
7476 *
7477 * Return 0 if successful, otherwise -ENODEV.
7478 **/
7479static int
7480lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
7481{
7482 if (vf > LPFC_VIR_FUNC_MAX)
7483 return -ENODEV;
7484
7485 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05007486 vf * LPFC_VFR_PAGE_SIZE +
7487 LPFC_ULP0_RQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04007488 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05007489 vf * LPFC_VFR_PAGE_SIZE +
7490 LPFC_ULP0_WQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04007491 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7492 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
7493 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7494 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
7495 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7496 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
7497 return 0;
7498}
7499
7500/**
7501 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
7502 * @phba: pointer to lpfc hba data structure.
7503 *
7504 * This routine is invoked to create the bootstrap mailbox
7505 * region consistent with the SLI-4 interface spec. This
7506 * routine allocates all memory necessary to communicate
7507 * mailbox commands to the port and sets up all alignment
7508 * needs. No locks are expected to be held when calling
7509 * this routine.
7510 *
7511 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007512 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04007513 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04007514 **/
7515static int
7516lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
7517{
7518 uint32_t bmbx_size;
7519 struct lpfc_dmabuf *dmabuf;
7520 struct dma_address *dma_address;
7521 uint32_t pa_addr;
7522 uint64_t phys_addr;
7523
7524 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
7525 if (!dmabuf)
7526 return -ENOMEM;
7527
7528 /*
7529 * The bootstrap mailbox region is comprised of 2 parts
7530 * plus an alignment restriction of 16 bytes.
7531 */
7532 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
Joe Perches1aee3832014-09-03 12:56:12 -04007533 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, bmbx_size,
7534 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04007535 if (!dmabuf->virt) {
7536 kfree(dmabuf);
7537 return -ENOMEM;
7538 }
James Smartda0436e2009-05-22 14:51:39 -04007539
7540 /*
7541 * Initialize the bootstrap mailbox pointers now so that the register
7542 * operations are simple later. The mailbox dma address is required
7543 * to be 16-byte aligned. Also align the virtual memory as each
7544 * maibox is copied into the bmbx mailbox region before issuing the
7545 * command to the port.
7546 */
7547 phba->sli4_hba.bmbx.dmabuf = dmabuf;
7548 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
7549
7550 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
7551 LPFC_ALIGN_16_BYTE);
7552 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
7553 LPFC_ALIGN_16_BYTE);
7554
7555 /*
7556 * Set the high and low physical addresses now. The SLI4 alignment
7557 * requirement is 16 bytes and the mailbox is posted to the port
7558 * as two 30-bit addresses. The other data is a bit marking whether
7559 * the 30-bit address is the high or low address.
7560 * Upcast bmbx aphys to 64bits so shift instruction compiles
7561 * clean on 32 bit machines.
7562 */
7563 dma_address = &phba->sli4_hba.bmbx.dma_address;
7564 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
7565 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
7566 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
7567 LPFC_BMBX_BIT1_ADDR_HI);
7568
7569 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
7570 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
7571 LPFC_BMBX_BIT1_ADDR_LO);
7572 return 0;
7573}
7574
7575/**
7576 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
7577 * @phba: pointer to lpfc hba data structure.
7578 *
7579 * This routine is invoked to teardown the bootstrap mailbox
7580 * region and release all host resources. This routine requires
7581 * the caller to ensure all mailbox commands recovered, no
7582 * additional mailbox comands are sent, and interrupts are disabled
7583 * before calling this routine.
7584 *
7585 **/
7586static void
7587lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
7588{
7589 dma_free_coherent(&phba->pcidev->dev,
7590 phba->sli4_hba.bmbx.bmbx_size,
7591 phba->sli4_hba.bmbx.dmabuf->virt,
7592 phba->sli4_hba.bmbx.dmabuf->phys);
7593
7594 kfree(phba->sli4_hba.bmbx.dmabuf);
7595 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
7596}
7597
7598/**
7599 * lpfc_sli4_read_config - Get the config parameters.
7600 * @phba: pointer to lpfc hba data structure.
7601 *
7602 * This routine is invoked to read the configuration parameters from the HBA.
7603 * The configuration parameters are used to set the base and maximum values
7604 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
7605 * allocation for the port.
7606 *
7607 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007608 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007609 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007610 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007611 **/
James Smartff78d8f2011-12-13 13:21:35 -05007612int
James Smartda0436e2009-05-22 14:51:39 -04007613lpfc_sli4_read_config(struct lpfc_hba *phba)
7614{
7615 LPFC_MBOXQ_t *pmb;
7616 struct lpfc_mbx_read_config *rd_config;
James Smart912e3ac2011-05-24 11:42:11 -04007617 union lpfc_sli4_cfg_shdr *shdr;
7618 uint32_t shdr_status, shdr_add_status;
7619 struct lpfc_mbx_get_func_cfg *get_func_cfg;
7620 struct lpfc_rsrc_desc_fcfcoe *desc;
James Smart8aa134a2012-08-14 14:25:29 -04007621 char *pdesc_0;
James Smartc6918162016-10-13 15:06:16 -07007622 uint16_t forced_link_speed;
7623 uint32_t if_type;
James Smart8aa134a2012-08-14 14:25:29 -04007624 int length, i, rc = 0, rc2;
James Smartda0436e2009-05-22 14:51:39 -04007625
7626 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7627 if (!pmb) {
7628 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7629 "2011 Unable to allocate memory for issuing "
7630 "SLI_CONFIG_SPECIAL mailbox command\n");
7631 return -ENOMEM;
7632 }
7633
7634 lpfc_read_config(phba, pmb);
7635
7636 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
7637 if (rc != MBX_SUCCESS) {
7638 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7639 "2012 Mailbox failed , mbxCmd x%x "
7640 "READ_CONFIG, mbxStatus x%x\n",
7641 bf_get(lpfc_mqe_command, &pmb->u.mqe),
7642 bf_get(lpfc_mqe_status, &pmb->u.mqe));
7643 rc = -EIO;
7644 } else {
7645 rd_config = &pmb->u.mqe.un.rd_config;
James Smartff78d8f2011-12-13 13:21:35 -05007646 if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
7647 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
7648 phba->sli4_hba.lnk_info.lnk_tp =
7649 bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
7650 phba->sli4_hba.lnk_info.lnk_no =
7651 bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
7652 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7653 "3081 lnk_type:%d, lnk_numb:%d\n",
7654 phba->sli4_hba.lnk_info.lnk_tp,
7655 phba->sli4_hba.lnk_info.lnk_no);
7656 } else
7657 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
7658 "3082 Mailbox (x%x) returned ldv:x0\n",
7659 bf_get(lpfc_mqe_command, &pmb->u.mqe));
James Smart44fd7fe2017-08-23 16:55:47 -07007660 if (bf_get(lpfc_mbx_rd_conf_bbscn_def, rd_config)) {
7661 phba->bbcredit_support = 1;
7662 phba->sli4_hba.bbscn_params.word0 = rd_config->word8;
7663 }
7664
James Smart6d368e52011-05-24 11:44:12 -04007665 phba->sli4_hba.extents_in_use =
7666 bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04007667 phba->sli4_hba.max_cfg_param.max_xri =
7668 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
7669 phba->sli4_hba.max_cfg_param.xri_base =
7670 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
7671 phba->sli4_hba.max_cfg_param.max_vpi =
7672 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
7673 phba->sli4_hba.max_cfg_param.vpi_base =
7674 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
7675 phba->sli4_hba.max_cfg_param.max_rpi =
7676 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
7677 phba->sli4_hba.max_cfg_param.rpi_base =
7678 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
7679 phba->sli4_hba.max_cfg_param.max_vfi =
7680 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
7681 phba->sli4_hba.max_cfg_param.vfi_base =
7682 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
7683 phba->sli4_hba.max_cfg_param.max_fcfi =
7684 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04007685 phba->sli4_hba.max_cfg_param.max_eq =
7686 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
7687 phba->sli4_hba.max_cfg_param.max_rq =
7688 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
7689 phba->sli4_hba.max_cfg_param.max_wq =
7690 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
7691 phba->sli4_hba.max_cfg_param.max_cq =
7692 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
7693 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
7694 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
7695 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
7696 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
James Smart5ffc2662009-11-18 15:39:44 -05007697 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
7698 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04007699 phba->max_vports = phba->max_vpi;
7700 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart6d368e52011-05-24 11:44:12 -04007701 "2003 cfg params Extents? %d "
7702 "XRI(B:%d M:%d), "
James Smartda0436e2009-05-22 14:51:39 -04007703 "VPI(B:%d M:%d) "
7704 "VFI(B:%d M:%d) "
7705 "RPI(B:%d M:%d) "
James Smart2ea259e2017-02-12 13:52:27 -08007706 "FCFI:%d EQ:%d CQ:%d WQ:%d RQ:%d\n",
James Smart6d368e52011-05-24 11:44:12 -04007707 phba->sli4_hba.extents_in_use,
James Smartda0436e2009-05-22 14:51:39 -04007708 phba->sli4_hba.max_cfg_param.xri_base,
7709 phba->sli4_hba.max_cfg_param.max_xri,
7710 phba->sli4_hba.max_cfg_param.vpi_base,
7711 phba->sli4_hba.max_cfg_param.max_vpi,
7712 phba->sli4_hba.max_cfg_param.vfi_base,
7713 phba->sli4_hba.max_cfg_param.max_vfi,
7714 phba->sli4_hba.max_cfg_param.rpi_base,
7715 phba->sli4_hba.max_cfg_param.max_rpi,
James Smart2ea259e2017-02-12 13:52:27 -08007716 phba->sli4_hba.max_cfg_param.max_fcfi,
7717 phba->sli4_hba.max_cfg_param.max_eq,
7718 phba->sli4_hba.max_cfg_param.max_cq,
7719 phba->sli4_hba.max_cfg_param.max_wq,
7720 phba->sli4_hba.max_cfg_param.max_rq);
7721
James Smartda0436e2009-05-22 14:51:39 -04007722 }
James Smart912e3ac2011-05-24 11:42:11 -04007723
7724 if (rc)
7725 goto read_cfg_out;
James Smartda0436e2009-05-22 14:51:39 -04007726
James Smartc6918162016-10-13 15:06:16 -07007727 /* Update link speed if forced link speed is supported */
7728 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7729 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
7730 forced_link_speed =
7731 bf_get(lpfc_mbx_rd_conf_link_speed, rd_config);
7732 if (forced_link_speed) {
7733 phba->hba_flag |= HBA_FORCED_LINK_SPEED;
7734
7735 switch (forced_link_speed) {
7736 case LINK_SPEED_1G:
7737 phba->cfg_link_speed =
7738 LPFC_USER_LINK_SPEED_1G;
7739 break;
7740 case LINK_SPEED_2G:
7741 phba->cfg_link_speed =
7742 LPFC_USER_LINK_SPEED_2G;
7743 break;
7744 case LINK_SPEED_4G:
7745 phba->cfg_link_speed =
7746 LPFC_USER_LINK_SPEED_4G;
7747 break;
7748 case LINK_SPEED_8G:
7749 phba->cfg_link_speed =
7750 LPFC_USER_LINK_SPEED_8G;
7751 break;
7752 case LINK_SPEED_10G:
7753 phba->cfg_link_speed =
7754 LPFC_USER_LINK_SPEED_10G;
7755 break;
7756 case LINK_SPEED_16G:
7757 phba->cfg_link_speed =
7758 LPFC_USER_LINK_SPEED_16G;
7759 break;
7760 case LINK_SPEED_32G:
7761 phba->cfg_link_speed =
7762 LPFC_USER_LINK_SPEED_32G;
7763 break;
7764 case 0xffff:
7765 phba->cfg_link_speed =
7766 LPFC_USER_LINK_SPEED_AUTO;
7767 break;
7768 default:
7769 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7770 "0047 Unrecognized link "
7771 "speed : %d\n",
7772 forced_link_speed);
7773 phba->cfg_link_speed =
7774 LPFC_USER_LINK_SPEED_AUTO;
7775 }
7776 }
7777 }
7778
James Smartda0436e2009-05-22 14:51:39 -04007779 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -04007780 length = phba->sli4_hba.max_cfg_param.max_xri -
7781 lpfc_sli4_get_els_iocb_cnt(phba);
7782 if (phba->cfg_hba_queue_depth > length) {
7783 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7784 "3361 HBA queue depth changed from %d to %d\n",
7785 phba->cfg_hba_queue_depth, length);
7786 phba->cfg_hba_queue_depth = length;
7787 }
James Smart912e3ac2011-05-24 11:42:11 -04007788
7789 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
7790 LPFC_SLI_INTF_IF_TYPE_2)
7791 goto read_cfg_out;
7792
7793 /* get the pf# and vf# for SLI4 if_type 2 port */
7794 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
7795 sizeof(struct lpfc_sli4_cfg_mhdr));
7796 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
7797 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
7798 length, LPFC_SLI4_MBX_EMBED);
7799
James Smart8aa134a2012-08-14 14:25:29 -04007800 rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart912e3ac2011-05-24 11:42:11 -04007801 shdr = (union lpfc_sli4_cfg_shdr *)
7802 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
7803 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7804 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart8aa134a2012-08-14 14:25:29 -04007805 if (rc2 || shdr_status || shdr_add_status) {
James Smart912e3ac2011-05-24 11:42:11 -04007806 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7807 "3026 Mailbox failed , mbxCmd x%x "
7808 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
7809 bf_get(lpfc_mqe_command, &pmb->u.mqe),
7810 bf_get(lpfc_mqe_status, &pmb->u.mqe));
James Smart912e3ac2011-05-24 11:42:11 -04007811 goto read_cfg_out;
7812 }
7813
7814 /* search for fc_fcoe resrouce descriptor */
7815 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
James Smart912e3ac2011-05-24 11:42:11 -04007816
James Smart8aa134a2012-08-14 14:25:29 -04007817 pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
7818 desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
7819 length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
7820 if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
7821 length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
7822 else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
7823 goto read_cfg_out;
7824
James Smart912e3ac2011-05-24 11:42:11 -04007825 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
James Smart8aa134a2012-08-14 14:25:29 -04007826 desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
James Smart912e3ac2011-05-24 11:42:11 -04007827 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
James Smart8aa134a2012-08-14 14:25:29 -04007828 bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
James Smart912e3ac2011-05-24 11:42:11 -04007829 phba->sli4_hba.iov.pf_number =
7830 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
7831 phba->sli4_hba.iov.vf_number =
7832 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
7833 break;
7834 }
7835 }
7836
7837 if (i < LPFC_RSRC_DESC_MAX_NUM)
7838 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7839 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
7840 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
7841 phba->sli4_hba.iov.vf_number);
James Smart8aa134a2012-08-14 14:25:29 -04007842 else
James Smart912e3ac2011-05-24 11:42:11 -04007843 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7844 "3028 GET_FUNCTION_CONFIG: failed to find "
7845 "Resrouce Descriptor:x%x\n",
7846 LPFC_RSRC_DESC_TYPE_FCFCOE);
James Smart912e3ac2011-05-24 11:42:11 -04007847
7848read_cfg_out:
7849 mempool_free(pmb, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04007850 return rc;
7851}
7852
7853/**
James Smart2fcee4b2010-12-15 17:57:46 -05007854 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
James Smartda0436e2009-05-22 14:51:39 -04007855 * @phba: pointer to lpfc hba data structure.
7856 *
James Smart2fcee4b2010-12-15 17:57:46 -05007857 * This routine is invoked to setup the port-side endian order when
7858 * the port if_type is 0. This routine has no function for other
7859 * if_types.
James Smartda0436e2009-05-22 14:51:39 -04007860 *
7861 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007862 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007863 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007864 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007865 **/
7866static int
7867lpfc_setup_endian_order(struct lpfc_hba *phba)
7868{
7869 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05007870 uint32_t if_type, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04007871 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
7872 HOST_ENDIAN_HIGH_WORD1};
7873
James Smart2fcee4b2010-12-15 17:57:46 -05007874 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7875 switch (if_type) {
7876 case LPFC_SLI_INTF_IF_TYPE_0:
7877 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
7878 GFP_KERNEL);
7879 if (!mboxq) {
7880 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7881 "0492 Unable to allocate memory for "
7882 "issuing SLI_CONFIG_SPECIAL mailbox "
7883 "command\n");
7884 return -ENOMEM;
7885 }
James Smartda0436e2009-05-22 14:51:39 -04007886
James Smart2fcee4b2010-12-15 17:57:46 -05007887 /*
7888 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
7889 * two words to contain special data values and no other data.
7890 */
7891 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
7892 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
7893 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7894 if (rc != MBX_SUCCESS) {
7895 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7896 "0493 SLI_CONFIG_SPECIAL mailbox "
7897 "failed with status x%x\n",
7898 rc);
7899 rc = -EIO;
7900 }
7901 mempool_free(mboxq, phba->mbox_mem_pool);
7902 break;
7903 case LPFC_SLI_INTF_IF_TYPE_2:
7904 case LPFC_SLI_INTF_IF_TYPE_1:
7905 default:
7906 break;
James Smartda0436e2009-05-22 14:51:39 -04007907 }
James Smartda0436e2009-05-22 14:51:39 -04007908 return rc;
7909}
7910
7911/**
James Smart895427b2017-02-12 13:52:30 -08007912 * lpfc_sli4_queue_verify - Verify and update EQ counts
James Smartda0436e2009-05-22 14:51:39 -04007913 * @phba: pointer to lpfc hba data structure.
7914 *
James Smart895427b2017-02-12 13:52:30 -08007915 * This routine is invoked to check the user settable queue counts for EQs.
7916 * After this routine is called the counts will be set to valid values that
James Smart5350d872011-10-10 21:33:49 -04007917 * adhere to the constraints of the system's interrupt vectors and the port's
7918 * queue resources.
James Smartda0436e2009-05-22 14:51:39 -04007919 *
7920 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007921 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007922 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04007923 **/
7924static int
James Smart5350d872011-10-10 21:33:49 -04007925lpfc_sli4_queue_verify(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04007926{
James Smart895427b2017-02-12 13:52:30 -08007927 int io_channel;
James Smart1ba981f2014-02-20 09:56:45 -05007928 int fof_vectors = phba->cfg_fof ? 1 : 0;
James Smartda0436e2009-05-22 14:51:39 -04007929
7930 /*
James Smart67d12732012-08-03 12:36:13 -04007931 * Sanity check for configured queue parameters against the run-time
James Smartda0436e2009-05-22 14:51:39 -04007932 * device parameters
7933 */
7934
James Smart67d12732012-08-03 12:36:13 -04007935 /* Sanity check on HBA EQ parameters */
James Smart895427b2017-02-12 13:52:30 -08007936 io_channel = phba->io_channel_irqs;
James Smartda0436e2009-05-22 14:51:39 -04007937
James Smart895427b2017-02-12 13:52:30 -08007938 if (phba->sli4_hba.num_online_cpu < io_channel) {
James Smart82c3e9b2012-09-29 11:29:50 -04007939 lpfc_printf_log(phba,
7940 KERN_ERR, LOG_INIT,
James Smart90695ee2012-08-14 14:25:36 -04007941 "3188 Reducing IO channels to match number of "
James Smart7bb03bb2013-04-17 20:19:16 -04007942 "online CPUs: from %d to %d\n",
James Smart895427b2017-02-12 13:52:30 -08007943 io_channel, phba->sli4_hba.num_online_cpu);
7944 io_channel = phba->sli4_hba.num_online_cpu;
James Smart90695ee2012-08-14 14:25:36 -04007945 }
7946
James Smart895427b2017-02-12 13:52:30 -08007947 if (io_channel + fof_vectors > phba->sli4_hba.max_cfg_param.max_eq) {
James Smart82c3e9b2012-09-29 11:29:50 -04007948 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7949 "2575 Reducing IO channels to match number of "
7950 "available EQs: from %d to %d\n",
James Smart895427b2017-02-12 13:52:30 -08007951 io_channel,
James Smart82c3e9b2012-09-29 11:29:50 -04007952 phba->sli4_hba.max_cfg_param.max_eq);
James Smart895427b2017-02-12 13:52:30 -08007953 io_channel = phba->sli4_hba.max_cfg_param.max_eq - fof_vectors;
James Smartda0436e2009-05-22 14:51:39 -04007954 }
James Smart67d12732012-08-03 12:36:13 -04007955
James Smart895427b2017-02-12 13:52:30 -08007956 /* The actual number of FCP / NVME event queues adopted */
7957 if (io_channel != phba->io_channel_irqs)
7958 phba->io_channel_irqs = io_channel;
7959 if (phba->cfg_fcp_io_channel > io_channel)
7960 phba->cfg_fcp_io_channel = io_channel;
7961 if (phba->cfg_nvme_io_channel > io_channel)
7962 phba->cfg_nvme_io_channel = io_channel;
James Smartbcb24f62017-11-20 16:00:36 -08007963 if (phba->nvmet_support) {
7964 if (phba->cfg_nvme_io_channel < phba->cfg_nvmet_mrq)
7965 phba->cfg_nvmet_mrq = phba->cfg_nvme_io_channel;
7966 }
7967 if (phba->cfg_nvmet_mrq > LPFC_NVMET_MRQ_MAX)
7968 phba->cfg_nvmet_mrq = LPFC_NVMET_MRQ_MAX;
James Smart895427b2017-02-12 13:52:30 -08007969
7970 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2d7dbc42017-02-12 13:52:35 -08007971 "2574 IO channels: irqs %d fcp %d nvme %d MRQ: %d\n",
James Smart895427b2017-02-12 13:52:30 -08007972 phba->io_channel_irqs, phba->cfg_fcp_io_channel,
James Smart2d7dbc42017-02-12 13:52:35 -08007973 phba->cfg_nvme_io_channel, phba->cfg_nvmet_mrq);
James Smartda0436e2009-05-22 14:51:39 -04007974
James Smartda0436e2009-05-22 14:51:39 -04007975 /* Get EQ depth from module parameter, fake the default for now */
7976 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
7977 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
7978
James Smart5350d872011-10-10 21:33:49 -04007979 /* Get CQ depth from module parameter, fake the default for now */
7980 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
7981 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
James Smart5350d872011-10-10 21:33:49 -04007982 return 0;
James Smart895427b2017-02-12 13:52:30 -08007983}
7984
7985static int
7986lpfc_alloc_nvme_wq_cq(struct lpfc_hba *phba, int wqidx)
7987{
7988 struct lpfc_queue *qdesc;
James Smart895427b2017-02-12 13:52:30 -08007989
James Smarta51e41b2017-12-08 17:18:06 -08007990 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
James Smart81b96ed2017-11-20 16:00:29 -08007991 phba->sli4_hba.cq_esize,
James Smarta51e41b2017-12-08 17:18:06 -08007992 LPFC_CQE_EXP_COUNT);
James Smart895427b2017-02-12 13:52:30 -08007993 if (!qdesc) {
7994 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7995 "0508 Failed allocate fast-path NVME CQ (%d)\n",
7996 wqidx);
7997 return 1;
7998 }
7999 phba->sli4_hba.nvme_cq[wqidx] = qdesc;
8000
James Smarta51e41b2017-12-08 17:18:06 -08008001 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
8002 LPFC_WQE128_SIZE, LPFC_WQE_EXP_COUNT);
James Smart895427b2017-02-12 13:52:30 -08008003 if (!qdesc) {
8004 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8005 "0509 Failed allocate fast-path NVME WQ (%d)\n",
8006 wqidx);
8007 return 1;
8008 }
8009 phba->sli4_hba.nvme_wq[wqidx] = qdesc;
8010 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8011 return 0;
8012}
8013
8014static int
8015lpfc_alloc_fcp_wq_cq(struct lpfc_hba *phba, int wqidx)
8016{
8017 struct lpfc_queue *qdesc;
James Smartc176ffa2018-01-30 15:58:46 -08008018 uint32_t wqesize;
James Smart895427b2017-02-12 13:52:30 -08008019
8020 /* Create Fast Path FCP CQs */
James Smartc176ffa2018-01-30 15:58:46 -08008021 if (phba->enab_exp_wqcq_pages)
James Smarta51e41b2017-12-08 17:18:06 -08008022 /* Increase the CQ size when WQEs contain an embedded cdb */
8023 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
8024 phba->sli4_hba.cq_esize,
8025 LPFC_CQE_EXP_COUNT);
8026
8027 else
8028 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8029 phba->sli4_hba.cq_esize,
8030 phba->sli4_hba.cq_ecount);
James Smart895427b2017-02-12 13:52:30 -08008031 if (!qdesc) {
8032 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8033 "0499 Failed allocate fast-path FCP CQ (%d)\n", wqidx);
8034 return 1;
8035 }
8036 phba->sli4_hba.fcp_cq[wqidx] = qdesc;
8037
8038 /* Create Fast Path FCP WQs */
James Smartc176ffa2018-01-30 15:58:46 -08008039 if (phba->enab_exp_wqcq_pages) {
James Smarta51e41b2017-12-08 17:18:06 -08008040 /* Increase the WQ size when WQEs contain an embedded cdb */
James Smartc176ffa2018-01-30 15:58:46 -08008041 wqesize = (phba->fcp_embed_io) ?
8042 LPFC_WQE128_SIZE : phba->sli4_hba.wq_esize;
James Smarta51e41b2017-12-08 17:18:06 -08008043 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
James Smartc176ffa2018-01-30 15:58:46 -08008044 wqesize,
James Smarta51e41b2017-12-08 17:18:06 -08008045 LPFC_WQE_EXP_COUNT);
James Smartc176ffa2018-01-30 15:58:46 -08008046 } else
James Smarta51e41b2017-12-08 17:18:06 -08008047 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8048 phba->sli4_hba.wq_esize,
8049 phba->sli4_hba.wq_ecount);
James Smartc176ffa2018-01-30 15:58:46 -08008050
James Smart895427b2017-02-12 13:52:30 -08008051 if (!qdesc) {
8052 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8053 "0503 Failed allocate fast-path FCP WQ (%d)\n",
8054 wqidx);
8055 return 1;
8056 }
8057 phba->sli4_hba.fcp_wq[wqidx] = qdesc;
8058 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8059 return 0;
James Smart5350d872011-10-10 21:33:49 -04008060}
8061
8062/**
8063 * lpfc_sli4_queue_create - Create all the SLI4 queues
8064 * @phba: pointer to lpfc hba data structure.
8065 *
8066 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
8067 * operation. For each SLI4 queue type, the parameters such as queue entry
8068 * count (queue depth) shall be taken from the module parameter. For now,
8069 * we just use some constant number as place holder.
8070 *
8071 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07008072 * 0 - successful
James Smart5350d872011-10-10 21:33:49 -04008073 * -ENOMEM - No availble memory
8074 * -EIO - The mailbox failed to complete successfully.
8075 **/
8076int
8077lpfc_sli4_queue_create(struct lpfc_hba *phba)
8078{
8079 struct lpfc_queue *qdesc;
James Smartd1f525a2017-04-21 16:04:55 -07008080 int idx, io_channel;
James Smart5350d872011-10-10 21:33:49 -04008081
8082 /*
James Smart67d12732012-08-03 12:36:13 -04008083 * Create HBA Record arrays.
James Smart895427b2017-02-12 13:52:30 -08008084 * Both NVME and FCP will share that same vectors / EQs
James Smart5350d872011-10-10 21:33:49 -04008085 */
James Smart895427b2017-02-12 13:52:30 -08008086 io_channel = phba->io_channel_irqs;
8087 if (!io_channel)
James Smart67d12732012-08-03 12:36:13 -04008088 return -ERANGE;
James Smart5350d872011-10-10 21:33:49 -04008089
James Smart67d12732012-08-03 12:36:13 -04008090 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
8091 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
8092 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
8093 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
8094 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
8095 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
James Smart895427b2017-02-12 13:52:30 -08008096 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
8097 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
8098 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
8099 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
James Smart67d12732012-08-03 12:36:13 -04008100
James Smart895427b2017-02-12 13:52:30 -08008101 phba->sli4_hba.hba_eq = kcalloc(io_channel,
8102 sizeof(struct lpfc_queue *),
8103 GFP_KERNEL);
James Smart67d12732012-08-03 12:36:13 -04008104 if (!phba->sli4_hba.hba_eq) {
James Smartda0436e2009-05-22 14:51:39 -04008105 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04008106 "2576 Failed allocate memory for "
8107 "fast-path EQ record array\n");
James Smartda0436e2009-05-22 14:51:39 -04008108 goto out_error;
8109 }
James Smart67d12732012-08-03 12:36:13 -04008110
James Smart895427b2017-02-12 13:52:30 -08008111 if (phba->cfg_fcp_io_channel) {
8112 phba->sli4_hba.fcp_cq = kcalloc(phba->cfg_fcp_io_channel,
8113 sizeof(struct lpfc_queue *),
8114 GFP_KERNEL);
8115 if (!phba->sli4_hba.fcp_cq) {
8116 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8117 "2577 Failed allocate memory for "
8118 "fast-path CQ record array\n");
8119 goto out_error;
8120 }
8121 phba->sli4_hba.fcp_wq = kcalloc(phba->cfg_fcp_io_channel,
8122 sizeof(struct lpfc_queue *),
8123 GFP_KERNEL);
8124 if (!phba->sli4_hba.fcp_wq) {
8125 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8126 "2578 Failed allocate memory for "
8127 "fast-path FCP WQ record array\n");
8128 goto out_error;
8129 }
8130 /*
8131 * Since the first EQ can have multiple CQs associated with it,
8132 * this array is used to quickly see if we have a FCP fast-path
8133 * CQ match.
8134 */
8135 phba->sli4_hba.fcp_cq_map = kcalloc(phba->cfg_fcp_io_channel,
8136 sizeof(uint16_t),
8137 GFP_KERNEL);
8138 if (!phba->sli4_hba.fcp_cq_map) {
8139 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8140 "2545 Failed allocate memory for "
8141 "fast-path CQ map\n");
8142 goto out_error;
8143 }
James Smart67d12732012-08-03 12:36:13 -04008144 }
8145
James Smart895427b2017-02-12 13:52:30 -08008146 if (phba->cfg_nvme_io_channel) {
8147 phba->sli4_hba.nvme_cq = kcalloc(phba->cfg_nvme_io_channel,
8148 sizeof(struct lpfc_queue *),
8149 GFP_KERNEL);
8150 if (!phba->sli4_hba.nvme_cq) {
8151 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8152 "6077 Failed allocate memory for "
8153 "fast-path CQ record array\n");
8154 goto out_error;
8155 }
8156
James Smart895427b2017-02-12 13:52:30 -08008157 phba->sli4_hba.nvme_wq = kcalloc(phba->cfg_nvme_io_channel,
8158 sizeof(struct lpfc_queue *),
8159 GFP_KERNEL);
8160 if (!phba->sli4_hba.nvme_wq) {
8161 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8162 "2581 Failed allocate memory for "
8163 "fast-path NVME WQ record array\n");
8164 goto out_error;
8165 }
8166
8167 /*
8168 * Since the first EQ can have multiple CQs associated with it,
8169 * this array is used to quickly see if we have a NVME fast-path
8170 * CQ match.
8171 */
8172 phba->sli4_hba.nvme_cq_map = kcalloc(phba->cfg_nvme_io_channel,
8173 sizeof(uint16_t),
8174 GFP_KERNEL);
8175 if (!phba->sli4_hba.nvme_cq_map) {
8176 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8177 "6078 Failed allocate memory for "
8178 "fast-path CQ map\n");
8179 goto out_error;
8180 }
James Smart2d7dbc42017-02-12 13:52:35 -08008181
8182 if (phba->nvmet_support) {
8183 phba->sli4_hba.nvmet_cqset = kcalloc(
8184 phba->cfg_nvmet_mrq,
8185 sizeof(struct lpfc_queue *),
8186 GFP_KERNEL);
8187 if (!phba->sli4_hba.nvmet_cqset) {
8188 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8189 "3121 Fail allocate memory for "
8190 "fast-path CQ set array\n");
8191 goto out_error;
8192 }
8193 phba->sli4_hba.nvmet_mrq_hdr = kcalloc(
8194 phba->cfg_nvmet_mrq,
8195 sizeof(struct lpfc_queue *),
8196 GFP_KERNEL);
8197 if (!phba->sli4_hba.nvmet_mrq_hdr) {
8198 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8199 "3122 Fail allocate memory for "
8200 "fast-path RQ set hdr array\n");
8201 goto out_error;
8202 }
8203 phba->sli4_hba.nvmet_mrq_data = kcalloc(
8204 phba->cfg_nvmet_mrq,
8205 sizeof(struct lpfc_queue *),
8206 GFP_KERNEL);
8207 if (!phba->sli4_hba.nvmet_mrq_data) {
8208 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8209 "3124 Fail allocate memory for "
8210 "fast-path RQ set data array\n");
8211 goto out_error;
8212 }
8213 }
James Smart67d12732012-08-03 12:36:13 -04008214 }
James Smartda0436e2009-05-22 14:51:39 -04008215
James Smart895427b2017-02-12 13:52:30 -08008216 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
James Smart67d12732012-08-03 12:36:13 -04008217
James Smart895427b2017-02-12 13:52:30 -08008218 /* Create HBA Event Queues (EQs) */
8219 for (idx = 0; idx < io_channel; idx++) {
James Smart67d12732012-08-03 12:36:13 -04008220 /* Create EQs */
James Smart81b96ed2017-11-20 16:00:29 -08008221 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8222 phba->sli4_hba.eq_esize,
James Smartda0436e2009-05-22 14:51:39 -04008223 phba->sli4_hba.eq_ecount);
8224 if (!qdesc) {
8225 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04008226 "0497 Failed allocate EQ (%d)\n", idx);
8227 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008228 }
James Smart67d12732012-08-03 12:36:13 -04008229 phba->sli4_hba.hba_eq[idx] = qdesc;
James Smartda0436e2009-05-22 14:51:39 -04008230 }
8231
James Smart895427b2017-02-12 13:52:30 -08008232 /* FCP and NVME io channels are not required to be balanced */
8233
8234 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++)
8235 if (lpfc_alloc_fcp_wq_cq(phba, idx))
8236 goto out_error;
8237
8238 for (idx = 0; idx < phba->cfg_nvme_io_channel; idx++)
8239 if (lpfc_alloc_nvme_wq_cq(phba, idx))
8240 goto out_error;
James Smart67d12732012-08-03 12:36:13 -04008241
James Smart2d7dbc42017-02-12 13:52:35 -08008242 if (phba->nvmet_support) {
8243 for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
8244 qdesc = lpfc_sli4_queue_alloc(phba,
James Smart81b96ed2017-11-20 16:00:29 -08008245 LPFC_DEFAULT_PAGE_SIZE,
8246 phba->sli4_hba.cq_esize,
8247 phba->sli4_hba.cq_ecount);
James Smart2d7dbc42017-02-12 13:52:35 -08008248 if (!qdesc) {
8249 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8250 "3142 Failed allocate NVME "
8251 "CQ Set (%d)\n", idx);
8252 goto out_error;
8253 }
8254 phba->sli4_hba.nvmet_cqset[idx] = qdesc;
8255 }
8256 }
8257
James Smartda0436e2009-05-22 14:51:39 -04008258 /*
James Smart67d12732012-08-03 12:36:13 -04008259 * Create Slow Path Completion Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04008260 */
8261
James Smartda0436e2009-05-22 14:51:39 -04008262 /* Create slow-path Mailbox Command Complete Queue */
James Smart81b96ed2017-11-20 16:00:29 -08008263 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8264 phba->sli4_hba.cq_esize,
James Smartda0436e2009-05-22 14:51:39 -04008265 phba->sli4_hba.cq_ecount);
8266 if (!qdesc) {
8267 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8268 "0500 Failed allocate slow-path mailbox CQ\n");
James Smart67d12732012-08-03 12:36:13 -04008269 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008270 }
8271 phba->sli4_hba.mbx_cq = qdesc;
8272
8273 /* Create slow-path ELS Complete Queue */
James Smart81b96ed2017-11-20 16:00:29 -08008274 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8275 phba->sli4_hba.cq_esize,
James Smartda0436e2009-05-22 14:51:39 -04008276 phba->sli4_hba.cq_ecount);
8277 if (!qdesc) {
8278 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8279 "0501 Failed allocate slow-path ELS CQ\n");
James Smart67d12732012-08-03 12:36:13 -04008280 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008281 }
8282 phba->sli4_hba.els_cq = qdesc;
8283
James Smartda0436e2009-05-22 14:51:39 -04008284
James Smart5350d872011-10-10 21:33:49 -04008285 /*
James Smart67d12732012-08-03 12:36:13 -04008286 * Create Slow Path Work Queues (WQs)
James Smart5350d872011-10-10 21:33:49 -04008287 */
James Smartda0436e2009-05-22 14:51:39 -04008288
8289 /* Create Mailbox Command Queue */
James Smartda0436e2009-05-22 14:51:39 -04008290
James Smart81b96ed2017-11-20 16:00:29 -08008291 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8292 phba->sli4_hba.mq_esize,
James Smartda0436e2009-05-22 14:51:39 -04008293 phba->sli4_hba.mq_ecount);
8294 if (!qdesc) {
8295 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8296 "0505 Failed allocate slow-path MQ\n");
James Smart67d12732012-08-03 12:36:13 -04008297 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008298 }
8299 phba->sli4_hba.mbx_wq = qdesc;
8300
8301 /*
James Smart67d12732012-08-03 12:36:13 -04008302 * Create ELS Work Queues
James Smartda0436e2009-05-22 14:51:39 -04008303 */
James Smartda0436e2009-05-22 14:51:39 -04008304
8305 /* Create slow-path ELS Work Queue */
James Smart81b96ed2017-11-20 16:00:29 -08008306 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8307 phba->sli4_hba.wq_esize,
James Smartda0436e2009-05-22 14:51:39 -04008308 phba->sli4_hba.wq_ecount);
8309 if (!qdesc) {
8310 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8311 "0504 Failed allocate slow-path ELS WQ\n");
James Smart67d12732012-08-03 12:36:13 -04008312 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008313 }
8314 phba->sli4_hba.els_wq = qdesc;
James Smart895427b2017-02-12 13:52:30 -08008315 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8316
8317 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
8318 /* Create NVME LS Complete Queue */
James Smart81b96ed2017-11-20 16:00:29 -08008319 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8320 phba->sli4_hba.cq_esize,
James Smart895427b2017-02-12 13:52:30 -08008321 phba->sli4_hba.cq_ecount);
8322 if (!qdesc) {
8323 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8324 "6079 Failed allocate NVME LS CQ\n");
8325 goto out_error;
8326 }
8327 phba->sli4_hba.nvmels_cq = qdesc;
8328
8329 /* Create NVME LS Work Queue */
James Smart81b96ed2017-11-20 16:00:29 -08008330 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8331 phba->sli4_hba.wq_esize,
James Smart895427b2017-02-12 13:52:30 -08008332 phba->sli4_hba.wq_ecount);
8333 if (!qdesc) {
8334 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8335 "6080 Failed allocate NVME LS WQ\n");
8336 goto out_error;
8337 }
8338 phba->sli4_hba.nvmels_wq = qdesc;
8339 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8340 }
James Smartda0436e2009-05-22 14:51:39 -04008341
James Smartda0436e2009-05-22 14:51:39 -04008342 /*
8343 * Create Receive Queue (RQ)
8344 */
James Smartda0436e2009-05-22 14:51:39 -04008345
8346 /* Create Receive Queue for header */
James Smart81b96ed2017-11-20 16:00:29 -08008347 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8348 phba->sli4_hba.rq_esize,
James Smartda0436e2009-05-22 14:51:39 -04008349 phba->sli4_hba.rq_ecount);
8350 if (!qdesc) {
8351 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8352 "0506 Failed allocate receive HRQ\n");
James Smart67d12732012-08-03 12:36:13 -04008353 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008354 }
8355 phba->sli4_hba.hdr_rq = qdesc;
8356
8357 /* Create Receive Queue for data */
James Smart81b96ed2017-11-20 16:00:29 -08008358 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8359 phba->sli4_hba.rq_esize,
James Smartda0436e2009-05-22 14:51:39 -04008360 phba->sli4_hba.rq_ecount);
8361 if (!qdesc) {
8362 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8363 "0507 Failed allocate receive DRQ\n");
James Smart67d12732012-08-03 12:36:13 -04008364 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008365 }
8366 phba->sli4_hba.dat_rq = qdesc;
8367
James Smart2d7dbc42017-02-12 13:52:35 -08008368 if (phba->nvmet_support) {
8369 for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
8370 /* Create NVMET Receive Queue for header */
8371 qdesc = lpfc_sli4_queue_alloc(phba,
James Smart81b96ed2017-11-20 16:00:29 -08008372 LPFC_DEFAULT_PAGE_SIZE,
James Smart2d7dbc42017-02-12 13:52:35 -08008373 phba->sli4_hba.rq_esize,
James Smart61f3d4b2017-05-15 15:20:41 -07008374 LPFC_NVMET_RQE_DEF_COUNT);
James Smart2d7dbc42017-02-12 13:52:35 -08008375 if (!qdesc) {
8376 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8377 "3146 Failed allocate "
8378 "receive HRQ\n");
8379 goto out_error;
8380 }
8381 phba->sli4_hba.nvmet_mrq_hdr[idx] = qdesc;
8382
8383 /* Only needed for header of RQ pair */
8384 qdesc->rqbp = kzalloc(sizeof(struct lpfc_rqb),
8385 GFP_KERNEL);
8386 if (qdesc->rqbp == NULL) {
8387 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8388 "6131 Failed allocate "
8389 "Header RQBP\n");
8390 goto out_error;
8391 }
8392
Dick Kennedy4b40d022017-08-23 16:55:38 -07008393 /* Put list in known state in case driver load fails. */
8394 INIT_LIST_HEAD(&qdesc->rqbp->rqb_buffer_list);
8395
James Smart2d7dbc42017-02-12 13:52:35 -08008396 /* Create NVMET Receive Queue for data */
8397 qdesc = lpfc_sli4_queue_alloc(phba,
James Smart81b96ed2017-11-20 16:00:29 -08008398 LPFC_DEFAULT_PAGE_SIZE,
James Smart2d7dbc42017-02-12 13:52:35 -08008399 phba->sli4_hba.rq_esize,
James Smart61f3d4b2017-05-15 15:20:41 -07008400 LPFC_NVMET_RQE_DEF_COUNT);
James Smart2d7dbc42017-02-12 13:52:35 -08008401 if (!qdesc) {
8402 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8403 "3156 Failed allocate "
8404 "receive DRQ\n");
8405 goto out_error;
8406 }
8407 phba->sli4_hba.nvmet_mrq_data[idx] = qdesc;
8408 }
8409 }
8410
James Smart1ba981f2014-02-20 09:56:45 -05008411 /* Create the Queues needed for Flash Optimized Fabric operations */
8412 if (phba->cfg_fof)
8413 lpfc_fof_queue_create(phba);
James Smartda0436e2009-05-22 14:51:39 -04008414 return 0;
8415
James Smartda0436e2009-05-22 14:51:39 -04008416out_error:
James Smart67d12732012-08-03 12:36:13 -04008417 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04008418 return -ENOMEM;
8419}
8420
James Smart895427b2017-02-12 13:52:30 -08008421static inline void
8422__lpfc_sli4_release_queue(struct lpfc_queue **qp)
8423{
8424 if (*qp != NULL) {
8425 lpfc_sli4_queue_free(*qp);
8426 *qp = NULL;
8427 }
8428}
8429
8430static inline void
8431lpfc_sli4_release_queues(struct lpfc_queue ***qs, int max)
8432{
8433 int idx;
8434
8435 if (*qs == NULL)
8436 return;
8437
8438 for (idx = 0; idx < max; idx++)
8439 __lpfc_sli4_release_queue(&(*qs)[idx]);
8440
8441 kfree(*qs);
8442 *qs = NULL;
8443}
8444
8445static inline void
8446lpfc_sli4_release_queue_map(uint16_t **qmap)
8447{
8448 if (*qmap != NULL) {
8449 kfree(*qmap);
8450 *qmap = NULL;
8451 }
8452}
8453
James Smartda0436e2009-05-22 14:51:39 -04008454/**
8455 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
8456 * @phba: pointer to lpfc hba data structure.
8457 *
8458 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
8459 * operation.
8460 *
8461 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008462 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008463 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04008464 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04008465 **/
James Smart5350d872011-10-10 21:33:49 -04008466void
James Smartda0436e2009-05-22 14:51:39 -04008467lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
8468{
James Smart1ba981f2014-02-20 09:56:45 -05008469 if (phba->cfg_fof)
8470 lpfc_fof_queue_destroy(phba);
8471
James Smart895427b2017-02-12 13:52:30 -08008472 /* Release HBA eqs */
8473 lpfc_sli4_release_queues(&phba->sli4_hba.hba_eq, phba->io_channel_irqs);
James Smart67d12732012-08-03 12:36:13 -04008474
James Smart895427b2017-02-12 13:52:30 -08008475 /* Release FCP cqs */
8476 lpfc_sli4_release_queues(&phba->sli4_hba.fcp_cq,
James Smartd1f525a2017-04-21 16:04:55 -07008477 phba->cfg_fcp_io_channel);
James Smart67d12732012-08-03 12:36:13 -04008478
James Smart895427b2017-02-12 13:52:30 -08008479 /* Release FCP wqs */
8480 lpfc_sli4_release_queues(&phba->sli4_hba.fcp_wq,
James Smartd1f525a2017-04-21 16:04:55 -07008481 phba->cfg_fcp_io_channel);
James Smart67d12732012-08-03 12:36:13 -04008482
8483 /* Release FCP CQ mapping array */
James Smart895427b2017-02-12 13:52:30 -08008484 lpfc_sli4_release_queue_map(&phba->sli4_hba.fcp_cq_map);
8485
8486 /* Release NVME cqs */
8487 lpfc_sli4_release_queues(&phba->sli4_hba.nvme_cq,
8488 phba->cfg_nvme_io_channel);
8489
8490 /* Release NVME wqs */
8491 lpfc_sli4_release_queues(&phba->sli4_hba.nvme_wq,
8492 phba->cfg_nvme_io_channel);
8493
8494 /* Release NVME CQ mapping array */
8495 lpfc_sli4_release_queue_map(&phba->sli4_hba.nvme_cq_map);
James Smartda0436e2009-05-22 14:51:39 -04008496
James Smartbcb24f62017-11-20 16:00:36 -08008497 if (phba->nvmet_support) {
8498 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_cqset,
8499 phba->cfg_nvmet_mrq);
James Smart2d7dbc42017-02-12 13:52:35 -08008500
James Smartbcb24f62017-11-20 16:00:36 -08008501 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_hdr,
8502 phba->cfg_nvmet_mrq);
8503 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_data,
8504 phba->cfg_nvmet_mrq);
8505 }
James Smart2d7dbc42017-02-12 13:52:35 -08008506
James Smartda0436e2009-05-22 14:51:39 -04008507 /* Release mailbox command work queue */
James Smart895427b2017-02-12 13:52:30 -08008508 __lpfc_sli4_release_queue(&phba->sli4_hba.mbx_wq);
James Smartda0436e2009-05-22 14:51:39 -04008509
8510 /* Release ELS work queue */
James Smart895427b2017-02-12 13:52:30 -08008511 __lpfc_sli4_release_queue(&phba->sli4_hba.els_wq);
8512
8513 /* Release ELS work queue */
8514 __lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_wq);
James Smartda0436e2009-05-22 14:51:39 -04008515
8516 /* Release unsolicited receive queue */
James Smart895427b2017-02-12 13:52:30 -08008517 __lpfc_sli4_release_queue(&phba->sli4_hba.hdr_rq);
8518 __lpfc_sli4_release_queue(&phba->sli4_hba.dat_rq);
James Smartda0436e2009-05-22 14:51:39 -04008519
James Smartda0436e2009-05-22 14:51:39 -04008520 /* Release ELS complete queue */
James Smart895427b2017-02-12 13:52:30 -08008521 __lpfc_sli4_release_queue(&phba->sli4_hba.els_cq);
8522
8523 /* Release NVME LS complete queue */
8524 __lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_cq);
James Smartda0436e2009-05-22 14:51:39 -04008525
8526 /* Release mailbox command complete queue */
James Smart895427b2017-02-12 13:52:30 -08008527 __lpfc_sli4_release_queue(&phba->sli4_hba.mbx_cq);
8528
8529 /* Everything on this list has been freed */
8530 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
8531}
8532
8533int
James Smart895427b2017-02-12 13:52:30 -08008534lpfc_free_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *rq)
8535{
8536 struct lpfc_rqb *rqbp;
8537 struct lpfc_dmabuf *h_buf;
8538 struct rqb_dmabuf *rqb_buffer;
8539
8540 rqbp = rq->rqbp;
8541 while (!list_empty(&rqbp->rqb_buffer_list)) {
8542 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
8543 struct lpfc_dmabuf, list);
8544
8545 rqb_buffer = container_of(h_buf, struct rqb_dmabuf, hbuf);
8546 (rqbp->rqb_free_buffer)(phba, rqb_buffer);
8547 rqbp->buffer_count--;
8548 }
8549 return 1;
8550}
8551
8552static int
8553lpfc_create_wq_cq(struct lpfc_hba *phba, struct lpfc_queue *eq,
8554 struct lpfc_queue *cq, struct lpfc_queue *wq, uint16_t *cq_map,
8555 int qidx, uint32_t qtype)
8556{
8557 struct lpfc_sli_ring *pring;
8558 int rc;
8559
8560 if (!eq || !cq || !wq) {
8561 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8562 "6085 Fast-path %s (%d) not allocated\n",
8563 ((eq) ? ((cq) ? "WQ" : "CQ") : "EQ"), qidx);
8564 return -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04008565 }
James Smartda0436e2009-05-22 14:51:39 -04008566
James Smart895427b2017-02-12 13:52:30 -08008567 /* create the Cq first */
8568 rc = lpfc_cq_create(phba, cq, eq,
8569 (qtype == LPFC_MBOX) ? LPFC_MCQ : LPFC_WCQ, qtype);
8570 if (rc) {
8571 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8572 "6086 Failed setup of CQ (%d), rc = 0x%x\n",
8573 qidx, (uint32_t)rc);
8574 return rc;
8575 }
James Smart81b96ed2017-11-20 16:00:29 -08008576 cq->chann = qidx;
James Smart895427b2017-02-12 13:52:30 -08008577
8578 if (qtype != LPFC_MBOX) {
8579 /* Setup nvme_cq_map for fast lookup */
8580 if (cq_map)
8581 *cq_map = cq->queue_id;
8582
8583 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8584 "6087 CQ setup: cq[%d]-id=%d, parent eq[%d]-id=%d\n",
8585 qidx, cq->queue_id, qidx, eq->queue_id);
8586
8587 /* create the wq */
8588 rc = lpfc_wq_create(phba, wq, cq, qtype);
8589 if (rc) {
8590 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8591 "6123 Fail setup fastpath WQ (%d), rc = 0x%x\n",
8592 qidx, (uint32_t)rc);
8593 /* no need to tear down cq - caller will do so */
8594 return rc;
8595 }
James Smart81b96ed2017-11-20 16:00:29 -08008596 wq->chann = qidx;
James Smart895427b2017-02-12 13:52:30 -08008597
8598 /* Bind this CQ/WQ to the NVME ring */
8599 pring = wq->pring;
8600 pring->sli.sli4.wqp = (void *)wq;
8601 cq->pring = pring;
8602
8603 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8604 "2593 WQ setup: wq[%d]-id=%d assoc=%d, cq[%d]-id=%d\n",
8605 qidx, wq->queue_id, wq->assoc_qid, qidx, cq->queue_id);
8606 } else {
8607 rc = lpfc_mq_create(phba, wq, cq, LPFC_MBOX);
8608 if (rc) {
8609 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8610 "0539 Failed setup of slow-path MQ: "
8611 "rc = 0x%x\n", rc);
8612 /* no need to tear down cq - caller will do so */
8613 return rc;
8614 }
8615
8616 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8617 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
8618 phba->sli4_hba.mbx_wq->queue_id,
8619 phba->sli4_hba.mbx_cq->queue_id);
8620 }
8621
8622 return 0;
James Smartda0436e2009-05-22 14:51:39 -04008623}
8624
8625/**
8626 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
8627 * @phba: pointer to lpfc hba data structure.
8628 *
8629 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
8630 * operation.
8631 *
8632 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008633 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008634 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04008635 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04008636 **/
8637int
8638lpfc_sli4_queue_setup(struct lpfc_hba *phba)
8639{
James Smart962bc512013-01-03 15:44:00 -05008640 uint32_t shdr_status, shdr_add_status;
8641 union lpfc_sli4_cfg_shdr *shdr;
8642 LPFC_MBOXQ_t *mboxq;
James Smart895427b2017-02-12 13:52:30 -08008643 int qidx;
8644 uint32_t length, io_channel;
8645 int rc = -ENOMEM;
James Smart962bc512013-01-03 15:44:00 -05008646
8647 /* Check for dual-ULP support */
8648 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8649 if (!mboxq) {
8650 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8651 "3249 Unable to allocate memory for "
8652 "QUERY_FW_CFG mailbox command\n");
8653 return -ENOMEM;
8654 }
8655 length = (sizeof(struct lpfc_mbx_query_fw_config) -
8656 sizeof(struct lpfc_sli4_cfg_mhdr));
8657 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
8658 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
8659 length, LPFC_SLI4_MBX_EMBED);
8660
8661 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8662
8663 shdr = (union lpfc_sli4_cfg_shdr *)
8664 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
8665 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
8666 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
8667 if (shdr_status || shdr_add_status || rc) {
8668 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8669 "3250 QUERY_FW_CFG mailbox failed with status "
8670 "x%x add_status x%x, mbx status x%x\n",
8671 shdr_status, shdr_add_status, rc);
8672 if (rc != MBX_TIMEOUT)
8673 mempool_free(mboxq, phba->mbox_mem_pool);
8674 rc = -ENXIO;
8675 goto out_error;
8676 }
8677
8678 phba->sli4_hba.fw_func_mode =
8679 mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
8680 phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
8681 phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
James Smart8b017a32015-05-21 13:55:18 -04008682 phba->sli4_hba.physical_port =
8683 mboxq->u.mqe.un.query_fw_cfg.rsp.physical_port;
James Smart962bc512013-01-03 15:44:00 -05008684 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8685 "3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
8686 "ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
8687 phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
8688
8689 if (rc != MBX_TIMEOUT)
8690 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04008691
8692 /*
James Smart67d12732012-08-03 12:36:13 -04008693 * Set up HBA Event Queues (EQs)
James Smartda0436e2009-05-22 14:51:39 -04008694 */
James Smart895427b2017-02-12 13:52:30 -08008695 io_channel = phba->io_channel_irqs;
James Smartda0436e2009-05-22 14:51:39 -04008696
James Smart67d12732012-08-03 12:36:13 -04008697 /* Set up HBA event queue */
James Smart895427b2017-02-12 13:52:30 -08008698 if (io_channel && !phba->sli4_hba.hba_eq) {
James Smart2e90f4b2011-12-13 13:22:37 -05008699 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8700 "3147 Fast-path EQs not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05008701 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04008702 goto out_error;
James Smart2e90f4b2011-12-13 13:22:37 -05008703 }
James Smart895427b2017-02-12 13:52:30 -08008704 for (qidx = 0; qidx < io_channel; qidx++) {
8705 if (!phba->sli4_hba.hba_eq[qidx]) {
James Smartda0436e2009-05-22 14:51:39 -04008706 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8707 "0522 Fast-path EQ (%d) not "
James Smart895427b2017-02-12 13:52:30 -08008708 "allocated\n", qidx);
James Smart1b511972011-12-13 13:23:09 -05008709 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08008710 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008711 }
James Smart895427b2017-02-12 13:52:30 -08008712 rc = lpfc_eq_create(phba, phba->sli4_hba.hba_eq[qidx],
8713 phba->cfg_fcp_imax);
James Smartda0436e2009-05-22 14:51:39 -04008714 if (rc) {
8715 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8716 "0523 Failed setup of fast-path EQ "
James Smart895427b2017-02-12 13:52:30 -08008717 "(%d), rc = 0x%x\n", qidx,
James Smarta2fc4aef2014-09-03 12:57:55 -04008718 (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -08008719 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008720 }
8721 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008722 "2584 HBA EQ setup: queue[%d]-id=%d\n",
8723 qidx, phba->sli4_hba.hba_eq[qidx]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04008724 }
8725
James Smart895427b2017-02-12 13:52:30 -08008726 if (phba->cfg_nvme_io_channel) {
8727 if (!phba->sli4_hba.nvme_cq || !phba->sli4_hba.nvme_wq) {
James Smart67d12732012-08-03 12:36:13 -04008728 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008729 "6084 Fast-path NVME %s array not allocated\n",
8730 (phba->sli4_hba.nvme_cq) ? "CQ" : "WQ");
James Smart67d12732012-08-03 12:36:13 -04008731 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08008732 goto out_destroy;
James Smart67d12732012-08-03 12:36:13 -04008733 }
8734
James Smart895427b2017-02-12 13:52:30 -08008735 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++) {
8736 rc = lpfc_create_wq_cq(phba,
8737 phba->sli4_hba.hba_eq[
8738 qidx % io_channel],
8739 phba->sli4_hba.nvme_cq[qidx],
8740 phba->sli4_hba.nvme_wq[qidx],
8741 &phba->sli4_hba.nvme_cq_map[qidx],
8742 qidx, LPFC_NVME);
8743 if (rc) {
8744 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8745 "6123 Failed to setup fastpath "
8746 "NVME WQ/CQ (%d), rc = 0x%x\n",
8747 qidx, (uint32_t)rc);
8748 goto out_destroy;
8749 }
8750 }
James Smart67d12732012-08-03 12:36:13 -04008751 }
8752
James Smart895427b2017-02-12 13:52:30 -08008753 if (phba->cfg_fcp_io_channel) {
8754 /* Set up fast-path FCP Response Complete Queue */
8755 if (!phba->sli4_hba.fcp_cq || !phba->sli4_hba.fcp_wq) {
James Smart67d12732012-08-03 12:36:13 -04008756 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008757 "3148 Fast-path FCP %s array not allocated\n",
8758 phba->sli4_hba.fcp_cq ? "WQ" : "CQ");
James Smart67d12732012-08-03 12:36:13 -04008759 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08008760 goto out_destroy;
James Smart67d12732012-08-03 12:36:13 -04008761 }
8762
James Smart895427b2017-02-12 13:52:30 -08008763 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++) {
8764 rc = lpfc_create_wq_cq(phba,
8765 phba->sli4_hba.hba_eq[
8766 qidx % io_channel],
8767 phba->sli4_hba.fcp_cq[qidx],
8768 phba->sli4_hba.fcp_wq[qidx],
8769 &phba->sli4_hba.fcp_cq_map[qidx],
8770 qidx, LPFC_FCP);
8771 if (rc) {
8772 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8773 "0535 Failed to setup fastpath "
8774 "FCP WQ/CQ (%d), rc = 0x%x\n",
8775 qidx, (uint32_t)rc);
8776 goto out_destroy;
8777 }
8778 }
James Smart67d12732012-08-03 12:36:13 -04008779 }
James Smartda0436e2009-05-22 14:51:39 -04008780
8781 /*
James Smart895427b2017-02-12 13:52:30 -08008782 * Set up Slow Path Complete Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04008783 */
8784
James Smart895427b2017-02-12 13:52:30 -08008785 /* Set up slow-path MBOX CQ/MQ */
8786
8787 if (!phba->sli4_hba.mbx_cq || !phba->sli4_hba.mbx_wq) {
James Smartda0436e2009-05-22 14:51:39 -04008788 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008789 "0528 %s not allocated\n",
8790 phba->sli4_hba.mbx_cq ?
James Smartd1f525a2017-04-21 16:04:55 -07008791 "Mailbox WQ" : "Mailbox CQ");
James Smart1b511972011-12-13 13:23:09 -05008792 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08008793 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008794 }
James Smart895427b2017-02-12 13:52:30 -08008795
8796 rc = lpfc_create_wq_cq(phba, phba->sli4_hba.hba_eq[0],
James Smartd1f525a2017-04-21 16:04:55 -07008797 phba->sli4_hba.mbx_cq,
8798 phba->sli4_hba.mbx_wq,
8799 NULL, 0, LPFC_MBOX);
James Smartda0436e2009-05-22 14:51:39 -04008800 if (rc) {
8801 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008802 "0529 Failed setup of mailbox WQ/CQ: rc = 0x%x\n",
8803 (uint32_t)rc);
8804 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008805 }
James Smart2d7dbc42017-02-12 13:52:35 -08008806 if (phba->nvmet_support) {
8807 if (!phba->sli4_hba.nvmet_cqset) {
8808 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8809 "3165 Fast-path NVME CQ Set "
8810 "array not allocated\n");
8811 rc = -ENOMEM;
8812 goto out_destroy;
8813 }
8814 if (phba->cfg_nvmet_mrq > 1) {
8815 rc = lpfc_cq_create_set(phba,
8816 phba->sli4_hba.nvmet_cqset,
8817 phba->sli4_hba.hba_eq,
8818 LPFC_WCQ, LPFC_NVMET);
8819 if (rc) {
8820 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8821 "3164 Failed setup of NVME CQ "
8822 "Set, rc = 0x%x\n",
8823 (uint32_t)rc);
8824 goto out_destroy;
8825 }
8826 } else {
8827 /* Set up NVMET Receive Complete Queue */
8828 rc = lpfc_cq_create(phba, phba->sli4_hba.nvmet_cqset[0],
8829 phba->sli4_hba.hba_eq[0],
8830 LPFC_WCQ, LPFC_NVMET);
8831 if (rc) {
8832 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8833 "6089 Failed setup NVMET CQ: "
8834 "rc = 0x%x\n", (uint32_t)rc);
8835 goto out_destroy;
8836 }
James Smart81b96ed2017-11-20 16:00:29 -08008837 phba->sli4_hba.nvmet_cqset[0]->chann = 0;
8838
James Smart2d7dbc42017-02-12 13:52:35 -08008839 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8840 "6090 NVMET CQ setup: cq-id=%d, "
8841 "parent eq-id=%d\n",
8842 phba->sli4_hba.nvmet_cqset[0]->queue_id,
8843 phba->sli4_hba.hba_eq[0]->queue_id);
8844 }
8845 }
James Smartda0436e2009-05-22 14:51:39 -04008846
James Smart895427b2017-02-12 13:52:30 -08008847 /* Set up slow-path ELS WQ/CQ */
8848 if (!phba->sli4_hba.els_cq || !phba->sli4_hba.els_wq) {
James Smartda0436e2009-05-22 14:51:39 -04008849 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008850 "0530 ELS %s not allocated\n",
8851 phba->sli4_hba.els_cq ? "WQ" : "CQ");
James Smart1b511972011-12-13 13:23:09 -05008852 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08008853 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008854 }
James Smart895427b2017-02-12 13:52:30 -08008855 rc = lpfc_create_wq_cq(phba, phba->sli4_hba.hba_eq[0],
8856 phba->sli4_hba.els_cq,
8857 phba->sli4_hba.els_wq,
8858 NULL, 0, LPFC_ELS);
James Smartda0436e2009-05-22 14:51:39 -04008859 if (rc) {
8860 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008861 "0529 Failed setup of ELS WQ/CQ: rc = 0x%x\n",
8862 (uint32_t)rc);
8863 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008864 }
8865 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8866 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
8867 phba->sli4_hba.els_wq->queue_id,
8868 phba->sli4_hba.els_cq->queue_id);
8869
James Smart895427b2017-02-12 13:52:30 -08008870 if (phba->cfg_nvme_io_channel) {
8871 /* Set up NVME LS Complete Queue */
8872 if (!phba->sli4_hba.nvmels_cq || !phba->sli4_hba.nvmels_wq) {
8873 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8874 "6091 LS %s not allocated\n",
8875 phba->sli4_hba.nvmels_cq ? "WQ" : "CQ");
8876 rc = -ENOMEM;
8877 goto out_destroy;
8878 }
8879 rc = lpfc_create_wq_cq(phba, phba->sli4_hba.hba_eq[0],
8880 phba->sli4_hba.nvmels_cq,
8881 phba->sli4_hba.nvmels_wq,
8882 NULL, 0, LPFC_NVME_LS);
8883 if (rc) {
8884 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8885 "0529 Failed setup of NVVME LS WQ/CQ: "
8886 "rc = 0x%x\n", (uint32_t)rc);
8887 goto out_destroy;
8888 }
8889
8890 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8891 "6096 ELS WQ setup: wq-id=%d, "
8892 "parent cq-id=%d\n",
8893 phba->sli4_hba.nvmels_wq->queue_id,
8894 phba->sli4_hba.nvmels_cq->queue_id);
8895 }
8896
James Smart2d7dbc42017-02-12 13:52:35 -08008897 /*
8898 * Create NVMET Receive Queue (RQ)
8899 */
8900 if (phba->nvmet_support) {
8901 if ((!phba->sli4_hba.nvmet_cqset) ||
8902 (!phba->sli4_hba.nvmet_mrq_hdr) ||
8903 (!phba->sli4_hba.nvmet_mrq_data)) {
8904 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8905 "6130 MRQ CQ Queues not "
8906 "allocated\n");
8907 rc = -ENOMEM;
8908 goto out_destroy;
8909 }
8910 if (phba->cfg_nvmet_mrq > 1) {
8911 rc = lpfc_mrq_create(phba,
8912 phba->sli4_hba.nvmet_mrq_hdr,
8913 phba->sli4_hba.nvmet_mrq_data,
8914 phba->sli4_hba.nvmet_cqset,
8915 LPFC_NVMET);
8916 if (rc) {
8917 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8918 "6098 Failed setup of NVMET "
8919 "MRQ: rc = 0x%x\n",
8920 (uint32_t)rc);
8921 goto out_destroy;
8922 }
8923
8924 } else {
8925 rc = lpfc_rq_create(phba,
8926 phba->sli4_hba.nvmet_mrq_hdr[0],
8927 phba->sli4_hba.nvmet_mrq_data[0],
8928 phba->sli4_hba.nvmet_cqset[0],
8929 LPFC_NVMET);
8930 if (rc) {
8931 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8932 "6057 Failed setup of NVMET "
8933 "Receive Queue: rc = 0x%x\n",
8934 (uint32_t)rc);
8935 goto out_destroy;
8936 }
8937
8938 lpfc_printf_log(
8939 phba, KERN_INFO, LOG_INIT,
8940 "6099 NVMET RQ setup: hdr-rq-id=%d, "
8941 "dat-rq-id=%d parent cq-id=%d\n",
8942 phba->sli4_hba.nvmet_mrq_hdr[0]->queue_id,
8943 phba->sli4_hba.nvmet_mrq_data[0]->queue_id,
8944 phba->sli4_hba.nvmet_cqset[0]->queue_id);
8945
8946 }
8947 }
8948
James Smartda0436e2009-05-22 14:51:39 -04008949 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
8950 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8951 "0540 Receive Queue not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05008952 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08008953 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008954 }
James Smart73d91e52011-10-10 21:32:10 -04008955
James Smartda0436e2009-05-22 14:51:39 -04008956 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04008957 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04008958 if (rc) {
8959 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8960 "0541 Failed setup of Receive Queue: "
James Smarta2fc4aef2014-09-03 12:57:55 -04008961 "rc = 0x%x\n", (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -08008962 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008963 }
James Smart73d91e52011-10-10 21:32:10 -04008964
James Smartda0436e2009-05-22 14:51:39 -04008965 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8966 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
8967 "parent cq-id=%d\n",
8968 phba->sli4_hba.hdr_rq->queue_id,
8969 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04008970 phba->sli4_hba.els_cq->queue_id);
James Smart1ba981f2014-02-20 09:56:45 -05008971
8972 if (phba->cfg_fof) {
8973 rc = lpfc_fof_queue_setup(phba);
8974 if (rc) {
8975 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8976 "0549 Failed setup of FOF Queues: "
8977 "rc = 0x%x\n", rc);
James Smart895427b2017-02-12 13:52:30 -08008978 goto out_destroy;
James Smart1ba981f2014-02-20 09:56:45 -05008979 }
8980 }
James Smart2c9c5a02015-04-07 15:07:15 -04008981
James Smart43140ca2017-03-04 09:30:34 -08008982 for (qidx = 0; qidx < io_channel; qidx += LPFC_MAX_EQ_DELAY_EQID_CNT)
James Smart0cf07f842017-06-01 21:07:10 -07008983 lpfc_modify_hba_eq_delay(phba, qidx, LPFC_MAX_EQ_DELAY_EQID_CNT,
8984 phba->cfg_fcp_imax);
James Smart43140ca2017-03-04 09:30:34 -08008985
James Smartda0436e2009-05-22 14:51:39 -04008986 return 0;
8987
James Smart895427b2017-02-12 13:52:30 -08008988out_destroy:
8989 lpfc_sli4_queue_unset(phba);
James Smartda0436e2009-05-22 14:51:39 -04008990out_error:
8991 return rc;
8992}
8993
8994/**
8995 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
8996 * @phba: pointer to lpfc hba data structure.
8997 *
8998 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
8999 * operation.
9000 *
9001 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009002 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009003 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04009004 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04009005 **/
9006void
9007lpfc_sli4_queue_unset(struct lpfc_hba *phba)
9008{
James Smart895427b2017-02-12 13:52:30 -08009009 int qidx;
James Smartda0436e2009-05-22 14:51:39 -04009010
James Smart1ba981f2014-02-20 09:56:45 -05009011 /* Unset the queues created for Flash Optimized Fabric operations */
9012 if (phba->cfg_fof)
9013 lpfc_fof_queue_destroy(phba);
James Smart895427b2017-02-12 13:52:30 -08009014
James Smartda0436e2009-05-22 14:51:39 -04009015 /* Unset mailbox command work queue */
James Smart895427b2017-02-12 13:52:30 -08009016 if (phba->sli4_hba.mbx_wq)
9017 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
9018
9019 /* Unset NVME LS work queue */
9020 if (phba->sli4_hba.nvmels_wq)
9021 lpfc_wq_destroy(phba, phba->sli4_hba.nvmels_wq);
9022
James Smartda0436e2009-05-22 14:51:39 -04009023 /* Unset ELS work queue */
Colin Ian King019c0d62017-05-06 23:13:55 +01009024 if (phba->sli4_hba.els_wq)
James Smart895427b2017-02-12 13:52:30 -08009025 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
9026
James Smartda0436e2009-05-22 14:51:39 -04009027 /* Unset unsolicited receive queue */
James Smart895427b2017-02-12 13:52:30 -08009028 if (phba->sli4_hba.hdr_rq)
9029 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq,
9030 phba->sli4_hba.dat_rq);
9031
James Smartda0436e2009-05-22 14:51:39 -04009032 /* Unset FCP work queue */
James Smart895427b2017-02-12 13:52:30 -08009033 if (phba->sli4_hba.fcp_wq)
9034 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
9035 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[qidx]);
9036
9037 /* Unset NVME work queue */
9038 if (phba->sli4_hba.nvme_wq) {
9039 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
9040 lpfc_wq_destroy(phba, phba->sli4_hba.nvme_wq[qidx]);
James Smart67d12732012-08-03 12:36:13 -04009041 }
James Smart895427b2017-02-12 13:52:30 -08009042
James Smartda0436e2009-05-22 14:51:39 -04009043 /* Unset mailbox command complete queue */
James Smart895427b2017-02-12 13:52:30 -08009044 if (phba->sli4_hba.mbx_cq)
9045 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
9046
James Smartda0436e2009-05-22 14:51:39 -04009047 /* Unset ELS complete queue */
James Smart895427b2017-02-12 13:52:30 -08009048 if (phba->sli4_hba.els_cq)
9049 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
9050
9051 /* Unset NVME LS complete queue */
9052 if (phba->sli4_hba.nvmels_cq)
9053 lpfc_cq_destroy(phba, phba->sli4_hba.nvmels_cq);
9054
9055 /* Unset NVME response complete queue */
9056 if (phba->sli4_hba.nvme_cq)
9057 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
9058 lpfc_cq_destroy(phba, phba->sli4_hba.nvme_cq[qidx]);
9059
James Smartbcb24f62017-11-20 16:00:36 -08009060 if (phba->nvmet_support) {
9061 /* Unset NVMET MRQ queue */
9062 if (phba->sli4_hba.nvmet_mrq_hdr) {
9063 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
9064 lpfc_rq_destroy(
9065 phba,
James Smart2d7dbc42017-02-12 13:52:35 -08009066 phba->sli4_hba.nvmet_mrq_hdr[qidx],
9067 phba->sli4_hba.nvmet_mrq_data[qidx]);
James Smartbcb24f62017-11-20 16:00:36 -08009068 }
James Smart2d7dbc42017-02-12 13:52:35 -08009069
James Smartbcb24f62017-11-20 16:00:36 -08009070 /* Unset NVMET CQ Set complete queue */
9071 if (phba->sli4_hba.nvmet_cqset) {
9072 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
9073 lpfc_cq_destroy(
9074 phba, phba->sli4_hba.nvmet_cqset[qidx]);
9075 }
James Smart2d7dbc42017-02-12 13:52:35 -08009076 }
9077
James Smartda0436e2009-05-22 14:51:39 -04009078 /* Unset FCP response complete queue */
James Smart895427b2017-02-12 13:52:30 -08009079 if (phba->sli4_hba.fcp_cq)
9080 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
9081 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[qidx]);
9082
James Smartda0436e2009-05-22 14:51:39 -04009083 /* Unset fast-path event queue */
James Smart895427b2017-02-12 13:52:30 -08009084 if (phba->sli4_hba.hba_eq)
9085 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
9086 lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[qidx]);
James Smartda0436e2009-05-22 14:51:39 -04009087}
9088
9089/**
9090 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
9091 * @phba: pointer to lpfc hba data structure.
9092 *
9093 * This routine is invoked to allocate and set up a pool of completion queue
9094 * events. The body of the completion queue event is a completion queue entry
9095 * CQE. For now, this pool is used for the interrupt service routine to queue
9096 * the following HBA completion queue events for the worker thread to process:
9097 * - Mailbox asynchronous events
9098 * - Receive queue completion unsolicited events
9099 * Later, this can be used for all the slow-path events.
9100 *
9101 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009102 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009103 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04009104 **/
9105static int
9106lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
9107{
9108 struct lpfc_cq_event *cq_event;
9109 int i;
9110
9111 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
9112 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
9113 if (!cq_event)
9114 goto out_pool_create_fail;
9115 list_add_tail(&cq_event->list,
9116 &phba->sli4_hba.sp_cqe_event_pool);
9117 }
9118 return 0;
9119
9120out_pool_create_fail:
9121 lpfc_sli4_cq_event_pool_destroy(phba);
9122 return -ENOMEM;
9123}
9124
9125/**
9126 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
9127 * @phba: pointer to lpfc hba data structure.
9128 *
9129 * This routine is invoked to free the pool of completion queue events at
9130 * driver unload time. Note that, it is the responsibility of the driver
9131 * cleanup routine to free all the outstanding completion-queue events
9132 * allocated from this pool back into the pool before invoking this routine
9133 * to destroy the pool.
9134 **/
9135static void
9136lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
9137{
9138 struct lpfc_cq_event *cq_event, *next_cq_event;
9139
9140 list_for_each_entry_safe(cq_event, next_cq_event,
9141 &phba->sli4_hba.sp_cqe_event_pool, list) {
9142 list_del(&cq_event->list);
9143 kfree(cq_event);
9144 }
9145}
9146
9147/**
9148 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
9149 * @phba: pointer to lpfc hba data structure.
9150 *
9151 * This routine is the lock free version of the API invoked to allocate a
9152 * completion-queue event from the free pool.
9153 *
9154 * Return: Pointer to the newly allocated completion-queue event if successful
9155 * NULL otherwise.
9156 **/
9157struct lpfc_cq_event *
9158__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
9159{
9160 struct lpfc_cq_event *cq_event = NULL;
9161
9162 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
9163 struct lpfc_cq_event, list);
9164 return cq_event;
9165}
9166
9167/**
9168 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
9169 * @phba: pointer to lpfc hba data structure.
9170 *
9171 * This routine is the lock version of the API invoked to allocate a
9172 * completion-queue event from the free pool.
9173 *
9174 * Return: Pointer to the newly allocated completion-queue event if successful
9175 * NULL otherwise.
9176 **/
9177struct lpfc_cq_event *
9178lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
9179{
9180 struct lpfc_cq_event *cq_event;
9181 unsigned long iflags;
9182
9183 spin_lock_irqsave(&phba->hbalock, iflags);
9184 cq_event = __lpfc_sli4_cq_event_alloc(phba);
9185 spin_unlock_irqrestore(&phba->hbalock, iflags);
9186 return cq_event;
9187}
9188
9189/**
9190 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
9191 * @phba: pointer to lpfc hba data structure.
9192 * @cq_event: pointer to the completion queue event to be freed.
9193 *
9194 * This routine is the lock free version of the API invoked to release a
9195 * completion-queue event back into the free pool.
9196 **/
9197void
9198__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
9199 struct lpfc_cq_event *cq_event)
9200{
9201 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
9202}
9203
9204/**
9205 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
9206 * @phba: pointer to lpfc hba data structure.
9207 * @cq_event: pointer to the completion queue event to be freed.
9208 *
9209 * This routine is the lock version of the API invoked to release a
9210 * completion-queue event back into the free pool.
9211 **/
9212void
9213lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
9214 struct lpfc_cq_event *cq_event)
9215{
9216 unsigned long iflags;
9217 spin_lock_irqsave(&phba->hbalock, iflags);
9218 __lpfc_sli4_cq_event_release(phba, cq_event);
9219 spin_unlock_irqrestore(&phba->hbalock, iflags);
9220}
9221
9222/**
9223 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
9224 * @phba: pointer to lpfc hba data structure.
9225 *
9226 * This routine is to free all the pending completion-queue events to the
9227 * back into the free pool for device reset.
9228 **/
9229static void
9230lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
9231{
9232 LIST_HEAD(cqelist);
9233 struct lpfc_cq_event *cqe;
9234 unsigned long iflags;
9235
9236 /* Retrieve all the pending WCQEs from pending WCQE lists */
9237 spin_lock_irqsave(&phba->hbalock, iflags);
9238 /* Pending FCP XRI abort events */
9239 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
9240 &cqelist);
9241 /* Pending ELS XRI abort events */
9242 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
9243 &cqelist);
9244 /* Pending asynnc events */
9245 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
9246 &cqelist);
9247 spin_unlock_irqrestore(&phba->hbalock, iflags);
9248
9249 while (!list_empty(&cqelist)) {
9250 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
9251 lpfc_sli4_cq_event_release(phba, cqe);
9252 }
9253}
9254
9255/**
9256 * lpfc_pci_function_reset - Reset pci function.
9257 * @phba: pointer to lpfc hba data structure.
9258 *
9259 * This routine is invoked to request a PCI function reset. It will destroys
9260 * all resources assigned to the PCI function which originates this request.
9261 *
9262 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009263 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009264 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04009265 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04009266 **/
9267int
9268lpfc_pci_function_reset(struct lpfc_hba *phba)
9269{
9270 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05009271 uint32_t rc = 0, if_type;
James Smartda0436e2009-05-22 14:51:39 -04009272 uint32_t shdr_status, shdr_add_status;
James Smart2f6fa2c2014-09-03 12:57:08 -04009273 uint32_t rdy_chk;
9274 uint32_t port_reset = 0;
James Smartda0436e2009-05-22 14:51:39 -04009275 union lpfc_sli4_cfg_shdr *shdr;
James Smart2fcee4b2010-12-15 17:57:46 -05009276 struct lpfc_register reg_data;
James Smart2b81f942012-03-01 22:37:18 -05009277 uint16_t devid;
James Smartda0436e2009-05-22 14:51:39 -04009278
James Smart2fcee4b2010-12-15 17:57:46 -05009279 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9280 switch (if_type) {
9281 case LPFC_SLI_INTF_IF_TYPE_0:
9282 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
9283 GFP_KERNEL);
9284 if (!mboxq) {
9285 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9286 "0494 Unable to allocate memory for "
9287 "issuing SLI_FUNCTION_RESET mailbox "
9288 "command\n");
9289 return -ENOMEM;
9290 }
9291
9292 /* Setup PCI function reset mailbox-ioctl command */
9293 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
9294 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
9295 LPFC_SLI4_MBX_EMBED);
9296 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9297 shdr = (union lpfc_sli4_cfg_shdr *)
9298 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
9299 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9300 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
9301 &shdr->response);
9302 if (rc != MBX_TIMEOUT)
9303 mempool_free(mboxq, phba->mbox_mem_pool);
9304 if (shdr_status || shdr_add_status || rc) {
9305 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9306 "0495 SLI_FUNCTION_RESET mailbox "
9307 "failed with status x%x add_status x%x,"
9308 " mbx status x%x\n",
9309 shdr_status, shdr_add_status, rc);
9310 rc = -ENXIO;
9311 }
9312 break;
9313 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart2f6fa2c2014-09-03 12:57:08 -04009314wait:
9315 /*
9316 * Poll the Port Status Register and wait for RDY for
9317 * up to 30 seconds. If the port doesn't respond, treat
9318 * it as an error.
9319 */
James Smart77d093f2015-04-07 15:07:08 -04009320 for (rdy_chk = 0; rdy_chk < 1500; rdy_chk++) {
James Smart2f6fa2c2014-09-03 12:57:08 -04009321 if (lpfc_readl(phba->sli4_hba.u.if_type2.
9322 STATUSregaddr, &reg_data.word0)) {
9323 rc = -ENODEV;
9324 goto out;
9325 }
9326 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
9327 break;
9328 msleep(20);
9329 }
9330
9331 if (!bf_get(lpfc_sliport_status_rdy, &reg_data)) {
9332 phba->work_status[0] = readl(
9333 phba->sli4_hba.u.if_type2.ERR1regaddr);
9334 phba->work_status[1] = readl(
9335 phba->sli4_hba.u.if_type2.ERR2regaddr);
9336 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9337 "2890 Port not ready, port status reg "
9338 "0x%x error 1=0x%x, error 2=0x%x\n",
9339 reg_data.word0,
9340 phba->work_status[0],
9341 phba->work_status[1]);
9342 rc = -ENODEV;
9343 goto out;
9344 }
9345
9346 if (!port_reset) {
9347 /*
9348 * Reset the port now
9349 */
James Smart2fcee4b2010-12-15 17:57:46 -05009350 reg_data.word0 = 0;
9351 bf_set(lpfc_sliport_ctrl_end, &reg_data,
9352 LPFC_SLIPORT_LITTLE_ENDIAN);
9353 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
9354 LPFC_SLIPORT_INIT_PORT);
9355 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
9356 CTRLregaddr);
James Smart8fcb8ac2012-03-01 22:35:58 -05009357 /* flush */
James Smart2b81f942012-03-01 22:37:18 -05009358 pci_read_config_word(phba->pcidev,
9359 PCI_DEVICE_ID, &devid);
James Smart2fcee4b2010-12-15 17:57:46 -05009360
James Smart2f6fa2c2014-09-03 12:57:08 -04009361 port_reset = 1;
9362 msleep(20);
9363 goto wait;
9364 } else if (bf_get(lpfc_sliport_status_rn, &reg_data)) {
9365 rc = -ENODEV;
9366 goto out;
James Smart2fcee4b2010-12-15 17:57:46 -05009367 }
9368 break;
James Smart2f6fa2c2014-09-03 12:57:08 -04009369
James Smart2fcee4b2010-12-15 17:57:46 -05009370 case LPFC_SLI_INTF_IF_TYPE_1:
9371 default:
9372 break;
James Smartda0436e2009-05-22 14:51:39 -04009373 }
9374
James Smart73d91e52011-10-10 21:32:10 -04009375out:
James Smart2fcee4b2010-12-15 17:57:46 -05009376 /* Catch the not-ready port failure after a port reset. */
James Smart2f6fa2c2014-09-03 12:57:08 -04009377 if (rc) {
James Smart229adb02013-04-17 20:16:51 -04009378 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9379 "3317 HBA not functional: IP Reset Failed "
James Smart2f6fa2c2014-09-03 12:57:08 -04009380 "try: echo fw_reset > board_mode\n");
James Smart2fcee4b2010-12-15 17:57:46 -05009381 rc = -ENODEV;
James Smart229adb02013-04-17 20:16:51 -04009382 }
James Smart2fcee4b2010-12-15 17:57:46 -05009383
James Smartda0436e2009-05-22 14:51:39 -04009384 return rc;
9385}
9386
9387/**
James Smartda0436e2009-05-22 14:51:39 -04009388 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
9389 * @phba: pointer to lpfc hba data structure.
9390 *
9391 * This routine is invoked to set up the PCI device memory space for device
9392 * with SLI-4 interface spec.
9393 *
9394 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009395 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04009396 * other values - error
9397 **/
9398static int
9399lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
9400{
9401 struct pci_dev *pdev;
9402 unsigned long bar0map_len, bar1map_len, bar2map_len;
9403 int error = -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -05009404 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04009405
9406 /* Obtain PCI device reference */
9407 if (!phba->pcidev)
9408 return error;
9409 else
9410 pdev = phba->pcidev;
9411
9412 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05009413 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
9414 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
9415 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
9416 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smartda0436e2009-05-22 14:51:39 -04009417 return error;
Michael Reed8e685972009-09-18 12:02:05 -05009418 }
9419 }
James Smartda0436e2009-05-22 14:51:39 -04009420
James Smart2fcee4b2010-12-15 17:57:46 -05009421 /*
9422 * The BARs and register set definitions and offset locations are
9423 * dependent on the if_type.
9424 */
9425 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
9426 &phba->sli4_hba.sli_intf.word0)) {
9427 return error;
9428 }
9429
9430 /* There is no SLI3 failback for SLI4 devices. */
9431 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
9432 LPFC_SLI_INTF_VALID) {
9433 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9434 "2894 SLI_INTF reg contents invalid "
9435 "sli_intf reg 0x%x\n",
9436 phba->sli4_hba.sli_intf.word0);
9437 return error;
9438 }
9439
9440 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9441 /*
9442 * Get the bus address of SLI4 device Bar regions and the
9443 * number of bytes required by each mapping. The mapping of the
9444 * particular PCI BARs regions is dependent on the type of
9445 * SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -04009446 */
James Smartf5ca6f22013-09-06 12:21:09 -04009447 if (pci_resource_start(pdev, PCI_64BIT_BAR0)) {
9448 phba->pci_bar0_map = pci_resource_start(pdev, PCI_64BIT_BAR0);
9449 bar0map_len = pci_resource_len(pdev, PCI_64BIT_BAR0);
James Smart2fcee4b2010-12-15 17:57:46 -05009450
9451 /*
9452 * Map SLI4 PCI Config Space Register base to a kernel virtual
9453 * addr
9454 */
9455 phba->sli4_hba.conf_regs_memmap_p =
9456 ioremap(phba->pci_bar0_map, bar0map_len);
9457 if (!phba->sli4_hba.conf_regs_memmap_p) {
9458 dev_printk(KERN_ERR, &pdev->dev,
9459 "ioremap failed for SLI4 PCI config "
9460 "registers.\n");
9461 goto out;
9462 }
James Smartf5ca6f22013-09-06 12:21:09 -04009463 phba->pci_bar0_memmap_p = phba->sli4_hba.conf_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05009464 /* Set up BAR0 PCI config space register memory map */
9465 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smart1dfb5a42010-02-12 14:40:50 -05009466 } else {
9467 phba->pci_bar0_map = pci_resource_start(pdev, 1);
9468 bar0map_len = pci_resource_len(pdev, 1);
James Smart2fcee4b2010-12-15 17:57:46 -05009469 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
9470 dev_printk(KERN_ERR, &pdev->dev,
9471 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
9472 goto out;
9473 }
9474 phba->sli4_hba.conf_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04009475 ioremap(phba->pci_bar0_map, bar0map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05009476 if (!phba->sli4_hba.conf_regs_memmap_p) {
9477 dev_printk(KERN_ERR, &pdev->dev,
9478 "ioremap failed for SLI4 PCI config "
9479 "registers.\n");
9480 goto out;
9481 }
9482 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smartda0436e2009-05-22 14:51:39 -04009483 }
9484
James Smarte4b97942017-11-20 16:00:31 -08009485 if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
9486 if (pci_resource_start(pdev, PCI_64BIT_BAR2)) {
9487 /*
9488 * Map SLI4 if type 0 HBA Control Register base to a
9489 * kernel virtual address and setup the registers.
9490 */
9491 phba->pci_bar1_map = pci_resource_start(pdev,
9492 PCI_64BIT_BAR2);
9493 bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
9494 phba->sli4_hba.ctrl_regs_memmap_p =
9495 ioremap(phba->pci_bar1_map,
9496 bar1map_len);
9497 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
9498 dev_err(&pdev->dev,
9499 "ioremap failed for SLI4 HBA "
9500 "control registers.\n");
9501 error = -ENOMEM;
9502 goto out_iounmap_conf;
9503 }
9504 phba->pci_bar2_memmap_p =
9505 phba->sli4_hba.ctrl_regs_memmap_p;
9506 lpfc_sli4_bar1_register_memmap(phba);
9507 } else {
9508 error = -ENOMEM;
James Smart2fcee4b2010-12-15 17:57:46 -05009509 goto out_iounmap_conf;
9510 }
James Smartda0436e2009-05-22 14:51:39 -04009511 }
9512
James Smarte4b97942017-11-20 16:00:31 -08009513 if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
9514 if (pci_resource_start(pdev, PCI_64BIT_BAR4)) {
9515 /*
9516 * Map SLI4 if type 0 HBA Doorbell Register base to
9517 * a kernel virtual address and setup the registers.
9518 */
9519 phba->pci_bar2_map = pci_resource_start(pdev,
9520 PCI_64BIT_BAR4);
9521 bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
9522 phba->sli4_hba.drbl_regs_memmap_p =
9523 ioremap(phba->pci_bar2_map,
9524 bar2map_len);
9525 if (!phba->sli4_hba.drbl_regs_memmap_p) {
9526 dev_err(&pdev->dev,
9527 "ioremap failed for SLI4 HBA"
9528 " doorbell registers.\n");
9529 error = -ENOMEM;
9530 goto out_iounmap_ctrl;
9531 }
9532 phba->pci_bar4_memmap_p =
9533 phba->sli4_hba.drbl_regs_memmap_p;
9534 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
9535 if (error)
9536 goto out_iounmap_all;
9537 } else {
9538 error = -ENOMEM;
James Smart2fcee4b2010-12-15 17:57:46 -05009539 goto out_iounmap_all;
James Smarte4b97942017-11-20 16:00:31 -08009540 }
James Smartda0436e2009-05-22 14:51:39 -04009541 }
9542
James Smartb71413d2018-02-22 08:18:40 -08009543 /* Set up the EQ/CQ register handeling functions now */
9544 if (if_type <= LPFC_SLI_INTF_IF_TYPE_2) {
9545 phba->sli4_hba.sli4_eq_clr_intr = lpfc_sli4_eq_clr_intr;
9546 phba->sli4_hba.sli4_eq_release = lpfc_sli4_eq_release;
9547 phba->sli4_hba.sli4_cq_release = lpfc_sli4_cq_release;
9548 }
9549
James Smartda0436e2009-05-22 14:51:39 -04009550 return 0;
9551
9552out_iounmap_all:
9553 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
9554out_iounmap_ctrl:
9555 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
9556out_iounmap_conf:
9557 iounmap(phba->sli4_hba.conf_regs_memmap_p);
9558out:
9559 return error;
9560}
9561
9562/**
9563 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
9564 * @phba: pointer to lpfc hba data structure.
9565 *
9566 * This routine is invoked to unset the PCI device memory space for device
9567 * with SLI-4 interface spec.
9568 **/
9569static void
9570lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
9571{
James Smart2e90f4b2011-12-13 13:22:37 -05009572 uint32_t if_type;
9573 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smartda0436e2009-05-22 14:51:39 -04009574
James Smart2e90f4b2011-12-13 13:22:37 -05009575 switch (if_type) {
9576 case LPFC_SLI_INTF_IF_TYPE_0:
9577 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
9578 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
9579 iounmap(phba->sli4_hba.conf_regs_memmap_p);
9580 break;
9581 case LPFC_SLI_INTF_IF_TYPE_2:
9582 iounmap(phba->sli4_hba.conf_regs_memmap_p);
9583 break;
9584 case LPFC_SLI_INTF_IF_TYPE_1:
9585 default:
9586 dev_printk(KERN_ERR, &phba->pcidev->dev,
9587 "FATAL - unsupported SLI4 interface type - %d\n",
9588 if_type);
9589 break;
9590 }
James Smartda0436e2009-05-22 14:51:39 -04009591}
9592
9593/**
James Smart3772a992009-05-22 14:50:54 -04009594 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
9595 * @phba: pointer to lpfc hba data structure.
9596 *
9597 * This routine is invoked to enable the MSI-X interrupt vectors to device
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009598 * with SLI-3 interface specs.
James Smart3772a992009-05-22 14:50:54 -04009599 *
9600 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009601 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04009602 * other values - error
9603 **/
9604static int
9605lpfc_sli_enable_msix(struct lpfc_hba *phba)
9606{
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009607 int rc;
James Smart3772a992009-05-22 14:50:54 -04009608 LPFC_MBOXQ_t *pmb;
9609
9610 /* Set up MSI-X multi-message vectors */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009611 rc = pci_alloc_irq_vectors(phba->pcidev,
9612 LPFC_MSIX_VECTORS, LPFC_MSIX_VECTORS, PCI_IRQ_MSIX);
9613 if (rc < 0) {
James Smart3772a992009-05-22 14:50:54 -04009614 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9615 "0420 PCI enable MSI-X failed (%d)\n", rc);
Alexander Gordeev029165a2014-07-16 20:05:15 +02009616 goto vec_fail_out;
James Smart3772a992009-05-22 14:50:54 -04009617 }
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009618
James Smart3772a992009-05-22 14:50:54 -04009619 /*
9620 * Assign MSI-X vectors to interrupt handlers
9621 */
9622
9623 /* vector-0 is associated to slow-path handler */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009624 rc = request_irq(pci_irq_vector(phba->pcidev, 0),
James Smarted243d32015-05-21 13:55:25 -04009625 &lpfc_sli_sp_intr_handler, 0,
James Smart3772a992009-05-22 14:50:54 -04009626 LPFC_SP_DRIVER_HANDLER_NAME, phba);
9627 if (rc) {
9628 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9629 "0421 MSI-X slow-path request_irq failed "
9630 "(%d)\n", rc);
9631 goto msi_fail_out;
9632 }
9633
9634 /* vector-1 is associated to fast-path handler */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009635 rc = request_irq(pci_irq_vector(phba->pcidev, 1),
James Smarted243d32015-05-21 13:55:25 -04009636 &lpfc_sli_fp_intr_handler, 0,
James Smart3772a992009-05-22 14:50:54 -04009637 LPFC_FP_DRIVER_HANDLER_NAME, phba);
9638
9639 if (rc) {
9640 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9641 "0429 MSI-X fast-path request_irq failed "
9642 "(%d)\n", rc);
9643 goto irq_fail_out;
9644 }
9645
9646 /*
9647 * Configure HBA MSI-X attention conditions to messages
9648 */
9649 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9650
9651 if (!pmb) {
9652 rc = -ENOMEM;
9653 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9654 "0474 Unable to allocate memory for issuing "
9655 "MBOX_CONFIG_MSI command\n");
9656 goto mem_fail_out;
9657 }
9658 rc = lpfc_config_msi(phba, pmb);
9659 if (rc)
9660 goto mbx_fail_out;
9661 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
9662 if (rc != MBX_SUCCESS) {
9663 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
9664 "0351 Config MSI mailbox command failed, "
9665 "mbxCmd x%x, mbxStatus x%x\n",
9666 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
9667 goto mbx_fail_out;
9668 }
9669
9670 /* Free memory allocated for mailbox command */
9671 mempool_free(pmb, phba->mbox_mem_pool);
9672 return rc;
9673
9674mbx_fail_out:
9675 /* Free memory allocated for mailbox command */
9676 mempool_free(pmb, phba->mbox_mem_pool);
9677
9678mem_fail_out:
9679 /* free the irq already requested */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009680 free_irq(pci_irq_vector(phba->pcidev, 1), phba);
James Smart3772a992009-05-22 14:50:54 -04009681
9682irq_fail_out:
9683 /* free the irq already requested */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009684 free_irq(pci_irq_vector(phba->pcidev, 0), phba);
James Smart3772a992009-05-22 14:50:54 -04009685
9686msi_fail_out:
9687 /* Unconfigure MSI-X capability structure */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009688 pci_free_irq_vectors(phba->pcidev);
Alexander Gordeev029165a2014-07-16 20:05:15 +02009689
9690vec_fail_out:
James Smart3772a992009-05-22 14:50:54 -04009691 return rc;
9692}
9693
9694/**
James Smart3772a992009-05-22 14:50:54 -04009695 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
9696 * @phba: pointer to lpfc hba data structure.
9697 *
9698 * This routine is invoked to enable the MSI interrupt mode to device with
9699 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
9700 * enable the MSI vector. The device driver is responsible for calling the
9701 * request_irq() to register MSI vector with a interrupt the handler, which
9702 * is done in this function.
9703 *
9704 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009705 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04009706 * other values - error
9707 */
9708static int
9709lpfc_sli_enable_msi(struct lpfc_hba *phba)
9710{
9711 int rc;
9712
9713 rc = pci_enable_msi(phba->pcidev);
9714 if (!rc)
9715 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9716 "0462 PCI enable MSI mode success.\n");
9717 else {
9718 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9719 "0471 PCI enable MSI mode failed (%d)\n", rc);
9720 return rc;
9721 }
9722
9723 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
James Smarted243d32015-05-21 13:55:25 -04009724 0, LPFC_DRIVER_NAME, phba);
James Smart3772a992009-05-22 14:50:54 -04009725 if (rc) {
9726 pci_disable_msi(phba->pcidev);
9727 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9728 "0478 MSI request_irq failed (%d)\n", rc);
9729 }
9730 return rc;
9731}
9732
9733/**
James Smart3772a992009-05-22 14:50:54 -04009734 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
9735 * @phba: pointer to lpfc hba data structure.
9736 *
9737 * This routine is invoked to enable device interrupt and associate driver's
9738 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
9739 * spec. Depends on the interrupt mode configured to the driver, the driver
9740 * will try to fallback from the configured interrupt mode to an interrupt
9741 * mode which is supported by the platform, kernel, and device in the order
9742 * of:
9743 * MSI-X -> MSI -> IRQ.
9744 *
9745 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009746 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04009747 * other values - error
9748 **/
9749static uint32_t
9750lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
9751{
9752 uint32_t intr_mode = LPFC_INTR_ERROR;
9753 int retval;
9754
9755 if (cfg_mode == 2) {
9756 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
9757 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
9758 if (!retval) {
9759 /* Now, try to enable MSI-X interrupt mode */
9760 retval = lpfc_sli_enable_msix(phba);
9761 if (!retval) {
9762 /* Indicate initialization to MSI-X mode */
9763 phba->intr_type = MSIX;
9764 intr_mode = 2;
9765 }
9766 }
9767 }
9768
9769 /* Fallback to MSI if MSI-X initialization failed */
9770 if (cfg_mode >= 1 && phba->intr_type == NONE) {
9771 retval = lpfc_sli_enable_msi(phba);
9772 if (!retval) {
9773 /* Indicate initialization to MSI mode */
9774 phba->intr_type = MSI;
9775 intr_mode = 1;
9776 }
9777 }
9778
9779 /* Fallback to INTx if both MSI-X/MSI initalization failed */
9780 if (phba->intr_type == NONE) {
9781 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
9782 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
9783 if (!retval) {
9784 /* Indicate initialization to INTx mode */
9785 phba->intr_type = INTx;
9786 intr_mode = 0;
9787 }
9788 }
9789 return intr_mode;
9790}
9791
9792/**
9793 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
9794 * @phba: pointer to lpfc hba data structure.
9795 *
9796 * This routine is invoked to disable device interrupt and disassociate the
9797 * driver's interrupt handler(s) from interrupt vector(s) to device with
9798 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
9799 * release the interrupt vector(s) for the message signaled interrupt.
9800 **/
9801static void
9802lpfc_sli_disable_intr(struct lpfc_hba *phba)
9803{
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009804 int nr_irqs, i;
9805
James Smart3772a992009-05-22 14:50:54 -04009806 if (phba->intr_type == MSIX)
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009807 nr_irqs = LPFC_MSIX_VECTORS;
9808 else
9809 nr_irqs = 1;
9810
9811 for (i = 0; i < nr_irqs; i++)
9812 free_irq(pci_irq_vector(phba->pcidev, i), phba);
9813 pci_free_irq_vectors(phba->pcidev);
James Smart3772a992009-05-22 14:50:54 -04009814
9815 /* Reset interrupt management states */
9816 phba->intr_type = NONE;
9817 phba->sli.slistat.sli_intr = 0;
James Smart3772a992009-05-22 14:50:54 -04009818}
9819
9820/**
James Smart895427b2017-02-12 13:52:30 -08009821 * lpfc_cpu_affinity_check - Check vector CPU affinity mappings
James Smart7bb03bb2013-04-17 20:19:16 -04009822 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -08009823 * @vectors: number of msix vectors allocated.
James Smart7bb03bb2013-04-17 20:19:16 -04009824 *
James Smart895427b2017-02-12 13:52:30 -08009825 * The routine will figure out the CPU affinity assignment for every
9826 * MSI-X vector allocated for the HBA. The hba_eq_hdl will be updated
9827 * with a pointer to the CPU mask that defines ALL the CPUs this vector
9828 * can be associated with. If the vector can be unquely associated with
9829 * a single CPU, that CPU will be recorded in hba_eq_hdl[index].cpu.
9830 * In addition, the CPU to IO channel mapping will be calculated
9831 * and the phba->sli4_hba.cpu_map array will reflect this.
James Smart7bb03bb2013-04-17 20:19:16 -04009832 */
James Smart895427b2017-02-12 13:52:30 -08009833static void
9834lpfc_cpu_affinity_check(struct lpfc_hba *phba, int vectors)
James Smart7bb03bb2013-04-17 20:19:16 -04009835{
9836 struct lpfc_vector_map_info *cpup;
James Smart895427b2017-02-12 13:52:30 -08009837 int index = 0;
9838 int vec = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04009839 int cpu;
James Smart7bb03bb2013-04-17 20:19:16 -04009840#ifdef CONFIG_X86
9841 struct cpuinfo_x86 *cpuinfo;
9842#endif
James Smart7bb03bb2013-04-17 20:19:16 -04009843
9844 /* Init cpu_map array */
9845 memset(phba->sli4_hba.cpu_map, 0xff,
9846 (sizeof(struct lpfc_vector_map_info) *
James Smart895427b2017-02-12 13:52:30 -08009847 phba->sli4_hba.num_present_cpu));
James Smart7bb03bb2013-04-17 20:19:16 -04009848
9849 /* Update CPU map with physical id and core id of each CPU */
9850 cpup = phba->sli4_hba.cpu_map;
9851 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
9852#ifdef CONFIG_X86
9853 cpuinfo = &cpu_data(cpu);
9854 cpup->phys_id = cpuinfo->phys_proc_id;
9855 cpup->core_id = cpuinfo->cpu_core_id;
9856#else
9857 /* No distinction between CPUs for other platforms */
9858 cpup->phys_id = 0;
9859 cpup->core_id = 0;
9860#endif
James Smart895427b2017-02-12 13:52:30 -08009861 cpup->channel_id = index; /* For now round robin */
9862 cpup->irq = pci_irq_vector(phba->pcidev, vec);
9863 vec++;
9864 if (vec >= vectors)
9865 vec = 0;
9866 index++;
9867 if (index >= phba->cfg_fcp_io_channel)
9868 index = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04009869 cpup++;
9870 }
James Smart7bb03bb2013-04-17 20:19:16 -04009871}
9872
9873
9874/**
James Smartda0436e2009-05-22 14:51:39 -04009875 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
9876 * @phba: pointer to lpfc hba data structure.
9877 *
9878 * This routine is invoked to enable the MSI-X interrupt vectors to device
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009879 * with SLI-4 interface spec.
James Smartda0436e2009-05-22 14:51:39 -04009880 *
9881 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009882 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04009883 * other values - error
9884 **/
9885static int
9886lpfc_sli4_enable_msix(struct lpfc_hba *phba)
9887{
James Smart75baf692010-06-08 18:31:21 -04009888 int vectors, rc, index;
James Smartb83d0052017-06-01 21:07:05 -07009889 char *name;
James Smartda0436e2009-05-22 14:51:39 -04009890
9891 /* Set up MSI-X multi-message vectors */
James Smart895427b2017-02-12 13:52:30 -08009892 vectors = phba->io_channel_irqs;
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009893 if (phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05009894 vectors++;
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009895
James Smartf358dd02017-02-12 13:52:34 -08009896 rc = pci_alloc_irq_vectors(phba->pcidev,
9897 (phba->nvmet_support) ? 1 : 2,
9898 vectors, PCI_IRQ_MSIX | PCI_IRQ_AFFINITY);
Alexander Gordeev4f871e12014-09-03 12:56:29 -04009899 if (rc < 0) {
James Smartda0436e2009-05-22 14:51:39 -04009900 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9901 "0484 PCI enable MSI-X failed (%d)\n", rc);
Alexander Gordeev029165a2014-07-16 20:05:15 +02009902 goto vec_fail_out;
James Smartda0436e2009-05-22 14:51:39 -04009903 }
Alexander Gordeev4f871e12014-09-03 12:56:29 -04009904 vectors = rc;
James Smart75baf692010-06-08 18:31:21 -04009905
James Smart7bb03bb2013-04-17 20:19:16 -04009906 /* Assign MSI-X vectors to interrupt handlers */
James Smart67d12732012-08-03 12:36:13 -04009907 for (index = 0; index < vectors; index++) {
James Smartb83d0052017-06-01 21:07:05 -07009908 name = phba->sli4_hba.hba_eq_hdl[index].handler_name;
9909 memset(name, 0, LPFC_SLI4_HANDLER_NAME_SZ);
9910 snprintf(name, LPFC_SLI4_HANDLER_NAME_SZ,
James Smart4305f182012-08-03 12:36:33 -04009911 LPFC_DRIVER_HANDLER_NAME"%d", index);
James Smartda0436e2009-05-22 14:51:39 -04009912
James Smart895427b2017-02-12 13:52:30 -08009913 phba->sli4_hba.hba_eq_hdl[index].idx = index;
9914 phba->sli4_hba.hba_eq_hdl[index].phba = phba;
9915 atomic_set(&phba->sli4_hba.hba_eq_hdl[index].hba_eq_in_use, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009916 if (phba->cfg_fof && (index == (vectors - 1)))
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009917 rc = request_irq(pci_irq_vector(phba->pcidev, index),
James Smarted243d32015-05-21 13:55:25 -04009918 &lpfc_sli4_fof_intr_handler, 0,
James Smartb83d0052017-06-01 21:07:05 -07009919 name,
James Smart895427b2017-02-12 13:52:30 -08009920 &phba->sli4_hba.hba_eq_hdl[index]);
James Smart1ba981f2014-02-20 09:56:45 -05009921 else
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009922 rc = request_irq(pci_irq_vector(phba->pcidev, index),
James Smarted243d32015-05-21 13:55:25 -04009923 &lpfc_sli4_hba_intr_handler, 0,
James Smartb83d0052017-06-01 21:07:05 -07009924 name,
James Smart895427b2017-02-12 13:52:30 -08009925 &phba->sli4_hba.hba_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -04009926 if (rc) {
9927 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9928 "0486 MSI-X fast-path (%d) "
9929 "request_irq failed (%d)\n", index, rc);
9930 goto cfg_fail_out;
9931 }
9932 }
9933
James Smart1ba981f2014-02-20 09:56:45 -05009934 if (phba->cfg_fof)
9935 vectors--;
9936
James Smart895427b2017-02-12 13:52:30 -08009937 if (vectors != phba->io_channel_irqs) {
James Smart82c3e9b2012-09-29 11:29:50 -04009938 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9939 "3238 Reducing IO channels to match number of "
9940 "MSI-X vectors, requested %d got %d\n",
James Smart895427b2017-02-12 13:52:30 -08009941 phba->io_channel_irqs, vectors);
9942 if (phba->cfg_fcp_io_channel > vectors)
9943 phba->cfg_fcp_io_channel = vectors;
9944 if (phba->cfg_nvme_io_channel > vectors)
9945 phba->cfg_nvme_io_channel = vectors;
9946 if (phba->cfg_fcp_io_channel > phba->cfg_nvme_io_channel)
9947 phba->io_channel_irqs = phba->cfg_fcp_io_channel;
9948 else
9949 phba->io_channel_irqs = phba->cfg_nvme_io_channel;
James Smart82c3e9b2012-09-29 11:29:50 -04009950 }
James Smart895427b2017-02-12 13:52:30 -08009951 lpfc_cpu_affinity_check(phba, vectors);
James Smart7bb03bb2013-04-17 20:19:16 -04009952
James Smartda0436e2009-05-22 14:51:39 -04009953 return rc;
9954
9955cfg_fail_out:
9956 /* free the irq already requested */
James Smart895427b2017-02-12 13:52:30 -08009957 for (--index; index >= 0; index--)
9958 free_irq(pci_irq_vector(phba->pcidev, index),
9959 &phba->sli4_hba.hba_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -04009960
James Smartda0436e2009-05-22 14:51:39 -04009961 /* Unconfigure MSI-X capability structure */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009962 pci_free_irq_vectors(phba->pcidev);
Alexander Gordeev029165a2014-07-16 20:05:15 +02009963
9964vec_fail_out:
James Smartda0436e2009-05-22 14:51:39 -04009965 return rc;
9966}
9967
9968/**
James Smartda0436e2009-05-22 14:51:39 -04009969 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
9970 * @phba: pointer to lpfc hba data structure.
9971 *
9972 * This routine is invoked to enable the MSI interrupt mode to device with
9973 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
9974 * to enable the MSI vector. The device driver is responsible for calling
9975 * the request_irq() to register MSI vector with a interrupt the handler,
9976 * which is done in this function.
9977 *
9978 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009979 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04009980 * other values - error
9981 **/
9982static int
9983lpfc_sli4_enable_msi(struct lpfc_hba *phba)
9984{
9985 int rc, index;
9986
9987 rc = pci_enable_msi(phba->pcidev);
9988 if (!rc)
9989 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9990 "0487 PCI enable MSI mode success.\n");
9991 else {
9992 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9993 "0488 PCI enable MSI mode failed (%d)\n", rc);
9994 return rc;
9995 }
9996
9997 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
James Smarted243d32015-05-21 13:55:25 -04009998 0, LPFC_DRIVER_NAME, phba);
James Smartda0436e2009-05-22 14:51:39 -04009999 if (rc) {
10000 pci_disable_msi(phba->pcidev);
10001 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10002 "0490 MSI request_irq failed (%d)\n", rc);
James Smart75baf692010-06-08 18:31:21 -040010003 return rc;
James Smartda0436e2009-05-22 14:51:39 -040010004 }
10005
James Smart895427b2017-02-12 13:52:30 -080010006 for (index = 0; index < phba->io_channel_irqs; index++) {
10007 phba->sli4_hba.hba_eq_hdl[index].idx = index;
10008 phba->sli4_hba.hba_eq_hdl[index].phba = phba;
James Smartda0436e2009-05-22 14:51:39 -040010009 }
10010
James Smart1ba981f2014-02-20 09:56:45 -050010011 if (phba->cfg_fof) {
James Smart895427b2017-02-12 13:52:30 -080010012 phba->sli4_hba.hba_eq_hdl[index].idx = index;
10013 phba->sli4_hba.hba_eq_hdl[index].phba = phba;
James Smart1ba981f2014-02-20 09:56:45 -050010014 }
James Smart75baf692010-06-08 18:31:21 -040010015 return 0;
James Smartda0436e2009-05-22 14:51:39 -040010016}
10017
10018/**
James Smartda0436e2009-05-22 14:51:39 -040010019 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
10020 * @phba: pointer to lpfc hba data structure.
10021 *
10022 * This routine is invoked to enable device interrupt and associate driver's
10023 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
10024 * interface spec. Depends on the interrupt mode configured to the driver,
10025 * the driver will try to fallback from the configured interrupt mode to an
10026 * interrupt mode which is supported by the platform, kernel, and device in
10027 * the order of:
10028 * MSI-X -> MSI -> IRQ.
10029 *
10030 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020010031 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -040010032 * other values - error
10033 **/
10034static uint32_t
10035lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
10036{
10037 uint32_t intr_mode = LPFC_INTR_ERROR;
James Smart895427b2017-02-12 13:52:30 -080010038 int retval, idx;
James Smartda0436e2009-05-22 14:51:39 -040010039
10040 if (cfg_mode == 2) {
10041 /* Preparation before conf_msi mbox cmd */
10042 retval = 0;
10043 if (!retval) {
10044 /* Now, try to enable MSI-X interrupt mode */
10045 retval = lpfc_sli4_enable_msix(phba);
10046 if (!retval) {
10047 /* Indicate initialization to MSI-X mode */
10048 phba->intr_type = MSIX;
10049 intr_mode = 2;
10050 }
10051 }
10052 }
10053
10054 /* Fallback to MSI if MSI-X initialization failed */
10055 if (cfg_mode >= 1 && phba->intr_type == NONE) {
10056 retval = lpfc_sli4_enable_msi(phba);
10057 if (!retval) {
10058 /* Indicate initialization to MSI mode */
10059 phba->intr_type = MSI;
10060 intr_mode = 1;
10061 }
10062 }
10063
10064 /* Fallback to INTx if both MSI-X/MSI initalization failed */
10065 if (phba->intr_type == NONE) {
10066 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
10067 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
10068 if (!retval) {
James Smart895427b2017-02-12 13:52:30 -080010069 struct lpfc_hba_eq_hdl *eqhdl;
10070
James Smartda0436e2009-05-22 14:51:39 -040010071 /* Indicate initialization to INTx mode */
10072 phba->intr_type = INTx;
10073 intr_mode = 0;
James Smart895427b2017-02-12 13:52:30 -080010074
10075 for (idx = 0; idx < phba->io_channel_irqs; idx++) {
10076 eqhdl = &phba->sli4_hba.hba_eq_hdl[idx];
10077 eqhdl->idx = idx;
10078 eqhdl->phba = phba;
10079 atomic_set(&eqhdl->hba_eq_in_use, 1);
James Smartda0436e2009-05-22 14:51:39 -040010080 }
James Smart1ba981f2014-02-20 09:56:45 -050010081 if (phba->cfg_fof) {
James Smart895427b2017-02-12 13:52:30 -080010082 eqhdl = &phba->sli4_hba.hba_eq_hdl[idx];
10083 eqhdl->idx = idx;
10084 eqhdl->phba = phba;
10085 atomic_set(&eqhdl->hba_eq_in_use, 1);
James Smart1ba981f2014-02-20 09:56:45 -050010086 }
James Smartda0436e2009-05-22 14:51:39 -040010087 }
10088 }
10089 return intr_mode;
10090}
10091
10092/**
10093 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
10094 * @phba: pointer to lpfc hba data structure.
10095 *
10096 * This routine is invoked to disable device interrupt and disassociate
10097 * the driver's interrupt handler(s) from interrupt vector(s) to device
10098 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
10099 * will release the interrupt vector(s) for the message signaled interrupt.
10100 **/
10101static void
10102lpfc_sli4_disable_intr(struct lpfc_hba *phba)
10103{
10104 /* Disable the currently initialized interrupt mode */
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010105 if (phba->intr_type == MSIX) {
10106 int index;
10107
10108 /* Free up MSI-X multi-message vectors */
James Smart895427b2017-02-12 13:52:30 -080010109 for (index = 0; index < phba->io_channel_irqs; index++)
10110 free_irq(pci_irq_vector(phba->pcidev, index),
10111 &phba->sli4_hba.hba_eq_hdl[index]);
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010112
10113 if (phba->cfg_fof)
James Smart895427b2017-02-12 13:52:30 -080010114 free_irq(pci_irq_vector(phba->pcidev, index),
10115 &phba->sli4_hba.hba_eq_hdl[index]);
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010116 } else {
James Smartda0436e2009-05-22 14:51:39 -040010117 free_irq(phba->pcidev->irq, phba);
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010118 }
10119
10120 pci_free_irq_vectors(phba->pcidev);
James Smartda0436e2009-05-22 14:51:39 -040010121
10122 /* Reset interrupt management states */
10123 phba->intr_type = NONE;
10124 phba->sli.slistat.sli_intr = 0;
James Smartda0436e2009-05-22 14:51:39 -040010125}
10126
10127/**
James Smart3772a992009-05-22 14:50:54 -040010128 * lpfc_unset_hba - Unset SLI3 hba device initialization
10129 * @phba: pointer to lpfc hba data structure.
10130 *
10131 * This routine is invoked to unset the HBA device initialization steps to
10132 * a device with SLI-3 interface spec.
10133 **/
10134static void
10135lpfc_unset_hba(struct lpfc_hba *phba)
10136{
10137 struct lpfc_vport *vport = phba->pport;
10138 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
10139
10140 spin_lock_irq(shost->host_lock);
10141 vport->load_flag |= FC_UNLOADING;
10142 spin_unlock_irq(shost->host_lock);
10143
James Smart72859902012-01-18 16:25:38 -050010144 kfree(phba->vpi_bmask);
10145 kfree(phba->vpi_ids);
10146
James Smart3772a992009-05-22 14:50:54 -040010147 lpfc_stop_hba_timers(phba);
10148
10149 phba->pport->work_port_events = 0;
10150
10151 lpfc_sli_hba_down(phba);
10152
10153 lpfc_sli_brdrestart(phba);
10154
10155 lpfc_sli_disable_intr(phba);
10156
10157 return;
10158}
10159
10160/**
James Smart5af5eee2010-10-22 11:06:38 -040010161 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
10162 * @phba: Pointer to HBA context object.
10163 *
10164 * This function is called in the SLI4 code path to wait for completion
10165 * of device's XRIs exchange busy. It will check the XRI exchange busy
10166 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
10167 * that, it will check the XRI exchange busy on outstanding FCP and ELS
10168 * I/Os every 30 seconds, log error message, and wait forever. Only when
10169 * all XRI exchange busy complete, the driver unload shall proceed with
10170 * invoking the function reset ioctl mailbox command to the CNA and the
10171 * the rest of the driver unload resource release.
10172 **/
10173static void
10174lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
10175{
10176 int wait_time = 0;
James Smart895427b2017-02-12 13:52:30 -080010177 int nvme_xri_cmpl = 1;
James Smart86c67372017-04-21 16:05:04 -070010178 int nvmet_xri_cmpl = 1;
James Smart895427b2017-02-12 13:52:30 -080010179 int fcp_xri_cmpl = 1;
James Smart5af5eee2010-10-22 11:06:38 -040010180 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
10181
James Smartc3725bd2017-11-20 16:00:42 -080010182 /* Driver just aborted IOs during the hba_unset process. Pause
10183 * here to give the HBA time to complete the IO and get entries
10184 * into the abts lists.
10185 */
10186 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1 * 5);
10187
10188 /* Wait for NVME pending IO to flush back to transport. */
10189 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
10190 lpfc_nvme_wait_for_io_drain(phba);
10191
James Smart895427b2017-02-12 13:52:30 -080010192 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP)
10193 fcp_xri_cmpl =
10194 list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
James Smart86c67372017-04-21 16:05:04 -070010195 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart895427b2017-02-12 13:52:30 -080010196 nvme_xri_cmpl =
10197 list_empty(&phba->sli4_hba.lpfc_abts_nvme_buf_list);
James Smart86c67372017-04-21 16:05:04 -070010198 nvmet_xri_cmpl =
10199 list_empty(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
10200 }
James Smart895427b2017-02-12 13:52:30 -080010201
James Smartf358dd02017-02-12 13:52:34 -080010202 while (!fcp_xri_cmpl || !els_xri_cmpl || !nvme_xri_cmpl ||
10203 !nvmet_xri_cmpl) {
James Smart5af5eee2010-10-22 11:06:38 -040010204 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
James Smart895427b2017-02-12 13:52:30 -080010205 if (!nvme_xri_cmpl)
10206 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10207 "6100 NVME XRI exchange busy "
10208 "wait time: %d seconds.\n",
10209 wait_time/1000);
James Smart5af5eee2010-10-22 11:06:38 -040010210 if (!fcp_xri_cmpl)
10211 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10212 "2877 FCP XRI exchange busy "
10213 "wait time: %d seconds.\n",
10214 wait_time/1000);
10215 if (!els_xri_cmpl)
10216 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10217 "2878 ELS XRI exchange busy "
10218 "wait time: %d seconds.\n",
10219 wait_time/1000);
10220 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
10221 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
10222 } else {
10223 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
10224 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
10225 }
James Smart86c67372017-04-21 16:05:04 -070010226 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart895427b2017-02-12 13:52:30 -080010227 nvme_xri_cmpl = list_empty(
10228 &phba->sli4_hba.lpfc_abts_nvme_buf_list);
James Smart86c67372017-04-21 16:05:04 -070010229 nvmet_xri_cmpl = list_empty(
10230 &phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
10231 }
James Smart895427b2017-02-12 13:52:30 -080010232
10233 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP)
10234 fcp_xri_cmpl = list_empty(
10235 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
10236
James Smart5af5eee2010-10-22 11:06:38 -040010237 els_xri_cmpl =
10238 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smartf358dd02017-02-12 13:52:34 -080010239
James Smart5af5eee2010-10-22 11:06:38 -040010240 }
10241}
10242
10243/**
James Smartda0436e2009-05-22 14:51:39 -040010244 * lpfc_sli4_hba_unset - Unset the fcoe hba
10245 * @phba: Pointer to HBA context object.
10246 *
10247 * This function is called in the SLI4 code path to reset the HBA's FCoE
10248 * function. The caller is not required to hold any lock. This routine
10249 * issues PCI function reset mailbox command to reset the FCoE function.
10250 * At the end of the function, it calls lpfc_hba_down_post function to
10251 * free any pending commands.
10252 **/
10253static void
10254lpfc_sli4_hba_unset(struct lpfc_hba *phba)
10255{
10256 int wait_cnt = 0;
10257 LPFC_MBOXQ_t *mboxq;
James Smart912e3ac2011-05-24 11:42:11 -040010258 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -040010259
10260 lpfc_stop_hba_timers(phba);
10261 phba->sli4_hba.intr_enable = 0;
10262
10263 /*
10264 * Gracefully wait out the potential current outstanding asynchronous
10265 * mailbox command.
10266 */
10267
10268 /* First, block any pending async mailbox command from posted */
10269 spin_lock_irq(&phba->hbalock);
10270 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
10271 spin_unlock_irq(&phba->hbalock);
10272 /* Now, trying to wait it out if we can */
10273 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
10274 msleep(10);
10275 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
10276 break;
10277 }
10278 /* Forcefully release the outstanding mailbox command if timed out */
10279 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
10280 spin_lock_irq(&phba->hbalock);
10281 mboxq = phba->sli.mbox_active;
10282 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
10283 __lpfc_mbox_cmpl_put(phba, mboxq);
10284 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10285 phba->sli.mbox_active = NULL;
10286 spin_unlock_irq(&phba->hbalock);
10287 }
10288
James Smart5af5eee2010-10-22 11:06:38 -040010289 /* Abort all iocbs associated with the hba */
10290 lpfc_sli_hba_iocb_abort(phba);
10291
10292 /* Wait for completion of device XRI exchange busy */
10293 lpfc_sli4_xri_exchange_busy_wait(phba);
10294
James Smartda0436e2009-05-22 14:51:39 -040010295 /* Disable PCI subsystem interrupt */
10296 lpfc_sli4_disable_intr(phba);
10297
James Smart912e3ac2011-05-24 11:42:11 -040010298 /* Disable SR-IOV if enabled */
10299 if (phba->cfg_sriov_nr_virtfn)
10300 pci_disable_sriov(pdev);
10301
James Smartda0436e2009-05-22 14:51:39 -040010302 /* Stop kthread signal shall trigger work_done one more time */
10303 kthread_stop(phba->worker_thread);
10304
James Smartd1f525a2017-04-21 16:04:55 -070010305 /* Unset the queues shared with the hardware then release all
10306 * allocated resources.
10307 */
10308 lpfc_sli4_queue_unset(phba);
10309 lpfc_sli4_queue_destroy(phba);
10310
James Smart3677a3a2010-09-29 11:19:14 -040010311 /* Reset SLI4 HBA FCoE function */
10312 lpfc_pci_function_reset(phba);
10313
James Smartda0436e2009-05-22 14:51:39 -040010314 /* Stop the SLI4 device port */
10315 phba->pport->work_port_events = 0;
10316}
10317
James Smart28baac72010-02-12 14:42:03 -050010318 /**
10319 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
10320 * @phba: Pointer to HBA context object.
10321 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
10322 *
10323 * This function is called in the SLI4 code path to read the port's
10324 * sli4 capabilities.
10325 *
10326 * This function may be be called from any context that can block-wait
10327 * for the completion. The expectation is that this routine is called
10328 * typically from probe_one or from the online routine.
10329 **/
10330int
10331lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
10332{
10333 int rc;
10334 struct lpfc_mqe *mqe;
10335 struct lpfc_pc_sli4_params *sli4_params;
10336 uint32_t mbox_tmo;
10337
10338 rc = 0;
10339 mqe = &mboxq->u.mqe;
10340
10341 /* Read the port's SLI4 Parameters port capabilities */
James Smartfedd3b72011-02-16 12:39:24 -050010342 lpfc_pc_sli4_params(mboxq);
James Smart28baac72010-02-12 14:42:03 -050010343 if (!phba->sli4_hba.intr_enable)
10344 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
10345 else {
James Smarta183a152011-10-10 21:32:43 -040010346 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart28baac72010-02-12 14:42:03 -050010347 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
10348 }
10349
10350 if (unlikely(rc))
10351 return 1;
10352
10353 sli4_params = &phba->sli4_hba.pc_sli4_params;
10354 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
10355 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
10356 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
10357 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
10358 &mqe->un.sli4_params);
10359 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
10360 &mqe->un.sli4_params);
10361 sli4_params->proto_types = mqe->un.sli4_params.word3;
10362 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
10363 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
10364 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
10365 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
10366 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
10367 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
10368 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
10369 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
10370 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
10371 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
10372 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
10373 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
10374 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
10375 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
10376 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
10377 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
10378 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
10379 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
10380 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
10381 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
James Smart05580562011-05-24 11:40:48 -040010382
10383 /* Make sure that sge_supp_len can be handled by the driver */
10384 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
10385 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
10386
James Smart28baac72010-02-12 14:42:03 -050010387 return rc;
10388}
10389
James Smartda0436e2009-05-22 14:51:39 -040010390/**
James Smartfedd3b72011-02-16 12:39:24 -050010391 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
10392 * @phba: Pointer to HBA context object.
10393 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
10394 *
10395 * This function is called in the SLI4 code path to read the port's
10396 * sli4 capabilities.
10397 *
10398 * This function may be be called from any context that can block-wait
10399 * for the completion. The expectation is that this routine is called
10400 * typically from probe_one or from the online routine.
10401 **/
10402int
10403lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
10404{
10405 int rc;
10406 struct lpfc_mqe *mqe = &mboxq->u.mqe;
10407 struct lpfc_pc_sli4_params *sli4_params;
James Smarta183a152011-10-10 21:32:43 -040010408 uint32_t mbox_tmo;
James Smartfedd3b72011-02-16 12:39:24 -050010409 int length;
10410 struct lpfc_sli4_parameters *mbx_sli4_parameters;
10411
James Smart6d368e52011-05-24 11:44:12 -040010412 /*
10413 * By default, the driver assumes the SLI4 port requires RPI
10414 * header postings. The SLI4_PARAM response will correct this
10415 * assumption.
10416 */
10417 phba->sli4_hba.rpi_hdrs_in_use = 1;
10418
James Smartfedd3b72011-02-16 12:39:24 -050010419 /* Read the port's SLI4 Config Parameters */
10420 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
10421 sizeof(struct lpfc_sli4_cfg_mhdr));
10422 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
10423 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
10424 length, LPFC_SLI4_MBX_EMBED);
10425 if (!phba->sli4_hba.intr_enable)
10426 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -040010427 else {
10428 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
10429 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
10430 }
James Smartfedd3b72011-02-16 12:39:24 -050010431 if (unlikely(rc))
10432 return rc;
10433 sli4_params = &phba->sli4_hba.pc_sli4_params;
10434 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
10435 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
10436 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
10437 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
10438 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
10439 mbx_sli4_parameters);
10440 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
10441 mbx_sli4_parameters);
10442 if (bf_get(cfg_phwq, mbx_sli4_parameters))
10443 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
10444 else
10445 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
10446 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
10447 sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
James Smart1ba981f2014-02-20 09:56:45 -050010448 sli4_params->oas_supported = bf_get(cfg_oas, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -050010449 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
10450 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
10451 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
10452 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
James Smart0c651872013-07-15 18:33:23 -040010453 sli4_params->wqsize = bf_get(cfg_wqsize, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -050010454 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
10455 mbx_sli4_parameters);
James Smart895427b2017-02-12 13:52:30 -080010456 sli4_params->wqpcnt = bf_get(cfg_wqpcnt, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -050010457 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
10458 mbx_sli4_parameters);
James Smart6d368e52011-05-24 11:44:12 -040010459 phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
10460 phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
James Smart895427b2017-02-12 13:52:30 -080010461 phba->nvme_support = (bf_get(cfg_nvme, mbx_sli4_parameters) &&
10462 bf_get(cfg_xib, mbx_sli4_parameters));
10463
10464 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP) ||
10465 !phba->nvme_support) {
10466 phba->nvme_support = 0;
10467 phba->nvmet_support = 0;
James Smartbcb24f62017-11-20 16:00:36 -080010468 phba->cfg_nvmet_mrq = LPFC_NVMET_MRQ_OFF;
James Smart895427b2017-02-12 13:52:30 -080010469 phba->cfg_nvme_io_channel = 0;
10470 phba->io_channel_irqs = phba->cfg_fcp_io_channel;
10471 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_NVME,
10472 "6101 Disabling NVME support: "
10473 "Not supported by firmware: %d %d\n",
10474 bf_get(cfg_nvme, mbx_sli4_parameters),
10475 bf_get(cfg_xib, mbx_sli4_parameters));
10476
10477 /* If firmware doesn't support NVME, just use SCSI support */
10478 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
10479 return -ENODEV;
10480 phba->cfg_enable_fc4_type = LPFC_ENABLE_FCP;
10481 }
James Smart05580562011-05-24 11:40:48 -040010482
James Smart20aefac2018-01-30 15:58:58 -080010483 /*
10484 * To support Suppress Response feature we must satisfy 3 conditions.
10485 * lpfc_suppress_rsp module parameter must be set (default).
10486 * In SLI4-Parameters Descriptor:
10487 * Extended Inline Buffers (XIB) must be supported.
10488 * Suppress Response IU Not Supported (SRIUNS) must NOT be supported
10489 * (double negative).
10490 */
10491 if (phba->cfg_suppress_rsp && bf_get(cfg_xib, mbx_sli4_parameters) &&
10492 !(bf_get(cfg_nosr, mbx_sli4_parameters)))
James Smartf358dd02017-02-12 13:52:34 -080010493 phba->sli.sli_flag |= LPFC_SLI_SUPPRESS_RSP;
James Smart20aefac2018-01-30 15:58:58 -080010494 else
10495 phba->cfg_suppress_rsp = 0;
James Smartf358dd02017-02-12 13:52:34 -080010496
James Smart0cf07f842017-06-01 21:07:10 -070010497 if (bf_get(cfg_eqdr, mbx_sli4_parameters))
10498 phba->sli.sli_flag |= LPFC_SLI_USE_EQDR;
10499
James Smart05580562011-05-24 11:40:48 -040010500 /* Make sure that sge_supp_len can be handled by the driver */
10501 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
10502 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
10503
James Smartb5c53952016-03-31 14:12:30 -070010504 /*
James Smartc176ffa2018-01-30 15:58:46 -080010505 * Check whether the adapter supports an embedded copy of the
10506 * FCP CMD IU within the WQE for FCP_Ixxx commands. In order
10507 * to use this option, 128-byte WQEs must be used.
James Smartb5c53952016-03-31 14:12:30 -070010508 */
10509 if (bf_get(cfg_ext_embed_cb, mbx_sli4_parameters))
10510 phba->fcp_embed_io = 1;
10511 else
10512 phba->fcp_embed_io = 0;
James Smart7bdedb32016-07-06 12:36:00 -070010513
James Smartc176ffa2018-01-30 15:58:46 -080010514 if ((bf_get(cfg_cqpsize, mbx_sli4_parameters) & LPFC_CQ_16K_PAGE_SZ) &&
10515 (bf_get(cfg_wqpsize, mbx_sli4_parameters) & LPFC_WQ_16K_PAGE_SZ) &&
10516 (sli4_params->wqsize & LPFC_WQ_SZ128_SUPPORT))
10517 phba->enab_exp_wqcq_pages = 1;
10518 else
10519 phba->enab_exp_wqcq_pages = 0;
James Smart7bdedb32016-07-06 12:36:00 -070010520 /*
10521 * Check if the SLI port supports MDS Diagnostics
10522 */
10523 if (bf_get(cfg_mds_diags, mbx_sli4_parameters))
10524 phba->mds_diags_support = 1;
10525 else
10526 phba->mds_diags_support = 0;
James Smartfedd3b72011-02-16 12:39:24 -050010527 return 0;
10528}
10529
10530/**
James Smart3772a992009-05-22 14:50:54 -040010531 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
10532 * @pdev: pointer to PCI device
10533 * @pid: pointer to PCI device identifier
10534 *
10535 * This routine is to be called to attach a device with SLI-3 interface spec
10536 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
10537 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
10538 * information of the device and driver to see if the driver state that it can
10539 * support this kind of device. If the match is successful, the driver core
10540 * invokes this routine. If this routine determines it can claim the HBA, it
10541 * does all the initialization that it needs to do to handle the HBA properly.
10542 *
10543 * Return code
10544 * 0 - driver can claim the device
10545 * negative value - driver can not claim the device
10546 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010547static int
James Smart3772a992009-05-22 14:50:54 -040010548lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
10549{
10550 struct lpfc_hba *phba;
10551 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -040010552 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -040010553 int error;
10554 uint32_t cfg_mode, intr_mode;
10555
10556 /* Allocate memory for HBA structure */
10557 phba = lpfc_hba_alloc(pdev);
10558 if (!phba)
10559 return -ENOMEM;
10560
10561 /* Perform generic PCI device enabling operation */
10562 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -040010563 if (error)
James Smart3772a992009-05-22 14:50:54 -040010564 goto out_free_phba;
James Smart3772a992009-05-22 14:50:54 -040010565
10566 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
10567 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
10568 if (error)
10569 goto out_disable_pci_dev;
10570
10571 /* Set up SLI-3 specific device PCI memory space */
10572 error = lpfc_sli_pci_mem_setup(phba);
10573 if (error) {
10574 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10575 "1402 Failed to set up pci memory space.\n");
10576 goto out_disable_pci_dev;
10577 }
10578
James Smart3772a992009-05-22 14:50:54 -040010579 /* Set up SLI-3 specific device driver resources */
10580 error = lpfc_sli_driver_resource_setup(phba);
10581 if (error) {
10582 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10583 "1404 Failed to set up driver resource.\n");
10584 goto out_unset_pci_mem_s3;
10585 }
10586
10587 /* Initialize and populate the iocb list per host */
James Smartd1f525a2017-04-21 16:04:55 -070010588
James Smart3772a992009-05-22 14:50:54 -040010589 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
10590 if (error) {
10591 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10592 "1405 Failed to initialize iocb list.\n");
10593 goto out_unset_driver_resource_s3;
10594 }
10595
10596 /* Set up common device driver resources */
10597 error = lpfc_setup_driver_resource_phase2(phba);
10598 if (error) {
10599 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10600 "1406 Failed to set up driver resource.\n");
10601 goto out_free_iocb_list;
10602 }
10603
James Smart079b5c92011-08-21 21:48:49 -040010604 /* Get the default values for Model Name and Description */
10605 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
10606
James Smart3772a992009-05-22 14:50:54 -040010607 /* Create SCSI host to the physical port */
10608 error = lpfc_create_shost(phba);
10609 if (error) {
10610 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10611 "1407 Failed to create scsi host.\n");
10612 goto out_unset_driver_resource;
10613 }
10614
10615 /* Configure sysfs attributes */
10616 vport = phba->pport;
10617 error = lpfc_alloc_sysfs_attr(vport);
10618 if (error) {
10619 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10620 "1476 Failed to allocate sysfs attr\n");
10621 goto out_destroy_shost;
10622 }
10623
James Smart6669f9b2009-10-02 15:16:45 -040010624 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -040010625 /* Now, trying to enable interrupt and bring up the device */
10626 cfg_mode = phba->cfg_use_msi;
10627 while (true) {
10628 /* Put device to a known state before enabling interrupt */
10629 lpfc_stop_port(phba);
10630 /* Configure and enable interrupt */
10631 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
10632 if (intr_mode == LPFC_INTR_ERROR) {
10633 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10634 "0431 Failed to enable interrupt.\n");
10635 error = -ENODEV;
10636 goto out_free_sysfs_attr;
10637 }
10638 /* SLI-3 HBA setup */
10639 if (lpfc_sli_hba_setup(phba)) {
10640 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10641 "1477 Failed to set up hba\n");
10642 error = -ENODEV;
10643 goto out_remove_device;
10644 }
10645
10646 /* Wait 50ms for the interrupts of previous mailbox commands */
10647 msleep(50);
10648 /* Check active interrupts on message signaled interrupts */
10649 if (intr_mode == 0 ||
10650 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
10651 /* Log the current active interrupt mode */
10652 phba->intr_mode = intr_mode;
10653 lpfc_log_intr_mode(phba, intr_mode);
10654 break;
10655 } else {
10656 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10657 "0447 Configure interrupt mode (%d) "
10658 "failed active interrupt test.\n",
10659 intr_mode);
10660 /* Disable the current interrupt mode */
10661 lpfc_sli_disable_intr(phba);
10662 /* Try next level of interrupt mode */
10663 cfg_mode = --intr_mode;
10664 }
10665 }
10666
10667 /* Perform post initialization setup */
10668 lpfc_post_init_setup(phba);
10669
10670 /* Check if there are static vports to be created. */
10671 lpfc_create_static_vport(phba);
10672
10673 return 0;
10674
10675out_remove_device:
10676 lpfc_unset_hba(phba);
10677out_free_sysfs_attr:
10678 lpfc_free_sysfs_attr(vport);
10679out_destroy_shost:
10680 lpfc_destroy_shost(phba);
10681out_unset_driver_resource:
10682 lpfc_unset_driver_resource_phase2(phba);
10683out_free_iocb_list:
10684 lpfc_free_iocb_list(phba);
10685out_unset_driver_resource_s3:
10686 lpfc_sli_driver_resource_unset(phba);
10687out_unset_pci_mem_s3:
10688 lpfc_sli_pci_mem_unset(phba);
10689out_disable_pci_dev:
10690 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040010691 if (shost)
10692 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -040010693out_free_phba:
10694 lpfc_hba_free(phba);
10695 return error;
10696}
10697
10698/**
10699 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -040010700 * @pdev: pointer to PCI device
10701 *
James Smart3772a992009-05-22 14:50:54 -040010702 * This routine is to be called to disattach a device with SLI-3 interface
10703 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
10704 * removed from PCI bus, it performs all the necessary cleanup for the HBA
10705 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -040010706 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010707static void
James Smart3772a992009-05-22 14:50:54 -040010708lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -050010709{
James Smart2e0fef82007-06-17 19:56:36 -050010710 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10711 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -050010712 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -050010713 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -050010714 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -050010715
James Smart549e55c2007-08-02 11:09:51 -040010716 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -040010717 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -040010718 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010719
James Smart858c9f62007-06-17 19:56:39 -050010720 lpfc_free_sysfs_attr(vport);
10721
James Smarteada2722008-12-04 22:39:13 -050010722 /* Release all the vports against this physical port */
10723 vports = lpfc_create_vport_work_array(phba);
10724 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040010725 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
10726 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
10727 continue;
James Smarteada2722008-12-04 22:39:13 -050010728 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040010729 }
James Smarteada2722008-12-04 22:39:13 -050010730 lpfc_destroy_vport_work_array(phba, vports);
10731
10732 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -050010733 fc_remove_host(shost);
10734 scsi_remove_host(shost);
James Smartd613b6a2017-02-12 13:52:37 -080010735
James Smart87af33f2007-10-27 13:37:43 -040010736 lpfc_cleanup(vport);
10737
James Smart2e0fef82007-06-17 19:56:36 -050010738 /*
10739 * Bring down the SLI Layer. This step disable all interrupts,
10740 * clears the rings, discards all mailbox commands, and resets
10741 * the HBA.
10742 */
James Smarta257bf92009-04-06 18:48:10 -040010743
Justin P. Mattock48e34d02010-12-30 15:07:58 -080010744 /* HBA interrupt will be disabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -050010745 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -040010746 /* Stop kthread signal shall trigger work_done one more time */
10747 kthread_stop(phba->worker_thread);
10748 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -050010749 lpfc_sli_brdrestart(phba);
10750
James Smart72859902012-01-18 16:25:38 -050010751 kfree(phba->vpi_bmask);
10752 kfree(phba->vpi_ids);
10753
James Smart3772a992009-05-22 14:50:54 -040010754 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -050010755 spin_lock_irq(&phba->hbalock);
10756 list_del_init(&vport->listentry);
10757 spin_unlock_irq(&phba->hbalock);
10758
James Smart858c9f62007-06-17 19:56:39 -050010759 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -050010760
James Smart912e3ac2011-05-24 11:42:11 -040010761 /* Disable SR-IOV if enabled */
10762 if (phba->cfg_sriov_nr_virtfn)
10763 pci_disable_sriov(pdev);
10764
James Smart5b75da22008-12-04 22:39:35 -050010765 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -040010766 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -050010767
James Smart858c9f62007-06-17 19:56:39 -050010768 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -050010769
10770 /*
10771 * Call scsi_free before mem_free since scsi bufs are released to their
10772 * corresponding pools here.
10773 */
10774 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -040010775 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -050010776
James Smart34b02dc2008-08-24 21:49:55 -040010777 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
10778 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -050010779
James Smart2e0fef82007-06-17 19:56:36 -050010780 /* Free resources associated with SLI2 interface */
10781 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -040010782 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -050010783
10784 /* unmap adapter SLIM and Control Registers */
10785 iounmap(phba->ctrl_regs_memmap_p);
10786 iounmap(phba->slim_memmap_p);
10787
James Smart3772a992009-05-22 14:50:54 -040010788 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -050010789
Johannes Thumshirne0c04832016-06-07 09:44:03 +020010790 pci_release_mem_regions(pdev);
James Smart2e0fef82007-06-17 19:56:36 -050010791 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -050010792}
10793
Linas Vepstas8d63f372007-02-14 14:28:36 -060010794/**
James Smart3772a992009-05-22 14:50:54 -040010795 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -050010796 * @pdev: pointer to PCI device
10797 * @msg: power management message
10798 *
James Smart3772a992009-05-22 14:50:54 -040010799 * This routine is to be called from the kernel's PCI subsystem to support
10800 * system Power Management (PM) to device with SLI-3 interface spec. When
10801 * PM invokes this method, it quiesces the device by stopping the driver's
10802 * worker thread for the device, turning off device's interrupt and DMA,
10803 * and bring the device offline. Note that as the driver implements the
10804 * minimum PM requirements to a power-aware driver's PM support for the
10805 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
10806 * to the suspend() method call will be treated as SUSPEND and the driver will
10807 * fully reinitialize its device during resume() method call, the driver will
10808 * set device to PCI_D3hot state in PCI config space instead of setting it
10809 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -050010810 *
10811 * Return code
James Smart3772a992009-05-22 14:50:54 -040010812 * 0 - driver suspended the device
10813 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -050010814 **/
10815static int
James Smart3772a992009-05-22 14:50:54 -040010816lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -050010817{
10818 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10819 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10820
10821 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10822 "0473 PCI device Power Management suspend.\n");
10823
10824 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040010825 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart3a55b532008-12-04 22:38:54 -050010826 lpfc_offline(phba);
10827 kthread_stop(phba->worker_thread);
10828
10829 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -040010830 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -050010831
10832 /* Save device state to PCI config space */
10833 pci_save_state(pdev);
10834 pci_set_power_state(pdev, PCI_D3hot);
10835
10836 return 0;
10837}
10838
10839/**
James Smart3772a992009-05-22 14:50:54 -040010840 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -050010841 * @pdev: pointer to PCI device
10842 *
James Smart3772a992009-05-22 14:50:54 -040010843 * This routine is to be called from the kernel's PCI subsystem to support
10844 * system Power Management (PM) to device with SLI-3 interface spec. When PM
10845 * invokes this method, it restores the device's PCI config space state and
10846 * fully reinitializes the device and brings it online. Note that as the
10847 * driver implements the minimum PM requirements to a power-aware driver's
10848 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
10849 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
10850 * driver will fully reinitialize its device during resume() method call,
10851 * the device will be set to PCI_D0 directly in PCI config space before
10852 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -050010853 *
10854 * Return code
James Smart3772a992009-05-22 14:50:54 -040010855 * 0 - driver suspended the device
10856 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -050010857 **/
10858static int
James Smart3772a992009-05-22 14:50:54 -040010859lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -050010860{
10861 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10862 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -050010863 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -050010864 int error;
10865
10866 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10867 "0452 PCI device Power Management resume.\n");
10868
10869 /* Restore device state from PCI config space */
10870 pci_set_power_state(pdev, PCI_D0);
10871 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -040010872
James Smart1dfb5a42010-02-12 14:40:50 -050010873 /*
10874 * As the new kernel behavior of pci_restore_state() API call clears
10875 * device saved_state flag, need to save the restored state again.
10876 */
10877 pci_save_state(pdev);
10878
James Smart3a55b532008-12-04 22:38:54 -050010879 if (pdev->is_busmaster)
10880 pci_set_master(pdev);
10881
10882 /* Startup the kernel thread for this host adapter. */
10883 phba->worker_thread = kthread_run(lpfc_do_work, phba,
10884 "lpfc_worker_%d", phba->brd_no);
10885 if (IS_ERR(phba->worker_thread)) {
10886 error = PTR_ERR(phba->worker_thread);
10887 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10888 "0434 PM resume failed to start worker "
10889 "thread: error=x%x.\n", error);
10890 return error;
10891 }
10892
James Smart5b75da22008-12-04 22:39:35 -050010893 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -040010894 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -050010895 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -050010896 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -050010897 "0430 PM resume Failed to enable interrupt\n");
10898 return -EIO;
10899 } else
10900 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -050010901
10902 /* Restart HBA and bring it online */
10903 lpfc_sli_brdrestart(phba);
10904 lpfc_online(phba);
10905
James Smart5b75da22008-12-04 22:39:35 -050010906 /* Log the current active interrupt mode */
10907 lpfc_log_intr_mode(phba, phba->intr_mode);
10908
James Smart3a55b532008-12-04 22:38:54 -050010909 return 0;
10910}
10911
10912/**
James Smart891478a2009-11-18 15:40:23 -050010913 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
10914 * @phba: pointer to lpfc hba data structure.
10915 *
10916 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -040010917 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -050010918 **/
10919static void
10920lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
10921{
10922 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10923 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -040010924
10925 /*
10926 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
10927 * and let the SCSI mid-layer to retry them to recover.
10928 */
James Smartdb55fba2014-04-04 13:52:02 -040010929 lpfc_sli_abort_fcp_rings(phba);
James Smart891478a2009-11-18 15:40:23 -050010930}
10931
10932/**
James Smart0d878412009-10-02 15:16:56 -040010933 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
10934 * @phba: pointer to lpfc hba data structure.
10935 *
10936 * This routine is called to prepare the SLI3 device for PCI slot reset. It
10937 * disables the device interrupt and pci device, and aborts the internal FCP
10938 * pending I/Os.
10939 **/
10940static void
10941lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
10942{
James Smart0d878412009-10-02 15:16:56 -040010943 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -050010944 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -040010945
James Smart75baf692010-06-08 18:31:21 -040010946 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040010947 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040010948
James Smarte2af0d22010-03-15 11:25:32 -040010949 /* Block all SCSI devices' I/Os on the host */
10950 lpfc_scsi_dev_block(phba);
10951
James Smartea714f32013-04-17 20:18:47 -040010952 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
10953 lpfc_sli_flush_fcp_rings(phba);
10954
James Smarte2af0d22010-03-15 11:25:32 -040010955 /* stop all timers */
10956 lpfc_stop_hba_timers(phba);
10957
James Smart0d878412009-10-02 15:16:56 -040010958 /* Disable interrupt and pci device */
10959 lpfc_sli_disable_intr(phba);
10960 pci_disable_device(phba->pcidev);
James Smart0d878412009-10-02 15:16:56 -040010961}
10962
10963/**
10964 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
10965 * @phba: pointer to lpfc hba data structure.
10966 *
10967 * This routine is called to prepare the SLI3 device for PCI slot permanently
10968 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
10969 * pending I/Os.
10970 **/
10971static void
James Smart75baf692010-06-08 18:31:21 -040010972lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
James Smart0d878412009-10-02 15:16:56 -040010973{
10974 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -050010975 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -040010976 /* Block all SCSI devices' I/Os on the host */
10977 lpfc_scsi_dev_block(phba);
10978
10979 /* stop all timers */
10980 lpfc_stop_hba_timers(phba);
10981
James Smart0d878412009-10-02 15:16:56 -040010982 /* Clean up all driver's outstanding SCSI I/Os */
10983 lpfc_sli_flush_fcp_rings(phba);
10984}
10985
10986/**
James Smart3772a992009-05-22 14:50:54 -040010987 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -040010988 * @pdev: pointer to PCI device.
10989 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -060010990 *
James Smart3772a992009-05-22 14:50:54 -040010991 * This routine is called from the PCI subsystem for I/O error handling to
10992 * device with SLI-3 interface spec. This function is called by the PCI
10993 * subsystem after a PCI bus error affecting this device has been detected.
10994 * When this function is invoked, it will need to stop all the I/Os and
10995 * interrupt(s) to the device. Once that is done, it will return
10996 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
10997 * as desired.
James Smarte59058c2008-08-24 21:49:00 -040010998 *
10999 * Return codes
James Smart0d878412009-10-02 15:16:56 -040011000 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -040011001 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
11002 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -040011003 **/
James Smart3772a992009-05-22 14:50:54 -040011004static pci_ers_result_t
11005lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -060011006{
James Smart51ef4c22007-08-02 11:10:31 -040011007 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11008 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060011009
James Smart0d878412009-10-02 15:16:56 -040011010 switch (state) {
11011 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -050011012 /* Non-fatal error, prepare for recovery */
11013 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -040011014 return PCI_ERS_RESULT_CAN_RECOVER;
11015 case pci_channel_io_frozen:
11016 /* Fatal error, prepare for slot reset */
11017 lpfc_sli_prep_dev_for_reset(phba);
11018 return PCI_ERS_RESULT_NEED_RESET;
11019 case pci_channel_io_perm_failure:
11020 /* Permanent failure, prepare for device down */
James Smart75baf692010-06-08 18:31:21 -040011021 lpfc_sli_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -060011022 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -040011023 default:
11024 /* Unknown state, prepare and request slot reset */
11025 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11026 "0472 Unknown PCI error state: x%x\n", state);
11027 lpfc_sli_prep_dev_for_reset(phba);
11028 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -040011029 }
Linas Vepstas8d63f372007-02-14 14:28:36 -060011030}
11031
11032/**
James Smart3772a992009-05-22 14:50:54 -040011033 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -040011034 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -060011035 *
James Smart3772a992009-05-22 14:50:54 -040011036 * This routine is called from the PCI subsystem for error handling to
11037 * device with SLI-3 interface spec. This is called after PCI bus has been
11038 * reset to restart the PCI card from scratch, as if from a cold-boot.
11039 * During the PCI subsystem error recovery, after driver returns
11040 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
11041 * recovery and then call this routine before calling the .resume method
11042 * to recover the device. This function will initialize the HBA device,
11043 * enable the interrupt, but it will just put the HBA to offline state
11044 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -040011045 *
11046 * Return codes
James Smart3772a992009-05-22 14:50:54 -040011047 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
11048 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -060011049 */
James Smart3772a992009-05-22 14:50:54 -040011050static pci_ers_result_t
11051lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -060011052{
James Smart51ef4c22007-08-02 11:10:31 -040011053 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11054 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060011055 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -050011056 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -060011057
11058 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +110011059 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -060011060 printk(KERN_ERR "lpfc: Cannot re-enable "
11061 "PCI device after reset.\n");
11062 return PCI_ERS_RESULT_DISCONNECT;
11063 }
11064
James Smart97207482008-12-04 22:39:19 -050011065 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050011066
11067 /*
11068 * As the new kernel behavior of pci_restore_state() API call clears
11069 * device saved_state flag, need to save the restored state again.
11070 */
11071 pci_save_state(pdev);
11072
James Smart97207482008-12-04 22:39:19 -050011073 if (pdev->is_busmaster)
11074 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -060011075
James Smart92d7f7b2007-06-17 19:56:38 -050011076 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011077 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -050011078 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -060011079
James Smart5b75da22008-12-04 22:39:35 -050011080 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -040011081 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -050011082 if (intr_mode == LPFC_INTR_ERROR) {
11083 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11084 "0427 Cannot re-enable interrupt after "
11085 "slot reset.\n");
11086 return PCI_ERS_RESULT_DISCONNECT;
11087 } else
11088 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -060011089
James Smart75baf692010-06-08 18:31:21 -040011090 /* Take device offline, it will perform cleanup */
James Smart618a5232012-06-12 13:54:36 -040011091 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
Linas Vepstas8d63f372007-02-14 14:28:36 -060011092 lpfc_offline(phba);
11093 lpfc_sli_brdrestart(phba);
11094
James Smart5b75da22008-12-04 22:39:35 -050011095 /* Log the current active interrupt mode */
11096 lpfc_log_intr_mode(phba, phba->intr_mode);
11097
Linas Vepstas8d63f372007-02-14 14:28:36 -060011098 return PCI_ERS_RESULT_RECOVERED;
11099}
11100
11101/**
James Smart3772a992009-05-22 14:50:54 -040011102 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -040011103 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -060011104 *
James Smart3772a992009-05-22 14:50:54 -040011105 * This routine is called from the PCI subsystem for error handling to device
11106 * with SLI-3 interface spec. It is called when kernel error recovery tells
11107 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
11108 * error recovery. After this call, traffic can start to flow from this device
11109 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -060011110 */
James Smart3772a992009-05-22 14:50:54 -040011111static void
11112lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -060011113{
James Smart51ef4c22007-08-02 11:10:31 -040011114 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11115 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060011116
James Smarte2af0d22010-03-15 11:25:32 -040011117 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -040011118 lpfc_online(phba);
James Smart0d878412009-10-02 15:16:56 -040011119
11120 /* Clean up Advanced Error Reporting (AER) if needed */
11121 if (phba->hba_flag & HBA_AER_ENABLED)
11122 pci_cleanup_aer_uncorrect_error_status(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -060011123}
11124
James Smart3772a992009-05-22 14:50:54 -040011125/**
James Smartda0436e2009-05-22 14:51:39 -040011126 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
11127 * @phba: pointer to lpfc hba data structure.
11128 *
11129 * returns the number of ELS/CT IOCBs to reserve
11130 **/
11131int
11132lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
11133{
11134 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
11135
James Smartf1126682009-06-10 17:22:44 -040011136 if (phba->sli_rev == LPFC_SLI_REV4) {
11137 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -040011138 return 10;
James Smartf1126682009-06-10 17:22:44 -040011139 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -040011140 return 25;
James Smartf1126682009-06-10 17:22:44 -040011141 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -040011142 return 50;
James Smartf1126682009-06-10 17:22:44 -040011143 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -040011144 return 100;
James Smart8a9d2e82012-05-09 21:16:12 -040011145 else if (max_xri <= 1536)
James Smart6a9c52c2009-10-02 15:16:51 -040011146 return 150;
James Smart8a9d2e82012-05-09 21:16:12 -040011147 else if (max_xri <= 2048)
11148 return 200;
11149 else
11150 return 250;
James Smartf1126682009-06-10 17:22:44 -040011151 } else
11152 return 0;
James Smartda0436e2009-05-22 14:51:39 -040011153}
11154
11155/**
James Smart895427b2017-02-12 13:52:30 -080011156 * lpfc_sli4_get_iocb_cnt - Calculate the # of total IOCBs to reserve
11157 * @phba: pointer to lpfc hba data structure.
11158 *
James Smartf358dd02017-02-12 13:52:34 -080011159 * returns the number of ELS/CT + NVMET IOCBs to reserve
James Smart895427b2017-02-12 13:52:30 -080011160 **/
11161int
11162lpfc_sli4_get_iocb_cnt(struct lpfc_hba *phba)
11163{
11164 int max_xri = lpfc_sli4_get_els_iocb_cnt(phba);
11165
James Smartf358dd02017-02-12 13:52:34 -080011166 if (phba->nvmet_support)
11167 max_xri += LPFC_NVMET_BUF_POST;
James Smart895427b2017-02-12 13:52:30 -080011168 return max_xri;
11169}
11170
11171
11172/**
James Smart52d52442011-05-24 11:42:45 -040011173 * lpfc_write_firmware - attempt to write a firmware image to the port
James Smart52d52442011-05-24 11:42:45 -040011174 * @fw: pointer to firmware image returned from request_firmware.
James Smartce396282012-09-29 11:30:56 -040011175 * @phba: pointer to lpfc hba data structure.
James Smart52d52442011-05-24 11:42:45 -040011176 *
James Smart52d52442011-05-24 11:42:45 -040011177 **/
James Smartce396282012-09-29 11:30:56 -040011178static void
11179lpfc_write_firmware(const struct firmware *fw, void *context)
James Smart52d52442011-05-24 11:42:45 -040011180{
James Smartce396282012-09-29 11:30:56 -040011181 struct lpfc_hba *phba = (struct lpfc_hba *)context;
James Smart6b5151f2012-01-18 16:24:06 -050011182 char fwrev[FW_REV_STR_SIZE];
James Smartce396282012-09-29 11:30:56 -040011183 struct lpfc_grp_hdr *image;
James Smart52d52442011-05-24 11:42:45 -040011184 struct list_head dma_buffer_list;
11185 int i, rc = 0;
11186 struct lpfc_dmabuf *dmabuf, *next;
11187 uint32_t offset = 0, temp_offset = 0;
James Smart6b6ef5d2016-10-13 15:06:17 -070011188 uint32_t magic_number, ftype, fid, fsize;
James Smart52d52442011-05-24 11:42:45 -040011189
James Smartc71ab862012-10-31 14:44:33 -040011190 /* It can be null in no-wait mode, sanity check */
James Smartce396282012-09-29 11:30:56 -040011191 if (!fw) {
11192 rc = -ENXIO;
11193 goto out;
11194 }
11195 image = (struct lpfc_grp_hdr *)fw->data;
11196
James Smart6b6ef5d2016-10-13 15:06:17 -070011197 magic_number = be32_to_cpu(image->magic_number);
11198 ftype = bf_get_be32(lpfc_grp_hdr_file_type, image);
11199 fid = bf_get_be32(lpfc_grp_hdr_id, image),
11200 fsize = be32_to_cpu(image->size);
11201
James Smart52d52442011-05-24 11:42:45 -040011202 INIT_LIST_HEAD(&dma_buffer_list);
James Smart6b6ef5d2016-10-13 15:06:17 -070011203 if ((magic_number != LPFC_GROUP_OJECT_MAGIC_G5 &&
11204 magic_number != LPFC_GROUP_OJECT_MAGIC_G6) ||
11205 ftype != LPFC_FILE_TYPE_GROUP || fsize != fw->size) {
James Smart52d52442011-05-24 11:42:45 -040011206 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11207 "3022 Invalid FW image found. "
Arnd Bergmannefe583c2016-10-17 14:35:46 +020011208 "Magic:%x Type:%x ID:%x Size %d %zd\n",
James Smart6b6ef5d2016-10-13 15:06:17 -070011209 magic_number, ftype, fid, fsize, fw->size);
James Smartce396282012-09-29 11:30:56 -040011210 rc = -EINVAL;
11211 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040011212 }
11213 lpfc_decode_firmware_rev(phba, fwrev, 1);
James Smart88a2cfb2011-07-22 18:36:33 -040011214 if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
James Smart52d52442011-05-24 11:42:45 -040011215 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartce396282012-09-29 11:30:56 -040011216 "3023 Updating Firmware, Current Version:%s "
James Smart52d52442011-05-24 11:42:45 -040011217 "New Version:%s\n",
James Smart88a2cfb2011-07-22 18:36:33 -040011218 fwrev, image->revision);
James Smart52d52442011-05-24 11:42:45 -040011219 for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
11220 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
11221 GFP_KERNEL);
11222 if (!dmabuf) {
11223 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040011224 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040011225 }
11226 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
11227 SLI4_PAGE_SIZE,
11228 &dmabuf->phys,
11229 GFP_KERNEL);
11230 if (!dmabuf->virt) {
11231 kfree(dmabuf);
11232 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040011233 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040011234 }
11235 list_add_tail(&dmabuf->list, &dma_buffer_list);
11236 }
11237 while (offset < fw->size) {
11238 temp_offset = offset;
11239 list_for_each_entry(dmabuf, &dma_buffer_list, list) {
James Smart079b5c92011-08-21 21:48:49 -040011240 if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
James Smart52d52442011-05-24 11:42:45 -040011241 memcpy(dmabuf->virt,
11242 fw->data + temp_offset,
James Smart079b5c92011-08-21 21:48:49 -040011243 fw->size - temp_offset);
11244 temp_offset = fw->size;
James Smart52d52442011-05-24 11:42:45 -040011245 break;
11246 }
James Smart52d52442011-05-24 11:42:45 -040011247 memcpy(dmabuf->virt, fw->data + temp_offset,
11248 SLI4_PAGE_SIZE);
James Smart88a2cfb2011-07-22 18:36:33 -040011249 temp_offset += SLI4_PAGE_SIZE;
James Smart52d52442011-05-24 11:42:45 -040011250 }
11251 rc = lpfc_wr_object(phba, &dma_buffer_list,
11252 (fw->size - offset), &offset);
James Smartce396282012-09-29 11:30:56 -040011253 if (rc)
11254 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040011255 }
11256 rc = offset;
11257 }
James Smartce396282012-09-29 11:30:56 -040011258
11259release_out:
James Smart52d52442011-05-24 11:42:45 -040011260 list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
11261 list_del(&dmabuf->list);
11262 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
11263 dmabuf->virt, dmabuf->phys);
11264 kfree(dmabuf);
11265 }
James Smartce396282012-09-29 11:30:56 -040011266 release_firmware(fw);
11267out:
11268 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartc71ab862012-10-31 14:44:33 -040011269 "3024 Firmware update done: %d.\n", rc);
James Smartce396282012-09-29 11:30:56 -040011270 return;
James Smart52d52442011-05-24 11:42:45 -040011271}
11272
11273/**
James Smartc71ab862012-10-31 14:44:33 -040011274 * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
11275 * @phba: pointer to lpfc hba data structure.
11276 *
11277 * This routine is called to perform Linux generic firmware upgrade on device
11278 * that supports such feature.
11279 **/
11280int
11281lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
11282{
11283 uint8_t file_name[ELX_MODEL_NAME_SIZE];
11284 int ret;
11285 const struct firmware *fw;
11286
11287 /* Only supported on SLI4 interface type 2 for now */
11288 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
11289 LPFC_SLI_INTF_IF_TYPE_2)
11290 return -EPERM;
11291
11292 snprintf(file_name, ELX_MODEL_NAME_SIZE, "%s.grp", phba->ModelName);
11293
11294 if (fw_upgrade == INT_FW_UPGRADE) {
11295 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
11296 file_name, &phba->pcidev->dev,
11297 GFP_KERNEL, (void *)phba,
11298 lpfc_write_firmware);
11299 } else if (fw_upgrade == RUN_FW_UPGRADE) {
11300 ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
11301 if (!ret)
11302 lpfc_write_firmware(fw, (void *)phba);
11303 } else {
11304 ret = -EINVAL;
11305 }
11306
11307 return ret;
11308}
11309
11310/**
James Smartda0436e2009-05-22 14:51:39 -040011311 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
11312 * @pdev: pointer to PCI device
11313 * @pid: pointer to PCI device identifier
11314 *
11315 * This routine is called from the kernel's PCI subsystem to device with
11316 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
11317 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
11318 * information of the device and driver to see if the driver state that it
11319 * can support this kind of device. If the match is successful, the driver
11320 * core invokes this routine. If this routine determines it can claim the HBA,
11321 * it does all the initialization that it needs to do to handle the HBA
11322 * properly.
11323 *
11324 * Return code
11325 * 0 - driver can claim the device
11326 * negative value - driver can not claim the device
11327 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011328static int
James Smartda0436e2009-05-22 14:51:39 -040011329lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
11330{
11331 struct lpfc_hba *phba;
11332 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -040011333 struct Scsi_Host *shost = NULL;
James Smart6c621a22017-05-15 15:20:45 -070011334 int error;
James Smartda0436e2009-05-22 14:51:39 -040011335 uint32_t cfg_mode, intr_mode;
James Smartda0436e2009-05-22 14:51:39 -040011336
11337 /* Allocate memory for HBA structure */
11338 phba = lpfc_hba_alloc(pdev);
11339 if (!phba)
11340 return -ENOMEM;
11341
11342 /* Perform generic PCI device enabling operation */
11343 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -040011344 if (error)
James Smartda0436e2009-05-22 14:51:39 -040011345 goto out_free_phba;
James Smartda0436e2009-05-22 14:51:39 -040011346
11347 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
11348 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
11349 if (error)
11350 goto out_disable_pci_dev;
11351
11352 /* Set up SLI-4 specific device PCI memory space */
11353 error = lpfc_sli4_pci_mem_setup(phba);
11354 if (error) {
11355 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11356 "1410 Failed to set up pci memory space.\n");
11357 goto out_disable_pci_dev;
11358 }
11359
James Smartda0436e2009-05-22 14:51:39 -040011360 /* Set up SLI-4 Specific device driver resources */
11361 error = lpfc_sli4_driver_resource_setup(phba);
11362 if (error) {
11363 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11364 "1412 Failed to set up driver resource.\n");
11365 goto out_unset_pci_mem_s4;
11366 }
11367
James Smart19ca7602010-11-20 23:11:55 -050011368 INIT_LIST_HEAD(&phba->active_rrq_list);
James Smart7d791df2011-07-22 18:37:52 -040011369 INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
James Smart19ca7602010-11-20 23:11:55 -050011370
James Smartda0436e2009-05-22 14:51:39 -040011371 /* Set up common device driver resources */
11372 error = lpfc_setup_driver_resource_phase2(phba);
11373 if (error) {
11374 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11375 "1414 Failed to set up driver resource.\n");
James Smart6c621a22017-05-15 15:20:45 -070011376 goto out_unset_driver_resource_s4;
James Smartda0436e2009-05-22 14:51:39 -040011377 }
11378
James Smart079b5c92011-08-21 21:48:49 -040011379 /* Get the default values for Model Name and Description */
11380 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
11381
James Smartda0436e2009-05-22 14:51:39 -040011382 /* Create SCSI host to the physical port */
11383 error = lpfc_create_shost(phba);
11384 if (error) {
11385 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11386 "1415 Failed to create scsi host.\n");
11387 goto out_unset_driver_resource;
11388 }
11389
11390 /* Configure sysfs attributes */
11391 vport = phba->pport;
11392 error = lpfc_alloc_sysfs_attr(vport);
11393 if (error) {
11394 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11395 "1416 Failed to allocate sysfs attr\n");
11396 goto out_destroy_shost;
11397 }
11398
James Smart6669f9b2009-10-02 15:16:45 -040011399 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -040011400 /* Now, trying to enable interrupt and bring up the device */
11401 cfg_mode = phba->cfg_use_msi;
James Smartda0436e2009-05-22 14:51:39 -040011402
James Smart7b15db32013-01-03 15:43:29 -050011403 /* Put device to a known state before enabling interrupt */
11404 lpfc_stop_port(phba);
James Smart895427b2017-02-12 13:52:30 -080011405
James Smart7b15db32013-01-03 15:43:29 -050011406 /* Configure and enable interrupt */
11407 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
11408 if (intr_mode == LPFC_INTR_ERROR) {
11409 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11410 "0426 Failed to enable interrupt.\n");
11411 error = -ENODEV;
11412 goto out_free_sysfs_attr;
James Smartda0436e2009-05-22 14:51:39 -040011413 }
James Smart7b15db32013-01-03 15:43:29 -050011414 /* Default to single EQ for non-MSI-X */
James Smart895427b2017-02-12 13:52:30 -080011415 if (phba->intr_type != MSIX) {
11416 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP)
11417 phba->cfg_fcp_io_channel = 1;
James Smart2d7dbc42017-02-12 13:52:35 -080011418 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart895427b2017-02-12 13:52:30 -080011419 phba->cfg_nvme_io_channel = 1;
James Smart2d7dbc42017-02-12 13:52:35 -080011420 if (phba->nvmet_support)
11421 phba->cfg_nvmet_mrq = 1;
11422 }
James Smart895427b2017-02-12 13:52:30 -080011423 phba->io_channel_irqs = 1;
11424 }
11425
James Smart7b15db32013-01-03 15:43:29 -050011426 /* Set up SLI-4 HBA */
11427 if (lpfc_sli4_hba_setup(phba)) {
11428 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11429 "1421 Failed to set up hba\n");
11430 error = -ENODEV;
11431 goto out_disable_intr;
11432 }
11433
11434 /* Log the current active interrupt mode */
11435 phba->intr_mode = intr_mode;
11436 lpfc_log_intr_mode(phba, intr_mode);
James Smartda0436e2009-05-22 14:51:39 -040011437
11438 /* Perform post initialization setup */
11439 lpfc_post_init_setup(phba);
11440
James Smart01649562017-02-12 13:52:32 -080011441 /* NVME support in FW earlier in the driver load corrects the
11442 * FC4 type making a check for nvme_support unnecessary.
11443 */
11444 if ((phba->nvmet_support == 0) &&
11445 (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)) {
11446 /* Create NVME binding with nvme_fc_transport. This
James Smartd1f525a2017-04-21 16:04:55 -070011447 * ensures the vport is initialized. If the localport
11448 * create fails, it should not unload the driver to
11449 * support field issues.
James Smart01649562017-02-12 13:52:32 -080011450 */
11451 error = lpfc_nvme_create_localport(vport);
11452 if (error) {
11453 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11454 "6004 NVME registration failed, "
11455 "error x%x\n",
11456 error);
James Smart01649562017-02-12 13:52:32 -080011457 }
11458 }
James Smart895427b2017-02-12 13:52:30 -080011459
James Smartc71ab862012-10-31 14:44:33 -040011460 /* check for firmware upgrade or downgrade */
11461 if (phba->cfg_request_firmware_upgrade)
Sebastian Herbsztdb6f1c22015-08-31 16:48:14 -040011462 lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
James Smart52d52442011-05-24 11:42:45 -040011463
James Smart1c6834a2009-07-19 10:01:26 -040011464 /* Check if there are static vports to be created. */
11465 lpfc_create_static_vport(phba);
James Smartda0436e2009-05-22 14:51:39 -040011466 return 0;
11467
11468out_disable_intr:
11469 lpfc_sli4_disable_intr(phba);
11470out_free_sysfs_attr:
11471 lpfc_free_sysfs_attr(vport);
11472out_destroy_shost:
11473 lpfc_destroy_shost(phba);
11474out_unset_driver_resource:
11475 lpfc_unset_driver_resource_phase2(phba);
James Smartda0436e2009-05-22 14:51:39 -040011476out_unset_driver_resource_s4:
11477 lpfc_sli4_driver_resource_unset(phba);
11478out_unset_pci_mem_s4:
11479 lpfc_sli4_pci_mem_unset(phba);
11480out_disable_pci_dev:
11481 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040011482 if (shost)
11483 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -040011484out_free_phba:
11485 lpfc_hba_free(phba);
11486 return error;
11487}
11488
11489/**
11490 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
11491 * @pdev: pointer to PCI device
11492 *
11493 * This routine is called from the kernel's PCI subsystem to device with
11494 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
11495 * removed from PCI bus, it performs all the necessary cleanup for the HBA
11496 * device to be removed from the PCI subsystem properly.
11497 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011498static void
James Smartda0436e2009-05-22 14:51:39 -040011499lpfc_pci_remove_one_s4(struct pci_dev *pdev)
11500{
11501 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11502 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
11503 struct lpfc_vport **vports;
11504 struct lpfc_hba *phba = vport->phba;
11505 int i;
11506
11507 /* Mark the device unloading flag */
11508 spin_lock_irq(&phba->hbalock);
11509 vport->load_flag |= FC_UNLOADING;
11510 spin_unlock_irq(&phba->hbalock);
11511
11512 /* Free the HBA sysfs attributes */
11513 lpfc_free_sysfs_attr(vport);
11514
11515 /* Release all the vports against this physical port */
11516 vports = lpfc_create_vport_work_array(phba);
11517 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040011518 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
11519 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
11520 continue;
James Smartda0436e2009-05-22 14:51:39 -040011521 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040011522 }
James Smartda0436e2009-05-22 14:51:39 -040011523 lpfc_destroy_vport_work_array(phba, vports);
11524
11525 /* Remove FC host and then SCSI host with the physical port */
11526 fc_remove_host(shost);
11527 scsi_remove_host(shost);
James Smartda0436e2009-05-22 14:51:39 -040011528
James Smartda0436e2009-05-22 14:51:39 -040011529 /* Perform ndlp cleanup on the physical port. The nvme and nvmet
11530 * localports are destroyed after to cleanup all transport memory.
11531 */
11532 lpfc_cleanup(vport);
11533 lpfc_nvmet_destroy_targetport(phba);
11534 lpfc_nvme_destroy_localport(vport);
11535
James Smart281d6192018-01-30 15:58:47 -080011536 /*
11537 * Bring down the SLI Layer. This step disables all interrupts,
11538 * clears the rings, discards all mailbox commands, and resets
11539 * the HBA FCoE function.
11540 */
11541 lpfc_debugfs_terminate(vport);
11542 lpfc_sli4_hba_unset(phba);
James Smartda0436e2009-05-22 14:51:39 -040011543
Dick Kennedy19017622017-09-29 17:34:27 -070011544 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -040011545 spin_lock_irq(&phba->hbalock);
11546 list_del_init(&vport->listentry);
11547 spin_unlock_irq(&phba->hbalock);
11548
James Smart3677a3a2010-09-29 11:19:14 -040011549 /* Perform scsi free before driver resource_unset since scsi
James Smartda0436e2009-05-22 14:51:39 -040011550 * buffers are released to their corresponding pools here.
11551 */
11552 lpfc_scsi_free(phba);
James Smart895427b2017-02-12 13:52:30 -080011553 lpfc_nvme_free(phba);
James Smart01649562017-02-12 13:52:32 -080011554 lpfc_free_iocb_list(phba);
James Smart67d12732012-08-03 12:36:13 -040011555
James Smartda0436e2009-05-22 14:51:39 -040011556 lpfc_sli4_driver_resource_unset(phba);
11557
11558 /* Unmap adapter Control and Doorbell registers */
11559 lpfc_sli4_pci_mem_unset(phba);
11560
11561 /* Release PCI resources and disable device's PCI function */
11562 scsi_host_put(shost);
11563 lpfc_disable_pci_dev(phba);
11564
11565 /* Finally, free the driver's device data structure */
11566 lpfc_hba_free(phba);
11567
11568 return;
11569}
11570
11571/**
11572 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
11573 * @pdev: pointer to PCI device
11574 * @msg: power management message
11575 *
11576 * This routine is called from the kernel's PCI subsystem to support system
11577 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
11578 * this method, it quiesces the device by stopping the driver's worker
11579 * thread for the device, turning off device's interrupt and DMA, and bring
11580 * the device offline. Note that as the driver implements the minimum PM
11581 * requirements to a power-aware driver's PM support for suspend/resume -- all
11582 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
11583 * method call will be treated as SUSPEND and the driver will fully
11584 * reinitialize its device during resume() method call, the driver will set
11585 * device to PCI_D3hot state in PCI config space instead of setting it
11586 * according to the @msg provided by the PM.
11587 *
11588 * Return code
11589 * 0 - driver suspended the device
11590 * Error otherwise
11591 **/
11592static int
11593lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
11594{
11595 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11596 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11597
11598 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart75baf692010-06-08 18:31:21 -040011599 "2843 PCI device Power Management suspend.\n");
James Smartda0436e2009-05-22 14:51:39 -040011600
11601 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040011602 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smartda0436e2009-05-22 14:51:39 -040011603 lpfc_offline(phba);
11604 kthread_stop(phba->worker_thread);
11605
11606 /* Disable interrupt from device */
11607 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040011608 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -040011609
11610 /* Save device state to PCI config space */
11611 pci_save_state(pdev);
11612 pci_set_power_state(pdev, PCI_D3hot);
11613
11614 return 0;
11615}
11616
11617/**
11618 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
11619 * @pdev: pointer to PCI device
11620 *
11621 * This routine is called from the kernel's PCI subsystem to support system
11622 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
11623 * this method, it restores the device's PCI config space state and fully
11624 * reinitializes the device and brings it online. Note that as the driver
11625 * implements the minimum PM requirements to a power-aware driver's PM for
11626 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
11627 * to the suspend() method call will be treated as SUSPEND and the driver
11628 * will fully reinitialize its device during resume() method call, the device
11629 * will be set to PCI_D0 directly in PCI config space before restoring the
11630 * state.
11631 *
11632 * Return code
11633 * 0 - driver suspended the device
11634 * Error otherwise
11635 **/
11636static int
11637lpfc_pci_resume_one_s4(struct pci_dev *pdev)
11638{
11639 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11640 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11641 uint32_t intr_mode;
11642 int error;
11643
11644 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11645 "0292 PCI device Power Management resume.\n");
11646
11647 /* Restore device state from PCI config space */
11648 pci_set_power_state(pdev, PCI_D0);
11649 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050011650
11651 /*
11652 * As the new kernel behavior of pci_restore_state() API call clears
11653 * device saved_state flag, need to save the restored state again.
11654 */
11655 pci_save_state(pdev);
11656
James Smartda0436e2009-05-22 14:51:39 -040011657 if (pdev->is_busmaster)
11658 pci_set_master(pdev);
11659
11660 /* Startup the kernel thread for this host adapter. */
11661 phba->worker_thread = kthread_run(lpfc_do_work, phba,
11662 "lpfc_worker_%d", phba->brd_no);
11663 if (IS_ERR(phba->worker_thread)) {
11664 error = PTR_ERR(phba->worker_thread);
11665 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11666 "0293 PM resume failed to start worker "
11667 "thread: error=x%x.\n", error);
11668 return error;
11669 }
11670
11671 /* Configure and enable interrupt */
11672 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
11673 if (intr_mode == LPFC_INTR_ERROR) {
11674 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11675 "0294 PM resume Failed to enable interrupt\n");
11676 return -EIO;
11677 } else
11678 phba->intr_mode = intr_mode;
11679
11680 /* Restart HBA and bring it online */
11681 lpfc_sli_brdrestart(phba);
11682 lpfc_online(phba);
11683
11684 /* Log the current active interrupt mode */
11685 lpfc_log_intr_mode(phba, phba->intr_mode);
11686
11687 return 0;
11688}
11689
11690/**
James Smart75baf692010-06-08 18:31:21 -040011691 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
11692 * @phba: pointer to lpfc hba data structure.
11693 *
11694 * This routine is called to prepare the SLI4 device for PCI slot recover. It
11695 * aborts all the outstanding SCSI I/Os to the pci device.
11696 **/
11697static void
11698lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
11699{
James Smart75baf692010-06-08 18:31:21 -040011700 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11701 "2828 PCI channel I/O abort preparing for recovery\n");
11702 /*
11703 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
11704 * and let the SCSI mid-layer to retry them to recover.
11705 */
James Smartdb55fba2014-04-04 13:52:02 -040011706 lpfc_sli_abort_fcp_rings(phba);
James Smart75baf692010-06-08 18:31:21 -040011707}
11708
11709/**
11710 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
11711 * @phba: pointer to lpfc hba data structure.
11712 *
11713 * This routine is called to prepare the SLI4 device for PCI slot reset. It
11714 * disables the device interrupt and pci device, and aborts the internal FCP
11715 * pending I/Os.
11716 **/
11717static void
11718lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
11719{
11720 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11721 "2826 PCI channel disable preparing for reset\n");
11722
11723 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040011724 lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
James Smart75baf692010-06-08 18:31:21 -040011725
11726 /* Block all SCSI devices' I/Os on the host */
11727 lpfc_scsi_dev_block(phba);
11728
James Smartea714f32013-04-17 20:18:47 -040011729 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
11730 lpfc_sli_flush_fcp_rings(phba);
11731
James Smartc3725bd2017-11-20 16:00:42 -080011732 /* Flush the outstanding NVME IOs if fc4 type enabled. */
11733 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
11734 lpfc_sli_flush_nvme_rings(phba);
11735
James Smart75baf692010-06-08 18:31:21 -040011736 /* stop all timers */
11737 lpfc_stop_hba_timers(phba);
11738
11739 /* Disable interrupt and pci device */
11740 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040011741 lpfc_sli4_queue_destroy(phba);
James Smart75baf692010-06-08 18:31:21 -040011742 pci_disable_device(phba->pcidev);
James Smart75baf692010-06-08 18:31:21 -040011743}
11744
11745/**
11746 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
11747 * @phba: pointer to lpfc hba data structure.
11748 *
11749 * This routine is called to prepare the SLI4 device for PCI slot permanently
11750 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
11751 * pending I/Os.
11752 **/
11753static void
11754lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
11755{
11756 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11757 "2827 PCI channel permanent disable for failure\n");
11758
11759 /* Block all SCSI devices' I/Os on the host */
11760 lpfc_scsi_dev_block(phba);
11761
11762 /* stop all timers */
11763 lpfc_stop_hba_timers(phba);
11764
11765 /* Clean up all driver's outstanding SCSI I/Os */
11766 lpfc_sli_flush_fcp_rings(phba);
James Smartc3725bd2017-11-20 16:00:42 -080011767
11768 /* Flush the outstanding NVME IOs if fc4 type enabled. */
11769 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
11770 lpfc_sli_flush_nvme_rings(phba);
James Smart75baf692010-06-08 18:31:21 -040011771}
11772
11773/**
James Smartda0436e2009-05-22 14:51:39 -040011774 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
11775 * @pdev: pointer to PCI device.
11776 * @state: the current PCI connection state.
11777 *
11778 * This routine is called from the PCI subsystem for error handling to device
11779 * with SLI-4 interface spec. This function is called by the PCI subsystem
11780 * after a PCI bus error affecting this device has been detected. When this
11781 * function is invoked, it will need to stop all the I/Os and interrupt(s)
11782 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
11783 * for the PCI subsystem to perform proper recovery as desired.
11784 *
11785 * Return codes
11786 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
11787 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11788 **/
11789static pci_ers_result_t
11790lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
11791{
James Smart75baf692010-06-08 18:31:21 -040011792 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11793 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11794
11795 switch (state) {
11796 case pci_channel_io_normal:
11797 /* Non-fatal error, prepare for recovery */
11798 lpfc_sli4_prep_dev_for_recover(phba);
11799 return PCI_ERS_RESULT_CAN_RECOVER;
11800 case pci_channel_io_frozen:
11801 /* Fatal error, prepare for slot reset */
11802 lpfc_sli4_prep_dev_for_reset(phba);
11803 return PCI_ERS_RESULT_NEED_RESET;
11804 case pci_channel_io_perm_failure:
11805 /* Permanent failure, prepare for device down */
11806 lpfc_sli4_prep_dev_for_perm_failure(phba);
11807 return PCI_ERS_RESULT_DISCONNECT;
11808 default:
11809 /* Unknown state, prepare and request slot reset */
11810 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11811 "2825 Unknown PCI error state: x%x\n", state);
11812 lpfc_sli4_prep_dev_for_reset(phba);
11813 return PCI_ERS_RESULT_NEED_RESET;
11814 }
James Smartda0436e2009-05-22 14:51:39 -040011815}
11816
11817/**
11818 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
11819 * @pdev: pointer to PCI device.
11820 *
11821 * This routine is called from the PCI subsystem for error handling to device
11822 * with SLI-4 interface spec. It is called after PCI bus has been reset to
11823 * restart the PCI card from scratch, as if from a cold-boot. During the
11824 * PCI subsystem error recovery, after the driver returns
11825 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
11826 * recovery and then call this routine before calling the .resume method to
11827 * recover the device. This function will initialize the HBA device, enable
11828 * the interrupt, but it will just put the HBA to offline state without
11829 * passing any I/O traffic.
11830 *
11831 * Return codes
11832 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
11833 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11834 */
11835static pci_ers_result_t
11836lpfc_io_slot_reset_s4(struct pci_dev *pdev)
11837{
James Smart75baf692010-06-08 18:31:21 -040011838 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11839 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11840 struct lpfc_sli *psli = &phba->sli;
11841 uint32_t intr_mode;
11842
11843 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
11844 if (pci_enable_device_mem(pdev)) {
11845 printk(KERN_ERR "lpfc: Cannot re-enable "
11846 "PCI device after reset.\n");
11847 return PCI_ERS_RESULT_DISCONNECT;
11848 }
11849
11850 pci_restore_state(pdev);
James Smart0a96e972011-07-22 18:37:28 -040011851
11852 /*
11853 * As the new kernel behavior of pci_restore_state() API call clears
11854 * device saved_state flag, need to save the restored state again.
11855 */
11856 pci_save_state(pdev);
11857
James Smart75baf692010-06-08 18:31:21 -040011858 if (pdev->is_busmaster)
11859 pci_set_master(pdev);
11860
11861 spin_lock_irq(&phba->hbalock);
11862 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
11863 spin_unlock_irq(&phba->hbalock);
11864
11865 /* Configure and enable interrupt */
11866 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
11867 if (intr_mode == LPFC_INTR_ERROR) {
11868 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11869 "2824 Cannot re-enable interrupt after "
11870 "slot reset.\n");
11871 return PCI_ERS_RESULT_DISCONNECT;
11872 } else
11873 phba->intr_mode = intr_mode;
11874
11875 /* Log the current active interrupt mode */
11876 lpfc_log_intr_mode(phba, phba->intr_mode);
11877
James Smartda0436e2009-05-22 14:51:39 -040011878 return PCI_ERS_RESULT_RECOVERED;
11879}
11880
11881/**
11882 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
11883 * @pdev: pointer to PCI device
11884 *
11885 * This routine is called from the PCI subsystem for error handling to device
11886 * with SLI-4 interface spec. It is called when kernel error recovery tells
11887 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
11888 * error recovery. After this call, traffic can start to flow from this device
11889 * again.
11890 **/
11891static void
11892lpfc_io_resume_s4(struct pci_dev *pdev)
11893{
James Smart75baf692010-06-08 18:31:21 -040011894 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11895 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11896
11897 /*
11898 * In case of slot reset, as function reset is performed through
11899 * mailbox command which needs DMA to be enabled, this operation
11900 * has to be moved to the io resume phase. Taking device offline
11901 * will perform the necessary cleanup.
11902 */
11903 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
11904 /* Perform device reset */
James Smart618a5232012-06-12 13:54:36 -040011905 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040011906 lpfc_offline(phba);
11907 lpfc_sli_brdrestart(phba);
11908 /* Bring the device back online */
11909 lpfc_online(phba);
11910 }
11911
11912 /* Clean up Advanced Error Reporting (AER) if needed */
11913 if (phba->hba_flag & HBA_AER_ENABLED)
11914 pci_cleanup_aer_uncorrect_error_status(pdev);
James Smartda0436e2009-05-22 14:51:39 -040011915}
11916
11917/**
James Smart3772a992009-05-22 14:50:54 -040011918 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
11919 * @pdev: pointer to PCI device
11920 * @pid: pointer to PCI device identifier
11921 *
11922 * This routine is to be registered to the kernel's PCI subsystem. When an
11923 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
11924 * at PCI device-specific information of the device and driver to see if the
11925 * driver state that it can support this kind of device. If the match is
11926 * successful, the driver core invokes this routine. This routine dispatches
11927 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
11928 * do all the initialization that it needs to do to handle the HBA device
11929 * properly.
11930 *
11931 * Return code
11932 * 0 - driver can claim the device
11933 * negative value - driver can not claim the device
11934 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011935static int
James Smart3772a992009-05-22 14:50:54 -040011936lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
11937{
11938 int rc;
James Smart8fa38512009-07-19 10:01:03 -040011939 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -040011940
James Smart28baac72010-02-12 14:42:03 -050011941 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -040011942 return -ENODEV;
11943
James Smart8fa38512009-07-19 10:01:03 -040011944 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -050011945 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -040011946 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040011947 else
James Smart3772a992009-05-22 14:50:54 -040011948 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040011949
James Smart3772a992009-05-22 14:50:54 -040011950 return rc;
11951}
11952
11953/**
11954 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
11955 * @pdev: pointer to PCI device
11956 *
11957 * This routine is to be registered to the kernel's PCI subsystem. When an
11958 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
11959 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
11960 * remove routine, which will perform all the necessary cleanup for the
11961 * device to be removed from the PCI subsystem properly.
11962 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011963static void
James Smart3772a992009-05-22 14:50:54 -040011964lpfc_pci_remove_one(struct pci_dev *pdev)
11965{
11966 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11967 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11968
11969 switch (phba->pci_dev_grp) {
11970 case LPFC_PCI_DEV_LP:
11971 lpfc_pci_remove_one_s3(pdev);
11972 break;
James Smartda0436e2009-05-22 14:51:39 -040011973 case LPFC_PCI_DEV_OC:
11974 lpfc_pci_remove_one_s4(pdev);
11975 break;
James Smart3772a992009-05-22 14:50:54 -040011976 default:
11977 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11978 "1424 Invalid PCI device group: 0x%x\n",
11979 phba->pci_dev_grp);
11980 break;
11981 }
11982 return;
11983}
11984
11985/**
11986 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
11987 * @pdev: pointer to PCI device
11988 * @msg: power management message
11989 *
11990 * This routine is to be registered to the kernel's PCI subsystem to support
11991 * system Power Management (PM). When PM invokes this method, it dispatches
11992 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
11993 * suspend the device.
11994 *
11995 * Return code
11996 * 0 - driver suspended the device
11997 * Error otherwise
11998 **/
11999static int
12000lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
12001{
12002 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12003 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12004 int rc = -ENODEV;
12005
12006 switch (phba->pci_dev_grp) {
12007 case LPFC_PCI_DEV_LP:
12008 rc = lpfc_pci_suspend_one_s3(pdev, msg);
12009 break;
James Smartda0436e2009-05-22 14:51:39 -040012010 case LPFC_PCI_DEV_OC:
12011 rc = lpfc_pci_suspend_one_s4(pdev, msg);
12012 break;
James Smart3772a992009-05-22 14:50:54 -040012013 default:
12014 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12015 "1425 Invalid PCI device group: 0x%x\n",
12016 phba->pci_dev_grp);
12017 break;
12018 }
12019 return rc;
12020}
12021
12022/**
12023 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
12024 * @pdev: pointer to PCI device
12025 *
12026 * This routine is to be registered to the kernel's PCI subsystem to support
12027 * system Power Management (PM). When PM invokes this method, it dispatches
12028 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
12029 * resume the device.
12030 *
12031 * Return code
12032 * 0 - driver suspended the device
12033 * Error otherwise
12034 **/
12035static int
12036lpfc_pci_resume_one(struct pci_dev *pdev)
12037{
12038 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12039 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12040 int rc = -ENODEV;
12041
12042 switch (phba->pci_dev_grp) {
12043 case LPFC_PCI_DEV_LP:
12044 rc = lpfc_pci_resume_one_s3(pdev);
12045 break;
James Smartda0436e2009-05-22 14:51:39 -040012046 case LPFC_PCI_DEV_OC:
12047 rc = lpfc_pci_resume_one_s4(pdev);
12048 break;
James Smart3772a992009-05-22 14:50:54 -040012049 default:
12050 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12051 "1426 Invalid PCI device group: 0x%x\n",
12052 phba->pci_dev_grp);
12053 break;
12054 }
12055 return rc;
12056}
12057
12058/**
12059 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
12060 * @pdev: pointer to PCI device.
12061 * @state: the current PCI connection state.
12062 *
12063 * This routine is registered to the PCI subsystem for error handling. This
12064 * function is called by the PCI subsystem after a PCI bus error affecting
12065 * this device has been detected. When this routine is invoked, it dispatches
12066 * the action to the proper SLI-3 or SLI-4 device error detected handling
12067 * routine, which will perform the proper error detected operation.
12068 *
12069 * Return codes
12070 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
12071 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
12072 **/
12073static pci_ers_result_t
12074lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
12075{
12076 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12077 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12078 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
12079
12080 switch (phba->pci_dev_grp) {
12081 case LPFC_PCI_DEV_LP:
12082 rc = lpfc_io_error_detected_s3(pdev, state);
12083 break;
James Smartda0436e2009-05-22 14:51:39 -040012084 case LPFC_PCI_DEV_OC:
12085 rc = lpfc_io_error_detected_s4(pdev, state);
12086 break;
James Smart3772a992009-05-22 14:50:54 -040012087 default:
12088 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12089 "1427 Invalid PCI device group: 0x%x\n",
12090 phba->pci_dev_grp);
12091 break;
12092 }
12093 return rc;
12094}
12095
12096/**
12097 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
12098 * @pdev: pointer to PCI device.
12099 *
12100 * This routine is registered to the PCI subsystem for error handling. This
12101 * function is called after PCI bus has been reset to restart the PCI card
12102 * from scratch, as if from a cold-boot. When this routine is invoked, it
12103 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
12104 * routine, which will perform the proper device reset.
12105 *
12106 * Return codes
12107 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
12108 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
12109 **/
12110static pci_ers_result_t
12111lpfc_io_slot_reset(struct pci_dev *pdev)
12112{
12113 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12114 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12115 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
12116
12117 switch (phba->pci_dev_grp) {
12118 case LPFC_PCI_DEV_LP:
12119 rc = lpfc_io_slot_reset_s3(pdev);
12120 break;
James Smartda0436e2009-05-22 14:51:39 -040012121 case LPFC_PCI_DEV_OC:
12122 rc = lpfc_io_slot_reset_s4(pdev);
12123 break;
James Smart3772a992009-05-22 14:50:54 -040012124 default:
12125 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12126 "1428 Invalid PCI device group: 0x%x\n",
12127 phba->pci_dev_grp);
12128 break;
12129 }
12130 return rc;
12131}
12132
12133/**
12134 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
12135 * @pdev: pointer to PCI device
12136 *
12137 * This routine is registered to the PCI subsystem for error handling. It
12138 * is called when kernel error recovery tells the lpfc driver that it is
12139 * OK to resume normal PCI operation after PCI bus error recovery. When
12140 * this routine is invoked, it dispatches the action to the proper SLI-3
12141 * or SLI-4 device io_resume routine, which will resume the device operation.
12142 **/
12143static void
12144lpfc_io_resume(struct pci_dev *pdev)
12145{
12146 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12147 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12148
12149 switch (phba->pci_dev_grp) {
12150 case LPFC_PCI_DEV_LP:
12151 lpfc_io_resume_s3(pdev);
12152 break;
James Smartda0436e2009-05-22 14:51:39 -040012153 case LPFC_PCI_DEV_OC:
12154 lpfc_io_resume_s4(pdev);
12155 break;
James Smart3772a992009-05-22 14:50:54 -040012156 default:
12157 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12158 "1429 Invalid PCI device group: 0x%x\n",
12159 phba->pci_dev_grp);
12160 break;
12161 }
12162 return;
12163}
12164
James Smart1ba981f2014-02-20 09:56:45 -050012165/**
12166 * lpfc_sli4_oas_verify - Verify OAS is supported by this adapter
12167 * @phba: pointer to lpfc hba data structure.
12168 *
12169 * This routine checks to see if OAS is supported for this adapter. If
12170 * supported, the configure Flash Optimized Fabric flag is set. Otherwise,
12171 * the enable oas flag is cleared and the pool created for OAS device data
12172 * is destroyed.
12173 *
12174 **/
12175void
12176lpfc_sli4_oas_verify(struct lpfc_hba *phba)
12177{
12178
12179 if (!phba->cfg_EnableXLane)
12180 return;
12181
12182 if (phba->sli4_hba.pc_sli4_params.oas_supported) {
12183 phba->cfg_fof = 1;
12184 } else {
James Smartf38fa0b2014-04-04 13:52:21 -040012185 phba->cfg_fof = 0;
James Smart1ba981f2014-02-20 09:56:45 -050012186 if (phba->device_data_mem_pool)
12187 mempool_destroy(phba->device_data_mem_pool);
12188 phba->device_data_mem_pool = NULL;
12189 }
12190
12191 return;
12192}
12193
12194/**
12195 * lpfc_fof_queue_setup - Set up all the fof queues
12196 * @phba: pointer to lpfc hba data structure.
12197 *
12198 * This routine is invoked to set up all the fof queues for the FC HBA
12199 * operation.
12200 *
12201 * Return codes
12202 * 0 - successful
12203 * -ENOMEM - No available memory
12204 **/
12205int
12206lpfc_fof_queue_setup(struct lpfc_hba *phba)
12207{
James Smart895427b2017-02-12 13:52:30 -080012208 struct lpfc_sli_ring *pring;
James Smart1ba981f2014-02-20 09:56:45 -050012209 int rc;
12210
12211 rc = lpfc_eq_create(phba, phba->sli4_hba.fof_eq, LPFC_MAX_IMAX);
12212 if (rc)
12213 return -ENOMEM;
12214
James Smartf38fa0b2014-04-04 13:52:21 -040012215 if (phba->cfg_fof) {
James Smart1ba981f2014-02-20 09:56:45 -050012216
12217 rc = lpfc_cq_create(phba, phba->sli4_hba.oas_cq,
12218 phba->sli4_hba.fof_eq, LPFC_WCQ, LPFC_FCP);
12219 if (rc)
12220 goto out_oas_cq;
12221
12222 rc = lpfc_wq_create(phba, phba->sli4_hba.oas_wq,
12223 phba->sli4_hba.oas_cq, LPFC_FCP);
12224 if (rc)
12225 goto out_oas_wq;
12226
James Smart895427b2017-02-12 13:52:30 -080012227 /* Bind this CQ/WQ to the NVME ring */
12228 pring = phba->sli4_hba.oas_wq->pring;
12229 pring->sli.sli4.wqp =
12230 (void *)phba->sli4_hba.oas_wq;
12231 phba->sli4_hba.oas_cq->pring = pring;
James Smart1ba981f2014-02-20 09:56:45 -050012232 }
12233
12234 return 0;
12235
12236out_oas_wq:
James Smartf38fa0b2014-04-04 13:52:21 -040012237 lpfc_cq_destroy(phba, phba->sli4_hba.oas_cq);
James Smart1ba981f2014-02-20 09:56:45 -050012238out_oas_cq:
12239 lpfc_eq_destroy(phba, phba->sli4_hba.fof_eq);
12240 return rc;
12241
12242}
12243
12244/**
12245 * lpfc_fof_queue_create - Create all the fof queues
12246 * @phba: pointer to lpfc hba data structure.
12247 *
12248 * This routine is invoked to allocate all the fof queues for the FC HBA
12249 * operation. For each SLI4 queue type, the parameters such as queue entry
12250 * count (queue depth) shall be taken from the module parameter. For now,
12251 * we just use some constant number as place holder.
12252 *
12253 * Return codes
12254 * 0 - successful
12255 * -ENOMEM - No availble memory
12256 * -EIO - The mailbox failed to complete successfully.
12257 **/
12258int
12259lpfc_fof_queue_create(struct lpfc_hba *phba)
12260{
12261 struct lpfc_queue *qdesc;
James Smartc176ffa2018-01-30 15:58:46 -080012262 uint32_t wqesize;
James Smart1ba981f2014-02-20 09:56:45 -050012263
12264 /* Create FOF EQ */
James Smart81b96ed2017-11-20 16:00:29 -080012265 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
12266 phba->sli4_hba.eq_esize,
James Smart1ba981f2014-02-20 09:56:45 -050012267 phba->sli4_hba.eq_ecount);
12268 if (!qdesc)
12269 goto out_error;
12270
12271 phba->sli4_hba.fof_eq = qdesc;
12272
James Smartf38fa0b2014-04-04 13:52:21 -040012273 if (phba->cfg_fof) {
James Smart1ba981f2014-02-20 09:56:45 -050012274
12275 /* Create OAS CQ */
James Smartc176ffa2018-01-30 15:58:46 -080012276 if (phba->enab_exp_wqcq_pages)
James Smarta51e41b2017-12-08 17:18:06 -080012277 qdesc = lpfc_sli4_queue_alloc(phba,
12278 LPFC_EXPANDED_PAGE_SIZE,
12279 phba->sli4_hba.cq_esize,
12280 LPFC_CQE_EXP_COUNT);
12281 else
12282 qdesc = lpfc_sli4_queue_alloc(phba,
12283 LPFC_DEFAULT_PAGE_SIZE,
12284 phba->sli4_hba.cq_esize,
12285 phba->sli4_hba.cq_ecount);
James Smart1ba981f2014-02-20 09:56:45 -050012286 if (!qdesc)
12287 goto out_error;
12288
12289 phba->sli4_hba.oas_cq = qdesc;
12290
12291 /* Create OAS WQ */
James Smartc176ffa2018-01-30 15:58:46 -080012292 if (phba->enab_exp_wqcq_pages) {
12293 wqesize = (phba->fcp_embed_io) ?
12294 LPFC_WQE128_SIZE : phba->sli4_hba.wq_esize;
James Smarta51e41b2017-12-08 17:18:06 -080012295 qdesc = lpfc_sli4_queue_alloc(phba,
12296 LPFC_EXPANDED_PAGE_SIZE,
James Smartc176ffa2018-01-30 15:58:46 -080012297 wqesize,
James Smarta51e41b2017-12-08 17:18:06 -080012298 LPFC_WQE_EXP_COUNT);
James Smartc176ffa2018-01-30 15:58:46 -080012299 } else
James Smarta51e41b2017-12-08 17:18:06 -080012300 qdesc = lpfc_sli4_queue_alloc(phba,
12301 LPFC_DEFAULT_PAGE_SIZE,
12302 phba->sli4_hba.wq_esize,
12303 phba->sli4_hba.wq_ecount);
James Smartc176ffa2018-01-30 15:58:46 -080012304
James Smart1ba981f2014-02-20 09:56:45 -050012305 if (!qdesc)
12306 goto out_error;
12307
12308 phba->sli4_hba.oas_wq = qdesc;
James Smart895427b2017-02-12 13:52:30 -080012309 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
James Smart1ba981f2014-02-20 09:56:45 -050012310
12311 }
12312 return 0;
12313
12314out_error:
12315 lpfc_fof_queue_destroy(phba);
12316 return -ENOMEM;
12317}
12318
12319/**
12320 * lpfc_fof_queue_destroy - Destroy all the fof queues
12321 * @phba: pointer to lpfc hba data structure.
12322 *
12323 * This routine is invoked to release all the SLI4 queues with the FC HBA
12324 * operation.
12325 *
12326 * Return codes
12327 * 0 - successful
12328 **/
12329int
12330lpfc_fof_queue_destroy(struct lpfc_hba *phba)
12331{
12332 /* Release FOF Event queue */
12333 if (phba->sli4_hba.fof_eq != NULL) {
12334 lpfc_sli4_queue_free(phba->sli4_hba.fof_eq);
12335 phba->sli4_hba.fof_eq = NULL;
12336 }
12337
12338 /* Release OAS Completion queue */
12339 if (phba->sli4_hba.oas_cq != NULL) {
12340 lpfc_sli4_queue_free(phba->sli4_hba.oas_cq);
12341 phba->sli4_hba.oas_cq = NULL;
12342 }
12343
12344 /* Release OAS Work queue */
12345 if (phba->sli4_hba.oas_wq != NULL) {
12346 lpfc_sli4_queue_free(phba->sli4_hba.oas_wq);
12347 phba->sli4_hba.oas_wq = NULL;
12348 }
12349 return 0;
12350}
12351
dea31012005-04-17 16:05:31 -050012352MODULE_DEVICE_TABLE(pci, lpfc_id_table);
12353
Stephen Hemmingera55b2d22012-09-07 09:33:16 -070012354static const struct pci_error_handlers lpfc_err_handler = {
Linas Vepstas8d63f372007-02-14 14:28:36 -060012355 .error_detected = lpfc_io_error_detected,
12356 .slot_reset = lpfc_io_slot_reset,
12357 .resume = lpfc_io_resume,
12358};
12359
dea31012005-04-17 16:05:31 -050012360static struct pci_driver lpfc_driver = {
12361 .name = LPFC_DRIVER_NAME,
12362 .id_table = lpfc_id_table,
12363 .probe = lpfc_pci_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080012364 .remove = lpfc_pci_remove_one,
Anton Blanchard85e8a232017-02-13 08:49:20 +110012365 .shutdown = lpfc_pci_remove_one,
James Smart3a55b532008-12-04 22:38:54 -050012366 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -040012367 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -050012368 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -050012369};
12370
James Smart3ef6d242012-01-18 16:23:48 -050012371static const struct file_operations lpfc_mgmt_fop = {
Al Viro858feac2013-04-14 22:39:37 -040012372 .owner = THIS_MODULE,
James Smart3ef6d242012-01-18 16:23:48 -050012373};
12374
12375static struct miscdevice lpfc_mgmt_dev = {
12376 .minor = MISC_DYNAMIC_MINOR,
12377 .name = "lpfcmgmt",
12378 .fops = &lpfc_mgmt_fop,
12379};
12380
James Smarte59058c2008-08-24 21:49:00 -040012381/**
James Smart3621a712009-04-06 18:47:14 -040012382 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -040012383 *
12384 * This routine is to be invoked when the lpfc module is loaded into the
12385 * kernel. The special kernel macro module_init() is used to indicate the
12386 * role of this routine to the kernel as lpfc module entry point.
12387 *
12388 * Return codes
12389 * 0 - successful
12390 * -ENOMEM - FC attach transport failed
12391 * all others - failed
12392 */
dea31012005-04-17 16:05:31 -050012393static int __init
12394lpfc_init(void)
12395{
12396 int error = 0;
12397
12398 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040012399 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -050012400
James Smart3ef6d242012-01-18 16:23:48 -050012401 error = misc_register(&lpfc_mgmt_dev);
12402 if (error)
12403 printk(KERN_ERR "Could not register lpfcmgmt device, "
12404 "misc_register returned with status %d", error);
12405
James Smart458c0832016-07-06 12:36:07 -070012406 lpfc_transport_functions.vport_create = lpfc_vport_create;
12407 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
dea31012005-04-17 16:05:31 -050012408 lpfc_transport_template =
12409 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -040012410 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -050012411 return -ENOMEM;
James Smart458c0832016-07-06 12:36:07 -070012412 lpfc_vport_transport_template =
12413 fc_attach_transport(&lpfc_vport_transport_functions);
12414 if (lpfc_vport_transport_template == NULL) {
12415 fc_release_transport(lpfc_transport_template);
12416 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -040012417 }
James Smart7bb03bb2013-04-17 20:19:16 -040012418
12419 /* Initialize in case vector mapping is needed */
James Smartb246de12013-05-31 17:03:07 -040012420 lpfc_used_cpu = NULL;
James Smart2ea259e2017-02-12 13:52:27 -080012421 lpfc_present_cpu = num_present_cpus();
James Smart7bb03bb2013-04-17 20:19:16 -040012422
dea31012005-04-17 16:05:31 -050012423 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -050012424 if (error) {
dea31012005-04-17 16:05:31 -050012425 fc_release_transport(lpfc_transport_template);
James Smart458c0832016-07-06 12:36:07 -070012426 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -050012427 }
dea31012005-04-17 16:05:31 -050012428
12429 return error;
12430}
12431
James Smarte59058c2008-08-24 21:49:00 -040012432/**
James Smart3621a712009-04-06 18:47:14 -040012433 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -040012434 *
12435 * This routine is invoked when the lpfc module is removed from the kernel.
12436 * The special kernel macro module_exit() is used to indicate the role of
12437 * this routine to the kernel as lpfc module exit point.
12438 */
dea31012005-04-17 16:05:31 -050012439static void __exit
12440lpfc_exit(void)
12441{
James Smart3ef6d242012-01-18 16:23:48 -050012442 misc_deregister(&lpfc_mgmt_dev);
dea31012005-04-17 16:05:31 -050012443 pci_unregister_driver(&lpfc_driver);
12444 fc_release_transport(lpfc_transport_template);
James Smart458c0832016-07-06 12:36:07 -070012445 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -050012446 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040012447 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
12448 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050012449 (1L << _dump_buf_data_order), _dump_buf_data);
12450 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
12451 }
12452
12453 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -040012454 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
12455 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050012456 (1L << _dump_buf_dif_order), _dump_buf_dif);
12457 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
12458 }
James Smartb246de12013-05-31 17:03:07 -040012459 kfree(lpfc_used_cpu);
Johannes Thumshirn79739672015-08-31 16:48:11 -040012460 idr_destroy(&lpfc_hba_index);
dea31012005-04-17 16:05:31 -050012461}
12462
12463module_init(lpfc_init);
12464module_exit(lpfc_exit);
12465MODULE_LICENSE("GPL");
12466MODULE_DESCRIPTION(LPFC_MODULE_DESC);
James Smartd080abe2017-02-12 13:52:39 -080012467MODULE_AUTHOR("Broadcom");
dea31012005-04-17 16:05:31 -050012468MODULE_VERSION("0:" LPFC_DRIVER_VERSION);