blob: c22b88a08c1bd1852228e738c6f09a89de227f7d [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smartd080abe2017-02-12 13:52:39 -08004 * Copyright (C) 2017 Broadcom. All Rights Reserved. The term *
5 * “Broadcom” refers to Broadcom Limited and/or its subsidiaries. *
James Smart50611572016-03-31 14:12:34 -07006 * Copyright (C) 2004-2016 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * EMULEX and SLI are trademarks of Emulex. *
James Smartd080abe2017-02-12 13:52:39 -08008 * www.broadcom.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04009 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -050010 * *
11 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040012 * modify it under the terms of version 2 of the GNU General *
13 * Public License as published by the Free Software Foundation. *
14 * This program is distributed in the hope that it will be useful. *
15 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
16 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
17 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
18 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19 * TO BE LEGALLY INVALID. See the GNU General Public License for *
20 * more details, a copy of which can be found in the file COPYING *
21 * included with this package. *
dea31012005-04-17 16:05:31 -050022 *******************************************************************/
23
dea31012005-04-17 16:05:31 -050024#include <linux/blkdev.h>
25#include <linux/delay.h>
26#include <linux/dma-mapping.h>
27#include <linux/idr.h>
28#include <linux/interrupt.h>
Paul Gortmakeracf3368f2011-05-27 09:47:43 -040029#include <linux/module.h>
dea31012005-04-17 16:05:31 -050030#include <linux/kthread.h>
31#include <linux/pci.h>
32#include <linux/spinlock.h>
James Smart92d7f7b2007-06-17 19:56:38 -050033#include <linux/ctype.h>
James Smart0d878412009-10-02 15:16:56 -040034#include <linux/aer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
James Smart52d52442011-05-24 11:42:45 -040036#include <linux/firmware.h>
James Smart3ef6d242012-01-18 16:23:48 -050037#include <linux/miscdevice.h>
James Smart7bb03bb2013-04-17 20:19:16 -040038#include <linux/percpu.h>
James Smart895427b2017-02-12 13:52:30 -080039#include <linux/msi.h>
dea31012005-04-17 16:05:31 -050040
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040041#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050042#include <scsi/scsi_device.h>
43#include <scsi/scsi_host.h>
44#include <scsi/scsi_transport_fc.h>
James Smart86c67372017-04-21 16:05:04 -070045#include <scsi/scsi_tcq.h>
46#include <scsi/fc/fc_fs.h>
47
48#include <linux/nvme-fc-driver.h>
dea31012005-04-17 16:05:31 -050049
James Smartda0436e2009-05-22 14:51:39 -040050#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050051#include "lpfc_hw.h"
52#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040053#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040054#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050055#include "lpfc_disc.h"
dea31012005-04-17 16:05:31 -050056#include "lpfc.h"
James Smart895427b2017-02-12 13:52:30 -080057#include "lpfc_scsi.h"
58#include "lpfc_nvme.h"
James Smart86c67372017-04-21 16:05:04 -070059#include "lpfc_nvmet.h"
dea31012005-04-17 16:05:31 -050060#include "lpfc_logmsg.h"
61#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050062#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050063#include "lpfc_version.h"
James Smart12f44452016-07-06 12:36:03 -070064#include "lpfc_ids.h"
dea31012005-04-17 16:05:31 -050065
James Smart81301a92008-12-04 22:39:46 -050066char *_dump_buf_data;
67unsigned long _dump_buf_data_order;
68char *_dump_buf_dif;
69unsigned long _dump_buf_dif_order;
70spinlock_t _dump_buf_lock;
71
James Smart7bb03bb2013-04-17 20:19:16 -040072/* Used when mapping IRQ vectors in a driver centric manner */
James Smartb246de12013-05-31 17:03:07 -040073uint16_t *lpfc_used_cpu;
74uint32_t lpfc_present_cpu;
James Smart7bb03bb2013-04-17 20:19:16 -040075
dea31012005-04-17 16:05:31 -050076static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
77static int lpfc_post_rcv_buf(struct lpfc_hba *);
James Smart5350d872011-10-10 21:33:49 -040078static int lpfc_sli4_queue_verify(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040079static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
80static int lpfc_setup_endian_order(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040081static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
James Smart8a9d2e82012-05-09 21:16:12 -040082static void lpfc_free_els_sgl_list(struct lpfc_hba *);
James Smartf358dd02017-02-12 13:52:34 -080083static void lpfc_free_nvmet_sgl_list(struct lpfc_hba *);
James Smart8a9d2e82012-05-09 21:16:12 -040084static void lpfc_init_sgl_list(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040085static int lpfc_init_active_sgl_array(struct lpfc_hba *);
86static void lpfc_free_active_sgl(struct lpfc_hba *);
87static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
88static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
89static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
90static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
91static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
James Smart618a5232012-06-12 13:54:36 -040092static void lpfc_sli4_disable_intr(struct lpfc_hba *);
93static uint32_t lpfc_sli4_enable_intr(struct lpfc_hba *, uint32_t);
James Smart1ba981f2014-02-20 09:56:45 -050094static void lpfc_sli4_oas_verify(struct lpfc_hba *phba);
dea31012005-04-17 16:05:31 -050095
96static struct scsi_transport_template *lpfc_transport_template = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050097static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea31012005-04-17 16:05:31 -050098static DEFINE_IDR(lpfc_hba_index);
James Smartf358dd02017-02-12 13:52:34 -080099#define LPFC_NVMET_BUF_POST 254
dea31012005-04-17 16:05:31 -0500100
James Smarte59058c2008-08-24 21:49:00 -0400101/**
James Smart3621a712009-04-06 18:47:14 -0400102 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
James Smarte59058c2008-08-24 21:49:00 -0400103 * @phba: pointer to lpfc hba data structure.
104 *
105 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
106 * mailbox command. It retrieves the revision information from the HBA and
107 * collects the Vital Product Data (VPD) about the HBA for preparing the
108 * configuration of the HBA.
109 *
110 * Return codes:
111 * 0 - success.
112 * -ERESTART - requests the SLI layer to reset the HBA and try again.
113 * Any other value - indicates an error.
114 **/
dea31012005-04-17 16:05:31 -0500115int
James Smart2e0fef82007-06-17 19:56:36 -0500116lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500117{
118 lpfc_vpd_t *vp = &phba->vpd;
119 int i = 0, rc;
120 LPFC_MBOXQ_t *pmb;
121 MAILBOX_t *mb;
122 char *lpfc_vpd_data = NULL;
123 uint16_t offset = 0;
124 static char licensed[56] =
125 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -0400126 static int init_key = 1;
dea31012005-04-17 16:05:31 -0500127
128 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
129 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500130 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500131 return -ENOMEM;
132 }
133
James Smart04c68492009-05-22 14:52:52 -0400134 mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -0500135 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500136
137 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -0400138 if (init_key) {
139 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -0500140
James Smart65a29c12006-07-06 15:50:50 -0400141 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
142 *ptext = cpu_to_be32(*ptext);
143 init_key = 0;
144 }
dea31012005-04-17 16:05:31 -0500145
146 lpfc_read_nv(phba, pmb);
147 memset((char*)mb->un.varRDnvp.rsvd3, 0,
148 sizeof (mb->un.varRDnvp.rsvd3));
149 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
150 sizeof (licensed));
151
152 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
153
154 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500155 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smarte8b62012007-08-02 11:10:09 -0400156 "0324 Config Port initialization "
dea31012005-04-17 16:05:31 -0500157 "error, mbxCmd x%x READ_NVPARM, "
158 "mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500159 mb->mbxCommand, mb->mbxStatus);
160 mempool_free(pmb, phba->mbox_mem_pool);
161 return -ERESTART;
162 }
163 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
James Smart2e0fef82007-06-17 19:56:36 -0500164 sizeof(phba->wwnn));
165 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
166 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500167 }
168
James Smart92d7f7b2007-06-17 19:56:38 -0500169 phba->sli3_options = 0x0;
170
dea31012005-04-17 16:05:31 -0500171 /* Setup and issue mailbox READ REV command */
172 lpfc_read_rev(phba, pmb);
173 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
174 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500175 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400176 "0439 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500177 "READ_REV, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500178 mb->mbxCommand, mb->mbxStatus);
179 mempool_free( pmb, phba->mbox_mem_pool);
180 return -ERESTART;
181 }
182
James Smart92d7f7b2007-06-17 19:56:38 -0500183
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500184 /*
185 * The value of rr must be 1 since the driver set the cv field to 1.
186 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500187 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500188 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500189 vp->rev.rBit = 0;
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500190 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400191 "0440 Adapter failed to init, READ_REV has "
192 "missing revision information.\n");
dea31012005-04-17 16:05:31 -0500193 mempool_free(pmb, phba->mbox_mem_pool);
194 return -ERESTART;
dea31012005-04-17 16:05:31 -0500195 }
196
James Smart495a7142008-06-14 22:52:59 -0400197 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
198 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -0500199 return -EINVAL;
James Smart495a7142008-06-14 22:52:59 -0400200 }
James Smarted957682007-06-17 19:56:37 -0500201
dea31012005-04-17 16:05:31 -0500202 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500203 vp->rev.rBit = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500204 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500205 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
206 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
207 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
208 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500209 vp->rev.biuRev = mb->un.varRdRev.biuRev;
210 vp->rev.smRev = mb->un.varRdRev.smRev;
211 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
212 vp->rev.endecRev = mb->un.varRdRev.endecRev;
213 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
214 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
215 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
216 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
217 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
218 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
219
James Smart92d7f7b2007-06-17 19:56:38 -0500220 /* If the sli feature level is less then 9, we must
221 * tear down all RPIs and VPIs on link down if NPIV
222 * is enabled.
223 */
224 if (vp->rev.feaLevelHigh < 9)
225 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
226
dea31012005-04-17 16:05:31 -0500227 if (lpfc_is_LC_HBA(phba->pcidev->device))
228 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
229 sizeof (phba->RandomData));
230
dea31012005-04-17 16:05:31 -0500231 /* Get adapter VPD information */
dea31012005-04-17 16:05:31 -0500232 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
233 if (!lpfc_vpd_data)
James Smartd7c255b2008-08-24 21:50:00 -0400234 goto out_free_mbox;
dea31012005-04-17 16:05:31 -0500235 do {
James Smarta0c87cb2009-07-19 10:01:10 -0400236 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
dea31012005-04-17 16:05:31 -0500237 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
238
239 if (rc != MBX_SUCCESS) {
240 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400241 "0441 VPD not present on adapter, "
dea31012005-04-17 16:05:31 -0500242 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500243 mb->mbxCommand, mb->mbxStatus);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500244 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500245 }
James Smart04c68492009-05-22 14:52:52 -0400246 /* dump mem may return a zero when finished or we got a
247 * mailbox error, either way we are done.
248 */
249 if (mb->un.varDmp.word_cnt == 0)
250 break;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500251 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
252 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
James Smartd7c255b2008-08-24 21:50:00 -0400253 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
254 lpfc_vpd_data + offset,
James Smart92d7f7b2007-06-17 19:56:38 -0500255 mb->un.varDmp.word_cnt);
dea31012005-04-17 16:05:31 -0500256 offset += mb->un.varDmp.word_cnt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500257 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
258 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500259
260 kfree(lpfc_vpd_data);
dea31012005-04-17 16:05:31 -0500261out_free_mbox:
262 mempool_free(pmb, phba->mbox_mem_pool);
263 return 0;
264}
265
James Smarte59058c2008-08-24 21:49:00 -0400266/**
James Smart3621a712009-04-06 18:47:14 -0400267 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
James Smarte59058c2008-08-24 21:49:00 -0400268 * @phba: pointer to lpfc hba data structure.
269 * @pmboxq: pointer to the driver internal queue element for mailbox command.
270 *
271 * This is the completion handler for driver's configuring asynchronous event
272 * mailbox command to the device. If the mailbox command returns successfully,
273 * it will set internal async event support flag to 1; otherwise, it will
274 * set internal async event support flag to 0.
275 **/
James Smart57127f12007-10-27 13:37:05 -0400276static void
277lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
278{
James Smart04c68492009-05-22 14:52:52 -0400279 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
James Smart57127f12007-10-27 13:37:05 -0400280 phba->temp_sensor_support = 1;
281 else
282 phba->temp_sensor_support = 0;
283 mempool_free(pmboxq, phba->mbox_mem_pool);
284 return;
285}
286
James Smarte59058c2008-08-24 21:49:00 -0400287/**
James Smart3621a712009-04-06 18:47:14 -0400288 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
James Smart97207482008-12-04 22:39:19 -0500289 * @phba: pointer to lpfc hba data structure.
290 * @pmboxq: pointer to the driver internal queue element for mailbox command.
291 *
292 * This is the completion handler for dump mailbox command for getting
293 * wake up parameters. When this command complete, the response contain
294 * Option rom version of the HBA. This function translate the version number
295 * into a human readable string and store it in OptionROMVersion.
296 **/
297static void
298lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
299{
300 struct prog_id *prg;
301 uint32_t prog_id_word;
302 char dist = ' ';
303 /* character array used for decoding dist type. */
304 char dist_char[] = "nabx";
305
James Smart04c68492009-05-22 14:52:52 -0400306 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
James Smart9f1e1b52008-12-04 22:39:40 -0500307 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500308 return;
James Smart9f1e1b52008-12-04 22:39:40 -0500309 }
James Smart97207482008-12-04 22:39:19 -0500310
311 prg = (struct prog_id *) &prog_id_word;
312
313 /* word 7 contain option rom version */
James Smart04c68492009-05-22 14:52:52 -0400314 prog_id_word = pmboxq->u.mb.un.varWords[7];
James Smart97207482008-12-04 22:39:19 -0500315
316 /* Decode the Option rom version word to a readable string */
317 if (prg->dist < 4)
318 dist = dist_char[prg->dist];
319
320 if ((prg->dist == 3) && (prg->num == 0))
James Smarta2fc4aef2014-09-03 12:57:55 -0400321 snprintf(phba->OptionROMVersion, 32, "%d.%d%d",
James Smart97207482008-12-04 22:39:19 -0500322 prg->ver, prg->rev, prg->lev);
323 else
James Smarta2fc4aef2014-09-03 12:57:55 -0400324 snprintf(phba->OptionROMVersion, 32, "%d.%d%d%c%d",
James Smart97207482008-12-04 22:39:19 -0500325 prg->ver, prg->rev, prg->lev,
326 dist, prg->num);
James Smart9f1e1b52008-12-04 22:39:40 -0500327 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500328 return;
329}
330
331/**
James Smart05580562011-05-24 11:40:48 -0400332 * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
333 * cfg_soft_wwnn, cfg_soft_wwpn
334 * @vport: pointer to lpfc vport data structure.
335 *
336 *
337 * Return codes
338 * None.
339 **/
340void
341lpfc_update_vport_wwn(struct lpfc_vport *vport)
342{
James Smartaeb3c812017-04-21 16:05:02 -0700343 uint8_t vvvl = vport->fc_sparam.cmn.valid_vendor_ver_level;
344 u32 *fawwpn_key = (u32 *)&vport->fc_sparam.un.vendorVersion[0];
345
James Smart05580562011-05-24 11:40:48 -0400346 /* If the soft name exists then update it using the service params */
347 if (vport->phba->cfg_soft_wwnn)
348 u64_to_wwn(vport->phba->cfg_soft_wwnn,
349 vport->fc_sparam.nodeName.u.wwn);
350 if (vport->phba->cfg_soft_wwpn)
351 u64_to_wwn(vport->phba->cfg_soft_wwpn,
352 vport->fc_sparam.portName.u.wwn);
353
354 /*
355 * If the name is empty or there exists a soft name
356 * then copy the service params name, otherwise use the fc name
357 */
358 if (vport->fc_nodename.u.wwn[0] == 0 || vport->phba->cfg_soft_wwnn)
359 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
360 sizeof(struct lpfc_name));
361 else
362 memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
363 sizeof(struct lpfc_name));
364
James Smartaeb3c812017-04-21 16:05:02 -0700365 /*
366 * If the port name has changed, then set the Param changes flag
367 * to unreg the login
368 */
369 if (vport->fc_portname.u.wwn[0] != 0 &&
370 memcmp(&vport->fc_portname, &vport->fc_sparam.portName,
371 sizeof(struct lpfc_name)))
372 vport->vport_flag |= FAWWPN_PARAM_CHG;
373
374 if (vport->fc_portname.u.wwn[0] == 0 ||
375 vport->phba->cfg_soft_wwpn ||
376 (vvvl == 1 && cpu_to_be32(*fawwpn_key) == FAPWWN_KEY_VENDOR) ||
377 vport->vport_flag & FAWWPN_SET) {
James Smart05580562011-05-24 11:40:48 -0400378 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
379 sizeof(struct lpfc_name));
James Smartaeb3c812017-04-21 16:05:02 -0700380 vport->vport_flag &= ~FAWWPN_SET;
381 if (vvvl == 1 && cpu_to_be32(*fawwpn_key) == FAPWWN_KEY_VENDOR)
382 vport->vport_flag |= FAWWPN_SET;
383 }
James Smart05580562011-05-24 11:40:48 -0400384 else
385 memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
386 sizeof(struct lpfc_name));
387}
388
389/**
James Smart3621a712009-04-06 18:47:14 -0400390 * lpfc_config_port_post - Perform lpfc initialization after config port
James Smarte59058c2008-08-24 21:49:00 -0400391 * @phba: pointer to lpfc hba data structure.
392 *
393 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
394 * command call. It performs all internal resource and state setups on the
395 * port: post IOCB buffers, enable appropriate host interrupt attentions,
396 * ELS ring timers, etc.
397 *
398 * Return codes
399 * 0 - success.
400 * Any other value - error.
401 **/
dea31012005-04-17 16:05:31 -0500402int
James Smart2e0fef82007-06-17 19:56:36 -0500403lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500404{
James Smart2e0fef82007-06-17 19:56:36 -0500405 struct lpfc_vport *vport = phba->pport;
James Smarta257bf92009-04-06 18:48:10 -0400406 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -0500407 LPFC_MBOXQ_t *pmb;
408 MAILBOX_t *mb;
409 struct lpfc_dmabuf *mp;
410 struct lpfc_sli *psli = &phba->sli;
411 uint32_t status, timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500412 int i, j;
413 int rc;
dea31012005-04-17 16:05:31 -0500414
James Smart7af67052007-10-27 13:38:11 -0400415 spin_lock_irq(&phba->hbalock);
416 /*
417 * If the Config port completed correctly the HBA is not
418 * over heated any more.
419 */
420 if (phba->over_temp_state == HBA_OVER_TEMP)
421 phba->over_temp_state = HBA_NORMAL_TEMP;
422 spin_unlock_irq(&phba->hbalock);
423
dea31012005-04-17 16:05:31 -0500424 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
425 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500426 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500427 return -ENOMEM;
428 }
James Smart04c68492009-05-22 14:52:52 -0400429 mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500430
dea31012005-04-17 16:05:31 -0500431 /* Get login parameters for NID. */
James Smart9f1177a2010-02-26 14:12:57 -0500432 rc = lpfc_read_sparam(phba, pmb, 0);
433 if (rc) {
434 mempool_free(pmb, phba->mbox_mem_pool);
435 return -ENOMEM;
436 }
437
James Smarted957682007-06-17 19:56:37 -0500438 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500439 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500440 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400441 "0448 Adapter failed init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500442 "READ_SPARM mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500443 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500444 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500445 mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart9f1177a2010-02-26 14:12:57 -0500446 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500447 lpfc_mbuf_free(phba, mp->virt, mp->phys);
448 kfree(mp);
449 return -EIO;
450 }
451
452 mp = (struct lpfc_dmabuf *) pmb->context1;
453
James Smart2e0fef82007-06-17 19:56:36 -0500454 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500455 lpfc_mbuf_free(phba, mp->virt, mp->phys);
456 kfree(mp);
457 pmb->context1 = NULL;
James Smart05580562011-05-24 11:40:48 -0400458 lpfc_update_vport_wwn(vport);
James Smarta257bf92009-04-06 18:48:10 -0400459
460 /* Update the fc_host data structures with new wwn. */
461 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
462 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart21e9a0a2009-05-22 14:53:21 -0400463 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smarta257bf92009-04-06 18:48:10 -0400464
dea31012005-04-17 16:05:31 -0500465 /* If no serial number in VPD data, use low 6 bytes of WWNN */
466 /* This should be consolidated into parse_vpd ? - mr */
467 if (phba->SerialNumber[0] == 0) {
468 uint8_t *outptr;
469
James Smart2e0fef82007-06-17 19:56:36 -0500470 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500471 for (i = 0; i < 12; i++) {
472 status = *outptr++;
473 j = ((status & 0xf0) >> 4);
474 if (j <= 9)
475 phba->SerialNumber[i] =
476 (char)((uint8_t) 0x30 + (uint8_t) j);
477 else
478 phba->SerialNumber[i] =
479 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
480 i++;
481 j = (status & 0xf);
482 if (j <= 9)
483 phba->SerialNumber[i] =
484 (char)((uint8_t) 0x30 + (uint8_t) j);
485 else
486 phba->SerialNumber[i] =
487 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
488 }
489 }
490
dea31012005-04-17 16:05:31 -0500491 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500492 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500493 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500494 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400495 "0453 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500496 "READ_CONFIG, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500497 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500498 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500499 mempool_free( pmb, phba->mbox_mem_pool);
500 return -EIO;
501 }
502
James Smarta0c87cb2009-07-19 10:01:10 -0400503 /* Check if the port is disabled */
504 lpfc_sli_read_link_ste(phba);
505
dea31012005-04-17 16:05:31 -0500506 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -0400507 i = (mb->un.varRdConfig.max_xri + 1);
508 if (phba->cfg_hba_queue_depth > i) {
509 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
510 "3359 HBA queue depth changed from %d to %d\n",
511 phba->cfg_hba_queue_depth, i);
512 phba->cfg_hba_queue_depth = i;
513 }
514
515 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
516 i = (mb->un.varRdConfig.max_xri >> 3);
517 if (phba->pport->cfg_lun_queue_depth > i) {
518 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
519 "3360 LUN queue depth changed from %d to %d\n",
520 phba->pport->cfg_lun_queue_depth, i);
521 phba->pport->cfg_lun_queue_depth = i;
522 }
dea31012005-04-17 16:05:31 -0500523
524 phba->lmt = mb->un.varRdConfig.lmt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500525
526 /* Get the default values for Model Name and Description */
527 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
528
James Smart2e0fef82007-06-17 19:56:36 -0500529 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500530
James Smart0b727fe2007-10-27 13:37:25 -0400531 /* Only process IOCBs on ELS ring till hba_state is READY */
James Smart895427b2017-02-12 13:52:30 -0800532 if (psli->sli3_ring[LPFC_EXTRA_RING].sli.sli3.cmdringaddr)
533 psli->sli3_ring[LPFC_EXTRA_RING].flag |= LPFC_STOP_IOCB_EVENT;
534 if (psli->sli3_ring[LPFC_FCP_RING].sli.sli3.cmdringaddr)
535 psli->sli3_ring[LPFC_FCP_RING].flag |= LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500536
537 /* Post receive buffers for desired rings */
James Smarted957682007-06-17 19:56:37 -0500538 if (phba->sli_rev != 3)
539 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500540
James Smart93996272008-08-24 21:50:30 -0400541 /*
542 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
543 */
544 if (phba->intr_type == MSIX) {
545 rc = lpfc_config_msi(phba, pmb);
546 if (rc) {
547 mempool_free(pmb, phba->mbox_mem_pool);
548 return -EIO;
549 }
550 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
551 if (rc != MBX_SUCCESS) {
552 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
553 "0352 Config MSI mailbox command "
554 "failed, mbxCmd x%x, mbxStatus x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400555 pmb->u.mb.mbxCommand,
556 pmb->u.mb.mbxStatus);
James Smart93996272008-08-24 21:50:30 -0400557 mempool_free(pmb, phba->mbox_mem_pool);
558 return -EIO;
559 }
560 }
561
James Smart04c68492009-05-22 14:52:52 -0400562 spin_lock_irq(&phba->hbalock);
James Smart93996272008-08-24 21:50:30 -0400563 /* Initialize ERATT handling flag */
564 phba->hba_flag &= ~HBA_ERATT_HANDLED;
565
dea31012005-04-17 16:05:31 -0500566 /* Enable appropriate host interrupts */
James Smart9940b972011-03-11 16:06:12 -0500567 if (lpfc_readl(phba->HCregaddr, &status)) {
568 spin_unlock_irq(&phba->hbalock);
569 return -EIO;
570 }
dea31012005-04-17 16:05:31 -0500571 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
572 if (psli->num_rings > 0)
573 status |= HC_R0INT_ENA;
574 if (psli->num_rings > 1)
575 status |= HC_R1INT_ENA;
576 if (psli->num_rings > 2)
577 status |= HC_R2INT_ENA;
578 if (psli->num_rings > 3)
579 status |= HC_R3INT_ENA;
580
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500581 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
582 (phba->cfg_poll & DISABLE_FCP_RING_INT))
James Smart93996272008-08-24 21:50:30 -0400583 status &= ~(HC_R0INT_ENA);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500584
dea31012005-04-17 16:05:31 -0500585 writel(status, phba->HCregaddr);
586 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500587 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500588
James Smart93996272008-08-24 21:50:30 -0400589 /* Set up ring-0 (ELS) timer */
590 timeout = phba->fc_ratov * 2;
James Smart256ec0d2013-04-17 20:14:58 -0400591 mod_timer(&vport->els_tmofunc,
592 jiffies + msecs_to_jiffies(1000 * timeout));
James Smart93996272008-08-24 21:50:30 -0400593 /* Set up heart beat (HB) timer */
James Smart256ec0d2013-04-17 20:14:58 -0400594 mod_timer(&phba->hb_tmofunc,
595 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -0500596 phba->hb_outstanding = 0;
597 phba->last_completion_time = jiffies;
James Smart93996272008-08-24 21:50:30 -0400598 /* Set up error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -0400599 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -0700600 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
dea31012005-04-17 16:05:31 -0500601
James Smarta0c87cb2009-07-19 10:01:10 -0400602 if (phba->hba_flag & LINK_DISABLED) {
603 lpfc_printf_log(phba,
604 KERN_ERR, LOG_INIT,
605 "2598 Adapter Link is disabled.\n");
606 lpfc_down_link(phba, pmb);
607 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
608 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
609 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
610 lpfc_printf_log(phba,
611 KERN_ERR, LOG_INIT,
612 "2599 Adapter failed to issue DOWN_LINK"
613 " mbox command rc 0x%x\n", rc);
614
615 mempool_free(pmb, phba->mbox_mem_pool);
616 return -EIO;
617 }
James Smarte40a02c2010-02-26 14:13:54 -0500618 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart026abb82011-12-13 13:20:45 -0500619 mempool_free(pmb, phba->mbox_mem_pool);
620 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
621 if (rc)
622 return rc;
dea31012005-04-17 16:05:31 -0500623 }
624 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400625 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500626 if (!pmb) {
627 phba->link_state = LPFC_HBA_ERROR;
628 return -ENOMEM;
629 }
630
James Smart57127f12007-10-27 13:37:05 -0400631 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
632 pmb->mbox_cmpl = lpfc_config_async_cmpl;
633 pmb->vport = phba->pport;
634 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500635
James Smart57127f12007-10-27 13:37:05 -0400636 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
637 lpfc_printf_log(phba,
638 KERN_ERR,
639 LOG_INIT,
640 "0456 Adapter failed to issue "
James Smarte4e74272009-07-19 10:01:38 -0400641 "ASYNCEVT_ENABLE mbox status x%x\n",
James Smart57127f12007-10-27 13:37:05 -0400642 rc);
643 mempool_free(pmb, phba->mbox_mem_pool);
644 }
James Smart97207482008-12-04 22:39:19 -0500645
646 /* Get Option rom version */
647 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500648 if (!pmb) {
649 phba->link_state = LPFC_HBA_ERROR;
650 return -ENOMEM;
651 }
652
James Smart97207482008-12-04 22:39:19 -0500653 lpfc_dump_wakeup_param(phba, pmb);
654 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
655 pmb->vport = phba->pport;
656 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
657
658 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
659 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
James Smarte4e74272009-07-19 10:01:38 -0400660 "to get Option ROM version status x%x\n", rc);
James Smart97207482008-12-04 22:39:19 -0500661 mempool_free(pmb, phba->mbox_mem_pool);
662 }
663
James Smartd7c255b2008-08-24 21:50:00 -0400664 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400665}
666
James Smarte59058c2008-08-24 21:49:00 -0400667/**
James Smart84d1b002010-02-12 14:42:33 -0500668 * lpfc_hba_init_link - Initialize the FC link
669 * @phba: pointer to lpfc hba data structure.
James Smart6e7288d2010-06-07 15:23:35 -0400670 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500671 *
672 * This routine will issue the INIT_LINK mailbox command call.
673 * It is available to other drivers through the lpfc_hba data
674 * structure for use as a delayed link up mechanism with the
675 * module parameter lpfc_suppress_link_up.
676 *
677 * Return code
678 * 0 - success
679 * Any other value - error
680 **/
Rashika Kheriae399b222014-09-03 12:55:28 -0400681static int
James Smart6e7288d2010-06-07 15:23:35 -0400682lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500683{
James Smart1b511972011-12-13 13:23:09 -0500684 return lpfc_hba_init_link_fc_topology(phba, phba->cfg_topology, flag);
685}
686
687/**
688 * lpfc_hba_init_link_fc_topology - Initialize FC link with desired topology
689 * @phba: pointer to lpfc hba data structure.
690 * @fc_topology: desired fc topology.
691 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
692 *
693 * This routine will issue the INIT_LINK mailbox command call.
694 * It is available to other drivers through the lpfc_hba data
695 * structure for use as a delayed link up mechanism with the
696 * module parameter lpfc_suppress_link_up.
697 *
698 * Return code
699 * 0 - success
700 * Any other value - error
701 **/
702int
703lpfc_hba_init_link_fc_topology(struct lpfc_hba *phba, uint32_t fc_topology,
704 uint32_t flag)
705{
James Smart84d1b002010-02-12 14:42:33 -0500706 struct lpfc_vport *vport = phba->pport;
707 LPFC_MBOXQ_t *pmb;
708 MAILBOX_t *mb;
709 int rc;
710
711 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
712 if (!pmb) {
713 phba->link_state = LPFC_HBA_ERROR;
714 return -ENOMEM;
715 }
716 mb = &pmb->u.mb;
717 pmb->vport = vport;
718
James Smart026abb82011-12-13 13:20:45 -0500719 if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_MAX) ||
720 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G) &&
721 !(phba->lmt & LMT_1Gb)) ||
722 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G) &&
723 !(phba->lmt & LMT_2Gb)) ||
724 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G) &&
725 !(phba->lmt & LMT_4Gb)) ||
726 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G) &&
727 !(phba->lmt & LMT_8Gb)) ||
728 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G) &&
729 !(phba->lmt & LMT_10Gb)) ||
730 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G) &&
James Smartd38dd522015-08-31 16:48:17 -0400731 !(phba->lmt & LMT_16Gb)) ||
732 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_32G) &&
733 !(phba->lmt & LMT_32Gb))) {
James Smart026abb82011-12-13 13:20:45 -0500734 /* Reset link speed to auto */
735 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
736 "1302 Invalid speed for this board:%d "
737 "Reset link speed to auto.\n",
738 phba->cfg_link_speed);
739 phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
740 }
James Smart1b511972011-12-13 13:23:09 -0500741 lpfc_init_link(phba, pmb, fc_topology, phba->cfg_link_speed);
James Smart84d1b002010-02-12 14:42:33 -0500742 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart1b511972011-12-13 13:23:09 -0500743 if (phba->sli_rev < LPFC_SLI_REV4)
744 lpfc_set_loopback_flag(phba);
James Smart6e7288d2010-06-07 15:23:35 -0400745 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart76a95d72010-11-20 23:11:48 -0500746 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
James Smart84d1b002010-02-12 14:42:33 -0500747 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
748 "0498 Adapter failed to init, mbxCmd x%x "
749 "INIT_LINK, mbxStatus x%x\n",
750 mb->mbxCommand, mb->mbxStatus);
James Smart76a95d72010-11-20 23:11:48 -0500751 if (phba->sli_rev <= LPFC_SLI_REV3) {
752 /* Clear all interrupt enable conditions */
753 writel(0, phba->HCregaddr);
754 readl(phba->HCregaddr); /* flush */
755 /* Clear all pending interrupts */
756 writel(0xffffffff, phba->HAregaddr);
757 readl(phba->HAregaddr); /* flush */
758 }
James Smart84d1b002010-02-12 14:42:33 -0500759 phba->link_state = LPFC_HBA_ERROR;
James Smart6e7288d2010-06-07 15:23:35 -0400760 if (rc != MBX_BUSY || flag == MBX_POLL)
James Smart84d1b002010-02-12 14:42:33 -0500761 mempool_free(pmb, phba->mbox_mem_pool);
762 return -EIO;
763 }
James Smarte40a02c2010-02-26 14:13:54 -0500764 phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
James Smart6e7288d2010-06-07 15:23:35 -0400765 if (flag == MBX_POLL)
766 mempool_free(pmb, phba->mbox_mem_pool);
James Smart84d1b002010-02-12 14:42:33 -0500767
768 return 0;
769}
770
771/**
772 * lpfc_hba_down_link - this routine downs the FC link
James Smart6e7288d2010-06-07 15:23:35 -0400773 * @phba: pointer to lpfc hba data structure.
774 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500775 *
776 * This routine will issue the DOWN_LINK mailbox command call.
777 * It is available to other drivers through the lpfc_hba data
778 * structure for use to stop the link.
779 *
780 * Return code
781 * 0 - success
782 * Any other value - error
783 **/
Rashika Kheriae399b222014-09-03 12:55:28 -0400784static int
James Smart6e7288d2010-06-07 15:23:35 -0400785lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500786{
787 LPFC_MBOXQ_t *pmb;
788 int rc;
789
790 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
791 if (!pmb) {
792 phba->link_state = LPFC_HBA_ERROR;
793 return -ENOMEM;
794 }
795
796 lpfc_printf_log(phba,
797 KERN_ERR, LOG_INIT,
798 "0491 Adapter Link is disabled.\n");
799 lpfc_down_link(phba, pmb);
800 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6e7288d2010-06-07 15:23:35 -0400801 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart84d1b002010-02-12 14:42:33 -0500802 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
803 lpfc_printf_log(phba,
804 KERN_ERR, LOG_INIT,
805 "2522 Adapter failed to issue DOWN_LINK"
806 " mbox command rc 0x%x\n", rc);
807
808 mempool_free(pmb, phba->mbox_mem_pool);
809 return -EIO;
810 }
James Smart6e7288d2010-06-07 15:23:35 -0400811 if (flag == MBX_POLL)
812 mempool_free(pmb, phba->mbox_mem_pool);
813
James Smart84d1b002010-02-12 14:42:33 -0500814 return 0;
815}
816
817/**
James Smart3621a712009-04-06 18:47:14 -0400818 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400819 * @phba: pointer to lpfc HBA data structure.
820 *
821 * This routine will do LPFC uninitialization before the HBA is reset when
822 * bringing down the SLI Layer.
823 *
824 * Return codes
825 * 0 - success.
826 * Any other value - error.
827 **/
dea31012005-04-17 16:05:31 -0500828int
James Smart2e0fef82007-06-17 19:56:36 -0500829lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500830{
James Smart1b32f6a2008-02-08 18:49:39 -0500831 struct lpfc_vport **vports;
832 int i;
James Smart3772a992009-05-22 14:50:54 -0400833
834 if (phba->sli_rev <= LPFC_SLI_REV3) {
835 /* Disable interrupts */
836 writel(0, phba->HCregaddr);
837 readl(phba->HCregaddr); /* flush */
838 }
dea31012005-04-17 16:05:31 -0500839
James Smart1b32f6a2008-02-08 18:49:39 -0500840 if (phba->pport->load_flag & FC_UNLOADING)
841 lpfc_cleanup_discovery_resources(phba->pport);
842 else {
843 vports = lpfc_create_vport_work_array(phba);
844 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400845 for (i = 0; i <= phba->max_vports &&
846 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500847 lpfc_cleanup_discovery_resources(vports[i]);
848 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500849 }
850 return 0;
dea31012005-04-17 16:05:31 -0500851}
852
James Smarte59058c2008-08-24 21:49:00 -0400853/**
James Smart68e814f2014-05-21 08:04:59 -0400854 * lpfc_sli4_free_sp_events - Cleanup sp_queue_events to free
855 * rspiocb which got deferred
856 *
857 * @phba: pointer to lpfc HBA data structure.
858 *
859 * This routine will cleanup completed slow path events after HBA is reset
860 * when bringing down the SLI Layer.
861 *
862 *
863 * Return codes
864 * void.
865 **/
866static void
867lpfc_sli4_free_sp_events(struct lpfc_hba *phba)
868{
869 struct lpfc_iocbq *rspiocbq;
870 struct hbq_dmabuf *dmabuf;
871 struct lpfc_cq_event *cq_event;
872
873 spin_lock_irq(&phba->hbalock);
874 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
875 spin_unlock_irq(&phba->hbalock);
876
877 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
878 /* Get the response iocb from the head of work queue */
879 spin_lock_irq(&phba->hbalock);
880 list_remove_head(&phba->sli4_hba.sp_queue_event,
881 cq_event, struct lpfc_cq_event, list);
882 spin_unlock_irq(&phba->hbalock);
883
884 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
885 case CQE_CODE_COMPL_WQE:
886 rspiocbq = container_of(cq_event, struct lpfc_iocbq,
887 cq_event);
888 lpfc_sli_release_iocbq(phba, rspiocbq);
889 break;
890 case CQE_CODE_RECEIVE:
891 case CQE_CODE_RECEIVE_V1:
892 dmabuf = container_of(cq_event, struct hbq_dmabuf,
893 cq_event);
894 lpfc_in_buf_free(phba, &dmabuf->dbuf);
895 }
896 }
897}
898
899/**
James Smartbcece5f2014-04-04 13:51:34 -0400900 * lpfc_hba_free_post_buf - Perform lpfc uninitialization after HBA reset
901 * @phba: pointer to lpfc HBA data structure.
902 *
903 * This routine will cleanup posted ELS buffers after the HBA is reset
904 * when bringing down the SLI Layer.
905 *
906 *
907 * Return codes
908 * void.
909 **/
910static void
911lpfc_hba_free_post_buf(struct lpfc_hba *phba)
912{
913 struct lpfc_sli *psli = &phba->sli;
914 struct lpfc_sli_ring *pring;
915 struct lpfc_dmabuf *mp, *next_mp;
James Smart07eab622014-04-04 13:51:44 -0400916 LIST_HEAD(buflist);
917 int count;
James Smartbcece5f2014-04-04 13:51:34 -0400918
919 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
920 lpfc_sli_hbqbuf_free_all(phba);
921 else {
922 /* Cleanup preposted buffers on the ELS ring */
James Smart895427b2017-02-12 13:52:30 -0800923 pring = &psli->sli3_ring[LPFC_ELS_RING];
James Smart07eab622014-04-04 13:51:44 -0400924 spin_lock_irq(&phba->hbalock);
925 list_splice_init(&pring->postbufq, &buflist);
926 spin_unlock_irq(&phba->hbalock);
927
928 count = 0;
929 list_for_each_entry_safe(mp, next_mp, &buflist, list) {
James Smartbcece5f2014-04-04 13:51:34 -0400930 list_del(&mp->list);
James Smart07eab622014-04-04 13:51:44 -0400931 count++;
James Smartbcece5f2014-04-04 13:51:34 -0400932 lpfc_mbuf_free(phba, mp->virt, mp->phys);
933 kfree(mp);
934 }
James Smart07eab622014-04-04 13:51:44 -0400935
936 spin_lock_irq(&phba->hbalock);
937 pring->postbufq_cnt -= count;
James Smartbcece5f2014-04-04 13:51:34 -0400938 spin_unlock_irq(&phba->hbalock);
939 }
940}
941
942/**
943 * lpfc_hba_clean_txcmplq - Perform lpfc uninitialization after HBA reset
944 * @phba: pointer to lpfc HBA data structure.
945 *
946 * This routine will cleanup the txcmplq after the HBA is reset when bringing
947 * down the SLI Layer.
948 *
949 * Return codes
950 * void
951 **/
952static void
953lpfc_hba_clean_txcmplq(struct lpfc_hba *phba)
954{
955 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -0800956 struct lpfc_queue *qp = NULL;
James Smartbcece5f2014-04-04 13:51:34 -0400957 struct lpfc_sli_ring *pring;
958 LIST_HEAD(completions);
959 int i;
960
James Smart895427b2017-02-12 13:52:30 -0800961 if (phba->sli_rev != LPFC_SLI_REV4) {
962 for (i = 0; i < psli->num_rings; i++) {
963 pring = &psli->sli3_ring[i];
James Smartbcece5f2014-04-04 13:51:34 -0400964 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -0800965 /* At this point in time the HBA is either reset or DOA
966 * Nothing should be on txcmplq as it will
967 * NEVER complete.
968 */
969 list_splice_init(&pring->txcmplq, &completions);
970 pring->txcmplq_cnt = 0;
James Smartbcece5f2014-04-04 13:51:34 -0400971 spin_unlock_irq(&phba->hbalock);
972
James Smart895427b2017-02-12 13:52:30 -0800973 lpfc_sli_abort_iocb_ring(phba, pring);
974 }
James Smartbcece5f2014-04-04 13:51:34 -0400975 /* Cancel all the IOCBs from the completions list */
James Smart895427b2017-02-12 13:52:30 -0800976 lpfc_sli_cancel_iocbs(phba, &completions,
977 IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
978 return;
979 }
980 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
981 pring = qp->pring;
982 if (!pring)
983 continue;
984 spin_lock_irq(&pring->ring_lock);
985 list_splice_init(&pring->txcmplq, &completions);
986 pring->txcmplq_cnt = 0;
987 spin_unlock_irq(&pring->ring_lock);
James Smartbcece5f2014-04-04 13:51:34 -0400988 lpfc_sli_abort_iocb_ring(phba, pring);
989 }
James Smart895427b2017-02-12 13:52:30 -0800990 /* Cancel all the IOCBs from the completions list */
991 lpfc_sli_cancel_iocbs(phba, &completions,
992 IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
James Smartbcece5f2014-04-04 13:51:34 -0400993}
994
995/**
James Smart3772a992009-05-22 14:50:54 -0400996 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smartbcece5f2014-04-04 13:51:34 -0400997 int i;
James Smarte59058c2008-08-24 21:49:00 -0400998 * @phba: pointer to lpfc HBA data structure.
999 *
1000 * This routine will do uninitialization after the HBA is reset when bring
1001 * down the SLI Layer.
1002 *
1003 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001004 * 0 - success.
James Smarte59058c2008-08-24 21:49:00 -04001005 * Any other value - error.
1006 **/
James Smart3772a992009-05-22 14:50:54 -04001007static int
1008lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05001009{
James Smartbcece5f2014-04-04 13:51:34 -04001010 lpfc_hba_free_post_buf(phba);
1011 lpfc_hba_clean_txcmplq(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001012 return 0;
1013}
James Smart5af5eee2010-10-22 11:06:38 -04001014
James Smartda0436e2009-05-22 14:51:39 -04001015/**
1016 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
1017 * @phba: pointer to lpfc HBA data structure.
1018 *
1019 * This routine will do uninitialization after the HBA is reset when bring
1020 * down the SLI Layer.
1021 *
1022 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001023 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001024 * Any other value - error.
1025 **/
1026static int
1027lpfc_hba_down_post_s4(struct lpfc_hba *phba)
1028{
1029 struct lpfc_scsi_buf *psb, *psb_next;
James Smart86c67372017-04-21 16:05:04 -07001030 struct lpfc_nvmet_rcv_ctx *ctxp, *ctxp_next;
James Smartda0436e2009-05-22 14:51:39 -04001031 LIST_HEAD(aborts);
James Smart895427b2017-02-12 13:52:30 -08001032 LIST_HEAD(nvme_aborts);
James Smart86c67372017-04-21 16:05:04 -07001033 LIST_HEAD(nvmet_aborts);
James Smartda0436e2009-05-22 14:51:39 -04001034 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -05001035 struct lpfc_sglq *sglq_entry = NULL;
1036
James Smart895427b2017-02-12 13:52:30 -08001037
1038 lpfc_sli_hbqbuf_free_all(phba);
James Smartbcece5f2014-04-04 13:51:34 -04001039 lpfc_hba_clean_txcmplq(phba);
1040
James Smartda0436e2009-05-22 14:51:39 -04001041 /* At this point in time the HBA is either reset or DOA. Either
1042 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
James Smart895427b2017-02-12 13:52:30 -08001043 * on the lpfc_els_sgl_list so that it can either be freed if the
James Smartda0436e2009-05-22 14:51:39 -04001044 * driver is unloading or reposted if the driver is restarting
1045 * the port.
1046 */
James Smart895427b2017-02-12 13:52:30 -08001047 spin_lock_irq(&phba->hbalock); /* required for lpfc_els_sgl_list and */
James Smartda0436e2009-05-22 14:51:39 -04001048 /* scsl_buf_list */
James Smart895427b2017-02-12 13:52:30 -08001049 /* sgl_list_lock required because worker thread uses this
James Smartda0436e2009-05-22 14:51:39 -04001050 * list.
1051 */
James Smart895427b2017-02-12 13:52:30 -08001052 spin_lock(&phba->sli4_hba.sgl_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -05001053 list_for_each_entry(sglq_entry,
1054 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
1055 sglq_entry->state = SGL_FREED;
1056
James Smartda0436e2009-05-22 14:51:39 -04001057 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
James Smart895427b2017-02-12 13:52:30 -08001058 &phba->sli4_hba.lpfc_els_sgl_list);
1059
James Smartf358dd02017-02-12 13:52:34 -08001060
James Smart895427b2017-02-12 13:52:30 -08001061 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04001062 /* abts_scsi_buf_list_lock required because worker thread uses this
1063 * list.
1064 */
James Smart895427b2017-02-12 13:52:30 -08001065 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
1066 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
1067 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
1068 &aborts);
1069 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
1070 }
1071
1072 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
1073 spin_lock(&phba->sli4_hba.abts_nvme_buf_list_lock);
1074 list_splice_init(&phba->sli4_hba.lpfc_abts_nvme_buf_list,
1075 &nvme_aborts);
James Smart86c67372017-04-21 16:05:04 -07001076 list_splice_init(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
1077 &nvmet_aborts);
James Smart895427b2017-02-12 13:52:30 -08001078 spin_unlock(&phba->sli4_hba.abts_nvme_buf_list_lock);
1079 }
1080
James Smartda0436e2009-05-22 14:51:39 -04001081 spin_unlock_irq(&phba->hbalock);
1082
1083 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
1084 psb->pCmd = NULL;
1085 psb->status = IOSTAT_SUCCESS;
1086 }
James Smarta40fc5f2013-04-17 20:17:40 -04001087 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
1088 list_splice(&aborts, &phba->lpfc_scsi_buf_list_put);
1089 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
James Smart68e814f2014-05-21 08:04:59 -04001090
James Smart86c67372017-04-21 16:05:04 -07001091 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
1092 list_for_each_entry_safe(psb, psb_next, &nvme_aborts, list) {
1093 psb->pCmd = NULL;
1094 psb->status = IOSTAT_SUCCESS;
1095 }
1096 spin_lock_irqsave(&phba->nvme_buf_list_put_lock, iflag);
1097 list_splice(&nvme_aborts, &phba->lpfc_nvme_buf_list_put);
1098 spin_unlock_irqrestore(&phba->nvme_buf_list_put_lock, iflag);
1099
1100 list_for_each_entry_safe(ctxp, ctxp_next, &nvmet_aborts, list) {
1101 ctxp->flag &= ~(LPFC_NVMET_XBUSY | LPFC_NVMET_ABORT_OP);
James Smart6c621a22017-05-15 15:20:45 -07001102 lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
James Smart86c67372017-04-21 16:05:04 -07001103 }
James Smart895427b2017-02-12 13:52:30 -08001104 }
James Smart895427b2017-02-12 13:52:30 -08001105
James Smart68e814f2014-05-21 08:04:59 -04001106 lpfc_sli4_free_sp_events(phba);
James Smartda0436e2009-05-22 14:51:39 -04001107 return 0;
1108}
1109
1110/**
1111 * lpfc_hba_down_post - Wrapper func for hba down post routine
1112 * @phba: pointer to lpfc HBA data structure.
1113 *
1114 * This routine wraps the actual SLI3 or SLI4 routine for performing
1115 * uninitialization after the HBA is reset when bring down the SLI Layer.
1116 *
1117 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001118 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001119 * Any other value - error.
1120 **/
1121int
1122lpfc_hba_down_post(struct lpfc_hba *phba)
1123{
1124 return (*phba->lpfc_hba_down_post)(phba);
1125}
Jamie Wellnitz41415862006-02-28 19:25:27 -05001126
James Smarte59058c2008-08-24 21:49:00 -04001127/**
James Smart3621a712009-04-06 18:47:14 -04001128 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001129 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1130 *
1131 * This is the HBA-timer timeout handler registered to the lpfc driver. When
1132 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
1133 * work-port-events bitmap and the worker thread is notified. This timeout
1134 * event will be used by the worker thread to invoke the actual timeout
1135 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
1136 * be performed in the timeout handler and the HBA timeout event bit shall
1137 * be cleared by the worker thread after it has taken the event bitmap out.
1138 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001139static void
James Smart858c9f62007-06-17 19:56:39 -05001140lpfc_hb_timeout(unsigned long ptr)
1141{
1142 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -04001143 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -05001144 unsigned long iflag;
1145
1146 phba = (struct lpfc_hba *)ptr;
James Smart93996272008-08-24 21:50:30 -04001147
1148 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -05001149 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04001150 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
1151 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -05001152 phba->pport->work_port_events |= WORKER_HB_TMO;
1153 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
1154
James Smart93996272008-08-24 21:50:30 -04001155 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -04001156 if (!tmo_posted)
1157 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -05001158 return;
1159}
1160
James Smarte59058c2008-08-24 21:49:00 -04001161/**
James Smart19ca7602010-11-20 23:11:55 -05001162 * lpfc_rrq_timeout - The RRQ-timer timeout handler
1163 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1164 *
1165 * This is the RRQ-timer timeout handler registered to the lpfc driver. When
1166 * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
1167 * work-port-events bitmap and the worker thread is notified. This timeout
1168 * event will be used by the worker thread to invoke the actual timeout
1169 * handler routine, lpfc_rrq_handler. Any periodical operations will
1170 * be performed in the timeout handler and the RRQ timeout event bit shall
1171 * be cleared by the worker thread after it has taken the event bitmap out.
1172 **/
1173static void
1174lpfc_rrq_timeout(unsigned long ptr)
1175{
1176 struct lpfc_hba *phba;
James Smart19ca7602010-11-20 23:11:55 -05001177 unsigned long iflag;
1178
1179 phba = (struct lpfc_hba *)ptr;
1180 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001181 if (!(phba->pport->load_flag & FC_UNLOADING))
1182 phba->hba_flag |= HBA_RRQ_ACTIVE;
1183 else
1184 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart19ca7602010-11-20 23:11:55 -05001185 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001186
1187 if (!(phba->pport->load_flag & FC_UNLOADING))
1188 lpfc_worker_wake_up(phba);
James Smart19ca7602010-11-20 23:11:55 -05001189}
1190
1191/**
James Smart3621a712009-04-06 18:47:14 -04001192 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -04001193 * @phba: pointer to lpfc hba data structure.
1194 * @pmboxq: pointer to the driver internal queue element for mailbox command.
1195 *
1196 * This is the callback function to the lpfc heart-beat mailbox command.
1197 * If configured, the lpfc driver issues the heart-beat mailbox command to
1198 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
1199 * heart-beat mailbox command is issued, the driver shall set up heart-beat
1200 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
1201 * heart-beat outstanding state. Once the mailbox command comes back and
1202 * no error conditions detected, the heart-beat mailbox command timer is
1203 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1204 * state is cleared for the next heart-beat. If the timer expired with the
1205 * heart-beat outstanding state set, the driver will put the HBA offline.
1206 **/
James Smart858c9f62007-06-17 19:56:39 -05001207static void
1208lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1209{
1210 unsigned long drvr_flag;
1211
1212 spin_lock_irqsave(&phba->hbalock, drvr_flag);
1213 phba->hb_outstanding = 0;
1214 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1215
James Smart93996272008-08-24 21:50:30 -04001216 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -05001217 mempool_free(pmboxq, phba->mbox_mem_pool);
1218 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1219 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -04001220 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -05001221 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001222 jiffies +
1223 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001224 return;
1225}
1226
James Smarte59058c2008-08-24 21:49:00 -04001227/**
James Smart3621a712009-04-06 18:47:14 -04001228 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001229 * @phba: pointer to lpfc hba data structure.
1230 *
1231 * This is the actual HBA-timer timeout handler to be invoked by the worker
1232 * thread whenever the HBA timer fired and HBA-timeout event posted. This
1233 * handler performs any periodic operations needed for the device. If such
1234 * periodic event has already been attended to either in the interrupt handler
1235 * or by processing slow-ring or fast-ring events within the HBA-timer
1236 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1237 * the timer for the next timeout period. If lpfc heart-beat mailbox command
1238 * is configured and there is no heart-beat mailbox command outstanding, a
1239 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1240 * has been a heart-beat mailbox command outstanding, the HBA shall be put
1241 * to offline.
1242 **/
James Smart858c9f62007-06-17 19:56:39 -05001243void
1244lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1245{
James Smart45ed1192009-10-02 15:17:02 -04001246 struct lpfc_vport **vports;
James Smart858c9f62007-06-17 19:56:39 -05001247 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -05001248 struct lpfc_dmabuf *buf_ptr;
James Smart45ed1192009-10-02 15:17:02 -04001249 int retval, i;
James Smart858c9f62007-06-17 19:56:39 -05001250 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -05001251 LIST_HEAD(completions);
James Smart0cf07f842017-06-01 21:07:10 -07001252 struct lpfc_queue *qp;
1253 unsigned long time_elapsed;
1254 uint32_t tick_cqe, max_cqe, val;
1255 uint64_t tot, data1, data2, data3;
Dick Kennedy66d7ce92017-08-23 16:55:42 -07001256 struct lpfc_nvmet_tgtport *tgtp;
James Smart0cf07f842017-06-01 21:07:10 -07001257 struct lpfc_register reg_data;
1258 void __iomem *eqdreg = phba->sli4_hba.u.if_type2.EQDregaddr;
James Smart858c9f62007-06-17 19:56:39 -05001259
James Smart45ed1192009-10-02 15:17:02 -04001260 vports = lpfc_create_vport_work_array(phba);
1261 if (vports != NULL)
James Smart4258e982015-12-16 18:11:58 -05001262 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart45ed1192009-10-02 15:17:02 -04001263 lpfc_rcv_seq_check_edtov(vports[i]);
James Smart4258e982015-12-16 18:11:58 -05001264 lpfc_fdmi_num_disc_check(vports[i]);
1265 }
James Smart45ed1192009-10-02 15:17:02 -04001266 lpfc_destroy_vport_work_array(phba, vports);
1267
James Smart858c9f62007-06-17 19:56:39 -05001268 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -04001269 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -05001270 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1271 return;
1272
James Smart0cf07f842017-06-01 21:07:10 -07001273 if (phba->cfg_auto_imax) {
1274 if (!phba->last_eqdelay_time) {
1275 phba->last_eqdelay_time = jiffies;
1276 goto skip_eqdelay;
1277 }
1278 time_elapsed = jiffies - phba->last_eqdelay_time;
1279 phba->last_eqdelay_time = jiffies;
1280
1281 tot = 0xffff;
1282 /* Check outstanding IO count */
1283 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
1284 if (phba->nvmet_support) {
Dick Kennedy66d7ce92017-08-23 16:55:42 -07001285 tgtp = phba->targetport->private;
1286 /* Calculate outstanding IOs */
1287 tot = atomic_read(&tgtp->rcv_fcp_cmd_drop);
1288 tot += atomic_read(&tgtp->xmt_fcp_release);
1289 tot = atomic_read(&tgtp->rcv_fcp_cmd_in) - tot;
James Smart0cf07f842017-06-01 21:07:10 -07001290 } else {
1291 tot = atomic_read(&phba->fc4NvmeIoCmpls);
1292 data1 = atomic_read(
1293 &phba->fc4NvmeInputRequests);
1294 data2 = atomic_read(
1295 &phba->fc4NvmeOutputRequests);
1296 data3 = atomic_read(
1297 &phba->fc4NvmeControlRequests);
1298 tot = (data1 + data2 + data3) - tot;
1299 }
1300 }
1301
1302 /* Interrupts per sec per EQ */
1303 val = phba->cfg_fcp_imax / phba->io_channel_irqs;
1304 tick_cqe = val / CONFIG_HZ; /* Per tick per EQ */
1305
1306 /* Assume 1 CQE/ISR, calc max CQEs allowed for time duration */
1307 max_cqe = time_elapsed * tick_cqe;
1308
1309 for (i = 0; i < phba->io_channel_irqs; i++) {
1310 /* Fast-path EQ */
1311 qp = phba->sli4_hba.hba_eq[i];
1312 if (!qp)
1313 continue;
1314
1315 /* Use no EQ delay if we don't have many outstanding
1316 * IOs, or if we are only processing 1 CQE/ISR or less.
1317 * Otherwise, assume we can process up to lpfc_fcp_imax
1318 * interrupts per HBA.
1319 */
1320 if (tot < LPFC_NODELAY_MAX_IO ||
1321 qp->EQ_cqe_cnt <= max_cqe)
1322 val = 0;
1323 else
1324 val = phba->cfg_fcp_imax;
1325
1326 if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
1327 /* Use EQ Delay Register method */
1328
1329 /* Convert for EQ Delay register */
1330 if (val) {
1331 /* First, interrupts per sec per EQ */
1332 val = phba->cfg_fcp_imax /
1333 phba->io_channel_irqs;
1334
1335 /* us delay between each interrupt */
1336 val = LPFC_SEC_TO_USEC / val;
1337 }
1338 if (val != qp->q_mode) {
1339 reg_data.word0 = 0;
1340 bf_set(lpfc_sliport_eqdelay_id,
1341 &reg_data, qp->queue_id);
1342 bf_set(lpfc_sliport_eqdelay_delay,
1343 &reg_data, val);
1344 writel(reg_data.word0, eqdreg);
1345 }
1346 } else {
1347 /* Use mbox command method */
1348 if (val != qp->q_mode)
1349 lpfc_modify_hba_eq_delay(phba, i,
1350 1, val);
1351 }
1352
1353 /*
1354 * val is cfg_fcp_imax or 0 for mbox delay or us delay
1355 * between interrupts for EQDR.
1356 */
1357 qp->q_mode = val;
1358 qp->EQ_cqe_cnt = 0;
1359 }
1360 }
1361
1362skip_eqdelay:
James Smart858c9f62007-06-17 19:56:39 -05001363 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -05001364
James Smart256ec0d2013-04-17 20:14:58 -04001365 if (time_after(phba->last_completion_time +
1366 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL),
1367 jiffies)) {
James Smart858c9f62007-06-17 19:56:39 -05001368 spin_unlock_irq(&phba->pport->work_port_lock);
1369 if (!phba->hb_outstanding)
1370 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001371 jiffies +
1372 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001373 else
1374 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001375 jiffies +
1376 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001377 return;
1378 }
1379 spin_unlock_irq(&phba->pport->work_port_lock);
1380
James Smart0ff10d42008-01-11 01:52:36 -05001381 if (phba->elsbuf_cnt &&
1382 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1383 spin_lock_irq(&phba->hbalock);
1384 list_splice_init(&phba->elsbuf, &completions);
1385 phba->elsbuf_cnt = 0;
1386 phba->elsbuf_prev_cnt = 0;
1387 spin_unlock_irq(&phba->hbalock);
1388
1389 while (!list_empty(&completions)) {
1390 list_remove_head(&completions, buf_ptr,
1391 struct lpfc_dmabuf, list);
1392 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1393 kfree(buf_ptr);
1394 }
1395 }
1396 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1397
James Smart858c9f62007-06-17 19:56:39 -05001398 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -05001399 if (phba->cfg_enable_hba_heartbeat) {
1400 if (!phba->hb_outstanding) {
James Smartbc739052010-08-04 16:11:18 -04001401 if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1402 (list_empty(&psli->mboxq))) {
1403 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1404 GFP_KERNEL);
1405 if (!pmboxq) {
1406 mod_timer(&phba->hb_tmofunc,
1407 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001408 msecs_to_jiffies(1000 *
1409 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001410 return;
1411 }
James Smart13815c82008-01-11 01:52:48 -05001412
James Smartbc739052010-08-04 16:11:18 -04001413 lpfc_heart_beat(phba, pmboxq);
1414 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1415 pmboxq->vport = phba->pport;
1416 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1417 MBX_NOWAIT);
James Smart13815c82008-01-11 01:52:48 -05001418
James Smartbc739052010-08-04 16:11:18 -04001419 if (retval != MBX_BUSY &&
1420 retval != MBX_SUCCESS) {
1421 mempool_free(pmboxq,
1422 phba->mbox_mem_pool);
1423 mod_timer(&phba->hb_tmofunc,
1424 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001425 msecs_to_jiffies(1000 *
1426 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001427 return;
1428 }
1429 phba->skipped_hb = 0;
1430 phba->hb_outstanding = 1;
1431 } else if (time_before_eq(phba->last_completion_time,
1432 phba->skipped_hb)) {
1433 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1434 "2857 Last completion time not "
1435 " updated in %d ms\n",
1436 jiffies_to_msecs(jiffies
1437 - phba->last_completion_time));
1438 } else
1439 phba->skipped_hb = jiffies;
1440
James Smart858c9f62007-06-17 19:56:39 -05001441 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001442 jiffies +
1443 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001444 return;
James Smart13815c82008-01-11 01:52:48 -05001445 } else {
1446 /*
1447 * If heart beat timeout called with hb_outstanding set
James Smartdcf2a4e2010-09-29 11:18:53 -04001448 * we need to give the hb mailbox cmd a chance to
1449 * complete or TMO.
James Smart13815c82008-01-11 01:52:48 -05001450 */
James Smartdcf2a4e2010-09-29 11:18:53 -04001451 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1452 "0459 Adapter heartbeat still out"
1453 "standing:last compl time was %d ms.\n",
1454 jiffies_to_msecs(jiffies
1455 - phba->last_completion_time));
1456 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001457 jiffies +
1458 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001459 }
James Smart4258e982015-12-16 18:11:58 -05001460 } else {
1461 mod_timer(&phba->hb_tmofunc,
1462 jiffies +
1463 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001464 }
1465}
1466
James Smarte59058c2008-08-24 21:49:00 -04001467/**
James Smart3621a712009-04-06 18:47:14 -04001468 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -04001469 * @phba: pointer to lpfc hba data structure.
1470 *
1471 * This routine is called to bring the HBA offline when HBA hardware error
1472 * other than Port Error 6 has been detected.
1473 **/
James Smart09372822008-01-11 01:52:54 -05001474static void
1475lpfc_offline_eratt(struct lpfc_hba *phba)
1476{
1477 struct lpfc_sli *psli = &phba->sli;
1478
1479 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001480 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -05001481 spin_unlock_irq(&phba->hbalock);
James Smart618a5232012-06-12 13:54:36 -04001482 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart09372822008-01-11 01:52:54 -05001483
1484 lpfc_offline(phba);
1485 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001486 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001487 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001488 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001489 lpfc_hba_down_post(phba);
1490 lpfc_sli_brdready(phba, HS_MBRDY);
1491 lpfc_unblock_mgmt_io(phba);
1492 phba->link_state = LPFC_HBA_ERROR;
1493 return;
1494}
1495
James Smarte59058c2008-08-24 21:49:00 -04001496/**
James Smartda0436e2009-05-22 14:51:39 -04001497 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1498 * @phba: pointer to lpfc hba data structure.
1499 *
1500 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1501 * other than Port Error 6 has been detected.
1502 **/
James Smarta88dbb62013-04-17 20:18:39 -04001503void
James Smartda0436e2009-05-22 14:51:39 -04001504lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1505{
James Smart946727d2015-04-07 15:07:09 -04001506 spin_lock_irq(&phba->hbalock);
1507 phba->link_state = LPFC_HBA_ERROR;
1508 spin_unlock_irq(&phba->hbalock);
1509
James Smart618a5232012-06-12 13:54:36 -04001510 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smartda0436e2009-05-22 14:51:39 -04001511 lpfc_offline(phba);
James Smartda0436e2009-05-22 14:51:39 -04001512 lpfc_hba_down_post(phba);
James Smartda0436e2009-05-22 14:51:39 -04001513 lpfc_unblock_mgmt_io(phba);
James Smartda0436e2009-05-22 14:51:39 -04001514}
1515
1516/**
James Smarta257bf92009-04-06 18:48:10 -04001517 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1518 * @phba: pointer to lpfc hba data structure.
1519 *
1520 * This routine is invoked to handle the deferred HBA hardware error
1521 * conditions. This type of error is indicated by HBA by setting ER1
1522 * and another ER bit in the host status register. The driver will
1523 * wait until the ER1 bit clears before handling the error condition.
1524 **/
1525static void
1526lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1527{
1528 uint32_t old_host_status = phba->work_hs;
James Smarta257bf92009-04-06 18:48:10 -04001529 struct lpfc_sli *psli = &phba->sli;
1530
James Smartf4b4c682009-05-22 14:53:12 -04001531 /* If the pci channel is offline, ignore possible errors,
1532 * since we cannot communicate with the pci card anyway.
1533 */
1534 if (pci_channel_offline(phba->pcidev)) {
1535 spin_lock_irq(&phba->hbalock);
1536 phba->hba_flag &= ~DEFER_ERATT;
1537 spin_unlock_irq(&phba->hbalock);
1538 return;
1539 }
1540
James Smarta257bf92009-04-06 18:48:10 -04001541 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1542 "0479 Deferred Adapter Hardware Error "
1543 "Data: x%x x%x x%x\n",
1544 phba->work_hs,
1545 phba->work_status[0], phba->work_status[1]);
1546
1547 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001548 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001549 spin_unlock_irq(&phba->hbalock);
1550
1551
1552 /*
1553 * Firmware stops when it triggred erratt. That could cause the I/Os
1554 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1555 * SCSI layer retry it after re-establishing link.
1556 */
James Smartdb55fba2014-04-04 13:52:02 -04001557 lpfc_sli_abort_fcp_rings(phba);
James Smarta257bf92009-04-06 18:48:10 -04001558
1559 /*
1560 * There was a firmware error. Take the hba offline and then
1561 * attempt to restart it.
1562 */
James Smart618a5232012-06-12 13:54:36 -04001563 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smarta257bf92009-04-06 18:48:10 -04001564 lpfc_offline(phba);
1565
1566 /* Wait for the ER1 bit to clear.*/
1567 while (phba->work_hs & HS_FFER1) {
1568 msleep(100);
James Smart9940b972011-03-11 16:06:12 -05001569 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1570 phba->work_hs = UNPLUG_ERR ;
1571 break;
1572 }
James Smarta257bf92009-04-06 18:48:10 -04001573 /* If driver is unloading let the worker thread continue */
1574 if (phba->pport->load_flag & FC_UNLOADING) {
1575 phba->work_hs = 0;
1576 break;
1577 }
1578 }
1579
1580 /*
1581 * This is to ptrotect against a race condition in which
1582 * first write to the host attention register clear the
1583 * host status register.
1584 */
1585 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1586 phba->work_hs = old_host_status & ~HS_FFER1;
1587
James Smart3772a992009-05-22 14:50:54 -04001588 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001589 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001590 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001591 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1592 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1593}
1594
James Smart3772a992009-05-22 14:50:54 -04001595static void
1596lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1597{
1598 struct lpfc_board_event_header board_event;
1599 struct Scsi_Host *shost;
1600
1601 board_event.event_type = FC_REG_BOARD_EVENT;
1602 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1603 shost = lpfc_shost_from_vport(phba->pport);
1604 fc_host_post_vendor_event(shost, fc_get_event_number(),
1605 sizeof(board_event),
1606 (char *) &board_event,
1607 LPFC_NL_VENDOR_ID);
1608}
1609
James Smarta257bf92009-04-06 18:48:10 -04001610/**
James Smart3772a992009-05-22 14:50:54 -04001611 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001612 * @phba: pointer to lpfc hba data structure.
1613 *
1614 * This routine is invoked to handle the following HBA hardware error
1615 * conditions:
1616 * 1 - HBA error attention interrupt
1617 * 2 - DMA ring index out of range
1618 * 3 - Mailbox command came back as unknown
1619 **/
James Smart3772a992009-05-22 14:50:54 -04001620static void
1621lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001622{
James Smart2e0fef82007-06-17 19:56:36 -05001623 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001624 struct lpfc_sli *psli = &phba->sli;
James Smartd2873e42006-08-18 17:46:43 -04001625 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001626 unsigned long temperature;
1627 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001628 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001629
Linas Vepstas8d63f372007-02-14 14:28:36 -06001630 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001631 * since we cannot communicate with the pci card anyway.
1632 */
1633 if (pci_channel_offline(phba->pcidev)) {
1634 spin_lock_irq(&phba->hbalock);
1635 phba->hba_flag &= ~DEFER_ERATT;
1636 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001637 return;
James Smart3772a992009-05-22 14:50:54 -04001638 }
1639
James Smart13815c82008-01-11 01:52:48 -05001640 /* If resets are disabled then leave the HBA alone and return */
1641 if (!phba->cfg_enable_hba_reset)
1642 return;
dea31012005-04-17 16:05:31 -05001643
James Smartea2151b2008-09-07 11:52:10 -04001644 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001645 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001646
James Smarta257bf92009-04-06 18:48:10 -04001647 if (phba->hba_flag & DEFER_ERATT)
1648 lpfc_handle_deferred_eratt(phba);
1649
James Smartdcf2a4e2010-09-29 11:18:53 -04001650 if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1651 if (phba->work_hs & HS_FFER6)
1652 /* Re-establishing Link */
1653 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1654 "1301 Re-establishing Link "
1655 "Data: x%x x%x x%x\n",
1656 phba->work_hs, phba->work_status[0],
1657 phba->work_status[1]);
1658 if (phba->work_hs & HS_FFER8)
1659 /* Device Zeroization */
1660 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1661 "2861 Host Authentication device "
1662 "zeroization Data:x%x x%x x%x\n",
1663 phba->work_hs, phba->work_status[0],
1664 phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001665
James Smart92d7f7b2007-06-17 19:56:38 -05001666 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001667 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001668 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001669
1670 /*
1671 * Firmware stops when it triggled erratt with HS_FFER6.
1672 * That could cause the I/Os dropped by the firmware.
1673 * Error iocb (I/O) on txcmplq and let the SCSI layer
1674 * retry it after re-establishing link.
1675 */
James Smartdb55fba2014-04-04 13:52:02 -04001676 lpfc_sli_abort_fcp_rings(phba);
dea31012005-04-17 16:05:31 -05001677
dea31012005-04-17 16:05:31 -05001678 /*
1679 * There was a firmware error. Take the hba offline and then
1680 * attempt to restart it.
1681 */
James Smart618a5232012-06-12 13:54:36 -04001682 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
dea31012005-04-17 16:05:31 -05001683 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001684 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001685 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001686 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001687 return;
1688 }
James Smart46fa3112007-04-25 09:51:45 -04001689 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001690 } else if (phba->work_hs & HS_CRIT_TEMP) {
1691 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1692 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1693 temp_event_data.event_code = LPFC_CRIT_TEMP;
1694 temp_event_data.data = (uint32_t)temperature;
1695
1696 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001697 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001698 "(%ld), taking this port offline "
1699 "Data: x%x x%x x%x\n",
1700 temperature, phba->work_hs,
1701 phba->work_status[0], phba->work_status[1]);
1702
1703 shost = lpfc_shost_from_vport(phba->pport);
1704 fc_host_post_vendor_event(shost, fc_get_event_number(),
1705 sizeof(temp_event_data),
1706 (char *) &temp_event_data,
1707 SCSI_NL_VID_TYPE_PCI
1708 | PCI_VENDOR_ID_EMULEX);
1709
James Smart7af67052007-10-27 13:38:11 -04001710 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001711 phba->over_temp_state = HBA_OVER_TEMP;
1712 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001713 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001714
dea31012005-04-17 16:05:31 -05001715 } else {
1716 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001717 * failure is a value other than FFER6. Do not call the offline
1718 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001719 */
1720 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001721 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001722 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001723 phba->work_hs,
dea31012005-04-17 16:05:31 -05001724 phba->work_status[0], phba->work_status[1]);
1725
James Smartd2873e42006-08-18 17:46:43 -04001726 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001727 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001728 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001729 sizeof(event_data), (char *) &event_data,
1730 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1731
James Smart09372822008-01-11 01:52:54 -05001732 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001733 }
James Smart93996272008-08-24 21:50:30 -04001734 return;
dea31012005-04-17 16:05:31 -05001735}
1736
James Smarte59058c2008-08-24 21:49:00 -04001737/**
James Smart618a5232012-06-12 13:54:36 -04001738 * lpfc_sli4_port_sta_fn_reset - The SLI4 function reset due to port status reg
1739 * @phba: pointer to lpfc hba data structure.
1740 * @mbx_action: flag for mailbox shutdown action.
1741 *
1742 * This routine is invoked to perform an SLI4 port PCI function reset in
1743 * response to port status register polling attention. It waits for port
1744 * status register (ERR, RDY, RN) bits before proceeding with function reset.
1745 * During this process, interrupt vectors are freed and later requested
1746 * for handling possible port resource change.
1747 **/
1748static int
James Smarte10b2022014-02-20 09:57:43 -05001749lpfc_sli4_port_sta_fn_reset(struct lpfc_hba *phba, int mbx_action,
1750 bool en_rn_msg)
James Smart618a5232012-06-12 13:54:36 -04001751{
1752 int rc;
1753 uint32_t intr_mode;
1754
James Smart65791f12016-07-06 12:35:56 -07001755 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
1756 LPFC_SLI_INTF_IF_TYPE_2) {
1757 /*
1758 * On error status condition, driver need to wait for port
1759 * ready before performing reset.
1760 */
1761 rc = lpfc_sli4_pdev_status_reg_wait(phba);
James Smart0e916ee2016-07-06 12:36:06 -07001762 if (rc)
James Smart65791f12016-07-06 12:35:56 -07001763 return rc;
James Smart618a5232012-06-12 13:54:36 -04001764 }
James Smart0e916ee2016-07-06 12:36:06 -07001765
James Smart65791f12016-07-06 12:35:56 -07001766 /* need reset: attempt for port recovery */
1767 if (en_rn_msg)
1768 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1769 "2887 Reset Needed: Attempting Port "
1770 "Recovery...\n");
1771 lpfc_offline_prep(phba, mbx_action);
1772 lpfc_offline(phba);
1773 /* release interrupt for possible resource change */
1774 lpfc_sli4_disable_intr(phba);
1775 lpfc_sli_brdrestart(phba);
1776 /* request and enable interrupt */
1777 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
1778 if (intr_mode == LPFC_INTR_ERROR) {
1779 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1780 "3175 Failed to enable interrupt\n");
1781 return -EIO;
1782 }
1783 phba->intr_mode = intr_mode;
1784 rc = lpfc_online(phba);
1785 if (rc == 0)
1786 lpfc_unblock_mgmt_io(phba);
1787
James Smart618a5232012-06-12 13:54:36 -04001788 return rc;
1789}
1790
1791/**
James Smartda0436e2009-05-22 14:51:39 -04001792 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1793 * @phba: pointer to lpfc hba data structure.
1794 *
1795 * This routine is invoked to handle the SLI4 HBA hardware error attention
1796 * conditions.
1797 **/
1798static void
1799lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1800{
1801 struct lpfc_vport *vport = phba->pport;
1802 uint32_t event_data;
1803 struct Scsi_Host *shost;
James Smart2fcee4b2010-12-15 17:57:46 -05001804 uint32_t if_type;
James Smart2e90f4b2011-12-13 13:22:37 -05001805 struct lpfc_register portstat_reg = {0};
1806 uint32_t reg_err1, reg_err2;
1807 uint32_t uerrlo_reg, uemasklo_reg;
James Smart65791f12016-07-06 12:35:56 -07001808 uint32_t smphr_port_status = 0, pci_rd_rc1, pci_rd_rc2;
James Smarte10b2022014-02-20 09:57:43 -05001809 bool en_rn_msg = true;
James Smart946727d2015-04-07 15:07:09 -04001810 struct temp_event temp_event_data;
James Smart65791f12016-07-06 12:35:56 -07001811 struct lpfc_register portsmphr_reg;
1812 int rc, i;
James Smartda0436e2009-05-22 14:51:39 -04001813
1814 /* If the pci channel is offline, ignore possible errors, since
1815 * we cannot communicate with the pci card anyway.
1816 */
1817 if (pci_channel_offline(phba->pcidev))
1818 return;
James Smartda0436e2009-05-22 14:51:39 -04001819
James Smart65791f12016-07-06 12:35:56 -07001820 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
James Smart2fcee4b2010-12-15 17:57:46 -05001821 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1822 switch (if_type) {
1823 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart2e90f4b2011-12-13 13:22:37 -05001824 pci_rd_rc1 = lpfc_readl(
1825 phba->sli4_hba.u.if_type0.UERRLOregaddr,
1826 &uerrlo_reg);
1827 pci_rd_rc2 = lpfc_readl(
1828 phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
1829 &uemasklo_reg);
1830 /* consider PCI bus read error as pci_channel_offline */
1831 if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
1832 return;
James Smart65791f12016-07-06 12:35:56 -07001833 if (!(phba->hba_flag & HBA_RECOVERABLE_UE)) {
1834 lpfc_sli4_offline_eratt(phba);
1835 return;
1836 }
1837 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1838 "7623 Checking UE recoverable");
1839
1840 for (i = 0; i < phba->sli4_hba.ue_to_sr / 1000; i++) {
1841 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1842 &portsmphr_reg.word0))
1843 continue;
1844
1845 smphr_port_status = bf_get(lpfc_port_smphr_port_status,
1846 &portsmphr_reg);
1847 if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1848 LPFC_PORT_SEM_UE_RECOVERABLE)
1849 break;
1850 /*Sleep for 1Sec, before checking SEMAPHORE */
1851 msleep(1000);
1852 }
1853
1854 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1855 "4827 smphr_port_status x%x : Waited %dSec",
1856 smphr_port_status, i);
1857
1858 /* Recoverable UE, reset the HBA device */
1859 if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1860 LPFC_PORT_SEM_UE_RECOVERABLE) {
1861 for (i = 0; i < 20; i++) {
1862 msleep(1000);
1863 if (!lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1864 &portsmphr_reg.word0) &&
1865 (LPFC_POST_STAGE_PORT_READY ==
1866 bf_get(lpfc_port_smphr_port_status,
1867 &portsmphr_reg))) {
1868 rc = lpfc_sli4_port_sta_fn_reset(phba,
1869 LPFC_MBX_NO_WAIT, en_rn_msg);
1870 if (rc == 0)
1871 return;
1872 lpfc_printf_log(phba,
1873 KERN_ERR, LOG_INIT,
1874 "4215 Failed to recover UE");
1875 break;
1876 }
1877 }
1878 }
1879 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1880 "7624 Firmware not ready: Failing UE recovery,"
1881 " waited %dSec", i);
James Smart2fcee4b2010-12-15 17:57:46 -05001882 lpfc_sli4_offline_eratt(phba);
1883 break;
James Smart946727d2015-04-07 15:07:09 -04001884
James Smart2fcee4b2010-12-15 17:57:46 -05001885 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart2e90f4b2011-12-13 13:22:37 -05001886 pci_rd_rc1 = lpfc_readl(
1887 phba->sli4_hba.u.if_type2.STATUSregaddr,
1888 &portstat_reg.word0);
1889 /* consider PCI bus read error as pci_channel_offline */
James Smart6b5151f2012-01-18 16:24:06 -05001890 if (pci_rd_rc1 == -EIO) {
1891 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1892 "3151 PCI bus read access failure: x%x\n",
1893 readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
James Smart2e90f4b2011-12-13 13:22:37 -05001894 return;
James Smart6b5151f2012-01-18 16:24:06 -05001895 }
James Smart2e90f4b2011-12-13 13:22:37 -05001896 reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
1897 reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
James Smart2fcee4b2010-12-15 17:57:46 -05001898 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
James Smart2fcee4b2010-12-15 17:57:46 -05001899 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1900 "2889 Port Overtemperature event, "
James Smart946727d2015-04-07 15:07:09 -04001901 "taking port offline Data: x%x x%x\n",
1902 reg_err1, reg_err2);
1903
James Smart310429e2016-07-06 12:35:54 -07001904 phba->sfp_alarm |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
James Smart946727d2015-04-07 15:07:09 -04001905 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1906 temp_event_data.event_code = LPFC_CRIT_TEMP;
1907 temp_event_data.data = 0xFFFFFFFF;
1908
1909 shost = lpfc_shost_from_vport(phba->pport);
1910 fc_host_post_vendor_event(shost, fc_get_event_number(),
1911 sizeof(temp_event_data),
1912 (char *)&temp_event_data,
1913 SCSI_NL_VID_TYPE_PCI
1914 | PCI_VENDOR_ID_EMULEX);
1915
James Smart2fcee4b2010-12-15 17:57:46 -05001916 spin_lock_irq(&phba->hbalock);
1917 phba->over_temp_state = HBA_OVER_TEMP;
1918 spin_unlock_irq(&phba->hbalock);
1919 lpfc_sli4_offline_eratt(phba);
James Smart946727d2015-04-07 15:07:09 -04001920 return;
James Smart2fcee4b2010-12-15 17:57:46 -05001921 }
James Smart2e90f4b2011-12-13 13:22:37 -05001922 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smarte10b2022014-02-20 09:57:43 -05001923 reg_err2 == SLIPORT_ERR2_REG_FW_RESTART) {
James Smart2e90f4b2011-12-13 13:22:37 -05001924 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte10b2022014-02-20 09:57:43 -05001925 "3143 Port Down: Firmware Update "
1926 "Detected\n");
1927 en_rn_msg = false;
1928 } else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smart2e90f4b2011-12-13 13:22:37 -05001929 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1930 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1931 "3144 Port Down: Debug Dump\n");
1932 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1933 reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
1934 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1935 "3145 Port Down: Provisioning\n");
James Smart618a5232012-06-12 13:54:36 -04001936
James Smart946727d2015-04-07 15:07:09 -04001937 /* If resets are disabled then leave the HBA alone and return */
1938 if (!phba->cfg_enable_hba_reset)
1939 return;
1940
James Smart618a5232012-06-12 13:54:36 -04001941 /* Check port status register for function reset */
James Smarte10b2022014-02-20 09:57:43 -05001942 rc = lpfc_sli4_port_sta_fn_reset(phba, LPFC_MBX_NO_WAIT,
1943 en_rn_msg);
James Smart618a5232012-06-12 13:54:36 -04001944 if (rc == 0) {
1945 /* don't report event on forced debug dump */
1946 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1947 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1948 return;
1949 else
1950 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001951 }
James Smart618a5232012-06-12 13:54:36 -04001952 /* fall through for not able to recover */
James Smart6b5151f2012-01-18 16:24:06 -05001953 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1954 "3152 Unrecoverable error, bring the port "
1955 "offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001956 lpfc_sli4_offline_eratt(phba);
1957 break;
1958 case LPFC_SLI_INTF_IF_TYPE_1:
1959 default:
1960 break;
1961 }
James Smart2e90f4b2011-12-13 13:22:37 -05001962 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1963 "3123 Report dump event to upper layer\n");
1964 /* Send an internal error event to mgmt application */
1965 lpfc_board_errevt_to_mgmt(phba);
1966
1967 event_data = FC_REG_DUMP_EVENT;
1968 shost = lpfc_shost_from_vport(vport);
1969 fc_host_post_vendor_event(shost, fc_get_event_number(),
1970 sizeof(event_data), (char *) &event_data,
1971 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
James Smartda0436e2009-05-22 14:51:39 -04001972}
1973
1974/**
1975 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1976 * @phba: pointer to lpfc HBA data structure.
1977 *
1978 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1979 * routine from the API jump table function pointer from the lpfc_hba struct.
1980 *
1981 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001982 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001983 * Any other value - error.
1984 **/
1985void
1986lpfc_handle_eratt(struct lpfc_hba *phba)
1987{
1988 (*phba->lpfc_handle_eratt)(phba);
1989}
1990
1991/**
James Smart3621a712009-04-06 18:47:14 -04001992 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04001993 * @phba: pointer to lpfc hba data structure.
1994 *
1995 * This routine is invoked from the worker thread to handle a HBA host
James Smart895427b2017-02-12 13:52:30 -08001996 * attention link event. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04001997 **/
dea31012005-04-17 16:05:31 -05001998void
James Smart2e0fef82007-06-17 19:56:36 -05001999lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002000{
James Smart2e0fef82007-06-17 19:56:36 -05002001 struct lpfc_vport *vport = phba->pport;
2002 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05002003 LPFC_MBOXQ_t *pmb;
2004 volatile uint32_t control;
2005 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05002006 int rc = 0;
dea31012005-04-17 16:05:31 -05002007
2008 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05002009 if (!pmb) {
2010 rc = 1;
dea31012005-04-17 16:05:31 -05002011 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05002012 }
dea31012005-04-17 16:05:31 -05002013
2014 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05002015 if (!mp) {
2016 rc = 2;
dea31012005-04-17 16:05:31 -05002017 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05002018 }
dea31012005-04-17 16:05:31 -05002019
2020 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05002021 if (!mp->virt) {
2022 rc = 3;
dea31012005-04-17 16:05:31 -05002023 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05002024 }
dea31012005-04-17 16:05:31 -05002025
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05002026 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04002027 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05002028
2029 psli->slistat.link_event++;
James Smart76a95d72010-11-20 23:11:48 -05002030 lpfc_read_topology(phba, pmb, mp);
2031 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smart2e0fef82007-06-17 19:56:36 -05002032 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04002033 /* Block ELS IOCBs until we have processed this mbox command */
James Smart895427b2017-02-12 13:52:30 -08002034 phba->sli.sli3_ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04002035 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05002036 if (rc == MBX_NOT_FINISHED) {
2037 rc = 4;
James Smart14691152006-12-02 13:34:28 -05002038 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05002039 }
dea31012005-04-17 16:05:31 -05002040
2041 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05002042 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002043 writel(HA_LATT, phba->HAregaddr);
2044 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002045 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002046
2047 return;
2048
James Smart14691152006-12-02 13:34:28 -05002049lpfc_handle_latt_free_mbuf:
James Smart895427b2017-02-12 13:52:30 -08002050 phba->sli.sli3_ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05002051 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05002052lpfc_handle_latt_free_mp:
2053 kfree(mp);
2054lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04002055 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002056lpfc_handle_latt_err_exit:
2057 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05002058 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002059 psli->sli_flag |= LPFC_PROCESS_LA;
2060 control = readl(phba->HCregaddr);
2061 control |= HC_LAINT_ENA;
2062 writel(control, phba->HCregaddr);
2063 readl(phba->HCregaddr); /* flush */
2064
2065 /* Clear Link Attention in HA REG */
2066 writel(HA_LATT, phba->HAregaddr);
2067 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05002068 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002069 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002070 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002071
James Smart09372822008-01-11 01:52:54 -05002072 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
2073 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05002074
2075 return;
2076}
2077
James Smarte59058c2008-08-24 21:49:00 -04002078/**
James Smart3621a712009-04-06 18:47:14 -04002079 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04002080 * @phba: pointer to lpfc hba data structure.
2081 * @vpd: pointer to the vital product data.
2082 * @len: length of the vital product data in bytes.
2083 *
2084 * This routine parses the Vital Product Data (VPD). The VPD is treated as
2085 * an array of characters. In this routine, the ModelName, ProgramType, and
2086 * ModelDesc, etc. fields of the phba data structure will be populated.
2087 *
2088 * Return codes
2089 * 0 - pointer to the VPD passed in is NULL
2090 * 1 - success
2091 **/
James Smart3772a992009-05-22 14:50:54 -04002092int
James Smart2e0fef82007-06-17 19:56:36 -05002093lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05002094{
2095 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05002096 int Length;
dea31012005-04-17 16:05:31 -05002097 int i, j;
2098 int finished = 0;
2099 int index = 0;
2100
2101 if (!vpd)
2102 return 0;
2103
2104 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05002105 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002106 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05002107 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
2108 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002109 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05002110 switch (vpd[index]) {
2111 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002112 case 0x91:
dea31012005-04-17 16:05:31 -05002113 index += 1;
2114 lenlo = vpd[index];
2115 index += 1;
2116 lenhi = vpd[index];
2117 index += 1;
2118 i = ((((unsigned short)lenhi) << 8) + lenlo);
2119 index += i;
2120 break;
2121 case 0x90:
2122 index += 1;
2123 lenlo = vpd[index];
2124 index += 1;
2125 lenhi = vpd[index];
2126 index += 1;
2127 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002128 if (Length > len - index)
2129 Length = len - index;
dea31012005-04-17 16:05:31 -05002130 while (Length > 0) {
2131 /* Look for Serial Number */
2132 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
2133 index += 2;
2134 i = vpd[index];
2135 index += 1;
2136 j = 0;
2137 Length -= (3+i);
2138 while(i--) {
2139 phba->SerialNumber[j++] = vpd[index++];
2140 if (j == 31)
2141 break;
2142 }
2143 phba->SerialNumber[j] = 0;
2144 continue;
2145 }
2146 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
2147 phba->vpd_flag |= VPD_MODEL_DESC;
2148 index += 2;
2149 i = vpd[index];
2150 index += 1;
2151 j = 0;
2152 Length -= (3+i);
2153 while(i--) {
2154 phba->ModelDesc[j++] = vpd[index++];
2155 if (j == 255)
2156 break;
2157 }
2158 phba->ModelDesc[j] = 0;
2159 continue;
2160 }
2161 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
2162 phba->vpd_flag |= VPD_MODEL_NAME;
2163 index += 2;
2164 i = vpd[index];
2165 index += 1;
2166 j = 0;
2167 Length -= (3+i);
2168 while(i--) {
2169 phba->ModelName[j++] = vpd[index++];
2170 if (j == 79)
2171 break;
2172 }
2173 phba->ModelName[j] = 0;
2174 continue;
2175 }
2176 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
2177 phba->vpd_flag |= VPD_PROGRAM_TYPE;
2178 index += 2;
2179 i = vpd[index];
2180 index += 1;
2181 j = 0;
2182 Length -= (3+i);
2183 while(i--) {
2184 phba->ProgramType[j++] = vpd[index++];
2185 if (j == 255)
2186 break;
2187 }
2188 phba->ProgramType[j] = 0;
2189 continue;
2190 }
2191 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
2192 phba->vpd_flag |= VPD_PORT;
2193 index += 2;
2194 i = vpd[index];
2195 index += 1;
2196 j = 0;
2197 Length -= (3+i);
2198 while(i--) {
James Smartcd1c8302011-10-10 21:33:25 -04002199 if ((phba->sli_rev == LPFC_SLI_REV4) &&
2200 (phba->sli4_hba.pport_name_sta ==
2201 LPFC_SLI4_PPNAME_GET)) {
2202 j++;
2203 index++;
2204 } else
2205 phba->Port[j++] = vpd[index++];
2206 if (j == 19)
2207 break;
dea31012005-04-17 16:05:31 -05002208 }
James Smartcd1c8302011-10-10 21:33:25 -04002209 if ((phba->sli_rev != LPFC_SLI_REV4) ||
2210 (phba->sli4_hba.pport_name_sta ==
2211 LPFC_SLI4_PPNAME_NON))
2212 phba->Port[j] = 0;
dea31012005-04-17 16:05:31 -05002213 continue;
2214 }
2215 else {
2216 index += 2;
2217 i = vpd[index];
2218 index += 1;
2219 index += i;
2220 Length -= (3 + i);
2221 }
2222 }
2223 finished = 0;
2224 break;
2225 case 0x78:
2226 finished = 1;
2227 break;
2228 default:
2229 index ++;
2230 break;
2231 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002232 }
dea31012005-04-17 16:05:31 -05002233
2234 return(1);
2235}
2236
James Smarte59058c2008-08-24 21:49:00 -04002237/**
James Smart3621a712009-04-06 18:47:14 -04002238 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04002239 * @phba: pointer to lpfc hba data structure.
2240 * @mdp: pointer to the data structure to hold the derived model name.
2241 * @descp: pointer to the data structure to hold the derived description.
2242 *
2243 * This routine retrieves HBA's description based on its registered PCI device
2244 * ID. The @descp passed into this function points to an array of 256 chars. It
2245 * shall be returned with the model name, maximum speed, and the host bus type.
2246 * The @mdp passed into this function points to an array of 80 chars. When the
2247 * function returns, the @mdp will be filled with the model name.
2248 **/
dea31012005-04-17 16:05:31 -05002249static void
James Smart2e0fef82007-06-17 19:56:36 -05002250lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05002251{
2252 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05002253 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002254 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04002255 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04002256 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002257 struct {
James Smarta747c9c2009-11-18 15:41:10 -05002258 char *name;
2259 char *bus;
2260 char *function;
2261 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002262
2263 if (mdp && mdp[0] != '\0'
2264 && descp && descp[0] != '\0')
2265 return;
2266
James Smartd38dd522015-08-31 16:48:17 -04002267 if (phba->lmt & LMT_32Gb)
2268 max_speed = 32;
2269 else if (phba->lmt & LMT_16Gb)
James Smartc0c11512011-05-24 11:41:34 -04002270 max_speed = 16;
2271 else if (phba->lmt & LMT_10Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002272 max_speed = 10;
2273 else if (phba->lmt & LMT_8Gb)
2274 max_speed = 8;
2275 else if (phba->lmt & LMT_4Gb)
2276 max_speed = 4;
2277 else if (phba->lmt & LMT_2Gb)
2278 max_speed = 2;
James Smart4169d862012-09-29 11:32:09 -04002279 else if (phba->lmt & LMT_1Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002280 max_speed = 1;
James Smart4169d862012-09-29 11:32:09 -04002281 else
2282 max_speed = 0;
dea31012005-04-17 16:05:31 -05002283
2284 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05002285
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002286 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002287 case PCI_DEVICE_ID_FIREFLY:
James Smart12222f42014-05-21 08:05:19 -04002288 m = (typeof(m)){"LP6000", "PCI",
2289 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002290 break;
dea31012005-04-17 16:05:31 -05002291 case PCI_DEVICE_ID_SUPERFLY:
2292 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smart12222f42014-05-21 08:05:19 -04002293 m = (typeof(m)){"LP7000", "PCI", ""};
dea31012005-04-17 16:05:31 -05002294 else
James Smart12222f42014-05-21 08:05:19 -04002295 m = (typeof(m)){"LP7000E", "PCI", ""};
2296 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
dea31012005-04-17 16:05:31 -05002297 break;
2298 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002299 m = (typeof(m)){"LP8000", "PCI",
James Smart12222f42014-05-21 08:05:19 -04002300 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002301 break;
2302 case PCI_DEVICE_ID_CENTAUR:
2303 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smart12222f42014-05-21 08:05:19 -04002304 m = (typeof(m)){"LP9002", "PCI", ""};
dea31012005-04-17 16:05:31 -05002305 else
James Smart12222f42014-05-21 08:05:19 -04002306 m = (typeof(m)){"LP9000", "PCI", ""};
2307 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
dea31012005-04-17 16:05:31 -05002308 break;
2309 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002310 m = (typeof(m)){"LP952", "PCI",
James Smart12222f42014-05-21 08:05:19 -04002311 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002312 break;
2313 case PCI_DEVICE_ID_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05002314 m = (typeof(m)){"LP9802", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002315 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002316 break;
2317 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05002318 m = (typeof(m)){"LP10000", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002319 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002320 break;
2321 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05002322 m = (typeof(m)){"LPX1000", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002323 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002324 break;
2325 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002326 m = (typeof(m)){"LP982", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002327 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002328 break;
2329 case PCI_DEVICE_ID_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002330 m = (typeof(m)){"LP1050", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002331 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002332 break;
2333 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05002334 m = (typeof(m)){"LP11000", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002335 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002336 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002337 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002338 m = (typeof(m)){"LP11000-SP", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002339 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002340 break;
2341 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002342 m = (typeof(m)){"LP11002-SP", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002343 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002344 break;
2345 case PCI_DEVICE_ID_NEPTUNE:
James Smart12222f42014-05-21 08:05:19 -04002346 m = (typeof(m)){"LPe1000", "PCIe",
2347 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002348 break;
2349 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smart12222f42014-05-21 08:05:19 -04002350 m = (typeof(m)){"LPe1000-SP", "PCIe",
2351 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002352 break;
2353 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smart12222f42014-05-21 08:05:19 -04002354 m = (typeof(m)){"LPe1002-SP", "PCIe",
2355 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002356 break;
dea31012005-04-17 16:05:31 -05002357 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05002358 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002359 break;
2360 case PCI_DEVICE_ID_BSMB:
James Smart12222f42014-05-21 08:05:19 -04002361 m = (typeof(m)){"LP111", "PCI-X2",
2362 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002363 break;
2364 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05002365 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002366 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002367 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002368 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002369 break;
2370 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002371 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04002372 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002373 break;
dea31012005-04-17 16:05:31 -05002374 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05002375 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002376 break;
2377 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05002378 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002379 break;
2380 case PCI_DEVICE_ID_LP101:
James Smart12222f42014-05-21 08:05:19 -04002381 m = (typeof(m)){"LP101", "PCI-X",
2382 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002383 break;
2384 case PCI_DEVICE_ID_LP10000S:
James Smart12222f42014-05-21 08:05:19 -04002385 m = (typeof(m)){"LP10000-S", "PCI",
2386 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002387 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002388 case PCI_DEVICE_ID_LP11000S:
James Smart12222f42014-05-21 08:05:19 -04002389 m = (typeof(m)){"LP11000-S", "PCI-X2",
2390 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05002391 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002392 case PCI_DEVICE_ID_LPE11000S:
James Smart12222f42014-05-21 08:05:19 -04002393 m = (typeof(m)){"LPe11000-S", "PCIe",
2394 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002395 break;
James Smartb87eab32007-04-25 09:53:28 -04002396 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05002397 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002398 break;
2399 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05002400 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002401 break;
2402 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05002403 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002404 break;
2405 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002406 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002407 break;
2408 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002409 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002410 break;
2411 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05002412 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002413 break;
James Smart84774a42008-08-24 21:50:06 -04002414 case PCI_DEVICE_ID_HORNET:
James Smart12222f42014-05-21 08:05:19 -04002415 m = (typeof(m)){"LP21000", "PCIe",
2416 "Obsolete, Unsupported FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002417 GE = 1;
2418 break;
2419 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05002420 m = (typeof(m)){"LPev12000", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002421 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002422 break;
2423 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05002424 m = (typeof(m)){"LPev12000", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002425 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002426 break;
2427 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05002428 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002429 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002430 break;
James Smartda0436e2009-05-22 14:51:39 -04002431 case PCI_DEVICE_ID_TIGERSHARK:
2432 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002433 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04002434 break;
James Smarta747c9c2009-11-18 15:41:10 -05002435 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04002436 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002437 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
2438 break;
2439 case PCI_DEVICE_ID_FALCON:
2440 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
2441 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04002442 break;
James Smart98fc5dd2010-06-07 15:24:29 -04002443 case PCI_DEVICE_ID_BALIUS:
2444 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
James Smart12222f42014-05-21 08:05:19 -04002445 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart98fc5dd2010-06-07 15:24:29 -04002446 break;
James Smart085c6472010-11-20 23:11:37 -05002447 case PCI_DEVICE_ID_LANCER_FC:
James Smartc0c11512011-05-24 11:41:34 -04002448 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
James Smart085c6472010-11-20 23:11:37 -05002449 break;
James Smart12222f42014-05-21 08:05:19 -04002450 case PCI_DEVICE_ID_LANCER_FC_VF:
2451 m = (typeof(m)){"LPe16000", "PCIe",
2452 "Obsolete, Unsupported Fibre Channel Adapter"};
2453 break;
James Smart085c6472010-11-20 23:11:37 -05002454 case PCI_DEVICE_ID_LANCER_FCOE:
2455 oneConnect = 1;
James Smart079b5c92011-08-21 21:48:49 -04002456 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
James Smart085c6472010-11-20 23:11:37 -05002457 break;
James Smart12222f42014-05-21 08:05:19 -04002458 case PCI_DEVICE_ID_LANCER_FCOE_VF:
2459 oneConnect = 1;
2460 m = (typeof(m)){"OCe15100", "PCIe",
2461 "Obsolete, Unsupported FCoE"};
2462 break;
James Smartd38dd522015-08-31 16:48:17 -04002463 case PCI_DEVICE_ID_LANCER_G6_FC:
2464 m = (typeof(m)){"LPe32000", "PCIe", "Fibre Channel Adapter"};
2465 break;
James Smartf8cafd32012-08-03 12:42:51 -04002466 case PCI_DEVICE_ID_SKYHAWK:
2467 case PCI_DEVICE_ID_SKYHAWK_VF:
2468 oneConnect = 1;
2469 m = (typeof(m)){"OCe14000", "PCIe", "FCoE"};
2470 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002471 default:
James Smarta747c9c2009-11-18 15:41:10 -05002472 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002473 break;
dea31012005-04-17 16:05:31 -05002474 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002475
2476 if (mdp && mdp[0] == '\0')
2477 snprintf(mdp, 79,"%s", m.name);
James Smartc0c11512011-05-24 11:41:34 -04002478 /*
2479 * oneConnect hba requires special processing, they are all initiators
James Smartda0436e2009-05-22 14:51:39 -04002480 * and we put the port number on the end
2481 */
2482 if (descp && descp[0] == '\0') {
2483 if (oneConnect)
2484 snprintf(descp, 255,
James Smart4169d862012-09-29 11:32:09 -04002485 "Emulex OneConnect %s, %s Initiator %s",
James Smarta747c9c2009-11-18 15:41:10 -05002486 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04002487 phba->Port);
James Smart4169d862012-09-29 11:32:09 -04002488 else if (max_speed == 0)
2489 snprintf(descp, 255,
Sebastian Herbszt290237d2015-08-31 16:48:08 -04002490 "Emulex %s %s %s",
James Smart4169d862012-09-29 11:32:09 -04002491 m.name, m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002492 else
2493 snprintf(descp, 255,
2494 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05002495 m.name, max_speed, (GE) ? "GE" : "Gb",
2496 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002497 }
dea31012005-04-17 16:05:31 -05002498}
2499
James Smarte59058c2008-08-24 21:49:00 -04002500/**
James Smart3621a712009-04-06 18:47:14 -04002501 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04002502 * @phba: pointer to lpfc hba data structure.
2503 * @pring: pointer to a IOCB ring.
2504 * @cnt: the number of IOCBs to be posted to the IOCB ring.
2505 *
2506 * This routine posts a given number of IOCBs with the associated DMA buffer
2507 * descriptors specified by the cnt argument to the given IOCB ring.
2508 *
2509 * Return codes
2510 * The number of IOCBs NOT able to be posted to the IOCB ring.
2511 **/
dea31012005-04-17 16:05:31 -05002512int
James Smart495a7142008-06-14 22:52:59 -04002513lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05002514{
2515 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002516 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05002517 struct lpfc_dmabuf *mp1, *mp2;
2518
2519 cnt += pring->missbufcnt;
2520
2521 /* While there are buffers to post */
2522 while (cnt > 0) {
2523 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002524 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002525 if (iocb == NULL) {
2526 pring->missbufcnt = cnt;
2527 return cnt;
2528 }
dea31012005-04-17 16:05:31 -05002529 icmd = &iocb->iocb;
2530
2531 /* 2 buffers can be posted per command */
2532 /* Allocate buffer to post */
2533 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2534 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04002535 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2536 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002537 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002538 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002539 pring->missbufcnt = cnt;
2540 return cnt;
2541 }
2542
2543 INIT_LIST_HEAD(&mp1->list);
2544 /* Allocate buffer to post */
2545 if (cnt > 1) {
2546 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2547 if (mp2)
2548 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2549 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04002550 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002551 kfree(mp2);
dea31012005-04-17 16:05:31 -05002552 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2553 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002554 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002555 pring->missbufcnt = cnt;
2556 return cnt;
2557 }
2558
2559 INIT_LIST_HEAD(&mp2->list);
2560 } else {
2561 mp2 = NULL;
2562 }
2563
2564 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2565 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2566 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2567 icmd->ulpBdeCount = 1;
2568 cnt--;
2569 if (mp2) {
2570 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2571 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2572 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2573 cnt--;
2574 icmd->ulpBdeCount = 2;
2575 }
2576
2577 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2578 icmd->ulpLe = 1;
2579
James Smart3772a992009-05-22 14:50:54 -04002580 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2581 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05002582 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2583 kfree(mp1);
2584 cnt++;
2585 if (mp2) {
2586 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2587 kfree(mp2);
2588 cnt++;
2589 }
James Bottomley604a3e32005-10-29 10:28:33 -05002590 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002591 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05002592 return cnt;
2593 }
dea31012005-04-17 16:05:31 -05002594 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05002595 if (mp2)
dea31012005-04-17 16:05:31 -05002596 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05002597 }
2598 pring->missbufcnt = 0;
2599 return 0;
2600}
2601
James Smarte59058c2008-08-24 21:49:00 -04002602/**
James Smart3621a712009-04-06 18:47:14 -04002603 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04002604 * @phba: pointer to lpfc hba data structure.
2605 *
2606 * This routine posts initial receive IOCB buffers to the ELS ring. The
2607 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
James Smart895427b2017-02-12 13:52:30 -08002608 * set to 64 IOCBs. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04002609 *
2610 * Return codes
2611 * 0 - success (currently always success)
2612 **/
dea31012005-04-17 16:05:31 -05002613static int
James Smart2e0fef82007-06-17 19:56:36 -05002614lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002615{
2616 struct lpfc_sli *psli = &phba->sli;
2617
2618 /* Ring 0, ELS / CT buffers */
James Smart895427b2017-02-12 13:52:30 -08002619 lpfc_post_buffer(phba, &psli->sli3_ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05002620 /* Ring 2 - FCP no buffers needed */
2621
2622 return 0;
2623}
2624
2625#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2626
James Smarte59058c2008-08-24 21:49:00 -04002627/**
James Smart3621a712009-04-06 18:47:14 -04002628 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04002629 * @HashResultPointer: pointer to an array as hash table.
2630 *
2631 * This routine sets up the initial values to the array of hash table entries
2632 * for the LC HBAs.
2633 **/
dea31012005-04-17 16:05:31 -05002634static void
2635lpfc_sha_init(uint32_t * HashResultPointer)
2636{
2637 HashResultPointer[0] = 0x67452301;
2638 HashResultPointer[1] = 0xEFCDAB89;
2639 HashResultPointer[2] = 0x98BADCFE;
2640 HashResultPointer[3] = 0x10325476;
2641 HashResultPointer[4] = 0xC3D2E1F0;
2642}
2643
James Smarte59058c2008-08-24 21:49:00 -04002644/**
James Smart3621a712009-04-06 18:47:14 -04002645 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04002646 * @HashResultPointer: pointer to an initial/result hash table.
2647 * @HashWorkingPointer: pointer to an working hash table.
2648 *
2649 * This routine iterates an initial hash table pointed by @HashResultPointer
2650 * with the values from the working hash table pointeed by @HashWorkingPointer.
2651 * The results are putting back to the initial hash table, returned through
2652 * the @HashResultPointer as the result hash table.
2653 **/
dea31012005-04-17 16:05:31 -05002654static void
2655lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2656{
2657 int t;
2658 uint32_t TEMP;
2659 uint32_t A, B, C, D, E;
2660 t = 16;
2661 do {
2662 HashWorkingPointer[t] =
2663 S(1,
2664 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2665 8] ^
2666 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2667 } while (++t <= 79);
2668 t = 0;
2669 A = HashResultPointer[0];
2670 B = HashResultPointer[1];
2671 C = HashResultPointer[2];
2672 D = HashResultPointer[3];
2673 E = HashResultPointer[4];
2674
2675 do {
2676 if (t < 20) {
2677 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2678 } else if (t < 40) {
2679 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2680 } else if (t < 60) {
2681 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2682 } else {
2683 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2684 }
2685 TEMP += S(5, A) + E + HashWorkingPointer[t];
2686 E = D;
2687 D = C;
2688 C = S(30, B);
2689 B = A;
2690 A = TEMP;
2691 } while (++t <= 79);
2692
2693 HashResultPointer[0] += A;
2694 HashResultPointer[1] += B;
2695 HashResultPointer[2] += C;
2696 HashResultPointer[3] += D;
2697 HashResultPointer[4] += E;
2698
2699}
2700
James Smarte59058c2008-08-24 21:49:00 -04002701/**
James Smart3621a712009-04-06 18:47:14 -04002702 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04002703 * @RandomChallenge: pointer to the entry of host challenge random number array.
2704 * @HashWorking: pointer to the entry of the working hash array.
2705 *
2706 * This routine calculates the working hash array referred by @HashWorking
2707 * from the challenge random numbers associated with the host, referred by
2708 * @RandomChallenge. The result is put into the entry of the working hash
2709 * array and returned by reference through @HashWorking.
2710 **/
dea31012005-04-17 16:05:31 -05002711static void
2712lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2713{
2714 *HashWorking = (*RandomChallenge ^ *HashWorking);
2715}
2716
James Smarte59058c2008-08-24 21:49:00 -04002717/**
James Smart3621a712009-04-06 18:47:14 -04002718 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04002719 * @phba: pointer to lpfc hba data structure.
2720 * @hbainit: pointer to an array of unsigned 32-bit integers.
2721 *
2722 * This routine performs the special handling for LC HBA initialization.
2723 **/
dea31012005-04-17 16:05:31 -05002724void
2725lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2726{
2727 int t;
2728 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05002729 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05002730
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002731 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002732 if (!HashWorking)
2733 return;
2734
dea31012005-04-17 16:05:31 -05002735 HashWorking[0] = HashWorking[78] = *pwwnn++;
2736 HashWorking[1] = HashWorking[79] = *pwwnn;
2737
2738 for (t = 0; t < 7; t++)
2739 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2740
2741 lpfc_sha_init(hbainit);
2742 lpfc_sha_iterate(hbainit, HashWorking);
2743 kfree(HashWorking);
2744}
2745
James Smarte59058c2008-08-24 21:49:00 -04002746/**
James Smart3621a712009-04-06 18:47:14 -04002747 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04002748 * @vport: pointer to a virtual N_Port data structure.
2749 *
2750 * This routine performs the necessary cleanups before deleting the @vport.
2751 * It invokes the discovery state machine to perform necessary state
2752 * transitions and to release the ndlps associated with the @vport. Note,
2753 * the physical port is treated as @vport 0.
2754 **/
James Smart87af33f2007-10-27 13:37:43 -04002755void
James Smart2e0fef82007-06-17 19:56:36 -05002756lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002757{
James Smart87af33f2007-10-27 13:37:43 -04002758 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002759 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04002760 int i = 0;
dea31012005-04-17 16:05:31 -05002761
James Smart87af33f2007-10-27 13:37:43 -04002762 if (phba->link_state > LPFC_LINK_DOWN)
2763 lpfc_port_link_failure(vport);
2764
2765 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002766 if (!NLP_CHK_NODE_ACT(ndlp)) {
2767 ndlp = lpfc_enable_node(vport, ndlp,
2768 NLP_STE_UNUSED_NODE);
2769 if (!ndlp)
2770 continue;
2771 spin_lock_irq(&phba->ndlp_lock);
2772 NLP_SET_FREE_REQ(ndlp);
2773 spin_unlock_irq(&phba->ndlp_lock);
2774 /* Trigger the release of the ndlp memory */
2775 lpfc_nlp_put(ndlp);
2776 continue;
2777 }
2778 spin_lock_irq(&phba->ndlp_lock);
2779 if (NLP_CHK_FREE_REQ(ndlp)) {
2780 /* The ndlp should not be in memory free mode already */
2781 spin_unlock_irq(&phba->ndlp_lock);
2782 continue;
2783 } else
2784 /* Indicate request for freeing ndlp memory */
2785 NLP_SET_FREE_REQ(ndlp);
2786 spin_unlock_irq(&phba->ndlp_lock);
2787
James Smart58da1ff2008-04-07 10:15:56 -04002788 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2789 ndlp->nlp_DID == Fabric_DID) {
2790 /* Just free up ndlp with Fabric_DID for vports */
2791 lpfc_nlp_put(ndlp);
2792 continue;
2793 }
2794
James Smarteff4a012012-01-18 16:25:25 -05002795 /* take care of nodes in unused state before the state
2796 * machine taking action.
2797 */
2798 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
2799 lpfc_nlp_put(ndlp);
2800 continue;
2801 }
2802
James Smart87af33f2007-10-27 13:37:43 -04002803 if (ndlp->nlp_type & NLP_FABRIC)
2804 lpfc_disc_state_machine(vport, ndlp, NULL,
2805 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002806
James Smart87af33f2007-10-27 13:37:43 -04002807 lpfc_disc_state_machine(vport, ndlp, NULL,
2808 NLP_EVT_DEVICE_RM);
2809 }
2810
James Smarta8adb832007-10-27 13:37:53 -04002811 /* At this point, ALL ndlp's should be gone
2812 * because of the previous NLP_EVT_DEVICE_RM.
2813 * Lets wait for this to happen, if needed.
2814 */
James Smart87af33f2007-10-27 13:37:43 -04002815 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002816 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002817 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002818 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002819 list_for_each_entry_safe(ndlp, next_ndlp,
2820 &vport->fc_nodes, nlp_listp) {
2821 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2822 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002823 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002824 "usgmap:x%x refcnt:%d\n",
2825 ndlp->nlp_DID, (void *)ndlp,
2826 ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01002827 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05002828 }
James Smarta8adb832007-10-27 13:37:53 -04002829 break;
James Smart87af33f2007-10-27 13:37:43 -04002830 }
James Smarta8adb832007-10-27 13:37:53 -04002831
2832 /* Wait for any activity on ndlps to settle */
2833 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002834 }
James Smart1151e3e2011-02-16 12:39:35 -05002835 lpfc_cleanup_vports_rrqs(vport, NULL);
dea31012005-04-17 16:05:31 -05002836}
2837
James Smarte59058c2008-08-24 21:49:00 -04002838/**
James Smart3621a712009-04-06 18:47:14 -04002839 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002840 * @vport: pointer to a virtual N_Port data structure.
2841 *
2842 * This routine stops all the timers associated with a @vport. This function
2843 * is invoked before disabling or deleting a @vport. Note that the physical
2844 * port is treated as @vport 0.
2845 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002846void
2847lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002848{
James Smart92d7f7b2007-06-17 19:56:38 -05002849 del_timer_sync(&vport->els_tmofunc);
James Smart92494142011-02-16 12:39:44 -05002850 del_timer_sync(&vport->delayed_disc_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002851 lpfc_can_disctmo(vport);
2852 return;
dea31012005-04-17 16:05:31 -05002853}
2854
James Smarte59058c2008-08-24 21:49:00 -04002855/**
James Smartecfd03c2010-02-12 14:41:27 -05002856 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2857 * @phba: pointer to lpfc hba data structure.
2858 *
2859 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2860 * caller of this routine should already hold the host lock.
2861 **/
2862void
2863__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2864{
James Smart5ac6b302010-10-22 11:05:36 -04002865 /* Clear pending FCF rediscovery wait flag */
2866 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2867
James Smartecfd03c2010-02-12 14:41:27 -05002868 /* Now, try to stop the timer */
2869 del_timer(&phba->fcf.redisc_wait);
2870}
2871
2872/**
2873 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2874 * @phba: pointer to lpfc hba data structure.
2875 *
2876 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2877 * checks whether the FCF rediscovery wait timer is pending with the host
2878 * lock held before proceeding with disabling the timer and clearing the
2879 * wait timer pendig flag.
2880 **/
2881void
2882lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2883{
2884 spin_lock_irq(&phba->hbalock);
2885 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2886 /* FCF rediscovery timer already fired or stopped */
2887 spin_unlock_irq(&phba->hbalock);
2888 return;
2889 }
2890 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart5ac6b302010-10-22 11:05:36 -04002891 /* Clear failover in progress flags */
2892 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
James Smartecfd03c2010-02-12 14:41:27 -05002893 spin_unlock_irq(&phba->hbalock);
2894}
2895
2896/**
James Smart3772a992009-05-22 14:50:54 -04002897 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002898 * @phba: pointer to lpfc hba data structure.
2899 *
2900 * This routine stops all the timers associated with a HBA. This function is
2901 * invoked before either putting a HBA offline or unloading the driver.
2902 **/
James Smart3772a992009-05-22 14:50:54 -04002903void
2904lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002905{
James Smart51ef4c22007-08-02 11:10:31 -04002906 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05002907 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002908 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002909 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002910 del_timer_sync(&phba->hb_tmofunc);
James Smart1151e3e2011-02-16 12:39:35 -05002911 if (phba->sli_rev == LPFC_SLI_REV4) {
2912 del_timer_sync(&phba->rrq_tmr);
2913 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2914 }
James Smart3772a992009-05-22 14:50:54 -04002915 phba->hb_outstanding = 0;
2916
2917 switch (phba->pci_dev_grp) {
2918 case LPFC_PCI_DEV_LP:
2919 /* Stop any LightPulse device specific driver timers */
2920 del_timer_sync(&phba->fcp_poll_timer);
2921 break;
2922 case LPFC_PCI_DEV_OC:
2923 /* Stop any OneConnect device sepcific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05002924 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04002925 break;
2926 default:
2927 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2928 "0297 Invalid device group (x%x)\n",
2929 phba->pci_dev_grp);
2930 break;
2931 }
James Smart2e0fef82007-06-17 19:56:36 -05002932 return;
dea31012005-04-17 16:05:31 -05002933}
2934
James Smarte59058c2008-08-24 21:49:00 -04002935/**
James Smart3621a712009-04-06 18:47:14 -04002936 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002937 * @phba: pointer to lpfc hba data structure.
2938 *
2939 * This routine marks a HBA's management interface as blocked. Once the HBA's
2940 * management interface is marked as blocked, all the user space access to
2941 * the HBA, whether they are from sysfs interface or libdfc interface will
2942 * all be blocked. The HBA is set to block the management interface when the
2943 * driver prepares the HBA interface for online or offline.
2944 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002945static void
James Smart618a5232012-06-12 13:54:36 -04002946lpfc_block_mgmt_io(struct lpfc_hba *phba, int mbx_action)
Adrian Bunka6ababd2007-11-05 18:07:33 +01002947{
2948 unsigned long iflag;
James Smart6e7288d2010-06-07 15:23:35 -04002949 uint8_t actcmd = MBX_HEARTBEAT;
2950 unsigned long timeout;
2951
Adrian Bunka6ababd2007-11-05 18:07:33 +01002952 spin_lock_irqsave(&phba->hbalock, iflag);
2953 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
James Smart618a5232012-06-12 13:54:36 -04002954 spin_unlock_irqrestore(&phba->hbalock, iflag);
2955 if (mbx_action == LPFC_MBX_NO_WAIT)
2956 return;
2957 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
2958 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002959 if (phba->sli.mbox_active) {
James Smart6e7288d2010-06-07 15:23:35 -04002960 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
James Smarta183a152011-10-10 21:32:43 -04002961 /* Determine how long we might wait for the active mailbox
2962 * command to be gracefully completed by firmware.
2963 */
2964 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
2965 phba->sli.mbox_active) * 1000) + jiffies;
2966 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002967 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002968
James Smart6e7288d2010-06-07 15:23:35 -04002969 /* Wait for the outstnading mailbox command to complete */
2970 while (phba->sli.mbox_active) {
2971 /* Check active mailbox complete status every 2ms */
2972 msleep(2);
2973 if (time_after(jiffies, timeout)) {
2974 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2975 "2813 Mgmt IO is Blocked %x "
2976 "- mbox cmd %x still active\n",
2977 phba->sli.sli_flag, actcmd);
2978 break;
2979 }
2980 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002981}
2982
James Smarte59058c2008-08-24 21:49:00 -04002983/**
James Smart6b5151f2012-01-18 16:24:06 -05002984 * lpfc_sli4_node_prep - Assign RPIs for active nodes.
2985 * @phba: pointer to lpfc hba data structure.
2986 *
2987 * Allocate RPIs for all active remote nodes. This is needed whenever
2988 * an SLI4 adapter is reset and the driver is not unloading. Its purpose
2989 * is to fixup the temporary rpi assignments.
2990 **/
2991void
2992lpfc_sli4_node_prep(struct lpfc_hba *phba)
2993{
2994 struct lpfc_nodelist *ndlp, *next_ndlp;
2995 struct lpfc_vport **vports;
James Smart9d3d3402017-04-21 16:05:00 -07002996 int i, rpi;
2997 unsigned long flags;
James Smart6b5151f2012-01-18 16:24:06 -05002998
2999 if (phba->sli_rev != LPFC_SLI_REV4)
3000 return;
3001
3002 vports = lpfc_create_vport_work_array(phba);
James Smart9d3d3402017-04-21 16:05:00 -07003003 if (vports == NULL)
3004 return;
James Smart6b5151f2012-01-18 16:24:06 -05003005
James Smart9d3d3402017-04-21 16:05:00 -07003006 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3007 if (vports[i]->load_flag & FC_UNLOADING)
3008 continue;
3009
3010 list_for_each_entry_safe(ndlp, next_ndlp,
3011 &vports[i]->fc_nodes,
3012 nlp_listp) {
3013 if (!NLP_CHK_NODE_ACT(ndlp))
3014 continue;
3015 rpi = lpfc_sli4_alloc_rpi(phba);
3016 if (rpi == LPFC_RPI_ALLOC_ERROR) {
3017 spin_lock_irqsave(&phba->ndlp_lock, flags);
3018 NLP_CLR_NODE_ACT(ndlp);
3019 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3020 continue;
James Smart6b5151f2012-01-18 16:24:06 -05003021 }
James Smart9d3d3402017-04-21 16:05:00 -07003022 ndlp->nlp_rpi = rpi;
3023 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3024 "0009 rpi:%x DID:%x "
3025 "flg:%x map:%x %p\n", ndlp->nlp_rpi,
3026 ndlp->nlp_DID, ndlp->nlp_flag,
3027 ndlp->nlp_usg_map, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -05003028 }
3029 }
3030 lpfc_destroy_vport_work_array(phba, vports);
3031}
3032
3033/**
James Smart3621a712009-04-06 18:47:14 -04003034 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04003035 * @phba: pointer to lpfc hba data structure.
3036 *
3037 * This routine initializes the HBA and brings a HBA online. During this
3038 * process, the management interface is blocked to prevent user space access
3039 * to the HBA interfering with the driver initialization.
3040 *
3041 * Return codes
3042 * 0 - successful
3043 * 1 - failed
3044 **/
dea31012005-04-17 16:05:31 -05003045int
James Smart2e0fef82007-06-17 19:56:36 -05003046lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003047{
Julia Lawall372bd282008-12-16 16:15:08 +01003048 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04003049 struct lpfc_vport **vports;
3050 int i;
James Smart16a3a202013-04-17 20:14:38 -04003051 bool vpis_cleared = false;
James Smart2e0fef82007-06-17 19:56:36 -05003052
dea31012005-04-17 16:05:31 -05003053 if (!phba)
3054 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01003055 vport = phba->pport;
dea31012005-04-17 16:05:31 -05003056
James Smart2e0fef82007-06-17 19:56:36 -05003057 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05003058 return 0;
3059
James Smarted957682007-06-17 19:56:37 -05003060 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003061 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05003062
James Smart618a5232012-06-12 13:54:36 -04003063 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05003064
James Smartda0436e2009-05-22 14:51:39 -04003065 if (phba->sli_rev == LPFC_SLI_REV4) {
3066 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
3067 lpfc_unblock_mgmt_io(phba);
3068 return 1;
3069 }
James Smart16a3a202013-04-17 20:14:38 -04003070 spin_lock_irq(&phba->hbalock);
3071 if (!phba->sli4_hba.max_cfg_param.vpi_used)
3072 vpis_cleared = true;
3073 spin_unlock_irq(&phba->hbalock);
James Smartda0436e2009-05-22 14:51:39 -04003074 } else {
James Smart895427b2017-02-12 13:52:30 -08003075 lpfc_sli_queue_init(phba);
James Smartda0436e2009-05-22 14:51:39 -04003076 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
3077 lpfc_unblock_mgmt_io(phba);
3078 return 1;
3079 }
James Smart46fa3112007-04-25 09:51:45 -04003080 }
dea31012005-04-17 16:05:31 -05003081
James Smart549e55c2007-08-02 11:09:51 -04003082 vports = lpfc_create_vport_work_array(phba);
Arnd Bergmannaeb66412016-03-14 15:29:44 +01003083 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04003084 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04003085 struct Scsi_Host *shost;
3086 shost = lpfc_shost_from_vport(vports[i]);
3087 spin_lock_irq(shost->host_lock);
3088 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
3089 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
3090 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart16a3a202013-04-17 20:14:38 -04003091 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart1c6834a2009-07-19 10:01:26 -04003092 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart16a3a202013-04-17 20:14:38 -04003093 if ((vpis_cleared) &&
3094 (vports[i]->port_type !=
3095 LPFC_PHYSICAL_PORT))
3096 vports[i]->vpi = 0;
3097 }
James Smart549e55c2007-08-02 11:09:51 -04003098 spin_unlock_irq(shost->host_lock);
3099 }
Arnd Bergmannaeb66412016-03-14 15:29:44 +01003100 }
3101 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05003102
James Smart46fa3112007-04-25 09:51:45 -04003103 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05003104 return 0;
3105}
3106
James Smarte59058c2008-08-24 21:49:00 -04003107/**
James Smart3621a712009-04-06 18:47:14 -04003108 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04003109 * @phba: pointer to lpfc hba data structure.
3110 *
3111 * This routine marks a HBA's management interface as not blocked. Once the
3112 * HBA's management interface is marked as not blocked, all the user space
3113 * access to the HBA, whether they are from sysfs interface or libdfc
3114 * interface will be allowed. The HBA is set to block the management interface
3115 * when the driver prepares the HBA interface for online or offline and then
3116 * set to unblock the management interface afterwards.
3117 **/
James Smart46fa3112007-04-25 09:51:45 -04003118void
James Smart46fa3112007-04-25 09:51:45 -04003119lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
3120{
3121 unsigned long iflag;
3122
James Smart2e0fef82007-06-17 19:56:36 -05003123 spin_lock_irqsave(&phba->hbalock, iflag);
3124 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
3125 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04003126}
3127
James Smarte59058c2008-08-24 21:49:00 -04003128/**
James Smart3621a712009-04-06 18:47:14 -04003129 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04003130 * @phba: pointer to lpfc hba data structure.
3131 *
3132 * This routine is invoked to prepare a HBA to be brought offline. It performs
3133 * unregistration login to all the nodes on all vports and flushes the mailbox
3134 * queue to make it ready to be brought offline.
3135 **/
James Smart46fa3112007-04-25 09:51:45 -04003136void
James Smart618a5232012-06-12 13:54:36 -04003137lpfc_offline_prep(struct lpfc_hba *phba, int mbx_action)
James Smart46fa3112007-04-25 09:51:45 -04003138{
James Smart2e0fef82007-06-17 19:56:36 -05003139 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04003140 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04003141 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05003142 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04003143 int i;
dea31012005-04-17 16:05:31 -05003144
James Smart2e0fef82007-06-17 19:56:36 -05003145 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04003146 return;
dea31012005-04-17 16:05:31 -05003147
James Smart618a5232012-06-12 13:54:36 -04003148 lpfc_block_mgmt_io(phba, mbx_action);
dea31012005-04-17 16:05:31 -05003149
3150 lpfc_linkdown(phba);
3151
James Smart87af33f2007-10-27 13:37:43 -04003152 /* Issue an unreg_login to all nodes on all vports */
3153 vports = lpfc_create_vport_work_array(phba);
3154 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04003155 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04003156 if (vports[i]->load_flag & FC_UNLOADING)
3157 continue;
James Smart72100cc2010-02-12 14:43:01 -05003158 shost = lpfc_shost_from_vport(vports[i]);
3159 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003160 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05003161 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
3162 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05003163 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05003164
James Smart87af33f2007-10-27 13:37:43 -04003165 shost = lpfc_shost_from_vport(vports[i]);
3166 list_for_each_entry_safe(ndlp, next_ndlp,
3167 &vports[i]->fc_nodes,
3168 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05003169 if (!NLP_CHK_NODE_ACT(ndlp))
3170 continue;
James Smart87af33f2007-10-27 13:37:43 -04003171 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
3172 continue;
3173 if (ndlp->nlp_type & NLP_FABRIC) {
3174 lpfc_disc_state_machine(vports[i], ndlp,
3175 NULL, NLP_EVT_DEVICE_RECOVERY);
3176 lpfc_disc_state_machine(vports[i], ndlp,
3177 NULL, NLP_EVT_DEVICE_RM);
3178 }
3179 spin_lock_irq(shost->host_lock);
3180 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart401ee0c2012-03-01 22:35:34 -05003181 spin_unlock_irq(shost->host_lock);
James Smart6b5151f2012-01-18 16:24:06 -05003182 /*
3183 * Whenever an SLI4 port goes offline, free the
James Smart401ee0c2012-03-01 22:35:34 -05003184 * RPI. Get a new RPI when the adapter port
3185 * comes back online.
James Smart6b5151f2012-01-18 16:24:06 -05003186 */
James Smartbe6bb942015-04-07 15:07:22 -04003187 if (phba->sli_rev == LPFC_SLI_REV4) {
3188 lpfc_printf_vlog(ndlp->vport,
3189 KERN_INFO, LOG_NODE,
3190 "0011 lpfc_offline: "
3191 "ndlp:x%p did %x "
3192 "usgmap:x%x rpi:%x\n",
3193 ndlp, ndlp->nlp_DID,
3194 ndlp->nlp_usg_map,
3195 ndlp->nlp_rpi);
3196
James Smart6b5151f2012-01-18 16:24:06 -05003197 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smartbe6bb942015-04-07 15:07:22 -04003198 }
James Smart87af33f2007-10-27 13:37:43 -04003199 lpfc_unreg_rpi(vports[i], ndlp);
3200 }
3201 }
3202 }
James Smart09372822008-01-11 01:52:54 -05003203 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05003204
James Smart618a5232012-06-12 13:54:36 -04003205 lpfc_sli_mbox_sys_shutdown(phba, mbx_action);
James Smart46fa3112007-04-25 09:51:45 -04003206}
3207
James Smarte59058c2008-08-24 21:49:00 -04003208/**
James Smart3621a712009-04-06 18:47:14 -04003209 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04003210 * @phba: pointer to lpfc hba data structure.
3211 *
3212 * This routine actually brings a HBA offline. It stops all the timers
3213 * associated with the HBA, brings down the SLI layer, and eventually
3214 * marks the HBA as in offline state for the upper layer protocol.
3215 **/
James Smart46fa3112007-04-25 09:51:45 -04003216void
James Smart2e0fef82007-06-17 19:56:36 -05003217lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04003218{
James Smart549e55c2007-08-02 11:09:51 -04003219 struct Scsi_Host *shost;
3220 struct lpfc_vport **vports;
3221 int i;
James Smart46fa3112007-04-25 09:51:45 -04003222
James Smart549e55c2007-08-02 11:09:51 -04003223 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04003224 return;
James Smart688a8862006-07-06 15:49:56 -04003225
James Smartda0436e2009-05-22 14:51:39 -04003226 /* stop port and all timers associated with this hba */
3227 lpfc_stop_port(phba);
Dick Kennedy4b40d022017-08-23 16:55:38 -07003228
3229 /* Tear down the local and target port registrations. The
3230 * nvme transports need to cleanup.
3231 */
3232 lpfc_nvmet_destroy_targetport(phba);
3233 lpfc_nvme_destroy_localport(phba->pport);
3234
James Smart51ef4c22007-08-02 11:10:31 -04003235 vports = lpfc_create_vport_work_array(phba);
3236 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04003237 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04003238 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05003239 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05003240 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003241 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05003242 /* Bring down the SLI Layer and cleanup. The HBA is offline
3243 now. */
3244 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003245 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04003246 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003247 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04003248 vports = lpfc_create_vport_work_array(phba);
3249 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04003250 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04003251 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04003252 spin_lock_irq(shost->host_lock);
3253 vports[i]->work_port_events = 0;
3254 vports[i]->fc_flag |= FC_OFFLINE_MODE;
3255 spin_unlock_irq(shost->host_lock);
3256 }
James Smart09372822008-01-11 01:52:54 -05003257 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05003258}
3259
James Smarte59058c2008-08-24 21:49:00 -04003260/**
James Smart3621a712009-04-06 18:47:14 -04003261 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04003262 * @phba: pointer to lpfc hba data structure.
3263 *
3264 * This routine is to free all the SCSI buffers and IOCBs from the driver
3265 * list back to kernel. It is called from lpfc_pci_remove_one to free
3266 * the internal resources before the device is removed from the system.
James Smarte59058c2008-08-24 21:49:00 -04003267 **/
James Smart8a9d2e82012-05-09 21:16:12 -04003268static void
James Smart2e0fef82007-06-17 19:56:36 -05003269lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003270{
3271 struct lpfc_scsi_buf *sb, *sb_next;
dea31012005-04-17 16:05:31 -05003272
James Smart895427b2017-02-12 13:52:30 -08003273 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
3274 return;
3275
James Smart2e0fef82007-06-17 19:56:36 -05003276 spin_lock_irq(&phba->hbalock);
James Smarta40fc5f2013-04-17 20:17:40 -04003277
dea31012005-04-17 16:05:31 -05003278 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smarta40fc5f2013-04-17 20:17:40 -04003279
3280 spin_lock(&phba->scsi_buf_list_put_lock);
3281 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_put,
3282 list) {
dea31012005-04-17 16:05:31 -05003283 list_del(&sb->list);
Romain Perier771db5c2017-07-06 10:13:05 +02003284 dma_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05003285 sb->dma_handle);
dea31012005-04-17 16:05:31 -05003286 kfree(sb);
3287 phba->total_scsi_bufs--;
3288 }
James Smarta40fc5f2013-04-17 20:17:40 -04003289 spin_unlock(&phba->scsi_buf_list_put_lock);
3290
3291 spin_lock(&phba->scsi_buf_list_get_lock);
3292 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_get,
3293 list) {
3294 list_del(&sb->list);
Romain Perier771db5c2017-07-06 10:13:05 +02003295 dma_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
James Smarta40fc5f2013-04-17 20:17:40 -04003296 sb->dma_handle);
3297 kfree(sb);
3298 phba->total_scsi_bufs--;
3299 }
3300 spin_unlock(&phba->scsi_buf_list_get_lock);
James Smart2e0fef82007-06-17 19:56:36 -05003301 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003302}
James Smart8a9d2e82012-05-09 21:16:12 -04003303/**
James Smart895427b2017-02-12 13:52:30 -08003304 * lpfc_nvme_free - Free all the NVME buffers and IOCBs from driver lists
3305 * @phba: pointer to lpfc hba data structure.
3306 *
3307 * This routine is to free all the NVME buffers and IOCBs from the driver
3308 * list back to kernel. It is called from lpfc_pci_remove_one to free
3309 * the internal resources before the device is removed from the system.
3310 **/
3311static void
3312lpfc_nvme_free(struct lpfc_hba *phba)
3313{
3314 struct lpfc_nvme_buf *lpfc_ncmd, *lpfc_ncmd_next;
James Smart895427b2017-02-12 13:52:30 -08003315
3316 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
3317 return;
3318
3319 spin_lock_irq(&phba->hbalock);
3320
3321 /* Release all the lpfc_nvme_bufs maintained by this host. */
3322 spin_lock(&phba->nvme_buf_list_put_lock);
3323 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3324 &phba->lpfc_nvme_buf_list_put, list) {
3325 list_del(&lpfc_ncmd->list);
Romain Perier771db5c2017-07-06 10:13:05 +02003326 dma_pool_free(phba->lpfc_sg_dma_buf_pool, lpfc_ncmd->data,
James Smart895427b2017-02-12 13:52:30 -08003327 lpfc_ncmd->dma_handle);
3328 kfree(lpfc_ncmd);
3329 phba->total_nvme_bufs--;
3330 }
3331 spin_unlock(&phba->nvme_buf_list_put_lock);
3332
3333 spin_lock(&phba->nvme_buf_list_get_lock);
3334 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3335 &phba->lpfc_nvme_buf_list_get, list) {
3336 list_del(&lpfc_ncmd->list);
Romain Perier771db5c2017-07-06 10:13:05 +02003337 dma_pool_free(phba->lpfc_sg_dma_buf_pool, lpfc_ncmd->data,
James Smart895427b2017-02-12 13:52:30 -08003338 lpfc_ncmd->dma_handle);
3339 kfree(lpfc_ncmd);
3340 phba->total_nvme_bufs--;
3341 }
3342 spin_unlock(&phba->nvme_buf_list_get_lock);
James Smart895427b2017-02-12 13:52:30 -08003343 spin_unlock_irq(&phba->hbalock);
3344}
3345/**
3346 * lpfc_sli4_els_sgl_update - update ELS xri-sgl sizing and mapping
James Smart8a9d2e82012-05-09 21:16:12 -04003347 * @phba: pointer to lpfc hba data structure.
3348 *
3349 * This routine first calculates the sizes of the current els and allocated
3350 * scsi sgl lists, and then goes through all sgls to updates the physical
3351 * XRIs assigned due to port function reset. During port initialization, the
3352 * current els and allocated scsi sgl lists are 0s.
3353 *
3354 * Return codes
3355 * 0 - successful (for now, it always returns 0)
3356 **/
3357int
James Smart895427b2017-02-12 13:52:30 -08003358lpfc_sli4_els_sgl_update(struct lpfc_hba *phba)
James Smart8a9d2e82012-05-09 21:16:12 -04003359{
3360 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
James Smart895427b2017-02-12 13:52:30 -08003361 uint16_t i, lxri, xri_cnt, els_xri_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04003362 LIST_HEAD(els_sgl_list);
James Smart8a9d2e82012-05-09 21:16:12 -04003363 int rc;
3364
3365 /*
3366 * update on pci function's els xri-sgl list
3367 */
3368 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
James Smart895427b2017-02-12 13:52:30 -08003369
James Smart8a9d2e82012-05-09 21:16:12 -04003370 if (els_xri_cnt > phba->sli4_hba.els_xri_cnt) {
3371 /* els xri-sgl expanded */
3372 xri_cnt = els_xri_cnt - phba->sli4_hba.els_xri_cnt;
3373 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3374 "3157 ELS xri-sgl count increased from "
3375 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3376 els_xri_cnt);
3377 /* allocate the additional els sgls */
3378 for (i = 0; i < xri_cnt; i++) {
3379 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3380 GFP_KERNEL);
3381 if (sglq_entry == NULL) {
3382 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3383 "2562 Failure to allocate an "
3384 "ELS sgl entry:%d\n", i);
3385 rc = -ENOMEM;
3386 goto out_free_mem;
3387 }
3388 sglq_entry->buff_type = GEN_BUFF_TYPE;
3389 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0,
3390 &sglq_entry->phys);
3391 if (sglq_entry->virt == NULL) {
3392 kfree(sglq_entry);
3393 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3394 "2563 Failure to allocate an "
3395 "ELS mbuf:%d\n", i);
3396 rc = -ENOMEM;
3397 goto out_free_mem;
3398 }
3399 sglq_entry->sgl = sglq_entry->virt;
3400 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
3401 sglq_entry->state = SGL_FREED;
3402 list_add_tail(&sglq_entry->list, &els_sgl_list);
3403 }
James Smart38c20672013-03-01 16:37:44 -05003404 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08003405 spin_lock(&phba->sli4_hba.sgl_list_lock);
3406 list_splice_init(&els_sgl_list,
3407 &phba->sli4_hba.lpfc_els_sgl_list);
3408 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05003409 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003410 } else if (els_xri_cnt < phba->sli4_hba.els_xri_cnt) {
3411 /* els xri-sgl shrinked */
3412 xri_cnt = phba->sli4_hba.els_xri_cnt - els_xri_cnt;
3413 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3414 "3158 ELS xri-sgl count decreased from "
3415 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3416 els_xri_cnt);
3417 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08003418 spin_lock(&phba->sli4_hba.sgl_list_lock);
3419 list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list,
3420 &els_sgl_list);
James Smart8a9d2e82012-05-09 21:16:12 -04003421 /* release extra els sgls from list */
3422 for (i = 0; i < xri_cnt; i++) {
3423 list_remove_head(&els_sgl_list,
3424 sglq_entry, struct lpfc_sglq, list);
3425 if (sglq_entry) {
James Smart895427b2017-02-12 13:52:30 -08003426 __lpfc_mbuf_free(phba, sglq_entry->virt,
3427 sglq_entry->phys);
James Smart8a9d2e82012-05-09 21:16:12 -04003428 kfree(sglq_entry);
3429 }
3430 }
James Smart895427b2017-02-12 13:52:30 -08003431 list_splice_init(&els_sgl_list,
3432 &phba->sli4_hba.lpfc_els_sgl_list);
3433 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003434 spin_unlock_irq(&phba->hbalock);
3435 } else
3436 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3437 "3163 ELS xri-sgl count unchanged: %d\n",
3438 els_xri_cnt);
3439 phba->sli4_hba.els_xri_cnt = els_xri_cnt;
3440
3441 /* update xris to els sgls on the list */
3442 sglq_entry = NULL;
3443 sglq_entry_next = NULL;
3444 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
James Smart895427b2017-02-12 13:52:30 -08003445 &phba->sli4_hba.lpfc_els_sgl_list, list) {
James Smart8a9d2e82012-05-09 21:16:12 -04003446 lxri = lpfc_sli4_next_xritag(phba);
3447 if (lxri == NO_XRI) {
3448 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3449 "2400 Failed to allocate xri for "
3450 "ELS sgl\n");
3451 rc = -ENOMEM;
3452 goto out_free_mem;
3453 }
3454 sglq_entry->sli4_lxritag = lxri;
3455 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3456 }
James Smart895427b2017-02-12 13:52:30 -08003457 return 0;
3458
3459out_free_mem:
3460 lpfc_free_els_sgl_list(phba);
3461 return rc;
3462}
3463
3464/**
James Smartf358dd02017-02-12 13:52:34 -08003465 * lpfc_sli4_nvmet_sgl_update - update xri-sgl sizing and mapping
3466 * @phba: pointer to lpfc hba data structure.
3467 *
3468 * This routine first calculates the sizes of the current els and allocated
3469 * scsi sgl lists, and then goes through all sgls to updates the physical
3470 * XRIs assigned due to port function reset. During port initialization, the
3471 * current els and allocated scsi sgl lists are 0s.
3472 *
3473 * Return codes
3474 * 0 - successful (for now, it always returns 0)
3475 **/
3476int
3477lpfc_sli4_nvmet_sgl_update(struct lpfc_hba *phba)
3478{
3479 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
3480 uint16_t i, lxri, xri_cnt, els_xri_cnt;
James Smart6c621a22017-05-15 15:20:45 -07003481 uint16_t nvmet_xri_cnt;
James Smartf358dd02017-02-12 13:52:34 -08003482 LIST_HEAD(nvmet_sgl_list);
3483 int rc;
3484
3485 /*
3486 * update on pci function's nvmet xri-sgl list
3487 */
3488 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
James Smart61f3d4b2017-05-15 15:20:41 -07003489
James Smart6c621a22017-05-15 15:20:45 -07003490 /* For NVMET, ALL remaining XRIs are dedicated for IO processing */
3491 nvmet_xri_cnt = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
James Smartf358dd02017-02-12 13:52:34 -08003492 if (nvmet_xri_cnt > phba->sli4_hba.nvmet_xri_cnt) {
3493 /* els xri-sgl expanded */
3494 xri_cnt = nvmet_xri_cnt - phba->sli4_hba.nvmet_xri_cnt;
3495 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3496 "6302 NVMET xri-sgl cnt grew from %d to %d\n",
3497 phba->sli4_hba.nvmet_xri_cnt, nvmet_xri_cnt);
3498 /* allocate the additional nvmet sgls */
3499 for (i = 0; i < xri_cnt; i++) {
3500 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3501 GFP_KERNEL);
3502 if (sglq_entry == NULL) {
3503 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3504 "6303 Failure to allocate an "
3505 "NVMET sgl entry:%d\n", i);
3506 rc = -ENOMEM;
3507 goto out_free_mem;
3508 }
3509 sglq_entry->buff_type = NVMET_BUFF_TYPE;
3510 sglq_entry->virt = lpfc_nvmet_buf_alloc(phba, 0,
3511 &sglq_entry->phys);
3512 if (sglq_entry->virt == NULL) {
3513 kfree(sglq_entry);
3514 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3515 "6304 Failure to allocate an "
3516 "NVMET buf:%d\n", i);
3517 rc = -ENOMEM;
3518 goto out_free_mem;
3519 }
3520 sglq_entry->sgl = sglq_entry->virt;
3521 memset(sglq_entry->sgl, 0,
3522 phba->cfg_sg_dma_buf_size);
3523 sglq_entry->state = SGL_FREED;
3524 list_add_tail(&sglq_entry->list, &nvmet_sgl_list);
3525 }
3526 spin_lock_irq(&phba->hbalock);
3527 spin_lock(&phba->sli4_hba.sgl_list_lock);
3528 list_splice_init(&nvmet_sgl_list,
3529 &phba->sli4_hba.lpfc_nvmet_sgl_list);
3530 spin_unlock(&phba->sli4_hba.sgl_list_lock);
3531 spin_unlock_irq(&phba->hbalock);
3532 } else if (nvmet_xri_cnt < phba->sli4_hba.nvmet_xri_cnt) {
3533 /* nvmet xri-sgl shrunk */
3534 xri_cnt = phba->sli4_hba.nvmet_xri_cnt - nvmet_xri_cnt;
3535 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3536 "6305 NVMET xri-sgl count decreased from "
3537 "%d to %d\n", phba->sli4_hba.nvmet_xri_cnt,
3538 nvmet_xri_cnt);
3539 spin_lock_irq(&phba->hbalock);
3540 spin_lock(&phba->sli4_hba.sgl_list_lock);
3541 list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list,
3542 &nvmet_sgl_list);
3543 /* release extra nvmet sgls from list */
3544 for (i = 0; i < xri_cnt; i++) {
3545 list_remove_head(&nvmet_sgl_list,
3546 sglq_entry, struct lpfc_sglq, list);
3547 if (sglq_entry) {
3548 lpfc_nvmet_buf_free(phba, sglq_entry->virt,
3549 sglq_entry->phys);
3550 kfree(sglq_entry);
3551 }
3552 }
3553 list_splice_init(&nvmet_sgl_list,
3554 &phba->sli4_hba.lpfc_nvmet_sgl_list);
3555 spin_unlock(&phba->sli4_hba.sgl_list_lock);
3556 spin_unlock_irq(&phba->hbalock);
3557 } else
3558 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3559 "6306 NVMET xri-sgl count unchanged: %d\n",
3560 nvmet_xri_cnt);
3561 phba->sli4_hba.nvmet_xri_cnt = nvmet_xri_cnt;
3562
3563 /* update xris to nvmet sgls on the list */
3564 sglq_entry = NULL;
3565 sglq_entry_next = NULL;
3566 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
3567 &phba->sli4_hba.lpfc_nvmet_sgl_list, list) {
3568 lxri = lpfc_sli4_next_xritag(phba);
3569 if (lxri == NO_XRI) {
3570 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3571 "6307 Failed to allocate xri for "
3572 "NVMET sgl\n");
3573 rc = -ENOMEM;
3574 goto out_free_mem;
3575 }
3576 sglq_entry->sli4_lxritag = lxri;
3577 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3578 }
3579 return 0;
3580
3581out_free_mem:
3582 lpfc_free_nvmet_sgl_list(phba);
3583 return rc;
3584}
3585
3586/**
James Smart895427b2017-02-12 13:52:30 -08003587 * lpfc_sli4_scsi_sgl_update - update xri-sgl sizing and mapping
3588 * @phba: pointer to lpfc hba data structure.
3589 *
3590 * This routine first calculates the sizes of the current els and allocated
3591 * scsi sgl lists, and then goes through all sgls to updates the physical
3592 * XRIs assigned due to port function reset. During port initialization, the
3593 * current els and allocated scsi sgl lists are 0s.
3594 *
3595 * Return codes
3596 * 0 - successful (for now, it always returns 0)
3597 **/
3598int
3599lpfc_sli4_scsi_sgl_update(struct lpfc_hba *phba)
3600{
3601 struct lpfc_scsi_buf *psb, *psb_next;
3602 uint16_t i, lxri, els_xri_cnt, scsi_xri_cnt;
3603 LIST_HEAD(scsi_sgl_list);
3604 int rc;
3605
3606 /*
3607 * update on pci function's els xri-sgl list
3608 */
3609 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3610 phba->total_scsi_bufs = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04003611
3612 /*
3613 * update on pci function's allocated scsi xri-sgl list
3614 */
James Smart8a9d2e82012-05-09 21:16:12 -04003615 /* maximum number of xris available for scsi buffers */
3616 phba->sli4_hba.scsi_xri_max = phba->sli4_hba.max_cfg_param.max_xri -
3617 els_xri_cnt;
3618
James Smart895427b2017-02-12 13:52:30 -08003619 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
3620 return 0;
3621
3622 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3623 phba->sli4_hba.scsi_xri_max = /* Split them up */
3624 (phba->sli4_hba.scsi_xri_max *
3625 phba->cfg_xri_split) / 100;
James Smart8a9d2e82012-05-09 21:16:12 -04003626
James Smarta40fc5f2013-04-17 20:17:40 -04003627 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart164cecd2013-09-06 12:22:38 -04003628 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003629 list_splice_init(&phba->lpfc_scsi_buf_list_get, &scsi_sgl_list);
3630 list_splice(&phba->lpfc_scsi_buf_list_put, &scsi_sgl_list);
James Smart164cecd2013-09-06 12:22:38 -04003631 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003632 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003633
James Smarte8c0a772017-04-21 16:04:49 -07003634 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3635 "6060 Current allocated SCSI xri-sgl count:%d, "
3636 "maximum SCSI xri count:%d (split:%d)\n",
3637 phba->sli4_hba.scsi_xri_cnt,
3638 phba->sli4_hba.scsi_xri_max, phba->cfg_xri_split);
3639
James Smart8a9d2e82012-05-09 21:16:12 -04003640 if (phba->sli4_hba.scsi_xri_cnt > phba->sli4_hba.scsi_xri_max) {
3641 /* max scsi xri shrinked below the allocated scsi buffers */
3642 scsi_xri_cnt = phba->sli4_hba.scsi_xri_cnt -
3643 phba->sli4_hba.scsi_xri_max;
3644 /* release the extra allocated scsi buffers */
3645 for (i = 0; i < scsi_xri_cnt; i++) {
3646 list_remove_head(&scsi_sgl_list, psb,
3647 struct lpfc_scsi_buf, list);
James Smarta2fc4aef2014-09-03 12:57:55 -04003648 if (psb) {
Romain Perier771db5c2017-07-06 10:13:05 +02003649 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
James Smarta2fc4aef2014-09-03 12:57:55 -04003650 psb->data, psb->dma_handle);
3651 kfree(psb);
3652 }
James Smart8a9d2e82012-05-09 21:16:12 -04003653 }
James Smarta40fc5f2013-04-17 20:17:40 -04003654 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003655 phba->sli4_hba.scsi_xri_cnt -= scsi_xri_cnt;
James Smarta40fc5f2013-04-17 20:17:40 -04003656 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003657 }
3658
3659 /* update xris associated to remaining allocated scsi buffers */
3660 psb = NULL;
3661 psb_next = NULL;
3662 list_for_each_entry_safe(psb, psb_next, &scsi_sgl_list, list) {
3663 lxri = lpfc_sli4_next_xritag(phba);
3664 if (lxri == NO_XRI) {
3665 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3666 "2560 Failed to allocate xri for "
3667 "scsi buffer\n");
3668 rc = -ENOMEM;
3669 goto out_free_mem;
3670 }
3671 psb->cur_iocbq.sli4_lxritag = lxri;
3672 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3673 }
James Smarta40fc5f2013-04-17 20:17:40 -04003674 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart164cecd2013-09-06 12:22:38 -04003675 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003676 list_splice_init(&scsi_sgl_list, &phba->lpfc_scsi_buf_list_get);
3677 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
James Smart164cecd2013-09-06 12:22:38 -04003678 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003679 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
dea31012005-04-17 16:05:31 -05003680 return 0;
James Smart8a9d2e82012-05-09 21:16:12 -04003681
3682out_free_mem:
James Smart8a9d2e82012-05-09 21:16:12 -04003683 lpfc_scsi_free(phba);
3684 return rc;
dea31012005-04-17 16:05:31 -05003685}
3686
James Smart96418b52017-03-04 09:30:31 -08003687static uint64_t
3688lpfc_get_wwpn(struct lpfc_hba *phba)
3689{
3690 uint64_t wwn;
3691 int rc;
3692 LPFC_MBOXQ_t *mboxq;
3693 MAILBOX_t *mb;
3694
James Smart96418b52017-03-04 09:30:31 -08003695 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
3696 GFP_KERNEL);
3697 if (!mboxq)
3698 return (uint64_t)-1;
3699
3700 /* First get WWN of HBA instance */
3701 lpfc_read_nv(phba, mboxq);
3702 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
3703 if (rc != MBX_SUCCESS) {
3704 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3705 "6019 Mailbox failed , mbxCmd x%x "
3706 "READ_NV, mbxStatus x%x\n",
3707 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
3708 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
3709 mempool_free(mboxq, phba->mbox_mem_pool);
3710 return (uint64_t) -1;
3711 }
3712 mb = &mboxq->u.mb;
3713 memcpy(&wwn, (char *)mb->un.varRDnvp.portname, sizeof(uint64_t));
3714 /* wwn is WWPN of HBA instance */
3715 mempool_free(mboxq, phba->mbox_mem_pool);
3716 if (phba->sli_rev == LPFC_SLI_REV4)
3717 return be64_to_cpu(wwn);
3718 else
3719 return (((wwn & 0xffffffff00000000) >> 32) |
3720 ((wwn & 0x00000000ffffffff) << 32));
3721
3722}
3723
James Smarte59058c2008-08-24 21:49:00 -04003724/**
James Smart895427b2017-02-12 13:52:30 -08003725 * lpfc_sli4_nvme_sgl_update - update xri-sgl sizing and mapping
3726 * @phba: pointer to lpfc hba data structure.
3727 *
3728 * This routine first calculates the sizes of the current els and allocated
3729 * scsi sgl lists, and then goes through all sgls to updates the physical
3730 * XRIs assigned due to port function reset. During port initialization, the
3731 * current els and allocated scsi sgl lists are 0s.
3732 *
3733 * Return codes
3734 * 0 - successful (for now, it always returns 0)
3735 **/
3736int
3737lpfc_sli4_nvme_sgl_update(struct lpfc_hba *phba)
3738{
3739 struct lpfc_nvme_buf *lpfc_ncmd = NULL, *lpfc_ncmd_next = NULL;
3740 uint16_t i, lxri, els_xri_cnt;
3741 uint16_t nvme_xri_cnt, nvme_xri_max;
3742 LIST_HEAD(nvme_sgl_list);
3743 int rc;
3744
3745 phba->total_nvme_bufs = 0;
3746
3747 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
3748 return 0;
3749 /*
3750 * update on pci function's allocated nvme xri-sgl list
3751 */
3752
3753 /* maximum number of xris available for nvme buffers */
3754 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3755 nvme_xri_max = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
3756 phba->sli4_hba.nvme_xri_max = nvme_xri_max;
3757 phba->sli4_hba.nvme_xri_max -= phba->sli4_hba.scsi_xri_max;
3758
3759 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3760 "6074 Current allocated NVME xri-sgl count:%d, "
3761 "maximum NVME xri count:%d\n",
3762 phba->sli4_hba.nvme_xri_cnt,
3763 phba->sli4_hba.nvme_xri_max);
3764
3765 spin_lock_irq(&phba->nvme_buf_list_get_lock);
3766 spin_lock(&phba->nvme_buf_list_put_lock);
3767 list_splice_init(&phba->lpfc_nvme_buf_list_get, &nvme_sgl_list);
3768 list_splice(&phba->lpfc_nvme_buf_list_put, &nvme_sgl_list);
3769 spin_unlock(&phba->nvme_buf_list_put_lock);
3770 spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3771
3772 if (phba->sli4_hba.nvme_xri_cnt > phba->sli4_hba.nvme_xri_max) {
3773 /* max nvme xri shrunk below the allocated nvme buffers */
3774 spin_lock_irq(&phba->nvme_buf_list_get_lock);
3775 nvme_xri_cnt = phba->sli4_hba.nvme_xri_cnt -
3776 phba->sli4_hba.nvme_xri_max;
3777 spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3778 /* release the extra allocated nvme buffers */
3779 for (i = 0; i < nvme_xri_cnt; i++) {
3780 list_remove_head(&nvme_sgl_list, lpfc_ncmd,
3781 struct lpfc_nvme_buf, list);
3782 if (lpfc_ncmd) {
Romain Perier771db5c2017-07-06 10:13:05 +02003783 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
James Smart895427b2017-02-12 13:52:30 -08003784 lpfc_ncmd->data,
3785 lpfc_ncmd->dma_handle);
3786 kfree(lpfc_ncmd);
3787 }
3788 }
3789 spin_lock_irq(&phba->nvme_buf_list_get_lock);
3790 phba->sli4_hba.nvme_xri_cnt -= nvme_xri_cnt;
3791 spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3792 }
3793
3794 /* update xris associated to remaining allocated nvme buffers */
3795 lpfc_ncmd = NULL;
3796 lpfc_ncmd_next = NULL;
3797 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3798 &nvme_sgl_list, list) {
3799 lxri = lpfc_sli4_next_xritag(phba);
3800 if (lxri == NO_XRI) {
3801 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3802 "6075 Failed to allocate xri for "
3803 "nvme buffer\n");
3804 rc = -ENOMEM;
3805 goto out_free_mem;
3806 }
3807 lpfc_ncmd->cur_iocbq.sli4_lxritag = lxri;
3808 lpfc_ncmd->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3809 }
3810 spin_lock_irq(&phba->nvme_buf_list_get_lock);
3811 spin_lock(&phba->nvme_buf_list_put_lock);
3812 list_splice_init(&nvme_sgl_list, &phba->lpfc_nvme_buf_list_get);
3813 INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_put);
3814 spin_unlock(&phba->nvme_buf_list_put_lock);
3815 spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3816 return 0;
3817
3818out_free_mem:
3819 lpfc_nvme_free(phba);
3820 return rc;
3821}
3822
3823/**
James Smart3621a712009-04-06 18:47:14 -04003824 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04003825 * @phba: pointer to lpfc hba data structure.
3826 * @instance: a unique integer ID to this FC port.
3827 * @dev: pointer to the device data structure.
3828 *
3829 * This routine creates a FC port for the upper layer protocol. The FC port
3830 * can be created on top of either a physical port or a virtual port provided
3831 * by the HBA. This routine also allocates a SCSI host data structure (shost)
3832 * and associates the FC port created before adding the shost into the SCSI
3833 * layer.
3834 *
3835 * Return codes
3836 * @vport - pointer to the virtual N_Port data structure.
3837 * NULL - port create failed.
3838 **/
James Smart2e0fef82007-06-17 19:56:36 -05003839struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04003840lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04003841{
James Smart2e0fef82007-06-17 19:56:36 -05003842 struct lpfc_vport *vport;
James Smart895427b2017-02-12 13:52:30 -08003843 struct Scsi_Host *shost = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003844 int error = 0;
James Smart96418b52017-03-04 09:30:31 -08003845 int i;
3846 uint64_t wwn;
3847 bool use_no_reset_hba = false;
James Smart56bc8022017-06-15 22:56:43 -07003848 int rc;
James Smart96418b52017-03-04 09:30:31 -08003849
James Smart56bc8022017-06-15 22:56:43 -07003850 if (lpfc_no_hba_reset_cnt) {
3851 if (phba->sli_rev < LPFC_SLI_REV4 &&
3852 dev == &phba->pcidev->dev) {
3853 /* Reset the port first */
3854 lpfc_sli_brdrestart(phba);
3855 rc = lpfc_sli_chipset_init(phba);
3856 if (rc)
3857 return NULL;
3858 }
3859 wwn = lpfc_get_wwpn(phba);
3860 }
James Smart96418b52017-03-04 09:30:31 -08003861
3862 for (i = 0; i < lpfc_no_hba_reset_cnt; i++) {
3863 if (wwn == lpfc_no_hba_reset[i]) {
3864 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3865 "6020 Setting use_no_reset port=%llx\n",
3866 wwn);
3867 use_no_reset_hba = true;
3868 break;
3869 }
3870 }
James Smart47a86172007-04-25 09:53:22 -04003871
James Smart895427b2017-02-12 13:52:30 -08003872 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
3873 if (dev != &phba->pcidev->dev) {
3874 shost = scsi_host_alloc(&lpfc_vport_template,
3875 sizeof(struct lpfc_vport));
3876 } else {
James Smart96418b52017-03-04 09:30:31 -08003877 if (!use_no_reset_hba)
James Smart895427b2017-02-12 13:52:30 -08003878 shost = scsi_host_alloc(&lpfc_template,
3879 sizeof(struct lpfc_vport));
3880 else
James Smart96418b52017-03-04 09:30:31 -08003881 shost = scsi_host_alloc(&lpfc_template_no_hr,
James Smart895427b2017-02-12 13:52:30 -08003882 sizeof(struct lpfc_vport));
3883 }
3884 } else if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
3885 shost = scsi_host_alloc(&lpfc_template_nvme,
James Smartea4142f2015-04-07 15:07:13 -04003886 sizeof(struct lpfc_vport));
3887 }
James Smart2e0fef82007-06-17 19:56:36 -05003888 if (!shost)
3889 goto out;
James Smart47a86172007-04-25 09:53:22 -04003890
James Smart2e0fef82007-06-17 19:56:36 -05003891 vport = (struct lpfc_vport *) shost->hostdata;
3892 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05003893 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05003894 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05003895 vport->fc_rscn_flush = 0;
James Smart3de2a652007-08-02 11:09:59 -04003896 lpfc_get_vport_cfgparam(vport);
James Smart895427b2017-02-12 13:52:30 -08003897
James Smart2e0fef82007-06-17 19:56:36 -05003898 shost->unique_id = instance;
3899 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04003900 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05003901 shost->this_id = -1;
3902 shost->max_cmd_len = 16;
James Smart8b0dff12015-05-22 10:42:38 -04003903 shost->nr_hw_queues = phba->cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04003904 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart28baac72010-02-12 14:42:03 -05003905 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04003906 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smartda0436e2009-05-22 14:51:39 -04003907 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
3908 }
James Smart81301a92008-12-04 22:39:46 -05003909
James Smart47a86172007-04-25 09:53:22 -04003910 /*
James Smart2e0fef82007-06-17 19:56:36 -05003911 * Set initial can_queue value since 0 is no longer supported and
3912 * scsi_add_host will fail. This will be adjusted later based on the
3913 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04003914 */
James Smart2e0fef82007-06-17 19:56:36 -05003915 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04003916 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05003917 shost->transportt = lpfc_vport_transport_template;
3918 vport->port_type = LPFC_NPIV_PORT;
3919 } else {
3920 shost->transportt = lpfc_transport_template;
3921 vport->port_type = LPFC_PHYSICAL_PORT;
3922 }
James Smart47a86172007-04-25 09:53:22 -04003923
James Smart2e0fef82007-06-17 19:56:36 -05003924 /* Initialize all internally managed lists. */
3925 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04003926 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05003927 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04003928
Tomas Jasek33cc5592017-03-03 13:45:48 +01003929 setup_timer(&vport->fc_disctmo, lpfc_disc_timeout,
3930 (unsigned long)vport);
James Smart47a86172007-04-25 09:53:22 -04003931
Tomas Jasek33cc5592017-03-03 13:45:48 +01003932 setup_timer(&vport->els_tmofunc, lpfc_els_timeout,
3933 (unsigned long)vport);
James Smart92494142011-02-16 12:39:44 -05003934
Tomas Jasek33cc5592017-03-03 13:45:48 +01003935 setup_timer(&vport->delayed_disc_tmo, lpfc_delayed_disc_tmo,
3936 (unsigned long)vport);
James Smart92494142011-02-16 12:39:44 -05003937
James Bottomleyd139b9b2009-11-05 13:33:12 -06003938 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05003939 if (error)
3940 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04003941
James Smart549e55c2007-08-02 11:09:51 -04003942 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003943 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04003944 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003945 return vport;
James Smart47a86172007-04-25 09:53:22 -04003946
James Smart2e0fef82007-06-17 19:56:36 -05003947out_put_shost:
3948 scsi_host_put(shost);
3949out:
3950 return NULL;
James Smart47a86172007-04-25 09:53:22 -04003951}
3952
James Smarte59058c2008-08-24 21:49:00 -04003953/**
James Smart3621a712009-04-06 18:47:14 -04003954 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04003955 * @vport: pointer to an lpfc virtual N_Port data structure.
3956 *
3957 * This routine destroys a FC port from the upper layer protocol. All the
3958 * resources associated with the port are released.
3959 **/
James Smart2e0fef82007-06-17 19:56:36 -05003960void
3961destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04003962{
James Smart92d7f7b2007-06-17 19:56:38 -05003963 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3964 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003965
James Smart858c9f62007-06-17 19:56:39 -05003966 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05003967 fc_remove_host(shost);
3968 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04003969
James Smart92d7f7b2007-06-17 19:56:38 -05003970 spin_lock_irq(&phba->hbalock);
3971 list_del_init(&vport->listentry);
3972 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04003973
James Smart92d7f7b2007-06-17 19:56:38 -05003974 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04003975 return;
James Smart47a86172007-04-25 09:53:22 -04003976}
3977
James Smarte59058c2008-08-24 21:49:00 -04003978/**
James Smart3621a712009-04-06 18:47:14 -04003979 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04003980 *
3981 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
3982 * uses the kernel idr facility to perform the task.
3983 *
3984 * Return codes:
3985 * instance - a unique integer ID allocated as the new instance.
3986 * -1 - lpfc get instance failed.
3987 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003988int
3989lpfc_get_instance(void)
3990{
Tejun Heoab516032013-02-27 17:04:44 -08003991 int ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003992
Tejun Heoab516032013-02-27 17:04:44 -08003993 ret = idr_alloc(&lpfc_hba_index, NULL, 0, 0, GFP_KERNEL);
3994 return ret < 0 ? -1 : ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003995}
3996
James Smarte59058c2008-08-24 21:49:00 -04003997/**
James Smart3621a712009-04-06 18:47:14 -04003998 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04003999 * @shost: pointer to SCSI host data structure.
4000 * @time: elapsed time of the scan in jiffies.
4001 *
4002 * This routine is called by the SCSI layer with a SCSI host to determine
4003 * whether the scan host is finished.
4004 *
4005 * Note: there is no scan_start function as adapter initialization will have
4006 * asynchronously kicked off the link initialization.
4007 *
4008 * Return codes
4009 * 0 - SCSI host scan is not over yet.
4010 * 1 - SCSI host scan is over.
4011 **/
James Smart47a86172007-04-25 09:53:22 -04004012int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
4013{
James Smart2e0fef82007-06-17 19:56:36 -05004014 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4015 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05004016 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04004017
James Smart858c9f62007-06-17 19:56:39 -05004018 spin_lock_irq(shost->host_lock);
4019
James Smart51ef4c22007-08-02 11:10:31 -04004020 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05004021 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04004022 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05004023 }
James Smart256ec0d2013-04-17 20:14:58 -04004024 if (time >= msecs_to_jiffies(30 * 1000)) {
James Smart2e0fef82007-06-17 19:56:36 -05004025 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004026 "0461 Scanning longer than 30 "
4027 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05004028 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04004029 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05004030 }
James Smart256ec0d2013-04-17 20:14:58 -04004031 if (time >= msecs_to_jiffies(15 * 1000) &&
4032 phba->link_state <= LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -05004033 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004034 "0465 Link down longer than 15 "
4035 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05004036 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05004037 goto finished;
James Smart47a86172007-04-25 09:53:22 -04004038 }
4039
James Smart2e0fef82007-06-17 19:56:36 -05004040 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05004041 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05004042 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05004043 goto finished;
James Smart256ec0d2013-04-17 20:14:58 -04004044 if (vport->fc_map_cnt == 0 && time < msecs_to_jiffies(2 * 1000))
James Smart858c9f62007-06-17 19:56:39 -05004045 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05004046 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05004047 goto finished;
4048
4049 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04004050
4051finished:
James Smart858c9f62007-06-17 19:56:39 -05004052 spin_unlock_irq(shost->host_lock);
4053 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05004054}
4055
James Smarte59058c2008-08-24 21:49:00 -04004056/**
James Smart3621a712009-04-06 18:47:14 -04004057 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04004058 * @shost: pointer to SCSI host data structure.
4059 *
4060 * This routine initializes a given SCSI host attributes on a FC port. The
4061 * SCSI host can be either on top of a physical port or a virtual port.
4062 **/
James Smart92d7f7b2007-06-17 19:56:38 -05004063void lpfc_host_attrib_init(struct Scsi_Host *shost)
4064{
4065 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4066 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04004067 /*
James Smart2e0fef82007-06-17 19:56:36 -05004068 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04004069 */
4070
James Smart2e0fef82007-06-17 19:56:36 -05004071 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4072 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04004073 fc_host_supported_classes(shost) = FC_COS_CLASS3;
4074
4075 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05004076 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04004077 fc_host_supported_fc4s(shost)[2] = 1;
4078 fc_host_supported_fc4s(shost)[7] = 1;
4079
James Smart92d7f7b2007-06-17 19:56:38 -05004080 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
4081 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04004082
4083 fc_host_supported_speeds(shost) = 0;
James Smartd38dd522015-08-31 16:48:17 -04004084 if (phba->lmt & LMT_32Gb)
4085 fc_host_supported_speeds(shost) |= FC_PORTSPEED_32GBIT;
James Smart88a2cfb2011-07-22 18:36:33 -04004086 if (phba->lmt & LMT_16Gb)
4087 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
James Smart47a86172007-04-25 09:53:22 -04004088 if (phba->lmt & LMT_10Gb)
4089 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04004090 if (phba->lmt & LMT_8Gb)
4091 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04004092 if (phba->lmt & LMT_4Gb)
4093 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
4094 if (phba->lmt & LMT_2Gb)
4095 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
4096 if (phba->lmt & LMT_1Gb)
4097 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
4098
4099 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05004100 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
4101 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04004102
Mike Christie0af5d702010-09-15 16:52:31 -05004103 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
4104
James Smart47a86172007-04-25 09:53:22 -04004105 /* This value is also unchanging */
4106 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05004107 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04004108 fc_host_active_fc4s(shost)[2] = 1;
4109 fc_host_active_fc4s(shost)[7] = 1;
4110
James Smart92d7f7b2007-06-17 19:56:38 -05004111 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04004112 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04004113 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04004114 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04004115}
dea31012005-04-17 16:05:31 -05004116
James Smarte59058c2008-08-24 21:49:00 -04004117/**
James Smartda0436e2009-05-22 14:51:39 -04004118 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04004119 * @phba: pointer to lpfc hba data structure.
4120 *
James Smartda0436e2009-05-22 14:51:39 -04004121 * This routine is invoked to stop an SLI3 device port, it stops the device
4122 * from generating interrupts and stops the device driver's timers for the
4123 * device.
James Smarte59058c2008-08-24 21:49:00 -04004124 **/
James Smartdb2378e2008-02-08 18:49:51 -05004125static void
James Smartda0436e2009-05-22 14:51:39 -04004126lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05004127{
James Smartda0436e2009-05-22 14:51:39 -04004128 /* Clear all interrupt enable conditions */
4129 writel(0, phba->HCregaddr);
4130 readl(phba->HCregaddr); /* flush */
4131 /* Clear all pending interrupts */
4132 writel(0xffffffff, phba->HAregaddr);
4133 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04004134
James Smartda0436e2009-05-22 14:51:39 -04004135 /* Reset some HBA SLI setup states */
4136 lpfc_stop_hba_timers(phba);
4137 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05004138}
4139
James Smarte59058c2008-08-24 21:49:00 -04004140/**
James Smartda0436e2009-05-22 14:51:39 -04004141 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05004142 * @phba: pointer to lpfc hba data structure.
4143 *
James Smartda0436e2009-05-22 14:51:39 -04004144 * This routine is invoked to stop an SLI4 device port, it stops the device
4145 * from generating interrupts and stops the device driver's timers for the
4146 * device.
4147 **/
4148static void
4149lpfc_stop_port_s4(struct lpfc_hba *phba)
4150{
4151 /* Reset some HBA SLI4 setup states */
4152 lpfc_stop_hba_timers(phba);
4153 phba->pport->work_port_events = 0;
4154 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04004155}
4156
4157/**
4158 * lpfc_stop_port - Wrapper function for stopping hba port
4159 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05004160 *
James Smartda0436e2009-05-22 14:51:39 -04004161 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
4162 * the API jump table function pointer from the lpfc_hba struct.
4163 **/
4164void
4165lpfc_stop_port(struct lpfc_hba *phba)
4166{
4167 phba->lpfc_stop_port(phba);
4168}
4169
4170/**
James Smartecfd03c2010-02-12 14:41:27 -05004171 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
4172 * @phba: Pointer to hba for which this call is being executed.
4173 *
4174 * This routine starts the timer waiting for the FCF rediscovery to complete.
4175 **/
4176void
4177lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
4178{
4179 unsigned long fcf_redisc_wait_tmo =
4180 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
4181 /* Start fcf rediscovery wait period timer */
4182 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
4183 spin_lock_irq(&phba->hbalock);
4184 /* Allow action to new fcf asynchronous event */
4185 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
4186 /* Mark the FCF rediscovery pending state */
4187 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
4188 spin_unlock_irq(&phba->hbalock);
4189}
4190
4191/**
4192 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
4193 * @ptr: Map to lpfc_hba data structure pointer.
4194 *
4195 * This routine is invoked when waiting for FCF table rediscover has been
4196 * timed out. If new FCF record(s) has (have) been discovered during the
4197 * wait period, a new FCF event shall be added to the FCOE async event
4198 * list, and then worker thread shall be waked up for processing from the
4199 * worker thread context.
4200 **/
Rashika Kheriae399b222014-09-03 12:55:28 -04004201static void
James Smartecfd03c2010-02-12 14:41:27 -05004202lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
4203{
4204 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
4205
4206 /* Don't send FCF rediscovery event if timer cancelled */
4207 spin_lock_irq(&phba->hbalock);
4208 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
4209 spin_unlock_irq(&phba->hbalock);
4210 return;
4211 }
4212 /* Clear FCF rediscovery timer pending flag */
4213 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
4214 /* FCF rediscovery event to worker thread */
4215 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
4216 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004217 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04004218 "2776 FCF rediscover quiescent timer expired\n");
James Smartecfd03c2010-02-12 14:41:27 -05004219 /* wake up worker thread */
4220 lpfc_worker_wake_up(phba);
4221}
4222
4223/**
James Smartda0436e2009-05-22 14:51:39 -04004224 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
4225 * @phba: pointer to lpfc hba data structure.
4226 * @acqe_link: pointer to the async link completion queue entry.
4227 *
4228 * This routine is to parse the SLI4 link-attention link fault code and
4229 * translate it into the base driver's read link attention mailbox command
4230 * status.
4231 *
4232 * Return: Link-attention status in terms of base driver's coding.
4233 **/
4234static uint16_t
4235lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
4236 struct lpfc_acqe_link *acqe_link)
4237{
4238 uint16_t latt_fault;
4239
4240 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
4241 case LPFC_ASYNC_LINK_FAULT_NONE:
4242 case LPFC_ASYNC_LINK_FAULT_LOCAL:
4243 case LPFC_ASYNC_LINK_FAULT_REMOTE:
4244 latt_fault = 0;
4245 break;
4246 default:
4247 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4248 "0398 Invalid link fault code: x%x\n",
4249 bf_get(lpfc_acqe_link_fault, acqe_link));
4250 latt_fault = MBXERR_ERROR;
4251 break;
4252 }
4253 return latt_fault;
4254}
4255
4256/**
4257 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
4258 * @phba: pointer to lpfc hba data structure.
4259 * @acqe_link: pointer to the async link completion queue entry.
4260 *
4261 * This routine is to parse the SLI4 link attention type and translate it
4262 * into the base driver's link attention type coding.
4263 *
4264 * Return: Link attention type in terms of base driver's coding.
4265 **/
4266static uint8_t
4267lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
4268 struct lpfc_acqe_link *acqe_link)
4269{
4270 uint8_t att_type;
4271
4272 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
4273 case LPFC_ASYNC_LINK_STATUS_DOWN:
4274 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
James Smart76a95d72010-11-20 23:11:48 -05004275 att_type = LPFC_ATT_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04004276 break;
4277 case LPFC_ASYNC_LINK_STATUS_UP:
4278 /* Ignore physical link up events - wait for logical link up */
James Smart76a95d72010-11-20 23:11:48 -05004279 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04004280 break;
4281 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
James Smart76a95d72010-11-20 23:11:48 -05004282 att_type = LPFC_ATT_LINK_UP;
James Smartda0436e2009-05-22 14:51:39 -04004283 break;
4284 default:
4285 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4286 "0399 Invalid link attention type: x%x\n",
4287 bf_get(lpfc_acqe_link_status, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05004288 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04004289 break;
4290 }
4291 return att_type;
4292}
4293
4294/**
James Smart8b68cd52012-09-29 11:32:37 -04004295 * lpfc_sli_port_speed_get - Get sli3 link speed code to link speed
4296 * @phba: pointer to lpfc hba data structure.
4297 *
4298 * This routine is to get an SLI3 FC port's link speed in Mbps.
4299 *
4300 * Return: link speed in terms of Mbps.
4301 **/
4302uint32_t
4303lpfc_sli_port_speed_get(struct lpfc_hba *phba)
4304{
4305 uint32_t link_speed;
4306
4307 if (!lpfc_is_link_up(phba))
4308 return 0;
4309
James Smarta085e872015-12-16 18:12:02 -05004310 if (phba->sli_rev <= LPFC_SLI_REV3) {
4311 switch (phba->fc_linkspeed) {
4312 case LPFC_LINK_SPEED_1GHZ:
4313 link_speed = 1000;
4314 break;
4315 case LPFC_LINK_SPEED_2GHZ:
4316 link_speed = 2000;
4317 break;
4318 case LPFC_LINK_SPEED_4GHZ:
4319 link_speed = 4000;
4320 break;
4321 case LPFC_LINK_SPEED_8GHZ:
4322 link_speed = 8000;
4323 break;
4324 case LPFC_LINK_SPEED_10GHZ:
4325 link_speed = 10000;
4326 break;
4327 case LPFC_LINK_SPEED_16GHZ:
4328 link_speed = 16000;
4329 break;
4330 default:
4331 link_speed = 0;
4332 }
4333 } else {
4334 if (phba->sli4_hba.link_state.logical_speed)
4335 link_speed =
4336 phba->sli4_hba.link_state.logical_speed;
4337 else
4338 link_speed = phba->sli4_hba.link_state.speed;
James Smart8b68cd52012-09-29 11:32:37 -04004339 }
4340 return link_speed;
4341}
4342
4343/**
4344 * lpfc_sli4_port_speed_parse - Parse async evt link speed code to link speed
4345 * @phba: pointer to lpfc hba data structure.
4346 * @evt_code: asynchronous event code.
4347 * @speed_code: asynchronous event link speed code.
4348 *
4349 * This routine is to parse the giving SLI4 async event link speed code into
4350 * value of Mbps for the link speed.
4351 *
4352 * Return: link speed in terms of Mbps.
4353 **/
4354static uint32_t
4355lpfc_sli4_port_speed_parse(struct lpfc_hba *phba, uint32_t evt_code,
4356 uint8_t speed_code)
4357{
4358 uint32_t port_speed;
4359
4360 switch (evt_code) {
4361 case LPFC_TRAILER_CODE_LINK:
4362 switch (speed_code) {
James Smart26d830e2015-04-07 15:07:17 -04004363 case LPFC_ASYNC_LINK_SPEED_ZERO:
James Smart8b68cd52012-09-29 11:32:37 -04004364 port_speed = 0;
4365 break;
James Smart26d830e2015-04-07 15:07:17 -04004366 case LPFC_ASYNC_LINK_SPEED_10MBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004367 port_speed = 10;
4368 break;
James Smart26d830e2015-04-07 15:07:17 -04004369 case LPFC_ASYNC_LINK_SPEED_100MBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004370 port_speed = 100;
4371 break;
James Smart26d830e2015-04-07 15:07:17 -04004372 case LPFC_ASYNC_LINK_SPEED_1GBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004373 port_speed = 1000;
4374 break;
James Smart26d830e2015-04-07 15:07:17 -04004375 case LPFC_ASYNC_LINK_SPEED_10GBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004376 port_speed = 10000;
4377 break;
James Smart26d830e2015-04-07 15:07:17 -04004378 case LPFC_ASYNC_LINK_SPEED_20GBPS:
4379 port_speed = 20000;
4380 break;
4381 case LPFC_ASYNC_LINK_SPEED_25GBPS:
4382 port_speed = 25000;
4383 break;
4384 case LPFC_ASYNC_LINK_SPEED_40GBPS:
4385 port_speed = 40000;
4386 break;
James Smart8b68cd52012-09-29 11:32:37 -04004387 default:
4388 port_speed = 0;
4389 }
4390 break;
4391 case LPFC_TRAILER_CODE_FC:
4392 switch (speed_code) {
James Smart26d830e2015-04-07 15:07:17 -04004393 case LPFC_FC_LA_SPEED_UNKNOWN:
James Smart8b68cd52012-09-29 11:32:37 -04004394 port_speed = 0;
4395 break;
James Smart26d830e2015-04-07 15:07:17 -04004396 case LPFC_FC_LA_SPEED_1G:
James Smart8b68cd52012-09-29 11:32:37 -04004397 port_speed = 1000;
4398 break;
James Smart26d830e2015-04-07 15:07:17 -04004399 case LPFC_FC_LA_SPEED_2G:
James Smart8b68cd52012-09-29 11:32:37 -04004400 port_speed = 2000;
4401 break;
James Smart26d830e2015-04-07 15:07:17 -04004402 case LPFC_FC_LA_SPEED_4G:
James Smart8b68cd52012-09-29 11:32:37 -04004403 port_speed = 4000;
4404 break;
James Smart26d830e2015-04-07 15:07:17 -04004405 case LPFC_FC_LA_SPEED_8G:
James Smart8b68cd52012-09-29 11:32:37 -04004406 port_speed = 8000;
4407 break;
James Smart26d830e2015-04-07 15:07:17 -04004408 case LPFC_FC_LA_SPEED_10G:
James Smart8b68cd52012-09-29 11:32:37 -04004409 port_speed = 10000;
4410 break;
James Smart26d830e2015-04-07 15:07:17 -04004411 case LPFC_FC_LA_SPEED_16G:
James Smart8b68cd52012-09-29 11:32:37 -04004412 port_speed = 16000;
4413 break;
James Smartd38dd522015-08-31 16:48:17 -04004414 case LPFC_FC_LA_SPEED_32G:
4415 port_speed = 32000;
4416 break;
James Smart8b68cd52012-09-29 11:32:37 -04004417 default:
4418 port_speed = 0;
4419 }
4420 break;
4421 default:
4422 port_speed = 0;
4423 }
4424 return port_speed;
4425}
4426
4427/**
James Smart70f3c072010-12-15 17:57:33 -05004428 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
James Smartda0436e2009-05-22 14:51:39 -04004429 * @phba: pointer to lpfc hba data structure.
4430 * @acqe_link: pointer to the async link completion queue entry.
4431 *
James Smart70f3c072010-12-15 17:57:33 -05004432 * This routine is to handle the SLI4 asynchronous FCoE link event.
James Smartda0436e2009-05-22 14:51:39 -04004433 **/
4434static void
4435lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
4436 struct lpfc_acqe_link *acqe_link)
4437{
4438 struct lpfc_dmabuf *mp;
4439 LPFC_MBOXQ_t *pmb;
4440 MAILBOX_t *mb;
James Smart76a95d72010-11-20 23:11:48 -05004441 struct lpfc_mbx_read_top *la;
James Smartda0436e2009-05-22 14:51:39 -04004442 uint8_t att_type;
James Smart76a95d72010-11-20 23:11:48 -05004443 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004444
4445 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05004446 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
James Smartda0436e2009-05-22 14:51:39 -04004447 return;
James Smart32b97932009-07-19 10:01:21 -04004448 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004449 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4450 if (!pmb) {
4451 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4452 "0395 The mboxq allocation failed\n");
4453 return;
4454 }
4455 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4456 if (!mp) {
4457 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4458 "0396 The lpfc_dmabuf allocation failed\n");
4459 goto out_free_pmb;
4460 }
4461 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
4462 if (!mp->virt) {
4463 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4464 "0397 The mbuf allocation failed\n");
4465 goto out_free_dmabuf;
4466 }
4467
4468 /* Cleanup any outstanding ELS commands */
4469 lpfc_els_flush_all_cmd(phba);
4470
4471 /* Block ELS IOCBs until we have done process link event */
James Smart895427b2017-02-12 13:52:30 -08004472 phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
James Smartda0436e2009-05-22 14:51:39 -04004473
4474 /* Update link event statistics */
4475 phba->sli.slistat.link_event++;
4476
James Smart76a95d72010-11-20 23:11:48 -05004477 /* Create lpfc_handle_latt mailbox command from link ACQE */
4478 lpfc_read_topology(phba, pmb, mp);
4479 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smartda0436e2009-05-22 14:51:39 -04004480 pmb->vport = phba->pport;
4481
James Smartda0436e2009-05-22 14:51:39 -04004482 /* Keep the link status for extra SLI4 state machine reference */
4483 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04004484 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_LINK,
4485 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smartda0436e2009-05-22 14:51:39 -04004486 phba->sli4_hba.link_state.duplex =
4487 bf_get(lpfc_acqe_link_duplex, acqe_link);
4488 phba->sli4_hba.link_state.status =
4489 bf_get(lpfc_acqe_link_status, acqe_link);
James Smart70f3c072010-12-15 17:57:33 -05004490 phba->sli4_hba.link_state.type =
4491 bf_get(lpfc_acqe_link_type, acqe_link);
4492 phba->sli4_hba.link_state.number =
4493 bf_get(lpfc_acqe_link_number, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04004494 phba->sli4_hba.link_state.fault =
4495 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05004496 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04004497 bf_get(lpfc_acqe_logical_link_speed, acqe_link) * 10;
4498
James Smart70f3c072010-12-15 17:57:33 -05004499 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartc31098c2011-04-16 11:03:33 -04004500 "2900 Async FC/FCoE Link event - Speed:%dGBit "
4501 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
4502 "Logical speed:%dMbps Fault:%d\n",
James Smart70f3c072010-12-15 17:57:33 -05004503 phba->sli4_hba.link_state.speed,
4504 phba->sli4_hba.link_state.topology,
4505 phba->sli4_hba.link_state.status,
4506 phba->sli4_hba.link_state.type,
4507 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04004508 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05004509 phba->sli4_hba.link_state.fault);
James Smart76a95d72010-11-20 23:11:48 -05004510 /*
4511 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
4512 * topology info. Note: Optional for non FC-AL ports.
4513 */
4514 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
4515 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4516 if (rc == MBX_NOT_FINISHED)
4517 goto out_free_dmabuf;
4518 return;
4519 }
4520 /*
4521 * For FCoE Mode: fill in all the topology information we need and call
4522 * the READ_TOPOLOGY completion routine to continue without actually
4523 * sending the READ_TOPOLOGY mailbox command to the port.
4524 */
4525 /* Parse and translate status field */
4526 mb = &pmb->u.mb;
4527 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
4528
4529 /* Parse and translate link attention fields */
4530 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
4531 la->eventTag = acqe_link->event_tag;
4532 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
4533 bf_set(lpfc_mbx_read_top_link_spd, la,
James Smarta085e872015-12-16 18:12:02 -05004534 (bf_get(lpfc_acqe_link_speed, acqe_link)));
James Smart76a95d72010-11-20 23:11:48 -05004535
4536 /* Fake the the following irrelvant fields */
4537 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
4538 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
4539 bf_set(lpfc_mbx_read_top_il, la, 0);
4540 bf_set(lpfc_mbx_read_top_pb, la, 0);
4541 bf_set(lpfc_mbx_read_top_fa, la, 0);
4542 bf_set(lpfc_mbx_read_top_mm, la, 0);
James Smartda0436e2009-05-22 14:51:39 -04004543
4544 /* Invoke the lpfc_handle_latt mailbox command callback function */
James Smart76a95d72010-11-20 23:11:48 -05004545 lpfc_mbx_cmpl_read_topology(phba, pmb);
James Smartda0436e2009-05-22 14:51:39 -04004546
4547 return;
4548
4549out_free_dmabuf:
4550 kfree(mp);
4551out_free_pmb:
4552 mempool_free(pmb, phba->mbox_mem_pool);
4553}
4554
4555/**
James Smart70f3c072010-12-15 17:57:33 -05004556 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
4557 * @phba: pointer to lpfc hba data structure.
4558 * @acqe_fc: pointer to the async fc completion queue entry.
4559 *
4560 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
4561 * that the event was received and then issue a read_topology mailbox command so
4562 * that the rest of the driver will treat it the same as SLI3.
4563 **/
4564static void
4565lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
4566{
4567 struct lpfc_dmabuf *mp;
4568 LPFC_MBOXQ_t *pmb;
James Smart7bdedb32016-07-06 12:36:00 -07004569 MAILBOX_t *mb;
4570 struct lpfc_mbx_read_top *la;
James Smart70f3c072010-12-15 17:57:33 -05004571 int rc;
4572
4573 if (bf_get(lpfc_trailer_type, acqe_fc) !=
4574 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
4575 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4576 "2895 Non FC link Event detected.(%d)\n",
4577 bf_get(lpfc_trailer_type, acqe_fc));
4578 return;
4579 }
4580 /* Keep the link status for extra SLI4 state machine reference */
4581 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04004582 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
4583 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
James Smart70f3c072010-12-15 17:57:33 -05004584 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
4585 phba->sli4_hba.link_state.topology =
4586 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
4587 phba->sli4_hba.link_state.status =
4588 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
4589 phba->sli4_hba.link_state.type =
4590 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
4591 phba->sli4_hba.link_state.number =
4592 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
4593 phba->sli4_hba.link_state.fault =
4594 bf_get(lpfc_acqe_link_fault, acqe_fc);
4595 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04004596 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
James Smart70f3c072010-12-15 17:57:33 -05004597 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4598 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
4599 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
4600 "%dMbps Fault:%d\n",
4601 phba->sli4_hba.link_state.speed,
4602 phba->sli4_hba.link_state.topology,
4603 phba->sli4_hba.link_state.status,
4604 phba->sli4_hba.link_state.type,
4605 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04004606 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05004607 phba->sli4_hba.link_state.fault);
4608 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4609 if (!pmb) {
4610 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4611 "2897 The mboxq allocation failed\n");
4612 return;
4613 }
4614 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4615 if (!mp) {
4616 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4617 "2898 The lpfc_dmabuf allocation failed\n");
4618 goto out_free_pmb;
4619 }
4620 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
4621 if (!mp->virt) {
4622 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4623 "2899 The mbuf allocation failed\n");
4624 goto out_free_dmabuf;
4625 }
4626
4627 /* Cleanup any outstanding ELS commands */
4628 lpfc_els_flush_all_cmd(phba);
4629
4630 /* Block ELS IOCBs until we have done process link event */
James Smart895427b2017-02-12 13:52:30 -08004631 phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
James Smart70f3c072010-12-15 17:57:33 -05004632
4633 /* Update link event statistics */
4634 phba->sli.slistat.link_event++;
4635
4636 /* Create lpfc_handle_latt mailbox command from link ACQE */
4637 lpfc_read_topology(phba, pmb, mp);
4638 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
4639 pmb->vport = phba->pport;
4640
James Smart7bdedb32016-07-06 12:36:00 -07004641 if (phba->sli4_hba.link_state.status != LPFC_FC_LA_TYPE_LINK_UP) {
James Smartae9e28f2017-05-15 15:20:51 -07004642 phba->link_flag &= ~(LS_MDS_LINK_DOWN | LS_MDS_LOOPBACK);
4643
4644 switch (phba->sli4_hba.link_state.status) {
4645 case LPFC_FC_LA_TYPE_MDS_LINK_DOWN:
4646 phba->link_flag |= LS_MDS_LINK_DOWN;
4647 break;
4648 case LPFC_FC_LA_TYPE_MDS_LOOPBACK:
4649 phba->link_flag |= LS_MDS_LOOPBACK;
4650 break;
4651 default:
4652 break;
4653 }
4654
James Smart7bdedb32016-07-06 12:36:00 -07004655 /* Parse and translate status field */
4656 mb = &pmb->u.mb;
4657 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba,
4658 (void *)acqe_fc);
4659
4660 /* Parse and translate link attention fields */
4661 la = (struct lpfc_mbx_read_top *)&pmb->u.mb.un.varReadTop;
4662 la->eventTag = acqe_fc->event_tag;
James Smart7bdedb32016-07-06 12:36:00 -07004663
James Smartaeb3c812017-04-21 16:05:02 -07004664 if (phba->sli4_hba.link_state.status ==
4665 LPFC_FC_LA_TYPE_UNEXP_WWPN) {
4666 bf_set(lpfc_mbx_read_top_att_type, la,
4667 LPFC_FC_LA_TYPE_UNEXP_WWPN);
4668 } else {
4669 bf_set(lpfc_mbx_read_top_att_type, la,
4670 LPFC_FC_LA_TYPE_LINK_DOWN);
4671 }
James Smart7bdedb32016-07-06 12:36:00 -07004672 /* Invoke the mailbox command callback function */
4673 lpfc_mbx_cmpl_read_topology(phba, pmb);
4674
4675 return;
4676 }
4677
James Smart70f3c072010-12-15 17:57:33 -05004678 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4679 if (rc == MBX_NOT_FINISHED)
4680 goto out_free_dmabuf;
4681 return;
4682
4683out_free_dmabuf:
4684 kfree(mp);
4685out_free_pmb:
4686 mempool_free(pmb, phba->mbox_mem_pool);
4687}
4688
4689/**
4690 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
4691 * @phba: pointer to lpfc hba data structure.
4692 * @acqe_fc: pointer to the async SLI completion queue entry.
4693 *
4694 * This routine is to handle the SLI4 asynchronous SLI events.
4695 **/
4696static void
4697lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
4698{
James Smart4b8bae02012-06-12 13:55:07 -04004699 char port_name;
James Smart8c1312e2012-10-31 14:45:09 -04004700 char message[128];
James Smart4b8bae02012-06-12 13:55:07 -04004701 uint8_t status;
James Smart946727d2015-04-07 15:07:09 -04004702 uint8_t evt_type;
James Smart448193b2015-12-16 18:12:05 -05004703 uint8_t operational = 0;
James Smart946727d2015-04-07 15:07:09 -04004704 struct temp_event temp_event_data;
James Smart4b8bae02012-06-12 13:55:07 -04004705 struct lpfc_acqe_misconfigured_event *misconfigured;
James Smart946727d2015-04-07 15:07:09 -04004706 struct Scsi_Host *shost;
James Smart4b8bae02012-06-12 13:55:07 -04004707
James Smart946727d2015-04-07 15:07:09 -04004708 evt_type = bf_get(lpfc_trailer_type, acqe_sli);
4709
James Smart448193b2015-12-16 18:12:05 -05004710 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4711 "2901 Async SLI event - Event Data1:x%08x Event Data2:"
4712 "x%08x SLI Event Type:%d\n",
4713 acqe_sli->event_data1, acqe_sli->event_data2,
4714 evt_type);
James Smart4b8bae02012-06-12 13:55:07 -04004715
4716 port_name = phba->Port[0];
4717 if (port_name == 0x00)
4718 port_name = '?'; /* get port name is empty */
4719
James Smart946727d2015-04-07 15:07:09 -04004720 switch (evt_type) {
4721 case LPFC_SLI_EVENT_TYPE_OVER_TEMP:
4722 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
4723 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
4724 temp_event_data.data = (uint32_t)acqe_sli->event_data1;
4725
4726 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4727 "3190 Over Temperature:%d Celsius- Port Name %c\n",
4728 acqe_sli->event_data1, port_name);
4729
James Smart310429e2016-07-06 12:35:54 -07004730 phba->sfp_warning |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
James Smart946727d2015-04-07 15:07:09 -04004731 shost = lpfc_shost_from_vport(phba->pport);
4732 fc_host_post_vendor_event(shost, fc_get_event_number(),
4733 sizeof(temp_event_data),
4734 (char *)&temp_event_data,
4735 SCSI_NL_VID_TYPE_PCI
4736 | PCI_VENDOR_ID_EMULEX);
4737 break;
4738 case LPFC_SLI_EVENT_TYPE_NORM_TEMP:
4739 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
4740 temp_event_data.event_code = LPFC_NORMAL_TEMP;
4741 temp_event_data.data = (uint32_t)acqe_sli->event_data1;
4742
4743 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4744 "3191 Normal Temperature:%d Celsius - Port Name %c\n",
4745 acqe_sli->event_data1, port_name);
4746
4747 shost = lpfc_shost_from_vport(phba->pport);
4748 fc_host_post_vendor_event(shost, fc_get_event_number(),
4749 sizeof(temp_event_data),
4750 (char *)&temp_event_data,
4751 SCSI_NL_VID_TYPE_PCI
4752 | PCI_VENDOR_ID_EMULEX);
4753 break;
4754 case LPFC_SLI_EVENT_TYPE_MISCONFIGURED:
4755 misconfigured = (struct lpfc_acqe_misconfigured_event *)
James Smart4b8bae02012-06-12 13:55:07 -04004756 &acqe_sli->event_data1;
4757
James Smart946727d2015-04-07 15:07:09 -04004758 /* fetch the status for this port */
4759 switch (phba->sli4_hba.lnk_info.lnk_no) {
4760 case LPFC_LINK_NUMBER_0:
James Smart448193b2015-12-16 18:12:05 -05004761 status = bf_get(lpfc_sli_misconfigured_port0_state,
4762 &misconfigured->theEvent);
4763 operational = bf_get(lpfc_sli_misconfigured_port0_op,
James Smart4b8bae02012-06-12 13:55:07 -04004764 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04004765 break;
4766 case LPFC_LINK_NUMBER_1:
James Smart448193b2015-12-16 18:12:05 -05004767 status = bf_get(lpfc_sli_misconfigured_port1_state,
4768 &misconfigured->theEvent);
4769 operational = bf_get(lpfc_sli_misconfigured_port1_op,
James Smart4b8bae02012-06-12 13:55:07 -04004770 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04004771 break;
4772 case LPFC_LINK_NUMBER_2:
James Smart448193b2015-12-16 18:12:05 -05004773 status = bf_get(lpfc_sli_misconfigured_port2_state,
4774 &misconfigured->theEvent);
4775 operational = bf_get(lpfc_sli_misconfigured_port2_op,
James Smart4b8bae02012-06-12 13:55:07 -04004776 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04004777 break;
4778 case LPFC_LINK_NUMBER_3:
James Smart448193b2015-12-16 18:12:05 -05004779 status = bf_get(lpfc_sli_misconfigured_port3_state,
4780 &misconfigured->theEvent);
4781 operational = bf_get(lpfc_sli_misconfigured_port3_op,
James Smart4b8bae02012-06-12 13:55:07 -04004782 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04004783 break;
4784 default:
James Smart448193b2015-12-16 18:12:05 -05004785 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4786 "3296 "
4787 "LPFC_SLI_EVENT_TYPE_MISCONFIGURED "
4788 "event: Invalid link %d",
4789 phba->sli4_hba.lnk_info.lnk_no);
4790 return;
James Smart946727d2015-04-07 15:07:09 -04004791 }
James Smart4b8bae02012-06-12 13:55:07 -04004792
James Smart448193b2015-12-16 18:12:05 -05004793 /* Skip if optic state unchanged */
4794 if (phba->sli4_hba.lnk_info.optic_state == status)
4795 return;
4796
James Smart946727d2015-04-07 15:07:09 -04004797 switch (status) {
4798 case LPFC_SLI_EVENT_STATUS_VALID:
James Smart448193b2015-12-16 18:12:05 -05004799 sprintf(message, "Physical Link is functional");
4800 break;
James Smart946727d2015-04-07 15:07:09 -04004801 case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
4802 sprintf(message, "Optics faulted/incorrectly "
4803 "installed/not installed - Reseat optics, "
4804 "if issue not resolved, replace.");
4805 break;
4806 case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
4807 sprintf(message,
4808 "Optics of two types installed - Remove one "
4809 "optic or install matching pair of optics.");
4810 break;
4811 case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
4812 sprintf(message, "Incompatible optics - Replace with "
James Smart292098b2012-09-29 11:31:41 -04004813 "compatible optics for card to function.");
James Smart946727d2015-04-07 15:07:09 -04004814 break;
James Smart448193b2015-12-16 18:12:05 -05004815 case LPFC_SLI_EVENT_STATUS_UNQUALIFIED:
4816 sprintf(message, "Unqualified optics - Replace with "
4817 "Avago optics for Warranty and Technical "
4818 "Support - Link is%s operational",
James Smart2ea259e2017-02-12 13:52:27 -08004819 (operational) ? " not" : "");
James Smart448193b2015-12-16 18:12:05 -05004820 break;
4821 case LPFC_SLI_EVENT_STATUS_UNCERTIFIED:
4822 sprintf(message, "Uncertified optics - Replace with "
4823 "Avago-certified optics to enable link "
4824 "operation - Link is%s operational",
James Smart2ea259e2017-02-12 13:52:27 -08004825 (operational) ? " not" : "");
James Smart448193b2015-12-16 18:12:05 -05004826 break;
James Smart946727d2015-04-07 15:07:09 -04004827 default:
4828 /* firmware is reporting a status we don't know about */
4829 sprintf(message, "Unknown event status x%02x", status);
4830 break;
4831 }
James Smart448193b2015-12-16 18:12:05 -05004832 phba->sli4_hba.lnk_info.optic_state = status;
James Smart946727d2015-04-07 15:07:09 -04004833 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart448193b2015-12-16 18:12:05 -05004834 "3176 Port Name %c %s\n", port_name, message);
James Smart946727d2015-04-07 15:07:09 -04004835 break;
4836 case LPFC_SLI_EVENT_TYPE_REMOTE_DPORT:
4837 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4838 "3192 Remote DPort Test Initiated - "
4839 "Event Data1:x%08x Event Data2: x%08x\n",
4840 acqe_sli->event_data1, acqe_sli->event_data2);
James Smart4b8bae02012-06-12 13:55:07 -04004841 break;
4842 default:
James Smart946727d2015-04-07 15:07:09 -04004843 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4844 "3193 Async SLI event - Event Data1:x%08x Event Data2:"
4845 "x%08x SLI Event Type:%d\n",
4846 acqe_sli->event_data1, acqe_sli->event_data2,
4847 evt_type);
James Smart4b8bae02012-06-12 13:55:07 -04004848 break;
4849 }
James Smart70f3c072010-12-15 17:57:33 -05004850}
4851
4852/**
James Smartfc2b9892010-02-26 14:15:29 -05004853 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
4854 * @vport: pointer to vport data structure.
4855 *
4856 * This routine is to perform Clear Virtual Link (CVL) on a vport in
4857 * response to a CVL event.
4858 *
4859 * Return the pointer to the ndlp with the vport if successful, otherwise
4860 * return NULL.
4861 **/
4862static struct lpfc_nodelist *
4863lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
4864{
4865 struct lpfc_nodelist *ndlp;
4866 struct Scsi_Host *shost;
4867 struct lpfc_hba *phba;
4868
4869 if (!vport)
4870 return NULL;
James Smartfc2b9892010-02-26 14:15:29 -05004871 phba = vport->phba;
4872 if (!phba)
4873 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04004874 ndlp = lpfc_findnode_did(vport, Fabric_DID);
4875 if (!ndlp) {
4876 /* Cannot find existing Fabric ndlp, so allocate a new one */
James Smart9d3d3402017-04-21 16:05:00 -07004877 ndlp = lpfc_nlp_init(vport, Fabric_DID);
James Smart78730cf2010-04-06 15:06:30 -04004878 if (!ndlp)
4879 return 0;
James Smart78730cf2010-04-06 15:06:30 -04004880 /* Set the node type */
4881 ndlp->nlp_type |= NLP_FABRIC;
4882 /* Put ndlp onto node list */
4883 lpfc_enqueue_node(vport, ndlp);
4884 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
4885 /* re-setup ndlp without removing from node list */
4886 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
4887 if (!ndlp)
4888 return 0;
4889 }
James Smart63e801c2010-11-20 23:14:19 -05004890 if ((phba->pport->port_state < LPFC_FLOGI) &&
4891 (phba->pport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05004892 return NULL;
4893 /* If virtual link is not yet instantiated ignore CVL */
James Smart63e801c2010-11-20 23:14:19 -05004894 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
4895 && (vport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05004896 return NULL;
4897 shost = lpfc_shost_from_vport(vport);
4898 if (!shost)
4899 return NULL;
4900 lpfc_linkdown_port(vport);
4901 lpfc_cleanup_pending_mbox(vport);
4902 spin_lock_irq(shost->host_lock);
4903 vport->fc_flag |= FC_VPORT_CVL_RCVD;
4904 spin_unlock_irq(shost->host_lock);
4905
4906 return ndlp;
4907}
4908
4909/**
4910 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
4911 * @vport: pointer to lpfc hba data structure.
4912 *
4913 * This routine is to perform Clear Virtual Link (CVL) on all vports in
4914 * response to a FCF dead event.
4915 **/
4916static void
4917lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
4918{
4919 struct lpfc_vport **vports;
4920 int i;
4921
4922 vports = lpfc_create_vport_work_array(phba);
4923 if (vports)
4924 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
4925 lpfc_sli4_perform_vport_cvl(vports[i]);
4926 lpfc_destroy_vport_work_array(phba, vports);
4927}
4928
4929/**
James Smart76a95d72010-11-20 23:11:48 -05004930 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
James Smartda0436e2009-05-22 14:51:39 -04004931 * @phba: pointer to lpfc hba data structure.
4932 * @acqe_link: pointer to the async fcoe completion queue entry.
4933 *
4934 * This routine is to handle the SLI4 asynchronous fcoe event.
4935 **/
4936static void
James Smart76a95d72010-11-20 23:11:48 -05004937lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
James Smart70f3c072010-12-15 17:57:33 -05004938 struct lpfc_acqe_fip *acqe_fip)
James Smartda0436e2009-05-22 14:51:39 -04004939{
James Smart70f3c072010-12-15 17:57:33 -05004940 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004941 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04004942 struct lpfc_vport *vport;
4943 struct lpfc_nodelist *ndlp;
4944 struct Scsi_Host *shost;
James Smart695a8142010-01-26 23:08:03 -05004945 int active_vlink_present;
4946 struct lpfc_vport **vports;
4947 int i;
James Smartda0436e2009-05-22 14:51:39 -04004948
James Smart70f3c072010-12-15 17:57:33 -05004949 phba->fc_eventTag = acqe_fip->event_tag;
4950 phba->fcoe_eventtag = acqe_fip->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004951 switch (event_type) {
James Smart70f3c072010-12-15 17:57:33 -05004952 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
4953 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
4954 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
James Smart999d8132010-03-15 11:24:56 -04004955 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4956 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004957 "2546 New FCF event, evt_tag:x%x, "
4958 "index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004959 acqe_fip->event_tag,
4960 acqe_fip->index);
James Smart999d8132010-03-15 11:24:56 -04004961 else
4962 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
4963 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004964 "2788 FCF param modified event, "
4965 "evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004966 acqe_fip->event_tag,
4967 acqe_fip->index);
James Smart38b92ef2010-08-04 16:11:39 -04004968 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004969 /*
4970 * During period of FCF discovery, read the FCF
4971 * table record indexed by the event to update
James Smarta93ff372010-10-22 11:06:08 -04004972 * FCF roundrobin failover eligible FCF bmask.
James Smart0c9ab6f2010-02-26 14:15:57 -05004973 */
4974 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4975 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004976 "2779 Read FCF (x%x) for updating "
4977 "roundrobin FCF failover bmask\n",
James Smart70f3c072010-12-15 17:57:33 -05004978 acqe_fip->index);
4979 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05004980 }
James Smart38b92ef2010-08-04 16:11:39 -04004981
4982 /* If the FCF discovery is in progress, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04004983 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04004984 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart38b92ef2010-08-04 16:11:39 -04004985 spin_unlock_irq(&phba->hbalock);
4986 break;
4987 }
4988 /* If fast FCF failover rescan event is pending, do nothing */
4989 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
4990 spin_unlock_irq(&phba->hbalock);
4991 break;
4992 }
4993
James Smartc2b97122013-05-31 17:05:36 -04004994 /* If the FCF has been in discovered state, do nothing. */
4995 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
James Smart3804dc82010-07-14 15:31:37 -04004996 spin_unlock_irq(&phba->hbalock);
4997 break;
4998 }
4999 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04005000
James Smart0c9ab6f2010-02-26 14:15:57 -05005001 /* Otherwise, scan the entire FCF table and re-discover SAN */
5002 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005003 "2770 Start FCF table scan per async FCF "
5004 "event, evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05005005 acqe_fip->event_tag, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05005006 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
5007 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04005008 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05005009 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
5010 "2547 Issue FCF scan read FCF mailbox "
James Smarta93ff372010-10-22 11:06:08 -04005011 "command failed (x%x)\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04005012 break;
5013
James Smart70f3c072010-12-15 17:57:33 -05005014 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
James Smartda0436e2009-05-22 14:51:39 -04005015 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04005016 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05005017 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
5018 acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04005019 break;
5020
James Smart70f3c072010-12-15 17:57:33 -05005021 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
James Smart80c17842012-03-01 22:35:45 -05005022 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05005023 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005024 "2549 FCF (x%x) disconnected from network, "
James Smart70f3c072010-12-15 17:57:33 -05005025 "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart38b92ef2010-08-04 16:11:39 -04005026 /*
5027 * If we are in the middle of FCF failover process, clear
5028 * the corresponding FCF bit in the roundrobin bitmap.
James Smartda0436e2009-05-22 14:51:39 -04005029 */
James Smartfc2b9892010-02-26 14:15:29 -05005030 spin_lock_irq(&phba->hbalock);
James Smarta1cadfe2016-07-06 12:36:02 -07005031 if ((phba->fcf.fcf_flag & FCF_DISCOVERY) &&
5032 (phba->fcf.current_rec.fcf_indx != acqe_fip->index)) {
James Smartfc2b9892010-02-26 14:15:29 -05005033 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005034 /* Update FLOGI FCF failover eligible FCF bmask */
James Smart70f3c072010-12-15 17:57:33 -05005035 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05005036 break;
5037 }
James Smart38b92ef2010-08-04 16:11:39 -04005038 spin_unlock_irq(&phba->hbalock);
5039
5040 /* If the event is not for currently used fcf do nothing */
James Smart70f3c072010-12-15 17:57:33 -05005041 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
James Smart38b92ef2010-08-04 16:11:39 -04005042 break;
5043
5044 /*
5045 * Otherwise, request the port to rediscover the entire FCF
5046 * table for a fast recovery from case that the current FCF
5047 * is no longer valid as we are not in the middle of FCF
5048 * failover process already.
5049 */
James Smartc2b97122013-05-31 17:05:36 -04005050 spin_lock_irq(&phba->hbalock);
5051 /* Mark the fast failover process in progress */
5052 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
5053 spin_unlock_irq(&phba->hbalock);
5054
5055 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
5056 "2771 Start FCF fast failover process due to "
5057 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
5058 "\n", acqe_fip->event_tag, acqe_fip->index);
5059 rc = lpfc_sli4_redisc_fcf_table(phba);
5060 if (rc) {
5061 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
5062 LOG_DISCOVERY,
5063 "2772 Issue FCF rediscover mabilbox "
5064 "command failed, fail through to FCF "
5065 "dead event\n");
5066 spin_lock_irq(&phba->hbalock);
5067 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
5068 spin_unlock_irq(&phba->hbalock);
5069 /*
5070 * Last resort will fail over by treating this
5071 * as a link down to FCF registration.
5072 */
5073 lpfc_sli4_fcf_dead_failthrough(phba);
5074 } else {
5075 /* Reset FCF roundrobin bmask for new discovery */
5076 lpfc_sli4_clear_fcf_rr_bmask(phba);
5077 /*
5078 * Handling fast FCF failover to a DEAD FCF event is
5079 * considered equalivant to receiving CVL to all vports.
5080 */
5081 lpfc_sli4_perform_all_vport_cvl(phba);
5082 }
James Smartda0436e2009-05-22 14:51:39 -04005083 break;
James Smart70f3c072010-12-15 17:57:33 -05005084 case LPFC_FIP_EVENT_TYPE_CVL:
James Smart80c17842012-03-01 22:35:45 -05005085 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05005086 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6669f9b2009-10-02 15:16:45 -04005087 "2718 Clear Virtual Link Received for VPI 0x%x"
James Smart70f3c072010-12-15 17:57:33 -05005088 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart6d368e52011-05-24 11:44:12 -04005089
James Smart6669f9b2009-10-02 15:16:45 -04005090 vport = lpfc_find_vport_by_vpid(phba,
James Smart5248a742011-07-22 18:37:06 -04005091 acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05005092 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04005093 if (!ndlp)
5094 break;
James Smart695a8142010-01-26 23:08:03 -05005095 active_vlink_present = 0;
5096
5097 vports = lpfc_create_vport_work_array(phba);
5098 if (vports) {
5099 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
5100 i++) {
5101 if ((!(vports[i]->fc_flag &
5102 FC_VPORT_CVL_RCVD)) &&
5103 (vports[i]->port_state > LPFC_FDISC)) {
5104 active_vlink_present = 1;
5105 break;
5106 }
5107 }
5108 lpfc_destroy_vport_work_array(phba, vports);
5109 }
5110
James Smartcc823552015-05-21 13:55:26 -04005111 /*
5112 * Don't re-instantiate if vport is marked for deletion.
5113 * If we are here first then vport_delete is going to wait
5114 * for discovery to complete.
5115 */
5116 if (!(vport->load_flag & FC_UNLOADING) &&
5117 active_vlink_present) {
James Smart695a8142010-01-26 23:08:03 -05005118 /*
5119 * If there are other active VLinks present,
5120 * re-instantiate the Vlink using FDISC.
5121 */
James Smart256ec0d2013-04-17 20:14:58 -04005122 mod_timer(&ndlp->nlp_delayfunc,
5123 jiffies + msecs_to_jiffies(1000));
James Smartfc2b9892010-02-26 14:15:29 -05005124 shost = lpfc_shost_from_vport(vport);
James Smart6669f9b2009-10-02 15:16:45 -04005125 spin_lock_irq(shost->host_lock);
5126 ndlp->nlp_flag |= NLP_DELAY_TMO;
5127 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05005128 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
5129 vport->port_state = LPFC_FDISC;
5130 } else {
James Smartecfd03c2010-02-12 14:41:27 -05005131 /*
5132 * Otherwise, we request port to rediscover
5133 * the entire FCF table for a fast recovery
5134 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05005135 * is no longer valid if we are not already
5136 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05005137 */
James Smartfc2b9892010-02-26 14:15:29 -05005138 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005139 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05005140 spin_unlock_irq(&phba->hbalock);
5141 break;
5142 }
5143 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05005144 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05005145 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005146 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
5147 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005148 "2773 Start FCF failover per CVL, "
James Smart70f3c072010-12-15 17:57:33 -05005149 "evt_tag:x%x\n", acqe_fip->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05005150 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05005151 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05005152 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
5153 LOG_DISCOVERY,
5154 "2774 Issue FCF rediscover "
5155 "mabilbox command failed, "
5156 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05005157 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005158 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05005159 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05005160 /*
5161 * Last resort will be re-try on the
5162 * the current registered FCF entry.
5163 */
5164 lpfc_retry_pport_discovery(phba);
James Smart38b92ef2010-08-04 16:11:39 -04005165 } else
5166 /*
5167 * Reset FCF roundrobin bmask for new
5168 * discovery.
5169 */
James Smart7d791df2011-07-22 18:37:52 -04005170 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart6669f9b2009-10-02 15:16:45 -04005171 }
5172 break;
James Smartda0436e2009-05-22 14:51:39 -04005173 default:
5174 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5175 "0288 Unknown FCoE event type 0x%x event tag "
James Smart70f3c072010-12-15 17:57:33 -05005176 "0x%x\n", event_type, acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04005177 break;
5178 }
5179}
5180
5181/**
5182 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
5183 * @phba: pointer to lpfc hba data structure.
5184 * @acqe_link: pointer to the async dcbx completion queue entry.
5185 *
5186 * This routine is to handle the SLI4 asynchronous dcbx event.
5187 **/
5188static void
5189lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
5190 struct lpfc_acqe_dcbx *acqe_dcbx)
5191{
James Smart4d9ab992009-10-02 15:16:39 -04005192 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04005193 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5194 "0290 The SLI4 DCBX asynchronous event is not "
5195 "handled yet\n");
5196}
5197
5198/**
James Smartb19a0612010-04-06 14:48:51 -04005199 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
5200 * @phba: pointer to lpfc hba data structure.
5201 * @acqe_link: pointer to the async grp5 completion queue entry.
5202 *
5203 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
5204 * is an asynchronous notified of a logical link speed change. The Port
5205 * reports the logical link speed in units of 10Mbps.
5206 **/
5207static void
5208lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
5209 struct lpfc_acqe_grp5 *acqe_grp5)
5210{
5211 uint16_t prev_ll_spd;
5212
5213 phba->fc_eventTag = acqe_grp5->event_tag;
5214 phba->fcoe_eventtag = acqe_grp5->event_tag;
5215 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
5216 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04005217 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5)) * 10;
James Smartb19a0612010-04-06 14:48:51 -04005218 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5219 "2789 GRP5 Async Event: Updating logical link speed "
James Smart8b68cd52012-09-29 11:32:37 -04005220 "from %dMbps to %dMbps\n", prev_ll_spd,
5221 phba->sli4_hba.link_state.logical_speed);
James Smartb19a0612010-04-06 14:48:51 -04005222}
5223
5224/**
James Smartda0436e2009-05-22 14:51:39 -04005225 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
5226 * @phba: pointer to lpfc hba data structure.
5227 *
5228 * This routine is invoked by the worker thread to process all the pending
5229 * SLI4 asynchronous events.
5230 **/
5231void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
5232{
5233 struct lpfc_cq_event *cq_event;
5234
5235 /* First, declare the async event has been handled */
5236 spin_lock_irq(&phba->hbalock);
5237 phba->hba_flag &= ~ASYNC_EVENT;
5238 spin_unlock_irq(&phba->hbalock);
5239 /* Now, handle all the async events */
5240 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
5241 /* Get the first event from the head of the event queue */
5242 spin_lock_irq(&phba->hbalock);
5243 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
5244 cq_event, struct lpfc_cq_event, list);
5245 spin_unlock_irq(&phba->hbalock);
5246 /* Process the asynchronous event */
5247 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
5248 case LPFC_TRAILER_CODE_LINK:
5249 lpfc_sli4_async_link_evt(phba,
5250 &cq_event->cqe.acqe_link);
5251 break;
5252 case LPFC_TRAILER_CODE_FCOE:
James Smart70f3c072010-12-15 17:57:33 -05005253 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04005254 break;
5255 case LPFC_TRAILER_CODE_DCBX:
5256 lpfc_sli4_async_dcbx_evt(phba,
5257 &cq_event->cqe.acqe_dcbx);
5258 break;
James Smartb19a0612010-04-06 14:48:51 -04005259 case LPFC_TRAILER_CODE_GRP5:
5260 lpfc_sli4_async_grp5_evt(phba,
5261 &cq_event->cqe.acqe_grp5);
5262 break;
James Smart70f3c072010-12-15 17:57:33 -05005263 case LPFC_TRAILER_CODE_FC:
5264 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
5265 break;
5266 case LPFC_TRAILER_CODE_SLI:
5267 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
5268 break;
James Smartda0436e2009-05-22 14:51:39 -04005269 default:
5270 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5271 "1804 Invalid asynchrous event code: "
5272 "x%x\n", bf_get(lpfc_trailer_code,
5273 &cq_event->cqe.mcqe_cmpl));
5274 break;
5275 }
5276 /* Free the completion event processed to the free pool */
5277 lpfc_sli4_cq_event_release(phba, cq_event);
5278 }
5279}
5280
5281/**
James Smartecfd03c2010-02-12 14:41:27 -05005282 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
5283 * @phba: pointer to lpfc hba data structure.
5284 *
5285 * This routine is invoked by the worker thread to process FCF table
5286 * rediscovery pending completion event.
5287 **/
5288void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
5289{
5290 int rc;
5291
5292 spin_lock_irq(&phba->hbalock);
5293 /* Clear FCF rediscovery timeout event */
5294 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
5295 /* Clear driver fast failover FCF record flag */
5296 phba->fcf.failover_rec.flag = 0;
5297 /* Set state for FCF fast failover */
5298 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
5299 spin_unlock_irq(&phba->hbalock);
5300
5301 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05005302 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005303 "2777 Start post-quiescent FCF table scan\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05005304 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05005305 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05005306 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
5307 "2747 Issue FCF scan read FCF mailbox "
5308 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05005309}
5310
5311/**
James Smartda0436e2009-05-22 14:51:39 -04005312 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
5313 * @phba: pointer to lpfc hba data structure.
5314 * @dev_grp: The HBA PCI-Device group number.
5315 *
5316 * This routine is invoked to set up the per HBA PCI-Device group function
5317 * API jump table entries.
5318 *
5319 * Return: 0 if success, otherwise -ENODEV
5320 **/
5321int
5322lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05005323{
5324 int rc;
5325
James Smartda0436e2009-05-22 14:51:39 -04005326 /* Set up lpfc PCI-device group */
5327 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05005328
James Smartda0436e2009-05-22 14:51:39 -04005329 /* The LPFC_PCI_DEV_OC uses SLI4 */
5330 if (dev_grp == LPFC_PCI_DEV_OC)
5331 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05005332
James Smartda0436e2009-05-22 14:51:39 -04005333 /* Set up device INIT API function jump table */
5334 rc = lpfc_init_api_table_setup(phba, dev_grp);
5335 if (rc)
5336 return -ENODEV;
5337 /* Set up SCSI API function jump table */
5338 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
5339 if (rc)
5340 return -ENODEV;
5341 /* Set up SLI API function jump table */
5342 rc = lpfc_sli_api_table_setup(phba, dev_grp);
5343 if (rc)
5344 return -ENODEV;
5345 /* Set up MBOX API function jump table */
5346 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
5347 if (rc)
5348 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05005349
James Smartda0436e2009-05-22 14:51:39 -04005350 return 0;
James Smart5b75da22008-12-04 22:39:35 -05005351}
5352
5353/**
James Smart3621a712009-04-06 18:47:14 -04005354 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05005355 * @phba: pointer to lpfc hba data structure.
5356 * @intr_mode: active interrupt mode adopted.
5357 *
5358 * This routine it invoked to log the currently used active interrupt mode
5359 * to the device.
James Smart3772a992009-05-22 14:50:54 -04005360 **/
5361static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05005362{
5363 switch (intr_mode) {
5364 case 0:
5365 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5366 "0470 Enable INTx interrupt mode.\n");
5367 break;
5368 case 1:
5369 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5370 "0481 Enabled MSI interrupt mode.\n");
5371 break;
5372 case 2:
5373 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5374 "0480 Enabled MSI-X interrupt mode.\n");
5375 break;
5376 default:
5377 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5378 "0482 Illegal interrupt mode.\n");
5379 break;
5380 }
5381 return;
5382}
5383
James Smart5b75da22008-12-04 22:39:35 -05005384/**
James Smart3772a992009-05-22 14:50:54 -04005385 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05005386 * @phba: pointer to lpfc hba data structure.
5387 *
James Smart3772a992009-05-22 14:50:54 -04005388 * This routine is invoked to enable the PCI device that is common to all
5389 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05005390 *
5391 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005392 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005393 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05005394 **/
James Smart3772a992009-05-22 14:50:54 -04005395static int
5396lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05005397{
James Smart3772a992009-05-22 14:50:54 -04005398 struct pci_dev *pdev;
James Smart5b75da22008-12-04 22:39:35 -05005399
James Smart3772a992009-05-22 14:50:54 -04005400 /* Obtain PCI device reference */
5401 if (!phba->pcidev)
5402 goto out_error;
5403 else
5404 pdev = phba->pcidev;
James Smart3772a992009-05-22 14:50:54 -04005405 /* Enable PCI device */
5406 if (pci_enable_device_mem(pdev))
5407 goto out_error;
5408 /* Request PCI resource for the device */
Johannes Thumshirne0c04832016-06-07 09:44:03 +02005409 if (pci_request_mem_regions(pdev, LPFC_DRIVER_NAME))
James Smart3772a992009-05-22 14:50:54 -04005410 goto out_disable_device;
5411 /* Set up device as PCI master and save state for EEH */
5412 pci_set_master(pdev);
5413 pci_try_set_mwi(pdev);
5414 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05005415
James Smart05580562011-05-24 11:40:48 -04005416 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
Jon Mason453193e2013-09-11 15:38:13 -07005417 if (pci_is_pcie(pdev))
James Smart05580562011-05-24 11:40:48 -04005418 pdev->needs_freset = 1;
5419
James Smart3772a992009-05-22 14:50:54 -04005420 return 0;
James Smart5b75da22008-12-04 22:39:35 -05005421
James Smart3772a992009-05-22 14:50:54 -04005422out_disable_device:
5423 pci_disable_device(pdev);
5424out_error:
James Smart079b5c92011-08-21 21:48:49 -04005425 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
Johannes Thumshirne0c04832016-06-07 09:44:03 +02005426 "1401 Failed to enable pci device\n");
James Smart3772a992009-05-22 14:50:54 -04005427 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05005428}
5429
5430/**
James Smart3772a992009-05-22 14:50:54 -04005431 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05005432 * @phba: pointer to lpfc hba data structure.
5433 *
James Smart3772a992009-05-22 14:50:54 -04005434 * This routine is invoked to disable the PCI device that is common to all
5435 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05005436 **/
5437static void
James Smart3772a992009-05-22 14:50:54 -04005438lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05005439{
James Smart3772a992009-05-22 14:50:54 -04005440 struct pci_dev *pdev;
James Smart5b75da22008-12-04 22:39:35 -05005441
James Smart3772a992009-05-22 14:50:54 -04005442 /* Obtain PCI device reference */
5443 if (!phba->pcidev)
5444 return;
5445 else
5446 pdev = phba->pcidev;
James Smart3772a992009-05-22 14:50:54 -04005447 /* Release PCI resource and disable PCI device */
Johannes Thumshirne0c04832016-06-07 09:44:03 +02005448 pci_release_mem_regions(pdev);
James Smart3772a992009-05-22 14:50:54 -04005449 pci_disable_device(pdev);
James Smart5b75da22008-12-04 22:39:35 -05005450
5451 return;
5452}
5453
5454/**
James Smart3772a992009-05-22 14:50:54 -04005455 * lpfc_reset_hba - Reset a hba
5456 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04005457 *
James Smart3772a992009-05-22 14:50:54 -04005458 * This routine is invoked to reset a hba device. It brings the HBA
5459 * offline, performs a board restart, and then brings the board back
5460 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
5461 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04005462 **/
James Smart3772a992009-05-22 14:50:54 -04005463void
5464lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05005465{
James Smart3772a992009-05-22 14:50:54 -04005466 /* If resets are disabled then set error state and return. */
5467 if (!phba->cfg_enable_hba_reset) {
5468 phba->link_state = LPFC_HBA_ERROR;
5469 return;
5470 }
James Smartee620212014-04-04 13:51:53 -04005471 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
5472 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
5473 else
5474 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart3772a992009-05-22 14:50:54 -04005475 lpfc_offline(phba);
5476 lpfc_sli_brdrestart(phba);
5477 lpfc_online(phba);
5478 lpfc_unblock_mgmt_io(phba);
5479}
dea31012005-04-17 16:05:31 -05005480
James Smart3772a992009-05-22 14:50:54 -04005481/**
James Smart0a96e972011-07-22 18:37:28 -04005482 * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
5483 * @phba: pointer to lpfc hba data structure.
5484 *
5485 * This function enables the PCI SR-IOV virtual functions to a physical
5486 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
5487 * enable the number of virtual functions to the physical function. As
5488 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
5489 * API call does not considered as an error condition for most of the device.
5490 **/
5491uint16_t
5492lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
5493{
5494 struct pci_dev *pdev = phba->pcidev;
5495 uint16_t nr_virtfn;
5496 int pos;
5497
James Smart0a96e972011-07-22 18:37:28 -04005498 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
5499 if (pos == 0)
5500 return 0;
5501
5502 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
5503 return nr_virtfn;
5504}
5505
5506/**
James Smart912e3ac2011-05-24 11:42:11 -04005507 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
5508 * @phba: pointer to lpfc hba data structure.
5509 * @nr_vfn: number of virtual functions to be enabled.
5510 *
5511 * This function enables the PCI SR-IOV virtual functions to a physical
5512 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
5513 * enable the number of virtual functions to the physical function. As
5514 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
5515 * API call does not considered as an error condition for most of the device.
5516 **/
5517int
5518lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
5519{
5520 struct pci_dev *pdev = phba->pcidev;
James Smart0a96e972011-07-22 18:37:28 -04005521 uint16_t max_nr_vfn;
James Smart912e3ac2011-05-24 11:42:11 -04005522 int rc;
5523
James Smart0a96e972011-07-22 18:37:28 -04005524 max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
5525 if (nr_vfn > max_nr_vfn) {
5526 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5527 "3057 Requested vfs (%d) greater than "
5528 "supported vfs (%d)", nr_vfn, max_nr_vfn);
5529 return -EINVAL;
5530 }
5531
James Smart912e3ac2011-05-24 11:42:11 -04005532 rc = pci_enable_sriov(pdev, nr_vfn);
5533 if (rc) {
5534 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5535 "2806 Failed to enable sriov on this device "
5536 "with vfn number nr_vf:%d, rc:%d\n",
5537 nr_vfn, rc);
5538 } else
5539 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5540 "2807 Successful enable sriov on this device "
5541 "with vfn number nr_vf:%d\n", nr_vfn);
5542 return rc;
5543}
5544
5545/**
James Smart895427b2017-02-12 13:52:30 -08005546 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
James Smart3772a992009-05-22 14:50:54 -04005547 * @phba: pointer to lpfc hba data structure.
5548 *
James Smart895427b2017-02-12 13:52:30 -08005549 * This routine is invoked to set up the driver internal resources before the
5550 * device specific resource setup to support the HBA device it attached to.
James Smart3772a992009-05-22 14:50:54 -04005551 *
5552 * Return codes
James Smart895427b2017-02-12 13:52:30 -08005553 * 0 - successful
5554 * other values - error
James Smart3772a992009-05-22 14:50:54 -04005555 **/
5556static int
James Smart895427b2017-02-12 13:52:30 -08005557lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
James Smart3772a992009-05-22 14:50:54 -04005558{
James Smart895427b2017-02-12 13:52:30 -08005559 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05005560
James Smart3772a992009-05-22 14:50:54 -04005561 /*
James Smart895427b2017-02-12 13:52:30 -08005562 * Driver resources common to all SLI revisions
James Smart3772a992009-05-22 14:50:54 -04005563 */
James Smart895427b2017-02-12 13:52:30 -08005564 atomic_set(&phba->fast_event_count, 0);
5565 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005566
James Smart895427b2017-02-12 13:52:30 -08005567 /* Initialize ndlp management spinlock */
5568 spin_lock_init(&phba->ndlp_lock);
James Smart3772a992009-05-22 14:50:54 -04005569
James Smart895427b2017-02-12 13:52:30 -08005570 INIT_LIST_HEAD(&phba->port_list);
5571 INIT_LIST_HEAD(&phba->work_list);
5572 init_waitqueue_head(&phba->wait_4_mlo_m_q);
5573
5574 /* Initialize the wait queue head for the kernel thread */
5575 init_waitqueue_head(&phba->work_waitq);
5576
5577 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smartf358dd02017-02-12 13:52:34 -08005578 "1403 Protocols supported %s %s %s\n",
James Smart895427b2017-02-12 13:52:30 -08005579 ((phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) ?
5580 "SCSI" : " "),
5581 ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) ?
James Smartf358dd02017-02-12 13:52:34 -08005582 "NVME" : " "),
5583 (phba->nvmet_support ? "NVMET" : " "));
James Smart895427b2017-02-12 13:52:30 -08005584
5585 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
5586 /* Initialize the scsi buffer list used by driver for scsi IO */
5587 spin_lock_init(&phba->scsi_buf_list_get_lock);
5588 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_get);
5589 spin_lock_init(&phba->scsi_buf_list_put_lock);
5590 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
5591 }
5592
5593 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
5594 (phba->nvmet_support == 0)) {
5595 /* Initialize the NVME buffer list used by driver for NVME IO */
5596 spin_lock_init(&phba->nvme_buf_list_get_lock);
5597 INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_get);
5598 spin_lock_init(&phba->nvme_buf_list_put_lock);
5599 INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_put);
5600 }
5601
5602 /* Initialize the fabric iocb list */
5603 INIT_LIST_HEAD(&phba->fabric_iocb_list);
5604
5605 /* Initialize list to save ELS buffers */
5606 INIT_LIST_HEAD(&phba->elsbuf);
5607
5608 /* Initialize FCF connection rec list */
5609 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
5610
5611 /* Initialize OAS configuration list */
5612 spin_lock_init(&phba->devicelock);
5613 INIT_LIST_HEAD(&phba->luns);
5614
James Smart3772a992009-05-22 14:50:54 -04005615 /* MBOX heartbeat timer */
Tomas Jasek33cc5592017-03-03 13:45:48 +01005616 setup_timer(&psli->mbox_tmo, lpfc_mbox_timeout, (unsigned long)phba);
James Smart3772a992009-05-22 14:50:54 -04005617 /* Fabric block timer */
Tomas Jasek33cc5592017-03-03 13:45:48 +01005618 setup_timer(&phba->fabric_block_timer, lpfc_fabric_block_timeout,
5619 (unsigned long)phba);
James Smart3772a992009-05-22 14:50:54 -04005620 /* EA polling mode timer */
Tomas Jasek33cc5592017-03-03 13:45:48 +01005621 setup_timer(&phba->eratt_poll, lpfc_poll_eratt,
5622 (unsigned long)phba);
James Smart895427b2017-02-12 13:52:30 -08005623 /* Heartbeat timer */
Tomas Jasek33cc5592017-03-03 13:45:48 +01005624 setup_timer(&phba->hb_tmofunc, lpfc_hb_timeout, (unsigned long)phba);
James Smart895427b2017-02-12 13:52:30 -08005625
5626 return 0;
5627}
5628
5629/**
5630 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev
5631 * @phba: pointer to lpfc hba data structure.
5632 *
5633 * This routine is invoked to set up the driver internal resources specific to
5634 * support the SLI-3 HBA device it attached to.
5635 *
5636 * Return codes
5637 * 0 - successful
5638 * other values - error
5639 **/
5640static int
5641lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
5642{
5643 int rc;
5644
5645 /*
5646 * Initialize timers used by driver
5647 */
5648
5649 /* FCP polling mode timer */
Tomas Jasek33cc5592017-03-03 13:45:48 +01005650 setup_timer(&phba->fcp_poll_timer, lpfc_poll_timeout,
5651 (unsigned long)phba);
James Smart3772a992009-05-22 14:50:54 -04005652
5653 /* Host attention work mask setup */
5654 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
5655 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
5656
5657 /* Get all the module params for configuring this host */
5658 lpfc_get_cfgparam(phba);
James Smart895427b2017-02-12 13:52:30 -08005659 /* Set up phase-1 common device driver resources */
5660
5661 rc = lpfc_setup_driver_resource_phase1(phba);
5662 if (rc)
5663 return -ENODEV;
5664
James Smart49198b32010-04-06 15:04:33 -04005665 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
5666 phba->menlo_flag |= HBA_MENLO_SUPPORT;
5667 /* check for menlo minimum sg count */
5668 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
5669 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
5670 }
5671
James Smart895427b2017-02-12 13:52:30 -08005672 if (!phba->sli.sli3_ring)
5673 phba->sli.sli3_ring = kzalloc(LPFC_SLI3_MAX_RING *
James Smart2a76a282012-08-03 12:35:54 -04005674 sizeof(struct lpfc_sli_ring), GFP_KERNEL);
James Smart895427b2017-02-12 13:52:30 -08005675 if (!phba->sli.sli3_ring)
James Smart2a76a282012-08-03 12:35:54 -04005676 return -ENOMEM;
5677
James Smart3772a992009-05-22 14:50:54 -04005678 /*
James Smart96f70772013-04-17 20:16:15 -04005679 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smart3772a992009-05-22 14:50:54 -04005680 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smart3772a992009-05-22 14:50:54 -04005681 */
James Smart3772a992009-05-22 14:50:54 -04005682
James Smart96f70772013-04-17 20:16:15 -04005683 /* Initialize the host templates the configured values. */
James Smart3772a992009-05-22 14:50:54 -04005684 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
James Smart96418b52017-03-04 09:30:31 -08005685 lpfc_template_no_hr.sg_tablesize = phba->cfg_sg_seg_cnt;
5686 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
James Smart3772a992009-05-22 14:50:54 -04005687
James Smart96f70772013-04-17 20:16:15 -04005688 /* There are going to be 2 reserved BDEs: 1 FCP cmnd + 1 FCP rsp */
5689 if (phba->cfg_enable_bg) {
5690 /*
5691 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
5692 * the FCP rsp, and a BDE for each. Sice we have no control
5693 * over how many protection data segments the SCSI Layer
5694 * will hand us (ie: there could be one for every block
5695 * in the IO), we just allocate enough BDEs to accomidate
5696 * our max amount and we need to limit lpfc_sg_seg_cnt to
5697 * minimize the risk of running out.
5698 */
5699 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5700 sizeof(struct fcp_rsp) +
5701 (LPFC_MAX_SG_SEG_CNT * sizeof(struct ulp_bde64));
5702
5703 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SEG_CNT_DIF)
5704 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT_DIF;
5705
5706 /* Total BDEs in BPL for scsi_sg_list and scsi_sg_prot_list */
5707 phba->cfg_total_seg_cnt = LPFC_MAX_SG_SEG_CNT;
5708 } else {
5709 /*
5710 * The scsi_buf for a regular I/O will hold the FCP cmnd,
5711 * the FCP rsp, a BDE for each, and a BDE for up to
5712 * cfg_sg_seg_cnt data segments.
5713 */
5714 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5715 sizeof(struct fcp_rsp) +
5716 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
5717
5718 /* Total BDEs in BPL for scsi_sg_list */
5719 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
5720 }
5721
5722 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
5723 "9088 sg_tablesize:%d dmabuf_size:%d total_bde:%d\n",
5724 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
5725 phba->cfg_total_seg_cnt);
5726
James Smart3772a992009-05-22 14:50:54 -04005727 phba->max_vpi = LPFC_MAX_VPI;
5728 /* This will be set to correct value after config_port mbox */
5729 phba->max_vports = 0;
5730
5731 /*
5732 * Initialize the SLI Layer to run with lpfc HBAs.
5733 */
5734 lpfc_sli_setup(phba);
James Smart895427b2017-02-12 13:52:30 -08005735 lpfc_sli_queue_init(phba);
James Smart3772a992009-05-22 14:50:54 -04005736
5737 /* Allocate device driver memory */
5738 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
5739 return -ENOMEM;
5740
James Smart912e3ac2011-05-24 11:42:11 -04005741 /*
5742 * Enable sr-iov virtual functions if supported and configured
5743 * through the module parameter.
5744 */
5745 if (phba->cfg_sriov_nr_virtfn > 0) {
5746 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
5747 phba->cfg_sriov_nr_virtfn);
5748 if (rc) {
5749 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5750 "2808 Requested number of SR-IOV "
5751 "virtual functions (%d) is not "
5752 "supported\n",
5753 phba->cfg_sriov_nr_virtfn);
5754 phba->cfg_sriov_nr_virtfn = 0;
5755 }
5756 }
5757
James Smart3772a992009-05-22 14:50:54 -04005758 return 0;
5759}
5760
5761/**
5762 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
5763 * @phba: pointer to lpfc hba data structure.
5764 *
5765 * This routine is invoked to unset the driver internal resources set up
5766 * specific for supporting the SLI-3 HBA device it attached to.
5767 **/
5768static void
5769lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
5770{
5771 /* Free device driver memory allocated */
5772 lpfc_mem_free_all(phba);
5773
5774 return;
5775}
5776
5777/**
James Smartda0436e2009-05-22 14:51:39 -04005778 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
5779 * @phba: pointer to lpfc hba data structure.
5780 *
5781 * This routine is invoked to set up the driver internal resources specific to
5782 * support the SLI-4 HBA device it attached to.
5783 *
5784 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005785 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005786 * other values - error
5787 **/
5788static int
5789lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
5790{
James Smart28baac72010-02-12 14:42:03 -05005791 LPFC_MBOXQ_t *mboxq;
James Smartf358dd02017-02-12 13:52:34 -08005792 MAILBOX_t *mb;
James Smart895427b2017-02-12 13:52:30 -08005793 int rc, i, max_buf_size;
James Smart28baac72010-02-12 14:42:03 -05005794 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
5795 struct lpfc_mqe *mqe;
James Smart09294d42013-04-17 20:16:05 -04005796 int longs;
James Smart1ba981f2014-02-20 09:56:45 -05005797 int fof_vectors = 0;
James Smartf358dd02017-02-12 13:52:34 -08005798 uint64_t wwn;
James Smartda0436e2009-05-22 14:51:39 -04005799
James Smart895427b2017-02-12 13:52:30 -08005800 phba->sli4_hba.num_online_cpu = num_online_cpus();
5801 phba->sli4_hba.num_present_cpu = lpfc_present_cpu;
5802 phba->sli4_hba.curr_disp_cpu = 0;
5803
James Smart716d3bc2013-09-06 12:18:28 -04005804 /* Get all the module params for configuring this host */
5805 lpfc_get_cfgparam(phba);
5806
James Smart895427b2017-02-12 13:52:30 -08005807 /* Set up phase-1 common device driver resources */
5808 rc = lpfc_setup_driver_resource_phase1(phba);
5809 if (rc)
5810 return -ENODEV;
5811
James Smartda0436e2009-05-22 14:51:39 -04005812 /* Before proceed, wait for POST done and device ready */
5813 rc = lpfc_sli4_post_status_check(phba);
5814 if (rc)
5815 return -ENODEV;
5816
5817 /*
5818 * Initialize timers used by driver
5819 */
5820
Tomas Jasek33cc5592017-03-03 13:45:48 +01005821 setup_timer(&phba->rrq_tmr, lpfc_rrq_timeout, (unsigned long)phba);
James Smartda0436e2009-05-22 14:51:39 -04005822
James Smartecfd03c2010-02-12 14:41:27 -05005823 /* FCF rediscover timer */
Tomas Jasek33cc5592017-03-03 13:45:48 +01005824 setup_timer(&phba->fcf.redisc_wait, lpfc_sli4_fcf_redisc_wait_tmo,
5825 (unsigned long)phba);
James Smartecfd03c2010-02-12 14:41:27 -05005826
James Smartda0436e2009-05-22 14:51:39 -04005827 /*
James Smart7ad20aa2011-05-24 11:44:28 -04005828 * Control structure for handling external multi-buffer mailbox
5829 * command pass-through.
5830 */
5831 memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
5832 sizeof(struct lpfc_mbox_ext_buf_ctx));
5833 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
5834
James Smartda0436e2009-05-22 14:51:39 -04005835 phba->max_vpi = LPFC_MAX_VPI;
James Smart67d12732012-08-03 12:36:13 -04005836
James Smartda0436e2009-05-22 14:51:39 -04005837 /* This will be set to correct value after the read_config mbox */
5838 phba->max_vports = 0;
5839
5840 /* Program the default value of vlan_id and fc_map */
5841 phba->valid_vlan = 0;
5842 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5843 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5844 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5845
5846 /*
James Smart2a76a282012-08-03 12:35:54 -04005847 * For SLI4, instead of using ring 0 (LPFC_FCP_RING) for FCP commands
James Smart895427b2017-02-12 13:52:30 -08005848 * we will associate a new ring, for each EQ/CQ/WQ tuple.
5849 * The WQ create will allocate the ring.
James Smart2a76a282012-08-03 12:35:54 -04005850 */
James Smart085c6472010-11-20 23:11:37 -05005851
James Smart09294d42013-04-17 20:16:05 -04005852 /*
5853 * It doesn't matter what family our adapter is in, we are
5854 * limited to 2 Pages, 512 SGEs, for our SGL.
5855 * There are going to be 2 reserved SGEs: 1 FCP cmnd + 1 FCP rsp
5856 */
5857 max_buf_size = (2 * SLI4_PAGE_SIZE);
5858 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SGL_SEG_CNT - 2)
5859 phba->cfg_sg_seg_cnt = LPFC_MAX_SGL_SEG_CNT - 2;
James Smart09294d42013-04-17 20:16:05 -04005860
James Smartda0436e2009-05-22 14:51:39 -04005861 /*
James Smart895427b2017-02-12 13:52:30 -08005862 * Since lpfc_sg_seg_cnt is module param, the sg_dma_buf_size
5863 * used to create the sg_dma_buf_pool must be calculated.
James Smart28baac72010-02-12 14:42:03 -05005864 */
James Smart96f70772013-04-17 20:16:15 -04005865 if (phba->cfg_enable_bg) {
5866 /*
James Smart895427b2017-02-12 13:52:30 -08005867 * The scsi_buf for a T10-DIF I/O holds the FCP cmnd,
5868 * the FCP rsp, and a SGE. Sice we have no control
5869 * over how many protection segments the SCSI Layer
James Smart96f70772013-04-17 20:16:15 -04005870 * will hand us (ie: there could be one for every block
James Smart895427b2017-02-12 13:52:30 -08005871 * in the IO), just allocate enough SGEs to accomidate
5872 * our max amount and we need to limit lpfc_sg_seg_cnt
5873 * to minimize the risk of running out.
James Smart96f70772013-04-17 20:16:15 -04005874 */
5875 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
James Smart895427b2017-02-12 13:52:30 -08005876 sizeof(struct fcp_rsp) + max_buf_size;
James Smart96f70772013-04-17 20:16:15 -04005877
5878 /* Total SGEs for scsi_sg_list and scsi_sg_prot_list */
5879 phba->cfg_total_seg_cnt = LPFC_MAX_SGL_SEG_CNT;
5880
5881 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SLI4_SEG_CNT_DIF)
James Smart895427b2017-02-12 13:52:30 -08005882 phba->cfg_sg_seg_cnt =
5883 LPFC_MAX_SG_SLI4_SEG_CNT_DIF;
James Smart96f70772013-04-17 20:16:15 -04005884 } else {
5885 /*
James Smart895427b2017-02-12 13:52:30 -08005886 * The scsi_buf for a regular I/O holds the FCP cmnd,
James Smart96f70772013-04-17 20:16:15 -04005887 * the FCP rsp, a SGE for each, and a SGE for up to
5888 * cfg_sg_seg_cnt data segments.
5889 */
5890 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
James Smart895427b2017-02-12 13:52:30 -08005891 sizeof(struct fcp_rsp) +
5892 ((phba->cfg_sg_seg_cnt + 2) *
5893 sizeof(struct sli4_sge));
James Smart96f70772013-04-17 20:16:15 -04005894
5895 /* Total SGEs for scsi_sg_list */
5896 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
James Smart895427b2017-02-12 13:52:30 -08005897
James Smart96f70772013-04-17 20:16:15 -04005898 /*
James Smart895427b2017-02-12 13:52:30 -08005899 * NOTE: if (phba->cfg_sg_seg_cnt + 2) <= 256 we only
5900 * need to post 1 page for the SGL.
James Smart96f70772013-04-17 20:16:15 -04005901 */
James Smart085c6472010-11-20 23:11:37 -05005902 }
James Smartacd68592012-01-18 16:25:09 -05005903
James Smart96f70772013-04-17 20:16:15 -04005904 /* Initialize the host templates with the updated values. */
5905 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5906 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
James Smart96418b52017-03-04 09:30:31 -08005907 lpfc_template_no_hr.sg_tablesize = phba->cfg_sg_seg_cnt;
James Smart96f70772013-04-17 20:16:15 -04005908
5909 if (phba->cfg_sg_dma_buf_size <= LPFC_MIN_SG_SLI4_BUF_SZ)
5910 phba->cfg_sg_dma_buf_size = LPFC_MIN_SG_SLI4_BUF_SZ;
5911 else
5912 phba->cfg_sg_dma_buf_size =
5913 SLI4_PAGE_ALIGN(phba->cfg_sg_dma_buf_size);
5914
5915 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
5916 "9087 sg_tablesize:%d dmabuf_size:%d total_sge:%d\n",
5917 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
5918 phba->cfg_total_seg_cnt);
James Smartda0436e2009-05-22 14:51:39 -04005919
5920 /* Initialize buffer queue management fields */
James Smart895427b2017-02-12 13:52:30 -08005921 INIT_LIST_HEAD(&phba->hbqs[LPFC_ELS_HBQ].hbq_buffer_list);
James Smartda0436e2009-05-22 14:51:39 -04005922 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
5923 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
5924
5925 /*
5926 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
5927 */
James Smart895427b2017-02-12 13:52:30 -08005928 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
5929 /* Initialize the Abort scsi buffer list used by driver */
5930 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
5931 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
5932 }
5933
5934 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
5935 /* Initialize the Abort nvme buffer list used by driver */
5936 spin_lock_init(&phba->sli4_hba.abts_nvme_buf_list_lock);
5937 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvme_buf_list);
James Smart86c67372017-04-21 16:05:04 -07005938 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
James Smarta8cf5df2017-05-15 15:20:46 -07005939 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_io_wait_list);
James Smart6c621a22017-05-15 15:20:45 -07005940
James Smart318083a2017-03-04 09:30:30 -08005941 /* Fast-path XRI aborted CQ Event work queue list */
5942 INIT_LIST_HEAD(&phba->sli4_hba.sp_nvme_xri_aborted_work_queue);
James Smart895427b2017-02-12 13:52:30 -08005943 }
5944
James Smartda0436e2009-05-22 14:51:39 -04005945 /* This abort list used by worker thread */
James Smart895427b2017-02-12 13:52:30 -08005946 spin_lock_init(&phba->sli4_hba.sgl_list_lock);
James Smarta8cf5df2017-05-15 15:20:46 -07005947 spin_lock_init(&phba->sli4_hba.nvmet_io_wait_lock);
James Smartda0436e2009-05-22 14:51:39 -04005948
5949 /*
James Smart6d368e52011-05-24 11:44:12 -04005950 * Initialize driver internal slow-path work queues
James Smartda0436e2009-05-22 14:51:39 -04005951 */
5952
5953 /* Driver internel slow-path CQ Event pool */
5954 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
5955 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04005956 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04005957 /* Asynchronous event CQ Event work queue list */
5958 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
5959 /* Fast-path XRI aborted CQ Event work queue list */
5960 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
5961 /* Slow-path XRI aborted CQ Event work queue list */
5962 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
5963 /* Receive queue CQ Event work queue list */
5964 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
5965
James Smart6d368e52011-05-24 11:44:12 -04005966 /* Initialize extent block lists. */
5967 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
5968 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
5969 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
5970 INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
5971
James Smartd1f525a2017-04-21 16:04:55 -07005972 /* Initialize mboxq lists. If the early init routines fail
5973 * these lists need to be correctly initialized.
5974 */
5975 INIT_LIST_HEAD(&phba->sli.mboxq);
5976 INIT_LIST_HEAD(&phba->sli.mboxq_cmpl);
5977
James Smart448193b2015-12-16 18:12:05 -05005978 /* initialize optic_state to 0xFF */
5979 phba->sli4_hba.lnk_info.optic_state = 0xff;
5980
James Smartda0436e2009-05-22 14:51:39 -04005981 /* Allocate device driver memory */
5982 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
5983 if (rc)
5984 return -ENOMEM;
5985
James Smart2fcee4b2010-12-15 17:57:46 -05005986 /* IF Type 2 ports get initialized now. */
5987 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
5988 LPFC_SLI_INTF_IF_TYPE_2) {
5989 rc = lpfc_pci_function_reset(phba);
James Smart895427b2017-02-12 13:52:30 -08005990 if (unlikely(rc)) {
5991 rc = -ENODEV;
5992 goto out_free_mem;
5993 }
James Smart946727d2015-04-07 15:07:09 -04005994 phba->temp_sensor_support = 1;
James Smart2fcee4b2010-12-15 17:57:46 -05005995 }
5996
James Smartda0436e2009-05-22 14:51:39 -04005997 /* Create the bootstrap mailbox command */
5998 rc = lpfc_create_bootstrap_mbox(phba);
5999 if (unlikely(rc))
6000 goto out_free_mem;
6001
6002 /* Set up the host's endian order with the device. */
6003 rc = lpfc_setup_endian_order(phba);
6004 if (unlikely(rc))
6005 goto out_free_bsmbx;
6006
6007 /* Set up the hba's configuration parameters. */
6008 rc = lpfc_sli4_read_config(phba);
6009 if (unlikely(rc))
6010 goto out_free_bsmbx;
James Smartcff261f2013-12-17 20:29:47 -05006011 rc = lpfc_mem_alloc_active_rrq_pool_s4(phba);
6012 if (unlikely(rc))
6013 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04006014
James Smart2fcee4b2010-12-15 17:57:46 -05006015 /* IF Type 0 ports get initialized now. */
6016 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
6017 LPFC_SLI_INTF_IF_TYPE_0) {
6018 rc = lpfc_pci_function_reset(phba);
6019 if (unlikely(rc))
6020 goto out_free_bsmbx;
6021 }
James Smartda0436e2009-05-22 14:51:39 -04006022
James Smartcb5172e2010-03-15 11:25:07 -04006023 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6024 GFP_KERNEL);
6025 if (!mboxq) {
6026 rc = -ENOMEM;
6027 goto out_free_bsmbx;
6028 }
6029
James Smartf358dd02017-02-12 13:52:34 -08006030 /* Check for NVMET being configured */
James Smart895427b2017-02-12 13:52:30 -08006031 phba->nvmet_support = 0;
James Smartf358dd02017-02-12 13:52:34 -08006032 if (lpfc_enable_nvmet_cnt) {
6033
6034 /* First get WWN of HBA instance */
6035 lpfc_read_nv(phba, mboxq);
6036 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6037 if (rc != MBX_SUCCESS) {
6038 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6039 "6016 Mailbox failed , mbxCmd x%x "
6040 "READ_NV, mbxStatus x%x\n",
6041 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
6042 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smartd1f525a2017-04-21 16:04:55 -07006043 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartf358dd02017-02-12 13:52:34 -08006044 rc = -EIO;
6045 goto out_free_bsmbx;
6046 }
6047 mb = &mboxq->u.mb;
6048 memcpy(&wwn, (char *)mb->un.varRDnvp.nodename,
6049 sizeof(uint64_t));
6050 wwn = cpu_to_be64(wwn);
6051 phba->sli4_hba.wwnn.u.name = wwn;
6052 memcpy(&wwn, (char *)mb->un.varRDnvp.portname,
6053 sizeof(uint64_t));
6054 /* wwn is WWPN of HBA instance */
6055 wwn = cpu_to_be64(wwn);
6056 phba->sli4_hba.wwpn.u.name = wwn;
6057
6058 /* Check to see if it matches any module parameter */
6059 for (i = 0; i < lpfc_enable_nvmet_cnt; i++) {
6060 if (wwn == lpfc_enable_nvmet[i]) {
James Smart7d708032017-03-08 14:36:01 -08006061#if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
James Smart3c603be2017-05-15 15:20:44 -07006062 if (lpfc_nvmet_mem_alloc(phba))
6063 break;
6064
6065 phba->nvmet_support = 1; /* a match */
6066
James Smartf358dd02017-02-12 13:52:34 -08006067 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6068 "6017 NVME Target %016llx\n",
6069 wwn);
James Smart7d708032017-03-08 14:36:01 -08006070#else
6071 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6072 "6021 Can't enable NVME Target."
6073 " NVME_TARGET_FC infrastructure"
6074 " is not in kernel\n");
6075#endif
James Smart3c603be2017-05-15 15:20:44 -07006076 break;
James Smartf358dd02017-02-12 13:52:34 -08006077 }
6078 }
6079 }
James Smart895427b2017-02-12 13:52:30 -08006080
6081 lpfc_nvme_mod_param_dep(phba);
6082
James Smartfedd3b72011-02-16 12:39:24 -05006083 /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
James Smartcb5172e2010-03-15 11:25:07 -04006084 lpfc_supported_pages(mboxq);
6085 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smartfedd3b72011-02-16 12:39:24 -05006086 if (!rc) {
6087 mqe = &mboxq->u.mqe;
6088 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
6089 LPFC_MAX_SUPPORTED_PAGES);
6090 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
6091 switch (pn_page[i]) {
6092 case LPFC_SLI4_PARAMETERS:
6093 phba->sli4_hba.pc_sli4_params.supported = 1;
6094 break;
6095 default:
6096 break;
6097 }
6098 }
6099 /* Read the port's SLI4 Parameters capabilities if supported. */
6100 if (phba->sli4_hba.pc_sli4_params.supported)
6101 rc = lpfc_pc_sli4_params_get(phba, mboxq);
6102 if (rc) {
6103 mempool_free(mboxq, phba->mbox_mem_pool);
6104 rc = -EIO;
6105 goto out_free_bsmbx;
James Smartcb5172e2010-03-15 11:25:07 -04006106 }
6107 }
James Smart65791f12016-07-06 12:35:56 -07006108
James Smartfedd3b72011-02-16 12:39:24 -05006109 /*
6110 * Get sli4 parameters that override parameters from Port capabilities.
James Smart6d368e52011-05-24 11:44:12 -04006111 * If this call fails, it isn't critical unless the SLI4 parameters come
6112 * back in conflict.
James Smartfedd3b72011-02-16 12:39:24 -05006113 */
James Smart6d368e52011-05-24 11:44:12 -04006114 rc = lpfc_get_sli4_parameters(phba, mboxq);
6115 if (rc) {
6116 if (phba->sli4_hba.extents_in_use &&
6117 phba->sli4_hba.rpi_hdrs_in_use) {
6118 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6119 "2999 Unsupported SLI4 Parameters "
6120 "Extents and RPI headers enabled.\n");
James Smart6d368e52011-05-24 11:44:12 -04006121 }
James Smart895427b2017-02-12 13:52:30 -08006122 mempool_free(mboxq, phba->mbox_mem_pool);
6123 goto out_free_bsmbx;
James Smart6d368e52011-05-24 11:44:12 -04006124 }
James Smart895427b2017-02-12 13:52:30 -08006125
James Smartcb5172e2010-03-15 11:25:07 -04006126 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1ba981f2014-02-20 09:56:45 -05006127
6128 /* Verify OAS is supported */
6129 lpfc_sli4_oas_verify(phba);
6130 if (phba->cfg_fof)
6131 fof_vectors = 1;
6132
James Smart5350d872011-10-10 21:33:49 -04006133 /* Verify all the SLI4 queues */
6134 rc = lpfc_sli4_queue_verify(phba);
James Smartda0436e2009-05-22 14:51:39 -04006135 if (rc)
6136 goto out_free_bsmbx;
6137
6138 /* Create driver internal CQE event pool */
6139 rc = lpfc_sli4_cq_event_pool_create(phba);
6140 if (rc)
James Smart5350d872011-10-10 21:33:49 -04006141 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04006142
James Smart8a9d2e82012-05-09 21:16:12 -04006143 /* Initialize sgl lists per host */
6144 lpfc_init_sgl_list(phba);
6145
6146 /* Allocate and initialize active sgl array */
James Smartda0436e2009-05-22 14:51:39 -04006147 rc = lpfc_init_active_sgl_array(phba);
6148 if (rc) {
6149 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6150 "1430 Failed to initialize sgl list.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04006151 goto out_destroy_cq_event_pool;
James Smartda0436e2009-05-22 14:51:39 -04006152 }
James Smartda0436e2009-05-22 14:51:39 -04006153 rc = lpfc_sli4_init_rpi_hdrs(phba);
6154 if (rc) {
6155 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6156 "1432 Failed to initialize rpi headers.\n");
6157 goto out_free_active_sgl;
6158 }
6159
James Smarta93ff372010-10-22 11:06:08 -04006160 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
James Smart0c9ab6f2010-02-26 14:15:57 -05006161 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
6162 phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
6163 GFP_KERNEL);
6164 if (!phba->fcf.fcf_rr_bmask) {
6165 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6166 "2759 Failed allocate memory for FCF round "
6167 "robin failover bmask\n");
James Smart05580562011-05-24 11:40:48 -04006168 rc = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -05006169 goto out_remove_rpi_hdrs;
6170 }
6171
James Smart895427b2017-02-12 13:52:30 -08006172 phba->sli4_hba.hba_eq_hdl = kcalloc(fof_vectors + phba->io_channel_irqs,
6173 sizeof(struct lpfc_hba_eq_hdl),
6174 GFP_KERNEL);
6175 if (!phba->sli4_hba.hba_eq_hdl) {
James Smart67d12732012-08-03 12:36:13 -04006176 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6177 "2572 Failed allocate memory for "
6178 "fast-path per-EQ handle array\n");
6179 rc = -ENOMEM;
6180 goto out_free_fcf_rr_bmask;
James Smartda0436e2009-05-22 14:51:39 -04006181 }
6182
James Smart895427b2017-02-12 13:52:30 -08006183 phba->sli4_hba.cpu_map = kcalloc(phba->sli4_hba.num_present_cpu,
6184 sizeof(struct lpfc_vector_map_info),
6185 GFP_KERNEL);
James Smart7bb03bb2013-04-17 20:19:16 -04006186 if (!phba->sli4_hba.cpu_map) {
6187 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6188 "3327 Failed allocate memory for msi-x "
6189 "interrupt vector mapping\n");
6190 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08006191 goto out_free_hba_eq_hdl;
James Smart7bb03bb2013-04-17 20:19:16 -04006192 }
James Smartb246de12013-05-31 17:03:07 -04006193 if (lpfc_used_cpu == NULL) {
James Smart895427b2017-02-12 13:52:30 -08006194 lpfc_used_cpu = kcalloc(lpfc_present_cpu, sizeof(uint16_t),
6195 GFP_KERNEL);
James Smartb246de12013-05-31 17:03:07 -04006196 if (!lpfc_used_cpu) {
6197 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6198 "3335 Failed allocate memory for msi-x "
6199 "interrupt vector mapping\n");
6200 kfree(phba->sli4_hba.cpu_map);
6201 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08006202 goto out_free_hba_eq_hdl;
James Smartb246de12013-05-31 17:03:07 -04006203 }
6204 for (i = 0; i < lpfc_present_cpu; i++)
6205 lpfc_used_cpu[i] = LPFC_VECTOR_MAP_EMPTY;
6206 }
6207
James Smart912e3ac2011-05-24 11:42:11 -04006208 /*
6209 * Enable sr-iov virtual functions if supported and configured
6210 * through the module parameter.
6211 */
6212 if (phba->cfg_sriov_nr_virtfn > 0) {
6213 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
6214 phba->cfg_sriov_nr_virtfn);
6215 if (rc) {
6216 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6217 "3020 Requested number of SR-IOV "
6218 "virtual functions (%d) is not "
6219 "supported\n",
6220 phba->cfg_sriov_nr_virtfn);
6221 phba->cfg_sriov_nr_virtfn = 0;
6222 }
6223 }
6224
James Smart5248a742011-07-22 18:37:06 -04006225 return 0;
James Smartda0436e2009-05-22 14:51:39 -04006226
James Smart895427b2017-02-12 13:52:30 -08006227out_free_hba_eq_hdl:
6228 kfree(phba->sli4_hba.hba_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05006229out_free_fcf_rr_bmask:
6230 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04006231out_remove_rpi_hdrs:
6232 lpfc_sli4_remove_rpi_hdrs(phba);
6233out_free_active_sgl:
6234 lpfc_free_active_sgl(phba);
James Smartda0436e2009-05-22 14:51:39 -04006235out_destroy_cq_event_pool:
6236 lpfc_sli4_cq_event_pool_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04006237out_free_bsmbx:
6238 lpfc_destroy_bootstrap_mbox(phba);
6239out_free_mem:
6240 lpfc_mem_free(phba);
6241 return rc;
6242}
6243
6244/**
6245 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
6246 * @phba: pointer to lpfc hba data structure.
6247 *
6248 * This routine is invoked to unset the driver internal resources set up
6249 * specific for supporting the SLI-4 HBA device it attached to.
6250 **/
6251static void
6252lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
6253{
6254 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6255
James Smart7bb03bb2013-04-17 20:19:16 -04006256 /* Free memory allocated for msi-x interrupt vector to CPU mapping */
6257 kfree(phba->sli4_hba.cpu_map);
6258 phba->sli4_hba.num_present_cpu = 0;
6259 phba->sli4_hba.num_online_cpu = 0;
James Smart76fd07a2014-02-20 09:57:18 -05006260 phba->sli4_hba.curr_disp_cpu = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04006261
James Smartda0436e2009-05-22 14:51:39 -04006262 /* Free memory allocated for fast-path work queue handles */
James Smart895427b2017-02-12 13:52:30 -08006263 kfree(phba->sli4_hba.hba_eq_hdl);
James Smartda0436e2009-05-22 14:51:39 -04006264
6265 /* Free the allocated rpi headers. */
6266 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04006267 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04006268
James Smart0c9ab6f2010-02-26 14:15:57 -05006269 /* Free eligible FCF index bmask */
6270 kfree(phba->fcf.fcf_rr_bmask);
6271
James Smartda0436e2009-05-22 14:51:39 -04006272 /* Free the ELS sgl list */
6273 lpfc_free_active_sgl(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04006274 lpfc_free_els_sgl_list(phba);
James Smartf358dd02017-02-12 13:52:34 -08006275 lpfc_free_nvmet_sgl_list(phba);
James Smartda0436e2009-05-22 14:51:39 -04006276
James Smartda0436e2009-05-22 14:51:39 -04006277 /* Free the completion queue EQ event pool */
6278 lpfc_sli4_cq_event_release_all(phba);
6279 lpfc_sli4_cq_event_pool_destroy(phba);
6280
James Smart6d368e52011-05-24 11:44:12 -04006281 /* Release resource identifiers. */
6282 lpfc_sli4_dealloc_resource_identifiers(phba);
6283
James Smartda0436e2009-05-22 14:51:39 -04006284 /* Free the bsmbx region. */
6285 lpfc_destroy_bootstrap_mbox(phba);
6286
6287 /* Free the SLI Layer memory with SLI4 HBAs */
6288 lpfc_mem_free_all(phba);
6289
6290 /* Free the current connect table */
6291 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04006292 &phba->fcf_conn_rec_list, list) {
6293 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04006294 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04006295 }
James Smartda0436e2009-05-22 14:51:39 -04006296
6297 return;
6298}
6299
6300/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006301 * lpfc_init_api_table_setup - Set up init api function jump table
James Smart3772a992009-05-22 14:50:54 -04006302 * @phba: The hba struct for which this call is being executed.
6303 * @dev_grp: The HBA PCI-Device group number.
6304 *
6305 * This routine sets up the device INIT interface API function jump table
6306 * in @phba struct.
6307 *
6308 * Returns: 0 - success, -ENODEV - failure.
6309 **/
6310int
6311lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6312{
James Smart84d1b002010-02-12 14:42:33 -05006313 phba->lpfc_hba_init_link = lpfc_hba_init_link;
6314 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart7f860592011-03-11 16:05:52 -05006315 phba->lpfc_selective_reset = lpfc_selective_reset;
James Smart3772a992009-05-22 14:50:54 -04006316 switch (dev_grp) {
6317 case LPFC_PCI_DEV_LP:
6318 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
6319 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
6320 phba->lpfc_stop_port = lpfc_stop_port_s3;
6321 break;
James Smartda0436e2009-05-22 14:51:39 -04006322 case LPFC_PCI_DEV_OC:
6323 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
6324 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
6325 phba->lpfc_stop_port = lpfc_stop_port_s4;
6326 break;
James Smart3772a992009-05-22 14:50:54 -04006327 default:
6328 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6329 "1431 Invalid HBA PCI-device group: 0x%x\n",
6330 dev_grp);
6331 return -ENODEV;
6332 break;
6333 }
6334 return 0;
6335}
6336
6337/**
James Smart3772a992009-05-22 14:50:54 -04006338 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
6339 * @phba: pointer to lpfc hba data structure.
6340 *
6341 * This routine is invoked to set up the driver internal resources after the
6342 * device specific resource setup to support the HBA device it attached to.
6343 *
6344 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006345 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006346 * other values - error
6347 **/
6348static int
6349lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
6350{
6351 int error;
6352
6353 /* Startup the kernel thread for this host adapter. */
6354 phba->worker_thread = kthread_run(lpfc_do_work, phba,
6355 "lpfc_worker_%d", phba->brd_no);
6356 if (IS_ERR(phba->worker_thread)) {
6357 error = PTR_ERR(phba->worker_thread);
6358 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05006359 }
6360
James Smart3772a992009-05-22 14:50:54 -04006361 return 0;
6362}
6363
6364/**
6365 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
6366 * @phba: pointer to lpfc hba data structure.
6367 *
6368 * This routine is invoked to unset the driver internal resources set up after
6369 * the device specific resource setup for supporting the HBA device it
6370 * attached to.
6371 **/
6372static void
6373lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
6374{
6375 /* Stop kernel worker thread */
6376 kthread_stop(phba->worker_thread);
6377}
6378
6379/**
6380 * lpfc_free_iocb_list - Free iocb list.
6381 * @phba: pointer to lpfc hba data structure.
6382 *
6383 * This routine is invoked to free the driver's IOCB list and memory.
6384 **/
James Smart6c621a22017-05-15 15:20:45 -07006385void
James Smart3772a992009-05-22 14:50:54 -04006386lpfc_free_iocb_list(struct lpfc_hba *phba)
6387{
6388 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
6389
6390 spin_lock_irq(&phba->hbalock);
6391 list_for_each_entry_safe(iocbq_entry, iocbq_next,
6392 &phba->lpfc_iocb_list, list) {
6393 list_del(&iocbq_entry->list);
6394 kfree(iocbq_entry);
6395 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05006396 }
James Smart3772a992009-05-22 14:50:54 -04006397 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006398
James Smart3772a992009-05-22 14:50:54 -04006399 return;
6400}
dea31012005-04-17 16:05:31 -05006401
James Smart3772a992009-05-22 14:50:54 -04006402/**
6403 * lpfc_init_iocb_list - Allocate and initialize iocb list.
6404 * @phba: pointer to lpfc hba data structure.
6405 *
6406 * This routine is invoked to allocate and initizlize the driver's IOCB
6407 * list and set up the IOCB tag array accordingly.
6408 *
6409 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006410 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006411 * other values - error
6412 **/
James Smart6c621a22017-05-15 15:20:45 -07006413int
James Smart3772a992009-05-22 14:50:54 -04006414lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
6415{
6416 struct lpfc_iocbq *iocbq_entry = NULL;
6417 uint16_t iotag;
6418 int i;
dea31012005-04-17 16:05:31 -05006419
6420 /* Initialize and populate the iocb list per host. */
6421 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04006422 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07006423 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05006424 if (iocbq_entry == NULL) {
6425 printk(KERN_ERR "%s: only allocated %d iocbs of "
6426 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07006427 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05006428 goto out_free_iocbq;
6429 }
6430
James Bottomley604a3e32005-10-29 10:28:33 -05006431 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
6432 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04006433 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05006434 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04006435 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05006436 goto out_free_iocbq;
6437 }
James Smart6d368e52011-05-24 11:44:12 -04006438 iocbq_entry->sli4_lxritag = NO_XRI;
James Smart3772a992009-05-22 14:50:54 -04006439 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05006440
6441 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006442 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
6443 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05006444 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006445 }
6446
James Smart3772a992009-05-22 14:50:54 -04006447 return 0;
6448
6449out_free_iocbq:
6450 lpfc_free_iocb_list(phba);
6451
6452 return -ENOMEM;
6453}
6454
6455/**
James Smart8a9d2e82012-05-09 21:16:12 -04006456 * lpfc_free_sgl_list - Free a given sgl list.
James Smartda0436e2009-05-22 14:51:39 -04006457 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -04006458 * @sglq_list: pointer to the head of sgl list.
James Smartda0436e2009-05-22 14:51:39 -04006459 *
James Smart8a9d2e82012-05-09 21:16:12 -04006460 * This routine is invoked to free a give sgl list and memory.
James Smartda0436e2009-05-22 14:51:39 -04006461 **/
James Smart8a9d2e82012-05-09 21:16:12 -04006462void
6463lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
James Smartda0436e2009-05-22 14:51:39 -04006464{
6465 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart8a9d2e82012-05-09 21:16:12 -04006466
6467 list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
6468 list_del(&sglq_entry->list);
6469 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
6470 kfree(sglq_entry);
6471 }
6472}
6473
6474/**
6475 * lpfc_free_els_sgl_list - Free els sgl list.
6476 * @phba: pointer to lpfc hba data structure.
6477 *
6478 * This routine is invoked to free the driver's els sgl list and memory.
6479 **/
6480static void
6481lpfc_free_els_sgl_list(struct lpfc_hba *phba)
6482{
James Smartda0436e2009-05-22 14:51:39 -04006483 LIST_HEAD(sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04006484
James Smart8a9d2e82012-05-09 21:16:12 -04006485 /* Retrieve all els sgls from driver list */
James Smartda0436e2009-05-22 14:51:39 -04006486 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006487 spin_lock(&phba->sli4_hba.sgl_list_lock);
6488 list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list, &sglq_list);
6489 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04006490 spin_unlock_irq(&phba->hbalock);
6491
James Smart8a9d2e82012-05-09 21:16:12 -04006492 /* Now free the sgl list */
6493 lpfc_free_sgl_list(phba, &sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04006494}
6495
6496/**
James Smartf358dd02017-02-12 13:52:34 -08006497 * lpfc_free_nvmet_sgl_list - Free nvmet sgl list.
6498 * @phba: pointer to lpfc hba data structure.
6499 *
6500 * This routine is invoked to free the driver's nvmet sgl list and memory.
6501 **/
6502static void
6503lpfc_free_nvmet_sgl_list(struct lpfc_hba *phba)
6504{
6505 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
6506 LIST_HEAD(sglq_list);
6507
6508 /* Retrieve all nvmet sgls from driver list */
6509 spin_lock_irq(&phba->hbalock);
6510 spin_lock(&phba->sli4_hba.sgl_list_lock);
6511 list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list, &sglq_list);
6512 spin_unlock(&phba->sli4_hba.sgl_list_lock);
6513 spin_unlock_irq(&phba->hbalock);
6514
6515 /* Now free the sgl list */
6516 list_for_each_entry_safe(sglq_entry, sglq_next, &sglq_list, list) {
6517 list_del(&sglq_entry->list);
6518 lpfc_nvmet_buf_free(phba, sglq_entry->virt, sglq_entry->phys);
6519 kfree(sglq_entry);
6520 }
Dick Kennedy4b40d022017-08-23 16:55:38 -07006521
6522 /* Update the nvmet_xri_cnt to reflect no current sgls.
6523 * The next initialization cycle sets the count and allocates
6524 * the sgls over again.
6525 */
6526 phba->sli4_hba.nvmet_xri_cnt = 0;
James Smartf358dd02017-02-12 13:52:34 -08006527}
6528
6529/**
James Smartda0436e2009-05-22 14:51:39 -04006530 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
6531 * @phba: pointer to lpfc hba data structure.
6532 *
6533 * This routine is invoked to allocate the driver's active sgl memory.
6534 * This array will hold the sglq_entry's for active IOs.
6535 **/
6536static int
6537lpfc_init_active_sgl_array(struct lpfc_hba *phba)
6538{
6539 int size;
6540 size = sizeof(struct lpfc_sglq *);
6541 size *= phba->sli4_hba.max_cfg_param.max_xri;
6542
6543 phba->sli4_hba.lpfc_sglq_active_list =
6544 kzalloc(size, GFP_KERNEL);
6545 if (!phba->sli4_hba.lpfc_sglq_active_list)
6546 return -ENOMEM;
6547 return 0;
6548}
6549
6550/**
6551 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
6552 * @phba: pointer to lpfc hba data structure.
6553 *
6554 * This routine is invoked to walk through the array of active sglq entries
6555 * and free all of the resources.
6556 * This is just a place holder for now.
6557 **/
6558static void
6559lpfc_free_active_sgl(struct lpfc_hba *phba)
6560{
6561 kfree(phba->sli4_hba.lpfc_sglq_active_list);
6562}
6563
6564/**
6565 * lpfc_init_sgl_list - Allocate and initialize sgl list.
6566 * @phba: pointer to lpfc hba data structure.
6567 *
6568 * This routine is invoked to allocate and initizlize the driver's sgl
6569 * list and set up the sgl xritag tag array accordingly.
6570 *
James Smartda0436e2009-05-22 14:51:39 -04006571 **/
James Smart8a9d2e82012-05-09 21:16:12 -04006572static void
James Smartda0436e2009-05-22 14:51:39 -04006573lpfc_init_sgl_list(struct lpfc_hba *phba)
6574{
James Smartda0436e2009-05-22 14:51:39 -04006575 /* Initialize and populate the sglq list per host/VF. */
James Smart895427b2017-02-12 13:52:30 -08006576 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_els_sgl_list);
James Smartda0436e2009-05-22 14:51:39 -04006577 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smartf358dd02017-02-12 13:52:34 -08006578 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_sgl_list);
James Smart86c67372017-04-21 16:05:04 -07006579 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
James Smartda0436e2009-05-22 14:51:39 -04006580
James Smart8a9d2e82012-05-09 21:16:12 -04006581 /* els xri-sgl book keeping */
6582 phba->sli4_hba.els_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04006583
James Smart8a9d2e82012-05-09 21:16:12 -04006584 /* scsi xri-buffer book keeping */
James Smartda0436e2009-05-22 14:51:39 -04006585 phba->sli4_hba.scsi_xri_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -08006586
6587 /* nvme xri-buffer book keeping */
6588 phba->sli4_hba.nvme_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04006589}
6590
6591/**
6592 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
6593 * @phba: pointer to lpfc hba data structure.
6594 *
6595 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -04006596 * port for those SLI4 ports that do not support extents. This routine
James Smartda0436e2009-05-22 14:51:39 -04006597 * posts a PAGE_SIZE memory region to the port to hold up to
James Smart88a2cfb2011-07-22 18:36:33 -04006598 * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
6599 * and should be called only when interrupts are disabled.
James Smartda0436e2009-05-22 14:51:39 -04006600 *
6601 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006602 * 0 - successful
James Smart88a2cfb2011-07-22 18:36:33 -04006603 * -ERROR - otherwise.
James Smartda0436e2009-05-22 14:51:39 -04006604 **/
6605int
6606lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
6607{
6608 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04006609 struct lpfc_rpi_hdr *rpi_hdr;
6610
6611 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
James Smartff78d8f2011-12-13 13:21:35 -05006612 if (!phba->sli4_hba.rpi_hdrs_in_use)
James Smart6d368e52011-05-24 11:44:12 -04006613 return rc;
James Smart6d368e52011-05-24 11:44:12 -04006614 if (phba->sli4_hba.extents_in_use)
6615 return -EIO;
James Smartda0436e2009-05-22 14:51:39 -04006616
6617 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
6618 if (!rpi_hdr) {
6619 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6620 "0391 Error during rpi post operation\n");
6621 lpfc_sli4_remove_rpis(phba);
6622 rc = -ENODEV;
6623 }
6624
6625 return rc;
6626}
6627
6628/**
6629 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
6630 * @phba: pointer to lpfc hba data structure.
6631 *
6632 * This routine is invoked to allocate a single 4KB memory region to
6633 * support rpis and stores them in the phba. This single region
6634 * provides support for up to 64 rpis. The region is used globally
6635 * by the device.
6636 *
6637 * Returns:
6638 * A valid rpi hdr on success.
6639 * A NULL pointer on any failure.
6640 **/
6641struct lpfc_rpi_hdr *
6642lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
6643{
6644 uint16_t rpi_limit, curr_rpi_range;
6645 struct lpfc_dmabuf *dmabuf;
6646 struct lpfc_rpi_hdr *rpi_hdr;
6647
James Smart6d368e52011-05-24 11:44:12 -04006648 /*
6649 * If the SLI4 port supports extents, posting the rpi header isn't
6650 * required. Set the expected maximum count and let the actual value
6651 * get set when extents are fully allocated.
6652 */
6653 if (!phba->sli4_hba.rpi_hdrs_in_use)
6654 return NULL;
6655 if (phba->sli4_hba.extents_in_use)
6656 return NULL;
6657
6658 /* The limit on the logical index is just the max_rpi count. */
James Smart845d9e82017-05-15 15:20:38 -07006659 rpi_limit = phba->sli4_hba.max_cfg_param.max_rpi;
James Smartda0436e2009-05-22 14:51:39 -04006660
6661 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04006662 /*
6663 * Establish the starting RPI in this header block. The starting
6664 * rpi is normalized to a zero base because the physical rpi is
6665 * port based.
6666 */
James Smart97f2ecf2012-03-01 22:35:23 -05006667 curr_rpi_range = phba->sli4_hba.next_rpi;
James Smartda0436e2009-05-22 14:51:39 -04006668 spin_unlock_irq(&phba->hbalock);
6669
James Smart845d9e82017-05-15 15:20:38 -07006670 /* Reached full RPI range */
6671 if (curr_rpi_range == rpi_limit)
James Smart6d368e52011-05-24 11:44:12 -04006672 return NULL;
James Smart845d9e82017-05-15 15:20:38 -07006673
James Smartda0436e2009-05-22 14:51:39 -04006674 /*
6675 * First allocate the protocol header region for the port. The
6676 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
6677 */
6678 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
6679 if (!dmabuf)
6680 return NULL;
6681
Joe Perches1aee3832014-09-03 12:56:12 -04006682 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
6683 LPFC_HDR_TEMPLATE_SIZE,
6684 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04006685 if (!dmabuf->virt) {
6686 rpi_hdr = NULL;
6687 goto err_free_dmabuf;
6688 }
6689
James Smartda0436e2009-05-22 14:51:39 -04006690 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
6691 rpi_hdr = NULL;
6692 goto err_free_coherent;
6693 }
6694
6695 /* Save the rpi header data for cleanup later. */
6696 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
6697 if (!rpi_hdr)
6698 goto err_free_coherent;
6699
6700 rpi_hdr->dmabuf = dmabuf;
6701 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
6702 rpi_hdr->page_count = 1;
6703 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04006704
6705 /* The rpi_hdr stores the logical index only. */
6706 rpi_hdr->start_rpi = curr_rpi_range;
James Smart845d9e82017-05-15 15:20:38 -07006707 rpi_hdr->next_rpi = phba->sli4_hba.next_rpi + LPFC_RPI_HDR_COUNT;
James Smartda0436e2009-05-22 14:51:39 -04006708 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
6709
James Smartda0436e2009-05-22 14:51:39 -04006710 spin_unlock_irq(&phba->hbalock);
6711 return rpi_hdr;
6712
6713 err_free_coherent:
6714 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
6715 dmabuf->virt, dmabuf->phys);
6716 err_free_dmabuf:
6717 kfree(dmabuf);
6718 return NULL;
6719}
6720
6721/**
6722 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
6723 * @phba: pointer to lpfc hba data structure.
6724 *
6725 * This routine is invoked to remove all memory resources allocated
James Smart6d368e52011-05-24 11:44:12 -04006726 * to support rpis for SLI4 ports not supporting extents. This routine
6727 * presumes the caller has released all rpis consumed by fabric or port
6728 * logins and is prepared to have the header pages removed.
James Smartda0436e2009-05-22 14:51:39 -04006729 **/
6730void
6731lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
6732{
6733 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
6734
James Smart6d368e52011-05-24 11:44:12 -04006735 if (!phba->sli4_hba.rpi_hdrs_in_use)
6736 goto exit;
6737
James Smartda0436e2009-05-22 14:51:39 -04006738 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
6739 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
6740 list_del(&rpi_hdr->list);
6741 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
6742 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
6743 kfree(rpi_hdr->dmabuf);
6744 kfree(rpi_hdr);
6745 }
James Smart6d368e52011-05-24 11:44:12 -04006746 exit:
6747 /* There are no rpis available to the port now. */
6748 phba->sli4_hba.next_rpi = 0;
James Smartda0436e2009-05-22 14:51:39 -04006749}
6750
6751/**
James Smart3772a992009-05-22 14:50:54 -04006752 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
6753 * @pdev: pointer to pci device data structure.
6754 *
6755 * This routine is invoked to allocate the driver hba data structure for an
6756 * HBA device. If the allocation is successful, the phba reference to the
6757 * PCI device data structure is set.
6758 *
6759 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006760 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04006761 * NULL - error
6762 **/
6763static struct lpfc_hba *
6764lpfc_hba_alloc(struct pci_dev *pdev)
6765{
6766 struct lpfc_hba *phba;
6767
6768 /* Allocate memory for HBA structure */
6769 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
6770 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02006771 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04006772 return NULL;
6773 }
6774
6775 /* Set reference to PCI device in HBA structure */
6776 phba->pcidev = pdev;
6777
6778 /* Assign an unused board number */
6779 phba->brd_no = lpfc_get_instance();
6780 if (phba->brd_no < 0) {
6781 kfree(phba);
6782 return NULL;
6783 }
James Smart65791f12016-07-06 12:35:56 -07006784 phba->eratt_poll_interval = LPFC_ERATT_POLL_INTERVAL;
James Smart3772a992009-05-22 14:50:54 -04006785
James Smart4fede782010-01-26 23:08:55 -05006786 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04006787 INIT_LIST_HEAD(&phba->ct_ev_waiters);
6788
James Smart3772a992009-05-22 14:50:54 -04006789 return phba;
6790}
6791
6792/**
6793 * lpfc_hba_free - Free driver hba data structure with a device.
6794 * @phba: pointer to lpfc hba data structure.
6795 *
6796 * This routine is invoked to free the driver hba data structure with an
6797 * HBA device.
6798 **/
6799static void
6800lpfc_hba_free(struct lpfc_hba *phba)
6801{
6802 /* Release the driver assigned board number */
6803 idr_remove(&lpfc_hba_index, phba->brd_no);
6804
James Smart895427b2017-02-12 13:52:30 -08006805 /* Free memory allocated with sli3 rings */
6806 kfree(phba->sli.sli3_ring);
6807 phba->sli.sli3_ring = NULL;
James Smart2a76a282012-08-03 12:35:54 -04006808
James Smart3772a992009-05-22 14:50:54 -04006809 kfree(phba);
6810 return;
6811}
6812
6813/**
6814 * lpfc_create_shost - Create hba physical port with associated scsi host.
6815 * @phba: pointer to lpfc hba data structure.
6816 *
6817 * This routine is invoked to create HBA physical port and associate a SCSI
6818 * host with it.
6819 *
6820 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006821 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006822 * other values - error
6823 **/
6824static int
6825lpfc_create_shost(struct lpfc_hba *phba)
6826{
6827 struct lpfc_vport *vport;
6828 struct Scsi_Host *shost;
6829
6830 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05006831 phba->fc_edtov = FF_DEF_EDTOV;
6832 phba->fc_ratov = FF_DEF_RATOV;
6833 phba->fc_altov = FF_DEF_ALTOV;
6834 phba->fc_arbtov = FF_DEF_ARBTOV;
6835
James Smartd7c47992010-06-08 18:31:54 -04006836 atomic_set(&phba->sdev_cnt, 0);
James Smart2cee7802017-06-01 21:07:02 -07006837 atomic_set(&phba->fc4ScsiInputRequests, 0);
6838 atomic_set(&phba->fc4ScsiOutputRequests, 0);
6839 atomic_set(&phba->fc4ScsiControlRequests, 0);
6840 atomic_set(&phba->fc4ScsiIoCmpls, 0);
6841 atomic_set(&phba->fc4NvmeInputRequests, 0);
6842 atomic_set(&phba->fc4NvmeOutputRequests, 0);
6843 atomic_set(&phba->fc4NvmeControlRequests, 0);
6844 atomic_set(&phba->fc4NvmeIoCmpls, 0);
6845 atomic_set(&phba->fc4NvmeLsRequests, 0);
6846 atomic_set(&phba->fc4NvmeLsCmpls, 0);
James Smart3de2a652007-08-02 11:09:59 -04006847 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05006848 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04006849 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05006850
6851 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05006852 phba->pport = vport;
James Smart2ea259e2017-02-12 13:52:27 -08006853
James Smartf358dd02017-02-12 13:52:34 -08006854 if (phba->nvmet_support) {
6855 /* Only 1 vport (pport) will support NVME target */
6856 if (phba->txrdy_payload_pool == NULL) {
Romain Perier771db5c2017-07-06 10:13:05 +02006857 phba->txrdy_payload_pool = dma_pool_create(
6858 "txrdy_pool", &phba->pcidev->dev,
James Smartf358dd02017-02-12 13:52:34 -08006859 TXRDY_PAYLOAD_LEN, 16, 0);
6860 if (phba->txrdy_payload_pool) {
6861 phba->targetport = NULL;
6862 phba->cfg_enable_fc4_type = LPFC_ENABLE_NVME;
6863 lpfc_printf_log(phba, KERN_INFO,
6864 LOG_INIT | LOG_NVME_DISC,
6865 "6076 NVME Target Found\n");
6866 }
6867 }
6868 }
6869
James Smart858c9f62007-06-17 19:56:39 -05006870 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04006871 /* Put reference to SCSI host to driver's device private data */
6872 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05006873
James Smart4258e982015-12-16 18:11:58 -05006874 /*
6875 * At this point we are fully registered with PSA. In addition,
6876 * any initial discovery should be completed.
6877 */
6878 vport->load_flag |= FC_ALLOW_FDMI;
James Smart8663cbb2016-03-31 14:12:33 -07006879 if (phba->cfg_enable_SmartSAN ||
6880 (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
James Smart4258e982015-12-16 18:11:58 -05006881
6882 /* Setup appropriate attribute masks */
6883 vport->fdmi_hba_mask = LPFC_FDMI2_HBA_ATTR;
James Smart8663cbb2016-03-31 14:12:33 -07006884 if (phba->cfg_enable_SmartSAN)
James Smart4258e982015-12-16 18:11:58 -05006885 vport->fdmi_port_mask = LPFC_FDMI2_SMART_ATTR;
6886 else
6887 vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
6888 }
James Smart3772a992009-05-22 14:50:54 -04006889 return 0;
6890}
dea31012005-04-17 16:05:31 -05006891
James Smart3772a992009-05-22 14:50:54 -04006892/**
6893 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
6894 * @phba: pointer to lpfc hba data structure.
6895 *
6896 * This routine is invoked to destroy HBA physical port and the associated
6897 * SCSI host.
6898 **/
6899static void
6900lpfc_destroy_shost(struct lpfc_hba *phba)
6901{
6902 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04006903
James Smart3772a992009-05-22 14:50:54 -04006904 /* Destroy physical port that associated with the SCSI host */
6905 destroy_port(vport);
6906
6907 return;
6908}
6909
6910/**
6911 * lpfc_setup_bg - Setup Block guard structures and debug areas.
6912 * @phba: pointer to lpfc hba data structure.
6913 * @shost: the shost to be used to detect Block guard settings.
6914 *
6915 * This routine sets up the local Block guard protocol settings for @shost.
6916 * This routine also allocates memory for debugging bg buffers.
6917 **/
6918static void
6919lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
6920{
James Smartbbeb79b2012-06-12 13:54:27 -04006921 uint32_t old_mask;
6922 uint32_t old_guard;
6923
James Smart3772a992009-05-22 14:50:54 -04006924 int pagecnt = 10;
James Smartb3b98b72016-10-13 15:06:06 -07006925 if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
James Smart3772a992009-05-22 14:50:54 -04006926 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6927 "1478 Registering BlockGuard with the "
6928 "SCSI layer\n");
James Smartbbeb79b2012-06-12 13:54:27 -04006929
James Smartb3b98b72016-10-13 15:06:06 -07006930 old_mask = phba->cfg_prot_mask;
6931 old_guard = phba->cfg_prot_guard;
James Smartbbeb79b2012-06-12 13:54:27 -04006932
6933 /* Only allow supported values */
James Smartb3b98b72016-10-13 15:06:06 -07006934 phba->cfg_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
James Smartbbeb79b2012-06-12 13:54:27 -04006935 SHOST_DIX_TYPE0_PROTECTION |
6936 SHOST_DIX_TYPE1_PROTECTION);
James Smartb3b98b72016-10-13 15:06:06 -07006937 phba->cfg_prot_guard &= (SHOST_DIX_GUARD_IP |
6938 SHOST_DIX_GUARD_CRC);
James Smartbbeb79b2012-06-12 13:54:27 -04006939
6940 /* DIF Type 1 protection for profiles AST1/C1 is end to end */
James Smartb3b98b72016-10-13 15:06:06 -07006941 if (phba->cfg_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
6942 phba->cfg_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
James Smartbbeb79b2012-06-12 13:54:27 -04006943
James Smartb3b98b72016-10-13 15:06:06 -07006944 if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
6945 if ((old_mask != phba->cfg_prot_mask) ||
6946 (old_guard != phba->cfg_prot_guard))
James Smartbbeb79b2012-06-12 13:54:27 -04006947 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6948 "1475 Registering BlockGuard with the "
6949 "SCSI layer: mask %d guard %d\n",
James Smartb3b98b72016-10-13 15:06:06 -07006950 phba->cfg_prot_mask,
6951 phba->cfg_prot_guard);
James Smartbbeb79b2012-06-12 13:54:27 -04006952
James Smartb3b98b72016-10-13 15:06:06 -07006953 scsi_host_set_prot(shost, phba->cfg_prot_mask);
6954 scsi_host_set_guard(shost, phba->cfg_prot_guard);
James Smartbbeb79b2012-06-12 13:54:27 -04006955 } else
6956 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6957 "1479 Not Registering BlockGuard with the SCSI "
6958 "layer, Bad protection parameters: %d %d\n",
6959 old_mask, old_guard);
James Smart98c9ea52007-10-27 13:37:33 -04006960 }
James Smartbbeb79b2012-06-12 13:54:27 -04006961
James Smart81301a92008-12-04 22:39:46 -05006962 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05006963 while (pagecnt) {
6964 spin_lock_init(&_dump_buf_lock);
6965 _dump_buf_data =
6966 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
6967 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04006968 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6969 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04006970 "_dump_buf_data at 0x%p\n",
6971 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05006972 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04006973 memset(_dump_buf_data, 0,
6974 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05006975 break;
James Smart3772a992009-05-22 14:50:54 -04006976 } else
James Smart81301a92008-12-04 22:39:46 -05006977 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05006978 }
James Smart81301a92008-12-04 22:39:46 -05006979 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04006980 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6981 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04006982 "memory for hexdump\n");
6983 } else
James Smart6a9c52c2009-10-02 15:16:51 -04006984 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6985 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05006986 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05006987 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05006988 while (pagecnt) {
6989 _dump_buf_dif =
6990 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
6991 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04006992 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6993 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04006994 "_dump_buf_dif at 0x%p\n",
6995 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05006996 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04006997 memset(_dump_buf_dif, 0,
6998 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05006999 break;
James Smart3772a992009-05-22 14:50:54 -04007000 } else
James Smart81301a92008-12-04 22:39:46 -05007001 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05007002 }
James Smart81301a92008-12-04 22:39:46 -05007003 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04007004 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7005 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04007006 "memory for hexdump\n");
7007 } else
James Smart6a9c52c2009-10-02 15:16:51 -04007008 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7009 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04007010 _dump_buf_dif);
7011}
7012
7013/**
7014 * lpfc_post_init_setup - Perform necessary device post initialization setup.
7015 * @phba: pointer to lpfc hba data structure.
7016 *
7017 * This routine is invoked to perform all the necessary post initialization
7018 * setup for the device.
7019 **/
7020static void
7021lpfc_post_init_setup(struct lpfc_hba *phba)
7022{
7023 struct Scsi_Host *shost;
7024 struct lpfc_adapter_event_header adapter_event;
7025
7026 /* Get the default values for Model Name and Description */
7027 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
7028
7029 /*
7030 * hba setup may have changed the hba_queue_depth so we need to
7031 * adjust the value of can_queue.
7032 */
7033 shost = pci_get_drvdata(phba->pcidev);
7034 shost->can_queue = phba->cfg_hba_queue_depth - 10;
7035 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
7036 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05007037
7038 lpfc_host_attrib_init(shost);
7039
James Smart2e0fef82007-06-17 19:56:36 -05007040 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7041 spin_lock_irq(shost->host_lock);
7042 lpfc_poll_start_timer(phba);
7043 spin_unlock_irq(shost->host_lock);
7044 }
James Smart8f6d98d2006-08-01 07:34:00 -04007045
James Smart93996272008-08-24 21:50:30 -04007046 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7047 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04007048 /* Send board arrival event to upper layer */
7049 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
7050 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
7051 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04007052 sizeof(adapter_event),
7053 (char *) &adapter_event,
7054 LPFC_NL_VENDOR_ID);
7055 return;
7056}
7057
7058/**
7059 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
7060 * @phba: pointer to lpfc hba data structure.
7061 *
7062 * This routine is invoked to set up the PCI device memory space for device
7063 * with SLI-3 interface spec.
7064 *
7065 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007066 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007067 * other values - error
7068 **/
7069static int
7070lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
7071{
7072 struct pci_dev *pdev;
7073 unsigned long bar0map_len, bar2map_len;
7074 int i, hbq_count;
7075 void *ptr;
7076 int error = -ENODEV;
7077
7078 /* Obtain PCI device reference */
7079 if (!phba->pcidev)
7080 return error;
7081 else
7082 pdev = phba->pcidev;
7083
7084 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05007085 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
7086 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
7087 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
7088 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smart3772a992009-05-22 14:50:54 -04007089 return error;
Michael Reed8e685972009-09-18 12:02:05 -05007090 }
7091 }
James Smart3772a992009-05-22 14:50:54 -04007092
7093 /* Get the bus address of Bar0 and Bar2 and the number of bytes
7094 * required by each mapping.
7095 */
7096 phba->pci_bar0_map = pci_resource_start(pdev, 0);
7097 bar0map_len = pci_resource_len(pdev, 0);
7098
7099 phba->pci_bar2_map = pci_resource_start(pdev, 2);
7100 bar2map_len = pci_resource_len(pdev, 2);
7101
7102 /* Map HBA SLIM to a kernel virtual address. */
7103 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
7104 if (!phba->slim_memmap_p) {
7105 dev_printk(KERN_ERR, &pdev->dev,
7106 "ioremap failed for SLIM memory.\n");
7107 goto out;
7108 }
7109
7110 /* Map HBA Control Registers to a kernel virtual address. */
7111 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
7112 if (!phba->ctrl_regs_memmap_p) {
7113 dev_printk(KERN_ERR, &pdev->dev,
7114 "ioremap failed for HBA control registers.\n");
7115 goto out_iounmap_slim;
7116 }
7117
7118 /* Allocate memory for SLI-2 structures */
Joe Perches1aee3832014-09-03 12:56:12 -04007119 phba->slim2p.virt = dma_zalloc_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7120 &phba->slim2p.phys, GFP_KERNEL);
James Smart3772a992009-05-22 14:50:54 -04007121 if (!phba->slim2p.virt)
7122 goto out_iounmap;
7123
James Smart3772a992009-05-22 14:50:54 -04007124 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04007125 phba->mbox_ext = (phba->slim2p.virt +
7126 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04007127 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
7128 phba->IOCBs = (phba->slim2p.virt +
7129 offsetof(struct lpfc_sli2_slim, IOCBs));
7130
7131 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
7132 lpfc_sli_hbq_size(),
7133 &phba->hbqslimp.phys,
7134 GFP_KERNEL);
7135 if (!phba->hbqslimp.virt)
7136 goto out_free_slim;
7137
7138 hbq_count = lpfc_sli_hbq_count();
7139 ptr = phba->hbqslimp.virt;
7140 for (i = 0; i < hbq_count; ++i) {
7141 phba->hbqs[i].hbq_virt = ptr;
7142 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
7143 ptr += (lpfc_hbq_defs[i]->entry_count *
7144 sizeof(struct lpfc_hbq_entry));
7145 }
7146 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
7147 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
7148
7149 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
7150
James Smart3772a992009-05-22 14:50:54 -04007151 phba->MBslimaddr = phba->slim_memmap_p;
7152 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
7153 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
7154 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
7155 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04007156
dea31012005-04-17 16:05:31 -05007157 return 0;
7158
dea31012005-04-17 16:05:31 -05007159out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04007160 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7161 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05007162out_iounmap:
7163 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05007164out_iounmap_slim:
dea31012005-04-17 16:05:31 -05007165 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05007166out:
7167 return error;
7168}
7169
James Smarte59058c2008-08-24 21:49:00 -04007170/**
James Smart3772a992009-05-22 14:50:54 -04007171 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
7172 * @phba: pointer to lpfc hba data structure.
7173 *
7174 * This routine is invoked to unset the PCI device memory space for device
7175 * with SLI-3 interface spec.
7176 **/
7177static void
7178lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
7179{
7180 struct pci_dev *pdev;
7181
7182 /* Obtain PCI device reference */
7183 if (!phba->pcidev)
7184 return;
7185 else
7186 pdev = phba->pcidev;
7187
7188 /* Free coherent DMA memory allocated */
7189 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
7190 phba->hbqslimp.virt, phba->hbqslimp.phys);
7191 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7192 phba->slim2p.virt, phba->slim2p.phys);
7193
7194 /* I/O memory unmap */
7195 iounmap(phba->ctrl_regs_memmap_p);
7196 iounmap(phba->slim_memmap_p);
7197
7198 return;
7199}
7200
7201/**
James Smartda0436e2009-05-22 14:51:39 -04007202 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
7203 * @phba: pointer to lpfc hba data structure.
7204 *
7205 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
7206 * done and check status.
7207 *
7208 * Return 0 if successful, otherwise -ENODEV.
7209 **/
7210int
7211lpfc_sli4_post_status_check(struct lpfc_hba *phba)
7212{
James Smart2fcee4b2010-12-15 17:57:46 -05007213 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
7214 struct lpfc_register reg_data;
7215 int i, port_error = 0;
7216 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04007217
James Smart9940b972011-03-11 16:06:12 -05007218 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
7219 memset(&reg_data, 0, sizeof(reg_data));
James Smart2fcee4b2010-12-15 17:57:46 -05007220 if (!phba->sli4_hba.PSMPHRregaddr)
James Smartda0436e2009-05-22 14:51:39 -04007221 return -ENODEV;
7222
James Smartda0436e2009-05-22 14:51:39 -04007223 /* Wait up to 30 seconds for the SLI Port POST done and ready */
7224 for (i = 0; i < 3000; i++) {
James Smart9940b972011-03-11 16:06:12 -05007225 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
7226 &portsmphr_reg.word0) ||
7227 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007228 /* Port has a fatal POST error, break out */
James Smartda0436e2009-05-22 14:51:39 -04007229 port_error = -ENODEV;
7230 break;
7231 }
James Smart2fcee4b2010-12-15 17:57:46 -05007232 if (LPFC_POST_STAGE_PORT_READY ==
7233 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
James Smartda0436e2009-05-22 14:51:39 -04007234 break;
James Smartda0436e2009-05-22 14:51:39 -04007235 msleep(10);
7236 }
7237
James Smart2fcee4b2010-12-15 17:57:46 -05007238 /*
7239 * If there was a port error during POST, then don't proceed with
7240 * other register reads as the data may not be valid. Just exit.
7241 */
7242 if (port_error) {
James Smartda0436e2009-05-22 14:51:39 -04007243 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05007244 "1408 Port Failed POST - portsmphr=0x%x, "
7245 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
7246 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
7247 portsmphr_reg.word0,
7248 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
7249 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
7250 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
7251 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
7252 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
7253 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
7254 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
7255 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
7256 } else {
James Smart28baac72010-02-12 14:42:03 -05007257 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05007258 "2534 Device Info: SLIFamily=0x%x, "
7259 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
7260 "SLIHint_2=0x%x, FT=0x%x\n",
James Smart28baac72010-02-12 14:42:03 -05007261 bf_get(lpfc_sli_intf_sli_family,
7262 &phba->sli4_hba.sli_intf),
7263 bf_get(lpfc_sli_intf_slirev,
7264 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05007265 bf_get(lpfc_sli_intf_if_type,
James Smart28baac72010-02-12 14:42:03 -05007266 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05007267 bf_get(lpfc_sli_intf_sli_hint1,
7268 &phba->sli4_hba.sli_intf),
7269 bf_get(lpfc_sli_intf_sli_hint2,
7270 &phba->sli4_hba.sli_intf),
7271 bf_get(lpfc_sli_intf_func_type,
James Smart28baac72010-02-12 14:42:03 -05007272 &phba->sli4_hba.sli_intf));
James Smart2fcee4b2010-12-15 17:57:46 -05007273 /*
7274 * Check for other Port errors during the initialization
7275 * process. Fail the load if the port did not come up
7276 * correctly.
7277 */
7278 if_type = bf_get(lpfc_sli_intf_if_type,
7279 &phba->sli4_hba.sli_intf);
7280 switch (if_type) {
7281 case LPFC_SLI_INTF_IF_TYPE_0:
7282 phba->sli4_hba.ue_mask_lo =
7283 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
7284 phba->sli4_hba.ue_mask_hi =
7285 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
7286 uerrlo_reg.word0 =
7287 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
7288 uerrhi_reg.word0 =
7289 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
7290 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
7291 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
7292 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7293 "1422 Unrecoverable Error "
7294 "Detected during POST "
7295 "uerr_lo_reg=0x%x, "
7296 "uerr_hi_reg=0x%x, "
7297 "ue_mask_lo_reg=0x%x, "
7298 "ue_mask_hi_reg=0x%x\n",
7299 uerrlo_reg.word0,
7300 uerrhi_reg.word0,
7301 phba->sli4_hba.ue_mask_lo,
7302 phba->sli4_hba.ue_mask_hi);
7303 port_error = -ENODEV;
7304 }
7305 break;
7306 case LPFC_SLI_INTF_IF_TYPE_2:
7307 /* Final checks. The port status should be clean. */
James Smart9940b972011-03-11 16:06:12 -05007308 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
7309 &reg_data.word0) ||
James Smart05580562011-05-24 11:40:48 -04007310 (bf_get(lpfc_sliport_status_err, &reg_data) &&
7311 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007312 phba->work_status[0] =
7313 readl(phba->sli4_hba.u.if_type2.
7314 ERR1regaddr);
7315 phba->work_status[1] =
7316 readl(phba->sli4_hba.u.if_type2.
7317 ERR2regaddr);
7318 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05007319 "2888 Unrecoverable port error "
7320 "following POST: port status reg "
7321 "0x%x, port_smphr reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05007322 "error 1=0x%x, error 2=0x%x\n",
7323 reg_data.word0,
7324 portsmphr_reg.word0,
7325 phba->work_status[0],
7326 phba->work_status[1]);
7327 port_error = -ENODEV;
7328 }
7329 break;
7330 case LPFC_SLI_INTF_IF_TYPE_1:
7331 default:
7332 break;
7333 }
James Smart28baac72010-02-12 14:42:03 -05007334 }
James Smartda0436e2009-05-22 14:51:39 -04007335 return port_error;
7336}
7337
7338/**
7339 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
7340 * @phba: pointer to lpfc hba data structure.
James Smart2fcee4b2010-12-15 17:57:46 -05007341 * @if_type: The SLI4 interface type getting configured.
James Smartda0436e2009-05-22 14:51:39 -04007342 *
7343 * This routine is invoked to set up SLI4 BAR0 PCI config space register
7344 * memory map.
7345 **/
7346static void
James Smart2fcee4b2010-12-15 17:57:46 -05007347lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04007348{
James Smart2fcee4b2010-12-15 17:57:46 -05007349 switch (if_type) {
7350 case LPFC_SLI_INTF_IF_TYPE_0:
7351 phba->sli4_hba.u.if_type0.UERRLOregaddr =
7352 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
7353 phba->sli4_hba.u.if_type0.UERRHIregaddr =
7354 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
7355 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
7356 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
7357 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
7358 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
7359 phba->sli4_hba.SLIINTFregaddr =
7360 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
7361 break;
7362 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart0cf07f842017-06-01 21:07:10 -07007363 phba->sli4_hba.u.if_type2.EQDregaddr =
7364 phba->sli4_hba.conf_regs_memmap_p +
7365 LPFC_CTL_PORT_EQ_DELAY_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05007366 phba->sli4_hba.u.if_type2.ERR1regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04007367 phba->sli4_hba.conf_regs_memmap_p +
7368 LPFC_CTL_PORT_ER1_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05007369 phba->sli4_hba.u.if_type2.ERR2regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04007370 phba->sli4_hba.conf_regs_memmap_p +
7371 LPFC_CTL_PORT_ER2_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05007372 phba->sli4_hba.u.if_type2.CTRLregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04007373 phba->sli4_hba.conf_regs_memmap_p +
7374 LPFC_CTL_PORT_CTL_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05007375 phba->sli4_hba.u.if_type2.STATUSregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04007376 phba->sli4_hba.conf_regs_memmap_p +
7377 LPFC_CTL_PORT_STA_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05007378 phba->sli4_hba.SLIINTFregaddr =
7379 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
7380 phba->sli4_hba.PSMPHRregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04007381 phba->sli4_hba.conf_regs_memmap_p +
7382 LPFC_CTL_PORT_SEM_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05007383 phba->sli4_hba.RQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05007384 phba->sli4_hba.conf_regs_memmap_p +
7385 LPFC_ULP0_RQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05007386 phba->sli4_hba.WQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05007387 phba->sli4_hba.conf_regs_memmap_p +
7388 LPFC_ULP0_WQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05007389 phba->sli4_hba.EQCQDBregaddr =
7390 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
7391 phba->sli4_hba.MQDBregaddr =
7392 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
7393 phba->sli4_hba.BMBXregaddr =
7394 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
7395 break;
7396 case LPFC_SLI_INTF_IF_TYPE_1:
7397 default:
7398 dev_printk(KERN_ERR, &phba->pcidev->dev,
7399 "FATAL - unsupported SLI4 interface type - %d\n",
7400 if_type);
7401 break;
7402 }
James Smartda0436e2009-05-22 14:51:39 -04007403}
7404
7405/**
7406 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
7407 * @phba: pointer to lpfc hba data structure.
7408 *
7409 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
7410 * memory map.
7411 **/
7412static void
7413lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
7414{
James Smart2fcee4b2010-12-15 17:57:46 -05007415 phba->sli4_hba.PSMPHRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
7416 LPFC_SLIPORT_IF0_SMPHR;
James Smartda0436e2009-05-22 14:51:39 -04007417 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05007418 LPFC_HST_ISR0;
James Smartda0436e2009-05-22 14:51:39 -04007419 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05007420 LPFC_HST_IMR0;
James Smartda0436e2009-05-22 14:51:39 -04007421 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05007422 LPFC_HST_ISCR0;
James Smartda0436e2009-05-22 14:51:39 -04007423}
7424
7425/**
7426 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
7427 * @phba: pointer to lpfc hba data structure.
7428 * @vf: virtual function number
7429 *
7430 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
7431 * based on the given viftual function number, @vf.
7432 *
7433 * Return 0 if successful, otherwise -ENODEV.
7434 **/
7435static int
7436lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
7437{
7438 if (vf > LPFC_VIR_FUNC_MAX)
7439 return -ENODEV;
7440
7441 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05007442 vf * LPFC_VFR_PAGE_SIZE +
7443 LPFC_ULP0_RQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04007444 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05007445 vf * LPFC_VFR_PAGE_SIZE +
7446 LPFC_ULP0_WQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04007447 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7448 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
7449 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7450 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
7451 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7452 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
7453 return 0;
7454}
7455
7456/**
7457 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
7458 * @phba: pointer to lpfc hba data structure.
7459 *
7460 * This routine is invoked to create the bootstrap mailbox
7461 * region consistent with the SLI-4 interface spec. This
7462 * routine allocates all memory necessary to communicate
7463 * mailbox commands to the port and sets up all alignment
7464 * needs. No locks are expected to be held when calling
7465 * this routine.
7466 *
7467 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007468 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04007469 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04007470 **/
7471static int
7472lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
7473{
7474 uint32_t bmbx_size;
7475 struct lpfc_dmabuf *dmabuf;
7476 struct dma_address *dma_address;
7477 uint32_t pa_addr;
7478 uint64_t phys_addr;
7479
7480 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
7481 if (!dmabuf)
7482 return -ENOMEM;
7483
7484 /*
7485 * The bootstrap mailbox region is comprised of 2 parts
7486 * plus an alignment restriction of 16 bytes.
7487 */
7488 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
Joe Perches1aee3832014-09-03 12:56:12 -04007489 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, bmbx_size,
7490 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04007491 if (!dmabuf->virt) {
7492 kfree(dmabuf);
7493 return -ENOMEM;
7494 }
James Smartda0436e2009-05-22 14:51:39 -04007495
7496 /*
7497 * Initialize the bootstrap mailbox pointers now so that the register
7498 * operations are simple later. The mailbox dma address is required
7499 * to be 16-byte aligned. Also align the virtual memory as each
7500 * maibox is copied into the bmbx mailbox region before issuing the
7501 * command to the port.
7502 */
7503 phba->sli4_hba.bmbx.dmabuf = dmabuf;
7504 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
7505
7506 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
7507 LPFC_ALIGN_16_BYTE);
7508 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
7509 LPFC_ALIGN_16_BYTE);
7510
7511 /*
7512 * Set the high and low physical addresses now. The SLI4 alignment
7513 * requirement is 16 bytes and the mailbox is posted to the port
7514 * as two 30-bit addresses. The other data is a bit marking whether
7515 * the 30-bit address is the high or low address.
7516 * Upcast bmbx aphys to 64bits so shift instruction compiles
7517 * clean on 32 bit machines.
7518 */
7519 dma_address = &phba->sli4_hba.bmbx.dma_address;
7520 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
7521 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
7522 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
7523 LPFC_BMBX_BIT1_ADDR_HI);
7524
7525 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
7526 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
7527 LPFC_BMBX_BIT1_ADDR_LO);
7528 return 0;
7529}
7530
7531/**
7532 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
7533 * @phba: pointer to lpfc hba data structure.
7534 *
7535 * This routine is invoked to teardown the bootstrap mailbox
7536 * region and release all host resources. This routine requires
7537 * the caller to ensure all mailbox commands recovered, no
7538 * additional mailbox comands are sent, and interrupts are disabled
7539 * before calling this routine.
7540 *
7541 **/
7542static void
7543lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
7544{
7545 dma_free_coherent(&phba->pcidev->dev,
7546 phba->sli4_hba.bmbx.bmbx_size,
7547 phba->sli4_hba.bmbx.dmabuf->virt,
7548 phba->sli4_hba.bmbx.dmabuf->phys);
7549
7550 kfree(phba->sli4_hba.bmbx.dmabuf);
7551 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
7552}
7553
7554/**
7555 * lpfc_sli4_read_config - Get the config parameters.
7556 * @phba: pointer to lpfc hba data structure.
7557 *
7558 * This routine is invoked to read the configuration parameters from the HBA.
7559 * The configuration parameters are used to set the base and maximum values
7560 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
7561 * allocation for the port.
7562 *
7563 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007564 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007565 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007566 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007567 **/
James Smartff78d8f2011-12-13 13:21:35 -05007568int
James Smartda0436e2009-05-22 14:51:39 -04007569lpfc_sli4_read_config(struct lpfc_hba *phba)
7570{
7571 LPFC_MBOXQ_t *pmb;
7572 struct lpfc_mbx_read_config *rd_config;
James Smart912e3ac2011-05-24 11:42:11 -04007573 union lpfc_sli4_cfg_shdr *shdr;
7574 uint32_t shdr_status, shdr_add_status;
7575 struct lpfc_mbx_get_func_cfg *get_func_cfg;
7576 struct lpfc_rsrc_desc_fcfcoe *desc;
James Smart8aa134a2012-08-14 14:25:29 -04007577 char *pdesc_0;
James Smartc6918162016-10-13 15:06:16 -07007578 uint16_t forced_link_speed;
7579 uint32_t if_type;
James Smart8aa134a2012-08-14 14:25:29 -04007580 int length, i, rc = 0, rc2;
James Smartda0436e2009-05-22 14:51:39 -04007581
7582 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7583 if (!pmb) {
7584 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7585 "2011 Unable to allocate memory for issuing "
7586 "SLI_CONFIG_SPECIAL mailbox command\n");
7587 return -ENOMEM;
7588 }
7589
7590 lpfc_read_config(phba, pmb);
7591
7592 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
7593 if (rc != MBX_SUCCESS) {
7594 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7595 "2012 Mailbox failed , mbxCmd x%x "
7596 "READ_CONFIG, mbxStatus x%x\n",
7597 bf_get(lpfc_mqe_command, &pmb->u.mqe),
7598 bf_get(lpfc_mqe_status, &pmb->u.mqe));
7599 rc = -EIO;
7600 } else {
7601 rd_config = &pmb->u.mqe.un.rd_config;
James Smartff78d8f2011-12-13 13:21:35 -05007602 if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
7603 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
7604 phba->sli4_hba.lnk_info.lnk_tp =
7605 bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
7606 phba->sli4_hba.lnk_info.lnk_no =
7607 bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
7608 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7609 "3081 lnk_type:%d, lnk_numb:%d\n",
7610 phba->sli4_hba.lnk_info.lnk_tp,
7611 phba->sli4_hba.lnk_info.lnk_no);
7612 } else
7613 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
7614 "3082 Mailbox (x%x) returned ldv:x0\n",
7615 bf_get(lpfc_mqe_command, &pmb->u.mqe));
James Smart6d368e52011-05-24 11:44:12 -04007616 phba->sli4_hba.extents_in_use =
7617 bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04007618 phba->sli4_hba.max_cfg_param.max_xri =
7619 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
7620 phba->sli4_hba.max_cfg_param.xri_base =
7621 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
7622 phba->sli4_hba.max_cfg_param.max_vpi =
7623 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
7624 phba->sli4_hba.max_cfg_param.vpi_base =
7625 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
7626 phba->sli4_hba.max_cfg_param.max_rpi =
7627 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
7628 phba->sli4_hba.max_cfg_param.rpi_base =
7629 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
7630 phba->sli4_hba.max_cfg_param.max_vfi =
7631 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
7632 phba->sli4_hba.max_cfg_param.vfi_base =
7633 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
7634 phba->sli4_hba.max_cfg_param.max_fcfi =
7635 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04007636 phba->sli4_hba.max_cfg_param.max_eq =
7637 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
7638 phba->sli4_hba.max_cfg_param.max_rq =
7639 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
7640 phba->sli4_hba.max_cfg_param.max_wq =
7641 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
7642 phba->sli4_hba.max_cfg_param.max_cq =
7643 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
7644 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
7645 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
7646 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
7647 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
James Smart5ffc2662009-11-18 15:39:44 -05007648 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
7649 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04007650 phba->max_vports = phba->max_vpi;
7651 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart6d368e52011-05-24 11:44:12 -04007652 "2003 cfg params Extents? %d "
7653 "XRI(B:%d M:%d), "
James Smartda0436e2009-05-22 14:51:39 -04007654 "VPI(B:%d M:%d) "
7655 "VFI(B:%d M:%d) "
7656 "RPI(B:%d M:%d) "
James Smart2ea259e2017-02-12 13:52:27 -08007657 "FCFI:%d EQ:%d CQ:%d WQ:%d RQ:%d\n",
James Smart6d368e52011-05-24 11:44:12 -04007658 phba->sli4_hba.extents_in_use,
James Smartda0436e2009-05-22 14:51:39 -04007659 phba->sli4_hba.max_cfg_param.xri_base,
7660 phba->sli4_hba.max_cfg_param.max_xri,
7661 phba->sli4_hba.max_cfg_param.vpi_base,
7662 phba->sli4_hba.max_cfg_param.max_vpi,
7663 phba->sli4_hba.max_cfg_param.vfi_base,
7664 phba->sli4_hba.max_cfg_param.max_vfi,
7665 phba->sli4_hba.max_cfg_param.rpi_base,
7666 phba->sli4_hba.max_cfg_param.max_rpi,
James Smart2ea259e2017-02-12 13:52:27 -08007667 phba->sli4_hba.max_cfg_param.max_fcfi,
7668 phba->sli4_hba.max_cfg_param.max_eq,
7669 phba->sli4_hba.max_cfg_param.max_cq,
7670 phba->sli4_hba.max_cfg_param.max_wq,
7671 phba->sli4_hba.max_cfg_param.max_rq);
7672
James Smartda0436e2009-05-22 14:51:39 -04007673 }
James Smart912e3ac2011-05-24 11:42:11 -04007674
7675 if (rc)
7676 goto read_cfg_out;
James Smartda0436e2009-05-22 14:51:39 -04007677
James Smartc6918162016-10-13 15:06:16 -07007678 /* Update link speed if forced link speed is supported */
7679 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7680 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
7681 forced_link_speed =
7682 bf_get(lpfc_mbx_rd_conf_link_speed, rd_config);
7683 if (forced_link_speed) {
7684 phba->hba_flag |= HBA_FORCED_LINK_SPEED;
7685
7686 switch (forced_link_speed) {
7687 case LINK_SPEED_1G:
7688 phba->cfg_link_speed =
7689 LPFC_USER_LINK_SPEED_1G;
7690 break;
7691 case LINK_SPEED_2G:
7692 phba->cfg_link_speed =
7693 LPFC_USER_LINK_SPEED_2G;
7694 break;
7695 case LINK_SPEED_4G:
7696 phba->cfg_link_speed =
7697 LPFC_USER_LINK_SPEED_4G;
7698 break;
7699 case LINK_SPEED_8G:
7700 phba->cfg_link_speed =
7701 LPFC_USER_LINK_SPEED_8G;
7702 break;
7703 case LINK_SPEED_10G:
7704 phba->cfg_link_speed =
7705 LPFC_USER_LINK_SPEED_10G;
7706 break;
7707 case LINK_SPEED_16G:
7708 phba->cfg_link_speed =
7709 LPFC_USER_LINK_SPEED_16G;
7710 break;
7711 case LINK_SPEED_32G:
7712 phba->cfg_link_speed =
7713 LPFC_USER_LINK_SPEED_32G;
7714 break;
7715 case 0xffff:
7716 phba->cfg_link_speed =
7717 LPFC_USER_LINK_SPEED_AUTO;
7718 break;
7719 default:
7720 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7721 "0047 Unrecognized link "
7722 "speed : %d\n",
7723 forced_link_speed);
7724 phba->cfg_link_speed =
7725 LPFC_USER_LINK_SPEED_AUTO;
7726 }
7727 }
7728 }
7729
James Smartda0436e2009-05-22 14:51:39 -04007730 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -04007731 length = phba->sli4_hba.max_cfg_param.max_xri -
7732 lpfc_sli4_get_els_iocb_cnt(phba);
7733 if (phba->cfg_hba_queue_depth > length) {
7734 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7735 "3361 HBA queue depth changed from %d to %d\n",
7736 phba->cfg_hba_queue_depth, length);
7737 phba->cfg_hba_queue_depth = length;
7738 }
James Smart912e3ac2011-05-24 11:42:11 -04007739
7740 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
7741 LPFC_SLI_INTF_IF_TYPE_2)
7742 goto read_cfg_out;
7743
7744 /* get the pf# and vf# for SLI4 if_type 2 port */
7745 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
7746 sizeof(struct lpfc_sli4_cfg_mhdr));
7747 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
7748 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
7749 length, LPFC_SLI4_MBX_EMBED);
7750
James Smart8aa134a2012-08-14 14:25:29 -04007751 rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart912e3ac2011-05-24 11:42:11 -04007752 shdr = (union lpfc_sli4_cfg_shdr *)
7753 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
7754 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7755 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart8aa134a2012-08-14 14:25:29 -04007756 if (rc2 || shdr_status || shdr_add_status) {
James Smart912e3ac2011-05-24 11:42:11 -04007757 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7758 "3026 Mailbox failed , mbxCmd x%x "
7759 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
7760 bf_get(lpfc_mqe_command, &pmb->u.mqe),
7761 bf_get(lpfc_mqe_status, &pmb->u.mqe));
James Smart912e3ac2011-05-24 11:42:11 -04007762 goto read_cfg_out;
7763 }
7764
7765 /* search for fc_fcoe resrouce descriptor */
7766 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
James Smart912e3ac2011-05-24 11:42:11 -04007767
James Smart8aa134a2012-08-14 14:25:29 -04007768 pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
7769 desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
7770 length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
7771 if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
7772 length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
7773 else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
7774 goto read_cfg_out;
7775
James Smart912e3ac2011-05-24 11:42:11 -04007776 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
James Smart8aa134a2012-08-14 14:25:29 -04007777 desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
James Smart912e3ac2011-05-24 11:42:11 -04007778 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
James Smart8aa134a2012-08-14 14:25:29 -04007779 bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
James Smart912e3ac2011-05-24 11:42:11 -04007780 phba->sli4_hba.iov.pf_number =
7781 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
7782 phba->sli4_hba.iov.vf_number =
7783 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
7784 break;
7785 }
7786 }
7787
7788 if (i < LPFC_RSRC_DESC_MAX_NUM)
7789 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7790 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
7791 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
7792 phba->sli4_hba.iov.vf_number);
James Smart8aa134a2012-08-14 14:25:29 -04007793 else
James Smart912e3ac2011-05-24 11:42:11 -04007794 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7795 "3028 GET_FUNCTION_CONFIG: failed to find "
7796 "Resrouce Descriptor:x%x\n",
7797 LPFC_RSRC_DESC_TYPE_FCFCOE);
James Smart912e3ac2011-05-24 11:42:11 -04007798
7799read_cfg_out:
7800 mempool_free(pmb, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04007801 return rc;
7802}
7803
7804/**
James Smart2fcee4b2010-12-15 17:57:46 -05007805 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
James Smartda0436e2009-05-22 14:51:39 -04007806 * @phba: pointer to lpfc hba data structure.
7807 *
James Smart2fcee4b2010-12-15 17:57:46 -05007808 * This routine is invoked to setup the port-side endian order when
7809 * the port if_type is 0. This routine has no function for other
7810 * if_types.
James Smartda0436e2009-05-22 14:51:39 -04007811 *
7812 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007813 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007814 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007815 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007816 **/
7817static int
7818lpfc_setup_endian_order(struct lpfc_hba *phba)
7819{
7820 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05007821 uint32_t if_type, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04007822 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
7823 HOST_ENDIAN_HIGH_WORD1};
7824
James Smart2fcee4b2010-12-15 17:57:46 -05007825 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7826 switch (if_type) {
7827 case LPFC_SLI_INTF_IF_TYPE_0:
7828 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
7829 GFP_KERNEL);
7830 if (!mboxq) {
7831 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7832 "0492 Unable to allocate memory for "
7833 "issuing SLI_CONFIG_SPECIAL mailbox "
7834 "command\n");
7835 return -ENOMEM;
7836 }
James Smartda0436e2009-05-22 14:51:39 -04007837
James Smart2fcee4b2010-12-15 17:57:46 -05007838 /*
7839 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
7840 * two words to contain special data values and no other data.
7841 */
7842 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
7843 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
7844 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7845 if (rc != MBX_SUCCESS) {
7846 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7847 "0493 SLI_CONFIG_SPECIAL mailbox "
7848 "failed with status x%x\n",
7849 rc);
7850 rc = -EIO;
7851 }
7852 mempool_free(mboxq, phba->mbox_mem_pool);
7853 break;
7854 case LPFC_SLI_INTF_IF_TYPE_2:
7855 case LPFC_SLI_INTF_IF_TYPE_1:
7856 default:
7857 break;
James Smartda0436e2009-05-22 14:51:39 -04007858 }
James Smartda0436e2009-05-22 14:51:39 -04007859 return rc;
7860}
7861
7862/**
James Smart895427b2017-02-12 13:52:30 -08007863 * lpfc_sli4_queue_verify - Verify and update EQ counts
James Smartda0436e2009-05-22 14:51:39 -04007864 * @phba: pointer to lpfc hba data structure.
7865 *
James Smart895427b2017-02-12 13:52:30 -08007866 * This routine is invoked to check the user settable queue counts for EQs.
7867 * After this routine is called the counts will be set to valid values that
James Smart5350d872011-10-10 21:33:49 -04007868 * adhere to the constraints of the system's interrupt vectors and the port's
7869 * queue resources.
James Smartda0436e2009-05-22 14:51:39 -04007870 *
7871 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007872 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007873 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04007874 **/
7875static int
James Smart5350d872011-10-10 21:33:49 -04007876lpfc_sli4_queue_verify(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04007877{
James Smart895427b2017-02-12 13:52:30 -08007878 int io_channel;
James Smart1ba981f2014-02-20 09:56:45 -05007879 int fof_vectors = phba->cfg_fof ? 1 : 0;
James Smartda0436e2009-05-22 14:51:39 -04007880
7881 /*
James Smart67d12732012-08-03 12:36:13 -04007882 * Sanity check for configured queue parameters against the run-time
James Smartda0436e2009-05-22 14:51:39 -04007883 * device parameters
7884 */
7885
James Smart67d12732012-08-03 12:36:13 -04007886 /* Sanity check on HBA EQ parameters */
James Smart895427b2017-02-12 13:52:30 -08007887 io_channel = phba->io_channel_irqs;
James Smartda0436e2009-05-22 14:51:39 -04007888
James Smart895427b2017-02-12 13:52:30 -08007889 if (phba->sli4_hba.num_online_cpu < io_channel) {
James Smart82c3e9b2012-09-29 11:29:50 -04007890 lpfc_printf_log(phba,
7891 KERN_ERR, LOG_INIT,
James Smart90695ee2012-08-14 14:25:36 -04007892 "3188 Reducing IO channels to match number of "
James Smart7bb03bb2013-04-17 20:19:16 -04007893 "online CPUs: from %d to %d\n",
James Smart895427b2017-02-12 13:52:30 -08007894 io_channel, phba->sli4_hba.num_online_cpu);
7895 io_channel = phba->sli4_hba.num_online_cpu;
James Smart90695ee2012-08-14 14:25:36 -04007896 }
7897
James Smart895427b2017-02-12 13:52:30 -08007898 if (io_channel + fof_vectors > phba->sli4_hba.max_cfg_param.max_eq) {
James Smart82c3e9b2012-09-29 11:29:50 -04007899 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7900 "2575 Reducing IO channels to match number of "
7901 "available EQs: from %d to %d\n",
James Smart895427b2017-02-12 13:52:30 -08007902 io_channel,
James Smart82c3e9b2012-09-29 11:29:50 -04007903 phba->sli4_hba.max_cfg_param.max_eq);
James Smart895427b2017-02-12 13:52:30 -08007904 io_channel = phba->sli4_hba.max_cfg_param.max_eq - fof_vectors;
James Smartda0436e2009-05-22 14:51:39 -04007905 }
James Smart67d12732012-08-03 12:36:13 -04007906
James Smart895427b2017-02-12 13:52:30 -08007907 /* The actual number of FCP / NVME event queues adopted */
7908 if (io_channel != phba->io_channel_irqs)
7909 phba->io_channel_irqs = io_channel;
7910 if (phba->cfg_fcp_io_channel > io_channel)
7911 phba->cfg_fcp_io_channel = io_channel;
7912 if (phba->cfg_nvme_io_channel > io_channel)
7913 phba->cfg_nvme_io_channel = io_channel;
James Smart2d7dbc42017-02-12 13:52:35 -08007914 if (phba->cfg_nvme_io_channel < phba->cfg_nvmet_mrq)
7915 phba->cfg_nvmet_mrq = phba->cfg_nvme_io_channel;
James Smart895427b2017-02-12 13:52:30 -08007916
7917 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2d7dbc42017-02-12 13:52:35 -08007918 "2574 IO channels: irqs %d fcp %d nvme %d MRQ: %d\n",
James Smart895427b2017-02-12 13:52:30 -08007919 phba->io_channel_irqs, phba->cfg_fcp_io_channel,
James Smart2d7dbc42017-02-12 13:52:35 -08007920 phba->cfg_nvme_io_channel, phba->cfg_nvmet_mrq);
James Smartda0436e2009-05-22 14:51:39 -04007921
James Smartda0436e2009-05-22 14:51:39 -04007922 /* Get EQ depth from module parameter, fake the default for now */
7923 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
7924 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
7925
James Smart5350d872011-10-10 21:33:49 -04007926 /* Get CQ depth from module parameter, fake the default for now */
7927 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
7928 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
James Smart5350d872011-10-10 21:33:49 -04007929 return 0;
James Smart895427b2017-02-12 13:52:30 -08007930}
7931
7932static int
7933lpfc_alloc_nvme_wq_cq(struct lpfc_hba *phba, int wqidx)
7934{
7935 struct lpfc_queue *qdesc;
7936 int cnt;
7937
7938 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7939 phba->sli4_hba.cq_ecount);
7940 if (!qdesc) {
7941 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7942 "0508 Failed allocate fast-path NVME CQ (%d)\n",
7943 wqidx);
7944 return 1;
7945 }
7946 phba->sli4_hba.nvme_cq[wqidx] = qdesc;
7947
7948 cnt = LPFC_NVME_WQSIZE;
7949 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_WQE128_SIZE, cnt);
7950 if (!qdesc) {
7951 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7952 "0509 Failed allocate fast-path NVME WQ (%d)\n",
7953 wqidx);
7954 return 1;
7955 }
7956 phba->sli4_hba.nvme_wq[wqidx] = qdesc;
7957 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
7958 return 0;
7959}
7960
7961static int
7962lpfc_alloc_fcp_wq_cq(struct lpfc_hba *phba, int wqidx)
7963{
7964 struct lpfc_queue *qdesc;
7965 uint32_t wqesize;
7966
7967 /* Create Fast Path FCP CQs */
7968 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7969 phba->sli4_hba.cq_ecount);
7970 if (!qdesc) {
7971 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7972 "0499 Failed allocate fast-path FCP CQ (%d)\n", wqidx);
7973 return 1;
7974 }
7975 phba->sli4_hba.fcp_cq[wqidx] = qdesc;
7976
7977 /* Create Fast Path FCP WQs */
7978 wqesize = (phba->fcp_embed_io) ?
James Smartd1f525a2017-04-21 16:04:55 -07007979 LPFC_WQE128_SIZE : phba->sli4_hba.wq_esize;
James Smart895427b2017-02-12 13:52:30 -08007980 qdesc = lpfc_sli4_queue_alloc(phba, wqesize, phba->sli4_hba.wq_ecount);
7981 if (!qdesc) {
7982 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7983 "0503 Failed allocate fast-path FCP WQ (%d)\n",
7984 wqidx);
7985 return 1;
7986 }
7987 phba->sli4_hba.fcp_wq[wqidx] = qdesc;
7988 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
7989 return 0;
James Smart5350d872011-10-10 21:33:49 -04007990}
7991
7992/**
7993 * lpfc_sli4_queue_create - Create all the SLI4 queues
7994 * @phba: pointer to lpfc hba data structure.
7995 *
7996 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
7997 * operation. For each SLI4 queue type, the parameters such as queue entry
7998 * count (queue depth) shall be taken from the module parameter. For now,
7999 * we just use some constant number as place holder.
8000 *
8001 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07008002 * 0 - successful
James Smart5350d872011-10-10 21:33:49 -04008003 * -ENOMEM - No availble memory
8004 * -EIO - The mailbox failed to complete successfully.
8005 **/
8006int
8007lpfc_sli4_queue_create(struct lpfc_hba *phba)
8008{
8009 struct lpfc_queue *qdesc;
James Smartd1f525a2017-04-21 16:04:55 -07008010 int idx, io_channel;
James Smart5350d872011-10-10 21:33:49 -04008011
8012 /*
James Smart67d12732012-08-03 12:36:13 -04008013 * Create HBA Record arrays.
James Smart895427b2017-02-12 13:52:30 -08008014 * Both NVME and FCP will share that same vectors / EQs
James Smart5350d872011-10-10 21:33:49 -04008015 */
James Smart895427b2017-02-12 13:52:30 -08008016 io_channel = phba->io_channel_irqs;
8017 if (!io_channel)
James Smart67d12732012-08-03 12:36:13 -04008018 return -ERANGE;
James Smart5350d872011-10-10 21:33:49 -04008019
James Smart67d12732012-08-03 12:36:13 -04008020 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
8021 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
8022 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
8023 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
8024 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
8025 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
James Smart895427b2017-02-12 13:52:30 -08008026 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
8027 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
8028 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
8029 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
James Smart67d12732012-08-03 12:36:13 -04008030
James Smart895427b2017-02-12 13:52:30 -08008031 phba->sli4_hba.hba_eq = kcalloc(io_channel,
8032 sizeof(struct lpfc_queue *),
8033 GFP_KERNEL);
James Smart67d12732012-08-03 12:36:13 -04008034 if (!phba->sli4_hba.hba_eq) {
James Smartda0436e2009-05-22 14:51:39 -04008035 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04008036 "2576 Failed allocate memory for "
8037 "fast-path EQ record array\n");
James Smartda0436e2009-05-22 14:51:39 -04008038 goto out_error;
8039 }
James Smart67d12732012-08-03 12:36:13 -04008040
James Smart895427b2017-02-12 13:52:30 -08008041 if (phba->cfg_fcp_io_channel) {
8042 phba->sli4_hba.fcp_cq = kcalloc(phba->cfg_fcp_io_channel,
8043 sizeof(struct lpfc_queue *),
8044 GFP_KERNEL);
8045 if (!phba->sli4_hba.fcp_cq) {
8046 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8047 "2577 Failed allocate memory for "
8048 "fast-path CQ record array\n");
8049 goto out_error;
8050 }
8051 phba->sli4_hba.fcp_wq = kcalloc(phba->cfg_fcp_io_channel,
8052 sizeof(struct lpfc_queue *),
8053 GFP_KERNEL);
8054 if (!phba->sli4_hba.fcp_wq) {
8055 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8056 "2578 Failed allocate memory for "
8057 "fast-path FCP WQ record array\n");
8058 goto out_error;
8059 }
8060 /*
8061 * Since the first EQ can have multiple CQs associated with it,
8062 * this array is used to quickly see if we have a FCP fast-path
8063 * CQ match.
8064 */
8065 phba->sli4_hba.fcp_cq_map = kcalloc(phba->cfg_fcp_io_channel,
8066 sizeof(uint16_t),
8067 GFP_KERNEL);
8068 if (!phba->sli4_hba.fcp_cq_map) {
8069 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8070 "2545 Failed allocate memory for "
8071 "fast-path CQ map\n");
8072 goto out_error;
8073 }
James Smart67d12732012-08-03 12:36:13 -04008074 }
8075
James Smart895427b2017-02-12 13:52:30 -08008076 if (phba->cfg_nvme_io_channel) {
8077 phba->sli4_hba.nvme_cq = kcalloc(phba->cfg_nvme_io_channel,
8078 sizeof(struct lpfc_queue *),
8079 GFP_KERNEL);
8080 if (!phba->sli4_hba.nvme_cq) {
8081 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8082 "6077 Failed allocate memory for "
8083 "fast-path CQ record array\n");
8084 goto out_error;
8085 }
8086
James Smart895427b2017-02-12 13:52:30 -08008087 phba->sli4_hba.nvme_wq = kcalloc(phba->cfg_nvme_io_channel,
8088 sizeof(struct lpfc_queue *),
8089 GFP_KERNEL);
8090 if (!phba->sli4_hba.nvme_wq) {
8091 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8092 "2581 Failed allocate memory for "
8093 "fast-path NVME WQ record array\n");
8094 goto out_error;
8095 }
8096
8097 /*
8098 * Since the first EQ can have multiple CQs associated with it,
8099 * this array is used to quickly see if we have a NVME fast-path
8100 * CQ match.
8101 */
8102 phba->sli4_hba.nvme_cq_map = kcalloc(phba->cfg_nvme_io_channel,
8103 sizeof(uint16_t),
8104 GFP_KERNEL);
8105 if (!phba->sli4_hba.nvme_cq_map) {
8106 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8107 "6078 Failed allocate memory for "
8108 "fast-path CQ map\n");
8109 goto out_error;
8110 }
James Smart2d7dbc42017-02-12 13:52:35 -08008111
8112 if (phba->nvmet_support) {
8113 phba->sli4_hba.nvmet_cqset = kcalloc(
8114 phba->cfg_nvmet_mrq,
8115 sizeof(struct lpfc_queue *),
8116 GFP_KERNEL);
8117 if (!phba->sli4_hba.nvmet_cqset) {
8118 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8119 "3121 Fail allocate memory for "
8120 "fast-path CQ set array\n");
8121 goto out_error;
8122 }
8123 phba->sli4_hba.nvmet_mrq_hdr = kcalloc(
8124 phba->cfg_nvmet_mrq,
8125 sizeof(struct lpfc_queue *),
8126 GFP_KERNEL);
8127 if (!phba->sli4_hba.nvmet_mrq_hdr) {
8128 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8129 "3122 Fail allocate memory for "
8130 "fast-path RQ set hdr array\n");
8131 goto out_error;
8132 }
8133 phba->sli4_hba.nvmet_mrq_data = kcalloc(
8134 phba->cfg_nvmet_mrq,
8135 sizeof(struct lpfc_queue *),
8136 GFP_KERNEL);
8137 if (!phba->sli4_hba.nvmet_mrq_data) {
8138 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8139 "3124 Fail allocate memory for "
8140 "fast-path RQ set data array\n");
8141 goto out_error;
8142 }
8143 }
James Smart67d12732012-08-03 12:36:13 -04008144 }
James Smartda0436e2009-05-22 14:51:39 -04008145
James Smart895427b2017-02-12 13:52:30 -08008146 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
James Smart67d12732012-08-03 12:36:13 -04008147
James Smart895427b2017-02-12 13:52:30 -08008148 /* Create HBA Event Queues (EQs) */
8149 for (idx = 0; idx < io_channel; idx++) {
James Smart67d12732012-08-03 12:36:13 -04008150 /* Create EQs */
James Smartda0436e2009-05-22 14:51:39 -04008151 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
8152 phba->sli4_hba.eq_ecount);
8153 if (!qdesc) {
8154 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04008155 "0497 Failed allocate EQ (%d)\n", idx);
8156 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008157 }
James Smart67d12732012-08-03 12:36:13 -04008158 phba->sli4_hba.hba_eq[idx] = qdesc;
James Smartda0436e2009-05-22 14:51:39 -04008159 }
8160
James Smart895427b2017-02-12 13:52:30 -08008161 /* FCP and NVME io channels are not required to be balanced */
8162
8163 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++)
8164 if (lpfc_alloc_fcp_wq_cq(phba, idx))
8165 goto out_error;
8166
8167 for (idx = 0; idx < phba->cfg_nvme_io_channel; idx++)
8168 if (lpfc_alloc_nvme_wq_cq(phba, idx))
8169 goto out_error;
James Smart67d12732012-08-03 12:36:13 -04008170
James Smart2d7dbc42017-02-12 13:52:35 -08008171 if (phba->nvmet_support) {
8172 for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
8173 qdesc = lpfc_sli4_queue_alloc(phba,
8174 phba->sli4_hba.cq_esize,
8175 phba->sli4_hba.cq_ecount);
8176 if (!qdesc) {
8177 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8178 "3142 Failed allocate NVME "
8179 "CQ Set (%d)\n", idx);
8180 goto out_error;
8181 }
8182 phba->sli4_hba.nvmet_cqset[idx] = qdesc;
8183 }
8184 }
8185
James Smartda0436e2009-05-22 14:51:39 -04008186 /*
James Smart67d12732012-08-03 12:36:13 -04008187 * Create Slow Path Completion Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04008188 */
8189
James Smartda0436e2009-05-22 14:51:39 -04008190 /* Create slow-path Mailbox Command Complete Queue */
8191 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
8192 phba->sli4_hba.cq_ecount);
8193 if (!qdesc) {
8194 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8195 "0500 Failed allocate slow-path mailbox CQ\n");
James Smart67d12732012-08-03 12:36:13 -04008196 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008197 }
8198 phba->sli4_hba.mbx_cq = qdesc;
8199
8200 /* Create slow-path ELS Complete Queue */
8201 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
8202 phba->sli4_hba.cq_ecount);
8203 if (!qdesc) {
8204 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8205 "0501 Failed allocate slow-path ELS CQ\n");
James Smart67d12732012-08-03 12:36:13 -04008206 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008207 }
8208 phba->sli4_hba.els_cq = qdesc;
8209
James Smartda0436e2009-05-22 14:51:39 -04008210
James Smart5350d872011-10-10 21:33:49 -04008211 /*
James Smart67d12732012-08-03 12:36:13 -04008212 * Create Slow Path Work Queues (WQs)
James Smart5350d872011-10-10 21:33:49 -04008213 */
James Smartda0436e2009-05-22 14:51:39 -04008214
8215 /* Create Mailbox Command Queue */
James Smartda0436e2009-05-22 14:51:39 -04008216
8217 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
8218 phba->sli4_hba.mq_ecount);
8219 if (!qdesc) {
8220 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8221 "0505 Failed allocate slow-path MQ\n");
James Smart67d12732012-08-03 12:36:13 -04008222 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008223 }
8224 phba->sli4_hba.mbx_wq = qdesc;
8225
8226 /*
James Smart67d12732012-08-03 12:36:13 -04008227 * Create ELS Work Queues
James Smartda0436e2009-05-22 14:51:39 -04008228 */
James Smartda0436e2009-05-22 14:51:39 -04008229
8230 /* Create slow-path ELS Work Queue */
8231 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
8232 phba->sli4_hba.wq_ecount);
8233 if (!qdesc) {
8234 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8235 "0504 Failed allocate slow-path ELS WQ\n");
James Smart67d12732012-08-03 12:36:13 -04008236 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008237 }
8238 phba->sli4_hba.els_wq = qdesc;
James Smart895427b2017-02-12 13:52:30 -08008239 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8240
8241 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
8242 /* Create NVME LS Complete Queue */
8243 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
8244 phba->sli4_hba.cq_ecount);
8245 if (!qdesc) {
8246 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8247 "6079 Failed allocate NVME LS CQ\n");
8248 goto out_error;
8249 }
8250 phba->sli4_hba.nvmels_cq = qdesc;
8251
8252 /* Create NVME LS Work Queue */
8253 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
8254 phba->sli4_hba.wq_ecount);
8255 if (!qdesc) {
8256 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8257 "6080 Failed allocate NVME LS WQ\n");
8258 goto out_error;
8259 }
8260 phba->sli4_hba.nvmels_wq = qdesc;
8261 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8262 }
James Smartda0436e2009-05-22 14:51:39 -04008263
James Smartda0436e2009-05-22 14:51:39 -04008264 /*
8265 * Create Receive Queue (RQ)
8266 */
James Smartda0436e2009-05-22 14:51:39 -04008267
8268 /* Create Receive Queue for header */
8269 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
8270 phba->sli4_hba.rq_ecount);
8271 if (!qdesc) {
8272 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8273 "0506 Failed allocate receive HRQ\n");
James Smart67d12732012-08-03 12:36:13 -04008274 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008275 }
8276 phba->sli4_hba.hdr_rq = qdesc;
8277
8278 /* Create Receive Queue for data */
8279 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
8280 phba->sli4_hba.rq_ecount);
8281 if (!qdesc) {
8282 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8283 "0507 Failed allocate receive DRQ\n");
James Smart67d12732012-08-03 12:36:13 -04008284 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008285 }
8286 phba->sli4_hba.dat_rq = qdesc;
8287
James Smart2d7dbc42017-02-12 13:52:35 -08008288 if (phba->nvmet_support) {
8289 for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
8290 /* Create NVMET Receive Queue for header */
8291 qdesc = lpfc_sli4_queue_alloc(phba,
8292 phba->sli4_hba.rq_esize,
James Smart61f3d4b2017-05-15 15:20:41 -07008293 LPFC_NVMET_RQE_DEF_COUNT);
James Smart2d7dbc42017-02-12 13:52:35 -08008294 if (!qdesc) {
8295 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8296 "3146 Failed allocate "
8297 "receive HRQ\n");
8298 goto out_error;
8299 }
8300 phba->sli4_hba.nvmet_mrq_hdr[idx] = qdesc;
8301
8302 /* Only needed for header of RQ pair */
8303 qdesc->rqbp = kzalloc(sizeof(struct lpfc_rqb),
8304 GFP_KERNEL);
8305 if (qdesc->rqbp == NULL) {
8306 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8307 "6131 Failed allocate "
8308 "Header RQBP\n");
8309 goto out_error;
8310 }
8311
Dick Kennedy4b40d022017-08-23 16:55:38 -07008312 /* Put list in known state in case driver load fails. */
8313 INIT_LIST_HEAD(&qdesc->rqbp->rqb_buffer_list);
8314
James Smart2d7dbc42017-02-12 13:52:35 -08008315 /* Create NVMET Receive Queue for data */
8316 qdesc = lpfc_sli4_queue_alloc(phba,
8317 phba->sli4_hba.rq_esize,
James Smart61f3d4b2017-05-15 15:20:41 -07008318 LPFC_NVMET_RQE_DEF_COUNT);
James Smart2d7dbc42017-02-12 13:52:35 -08008319 if (!qdesc) {
8320 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8321 "3156 Failed allocate "
8322 "receive DRQ\n");
8323 goto out_error;
8324 }
8325 phba->sli4_hba.nvmet_mrq_data[idx] = qdesc;
8326 }
8327 }
8328
James Smart1ba981f2014-02-20 09:56:45 -05008329 /* Create the Queues needed for Flash Optimized Fabric operations */
8330 if (phba->cfg_fof)
8331 lpfc_fof_queue_create(phba);
James Smartda0436e2009-05-22 14:51:39 -04008332 return 0;
8333
James Smartda0436e2009-05-22 14:51:39 -04008334out_error:
James Smart67d12732012-08-03 12:36:13 -04008335 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04008336 return -ENOMEM;
8337}
8338
James Smart895427b2017-02-12 13:52:30 -08008339static inline void
8340__lpfc_sli4_release_queue(struct lpfc_queue **qp)
8341{
8342 if (*qp != NULL) {
8343 lpfc_sli4_queue_free(*qp);
8344 *qp = NULL;
8345 }
8346}
8347
8348static inline void
8349lpfc_sli4_release_queues(struct lpfc_queue ***qs, int max)
8350{
8351 int idx;
8352
8353 if (*qs == NULL)
8354 return;
8355
8356 for (idx = 0; idx < max; idx++)
8357 __lpfc_sli4_release_queue(&(*qs)[idx]);
8358
8359 kfree(*qs);
8360 *qs = NULL;
8361}
8362
8363static inline void
8364lpfc_sli4_release_queue_map(uint16_t **qmap)
8365{
8366 if (*qmap != NULL) {
8367 kfree(*qmap);
8368 *qmap = NULL;
8369 }
8370}
8371
James Smartda0436e2009-05-22 14:51:39 -04008372/**
8373 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
8374 * @phba: pointer to lpfc hba data structure.
8375 *
8376 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
8377 * operation.
8378 *
8379 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008380 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008381 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04008382 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04008383 **/
James Smart5350d872011-10-10 21:33:49 -04008384void
James Smartda0436e2009-05-22 14:51:39 -04008385lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
8386{
James Smart1ba981f2014-02-20 09:56:45 -05008387 if (phba->cfg_fof)
8388 lpfc_fof_queue_destroy(phba);
8389
James Smart895427b2017-02-12 13:52:30 -08008390 /* Release HBA eqs */
8391 lpfc_sli4_release_queues(&phba->sli4_hba.hba_eq, phba->io_channel_irqs);
James Smart67d12732012-08-03 12:36:13 -04008392
James Smart895427b2017-02-12 13:52:30 -08008393 /* Release FCP cqs */
8394 lpfc_sli4_release_queues(&phba->sli4_hba.fcp_cq,
James Smartd1f525a2017-04-21 16:04:55 -07008395 phba->cfg_fcp_io_channel);
James Smart67d12732012-08-03 12:36:13 -04008396
James Smart895427b2017-02-12 13:52:30 -08008397 /* Release FCP wqs */
8398 lpfc_sli4_release_queues(&phba->sli4_hba.fcp_wq,
James Smartd1f525a2017-04-21 16:04:55 -07008399 phba->cfg_fcp_io_channel);
James Smart67d12732012-08-03 12:36:13 -04008400
8401 /* Release FCP CQ mapping array */
James Smart895427b2017-02-12 13:52:30 -08008402 lpfc_sli4_release_queue_map(&phba->sli4_hba.fcp_cq_map);
8403
8404 /* Release NVME cqs */
8405 lpfc_sli4_release_queues(&phba->sli4_hba.nvme_cq,
8406 phba->cfg_nvme_io_channel);
8407
8408 /* Release NVME wqs */
8409 lpfc_sli4_release_queues(&phba->sli4_hba.nvme_wq,
8410 phba->cfg_nvme_io_channel);
8411
8412 /* Release NVME CQ mapping array */
8413 lpfc_sli4_release_queue_map(&phba->sli4_hba.nvme_cq_map);
James Smartda0436e2009-05-22 14:51:39 -04008414
James Smart2d7dbc42017-02-12 13:52:35 -08008415 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_cqset,
8416 phba->cfg_nvmet_mrq);
8417
8418 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_hdr,
8419 phba->cfg_nvmet_mrq);
8420 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_data,
8421 phba->cfg_nvmet_mrq);
8422
James Smartda0436e2009-05-22 14:51:39 -04008423 /* Release mailbox command work queue */
James Smart895427b2017-02-12 13:52:30 -08008424 __lpfc_sli4_release_queue(&phba->sli4_hba.mbx_wq);
James Smartda0436e2009-05-22 14:51:39 -04008425
8426 /* Release ELS work queue */
James Smart895427b2017-02-12 13:52:30 -08008427 __lpfc_sli4_release_queue(&phba->sli4_hba.els_wq);
8428
8429 /* Release ELS work queue */
8430 __lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_wq);
James Smartda0436e2009-05-22 14:51:39 -04008431
8432 /* Release unsolicited receive queue */
James Smart895427b2017-02-12 13:52:30 -08008433 __lpfc_sli4_release_queue(&phba->sli4_hba.hdr_rq);
8434 __lpfc_sli4_release_queue(&phba->sli4_hba.dat_rq);
James Smartda0436e2009-05-22 14:51:39 -04008435
James Smartda0436e2009-05-22 14:51:39 -04008436 /* Release ELS complete queue */
James Smart895427b2017-02-12 13:52:30 -08008437 __lpfc_sli4_release_queue(&phba->sli4_hba.els_cq);
8438
8439 /* Release NVME LS complete queue */
8440 __lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_cq);
James Smartda0436e2009-05-22 14:51:39 -04008441
8442 /* Release mailbox command complete queue */
James Smart895427b2017-02-12 13:52:30 -08008443 __lpfc_sli4_release_queue(&phba->sli4_hba.mbx_cq);
8444
8445 /* Everything on this list has been freed */
8446 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
8447}
8448
8449int
James Smart895427b2017-02-12 13:52:30 -08008450lpfc_free_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *rq)
8451{
8452 struct lpfc_rqb *rqbp;
8453 struct lpfc_dmabuf *h_buf;
8454 struct rqb_dmabuf *rqb_buffer;
8455
8456 rqbp = rq->rqbp;
8457 while (!list_empty(&rqbp->rqb_buffer_list)) {
8458 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
8459 struct lpfc_dmabuf, list);
8460
8461 rqb_buffer = container_of(h_buf, struct rqb_dmabuf, hbuf);
8462 (rqbp->rqb_free_buffer)(phba, rqb_buffer);
8463 rqbp->buffer_count--;
8464 }
8465 return 1;
8466}
8467
8468static int
8469lpfc_create_wq_cq(struct lpfc_hba *phba, struct lpfc_queue *eq,
8470 struct lpfc_queue *cq, struct lpfc_queue *wq, uint16_t *cq_map,
8471 int qidx, uint32_t qtype)
8472{
8473 struct lpfc_sli_ring *pring;
8474 int rc;
8475
8476 if (!eq || !cq || !wq) {
8477 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8478 "6085 Fast-path %s (%d) not allocated\n",
8479 ((eq) ? ((cq) ? "WQ" : "CQ") : "EQ"), qidx);
8480 return -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04008481 }
James Smartda0436e2009-05-22 14:51:39 -04008482
James Smart895427b2017-02-12 13:52:30 -08008483 /* create the Cq first */
8484 rc = lpfc_cq_create(phba, cq, eq,
8485 (qtype == LPFC_MBOX) ? LPFC_MCQ : LPFC_WCQ, qtype);
8486 if (rc) {
8487 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8488 "6086 Failed setup of CQ (%d), rc = 0x%x\n",
8489 qidx, (uint32_t)rc);
8490 return rc;
8491 }
8492
8493 if (qtype != LPFC_MBOX) {
8494 /* Setup nvme_cq_map for fast lookup */
8495 if (cq_map)
8496 *cq_map = cq->queue_id;
8497
8498 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8499 "6087 CQ setup: cq[%d]-id=%d, parent eq[%d]-id=%d\n",
8500 qidx, cq->queue_id, qidx, eq->queue_id);
8501
8502 /* create the wq */
8503 rc = lpfc_wq_create(phba, wq, cq, qtype);
8504 if (rc) {
8505 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8506 "6123 Fail setup fastpath WQ (%d), rc = 0x%x\n",
8507 qidx, (uint32_t)rc);
8508 /* no need to tear down cq - caller will do so */
8509 return rc;
8510 }
8511
8512 /* Bind this CQ/WQ to the NVME ring */
8513 pring = wq->pring;
8514 pring->sli.sli4.wqp = (void *)wq;
8515 cq->pring = pring;
8516
8517 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8518 "2593 WQ setup: wq[%d]-id=%d assoc=%d, cq[%d]-id=%d\n",
8519 qidx, wq->queue_id, wq->assoc_qid, qidx, cq->queue_id);
8520 } else {
8521 rc = lpfc_mq_create(phba, wq, cq, LPFC_MBOX);
8522 if (rc) {
8523 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8524 "0539 Failed setup of slow-path MQ: "
8525 "rc = 0x%x\n", rc);
8526 /* no need to tear down cq - caller will do so */
8527 return rc;
8528 }
8529
8530 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8531 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
8532 phba->sli4_hba.mbx_wq->queue_id,
8533 phba->sli4_hba.mbx_cq->queue_id);
8534 }
8535
8536 return 0;
James Smartda0436e2009-05-22 14:51:39 -04008537}
8538
8539/**
8540 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
8541 * @phba: pointer to lpfc hba data structure.
8542 *
8543 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
8544 * operation.
8545 *
8546 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008547 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008548 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04008549 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04008550 **/
8551int
8552lpfc_sli4_queue_setup(struct lpfc_hba *phba)
8553{
James Smart962bc512013-01-03 15:44:00 -05008554 uint32_t shdr_status, shdr_add_status;
8555 union lpfc_sli4_cfg_shdr *shdr;
8556 LPFC_MBOXQ_t *mboxq;
James Smart895427b2017-02-12 13:52:30 -08008557 int qidx;
8558 uint32_t length, io_channel;
8559 int rc = -ENOMEM;
James Smart962bc512013-01-03 15:44:00 -05008560
8561 /* Check for dual-ULP support */
8562 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8563 if (!mboxq) {
8564 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8565 "3249 Unable to allocate memory for "
8566 "QUERY_FW_CFG mailbox command\n");
8567 return -ENOMEM;
8568 }
8569 length = (sizeof(struct lpfc_mbx_query_fw_config) -
8570 sizeof(struct lpfc_sli4_cfg_mhdr));
8571 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
8572 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
8573 length, LPFC_SLI4_MBX_EMBED);
8574
8575 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8576
8577 shdr = (union lpfc_sli4_cfg_shdr *)
8578 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
8579 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
8580 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
8581 if (shdr_status || shdr_add_status || rc) {
8582 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8583 "3250 QUERY_FW_CFG mailbox failed with status "
8584 "x%x add_status x%x, mbx status x%x\n",
8585 shdr_status, shdr_add_status, rc);
8586 if (rc != MBX_TIMEOUT)
8587 mempool_free(mboxq, phba->mbox_mem_pool);
8588 rc = -ENXIO;
8589 goto out_error;
8590 }
8591
8592 phba->sli4_hba.fw_func_mode =
8593 mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
8594 phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
8595 phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
James Smart8b017a32015-05-21 13:55:18 -04008596 phba->sli4_hba.physical_port =
8597 mboxq->u.mqe.un.query_fw_cfg.rsp.physical_port;
James Smart962bc512013-01-03 15:44:00 -05008598 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8599 "3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
8600 "ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
8601 phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
8602
8603 if (rc != MBX_TIMEOUT)
8604 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04008605
8606 /*
James Smart67d12732012-08-03 12:36:13 -04008607 * Set up HBA Event Queues (EQs)
James Smartda0436e2009-05-22 14:51:39 -04008608 */
James Smart895427b2017-02-12 13:52:30 -08008609 io_channel = phba->io_channel_irqs;
James Smartda0436e2009-05-22 14:51:39 -04008610
James Smart67d12732012-08-03 12:36:13 -04008611 /* Set up HBA event queue */
James Smart895427b2017-02-12 13:52:30 -08008612 if (io_channel && !phba->sli4_hba.hba_eq) {
James Smart2e90f4b2011-12-13 13:22:37 -05008613 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8614 "3147 Fast-path EQs not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05008615 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04008616 goto out_error;
James Smart2e90f4b2011-12-13 13:22:37 -05008617 }
James Smart895427b2017-02-12 13:52:30 -08008618 for (qidx = 0; qidx < io_channel; qidx++) {
8619 if (!phba->sli4_hba.hba_eq[qidx]) {
James Smartda0436e2009-05-22 14:51:39 -04008620 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8621 "0522 Fast-path EQ (%d) not "
James Smart895427b2017-02-12 13:52:30 -08008622 "allocated\n", qidx);
James Smart1b511972011-12-13 13:23:09 -05008623 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08008624 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008625 }
James Smart895427b2017-02-12 13:52:30 -08008626 rc = lpfc_eq_create(phba, phba->sli4_hba.hba_eq[qidx],
8627 phba->cfg_fcp_imax);
James Smartda0436e2009-05-22 14:51:39 -04008628 if (rc) {
8629 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8630 "0523 Failed setup of fast-path EQ "
James Smart895427b2017-02-12 13:52:30 -08008631 "(%d), rc = 0x%x\n", qidx,
James Smarta2fc4aef2014-09-03 12:57:55 -04008632 (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -08008633 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008634 }
8635 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008636 "2584 HBA EQ setup: queue[%d]-id=%d\n",
8637 qidx, phba->sli4_hba.hba_eq[qidx]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04008638 }
8639
James Smart895427b2017-02-12 13:52:30 -08008640 if (phba->cfg_nvme_io_channel) {
8641 if (!phba->sli4_hba.nvme_cq || !phba->sli4_hba.nvme_wq) {
James Smart67d12732012-08-03 12:36:13 -04008642 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008643 "6084 Fast-path NVME %s array not allocated\n",
8644 (phba->sli4_hba.nvme_cq) ? "CQ" : "WQ");
James Smart67d12732012-08-03 12:36:13 -04008645 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08008646 goto out_destroy;
James Smart67d12732012-08-03 12:36:13 -04008647 }
8648
James Smart895427b2017-02-12 13:52:30 -08008649 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++) {
8650 rc = lpfc_create_wq_cq(phba,
8651 phba->sli4_hba.hba_eq[
8652 qidx % io_channel],
8653 phba->sli4_hba.nvme_cq[qidx],
8654 phba->sli4_hba.nvme_wq[qidx],
8655 &phba->sli4_hba.nvme_cq_map[qidx],
8656 qidx, LPFC_NVME);
8657 if (rc) {
8658 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8659 "6123 Failed to setup fastpath "
8660 "NVME WQ/CQ (%d), rc = 0x%x\n",
8661 qidx, (uint32_t)rc);
8662 goto out_destroy;
8663 }
8664 }
James Smart67d12732012-08-03 12:36:13 -04008665 }
8666
James Smart895427b2017-02-12 13:52:30 -08008667 if (phba->cfg_fcp_io_channel) {
8668 /* Set up fast-path FCP Response Complete Queue */
8669 if (!phba->sli4_hba.fcp_cq || !phba->sli4_hba.fcp_wq) {
James Smart67d12732012-08-03 12:36:13 -04008670 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008671 "3148 Fast-path FCP %s array not allocated\n",
8672 phba->sli4_hba.fcp_cq ? "WQ" : "CQ");
James Smart67d12732012-08-03 12:36:13 -04008673 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08008674 goto out_destroy;
James Smart67d12732012-08-03 12:36:13 -04008675 }
8676
James Smart895427b2017-02-12 13:52:30 -08008677 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++) {
8678 rc = lpfc_create_wq_cq(phba,
8679 phba->sli4_hba.hba_eq[
8680 qidx % io_channel],
8681 phba->sli4_hba.fcp_cq[qidx],
8682 phba->sli4_hba.fcp_wq[qidx],
8683 &phba->sli4_hba.fcp_cq_map[qidx],
8684 qidx, LPFC_FCP);
8685 if (rc) {
8686 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8687 "0535 Failed to setup fastpath "
8688 "FCP WQ/CQ (%d), rc = 0x%x\n",
8689 qidx, (uint32_t)rc);
8690 goto out_destroy;
8691 }
8692 }
James Smart67d12732012-08-03 12:36:13 -04008693 }
James Smartda0436e2009-05-22 14:51:39 -04008694
8695 /*
James Smart895427b2017-02-12 13:52:30 -08008696 * Set up Slow Path Complete Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04008697 */
8698
James Smart895427b2017-02-12 13:52:30 -08008699 /* Set up slow-path MBOX CQ/MQ */
8700
8701 if (!phba->sli4_hba.mbx_cq || !phba->sli4_hba.mbx_wq) {
James Smartda0436e2009-05-22 14:51:39 -04008702 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008703 "0528 %s not allocated\n",
8704 phba->sli4_hba.mbx_cq ?
James Smartd1f525a2017-04-21 16:04:55 -07008705 "Mailbox WQ" : "Mailbox CQ");
James Smart1b511972011-12-13 13:23:09 -05008706 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08008707 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008708 }
James Smart895427b2017-02-12 13:52:30 -08008709
8710 rc = lpfc_create_wq_cq(phba, phba->sli4_hba.hba_eq[0],
James Smartd1f525a2017-04-21 16:04:55 -07008711 phba->sli4_hba.mbx_cq,
8712 phba->sli4_hba.mbx_wq,
8713 NULL, 0, LPFC_MBOX);
James Smartda0436e2009-05-22 14:51:39 -04008714 if (rc) {
8715 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008716 "0529 Failed setup of mailbox WQ/CQ: rc = 0x%x\n",
8717 (uint32_t)rc);
8718 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008719 }
James Smart2d7dbc42017-02-12 13:52:35 -08008720 if (phba->nvmet_support) {
8721 if (!phba->sli4_hba.nvmet_cqset) {
8722 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8723 "3165 Fast-path NVME CQ Set "
8724 "array not allocated\n");
8725 rc = -ENOMEM;
8726 goto out_destroy;
8727 }
8728 if (phba->cfg_nvmet_mrq > 1) {
8729 rc = lpfc_cq_create_set(phba,
8730 phba->sli4_hba.nvmet_cqset,
8731 phba->sli4_hba.hba_eq,
8732 LPFC_WCQ, LPFC_NVMET);
8733 if (rc) {
8734 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8735 "3164 Failed setup of NVME CQ "
8736 "Set, rc = 0x%x\n",
8737 (uint32_t)rc);
8738 goto out_destroy;
8739 }
8740 } else {
8741 /* Set up NVMET Receive Complete Queue */
8742 rc = lpfc_cq_create(phba, phba->sli4_hba.nvmet_cqset[0],
8743 phba->sli4_hba.hba_eq[0],
8744 LPFC_WCQ, LPFC_NVMET);
8745 if (rc) {
8746 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8747 "6089 Failed setup NVMET CQ: "
8748 "rc = 0x%x\n", (uint32_t)rc);
8749 goto out_destroy;
8750 }
8751 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8752 "6090 NVMET CQ setup: cq-id=%d, "
8753 "parent eq-id=%d\n",
8754 phba->sli4_hba.nvmet_cqset[0]->queue_id,
8755 phba->sli4_hba.hba_eq[0]->queue_id);
8756 }
8757 }
James Smartda0436e2009-05-22 14:51:39 -04008758
James Smart895427b2017-02-12 13:52:30 -08008759 /* Set up slow-path ELS WQ/CQ */
8760 if (!phba->sli4_hba.els_cq || !phba->sli4_hba.els_wq) {
James Smartda0436e2009-05-22 14:51:39 -04008761 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008762 "0530 ELS %s not allocated\n",
8763 phba->sli4_hba.els_cq ? "WQ" : "CQ");
James Smart1b511972011-12-13 13:23:09 -05008764 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08008765 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008766 }
James Smart895427b2017-02-12 13:52:30 -08008767 rc = lpfc_create_wq_cq(phba, phba->sli4_hba.hba_eq[0],
8768 phba->sli4_hba.els_cq,
8769 phba->sli4_hba.els_wq,
8770 NULL, 0, LPFC_ELS);
James Smartda0436e2009-05-22 14:51:39 -04008771 if (rc) {
8772 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008773 "0529 Failed setup of ELS WQ/CQ: rc = 0x%x\n",
8774 (uint32_t)rc);
8775 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008776 }
8777 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8778 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
8779 phba->sli4_hba.els_wq->queue_id,
8780 phba->sli4_hba.els_cq->queue_id);
8781
James Smart895427b2017-02-12 13:52:30 -08008782 if (phba->cfg_nvme_io_channel) {
8783 /* Set up NVME LS Complete Queue */
8784 if (!phba->sli4_hba.nvmels_cq || !phba->sli4_hba.nvmels_wq) {
8785 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8786 "6091 LS %s not allocated\n",
8787 phba->sli4_hba.nvmels_cq ? "WQ" : "CQ");
8788 rc = -ENOMEM;
8789 goto out_destroy;
8790 }
8791 rc = lpfc_create_wq_cq(phba, phba->sli4_hba.hba_eq[0],
8792 phba->sli4_hba.nvmels_cq,
8793 phba->sli4_hba.nvmels_wq,
8794 NULL, 0, LPFC_NVME_LS);
8795 if (rc) {
8796 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8797 "0529 Failed setup of NVVME LS WQ/CQ: "
8798 "rc = 0x%x\n", (uint32_t)rc);
8799 goto out_destroy;
8800 }
8801
8802 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8803 "6096 ELS WQ setup: wq-id=%d, "
8804 "parent cq-id=%d\n",
8805 phba->sli4_hba.nvmels_wq->queue_id,
8806 phba->sli4_hba.nvmels_cq->queue_id);
8807 }
8808
James Smart2d7dbc42017-02-12 13:52:35 -08008809 /*
8810 * Create NVMET Receive Queue (RQ)
8811 */
8812 if (phba->nvmet_support) {
8813 if ((!phba->sli4_hba.nvmet_cqset) ||
8814 (!phba->sli4_hba.nvmet_mrq_hdr) ||
8815 (!phba->sli4_hba.nvmet_mrq_data)) {
8816 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8817 "6130 MRQ CQ Queues not "
8818 "allocated\n");
8819 rc = -ENOMEM;
8820 goto out_destroy;
8821 }
8822 if (phba->cfg_nvmet_mrq > 1) {
8823 rc = lpfc_mrq_create(phba,
8824 phba->sli4_hba.nvmet_mrq_hdr,
8825 phba->sli4_hba.nvmet_mrq_data,
8826 phba->sli4_hba.nvmet_cqset,
8827 LPFC_NVMET);
8828 if (rc) {
8829 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8830 "6098 Failed setup of NVMET "
8831 "MRQ: rc = 0x%x\n",
8832 (uint32_t)rc);
8833 goto out_destroy;
8834 }
8835
8836 } else {
8837 rc = lpfc_rq_create(phba,
8838 phba->sli4_hba.nvmet_mrq_hdr[0],
8839 phba->sli4_hba.nvmet_mrq_data[0],
8840 phba->sli4_hba.nvmet_cqset[0],
8841 LPFC_NVMET);
8842 if (rc) {
8843 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8844 "6057 Failed setup of NVMET "
8845 "Receive Queue: rc = 0x%x\n",
8846 (uint32_t)rc);
8847 goto out_destroy;
8848 }
8849
8850 lpfc_printf_log(
8851 phba, KERN_INFO, LOG_INIT,
8852 "6099 NVMET RQ setup: hdr-rq-id=%d, "
8853 "dat-rq-id=%d parent cq-id=%d\n",
8854 phba->sli4_hba.nvmet_mrq_hdr[0]->queue_id,
8855 phba->sli4_hba.nvmet_mrq_data[0]->queue_id,
8856 phba->sli4_hba.nvmet_cqset[0]->queue_id);
8857
8858 }
8859 }
8860
James Smartda0436e2009-05-22 14:51:39 -04008861 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
8862 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8863 "0540 Receive Queue not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05008864 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08008865 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008866 }
James Smart73d91e52011-10-10 21:32:10 -04008867
James Smartda0436e2009-05-22 14:51:39 -04008868 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04008869 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04008870 if (rc) {
8871 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8872 "0541 Failed setup of Receive Queue: "
James Smarta2fc4aef2014-09-03 12:57:55 -04008873 "rc = 0x%x\n", (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -08008874 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008875 }
James Smart73d91e52011-10-10 21:32:10 -04008876
James Smartda0436e2009-05-22 14:51:39 -04008877 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8878 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
8879 "parent cq-id=%d\n",
8880 phba->sli4_hba.hdr_rq->queue_id,
8881 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04008882 phba->sli4_hba.els_cq->queue_id);
James Smart1ba981f2014-02-20 09:56:45 -05008883
8884 if (phba->cfg_fof) {
8885 rc = lpfc_fof_queue_setup(phba);
8886 if (rc) {
8887 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8888 "0549 Failed setup of FOF Queues: "
8889 "rc = 0x%x\n", rc);
James Smart895427b2017-02-12 13:52:30 -08008890 goto out_destroy;
James Smart1ba981f2014-02-20 09:56:45 -05008891 }
8892 }
James Smart2c9c5a02015-04-07 15:07:15 -04008893
James Smart43140ca2017-03-04 09:30:34 -08008894 for (qidx = 0; qidx < io_channel; qidx += LPFC_MAX_EQ_DELAY_EQID_CNT)
James Smart0cf07f842017-06-01 21:07:10 -07008895 lpfc_modify_hba_eq_delay(phba, qidx, LPFC_MAX_EQ_DELAY_EQID_CNT,
8896 phba->cfg_fcp_imax);
James Smart43140ca2017-03-04 09:30:34 -08008897
James Smartda0436e2009-05-22 14:51:39 -04008898 return 0;
8899
James Smart895427b2017-02-12 13:52:30 -08008900out_destroy:
8901 lpfc_sli4_queue_unset(phba);
James Smartda0436e2009-05-22 14:51:39 -04008902out_error:
8903 return rc;
8904}
8905
8906/**
8907 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
8908 * @phba: pointer to lpfc hba data structure.
8909 *
8910 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
8911 * operation.
8912 *
8913 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008914 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008915 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04008916 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04008917 **/
8918void
8919lpfc_sli4_queue_unset(struct lpfc_hba *phba)
8920{
James Smart895427b2017-02-12 13:52:30 -08008921 int qidx;
James Smartda0436e2009-05-22 14:51:39 -04008922
James Smart1ba981f2014-02-20 09:56:45 -05008923 /* Unset the queues created for Flash Optimized Fabric operations */
8924 if (phba->cfg_fof)
8925 lpfc_fof_queue_destroy(phba);
James Smart895427b2017-02-12 13:52:30 -08008926
James Smartda0436e2009-05-22 14:51:39 -04008927 /* Unset mailbox command work queue */
James Smart895427b2017-02-12 13:52:30 -08008928 if (phba->sli4_hba.mbx_wq)
8929 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
8930
8931 /* Unset NVME LS work queue */
8932 if (phba->sli4_hba.nvmels_wq)
8933 lpfc_wq_destroy(phba, phba->sli4_hba.nvmels_wq);
8934
James Smartda0436e2009-05-22 14:51:39 -04008935 /* Unset ELS work queue */
Colin Ian King019c0d62017-05-06 23:13:55 +01008936 if (phba->sli4_hba.els_wq)
James Smart895427b2017-02-12 13:52:30 -08008937 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
8938
James Smartda0436e2009-05-22 14:51:39 -04008939 /* Unset unsolicited receive queue */
James Smart895427b2017-02-12 13:52:30 -08008940 if (phba->sli4_hba.hdr_rq)
8941 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq,
8942 phba->sli4_hba.dat_rq);
8943
James Smartda0436e2009-05-22 14:51:39 -04008944 /* Unset FCP work queue */
James Smart895427b2017-02-12 13:52:30 -08008945 if (phba->sli4_hba.fcp_wq)
8946 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
8947 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[qidx]);
8948
8949 /* Unset NVME work queue */
8950 if (phba->sli4_hba.nvme_wq) {
8951 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
8952 lpfc_wq_destroy(phba, phba->sli4_hba.nvme_wq[qidx]);
James Smart67d12732012-08-03 12:36:13 -04008953 }
James Smart895427b2017-02-12 13:52:30 -08008954
James Smartda0436e2009-05-22 14:51:39 -04008955 /* Unset mailbox command complete queue */
James Smart895427b2017-02-12 13:52:30 -08008956 if (phba->sli4_hba.mbx_cq)
8957 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
8958
James Smartda0436e2009-05-22 14:51:39 -04008959 /* Unset ELS complete queue */
James Smart895427b2017-02-12 13:52:30 -08008960 if (phba->sli4_hba.els_cq)
8961 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
8962
8963 /* Unset NVME LS complete queue */
8964 if (phba->sli4_hba.nvmels_cq)
8965 lpfc_cq_destroy(phba, phba->sli4_hba.nvmels_cq);
8966
8967 /* Unset NVME response complete queue */
8968 if (phba->sli4_hba.nvme_cq)
8969 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
8970 lpfc_cq_destroy(phba, phba->sli4_hba.nvme_cq[qidx]);
8971
James Smart2d7dbc42017-02-12 13:52:35 -08008972 /* Unset NVMET MRQ queue */
8973 if (phba->sli4_hba.nvmet_mrq_hdr) {
8974 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
8975 lpfc_rq_destroy(phba,
8976 phba->sli4_hba.nvmet_mrq_hdr[qidx],
8977 phba->sli4_hba.nvmet_mrq_data[qidx]);
8978 }
8979
8980 /* Unset NVMET CQ Set complete queue */
8981 if (phba->sli4_hba.nvmet_cqset) {
8982 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
8983 lpfc_cq_destroy(phba,
8984 phba->sli4_hba.nvmet_cqset[qidx]);
8985 }
8986
James Smartda0436e2009-05-22 14:51:39 -04008987 /* Unset FCP response complete queue */
James Smart895427b2017-02-12 13:52:30 -08008988 if (phba->sli4_hba.fcp_cq)
8989 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
8990 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[qidx]);
8991
James Smartda0436e2009-05-22 14:51:39 -04008992 /* Unset fast-path event queue */
James Smart895427b2017-02-12 13:52:30 -08008993 if (phba->sli4_hba.hba_eq)
8994 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
8995 lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[qidx]);
James Smartda0436e2009-05-22 14:51:39 -04008996}
8997
8998/**
8999 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
9000 * @phba: pointer to lpfc hba data structure.
9001 *
9002 * This routine is invoked to allocate and set up a pool of completion queue
9003 * events. The body of the completion queue event is a completion queue entry
9004 * CQE. For now, this pool is used for the interrupt service routine to queue
9005 * the following HBA completion queue events for the worker thread to process:
9006 * - Mailbox asynchronous events
9007 * - Receive queue completion unsolicited events
9008 * Later, this can be used for all the slow-path events.
9009 *
9010 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009011 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009012 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04009013 **/
9014static int
9015lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
9016{
9017 struct lpfc_cq_event *cq_event;
9018 int i;
9019
9020 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
9021 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
9022 if (!cq_event)
9023 goto out_pool_create_fail;
9024 list_add_tail(&cq_event->list,
9025 &phba->sli4_hba.sp_cqe_event_pool);
9026 }
9027 return 0;
9028
9029out_pool_create_fail:
9030 lpfc_sli4_cq_event_pool_destroy(phba);
9031 return -ENOMEM;
9032}
9033
9034/**
9035 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
9036 * @phba: pointer to lpfc hba data structure.
9037 *
9038 * This routine is invoked to free the pool of completion queue events at
9039 * driver unload time. Note that, it is the responsibility of the driver
9040 * cleanup routine to free all the outstanding completion-queue events
9041 * allocated from this pool back into the pool before invoking this routine
9042 * to destroy the pool.
9043 **/
9044static void
9045lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
9046{
9047 struct lpfc_cq_event *cq_event, *next_cq_event;
9048
9049 list_for_each_entry_safe(cq_event, next_cq_event,
9050 &phba->sli4_hba.sp_cqe_event_pool, list) {
9051 list_del(&cq_event->list);
9052 kfree(cq_event);
9053 }
9054}
9055
9056/**
9057 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
9058 * @phba: pointer to lpfc hba data structure.
9059 *
9060 * This routine is the lock free version of the API invoked to allocate a
9061 * completion-queue event from the free pool.
9062 *
9063 * Return: Pointer to the newly allocated completion-queue event if successful
9064 * NULL otherwise.
9065 **/
9066struct lpfc_cq_event *
9067__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
9068{
9069 struct lpfc_cq_event *cq_event = NULL;
9070
9071 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
9072 struct lpfc_cq_event, list);
9073 return cq_event;
9074}
9075
9076/**
9077 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
9078 * @phba: pointer to lpfc hba data structure.
9079 *
9080 * This routine is the lock version of the API invoked to allocate a
9081 * completion-queue event from the free pool.
9082 *
9083 * Return: Pointer to the newly allocated completion-queue event if successful
9084 * NULL otherwise.
9085 **/
9086struct lpfc_cq_event *
9087lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
9088{
9089 struct lpfc_cq_event *cq_event;
9090 unsigned long iflags;
9091
9092 spin_lock_irqsave(&phba->hbalock, iflags);
9093 cq_event = __lpfc_sli4_cq_event_alloc(phba);
9094 spin_unlock_irqrestore(&phba->hbalock, iflags);
9095 return cq_event;
9096}
9097
9098/**
9099 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
9100 * @phba: pointer to lpfc hba data structure.
9101 * @cq_event: pointer to the completion queue event to be freed.
9102 *
9103 * This routine is the lock free version of the API invoked to release a
9104 * completion-queue event back into the free pool.
9105 **/
9106void
9107__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
9108 struct lpfc_cq_event *cq_event)
9109{
9110 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
9111}
9112
9113/**
9114 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
9115 * @phba: pointer to lpfc hba data structure.
9116 * @cq_event: pointer to the completion queue event to be freed.
9117 *
9118 * This routine is the lock version of the API invoked to release a
9119 * completion-queue event back into the free pool.
9120 **/
9121void
9122lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
9123 struct lpfc_cq_event *cq_event)
9124{
9125 unsigned long iflags;
9126 spin_lock_irqsave(&phba->hbalock, iflags);
9127 __lpfc_sli4_cq_event_release(phba, cq_event);
9128 spin_unlock_irqrestore(&phba->hbalock, iflags);
9129}
9130
9131/**
9132 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
9133 * @phba: pointer to lpfc hba data structure.
9134 *
9135 * This routine is to free all the pending completion-queue events to the
9136 * back into the free pool for device reset.
9137 **/
9138static void
9139lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
9140{
9141 LIST_HEAD(cqelist);
9142 struct lpfc_cq_event *cqe;
9143 unsigned long iflags;
9144
9145 /* Retrieve all the pending WCQEs from pending WCQE lists */
9146 spin_lock_irqsave(&phba->hbalock, iflags);
9147 /* Pending FCP XRI abort events */
9148 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
9149 &cqelist);
9150 /* Pending ELS XRI abort events */
9151 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
9152 &cqelist);
James Smart318083a2017-03-04 09:30:30 -08009153 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
9154 /* Pending NVME XRI abort events */
9155 list_splice_init(&phba->sli4_hba.sp_nvme_xri_aborted_work_queue,
9156 &cqelist);
9157 }
James Smartda0436e2009-05-22 14:51:39 -04009158 /* Pending asynnc events */
9159 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
9160 &cqelist);
9161 spin_unlock_irqrestore(&phba->hbalock, iflags);
9162
9163 while (!list_empty(&cqelist)) {
9164 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
9165 lpfc_sli4_cq_event_release(phba, cqe);
9166 }
9167}
9168
9169/**
9170 * lpfc_pci_function_reset - Reset pci function.
9171 * @phba: pointer to lpfc hba data structure.
9172 *
9173 * This routine is invoked to request a PCI function reset. It will destroys
9174 * all resources assigned to the PCI function which originates this request.
9175 *
9176 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009177 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009178 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04009179 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04009180 **/
9181int
9182lpfc_pci_function_reset(struct lpfc_hba *phba)
9183{
9184 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05009185 uint32_t rc = 0, if_type;
James Smartda0436e2009-05-22 14:51:39 -04009186 uint32_t shdr_status, shdr_add_status;
James Smart2f6fa2c2014-09-03 12:57:08 -04009187 uint32_t rdy_chk;
9188 uint32_t port_reset = 0;
James Smartda0436e2009-05-22 14:51:39 -04009189 union lpfc_sli4_cfg_shdr *shdr;
James Smart2fcee4b2010-12-15 17:57:46 -05009190 struct lpfc_register reg_data;
James Smart2b81f942012-03-01 22:37:18 -05009191 uint16_t devid;
James Smartda0436e2009-05-22 14:51:39 -04009192
James Smart2fcee4b2010-12-15 17:57:46 -05009193 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9194 switch (if_type) {
9195 case LPFC_SLI_INTF_IF_TYPE_0:
9196 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
9197 GFP_KERNEL);
9198 if (!mboxq) {
9199 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9200 "0494 Unable to allocate memory for "
9201 "issuing SLI_FUNCTION_RESET mailbox "
9202 "command\n");
9203 return -ENOMEM;
9204 }
9205
9206 /* Setup PCI function reset mailbox-ioctl command */
9207 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
9208 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
9209 LPFC_SLI4_MBX_EMBED);
9210 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9211 shdr = (union lpfc_sli4_cfg_shdr *)
9212 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
9213 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9214 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
9215 &shdr->response);
9216 if (rc != MBX_TIMEOUT)
9217 mempool_free(mboxq, phba->mbox_mem_pool);
9218 if (shdr_status || shdr_add_status || rc) {
9219 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9220 "0495 SLI_FUNCTION_RESET mailbox "
9221 "failed with status x%x add_status x%x,"
9222 " mbx status x%x\n",
9223 shdr_status, shdr_add_status, rc);
9224 rc = -ENXIO;
9225 }
9226 break;
9227 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart2f6fa2c2014-09-03 12:57:08 -04009228wait:
9229 /*
9230 * Poll the Port Status Register and wait for RDY for
9231 * up to 30 seconds. If the port doesn't respond, treat
9232 * it as an error.
9233 */
James Smart77d093f2015-04-07 15:07:08 -04009234 for (rdy_chk = 0; rdy_chk < 1500; rdy_chk++) {
James Smart2f6fa2c2014-09-03 12:57:08 -04009235 if (lpfc_readl(phba->sli4_hba.u.if_type2.
9236 STATUSregaddr, &reg_data.word0)) {
9237 rc = -ENODEV;
9238 goto out;
9239 }
9240 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
9241 break;
9242 msleep(20);
9243 }
9244
9245 if (!bf_get(lpfc_sliport_status_rdy, &reg_data)) {
9246 phba->work_status[0] = readl(
9247 phba->sli4_hba.u.if_type2.ERR1regaddr);
9248 phba->work_status[1] = readl(
9249 phba->sli4_hba.u.if_type2.ERR2regaddr);
9250 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9251 "2890 Port not ready, port status reg "
9252 "0x%x error 1=0x%x, error 2=0x%x\n",
9253 reg_data.word0,
9254 phba->work_status[0],
9255 phba->work_status[1]);
9256 rc = -ENODEV;
9257 goto out;
9258 }
9259
9260 if (!port_reset) {
9261 /*
9262 * Reset the port now
9263 */
James Smart2fcee4b2010-12-15 17:57:46 -05009264 reg_data.word0 = 0;
9265 bf_set(lpfc_sliport_ctrl_end, &reg_data,
9266 LPFC_SLIPORT_LITTLE_ENDIAN);
9267 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
9268 LPFC_SLIPORT_INIT_PORT);
9269 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
9270 CTRLregaddr);
James Smart8fcb8ac2012-03-01 22:35:58 -05009271 /* flush */
James Smart2b81f942012-03-01 22:37:18 -05009272 pci_read_config_word(phba->pcidev,
9273 PCI_DEVICE_ID, &devid);
James Smart2fcee4b2010-12-15 17:57:46 -05009274
James Smart2f6fa2c2014-09-03 12:57:08 -04009275 port_reset = 1;
9276 msleep(20);
9277 goto wait;
9278 } else if (bf_get(lpfc_sliport_status_rn, &reg_data)) {
9279 rc = -ENODEV;
9280 goto out;
James Smart2fcee4b2010-12-15 17:57:46 -05009281 }
9282 break;
James Smart2f6fa2c2014-09-03 12:57:08 -04009283
James Smart2fcee4b2010-12-15 17:57:46 -05009284 case LPFC_SLI_INTF_IF_TYPE_1:
9285 default:
9286 break;
James Smartda0436e2009-05-22 14:51:39 -04009287 }
9288
James Smart73d91e52011-10-10 21:32:10 -04009289out:
James Smart2fcee4b2010-12-15 17:57:46 -05009290 /* Catch the not-ready port failure after a port reset. */
James Smart2f6fa2c2014-09-03 12:57:08 -04009291 if (rc) {
James Smart229adb02013-04-17 20:16:51 -04009292 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9293 "3317 HBA not functional: IP Reset Failed "
James Smart2f6fa2c2014-09-03 12:57:08 -04009294 "try: echo fw_reset > board_mode\n");
James Smart2fcee4b2010-12-15 17:57:46 -05009295 rc = -ENODEV;
James Smart229adb02013-04-17 20:16:51 -04009296 }
James Smart2fcee4b2010-12-15 17:57:46 -05009297
James Smartda0436e2009-05-22 14:51:39 -04009298 return rc;
9299}
9300
9301/**
James Smartda0436e2009-05-22 14:51:39 -04009302 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
9303 * @phba: pointer to lpfc hba data structure.
9304 *
9305 * This routine is invoked to set up the PCI device memory space for device
9306 * with SLI-4 interface spec.
9307 *
9308 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009309 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04009310 * other values - error
9311 **/
9312static int
9313lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
9314{
9315 struct pci_dev *pdev;
9316 unsigned long bar0map_len, bar1map_len, bar2map_len;
9317 int error = -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -05009318 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04009319
9320 /* Obtain PCI device reference */
9321 if (!phba->pcidev)
9322 return error;
9323 else
9324 pdev = phba->pcidev;
9325
9326 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05009327 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
9328 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
9329 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
9330 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smartda0436e2009-05-22 14:51:39 -04009331 return error;
Michael Reed8e685972009-09-18 12:02:05 -05009332 }
9333 }
James Smartda0436e2009-05-22 14:51:39 -04009334
James Smart2fcee4b2010-12-15 17:57:46 -05009335 /*
9336 * The BARs and register set definitions and offset locations are
9337 * dependent on the if_type.
9338 */
9339 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
9340 &phba->sli4_hba.sli_intf.word0)) {
9341 return error;
9342 }
9343
9344 /* There is no SLI3 failback for SLI4 devices. */
9345 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
9346 LPFC_SLI_INTF_VALID) {
9347 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9348 "2894 SLI_INTF reg contents invalid "
9349 "sli_intf reg 0x%x\n",
9350 phba->sli4_hba.sli_intf.word0);
9351 return error;
9352 }
9353
9354 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9355 /*
9356 * Get the bus address of SLI4 device Bar regions and the
9357 * number of bytes required by each mapping. The mapping of the
9358 * particular PCI BARs regions is dependent on the type of
9359 * SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -04009360 */
James Smartf5ca6f22013-09-06 12:21:09 -04009361 if (pci_resource_start(pdev, PCI_64BIT_BAR0)) {
9362 phba->pci_bar0_map = pci_resource_start(pdev, PCI_64BIT_BAR0);
9363 bar0map_len = pci_resource_len(pdev, PCI_64BIT_BAR0);
James Smart2fcee4b2010-12-15 17:57:46 -05009364
9365 /*
9366 * Map SLI4 PCI Config Space Register base to a kernel virtual
9367 * addr
9368 */
9369 phba->sli4_hba.conf_regs_memmap_p =
9370 ioremap(phba->pci_bar0_map, bar0map_len);
9371 if (!phba->sli4_hba.conf_regs_memmap_p) {
9372 dev_printk(KERN_ERR, &pdev->dev,
9373 "ioremap failed for SLI4 PCI config "
9374 "registers.\n");
9375 goto out;
9376 }
James Smartf5ca6f22013-09-06 12:21:09 -04009377 phba->pci_bar0_memmap_p = phba->sli4_hba.conf_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05009378 /* Set up BAR0 PCI config space register memory map */
9379 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smart1dfb5a42010-02-12 14:40:50 -05009380 } else {
9381 phba->pci_bar0_map = pci_resource_start(pdev, 1);
9382 bar0map_len = pci_resource_len(pdev, 1);
James Smart2fcee4b2010-12-15 17:57:46 -05009383 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
9384 dev_printk(KERN_ERR, &pdev->dev,
9385 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
9386 goto out;
9387 }
9388 phba->sli4_hba.conf_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04009389 ioremap(phba->pci_bar0_map, bar0map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05009390 if (!phba->sli4_hba.conf_regs_memmap_p) {
9391 dev_printk(KERN_ERR, &pdev->dev,
9392 "ioremap failed for SLI4 PCI config "
9393 "registers.\n");
9394 goto out;
9395 }
9396 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smartda0436e2009-05-22 14:51:39 -04009397 }
9398
James Smartc31098c2011-04-16 11:03:33 -04009399 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
James Smartf5ca6f22013-09-06 12:21:09 -04009400 (pci_resource_start(pdev, PCI_64BIT_BAR2))) {
James Smart2fcee4b2010-12-15 17:57:46 -05009401 /*
9402 * Map SLI4 if type 0 HBA Control Register base to a kernel
9403 * virtual address and setup the registers.
9404 */
James Smartf5ca6f22013-09-06 12:21:09 -04009405 phba->pci_bar1_map = pci_resource_start(pdev, PCI_64BIT_BAR2);
9406 bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
James Smart2fcee4b2010-12-15 17:57:46 -05009407 phba->sli4_hba.ctrl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04009408 ioremap(phba->pci_bar1_map, bar1map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05009409 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
9410 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04009411 "ioremap failed for SLI4 HBA control registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05009412 goto out_iounmap_conf;
9413 }
James Smartf5ca6f22013-09-06 12:21:09 -04009414 phba->pci_bar2_memmap_p = phba->sli4_hba.ctrl_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05009415 lpfc_sli4_bar1_register_memmap(phba);
James Smartda0436e2009-05-22 14:51:39 -04009416 }
9417
James Smartc31098c2011-04-16 11:03:33 -04009418 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
James Smartf5ca6f22013-09-06 12:21:09 -04009419 (pci_resource_start(pdev, PCI_64BIT_BAR4))) {
James Smart2fcee4b2010-12-15 17:57:46 -05009420 /*
9421 * Map SLI4 if type 0 HBA Doorbell Register base to a kernel
9422 * virtual address and setup the registers.
9423 */
James Smartf5ca6f22013-09-06 12:21:09 -04009424 phba->pci_bar2_map = pci_resource_start(pdev, PCI_64BIT_BAR4);
9425 bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
James Smart2fcee4b2010-12-15 17:57:46 -05009426 phba->sli4_hba.drbl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04009427 ioremap(phba->pci_bar2_map, bar2map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05009428 if (!phba->sli4_hba.drbl_regs_memmap_p) {
9429 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04009430 "ioremap failed for SLI4 HBA doorbell registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05009431 goto out_iounmap_ctrl;
9432 }
James Smartf5ca6f22013-09-06 12:21:09 -04009433 phba->pci_bar4_memmap_p = phba->sli4_hba.drbl_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05009434 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
9435 if (error)
9436 goto out_iounmap_all;
James Smartda0436e2009-05-22 14:51:39 -04009437 }
9438
James Smartda0436e2009-05-22 14:51:39 -04009439 return 0;
9440
9441out_iounmap_all:
9442 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
9443out_iounmap_ctrl:
9444 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
9445out_iounmap_conf:
9446 iounmap(phba->sli4_hba.conf_regs_memmap_p);
9447out:
9448 return error;
9449}
9450
9451/**
9452 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
9453 * @phba: pointer to lpfc hba data structure.
9454 *
9455 * This routine is invoked to unset the PCI device memory space for device
9456 * with SLI-4 interface spec.
9457 **/
9458static void
9459lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
9460{
James Smart2e90f4b2011-12-13 13:22:37 -05009461 uint32_t if_type;
9462 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smartda0436e2009-05-22 14:51:39 -04009463
James Smart2e90f4b2011-12-13 13:22:37 -05009464 switch (if_type) {
9465 case LPFC_SLI_INTF_IF_TYPE_0:
9466 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
9467 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
9468 iounmap(phba->sli4_hba.conf_regs_memmap_p);
9469 break;
9470 case LPFC_SLI_INTF_IF_TYPE_2:
9471 iounmap(phba->sli4_hba.conf_regs_memmap_p);
9472 break;
9473 case LPFC_SLI_INTF_IF_TYPE_1:
9474 default:
9475 dev_printk(KERN_ERR, &phba->pcidev->dev,
9476 "FATAL - unsupported SLI4 interface type - %d\n",
9477 if_type);
9478 break;
9479 }
James Smartda0436e2009-05-22 14:51:39 -04009480}
9481
9482/**
James Smart3772a992009-05-22 14:50:54 -04009483 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
9484 * @phba: pointer to lpfc hba data structure.
9485 *
9486 * This routine is invoked to enable the MSI-X interrupt vectors to device
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009487 * with SLI-3 interface specs.
James Smart3772a992009-05-22 14:50:54 -04009488 *
9489 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009490 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04009491 * other values - error
9492 **/
9493static int
9494lpfc_sli_enable_msix(struct lpfc_hba *phba)
9495{
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009496 int rc;
James Smart3772a992009-05-22 14:50:54 -04009497 LPFC_MBOXQ_t *pmb;
9498
9499 /* Set up MSI-X multi-message vectors */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009500 rc = pci_alloc_irq_vectors(phba->pcidev,
9501 LPFC_MSIX_VECTORS, LPFC_MSIX_VECTORS, PCI_IRQ_MSIX);
9502 if (rc < 0) {
James Smart3772a992009-05-22 14:50:54 -04009503 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9504 "0420 PCI enable MSI-X failed (%d)\n", rc);
Alexander Gordeev029165a2014-07-16 20:05:15 +02009505 goto vec_fail_out;
James Smart3772a992009-05-22 14:50:54 -04009506 }
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009507
James Smart3772a992009-05-22 14:50:54 -04009508 /*
9509 * Assign MSI-X vectors to interrupt handlers
9510 */
9511
9512 /* vector-0 is associated to slow-path handler */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009513 rc = request_irq(pci_irq_vector(phba->pcidev, 0),
James Smarted243d32015-05-21 13:55:25 -04009514 &lpfc_sli_sp_intr_handler, 0,
James Smart3772a992009-05-22 14:50:54 -04009515 LPFC_SP_DRIVER_HANDLER_NAME, phba);
9516 if (rc) {
9517 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9518 "0421 MSI-X slow-path request_irq failed "
9519 "(%d)\n", rc);
9520 goto msi_fail_out;
9521 }
9522
9523 /* vector-1 is associated to fast-path handler */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009524 rc = request_irq(pci_irq_vector(phba->pcidev, 1),
James Smarted243d32015-05-21 13:55:25 -04009525 &lpfc_sli_fp_intr_handler, 0,
James Smart3772a992009-05-22 14:50:54 -04009526 LPFC_FP_DRIVER_HANDLER_NAME, phba);
9527
9528 if (rc) {
9529 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9530 "0429 MSI-X fast-path request_irq failed "
9531 "(%d)\n", rc);
9532 goto irq_fail_out;
9533 }
9534
9535 /*
9536 * Configure HBA MSI-X attention conditions to messages
9537 */
9538 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9539
9540 if (!pmb) {
9541 rc = -ENOMEM;
9542 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9543 "0474 Unable to allocate memory for issuing "
9544 "MBOX_CONFIG_MSI command\n");
9545 goto mem_fail_out;
9546 }
9547 rc = lpfc_config_msi(phba, pmb);
9548 if (rc)
9549 goto mbx_fail_out;
9550 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
9551 if (rc != MBX_SUCCESS) {
9552 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
9553 "0351 Config MSI mailbox command failed, "
9554 "mbxCmd x%x, mbxStatus x%x\n",
9555 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
9556 goto mbx_fail_out;
9557 }
9558
9559 /* Free memory allocated for mailbox command */
9560 mempool_free(pmb, phba->mbox_mem_pool);
9561 return rc;
9562
9563mbx_fail_out:
9564 /* Free memory allocated for mailbox command */
9565 mempool_free(pmb, phba->mbox_mem_pool);
9566
9567mem_fail_out:
9568 /* free the irq already requested */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009569 free_irq(pci_irq_vector(phba->pcidev, 1), phba);
James Smart3772a992009-05-22 14:50:54 -04009570
9571irq_fail_out:
9572 /* free the irq already requested */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009573 free_irq(pci_irq_vector(phba->pcidev, 0), phba);
James Smart3772a992009-05-22 14:50:54 -04009574
9575msi_fail_out:
9576 /* Unconfigure MSI-X capability structure */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009577 pci_free_irq_vectors(phba->pcidev);
Alexander Gordeev029165a2014-07-16 20:05:15 +02009578
9579vec_fail_out:
James Smart3772a992009-05-22 14:50:54 -04009580 return rc;
9581}
9582
9583/**
James Smart3772a992009-05-22 14:50:54 -04009584 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
9585 * @phba: pointer to lpfc hba data structure.
9586 *
9587 * This routine is invoked to enable the MSI interrupt mode to device with
9588 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
9589 * enable the MSI vector. The device driver is responsible for calling the
9590 * request_irq() to register MSI vector with a interrupt the handler, which
9591 * is done in this function.
9592 *
9593 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009594 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04009595 * other values - error
9596 */
9597static int
9598lpfc_sli_enable_msi(struct lpfc_hba *phba)
9599{
9600 int rc;
9601
9602 rc = pci_enable_msi(phba->pcidev);
9603 if (!rc)
9604 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9605 "0462 PCI enable MSI mode success.\n");
9606 else {
9607 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9608 "0471 PCI enable MSI mode failed (%d)\n", rc);
9609 return rc;
9610 }
9611
9612 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
James Smarted243d32015-05-21 13:55:25 -04009613 0, LPFC_DRIVER_NAME, phba);
James Smart3772a992009-05-22 14:50:54 -04009614 if (rc) {
9615 pci_disable_msi(phba->pcidev);
9616 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9617 "0478 MSI request_irq failed (%d)\n", rc);
9618 }
9619 return rc;
9620}
9621
9622/**
James Smart3772a992009-05-22 14:50:54 -04009623 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
9624 * @phba: pointer to lpfc hba data structure.
9625 *
9626 * This routine is invoked to enable device interrupt and associate driver's
9627 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
9628 * spec. Depends on the interrupt mode configured to the driver, the driver
9629 * will try to fallback from the configured interrupt mode to an interrupt
9630 * mode which is supported by the platform, kernel, and device in the order
9631 * of:
9632 * MSI-X -> MSI -> IRQ.
9633 *
9634 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009635 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04009636 * other values - error
9637 **/
9638static uint32_t
9639lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
9640{
9641 uint32_t intr_mode = LPFC_INTR_ERROR;
9642 int retval;
9643
9644 if (cfg_mode == 2) {
9645 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
9646 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
9647 if (!retval) {
9648 /* Now, try to enable MSI-X interrupt mode */
9649 retval = lpfc_sli_enable_msix(phba);
9650 if (!retval) {
9651 /* Indicate initialization to MSI-X mode */
9652 phba->intr_type = MSIX;
9653 intr_mode = 2;
9654 }
9655 }
9656 }
9657
9658 /* Fallback to MSI if MSI-X initialization failed */
9659 if (cfg_mode >= 1 && phba->intr_type == NONE) {
9660 retval = lpfc_sli_enable_msi(phba);
9661 if (!retval) {
9662 /* Indicate initialization to MSI mode */
9663 phba->intr_type = MSI;
9664 intr_mode = 1;
9665 }
9666 }
9667
9668 /* Fallback to INTx if both MSI-X/MSI initalization failed */
9669 if (phba->intr_type == NONE) {
9670 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
9671 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
9672 if (!retval) {
9673 /* Indicate initialization to INTx mode */
9674 phba->intr_type = INTx;
9675 intr_mode = 0;
9676 }
9677 }
9678 return intr_mode;
9679}
9680
9681/**
9682 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
9683 * @phba: pointer to lpfc hba data structure.
9684 *
9685 * This routine is invoked to disable device interrupt and disassociate the
9686 * driver's interrupt handler(s) from interrupt vector(s) to device with
9687 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
9688 * release the interrupt vector(s) for the message signaled interrupt.
9689 **/
9690static void
9691lpfc_sli_disable_intr(struct lpfc_hba *phba)
9692{
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009693 int nr_irqs, i;
9694
James Smart3772a992009-05-22 14:50:54 -04009695 if (phba->intr_type == MSIX)
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009696 nr_irqs = LPFC_MSIX_VECTORS;
9697 else
9698 nr_irqs = 1;
9699
9700 for (i = 0; i < nr_irqs; i++)
9701 free_irq(pci_irq_vector(phba->pcidev, i), phba);
9702 pci_free_irq_vectors(phba->pcidev);
James Smart3772a992009-05-22 14:50:54 -04009703
9704 /* Reset interrupt management states */
9705 phba->intr_type = NONE;
9706 phba->sli.slistat.sli_intr = 0;
James Smart3772a992009-05-22 14:50:54 -04009707}
9708
9709/**
James Smart895427b2017-02-12 13:52:30 -08009710 * lpfc_cpu_affinity_check - Check vector CPU affinity mappings
James Smart7bb03bb2013-04-17 20:19:16 -04009711 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -08009712 * @vectors: number of msix vectors allocated.
James Smart7bb03bb2013-04-17 20:19:16 -04009713 *
James Smart895427b2017-02-12 13:52:30 -08009714 * The routine will figure out the CPU affinity assignment for every
9715 * MSI-X vector allocated for the HBA. The hba_eq_hdl will be updated
9716 * with a pointer to the CPU mask that defines ALL the CPUs this vector
9717 * can be associated with. If the vector can be unquely associated with
9718 * a single CPU, that CPU will be recorded in hba_eq_hdl[index].cpu.
9719 * In addition, the CPU to IO channel mapping will be calculated
9720 * and the phba->sli4_hba.cpu_map array will reflect this.
James Smart7bb03bb2013-04-17 20:19:16 -04009721 */
James Smart895427b2017-02-12 13:52:30 -08009722static void
9723lpfc_cpu_affinity_check(struct lpfc_hba *phba, int vectors)
James Smart7bb03bb2013-04-17 20:19:16 -04009724{
9725 struct lpfc_vector_map_info *cpup;
James Smart895427b2017-02-12 13:52:30 -08009726 int index = 0;
9727 int vec = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04009728 int cpu;
James Smart7bb03bb2013-04-17 20:19:16 -04009729#ifdef CONFIG_X86
9730 struct cpuinfo_x86 *cpuinfo;
9731#endif
James Smart7bb03bb2013-04-17 20:19:16 -04009732
9733 /* Init cpu_map array */
9734 memset(phba->sli4_hba.cpu_map, 0xff,
9735 (sizeof(struct lpfc_vector_map_info) *
James Smart895427b2017-02-12 13:52:30 -08009736 phba->sli4_hba.num_present_cpu));
James Smart7bb03bb2013-04-17 20:19:16 -04009737
9738 /* Update CPU map with physical id and core id of each CPU */
9739 cpup = phba->sli4_hba.cpu_map;
9740 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
9741#ifdef CONFIG_X86
9742 cpuinfo = &cpu_data(cpu);
9743 cpup->phys_id = cpuinfo->phys_proc_id;
9744 cpup->core_id = cpuinfo->cpu_core_id;
9745#else
9746 /* No distinction between CPUs for other platforms */
9747 cpup->phys_id = 0;
9748 cpup->core_id = 0;
9749#endif
James Smart895427b2017-02-12 13:52:30 -08009750 cpup->channel_id = index; /* For now round robin */
9751 cpup->irq = pci_irq_vector(phba->pcidev, vec);
9752 vec++;
9753 if (vec >= vectors)
9754 vec = 0;
9755 index++;
9756 if (index >= phba->cfg_fcp_io_channel)
9757 index = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04009758 cpup++;
9759 }
James Smart7bb03bb2013-04-17 20:19:16 -04009760}
9761
9762
9763/**
James Smartda0436e2009-05-22 14:51:39 -04009764 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
9765 * @phba: pointer to lpfc hba data structure.
9766 *
9767 * This routine is invoked to enable the MSI-X interrupt vectors to device
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009768 * with SLI-4 interface spec.
James Smartda0436e2009-05-22 14:51:39 -04009769 *
9770 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009771 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04009772 * other values - error
9773 **/
9774static int
9775lpfc_sli4_enable_msix(struct lpfc_hba *phba)
9776{
James Smart75baf692010-06-08 18:31:21 -04009777 int vectors, rc, index;
James Smartb83d0052017-06-01 21:07:05 -07009778 char *name;
James Smartda0436e2009-05-22 14:51:39 -04009779
9780 /* Set up MSI-X multi-message vectors */
James Smart895427b2017-02-12 13:52:30 -08009781 vectors = phba->io_channel_irqs;
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009782 if (phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05009783 vectors++;
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009784
James Smartf358dd02017-02-12 13:52:34 -08009785 rc = pci_alloc_irq_vectors(phba->pcidev,
9786 (phba->nvmet_support) ? 1 : 2,
9787 vectors, PCI_IRQ_MSIX | PCI_IRQ_AFFINITY);
Alexander Gordeev4f871e12014-09-03 12:56:29 -04009788 if (rc < 0) {
James Smartda0436e2009-05-22 14:51:39 -04009789 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9790 "0484 PCI enable MSI-X failed (%d)\n", rc);
Alexander Gordeev029165a2014-07-16 20:05:15 +02009791 goto vec_fail_out;
James Smartda0436e2009-05-22 14:51:39 -04009792 }
Alexander Gordeev4f871e12014-09-03 12:56:29 -04009793 vectors = rc;
James Smart75baf692010-06-08 18:31:21 -04009794
James Smart7bb03bb2013-04-17 20:19:16 -04009795 /* Assign MSI-X vectors to interrupt handlers */
James Smart67d12732012-08-03 12:36:13 -04009796 for (index = 0; index < vectors; index++) {
James Smartb83d0052017-06-01 21:07:05 -07009797 name = phba->sli4_hba.hba_eq_hdl[index].handler_name;
9798 memset(name, 0, LPFC_SLI4_HANDLER_NAME_SZ);
9799 snprintf(name, LPFC_SLI4_HANDLER_NAME_SZ,
James Smart4305f182012-08-03 12:36:33 -04009800 LPFC_DRIVER_HANDLER_NAME"%d", index);
James Smartda0436e2009-05-22 14:51:39 -04009801
James Smart895427b2017-02-12 13:52:30 -08009802 phba->sli4_hba.hba_eq_hdl[index].idx = index;
9803 phba->sli4_hba.hba_eq_hdl[index].phba = phba;
9804 atomic_set(&phba->sli4_hba.hba_eq_hdl[index].hba_eq_in_use, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009805 if (phba->cfg_fof && (index == (vectors - 1)))
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009806 rc = request_irq(pci_irq_vector(phba->pcidev, index),
James Smarted243d32015-05-21 13:55:25 -04009807 &lpfc_sli4_fof_intr_handler, 0,
James Smartb83d0052017-06-01 21:07:05 -07009808 name,
James Smart895427b2017-02-12 13:52:30 -08009809 &phba->sli4_hba.hba_eq_hdl[index]);
James Smart1ba981f2014-02-20 09:56:45 -05009810 else
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009811 rc = request_irq(pci_irq_vector(phba->pcidev, index),
James Smarted243d32015-05-21 13:55:25 -04009812 &lpfc_sli4_hba_intr_handler, 0,
James Smartb83d0052017-06-01 21:07:05 -07009813 name,
James Smart895427b2017-02-12 13:52:30 -08009814 &phba->sli4_hba.hba_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -04009815 if (rc) {
9816 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9817 "0486 MSI-X fast-path (%d) "
9818 "request_irq failed (%d)\n", index, rc);
9819 goto cfg_fail_out;
9820 }
9821 }
9822
James Smart1ba981f2014-02-20 09:56:45 -05009823 if (phba->cfg_fof)
9824 vectors--;
9825
James Smart895427b2017-02-12 13:52:30 -08009826 if (vectors != phba->io_channel_irqs) {
James Smart82c3e9b2012-09-29 11:29:50 -04009827 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9828 "3238 Reducing IO channels to match number of "
9829 "MSI-X vectors, requested %d got %d\n",
James Smart895427b2017-02-12 13:52:30 -08009830 phba->io_channel_irqs, vectors);
9831 if (phba->cfg_fcp_io_channel > vectors)
9832 phba->cfg_fcp_io_channel = vectors;
9833 if (phba->cfg_nvme_io_channel > vectors)
9834 phba->cfg_nvme_io_channel = vectors;
9835 if (phba->cfg_fcp_io_channel > phba->cfg_nvme_io_channel)
9836 phba->io_channel_irqs = phba->cfg_fcp_io_channel;
9837 else
9838 phba->io_channel_irqs = phba->cfg_nvme_io_channel;
James Smart82c3e9b2012-09-29 11:29:50 -04009839 }
James Smart895427b2017-02-12 13:52:30 -08009840 lpfc_cpu_affinity_check(phba, vectors);
James Smart7bb03bb2013-04-17 20:19:16 -04009841
James Smartda0436e2009-05-22 14:51:39 -04009842 return rc;
9843
9844cfg_fail_out:
9845 /* free the irq already requested */
James Smart895427b2017-02-12 13:52:30 -08009846 for (--index; index >= 0; index--)
9847 free_irq(pci_irq_vector(phba->pcidev, index),
9848 &phba->sli4_hba.hba_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -04009849
James Smartda0436e2009-05-22 14:51:39 -04009850 /* Unconfigure MSI-X capability structure */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009851 pci_free_irq_vectors(phba->pcidev);
Alexander Gordeev029165a2014-07-16 20:05:15 +02009852
9853vec_fail_out:
James Smartda0436e2009-05-22 14:51:39 -04009854 return rc;
9855}
9856
9857/**
James Smartda0436e2009-05-22 14:51:39 -04009858 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
9859 * @phba: pointer to lpfc hba data structure.
9860 *
9861 * This routine is invoked to enable the MSI interrupt mode to device with
9862 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
9863 * to enable the MSI vector. The device driver is responsible for calling
9864 * the request_irq() to register MSI vector with a interrupt the handler,
9865 * which is done in this function.
9866 *
9867 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009868 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04009869 * other values - error
9870 **/
9871static int
9872lpfc_sli4_enable_msi(struct lpfc_hba *phba)
9873{
9874 int rc, index;
9875
9876 rc = pci_enable_msi(phba->pcidev);
9877 if (!rc)
9878 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9879 "0487 PCI enable MSI mode success.\n");
9880 else {
9881 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9882 "0488 PCI enable MSI mode failed (%d)\n", rc);
9883 return rc;
9884 }
9885
9886 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
James Smarted243d32015-05-21 13:55:25 -04009887 0, LPFC_DRIVER_NAME, phba);
James Smartda0436e2009-05-22 14:51:39 -04009888 if (rc) {
9889 pci_disable_msi(phba->pcidev);
9890 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9891 "0490 MSI request_irq failed (%d)\n", rc);
James Smart75baf692010-06-08 18:31:21 -04009892 return rc;
James Smartda0436e2009-05-22 14:51:39 -04009893 }
9894
James Smart895427b2017-02-12 13:52:30 -08009895 for (index = 0; index < phba->io_channel_irqs; index++) {
9896 phba->sli4_hba.hba_eq_hdl[index].idx = index;
9897 phba->sli4_hba.hba_eq_hdl[index].phba = phba;
James Smartda0436e2009-05-22 14:51:39 -04009898 }
9899
James Smart1ba981f2014-02-20 09:56:45 -05009900 if (phba->cfg_fof) {
James Smart895427b2017-02-12 13:52:30 -08009901 phba->sli4_hba.hba_eq_hdl[index].idx = index;
9902 phba->sli4_hba.hba_eq_hdl[index].phba = phba;
James Smart1ba981f2014-02-20 09:56:45 -05009903 }
James Smart75baf692010-06-08 18:31:21 -04009904 return 0;
James Smartda0436e2009-05-22 14:51:39 -04009905}
9906
9907/**
James Smartda0436e2009-05-22 14:51:39 -04009908 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
9909 * @phba: pointer to lpfc hba data structure.
9910 *
9911 * This routine is invoked to enable device interrupt and associate driver's
9912 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
9913 * interface spec. Depends on the interrupt mode configured to the driver,
9914 * the driver will try to fallback from the configured interrupt mode to an
9915 * interrupt mode which is supported by the platform, kernel, and device in
9916 * the order of:
9917 * MSI-X -> MSI -> IRQ.
9918 *
9919 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009920 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04009921 * other values - error
9922 **/
9923static uint32_t
9924lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
9925{
9926 uint32_t intr_mode = LPFC_INTR_ERROR;
James Smart895427b2017-02-12 13:52:30 -08009927 int retval, idx;
James Smartda0436e2009-05-22 14:51:39 -04009928
9929 if (cfg_mode == 2) {
9930 /* Preparation before conf_msi mbox cmd */
9931 retval = 0;
9932 if (!retval) {
9933 /* Now, try to enable MSI-X interrupt mode */
9934 retval = lpfc_sli4_enable_msix(phba);
9935 if (!retval) {
9936 /* Indicate initialization to MSI-X mode */
9937 phba->intr_type = MSIX;
9938 intr_mode = 2;
9939 }
9940 }
9941 }
9942
9943 /* Fallback to MSI if MSI-X initialization failed */
9944 if (cfg_mode >= 1 && phba->intr_type == NONE) {
9945 retval = lpfc_sli4_enable_msi(phba);
9946 if (!retval) {
9947 /* Indicate initialization to MSI mode */
9948 phba->intr_type = MSI;
9949 intr_mode = 1;
9950 }
9951 }
9952
9953 /* Fallback to INTx if both MSI-X/MSI initalization failed */
9954 if (phba->intr_type == NONE) {
9955 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
9956 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
9957 if (!retval) {
James Smart895427b2017-02-12 13:52:30 -08009958 struct lpfc_hba_eq_hdl *eqhdl;
9959
James Smartda0436e2009-05-22 14:51:39 -04009960 /* Indicate initialization to INTx mode */
9961 phba->intr_type = INTx;
9962 intr_mode = 0;
James Smart895427b2017-02-12 13:52:30 -08009963
9964 for (idx = 0; idx < phba->io_channel_irqs; idx++) {
9965 eqhdl = &phba->sli4_hba.hba_eq_hdl[idx];
9966 eqhdl->idx = idx;
9967 eqhdl->phba = phba;
9968 atomic_set(&eqhdl->hba_eq_in_use, 1);
James Smartda0436e2009-05-22 14:51:39 -04009969 }
James Smart1ba981f2014-02-20 09:56:45 -05009970 if (phba->cfg_fof) {
James Smart895427b2017-02-12 13:52:30 -08009971 eqhdl = &phba->sli4_hba.hba_eq_hdl[idx];
9972 eqhdl->idx = idx;
9973 eqhdl->phba = phba;
9974 atomic_set(&eqhdl->hba_eq_in_use, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009975 }
James Smartda0436e2009-05-22 14:51:39 -04009976 }
9977 }
9978 return intr_mode;
9979}
9980
9981/**
9982 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
9983 * @phba: pointer to lpfc hba data structure.
9984 *
9985 * This routine is invoked to disable device interrupt and disassociate
9986 * the driver's interrupt handler(s) from interrupt vector(s) to device
9987 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
9988 * will release the interrupt vector(s) for the message signaled interrupt.
9989 **/
9990static void
9991lpfc_sli4_disable_intr(struct lpfc_hba *phba)
9992{
9993 /* Disable the currently initialized interrupt mode */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009994 if (phba->intr_type == MSIX) {
9995 int index;
9996
9997 /* Free up MSI-X multi-message vectors */
James Smart895427b2017-02-12 13:52:30 -08009998 for (index = 0; index < phba->io_channel_irqs; index++)
9999 free_irq(pci_irq_vector(phba->pcidev, index),
10000 &phba->sli4_hba.hba_eq_hdl[index]);
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010001
10002 if (phba->cfg_fof)
James Smart895427b2017-02-12 13:52:30 -080010003 free_irq(pci_irq_vector(phba->pcidev, index),
10004 &phba->sli4_hba.hba_eq_hdl[index]);
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010005 } else {
James Smartda0436e2009-05-22 14:51:39 -040010006 free_irq(phba->pcidev->irq, phba);
Christoph Hellwig45ffac12017-02-12 13:52:26 -080010007 }
10008
10009 pci_free_irq_vectors(phba->pcidev);
James Smartda0436e2009-05-22 14:51:39 -040010010
10011 /* Reset interrupt management states */
10012 phba->intr_type = NONE;
10013 phba->sli.slistat.sli_intr = 0;
James Smartda0436e2009-05-22 14:51:39 -040010014}
10015
10016/**
James Smart3772a992009-05-22 14:50:54 -040010017 * lpfc_unset_hba - Unset SLI3 hba device initialization
10018 * @phba: pointer to lpfc hba data structure.
10019 *
10020 * This routine is invoked to unset the HBA device initialization steps to
10021 * a device with SLI-3 interface spec.
10022 **/
10023static void
10024lpfc_unset_hba(struct lpfc_hba *phba)
10025{
10026 struct lpfc_vport *vport = phba->pport;
10027 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
10028
10029 spin_lock_irq(shost->host_lock);
10030 vport->load_flag |= FC_UNLOADING;
10031 spin_unlock_irq(shost->host_lock);
10032
James Smart72859902012-01-18 16:25:38 -050010033 kfree(phba->vpi_bmask);
10034 kfree(phba->vpi_ids);
10035
James Smart3772a992009-05-22 14:50:54 -040010036 lpfc_stop_hba_timers(phba);
10037
10038 phba->pport->work_port_events = 0;
10039
10040 lpfc_sli_hba_down(phba);
10041
10042 lpfc_sli_brdrestart(phba);
10043
10044 lpfc_sli_disable_intr(phba);
10045
10046 return;
10047}
10048
10049/**
James Smart5af5eee2010-10-22 11:06:38 -040010050 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
10051 * @phba: Pointer to HBA context object.
10052 *
10053 * This function is called in the SLI4 code path to wait for completion
10054 * of device's XRIs exchange busy. It will check the XRI exchange busy
10055 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
10056 * that, it will check the XRI exchange busy on outstanding FCP and ELS
10057 * I/Os every 30 seconds, log error message, and wait forever. Only when
10058 * all XRI exchange busy complete, the driver unload shall proceed with
10059 * invoking the function reset ioctl mailbox command to the CNA and the
10060 * the rest of the driver unload resource release.
10061 **/
10062static void
10063lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
10064{
10065 int wait_time = 0;
James Smart895427b2017-02-12 13:52:30 -080010066 int nvme_xri_cmpl = 1;
James Smart86c67372017-04-21 16:05:04 -070010067 int nvmet_xri_cmpl = 1;
James Smart895427b2017-02-12 13:52:30 -080010068 int fcp_xri_cmpl = 1;
James Smart5af5eee2010-10-22 11:06:38 -040010069 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
10070
James Smart895427b2017-02-12 13:52:30 -080010071 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP)
10072 fcp_xri_cmpl =
10073 list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
James Smart86c67372017-04-21 16:05:04 -070010074 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart895427b2017-02-12 13:52:30 -080010075 nvme_xri_cmpl =
10076 list_empty(&phba->sli4_hba.lpfc_abts_nvme_buf_list);
James Smart86c67372017-04-21 16:05:04 -070010077 nvmet_xri_cmpl =
10078 list_empty(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
10079 }
James Smart895427b2017-02-12 13:52:30 -080010080
James Smartf358dd02017-02-12 13:52:34 -080010081 while (!fcp_xri_cmpl || !els_xri_cmpl || !nvme_xri_cmpl ||
10082 !nvmet_xri_cmpl) {
James Smart5af5eee2010-10-22 11:06:38 -040010083 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
James Smart895427b2017-02-12 13:52:30 -080010084 if (!nvme_xri_cmpl)
10085 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10086 "6100 NVME XRI exchange busy "
10087 "wait time: %d seconds.\n",
10088 wait_time/1000);
James Smart5af5eee2010-10-22 11:06:38 -040010089 if (!fcp_xri_cmpl)
10090 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10091 "2877 FCP XRI exchange busy "
10092 "wait time: %d seconds.\n",
10093 wait_time/1000);
10094 if (!els_xri_cmpl)
10095 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10096 "2878 ELS XRI exchange busy "
10097 "wait time: %d seconds.\n",
10098 wait_time/1000);
10099 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
10100 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
10101 } else {
10102 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
10103 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
10104 }
James Smart86c67372017-04-21 16:05:04 -070010105 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart895427b2017-02-12 13:52:30 -080010106 nvme_xri_cmpl = list_empty(
10107 &phba->sli4_hba.lpfc_abts_nvme_buf_list);
James Smart86c67372017-04-21 16:05:04 -070010108 nvmet_xri_cmpl = list_empty(
10109 &phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
10110 }
James Smart895427b2017-02-12 13:52:30 -080010111
10112 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP)
10113 fcp_xri_cmpl = list_empty(
10114 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
10115
James Smart5af5eee2010-10-22 11:06:38 -040010116 els_xri_cmpl =
10117 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smartf358dd02017-02-12 13:52:34 -080010118
James Smart5af5eee2010-10-22 11:06:38 -040010119 }
10120}
10121
10122/**
James Smartda0436e2009-05-22 14:51:39 -040010123 * lpfc_sli4_hba_unset - Unset the fcoe hba
10124 * @phba: Pointer to HBA context object.
10125 *
10126 * This function is called in the SLI4 code path to reset the HBA's FCoE
10127 * function. The caller is not required to hold any lock. This routine
10128 * issues PCI function reset mailbox command to reset the FCoE function.
10129 * At the end of the function, it calls lpfc_hba_down_post function to
10130 * free any pending commands.
10131 **/
10132static void
10133lpfc_sli4_hba_unset(struct lpfc_hba *phba)
10134{
10135 int wait_cnt = 0;
10136 LPFC_MBOXQ_t *mboxq;
James Smart912e3ac2011-05-24 11:42:11 -040010137 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -040010138
10139 lpfc_stop_hba_timers(phba);
10140 phba->sli4_hba.intr_enable = 0;
10141
10142 /*
10143 * Gracefully wait out the potential current outstanding asynchronous
10144 * mailbox command.
10145 */
10146
10147 /* First, block any pending async mailbox command from posted */
10148 spin_lock_irq(&phba->hbalock);
10149 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
10150 spin_unlock_irq(&phba->hbalock);
10151 /* Now, trying to wait it out if we can */
10152 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
10153 msleep(10);
10154 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
10155 break;
10156 }
10157 /* Forcefully release the outstanding mailbox command if timed out */
10158 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
10159 spin_lock_irq(&phba->hbalock);
10160 mboxq = phba->sli.mbox_active;
10161 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
10162 __lpfc_mbox_cmpl_put(phba, mboxq);
10163 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10164 phba->sli.mbox_active = NULL;
10165 spin_unlock_irq(&phba->hbalock);
10166 }
10167
James Smart5af5eee2010-10-22 11:06:38 -040010168 /* Abort all iocbs associated with the hba */
10169 lpfc_sli_hba_iocb_abort(phba);
10170
10171 /* Wait for completion of device XRI exchange busy */
10172 lpfc_sli4_xri_exchange_busy_wait(phba);
10173
James Smartda0436e2009-05-22 14:51:39 -040010174 /* Disable PCI subsystem interrupt */
10175 lpfc_sli4_disable_intr(phba);
10176
James Smart912e3ac2011-05-24 11:42:11 -040010177 /* Disable SR-IOV if enabled */
10178 if (phba->cfg_sriov_nr_virtfn)
10179 pci_disable_sriov(pdev);
10180
James Smartda0436e2009-05-22 14:51:39 -040010181 /* Stop kthread signal shall trigger work_done one more time */
10182 kthread_stop(phba->worker_thread);
10183
James Smartd1f525a2017-04-21 16:04:55 -070010184 /* Unset the queues shared with the hardware then release all
10185 * allocated resources.
10186 */
10187 lpfc_sli4_queue_unset(phba);
10188 lpfc_sli4_queue_destroy(phba);
10189
James Smart3677a3a2010-09-29 11:19:14 -040010190 /* Reset SLI4 HBA FCoE function */
10191 lpfc_pci_function_reset(phba);
10192
James Smartda0436e2009-05-22 14:51:39 -040010193 /* Stop the SLI4 device port */
10194 phba->pport->work_port_events = 0;
10195}
10196
James Smart28baac72010-02-12 14:42:03 -050010197 /**
10198 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
10199 * @phba: Pointer to HBA context object.
10200 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
10201 *
10202 * This function is called in the SLI4 code path to read the port's
10203 * sli4 capabilities.
10204 *
10205 * This function may be be called from any context that can block-wait
10206 * for the completion. The expectation is that this routine is called
10207 * typically from probe_one or from the online routine.
10208 **/
10209int
10210lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
10211{
10212 int rc;
10213 struct lpfc_mqe *mqe;
10214 struct lpfc_pc_sli4_params *sli4_params;
10215 uint32_t mbox_tmo;
10216
10217 rc = 0;
10218 mqe = &mboxq->u.mqe;
10219
10220 /* Read the port's SLI4 Parameters port capabilities */
James Smartfedd3b72011-02-16 12:39:24 -050010221 lpfc_pc_sli4_params(mboxq);
James Smart28baac72010-02-12 14:42:03 -050010222 if (!phba->sli4_hba.intr_enable)
10223 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
10224 else {
James Smarta183a152011-10-10 21:32:43 -040010225 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart28baac72010-02-12 14:42:03 -050010226 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
10227 }
10228
10229 if (unlikely(rc))
10230 return 1;
10231
10232 sli4_params = &phba->sli4_hba.pc_sli4_params;
10233 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
10234 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
10235 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
10236 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
10237 &mqe->un.sli4_params);
10238 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
10239 &mqe->un.sli4_params);
10240 sli4_params->proto_types = mqe->un.sli4_params.word3;
10241 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
10242 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
10243 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
10244 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
10245 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
10246 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
10247 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
10248 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
10249 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
10250 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
10251 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
10252 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
10253 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
10254 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
10255 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
10256 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
10257 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
10258 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
10259 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
10260 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
James Smart05580562011-05-24 11:40:48 -040010261
10262 /* Make sure that sge_supp_len can be handled by the driver */
10263 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
10264 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
10265
James Smart28baac72010-02-12 14:42:03 -050010266 return rc;
10267}
10268
James Smartda0436e2009-05-22 14:51:39 -040010269/**
James Smartfedd3b72011-02-16 12:39:24 -050010270 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
10271 * @phba: Pointer to HBA context object.
10272 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
10273 *
10274 * This function is called in the SLI4 code path to read the port's
10275 * sli4 capabilities.
10276 *
10277 * This function may be be called from any context that can block-wait
10278 * for the completion. The expectation is that this routine is called
10279 * typically from probe_one or from the online routine.
10280 **/
10281int
10282lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
10283{
10284 int rc;
10285 struct lpfc_mqe *mqe = &mboxq->u.mqe;
10286 struct lpfc_pc_sli4_params *sli4_params;
James Smarta183a152011-10-10 21:32:43 -040010287 uint32_t mbox_tmo;
James Smartfedd3b72011-02-16 12:39:24 -050010288 int length;
10289 struct lpfc_sli4_parameters *mbx_sli4_parameters;
10290
James Smart6d368e52011-05-24 11:44:12 -040010291 /*
10292 * By default, the driver assumes the SLI4 port requires RPI
10293 * header postings. The SLI4_PARAM response will correct this
10294 * assumption.
10295 */
10296 phba->sli4_hba.rpi_hdrs_in_use = 1;
10297
James Smartfedd3b72011-02-16 12:39:24 -050010298 /* Read the port's SLI4 Config Parameters */
10299 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
10300 sizeof(struct lpfc_sli4_cfg_mhdr));
10301 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
10302 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
10303 length, LPFC_SLI4_MBX_EMBED);
10304 if (!phba->sli4_hba.intr_enable)
10305 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -040010306 else {
10307 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
10308 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
10309 }
James Smartfedd3b72011-02-16 12:39:24 -050010310 if (unlikely(rc))
10311 return rc;
10312 sli4_params = &phba->sli4_hba.pc_sli4_params;
10313 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
10314 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
10315 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
10316 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
10317 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
10318 mbx_sli4_parameters);
10319 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
10320 mbx_sli4_parameters);
10321 if (bf_get(cfg_phwq, mbx_sli4_parameters))
10322 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
10323 else
10324 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
10325 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
10326 sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
James Smart1ba981f2014-02-20 09:56:45 -050010327 sli4_params->oas_supported = bf_get(cfg_oas, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -050010328 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
10329 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
10330 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
10331 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
James Smart0c651872013-07-15 18:33:23 -040010332 sli4_params->wqsize = bf_get(cfg_wqsize, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -050010333 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
10334 mbx_sli4_parameters);
James Smart895427b2017-02-12 13:52:30 -080010335 sli4_params->wqpcnt = bf_get(cfg_wqpcnt, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -050010336 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
10337 mbx_sli4_parameters);
James Smart6d368e52011-05-24 11:44:12 -040010338 phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
10339 phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
James Smart895427b2017-02-12 13:52:30 -080010340 phba->nvme_support = (bf_get(cfg_nvme, mbx_sli4_parameters) &&
10341 bf_get(cfg_xib, mbx_sli4_parameters));
10342
10343 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP) ||
10344 !phba->nvme_support) {
10345 phba->nvme_support = 0;
10346 phba->nvmet_support = 0;
James Smart2d7dbc42017-02-12 13:52:35 -080010347 phba->cfg_nvmet_mrq = 0;
James Smart895427b2017-02-12 13:52:30 -080010348 phba->cfg_nvme_io_channel = 0;
10349 phba->io_channel_irqs = phba->cfg_fcp_io_channel;
10350 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_NVME,
10351 "6101 Disabling NVME support: "
10352 "Not supported by firmware: %d %d\n",
10353 bf_get(cfg_nvme, mbx_sli4_parameters),
10354 bf_get(cfg_xib, mbx_sli4_parameters));
10355
10356 /* If firmware doesn't support NVME, just use SCSI support */
10357 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
10358 return -ENODEV;
10359 phba->cfg_enable_fc4_type = LPFC_ENABLE_FCP;
10360 }
James Smart05580562011-05-24 11:40:48 -040010361
James Smartf358dd02017-02-12 13:52:34 -080010362 if (bf_get(cfg_xib, mbx_sli4_parameters) && phba->cfg_suppress_rsp)
10363 phba->sli.sli_flag |= LPFC_SLI_SUPPRESS_RSP;
10364
James Smart0cf07f842017-06-01 21:07:10 -070010365 if (bf_get(cfg_eqdr, mbx_sli4_parameters))
10366 phba->sli.sli_flag |= LPFC_SLI_USE_EQDR;
10367
James Smart05580562011-05-24 11:40:48 -040010368 /* Make sure that sge_supp_len can be handled by the driver */
10369 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
10370 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
10371
James Smartb5c53952016-03-31 14:12:30 -070010372 /*
10373 * Issue IOs with CDB embedded in WQE to minimized the number
10374 * of DMAs the firmware has to do. Setting this to 1 also forces
10375 * the driver to use 128 bytes WQEs for FCP IOs.
10376 */
10377 if (bf_get(cfg_ext_embed_cb, mbx_sli4_parameters))
10378 phba->fcp_embed_io = 1;
10379 else
10380 phba->fcp_embed_io = 0;
James Smart7bdedb32016-07-06 12:36:00 -070010381
10382 /*
10383 * Check if the SLI port supports MDS Diagnostics
10384 */
10385 if (bf_get(cfg_mds_diags, mbx_sli4_parameters))
10386 phba->mds_diags_support = 1;
10387 else
10388 phba->mds_diags_support = 0;
James Smartfedd3b72011-02-16 12:39:24 -050010389 return 0;
10390}
10391
10392/**
James Smart3772a992009-05-22 14:50:54 -040010393 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
10394 * @pdev: pointer to PCI device
10395 * @pid: pointer to PCI device identifier
10396 *
10397 * This routine is to be called to attach a device with SLI-3 interface spec
10398 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
10399 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
10400 * information of the device and driver to see if the driver state that it can
10401 * support this kind of device. If the match is successful, the driver core
10402 * invokes this routine. If this routine determines it can claim the HBA, it
10403 * does all the initialization that it needs to do to handle the HBA properly.
10404 *
10405 * Return code
10406 * 0 - driver can claim the device
10407 * negative value - driver can not claim the device
10408 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010409static int
James Smart3772a992009-05-22 14:50:54 -040010410lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
10411{
10412 struct lpfc_hba *phba;
10413 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -040010414 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -040010415 int error;
10416 uint32_t cfg_mode, intr_mode;
10417
10418 /* Allocate memory for HBA structure */
10419 phba = lpfc_hba_alloc(pdev);
10420 if (!phba)
10421 return -ENOMEM;
10422
10423 /* Perform generic PCI device enabling operation */
10424 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -040010425 if (error)
James Smart3772a992009-05-22 14:50:54 -040010426 goto out_free_phba;
James Smart3772a992009-05-22 14:50:54 -040010427
10428 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
10429 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
10430 if (error)
10431 goto out_disable_pci_dev;
10432
10433 /* Set up SLI-3 specific device PCI memory space */
10434 error = lpfc_sli_pci_mem_setup(phba);
10435 if (error) {
10436 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10437 "1402 Failed to set up pci memory space.\n");
10438 goto out_disable_pci_dev;
10439 }
10440
James Smart3772a992009-05-22 14:50:54 -040010441 /* Set up SLI-3 specific device driver resources */
10442 error = lpfc_sli_driver_resource_setup(phba);
10443 if (error) {
10444 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10445 "1404 Failed to set up driver resource.\n");
10446 goto out_unset_pci_mem_s3;
10447 }
10448
10449 /* Initialize and populate the iocb list per host */
James Smartd1f525a2017-04-21 16:04:55 -070010450
James Smart3772a992009-05-22 14:50:54 -040010451 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
10452 if (error) {
10453 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10454 "1405 Failed to initialize iocb list.\n");
10455 goto out_unset_driver_resource_s3;
10456 }
10457
10458 /* Set up common device driver resources */
10459 error = lpfc_setup_driver_resource_phase2(phba);
10460 if (error) {
10461 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10462 "1406 Failed to set up driver resource.\n");
10463 goto out_free_iocb_list;
10464 }
10465
James Smart079b5c92011-08-21 21:48:49 -040010466 /* Get the default values for Model Name and Description */
10467 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
10468
James Smart3772a992009-05-22 14:50:54 -040010469 /* Create SCSI host to the physical port */
10470 error = lpfc_create_shost(phba);
10471 if (error) {
10472 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10473 "1407 Failed to create scsi host.\n");
10474 goto out_unset_driver_resource;
10475 }
10476
10477 /* Configure sysfs attributes */
10478 vport = phba->pport;
10479 error = lpfc_alloc_sysfs_attr(vport);
10480 if (error) {
10481 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10482 "1476 Failed to allocate sysfs attr\n");
10483 goto out_destroy_shost;
10484 }
10485
James Smart6669f9b2009-10-02 15:16:45 -040010486 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -040010487 /* Now, trying to enable interrupt and bring up the device */
10488 cfg_mode = phba->cfg_use_msi;
10489 while (true) {
10490 /* Put device to a known state before enabling interrupt */
10491 lpfc_stop_port(phba);
10492 /* Configure and enable interrupt */
10493 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
10494 if (intr_mode == LPFC_INTR_ERROR) {
10495 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10496 "0431 Failed to enable interrupt.\n");
10497 error = -ENODEV;
10498 goto out_free_sysfs_attr;
10499 }
10500 /* SLI-3 HBA setup */
10501 if (lpfc_sli_hba_setup(phba)) {
10502 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10503 "1477 Failed to set up hba\n");
10504 error = -ENODEV;
10505 goto out_remove_device;
10506 }
10507
10508 /* Wait 50ms for the interrupts of previous mailbox commands */
10509 msleep(50);
10510 /* Check active interrupts on message signaled interrupts */
10511 if (intr_mode == 0 ||
10512 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
10513 /* Log the current active interrupt mode */
10514 phba->intr_mode = intr_mode;
10515 lpfc_log_intr_mode(phba, intr_mode);
10516 break;
10517 } else {
10518 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10519 "0447 Configure interrupt mode (%d) "
10520 "failed active interrupt test.\n",
10521 intr_mode);
10522 /* Disable the current interrupt mode */
10523 lpfc_sli_disable_intr(phba);
10524 /* Try next level of interrupt mode */
10525 cfg_mode = --intr_mode;
10526 }
10527 }
10528
10529 /* Perform post initialization setup */
10530 lpfc_post_init_setup(phba);
10531
10532 /* Check if there are static vports to be created. */
10533 lpfc_create_static_vport(phba);
10534
10535 return 0;
10536
10537out_remove_device:
10538 lpfc_unset_hba(phba);
10539out_free_sysfs_attr:
10540 lpfc_free_sysfs_attr(vport);
10541out_destroy_shost:
10542 lpfc_destroy_shost(phba);
10543out_unset_driver_resource:
10544 lpfc_unset_driver_resource_phase2(phba);
10545out_free_iocb_list:
10546 lpfc_free_iocb_list(phba);
10547out_unset_driver_resource_s3:
10548 lpfc_sli_driver_resource_unset(phba);
10549out_unset_pci_mem_s3:
10550 lpfc_sli_pci_mem_unset(phba);
10551out_disable_pci_dev:
10552 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040010553 if (shost)
10554 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -040010555out_free_phba:
10556 lpfc_hba_free(phba);
10557 return error;
10558}
10559
10560/**
10561 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -040010562 * @pdev: pointer to PCI device
10563 *
James Smart3772a992009-05-22 14:50:54 -040010564 * This routine is to be called to disattach a device with SLI-3 interface
10565 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
10566 * removed from PCI bus, it performs all the necessary cleanup for the HBA
10567 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -040010568 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010569static void
James Smart3772a992009-05-22 14:50:54 -040010570lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -050010571{
James Smart2e0fef82007-06-17 19:56:36 -050010572 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10573 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -050010574 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -050010575 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -050010576 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -050010577
James Smart549e55c2007-08-02 11:09:51 -040010578 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -040010579 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -040010580 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010581
James Smart858c9f62007-06-17 19:56:39 -050010582 lpfc_free_sysfs_attr(vport);
10583
James Smarteada2722008-12-04 22:39:13 -050010584 /* Release all the vports against this physical port */
10585 vports = lpfc_create_vport_work_array(phba);
10586 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040010587 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
10588 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
10589 continue;
James Smarteada2722008-12-04 22:39:13 -050010590 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040010591 }
James Smarteada2722008-12-04 22:39:13 -050010592 lpfc_destroy_vport_work_array(phba, vports);
10593
10594 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -050010595 fc_remove_host(shost);
10596 scsi_remove_host(shost);
James Smartd613b6a2017-02-12 13:52:37 -080010597
James Smart87af33f2007-10-27 13:37:43 -040010598 lpfc_cleanup(vport);
10599
James Smart2e0fef82007-06-17 19:56:36 -050010600 /*
10601 * Bring down the SLI Layer. This step disable all interrupts,
10602 * clears the rings, discards all mailbox commands, and resets
10603 * the HBA.
10604 */
James Smarta257bf92009-04-06 18:48:10 -040010605
Justin P. Mattock48e34d02010-12-30 15:07:58 -080010606 /* HBA interrupt will be disabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -050010607 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -040010608 /* Stop kthread signal shall trigger work_done one more time */
10609 kthread_stop(phba->worker_thread);
10610 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -050010611 lpfc_sli_brdrestart(phba);
10612
James Smart72859902012-01-18 16:25:38 -050010613 kfree(phba->vpi_bmask);
10614 kfree(phba->vpi_ids);
10615
James Smart3772a992009-05-22 14:50:54 -040010616 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -050010617 spin_lock_irq(&phba->hbalock);
10618 list_del_init(&vport->listentry);
10619 spin_unlock_irq(&phba->hbalock);
10620
James Smart858c9f62007-06-17 19:56:39 -050010621 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -050010622
James Smart912e3ac2011-05-24 11:42:11 -040010623 /* Disable SR-IOV if enabled */
10624 if (phba->cfg_sriov_nr_virtfn)
10625 pci_disable_sriov(pdev);
10626
James Smart5b75da22008-12-04 22:39:35 -050010627 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -040010628 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -050010629
James Smart858c9f62007-06-17 19:56:39 -050010630 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -050010631
10632 /*
10633 * Call scsi_free before mem_free since scsi bufs are released to their
10634 * corresponding pools here.
10635 */
10636 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -040010637 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -050010638
James Smart34b02dc2008-08-24 21:49:55 -040010639 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
10640 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -050010641
James Smart2e0fef82007-06-17 19:56:36 -050010642 /* Free resources associated with SLI2 interface */
10643 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -040010644 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -050010645
10646 /* unmap adapter SLIM and Control Registers */
10647 iounmap(phba->ctrl_regs_memmap_p);
10648 iounmap(phba->slim_memmap_p);
10649
James Smart3772a992009-05-22 14:50:54 -040010650 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -050010651
Johannes Thumshirne0c04832016-06-07 09:44:03 +020010652 pci_release_mem_regions(pdev);
James Smart2e0fef82007-06-17 19:56:36 -050010653 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -050010654}
10655
Linas Vepstas8d63f372007-02-14 14:28:36 -060010656/**
James Smart3772a992009-05-22 14:50:54 -040010657 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -050010658 * @pdev: pointer to PCI device
10659 * @msg: power management message
10660 *
James Smart3772a992009-05-22 14:50:54 -040010661 * This routine is to be called from the kernel's PCI subsystem to support
10662 * system Power Management (PM) to device with SLI-3 interface spec. When
10663 * PM invokes this method, it quiesces the device by stopping the driver's
10664 * worker thread for the device, turning off device's interrupt and DMA,
10665 * and bring the device offline. Note that as the driver implements the
10666 * minimum PM requirements to a power-aware driver's PM support for the
10667 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
10668 * to the suspend() method call will be treated as SUSPEND and the driver will
10669 * fully reinitialize its device during resume() method call, the driver will
10670 * set device to PCI_D3hot state in PCI config space instead of setting it
10671 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -050010672 *
10673 * Return code
James Smart3772a992009-05-22 14:50:54 -040010674 * 0 - driver suspended the device
10675 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -050010676 **/
10677static int
James Smart3772a992009-05-22 14:50:54 -040010678lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -050010679{
10680 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10681 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10682
10683 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10684 "0473 PCI device Power Management suspend.\n");
10685
10686 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040010687 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart3a55b532008-12-04 22:38:54 -050010688 lpfc_offline(phba);
10689 kthread_stop(phba->worker_thread);
10690
10691 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -040010692 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -050010693
10694 /* Save device state to PCI config space */
10695 pci_save_state(pdev);
10696 pci_set_power_state(pdev, PCI_D3hot);
10697
10698 return 0;
10699}
10700
10701/**
James Smart3772a992009-05-22 14:50:54 -040010702 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -050010703 * @pdev: pointer to PCI device
10704 *
James Smart3772a992009-05-22 14:50:54 -040010705 * This routine is to be called from the kernel's PCI subsystem to support
10706 * system Power Management (PM) to device with SLI-3 interface spec. When PM
10707 * invokes this method, it restores the device's PCI config space state and
10708 * fully reinitializes the device and brings it online. Note that as the
10709 * driver implements the minimum PM requirements to a power-aware driver's
10710 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
10711 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
10712 * driver will fully reinitialize its device during resume() method call,
10713 * the device will be set to PCI_D0 directly in PCI config space before
10714 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -050010715 *
10716 * Return code
James Smart3772a992009-05-22 14:50:54 -040010717 * 0 - driver suspended the device
10718 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -050010719 **/
10720static int
James Smart3772a992009-05-22 14:50:54 -040010721lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -050010722{
10723 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10724 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -050010725 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -050010726 int error;
10727
10728 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10729 "0452 PCI device Power Management resume.\n");
10730
10731 /* Restore device state from PCI config space */
10732 pci_set_power_state(pdev, PCI_D0);
10733 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -040010734
James Smart1dfb5a42010-02-12 14:40:50 -050010735 /*
10736 * As the new kernel behavior of pci_restore_state() API call clears
10737 * device saved_state flag, need to save the restored state again.
10738 */
10739 pci_save_state(pdev);
10740
James Smart3a55b532008-12-04 22:38:54 -050010741 if (pdev->is_busmaster)
10742 pci_set_master(pdev);
10743
10744 /* Startup the kernel thread for this host adapter. */
10745 phba->worker_thread = kthread_run(lpfc_do_work, phba,
10746 "lpfc_worker_%d", phba->brd_no);
10747 if (IS_ERR(phba->worker_thread)) {
10748 error = PTR_ERR(phba->worker_thread);
10749 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10750 "0434 PM resume failed to start worker "
10751 "thread: error=x%x.\n", error);
10752 return error;
10753 }
10754
James Smart5b75da22008-12-04 22:39:35 -050010755 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -040010756 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -050010757 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -050010758 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -050010759 "0430 PM resume Failed to enable interrupt\n");
10760 return -EIO;
10761 } else
10762 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -050010763
10764 /* Restart HBA and bring it online */
10765 lpfc_sli_brdrestart(phba);
10766 lpfc_online(phba);
10767
James Smart5b75da22008-12-04 22:39:35 -050010768 /* Log the current active interrupt mode */
10769 lpfc_log_intr_mode(phba, phba->intr_mode);
10770
James Smart3a55b532008-12-04 22:38:54 -050010771 return 0;
10772}
10773
10774/**
James Smart891478a2009-11-18 15:40:23 -050010775 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
10776 * @phba: pointer to lpfc hba data structure.
10777 *
10778 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -040010779 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -050010780 **/
10781static void
10782lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
10783{
10784 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10785 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -040010786
10787 /*
10788 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
10789 * and let the SCSI mid-layer to retry them to recover.
10790 */
James Smartdb55fba2014-04-04 13:52:02 -040010791 lpfc_sli_abort_fcp_rings(phba);
James Smart891478a2009-11-18 15:40:23 -050010792}
10793
10794/**
James Smart0d878412009-10-02 15:16:56 -040010795 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
10796 * @phba: pointer to lpfc hba data structure.
10797 *
10798 * This routine is called to prepare the SLI3 device for PCI slot reset. It
10799 * disables the device interrupt and pci device, and aborts the internal FCP
10800 * pending I/Os.
10801 **/
10802static void
10803lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
10804{
James Smart0d878412009-10-02 15:16:56 -040010805 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -050010806 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -040010807
James Smart75baf692010-06-08 18:31:21 -040010808 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040010809 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040010810
James Smarte2af0d22010-03-15 11:25:32 -040010811 /* Block all SCSI devices' I/Os on the host */
10812 lpfc_scsi_dev_block(phba);
10813
James Smartea714f32013-04-17 20:18:47 -040010814 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
10815 lpfc_sli_flush_fcp_rings(phba);
10816
James Smarte2af0d22010-03-15 11:25:32 -040010817 /* stop all timers */
10818 lpfc_stop_hba_timers(phba);
10819
James Smart0d878412009-10-02 15:16:56 -040010820 /* Disable interrupt and pci device */
10821 lpfc_sli_disable_intr(phba);
10822 pci_disable_device(phba->pcidev);
James Smart0d878412009-10-02 15:16:56 -040010823}
10824
10825/**
10826 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
10827 * @phba: pointer to lpfc hba data structure.
10828 *
10829 * This routine is called to prepare the SLI3 device for PCI slot permanently
10830 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
10831 * pending I/Os.
10832 **/
10833static void
James Smart75baf692010-06-08 18:31:21 -040010834lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
James Smart0d878412009-10-02 15:16:56 -040010835{
10836 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -050010837 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -040010838 /* Block all SCSI devices' I/Os on the host */
10839 lpfc_scsi_dev_block(phba);
10840
10841 /* stop all timers */
10842 lpfc_stop_hba_timers(phba);
10843
James Smart0d878412009-10-02 15:16:56 -040010844 /* Clean up all driver's outstanding SCSI I/Os */
10845 lpfc_sli_flush_fcp_rings(phba);
10846}
10847
10848/**
James Smart3772a992009-05-22 14:50:54 -040010849 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -040010850 * @pdev: pointer to PCI device.
10851 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -060010852 *
James Smart3772a992009-05-22 14:50:54 -040010853 * This routine is called from the PCI subsystem for I/O error handling to
10854 * device with SLI-3 interface spec. This function is called by the PCI
10855 * subsystem after a PCI bus error affecting this device has been detected.
10856 * When this function is invoked, it will need to stop all the I/Os and
10857 * interrupt(s) to the device. Once that is done, it will return
10858 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
10859 * as desired.
James Smarte59058c2008-08-24 21:49:00 -040010860 *
10861 * Return codes
James Smart0d878412009-10-02 15:16:56 -040010862 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -040010863 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10864 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -040010865 **/
James Smart3772a992009-05-22 14:50:54 -040010866static pci_ers_result_t
10867lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -060010868{
James Smart51ef4c22007-08-02 11:10:31 -040010869 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10870 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060010871
James Smart0d878412009-10-02 15:16:56 -040010872 switch (state) {
10873 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -050010874 /* Non-fatal error, prepare for recovery */
10875 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -040010876 return PCI_ERS_RESULT_CAN_RECOVER;
10877 case pci_channel_io_frozen:
10878 /* Fatal error, prepare for slot reset */
10879 lpfc_sli_prep_dev_for_reset(phba);
10880 return PCI_ERS_RESULT_NEED_RESET;
10881 case pci_channel_io_perm_failure:
10882 /* Permanent failure, prepare for device down */
James Smart75baf692010-06-08 18:31:21 -040010883 lpfc_sli_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -060010884 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -040010885 default:
10886 /* Unknown state, prepare and request slot reset */
10887 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10888 "0472 Unknown PCI error state: x%x\n", state);
10889 lpfc_sli_prep_dev_for_reset(phba);
10890 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -040010891 }
Linas Vepstas8d63f372007-02-14 14:28:36 -060010892}
10893
10894/**
James Smart3772a992009-05-22 14:50:54 -040010895 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -040010896 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -060010897 *
James Smart3772a992009-05-22 14:50:54 -040010898 * This routine is called from the PCI subsystem for error handling to
10899 * device with SLI-3 interface spec. This is called after PCI bus has been
10900 * reset to restart the PCI card from scratch, as if from a cold-boot.
10901 * During the PCI subsystem error recovery, after driver returns
10902 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
10903 * recovery and then call this routine before calling the .resume method
10904 * to recover the device. This function will initialize the HBA device,
10905 * enable the interrupt, but it will just put the HBA to offline state
10906 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -040010907 *
10908 * Return codes
James Smart3772a992009-05-22 14:50:54 -040010909 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10910 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -060010911 */
James Smart3772a992009-05-22 14:50:54 -040010912static pci_ers_result_t
10913lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -060010914{
James Smart51ef4c22007-08-02 11:10:31 -040010915 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10916 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060010917 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -050010918 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -060010919
10920 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +110010921 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -060010922 printk(KERN_ERR "lpfc: Cannot re-enable "
10923 "PCI device after reset.\n");
10924 return PCI_ERS_RESULT_DISCONNECT;
10925 }
10926
James Smart97207482008-12-04 22:39:19 -050010927 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050010928
10929 /*
10930 * As the new kernel behavior of pci_restore_state() API call clears
10931 * device saved_state flag, need to save the restored state again.
10932 */
10933 pci_save_state(pdev);
10934
James Smart97207482008-12-04 22:39:19 -050010935 if (pdev->is_busmaster)
10936 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -060010937
James Smart92d7f7b2007-06-17 19:56:38 -050010938 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010939 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -050010940 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -060010941
James Smart5b75da22008-12-04 22:39:35 -050010942 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -040010943 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -050010944 if (intr_mode == LPFC_INTR_ERROR) {
10945 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10946 "0427 Cannot re-enable interrupt after "
10947 "slot reset.\n");
10948 return PCI_ERS_RESULT_DISCONNECT;
10949 } else
10950 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -060010951
James Smart75baf692010-06-08 18:31:21 -040010952 /* Take device offline, it will perform cleanup */
James Smart618a5232012-06-12 13:54:36 -040010953 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
Linas Vepstas8d63f372007-02-14 14:28:36 -060010954 lpfc_offline(phba);
10955 lpfc_sli_brdrestart(phba);
10956
James Smart5b75da22008-12-04 22:39:35 -050010957 /* Log the current active interrupt mode */
10958 lpfc_log_intr_mode(phba, phba->intr_mode);
10959
Linas Vepstas8d63f372007-02-14 14:28:36 -060010960 return PCI_ERS_RESULT_RECOVERED;
10961}
10962
10963/**
James Smart3772a992009-05-22 14:50:54 -040010964 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -040010965 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -060010966 *
James Smart3772a992009-05-22 14:50:54 -040010967 * This routine is called from the PCI subsystem for error handling to device
10968 * with SLI-3 interface spec. It is called when kernel error recovery tells
10969 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
10970 * error recovery. After this call, traffic can start to flow from this device
10971 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -060010972 */
James Smart3772a992009-05-22 14:50:54 -040010973static void
10974lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -060010975{
James Smart51ef4c22007-08-02 11:10:31 -040010976 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10977 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060010978
James Smarte2af0d22010-03-15 11:25:32 -040010979 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -040010980 lpfc_online(phba);
James Smart0d878412009-10-02 15:16:56 -040010981
10982 /* Clean up Advanced Error Reporting (AER) if needed */
10983 if (phba->hba_flag & HBA_AER_ENABLED)
10984 pci_cleanup_aer_uncorrect_error_status(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -060010985}
10986
James Smart3772a992009-05-22 14:50:54 -040010987/**
James Smartda0436e2009-05-22 14:51:39 -040010988 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
10989 * @phba: pointer to lpfc hba data structure.
10990 *
10991 * returns the number of ELS/CT IOCBs to reserve
10992 **/
10993int
10994lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
10995{
10996 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
10997
James Smartf1126682009-06-10 17:22:44 -040010998 if (phba->sli_rev == LPFC_SLI_REV4) {
10999 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -040011000 return 10;
James Smartf1126682009-06-10 17:22:44 -040011001 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -040011002 return 25;
James Smartf1126682009-06-10 17:22:44 -040011003 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -040011004 return 50;
James Smartf1126682009-06-10 17:22:44 -040011005 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -040011006 return 100;
James Smart8a9d2e82012-05-09 21:16:12 -040011007 else if (max_xri <= 1536)
James Smart6a9c52c2009-10-02 15:16:51 -040011008 return 150;
James Smart8a9d2e82012-05-09 21:16:12 -040011009 else if (max_xri <= 2048)
11010 return 200;
11011 else
11012 return 250;
James Smartf1126682009-06-10 17:22:44 -040011013 } else
11014 return 0;
James Smartda0436e2009-05-22 14:51:39 -040011015}
11016
11017/**
James Smart895427b2017-02-12 13:52:30 -080011018 * lpfc_sli4_get_iocb_cnt - Calculate the # of total IOCBs to reserve
11019 * @phba: pointer to lpfc hba data structure.
11020 *
James Smartf358dd02017-02-12 13:52:34 -080011021 * returns the number of ELS/CT + NVMET IOCBs to reserve
James Smart895427b2017-02-12 13:52:30 -080011022 **/
11023int
11024lpfc_sli4_get_iocb_cnt(struct lpfc_hba *phba)
11025{
11026 int max_xri = lpfc_sli4_get_els_iocb_cnt(phba);
11027
James Smartf358dd02017-02-12 13:52:34 -080011028 if (phba->nvmet_support)
11029 max_xri += LPFC_NVMET_BUF_POST;
James Smart895427b2017-02-12 13:52:30 -080011030 return max_xri;
11031}
11032
11033
11034/**
James Smart52d52442011-05-24 11:42:45 -040011035 * lpfc_write_firmware - attempt to write a firmware image to the port
James Smart52d52442011-05-24 11:42:45 -040011036 * @fw: pointer to firmware image returned from request_firmware.
James Smartce396282012-09-29 11:30:56 -040011037 * @phba: pointer to lpfc hba data structure.
James Smart52d52442011-05-24 11:42:45 -040011038 *
James Smart52d52442011-05-24 11:42:45 -040011039 **/
James Smartce396282012-09-29 11:30:56 -040011040static void
11041lpfc_write_firmware(const struct firmware *fw, void *context)
James Smart52d52442011-05-24 11:42:45 -040011042{
James Smartce396282012-09-29 11:30:56 -040011043 struct lpfc_hba *phba = (struct lpfc_hba *)context;
James Smart6b5151f2012-01-18 16:24:06 -050011044 char fwrev[FW_REV_STR_SIZE];
James Smartce396282012-09-29 11:30:56 -040011045 struct lpfc_grp_hdr *image;
James Smart52d52442011-05-24 11:42:45 -040011046 struct list_head dma_buffer_list;
11047 int i, rc = 0;
11048 struct lpfc_dmabuf *dmabuf, *next;
11049 uint32_t offset = 0, temp_offset = 0;
James Smart6b6ef5d2016-10-13 15:06:17 -070011050 uint32_t magic_number, ftype, fid, fsize;
James Smart52d52442011-05-24 11:42:45 -040011051
James Smartc71ab862012-10-31 14:44:33 -040011052 /* It can be null in no-wait mode, sanity check */
James Smartce396282012-09-29 11:30:56 -040011053 if (!fw) {
11054 rc = -ENXIO;
11055 goto out;
11056 }
11057 image = (struct lpfc_grp_hdr *)fw->data;
11058
James Smart6b6ef5d2016-10-13 15:06:17 -070011059 magic_number = be32_to_cpu(image->magic_number);
11060 ftype = bf_get_be32(lpfc_grp_hdr_file_type, image);
11061 fid = bf_get_be32(lpfc_grp_hdr_id, image),
11062 fsize = be32_to_cpu(image->size);
11063
James Smart52d52442011-05-24 11:42:45 -040011064 INIT_LIST_HEAD(&dma_buffer_list);
James Smart6b6ef5d2016-10-13 15:06:17 -070011065 if ((magic_number != LPFC_GROUP_OJECT_MAGIC_G5 &&
11066 magic_number != LPFC_GROUP_OJECT_MAGIC_G6) ||
11067 ftype != LPFC_FILE_TYPE_GROUP || fsize != fw->size) {
James Smart52d52442011-05-24 11:42:45 -040011068 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11069 "3022 Invalid FW image found. "
Arnd Bergmannefe583c2016-10-17 14:35:46 +020011070 "Magic:%x Type:%x ID:%x Size %d %zd\n",
James Smart6b6ef5d2016-10-13 15:06:17 -070011071 magic_number, ftype, fid, fsize, fw->size);
James Smartce396282012-09-29 11:30:56 -040011072 rc = -EINVAL;
11073 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040011074 }
11075 lpfc_decode_firmware_rev(phba, fwrev, 1);
James Smart88a2cfb2011-07-22 18:36:33 -040011076 if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
James Smart52d52442011-05-24 11:42:45 -040011077 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartce396282012-09-29 11:30:56 -040011078 "3023 Updating Firmware, Current Version:%s "
James Smart52d52442011-05-24 11:42:45 -040011079 "New Version:%s\n",
James Smart88a2cfb2011-07-22 18:36:33 -040011080 fwrev, image->revision);
James Smart52d52442011-05-24 11:42:45 -040011081 for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
11082 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
11083 GFP_KERNEL);
11084 if (!dmabuf) {
11085 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040011086 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040011087 }
11088 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
11089 SLI4_PAGE_SIZE,
11090 &dmabuf->phys,
11091 GFP_KERNEL);
11092 if (!dmabuf->virt) {
11093 kfree(dmabuf);
11094 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040011095 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040011096 }
11097 list_add_tail(&dmabuf->list, &dma_buffer_list);
11098 }
11099 while (offset < fw->size) {
11100 temp_offset = offset;
11101 list_for_each_entry(dmabuf, &dma_buffer_list, list) {
James Smart079b5c92011-08-21 21:48:49 -040011102 if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
James Smart52d52442011-05-24 11:42:45 -040011103 memcpy(dmabuf->virt,
11104 fw->data + temp_offset,
James Smart079b5c92011-08-21 21:48:49 -040011105 fw->size - temp_offset);
11106 temp_offset = fw->size;
James Smart52d52442011-05-24 11:42:45 -040011107 break;
11108 }
James Smart52d52442011-05-24 11:42:45 -040011109 memcpy(dmabuf->virt, fw->data + temp_offset,
11110 SLI4_PAGE_SIZE);
James Smart88a2cfb2011-07-22 18:36:33 -040011111 temp_offset += SLI4_PAGE_SIZE;
James Smart52d52442011-05-24 11:42:45 -040011112 }
11113 rc = lpfc_wr_object(phba, &dma_buffer_list,
11114 (fw->size - offset), &offset);
James Smartce396282012-09-29 11:30:56 -040011115 if (rc)
11116 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040011117 }
11118 rc = offset;
11119 }
James Smartce396282012-09-29 11:30:56 -040011120
11121release_out:
James Smart52d52442011-05-24 11:42:45 -040011122 list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
11123 list_del(&dmabuf->list);
11124 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
11125 dmabuf->virt, dmabuf->phys);
11126 kfree(dmabuf);
11127 }
James Smartce396282012-09-29 11:30:56 -040011128 release_firmware(fw);
11129out:
11130 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartc71ab862012-10-31 14:44:33 -040011131 "3024 Firmware update done: %d.\n", rc);
James Smartce396282012-09-29 11:30:56 -040011132 return;
James Smart52d52442011-05-24 11:42:45 -040011133}
11134
11135/**
James Smartc71ab862012-10-31 14:44:33 -040011136 * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
11137 * @phba: pointer to lpfc hba data structure.
11138 *
11139 * This routine is called to perform Linux generic firmware upgrade on device
11140 * that supports such feature.
11141 **/
11142int
11143lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
11144{
11145 uint8_t file_name[ELX_MODEL_NAME_SIZE];
11146 int ret;
11147 const struct firmware *fw;
11148
11149 /* Only supported on SLI4 interface type 2 for now */
11150 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
11151 LPFC_SLI_INTF_IF_TYPE_2)
11152 return -EPERM;
11153
11154 snprintf(file_name, ELX_MODEL_NAME_SIZE, "%s.grp", phba->ModelName);
11155
11156 if (fw_upgrade == INT_FW_UPGRADE) {
11157 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
11158 file_name, &phba->pcidev->dev,
11159 GFP_KERNEL, (void *)phba,
11160 lpfc_write_firmware);
11161 } else if (fw_upgrade == RUN_FW_UPGRADE) {
11162 ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
11163 if (!ret)
11164 lpfc_write_firmware(fw, (void *)phba);
11165 } else {
11166 ret = -EINVAL;
11167 }
11168
11169 return ret;
11170}
11171
11172/**
James Smartda0436e2009-05-22 14:51:39 -040011173 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
11174 * @pdev: pointer to PCI device
11175 * @pid: pointer to PCI device identifier
11176 *
11177 * This routine is called from the kernel's PCI subsystem to device with
11178 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
11179 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
11180 * information of the device and driver to see if the driver state that it
11181 * can support this kind of device. If the match is successful, the driver
11182 * core invokes this routine. If this routine determines it can claim the HBA,
11183 * it does all the initialization that it needs to do to handle the HBA
11184 * properly.
11185 *
11186 * Return code
11187 * 0 - driver can claim the device
11188 * negative value - driver can not claim the device
11189 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011190static int
James Smartda0436e2009-05-22 14:51:39 -040011191lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
11192{
11193 struct lpfc_hba *phba;
11194 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -040011195 struct Scsi_Host *shost = NULL;
James Smart6c621a22017-05-15 15:20:45 -070011196 int error;
James Smartda0436e2009-05-22 14:51:39 -040011197 uint32_t cfg_mode, intr_mode;
James Smartda0436e2009-05-22 14:51:39 -040011198
11199 /* Allocate memory for HBA structure */
11200 phba = lpfc_hba_alloc(pdev);
11201 if (!phba)
11202 return -ENOMEM;
11203
11204 /* Perform generic PCI device enabling operation */
11205 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -040011206 if (error)
James Smartda0436e2009-05-22 14:51:39 -040011207 goto out_free_phba;
James Smartda0436e2009-05-22 14:51:39 -040011208
11209 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
11210 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
11211 if (error)
11212 goto out_disable_pci_dev;
11213
11214 /* Set up SLI-4 specific device PCI memory space */
11215 error = lpfc_sli4_pci_mem_setup(phba);
11216 if (error) {
11217 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11218 "1410 Failed to set up pci memory space.\n");
11219 goto out_disable_pci_dev;
11220 }
11221
James Smartda0436e2009-05-22 14:51:39 -040011222 /* Set up SLI-4 Specific device driver resources */
11223 error = lpfc_sli4_driver_resource_setup(phba);
11224 if (error) {
11225 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11226 "1412 Failed to set up driver resource.\n");
11227 goto out_unset_pci_mem_s4;
11228 }
11229
James Smart19ca7602010-11-20 23:11:55 -050011230 INIT_LIST_HEAD(&phba->active_rrq_list);
James Smart7d791df2011-07-22 18:37:52 -040011231 INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
James Smart19ca7602010-11-20 23:11:55 -050011232
James Smartda0436e2009-05-22 14:51:39 -040011233 /* Set up common device driver resources */
11234 error = lpfc_setup_driver_resource_phase2(phba);
11235 if (error) {
11236 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11237 "1414 Failed to set up driver resource.\n");
James Smart6c621a22017-05-15 15:20:45 -070011238 goto out_unset_driver_resource_s4;
James Smartda0436e2009-05-22 14:51:39 -040011239 }
11240
James Smart079b5c92011-08-21 21:48:49 -040011241 /* Get the default values for Model Name and Description */
11242 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
11243
James Smartda0436e2009-05-22 14:51:39 -040011244 /* Create SCSI host to the physical port */
11245 error = lpfc_create_shost(phba);
11246 if (error) {
11247 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11248 "1415 Failed to create scsi host.\n");
11249 goto out_unset_driver_resource;
11250 }
11251
11252 /* Configure sysfs attributes */
11253 vport = phba->pport;
11254 error = lpfc_alloc_sysfs_attr(vport);
11255 if (error) {
11256 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11257 "1416 Failed to allocate sysfs attr\n");
11258 goto out_destroy_shost;
11259 }
11260
James Smart6669f9b2009-10-02 15:16:45 -040011261 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -040011262 /* Now, trying to enable interrupt and bring up the device */
11263 cfg_mode = phba->cfg_use_msi;
James Smartda0436e2009-05-22 14:51:39 -040011264
James Smart7b15db32013-01-03 15:43:29 -050011265 /* Put device to a known state before enabling interrupt */
11266 lpfc_stop_port(phba);
James Smart895427b2017-02-12 13:52:30 -080011267
James Smart7b15db32013-01-03 15:43:29 -050011268 /* Configure and enable interrupt */
11269 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
11270 if (intr_mode == LPFC_INTR_ERROR) {
11271 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11272 "0426 Failed to enable interrupt.\n");
11273 error = -ENODEV;
11274 goto out_free_sysfs_attr;
James Smartda0436e2009-05-22 14:51:39 -040011275 }
James Smart7b15db32013-01-03 15:43:29 -050011276 /* Default to single EQ for non-MSI-X */
James Smart895427b2017-02-12 13:52:30 -080011277 if (phba->intr_type != MSIX) {
11278 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP)
11279 phba->cfg_fcp_io_channel = 1;
James Smart2d7dbc42017-02-12 13:52:35 -080011280 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart895427b2017-02-12 13:52:30 -080011281 phba->cfg_nvme_io_channel = 1;
James Smart2d7dbc42017-02-12 13:52:35 -080011282 if (phba->nvmet_support)
11283 phba->cfg_nvmet_mrq = 1;
11284 }
James Smart895427b2017-02-12 13:52:30 -080011285 phba->io_channel_irqs = 1;
11286 }
11287
James Smart7b15db32013-01-03 15:43:29 -050011288 /* Set up SLI-4 HBA */
11289 if (lpfc_sli4_hba_setup(phba)) {
11290 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11291 "1421 Failed to set up hba\n");
11292 error = -ENODEV;
11293 goto out_disable_intr;
11294 }
11295
11296 /* Log the current active interrupt mode */
11297 phba->intr_mode = intr_mode;
11298 lpfc_log_intr_mode(phba, intr_mode);
James Smartda0436e2009-05-22 14:51:39 -040011299
11300 /* Perform post initialization setup */
11301 lpfc_post_init_setup(phba);
11302
James Smart01649562017-02-12 13:52:32 -080011303 /* NVME support in FW earlier in the driver load corrects the
11304 * FC4 type making a check for nvme_support unnecessary.
11305 */
11306 if ((phba->nvmet_support == 0) &&
11307 (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)) {
11308 /* Create NVME binding with nvme_fc_transport. This
James Smartd1f525a2017-04-21 16:04:55 -070011309 * ensures the vport is initialized. If the localport
11310 * create fails, it should not unload the driver to
11311 * support field issues.
James Smart01649562017-02-12 13:52:32 -080011312 */
11313 error = lpfc_nvme_create_localport(vport);
11314 if (error) {
11315 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11316 "6004 NVME registration failed, "
11317 "error x%x\n",
11318 error);
James Smart01649562017-02-12 13:52:32 -080011319 }
11320 }
James Smart895427b2017-02-12 13:52:30 -080011321
James Smartc71ab862012-10-31 14:44:33 -040011322 /* check for firmware upgrade or downgrade */
11323 if (phba->cfg_request_firmware_upgrade)
Sebastian Herbsztdb6f1c22015-08-31 16:48:14 -040011324 lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
James Smart52d52442011-05-24 11:42:45 -040011325
James Smart1c6834a2009-07-19 10:01:26 -040011326 /* Check if there are static vports to be created. */
11327 lpfc_create_static_vport(phba);
James Smartda0436e2009-05-22 14:51:39 -040011328 return 0;
11329
11330out_disable_intr:
11331 lpfc_sli4_disable_intr(phba);
11332out_free_sysfs_attr:
11333 lpfc_free_sysfs_attr(vport);
11334out_destroy_shost:
11335 lpfc_destroy_shost(phba);
11336out_unset_driver_resource:
11337 lpfc_unset_driver_resource_phase2(phba);
James Smartda0436e2009-05-22 14:51:39 -040011338out_unset_driver_resource_s4:
11339 lpfc_sli4_driver_resource_unset(phba);
11340out_unset_pci_mem_s4:
11341 lpfc_sli4_pci_mem_unset(phba);
11342out_disable_pci_dev:
11343 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040011344 if (shost)
11345 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -040011346out_free_phba:
11347 lpfc_hba_free(phba);
11348 return error;
11349}
11350
11351/**
11352 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
11353 * @pdev: pointer to PCI device
11354 *
11355 * This routine is called from the kernel's PCI subsystem to device with
11356 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
11357 * removed from PCI bus, it performs all the necessary cleanup for the HBA
11358 * device to be removed from the PCI subsystem properly.
11359 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011360static void
James Smartda0436e2009-05-22 14:51:39 -040011361lpfc_pci_remove_one_s4(struct pci_dev *pdev)
11362{
11363 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11364 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
11365 struct lpfc_vport **vports;
11366 struct lpfc_hba *phba = vport->phba;
11367 int i;
11368
11369 /* Mark the device unloading flag */
11370 spin_lock_irq(&phba->hbalock);
11371 vport->load_flag |= FC_UNLOADING;
11372 spin_unlock_irq(&phba->hbalock);
11373
11374 /* Free the HBA sysfs attributes */
11375 lpfc_free_sysfs_attr(vport);
11376
11377 /* Release all the vports against this physical port */
11378 vports = lpfc_create_vport_work_array(phba);
11379 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040011380 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
11381 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
11382 continue;
James Smartda0436e2009-05-22 14:51:39 -040011383 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040011384 }
James Smartda0436e2009-05-22 14:51:39 -040011385 lpfc_destroy_vport_work_array(phba, vports);
11386
11387 /* Remove FC host and then SCSI host with the physical port */
11388 fc_remove_host(shost);
11389 scsi_remove_host(shost);
11390
James Smartd613b6a2017-02-12 13:52:37 -080011391 /* Perform ndlp cleanup on the physical port. The nvme and nvmet
11392 * localports are destroyed after to cleanup all transport memory.
James Smart895427b2017-02-12 13:52:30 -080011393 */
James Smartda0436e2009-05-22 14:51:39 -040011394 lpfc_cleanup(vport);
James Smartd613b6a2017-02-12 13:52:37 -080011395 lpfc_nvmet_destroy_targetport(phba);
11396 lpfc_nvme_destroy_localport(vport);
James Smartda0436e2009-05-22 14:51:39 -040011397
11398 /*
11399 * Bring down the SLI Layer. This step disables all interrupts,
11400 * clears the rings, discards all mailbox commands, and resets
11401 * the HBA FCoE function.
11402 */
11403 lpfc_debugfs_terminate(vport);
11404 lpfc_sli4_hba_unset(phba);
11405
11406 spin_lock_irq(&phba->hbalock);
11407 list_del_init(&vport->listentry);
11408 spin_unlock_irq(&phba->hbalock);
11409
James Smart3677a3a2010-09-29 11:19:14 -040011410 /* Perform scsi free before driver resource_unset since scsi
James Smartda0436e2009-05-22 14:51:39 -040011411 * buffers are released to their corresponding pools here.
11412 */
11413 lpfc_scsi_free(phba);
James Smart895427b2017-02-12 13:52:30 -080011414 lpfc_nvme_free(phba);
James Smart01649562017-02-12 13:52:32 -080011415 lpfc_free_iocb_list(phba);
James Smart67d12732012-08-03 12:36:13 -040011416
James Smartda0436e2009-05-22 14:51:39 -040011417 lpfc_sli4_driver_resource_unset(phba);
11418
11419 /* Unmap adapter Control and Doorbell registers */
11420 lpfc_sli4_pci_mem_unset(phba);
11421
11422 /* Release PCI resources and disable device's PCI function */
11423 scsi_host_put(shost);
11424 lpfc_disable_pci_dev(phba);
11425
11426 /* Finally, free the driver's device data structure */
11427 lpfc_hba_free(phba);
11428
11429 return;
11430}
11431
11432/**
11433 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
11434 * @pdev: pointer to PCI device
11435 * @msg: power management message
11436 *
11437 * This routine is called from the kernel's PCI subsystem to support system
11438 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
11439 * this method, it quiesces the device by stopping the driver's worker
11440 * thread for the device, turning off device's interrupt and DMA, and bring
11441 * the device offline. Note that as the driver implements the minimum PM
11442 * requirements to a power-aware driver's PM support for suspend/resume -- all
11443 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
11444 * method call will be treated as SUSPEND and the driver will fully
11445 * reinitialize its device during resume() method call, the driver will set
11446 * device to PCI_D3hot state in PCI config space instead of setting it
11447 * according to the @msg provided by the PM.
11448 *
11449 * Return code
11450 * 0 - driver suspended the device
11451 * Error otherwise
11452 **/
11453static int
11454lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
11455{
11456 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11457 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11458
11459 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart75baf692010-06-08 18:31:21 -040011460 "2843 PCI device Power Management suspend.\n");
James Smartda0436e2009-05-22 14:51:39 -040011461
11462 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040011463 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smartda0436e2009-05-22 14:51:39 -040011464 lpfc_offline(phba);
11465 kthread_stop(phba->worker_thread);
11466
11467 /* Disable interrupt from device */
11468 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040011469 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -040011470
11471 /* Save device state to PCI config space */
11472 pci_save_state(pdev);
11473 pci_set_power_state(pdev, PCI_D3hot);
11474
11475 return 0;
11476}
11477
11478/**
11479 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
11480 * @pdev: pointer to PCI device
11481 *
11482 * This routine is called from the kernel's PCI subsystem to support system
11483 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
11484 * this method, it restores the device's PCI config space state and fully
11485 * reinitializes the device and brings it online. Note that as the driver
11486 * implements the minimum PM requirements to a power-aware driver's PM for
11487 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
11488 * to the suspend() method call will be treated as SUSPEND and the driver
11489 * will fully reinitialize its device during resume() method call, the device
11490 * will be set to PCI_D0 directly in PCI config space before restoring the
11491 * state.
11492 *
11493 * Return code
11494 * 0 - driver suspended the device
11495 * Error otherwise
11496 **/
11497static int
11498lpfc_pci_resume_one_s4(struct pci_dev *pdev)
11499{
11500 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11501 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11502 uint32_t intr_mode;
11503 int error;
11504
11505 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11506 "0292 PCI device Power Management resume.\n");
11507
11508 /* Restore device state from PCI config space */
11509 pci_set_power_state(pdev, PCI_D0);
11510 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050011511
11512 /*
11513 * As the new kernel behavior of pci_restore_state() API call clears
11514 * device saved_state flag, need to save the restored state again.
11515 */
11516 pci_save_state(pdev);
11517
James Smartda0436e2009-05-22 14:51:39 -040011518 if (pdev->is_busmaster)
11519 pci_set_master(pdev);
11520
11521 /* Startup the kernel thread for this host adapter. */
11522 phba->worker_thread = kthread_run(lpfc_do_work, phba,
11523 "lpfc_worker_%d", phba->brd_no);
11524 if (IS_ERR(phba->worker_thread)) {
11525 error = PTR_ERR(phba->worker_thread);
11526 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11527 "0293 PM resume failed to start worker "
11528 "thread: error=x%x.\n", error);
11529 return error;
11530 }
11531
11532 /* Configure and enable interrupt */
11533 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
11534 if (intr_mode == LPFC_INTR_ERROR) {
11535 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11536 "0294 PM resume Failed to enable interrupt\n");
11537 return -EIO;
11538 } else
11539 phba->intr_mode = intr_mode;
11540
11541 /* Restart HBA and bring it online */
11542 lpfc_sli_brdrestart(phba);
11543 lpfc_online(phba);
11544
11545 /* Log the current active interrupt mode */
11546 lpfc_log_intr_mode(phba, phba->intr_mode);
11547
11548 return 0;
11549}
11550
11551/**
James Smart75baf692010-06-08 18:31:21 -040011552 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
11553 * @phba: pointer to lpfc hba data structure.
11554 *
11555 * This routine is called to prepare the SLI4 device for PCI slot recover. It
11556 * aborts all the outstanding SCSI I/Os to the pci device.
11557 **/
11558static void
11559lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
11560{
James Smart75baf692010-06-08 18:31:21 -040011561 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11562 "2828 PCI channel I/O abort preparing for recovery\n");
11563 /*
11564 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
11565 * and let the SCSI mid-layer to retry them to recover.
11566 */
James Smartdb55fba2014-04-04 13:52:02 -040011567 lpfc_sli_abort_fcp_rings(phba);
James Smart75baf692010-06-08 18:31:21 -040011568}
11569
11570/**
11571 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
11572 * @phba: pointer to lpfc hba data structure.
11573 *
11574 * This routine is called to prepare the SLI4 device for PCI slot reset. It
11575 * disables the device interrupt and pci device, and aborts the internal FCP
11576 * pending I/Os.
11577 **/
11578static void
11579lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
11580{
11581 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11582 "2826 PCI channel disable preparing for reset\n");
11583
11584 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040011585 lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
James Smart75baf692010-06-08 18:31:21 -040011586
11587 /* Block all SCSI devices' I/Os on the host */
11588 lpfc_scsi_dev_block(phba);
11589
James Smartea714f32013-04-17 20:18:47 -040011590 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
11591 lpfc_sli_flush_fcp_rings(phba);
11592
James Smart75baf692010-06-08 18:31:21 -040011593 /* stop all timers */
11594 lpfc_stop_hba_timers(phba);
11595
11596 /* Disable interrupt and pci device */
11597 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040011598 lpfc_sli4_queue_destroy(phba);
James Smart75baf692010-06-08 18:31:21 -040011599 pci_disable_device(phba->pcidev);
James Smart75baf692010-06-08 18:31:21 -040011600}
11601
11602/**
11603 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
11604 * @phba: pointer to lpfc hba data structure.
11605 *
11606 * This routine is called to prepare the SLI4 device for PCI slot permanently
11607 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
11608 * pending I/Os.
11609 **/
11610static void
11611lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
11612{
11613 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11614 "2827 PCI channel permanent disable for failure\n");
11615
11616 /* Block all SCSI devices' I/Os on the host */
11617 lpfc_scsi_dev_block(phba);
11618
11619 /* stop all timers */
11620 lpfc_stop_hba_timers(phba);
11621
11622 /* Clean up all driver's outstanding SCSI I/Os */
11623 lpfc_sli_flush_fcp_rings(phba);
11624}
11625
11626/**
James Smartda0436e2009-05-22 14:51:39 -040011627 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
11628 * @pdev: pointer to PCI device.
11629 * @state: the current PCI connection state.
11630 *
11631 * This routine is called from the PCI subsystem for error handling to device
11632 * with SLI-4 interface spec. This function is called by the PCI subsystem
11633 * after a PCI bus error affecting this device has been detected. When this
11634 * function is invoked, it will need to stop all the I/Os and interrupt(s)
11635 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
11636 * for the PCI subsystem to perform proper recovery as desired.
11637 *
11638 * Return codes
11639 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
11640 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11641 **/
11642static pci_ers_result_t
11643lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
11644{
James Smart75baf692010-06-08 18:31:21 -040011645 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11646 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11647
11648 switch (state) {
11649 case pci_channel_io_normal:
11650 /* Non-fatal error, prepare for recovery */
11651 lpfc_sli4_prep_dev_for_recover(phba);
11652 return PCI_ERS_RESULT_CAN_RECOVER;
11653 case pci_channel_io_frozen:
11654 /* Fatal error, prepare for slot reset */
11655 lpfc_sli4_prep_dev_for_reset(phba);
11656 return PCI_ERS_RESULT_NEED_RESET;
11657 case pci_channel_io_perm_failure:
11658 /* Permanent failure, prepare for device down */
11659 lpfc_sli4_prep_dev_for_perm_failure(phba);
11660 return PCI_ERS_RESULT_DISCONNECT;
11661 default:
11662 /* Unknown state, prepare and request slot reset */
11663 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11664 "2825 Unknown PCI error state: x%x\n", state);
11665 lpfc_sli4_prep_dev_for_reset(phba);
11666 return PCI_ERS_RESULT_NEED_RESET;
11667 }
James Smartda0436e2009-05-22 14:51:39 -040011668}
11669
11670/**
11671 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
11672 * @pdev: pointer to PCI device.
11673 *
11674 * This routine is called from the PCI subsystem for error handling to device
11675 * with SLI-4 interface spec. It is called after PCI bus has been reset to
11676 * restart the PCI card from scratch, as if from a cold-boot. During the
11677 * PCI subsystem error recovery, after the driver returns
11678 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
11679 * recovery and then call this routine before calling the .resume method to
11680 * recover the device. This function will initialize the HBA device, enable
11681 * the interrupt, but it will just put the HBA to offline state without
11682 * passing any I/O traffic.
11683 *
11684 * Return codes
11685 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
11686 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11687 */
11688static pci_ers_result_t
11689lpfc_io_slot_reset_s4(struct pci_dev *pdev)
11690{
James Smart75baf692010-06-08 18:31:21 -040011691 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11692 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11693 struct lpfc_sli *psli = &phba->sli;
11694 uint32_t intr_mode;
11695
11696 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
11697 if (pci_enable_device_mem(pdev)) {
11698 printk(KERN_ERR "lpfc: Cannot re-enable "
11699 "PCI device after reset.\n");
11700 return PCI_ERS_RESULT_DISCONNECT;
11701 }
11702
11703 pci_restore_state(pdev);
James Smart0a96e972011-07-22 18:37:28 -040011704
11705 /*
11706 * As the new kernel behavior of pci_restore_state() API call clears
11707 * device saved_state flag, need to save the restored state again.
11708 */
11709 pci_save_state(pdev);
11710
James Smart75baf692010-06-08 18:31:21 -040011711 if (pdev->is_busmaster)
11712 pci_set_master(pdev);
11713
11714 spin_lock_irq(&phba->hbalock);
11715 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
11716 spin_unlock_irq(&phba->hbalock);
11717
11718 /* Configure and enable interrupt */
11719 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
11720 if (intr_mode == LPFC_INTR_ERROR) {
11721 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11722 "2824 Cannot re-enable interrupt after "
11723 "slot reset.\n");
11724 return PCI_ERS_RESULT_DISCONNECT;
11725 } else
11726 phba->intr_mode = intr_mode;
11727
11728 /* Log the current active interrupt mode */
11729 lpfc_log_intr_mode(phba, phba->intr_mode);
11730
James Smartda0436e2009-05-22 14:51:39 -040011731 return PCI_ERS_RESULT_RECOVERED;
11732}
11733
11734/**
11735 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
11736 * @pdev: pointer to PCI device
11737 *
11738 * This routine is called from the PCI subsystem for error handling to device
11739 * with SLI-4 interface spec. It is called when kernel error recovery tells
11740 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
11741 * error recovery. After this call, traffic can start to flow from this device
11742 * again.
11743 **/
11744static void
11745lpfc_io_resume_s4(struct pci_dev *pdev)
11746{
James Smart75baf692010-06-08 18:31:21 -040011747 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11748 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11749
11750 /*
11751 * In case of slot reset, as function reset is performed through
11752 * mailbox command which needs DMA to be enabled, this operation
11753 * has to be moved to the io resume phase. Taking device offline
11754 * will perform the necessary cleanup.
11755 */
11756 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
11757 /* Perform device reset */
James Smart618a5232012-06-12 13:54:36 -040011758 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040011759 lpfc_offline(phba);
11760 lpfc_sli_brdrestart(phba);
11761 /* Bring the device back online */
11762 lpfc_online(phba);
11763 }
11764
11765 /* Clean up Advanced Error Reporting (AER) if needed */
11766 if (phba->hba_flag & HBA_AER_ENABLED)
11767 pci_cleanup_aer_uncorrect_error_status(pdev);
James Smartda0436e2009-05-22 14:51:39 -040011768}
11769
11770/**
James Smart3772a992009-05-22 14:50:54 -040011771 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
11772 * @pdev: pointer to PCI device
11773 * @pid: pointer to PCI device identifier
11774 *
11775 * This routine is to be registered to the kernel's PCI subsystem. When an
11776 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
11777 * at PCI device-specific information of the device and driver to see if the
11778 * driver state that it can support this kind of device. If the match is
11779 * successful, the driver core invokes this routine. This routine dispatches
11780 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
11781 * do all the initialization that it needs to do to handle the HBA device
11782 * properly.
11783 *
11784 * Return code
11785 * 0 - driver can claim the device
11786 * negative value - driver can not claim the device
11787 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011788static int
James Smart3772a992009-05-22 14:50:54 -040011789lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
11790{
11791 int rc;
James Smart8fa38512009-07-19 10:01:03 -040011792 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -040011793
James Smart28baac72010-02-12 14:42:03 -050011794 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -040011795 return -ENODEV;
11796
James Smart8fa38512009-07-19 10:01:03 -040011797 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -050011798 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -040011799 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040011800 else
James Smart3772a992009-05-22 14:50:54 -040011801 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040011802
James Smart3772a992009-05-22 14:50:54 -040011803 return rc;
11804}
11805
11806/**
11807 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
11808 * @pdev: pointer to PCI device
11809 *
11810 * This routine is to be registered to the kernel's PCI subsystem. When an
11811 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
11812 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
11813 * remove routine, which will perform all the necessary cleanup for the
11814 * device to be removed from the PCI subsystem properly.
11815 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011816static void
James Smart3772a992009-05-22 14:50:54 -040011817lpfc_pci_remove_one(struct pci_dev *pdev)
11818{
11819 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11820 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11821
11822 switch (phba->pci_dev_grp) {
11823 case LPFC_PCI_DEV_LP:
11824 lpfc_pci_remove_one_s3(pdev);
11825 break;
James Smartda0436e2009-05-22 14:51:39 -040011826 case LPFC_PCI_DEV_OC:
11827 lpfc_pci_remove_one_s4(pdev);
11828 break;
James Smart3772a992009-05-22 14:50:54 -040011829 default:
11830 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11831 "1424 Invalid PCI device group: 0x%x\n",
11832 phba->pci_dev_grp);
11833 break;
11834 }
11835 return;
11836}
11837
11838/**
11839 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
11840 * @pdev: pointer to PCI device
11841 * @msg: power management message
11842 *
11843 * This routine is to be registered to the kernel's PCI subsystem to support
11844 * system Power Management (PM). When PM invokes this method, it dispatches
11845 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
11846 * suspend the device.
11847 *
11848 * Return code
11849 * 0 - driver suspended the device
11850 * Error otherwise
11851 **/
11852static int
11853lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
11854{
11855 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11856 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11857 int rc = -ENODEV;
11858
11859 switch (phba->pci_dev_grp) {
11860 case LPFC_PCI_DEV_LP:
11861 rc = lpfc_pci_suspend_one_s3(pdev, msg);
11862 break;
James Smartda0436e2009-05-22 14:51:39 -040011863 case LPFC_PCI_DEV_OC:
11864 rc = lpfc_pci_suspend_one_s4(pdev, msg);
11865 break;
James Smart3772a992009-05-22 14:50:54 -040011866 default:
11867 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11868 "1425 Invalid PCI device group: 0x%x\n",
11869 phba->pci_dev_grp);
11870 break;
11871 }
11872 return rc;
11873}
11874
11875/**
11876 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
11877 * @pdev: pointer to PCI device
11878 *
11879 * This routine is to be registered to the kernel's PCI subsystem to support
11880 * system Power Management (PM). When PM invokes this method, it dispatches
11881 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
11882 * resume the device.
11883 *
11884 * Return code
11885 * 0 - driver suspended the device
11886 * Error otherwise
11887 **/
11888static int
11889lpfc_pci_resume_one(struct pci_dev *pdev)
11890{
11891 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11892 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11893 int rc = -ENODEV;
11894
11895 switch (phba->pci_dev_grp) {
11896 case LPFC_PCI_DEV_LP:
11897 rc = lpfc_pci_resume_one_s3(pdev);
11898 break;
James Smartda0436e2009-05-22 14:51:39 -040011899 case LPFC_PCI_DEV_OC:
11900 rc = lpfc_pci_resume_one_s4(pdev);
11901 break;
James Smart3772a992009-05-22 14:50:54 -040011902 default:
11903 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11904 "1426 Invalid PCI device group: 0x%x\n",
11905 phba->pci_dev_grp);
11906 break;
11907 }
11908 return rc;
11909}
11910
11911/**
11912 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
11913 * @pdev: pointer to PCI device.
11914 * @state: the current PCI connection state.
11915 *
11916 * This routine is registered to the PCI subsystem for error handling. This
11917 * function is called by the PCI subsystem after a PCI bus error affecting
11918 * this device has been detected. When this routine is invoked, it dispatches
11919 * the action to the proper SLI-3 or SLI-4 device error detected handling
11920 * routine, which will perform the proper error detected operation.
11921 *
11922 * Return codes
11923 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
11924 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11925 **/
11926static pci_ers_result_t
11927lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
11928{
11929 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11930 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11931 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
11932
11933 switch (phba->pci_dev_grp) {
11934 case LPFC_PCI_DEV_LP:
11935 rc = lpfc_io_error_detected_s3(pdev, state);
11936 break;
James Smartda0436e2009-05-22 14:51:39 -040011937 case LPFC_PCI_DEV_OC:
11938 rc = lpfc_io_error_detected_s4(pdev, state);
11939 break;
James Smart3772a992009-05-22 14:50:54 -040011940 default:
11941 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11942 "1427 Invalid PCI device group: 0x%x\n",
11943 phba->pci_dev_grp);
11944 break;
11945 }
11946 return rc;
11947}
11948
11949/**
11950 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
11951 * @pdev: pointer to PCI device.
11952 *
11953 * This routine is registered to the PCI subsystem for error handling. This
11954 * function is called after PCI bus has been reset to restart the PCI card
11955 * from scratch, as if from a cold-boot. When this routine is invoked, it
11956 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
11957 * routine, which will perform the proper device reset.
11958 *
11959 * Return codes
11960 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
11961 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11962 **/
11963static pci_ers_result_t
11964lpfc_io_slot_reset(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 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
11969
11970 switch (phba->pci_dev_grp) {
11971 case LPFC_PCI_DEV_LP:
11972 rc = lpfc_io_slot_reset_s3(pdev);
11973 break;
James Smartda0436e2009-05-22 14:51:39 -040011974 case LPFC_PCI_DEV_OC:
11975 rc = lpfc_io_slot_reset_s4(pdev);
11976 break;
James Smart3772a992009-05-22 14:50:54 -040011977 default:
11978 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11979 "1428 Invalid PCI device group: 0x%x\n",
11980 phba->pci_dev_grp);
11981 break;
11982 }
11983 return rc;
11984}
11985
11986/**
11987 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
11988 * @pdev: pointer to PCI device
11989 *
11990 * This routine is registered to the PCI subsystem for error handling. It
11991 * is called when kernel error recovery tells the lpfc driver that it is
11992 * OK to resume normal PCI operation after PCI bus error recovery. When
11993 * this routine is invoked, it dispatches the action to the proper SLI-3
11994 * or SLI-4 device io_resume routine, which will resume the device operation.
11995 **/
11996static void
11997lpfc_io_resume(struct pci_dev *pdev)
11998{
11999 struct Scsi_Host *shost = pci_get_drvdata(pdev);
12000 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12001
12002 switch (phba->pci_dev_grp) {
12003 case LPFC_PCI_DEV_LP:
12004 lpfc_io_resume_s3(pdev);
12005 break;
James Smartda0436e2009-05-22 14:51:39 -040012006 case LPFC_PCI_DEV_OC:
12007 lpfc_io_resume_s4(pdev);
12008 break;
James Smart3772a992009-05-22 14:50:54 -040012009 default:
12010 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12011 "1429 Invalid PCI device group: 0x%x\n",
12012 phba->pci_dev_grp);
12013 break;
12014 }
12015 return;
12016}
12017
James Smart1ba981f2014-02-20 09:56:45 -050012018/**
12019 * lpfc_sli4_oas_verify - Verify OAS is supported by this adapter
12020 * @phba: pointer to lpfc hba data structure.
12021 *
12022 * This routine checks to see if OAS is supported for this adapter. If
12023 * supported, the configure Flash Optimized Fabric flag is set. Otherwise,
12024 * the enable oas flag is cleared and the pool created for OAS device data
12025 * is destroyed.
12026 *
12027 **/
12028void
12029lpfc_sli4_oas_verify(struct lpfc_hba *phba)
12030{
12031
12032 if (!phba->cfg_EnableXLane)
12033 return;
12034
12035 if (phba->sli4_hba.pc_sli4_params.oas_supported) {
12036 phba->cfg_fof = 1;
12037 } else {
James Smartf38fa0b2014-04-04 13:52:21 -040012038 phba->cfg_fof = 0;
James Smart1ba981f2014-02-20 09:56:45 -050012039 if (phba->device_data_mem_pool)
12040 mempool_destroy(phba->device_data_mem_pool);
12041 phba->device_data_mem_pool = NULL;
12042 }
12043
12044 return;
12045}
12046
12047/**
12048 * lpfc_fof_queue_setup - Set up all the fof queues
12049 * @phba: pointer to lpfc hba data structure.
12050 *
12051 * This routine is invoked to set up all the fof queues for the FC HBA
12052 * operation.
12053 *
12054 * Return codes
12055 * 0 - successful
12056 * -ENOMEM - No available memory
12057 **/
12058int
12059lpfc_fof_queue_setup(struct lpfc_hba *phba)
12060{
James Smart895427b2017-02-12 13:52:30 -080012061 struct lpfc_sli_ring *pring;
James Smart1ba981f2014-02-20 09:56:45 -050012062 int rc;
12063
12064 rc = lpfc_eq_create(phba, phba->sli4_hba.fof_eq, LPFC_MAX_IMAX);
12065 if (rc)
12066 return -ENOMEM;
12067
James Smartf38fa0b2014-04-04 13:52:21 -040012068 if (phba->cfg_fof) {
James Smart1ba981f2014-02-20 09:56:45 -050012069
12070 rc = lpfc_cq_create(phba, phba->sli4_hba.oas_cq,
12071 phba->sli4_hba.fof_eq, LPFC_WCQ, LPFC_FCP);
12072 if (rc)
12073 goto out_oas_cq;
12074
12075 rc = lpfc_wq_create(phba, phba->sli4_hba.oas_wq,
12076 phba->sli4_hba.oas_cq, LPFC_FCP);
12077 if (rc)
12078 goto out_oas_wq;
12079
James Smart895427b2017-02-12 13:52:30 -080012080 /* Bind this CQ/WQ to the NVME ring */
12081 pring = phba->sli4_hba.oas_wq->pring;
12082 pring->sli.sli4.wqp =
12083 (void *)phba->sli4_hba.oas_wq;
12084 phba->sli4_hba.oas_cq->pring = pring;
James Smart1ba981f2014-02-20 09:56:45 -050012085 }
12086
12087 return 0;
12088
12089out_oas_wq:
James Smartf38fa0b2014-04-04 13:52:21 -040012090 lpfc_cq_destroy(phba, phba->sli4_hba.oas_cq);
James Smart1ba981f2014-02-20 09:56:45 -050012091out_oas_cq:
12092 lpfc_eq_destroy(phba, phba->sli4_hba.fof_eq);
12093 return rc;
12094
12095}
12096
12097/**
12098 * lpfc_fof_queue_create - Create all the fof queues
12099 * @phba: pointer to lpfc hba data structure.
12100 *
12101 * This routine is invoked to allocate all the fof queues for the FC HBA
12102 * operation. For each SLI4 queue type, the parameters such as queue entry
12103 * count (queue depth) shall be taken from the module parameter. For now,
12104 * we just use some constant number as place holder.
12105 *
12106 * Return codes
12107 * 0 - successful
12108 * -ENOMEM - No availble memory
12109 * -EIO - The mailbox failed to complete successfully.
12110 **/
12111int
12112lpfc_fof_queue_create(struct lpfc_hba *phba)
12113{
12114 struct lpfc_queue *qdesc;
James Smart7e04e212017-04-21 16:05:05 -070012115 uint32_t wqesize;
James Smart1ba981f2014-02-20 09:56:45 -050012116
12117 /* Create FOF EQ */
12118 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
12119 phba->sli4_hba.eq_ecount);
12120 if (!qdesc)
12121 goto out_error;
12122
12123 phba->sli4_hba.fof_eq = qdesc;
12124
James Smartf38fa0b2014-04-04 13:52:21 -040012125 if (phba->cfg_fof) {
James Smart1ba981f2014-02-20 09:56:45 -050012126
12127 /* Create OAS CQ */
12128 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
12129 phba->sli4_hba.cq_ecount);
12130 if (!qdesc)
12131 goto out_error;
12132
12133 phba->sli4_hba.oas_cq = qdesc;
12134
12135 /* Create OAS WQ */
James Smart7e04e212017-04-21 16:05:05 -070012136 wqesize = (phba->fcp_embed_io) ?
12137 LPFC_WQE128_SIZE : phba->sli4_hba.wq_esize;
12138 qdesc = lpfc_sli4_queue_alloc(phba, wqesize,
James Smart1ba981f2014-02-20 09:56:45 -050012139 phba->sli4_hba.wq_ecount);
James Smart7e04e212017-04-21 16:05:05 -070012140
James Smart1ba981f2014-02-20 09:56:45 -050012141 if (!qdesc)
12142 goto out_error;
12143
12144 phba->sli4_hba.oas_wq = qdesc;
James Smart895427b2017-02-12 13:52:30 -080012145 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
James Smart1ba981f2014-02-20 09:56:45 -050012146
12147 }
12148 return 0;
12149
12150out_error:
12151 lpfc_fof_queue_destroy(phba);
12152 return -ENOMEM;
12153}
12154
12155/**
12156 * lpfc_fof_queue_destroy - Destroy all the fof queues
12157 * @phba: pointer to lpfc hba data structure.
12158 *
12159 * This routine is invoked to release all the SLI4 queues with the FC HBA
12160 * operation.
12161 *
12162 * Return codes
12163 * 0 - successful
12164 **/
12165int
12166lpfc_fof_queue_destroy(struct lpfc_hba *phba)
12167{
12168 /* Release FOF Event queue */
12169 if (phba->sli4_hba.fof_eq != NULL) {
12170 lpfc_sli4_queue_free(phba->sli4_hba.fof_eq);
12171 phba->sli4_hba.fof_eq = NULL;
12172 }
12173
12174 /* Release OAS Completion queue */
12175 if (phba->sli4_hba.oas_cq != NULL) {
12176 lpfc_sli4_queue_free(phba->sli4_hba.oas_cq);
12177 phba->sli4_hba.oas_cq = NULL;
12178 }
12179
12180 /* Release OAS Work queue */
12181 if (phba->sli4_hba.oas_wq != NULL) {
12182 lpfc_sli4_queue_free(phba->sli4_hba.oas_wq);
12183 phba->sli4_hba.oas_wq = NULL;
12184 }
12185 return 0;
12186}
12187
dea31012005-04-17 16:05:31 -050012188MODULE_DEVICE_TABLE(pci, lpfc_id_table);
12189
Stephen Hemmingera55b2d22012-09-07 09:33:16 -070012190static const struct pci_error_handlers lpfc_err_handler = {
Linas Vepstas8d63f372007-02-14 14:28:36 -060012191 .error_detected = lpfc_io_error_detected,
12192 .slot_reset = lpfc_io_slot_reset,
12193 .resume = lpfc_io_resume,
12194};
12195
dea31012005-04-17 16:05:31 -050012196static struct pci_driver lpfc_driver = {
12197 .name = LPFC_DRIVER_NAME,
12198 .id_table = lpfc_id_table,
12199 .probe = lpfc_pci_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080012200 .remove = lpfc_pci_remove_one,
Anton Blanchard85e8a232017-02-13 08:49:20 +110012201 .shutdown = lpfc_pci_remove_one,
James Smart3a55b532008-12-04 22:38:54 -050012202 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -040012203 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -050012204 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -050012205};
12206
James Smart3ef6d242012-01-18 16:23:48 -050012207static const struct file_operations lpfc_mgmt_fop = {
Al Viro858feac2013-04-14 22:39:37 -040012208 .owner = THIS_MODULE,
James Smart3ef6d242012-01-18 16:23:48 -050012209};
12210
12211static struct miscdevice lpfc_mgmt_dev = {
12212 .minor = MISC_DYNAMIC_MINOR,
12213 .name = "lpfcmgmt",
12214 .fops = &lpfc_mgmt_fop,
12215};
12216
James Smarte59058c2008-08-24 21:49:00 -040012217/**
James Smart3621a712009-04-06 18:47:14 -040012218 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -040012219 *
12220 * This routine is to be invoked when the lpfc module is loaded into the
12221 * kernel. The special kernel macro module_init() is used to indicate the
12222 * role of this routine to the kernel as lpfc module entry point.
12223 *
12224 * Return codes
12225 * 0 - successful
12226 * -ENOMEM - FC attach transport failed
12227 * all others - failed
12228 */
dea31012005-04-17 16:05:31 -050012229static int __init
12230lpfc_init(void)
12231{
12232 int error = 0;
12233
12234 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040012235 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -050012236
James Smart3ef6d242012-01-18 16:23:48 -050012237 error = misc_register(&lpfc_mgmt_dev);
12238 if (error)
12239 printk(KERN_ERR "Could not register lpfcmgmt device, "
12240 "misc_register returned with status %d", error);
12241
James Smart458c0832016-07-06 12:36:07 -070012242 lpfc_transport_functions.vport_create = lpfc_vport_create;
12243 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
dea31012005-04-17 16:05:31 -050012244 lpfc_transport_template =
12245 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -040012246 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -050012247 return -ENOMEM;
James Smart458c0832016-07-06 12:36:07 -070012248 lpfc_vport_transport_template =
12249 fc_attach_transport(&lpfc_vport_transport_functions);
12250 if (lpfc_vport_transport_template == NULL) {
12251 fc_release_transport(lpfc_transport_template);
12252 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -040012253 }
James Smart7bb03bb2013-04-17 20:19:16 -040012254
12255 /* Initialize in case vector mapping is needed */
James Smartb246de12013-05-31 17:03:07 -040012256 lpfc_used_cpu = NULL;
James Smart2ea259e2017-02-12 13:52:27 -080012257 lpfc_present_cpu = num_present_cpus();
James Smart7bb03bb2013-04-17 20:19:16 -040012258
dea31012005-04-17 16:05:31 -050012259 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -050012260 if (error) {
dea31012005-04-17 16:05:31 -050012261 fc_release_transport(lpfc_transport_template);
James Smart458c0832016-07-06 12:36:07 -070012262 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -050012263 }
dea31012005-04-17 16:05:31 -050012264
12265 return error;
12266}
12267
James Smarte59058c2008-08-24 21:49:00 -040012268/**
James Smart3621a712009-04-06 18:47:14 -040012269 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -040012270 *
12271 * This routine is invoked when the lpfc module is removed from the kernel.
12272 * The special kernel macro module_exit() is used to indicate the role of
12273 * this routine to the kernel as lpfc module exit point.
12274 */
dea31012005-04-17 16:05:31 -050012275static void __exit
12276lpfc_exit(void)
12277{
James Smart3ef6d242012-01-18 16:23:48 -050012278 misc_deregister(&lpfc_mgmt_dev);
dea31012005-04-17 16:05:31 -050012279 pci_unregister_driver(&lpfc_driver);
12280 fc_release_transport(lpfc_transport_template);
James Smart458c0832016-07-06 12:36:07 -070012281 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -050012282 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040012283 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
12284 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050012285 (1L << _dump_buf_data_order), _dump_buf_data);
12286 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
12287 }
12288
12289 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -040012290 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
12291 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050012292 (1L << _dump_buf_dif_order), _dump_buf_dif);
12293 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
12294 }
James Smartb246de12013-05-31 17:03:07 -040012295 kfree(lpfc_used_cpu);
Johannes Thumshirn79739672015-08-31 16:48:11 -040012296 idr_destroy(&lpfc_hba_index);
dea31012005-04-17 16:05:31 -050012297}
12298
12299module_init(lpfc_init);
12300module_exit(lpfc_exit);
12301MODULE_LICENSE("GPL");
12302MODULE_DESCRIPTION(LPFC_MODULE_DESC);
James Smartd080abe2017-02-12 13:52:39 -080012303MODULE_AUTHOR("Broadcom");
dea31012005-04-17 16:05:31 -050012304MODULE_VERSION("0:" LPFC_DRIVER_VERSION);