blob: b56da015e3b36179207661fd34cbf83249d1cd6c [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>
45
James Smartda0436e2009-05-22 14:51:39 -040046#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050047#include "lpfc_hw.h"
48#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040049#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040050#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050051#include "lpfc_disc.h"
dea31012005-04-17 16:05:31 -050052#include "lpfc.h"
James Smart895427b2017-02-12 13:52:30 -080053#include "lpfc_scsi.h"
54#include "lpfc_nvme.h"
dea31012005-04-17 16:05:31 -050055#include "lpfc_logmsg.h"
56#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050057#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050058#include "lpfc_version.h"
James Smart12f44452016-07-06 12:36:03 -070059#include "lpfc_ids.h"
dea31012005-04-17 16:05:31 -050060
James Smart81301a92008-12-04 22:39:46 -050061char *_dump_buf_data;
62unsigned long _dump_buf_data_order;
63char *_dump_buf_dif;
64unsigned long _dump_buf_dif_order;
65spinlock_t _dump_buf_lock;
66
James Smart7bb03bb2013-04-17 20:19:16 -040067/* Used when mapping IRQ vectors in a driver centric manner */
James Smartb246de12013-05-31 17:03:07 -040068uint16_t *lpfc_used_cpu;
69uint32_t lpfc_present_cpu;
James Smart7bb03bb2013-04-17 20:19:16 -040070
dea31012005-04-17 16:05:31 -050071static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
72static int lpfc_post_rcv_buf(struct lpfc_hba *);
James Smart5350d872011-10-10 21:33:49 -040073static int lpfc_sli4_queue_verify(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040074static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
75static int lpfc_setup_endian_order(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040076static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
James Smart8a9d2e82012-05-09 21:16:12 -040077static void lpfc_free_els_sgl_list(struct lpfc_hba *);
James Smartf358dd02017-02-12 13:52:34 -080078static void lpfc_free_nvmet_sgl_list(struct lpfc_hba *);
James Smart8a9d2e82012-05-09 21:16:12 -040079static void lpfc_init_sgl_list(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040080static int lpfc_init_active_sgl_array(struct lpfc_hba *);
81static void lpfc_free_active_sgl(struct lpfc_hba *);
82static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
83static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
84static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
85static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
86static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
James Smart618a5232012-06-12 13:54:36 -040087static void lpfc_sli4_disable_intr(struct lpfc_hba *);
88static uint32_t lpfc_sli4_enable_intr(struct lpfc_hba *, uint32_t);
James Smart1ba981f2014-02-20 09:56:45 -050089static void lpfc_sli4_oas_verify(struct lpfc_hba *phba);
dea31012005-04-17 16:05:31 -050090
91static struct scsi_transport_template *lpfc_transport_template = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050092static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea31012005-04-17 16:05:31 -050093static DEFINE_IDR(lpfc_hba_index);
James Smartf358dd02017-02-12 13:52:34 -080094#define LPFC_NVMET_BUF_POST 254
dea31012005-04-17 16:05:31 -050095
James Smarte59058c2008-08-24 21:49:00 -040096/**
James Smart3621a712009-04-06 18:47:14 -040097 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
James Smarte59058c2008-08-24 21:49:00 -040098 * @phba: pointer to lpfc hba data structure.
99 *
100 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
101 * mailbox command. It retrieves the revision information from the HBA and
102 * collects the Vital Product Data (VPD) about the HBA for preparing the
103 * configuration of the HBA.
104 *
105 * Return codes:
106 * 0 - success.
107 * -ERESTART - requests the SLI layer to reset the HBA and try again.
108 * Any other value - indicates an error.
109 **/
dea31012005-04-17 16:05:31 -0500110int
James Smart2e0fef82007-06-17 19:56:36 -0500111lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500112{
113 lpfc_vpd_t *vp = &phba->vpd;
114 int i = 0, rc;
115 LPFC_MBOXQ_t *pmb;
116 MAILBOX_t *mb;
117 char *lpfc_vpd_data = NULL;
118 uint16_t offset = 0;
119 static char licensed[56] =
120 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -0400121 static int init_key = 1;
dea31012005-04-17 16:05:31 -0500122
123 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
124 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500125 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500126 return -ENOMEM;
127 }
128
James Smart04c68492009-05-22 14:52:52 -0400129 mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -0500130 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500131
132 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -0400133 if (init_key) {
134 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -0500135
James Smart65a29c12006-07-06 15:50:50 -0400136 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
137 *ptext = cpu_to_be32(*ptext);
138 init_key = 0;
139 }
dea31012005-04-17 16:05:31 -0500140
141 lpfc_read_nv(phba, pmb);
142 memset((char*)mb->un.varRDnvp.rsvd3, 0,
143 sizeof (mb->un.varRDnvp.rsvd3));
144 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
145 sizeof (licensed));
146
147 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
148
149 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500150 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smarte8b62012007-08-02 11:10:09 -0400151 "0324 Config Port initialization "
dea31012005-04-17 16:05:31 -0500152 "error, mbxCmd x%x READ_NVPARM, "
153 "mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500154 mb->mbxCommand, mb->mbxStatus);
155 mempool_free(pmb, phba->mbox_mem_pool);
156 return -ERESTART;
157 }
158 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
James Smart2e0fef82007-06-17 19:56:36 -0500159 sizeof(phba->wwnn));
160 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
161 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500162 }
163
James Smart92d7f7b2007-06-17 19:56:38 -0500164 phba->sli3_options = 0x0;
165
dea31012005-04-17 16:05:31 -0500166 /* Setup and issue mailbox READ REV command */
167 lpfc_read_rev(phba, pmb);
168 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
169 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500170 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400171 "0439 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500172 "READ_REV, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500173 mb->mbxCommand, mb->mbxStatus);
174 mempool_free( pmb, phba->mbox_mem_pool);
175 return -ERESTART;
176 }
177
James Smart92d7f7b2007-06-17 19:56:38 -0500178
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500179 /*
180 * The value of rr must be 1 since the driver set the cv field to 1.
181 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500182 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500183 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500184 vp->rev.rBit = 0;
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500185 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400186 "0440 Adapter failed to init, READ_REV has "
187 "missing revision information.\n");
dea31012005-04-17 16:05:31 -0500188 mempool_free(pmb, phba->mbox_mem_pool);
189 return -ERESTART;
dea31012005-04-17 16:05:31 -0500190 }
191
James Smart495a7142008-06-14 22:52:59 -0400192 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
193 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -0500194 return -EINVAL;
James Smart495a7142008-06-14 22:52:59 -0400195 }
James Smarted957682007-06-17 19:56:37 -0500196
dea31012005-04-17 16:05:31 -0500197 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500198 vp->rev.rBit = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500199 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500200 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
201 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
202 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
203 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500204 vp->rev.biuRev = mb->un.varRdRev.biuRev;
205 vp->rev.smRev = mb->un.varRdRev.smRev;
206 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
207 vp->rev.endecRev = mb->un.varRdRev.endecRev;
208 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
209 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
210 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
211 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
212 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
213 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
214
James Smart92d7f7b2007-06-17 19:56:38 -0500215 /* If the sli feature level is less then 9, we must
216 * tear down all RPIs and VPIs on link down if NPIV
217 * is enabled.
218 */
219 if (vp->rev.feaLevelHigh < 9)
220 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
221
dea31012005-04-17 16:05:31 -0500222 if (lpfc_is_LC_HBA(phba->pcidev->device))
223 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
224 sizeof (phba->RandomData));
225
dea31012005-04-17 16:05:31 -0500226 /* Get adapter VPD information */
dea31012005-04-17 16:05:31 -0500227 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
228 if (!lpfc_vpd_data)
James Smartd7c255b2008-08-24 21:50:00 -0400229 goto out_free_mbox;
dea31012005-04-17 16:05:31 -0500230 do {
James Smarta0c87cb2009-07-19 10:01:10 -0400231 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
dea31012005-04-17 16:05:31 -0500232 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
233
234 if (rc != MBX_SUCCESS) {
235 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400236 "0441 VPD not present on adapter, "
dea31012005-04-17 16:05:31 -0500237 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500238 mb->mbxCommand, mb->mbxStatus);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500239 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500240 }
James Smart04c68492009-05-22 14:52:52 -0400241 /* dump mem may return a zero when finished or we got a
242 * mailbox error, either way we are done.
243 */
244 if (mb->un.varDmp.word_cnt == 0)
245 break;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500246 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
247 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
James Smartd7c255b2008-08-24 21:50:00 -0400248 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
249 lpfc_vpd_data + offset,
James Smart92d7f7b2007-06-17 19:56:38 -0500250 mb->un.varDmp.word_cnt);
dea31012005-04-17 16:05:31 -0500251 offset += mb->un.varDmp.word_cnt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500252 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
253 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500254
255 kfree(lpfc_vpd_data);
dea31012005-04-17 16:05:31 -0500256out_free_mbox:
257 mempool_free(pmb, phba->mbox_mem_pool);
258 return 0;
259}
260
James Smarte59058c2008-08-24 21:49:00 -0400261/**
James Smart3621a712009-04-06 18:47:14 -0400262 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
James Smarte59058c2008-08-24 21:49:00 -0400263 * @phba: pointer to lpfc hba data structure.
264 * @pmboxq: pointer to the driver internal queue element for mailbox command.
265 *
266 * This is the completion handler for driver's configuring asynchronous event
267 * mailbox command to the device. If the mailbox command returns successfully,
268 * it will set internal async event support flag to 1; otherwise, it will
269 * set internal async event support flag to 0.
270 **/
James Smart57127f12007-10-27 13:37:05 -0400271static void
272lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
273{
James Smart04c68492009-05-22 14:52:52 -0400274 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
James Smart57127f12007-10-27 13:37:05 -0400275 phba->temp_sensor_support = 1;
276 else
277 phba->temp_sensor_support = 0;
278 mempool_free(pmboxq, phba->mbox_mem_pool);
279 return;
280}
281
James Smarte59058c2008-08-24 21:49:00 -0400282/**
James Smart3621a712009-04-06 18:47:14 -0400283 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
James Smart97207482008-12-04 22:39:19 -0500284 * @phba: pointer to lpfc hba data structure.
285 * @pmboxq: pointer to the driver internal queue element for mailbox command.
286 *
287 * This is the completion handler for dump mailbox command for getting
288 * wake up parameters. When this command complete, the response contain
289 * Option rom version of the HBA. This function translate the version number
290 * into a human readable string and store it in OptionROMVersion.
291 **/
292static void
293lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
294{
295 struct prog_id *prg;
296 uint32_t prog_id_word;
297 char dist = ' ';
298 /* character array used for decoding dist type. */
299 char dist_char[] = "nabx";
300
James Smart04c68492009-05-22 14:52:52 -0400301 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
James Smart9f1e1b52008-12-04 22:39:40 -0500302 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500303 return;
James Smart9f1e1b52008-12-04 22:39:40 -0500304 }
James Smart97207482008-12-04 22:39:19 -0500305
306 prg = (struct prog_id *) &prog_id_word;
307
308 /* word 7 contain option rom version */
James Smart04c68492009-05-22 14:52:52 -0400309 prog_id_word = pmboxq->u.mb.un.varWords[7];
James Smart97207482008-12-04 22:39:19 -0500310
311 /* Decode the Option rom version word to a readable string */
312 if (prg->dist < 4)
313 dist = dist_char[prg->dist];
314
315 if ((prg->dist == 3) && (prg->num == 0))
James Smarta2fc4aef2014-09-03 12:57:55 -0400316 snprintf(phba->OptionROMVersion, 32, "%d.%d%d",
James Smart97207482008-12-04 22:39:19 -0500317 prg->ver, prg->rev, prg->lev);
318 else
James Smarta2fc4aef2014-09-03 12:57:55 -0400319 snprintf(phba->OptionROMVersion, 32, "%d.%d%d%c%d",
James Smart97207482008-12-04 22:39:19 -0500320 prg->ver, prg->rev, prg->lev,
321 dist, prg->num);
James Smart9f1e1b52008-12-04 22:39:40 -0500322 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500323 return;
324}
325
326/**
James Smart05580562011-05-24 11:40:48 -0400327 * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
328 * cfg_soft_wwnn, cfg_soft_wwpn
329 * @vport: pointer to lpfc vport data structure.
330 *
331 *
332 * Return codes
333 * None.
334 **/
335void
336lpfc_update_vport_wwn(struct lpfc_vport *vport)
337{
338 /* If the soft name exists then update it using the service params */
339 if (vport->phba->cfg_soft_wwnn)
340 u64_to_wwn(vport->phba->cfg_soft_wwnn,
341 vport->fc_sparam.nodeName.u.wwn);
342 if (vport->phba->cfg_soft_wwpn)
343 u64_to_wwn(vport->phba->cfg_soft_wwpn,
344 vport->fc_sparam.portName.u.wwn);
345
346 /*
347 * If the name is empty or there exists a soft name
348 * then copy the service params name, otherwise use the fc name
349 */
350 if (vport->fc_nodename.u.wwn[0] == 0 || vport->phba->cfg_soft_wwnn)
351 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
352 sizeof(struct lpfc_name));
353 else
354 memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
355 sizeof(struct lpfc_name));
356
357 if (vport->fc_portname.u.wwn[0] == 0 || vport->phba->cfg_soft_wwpn)
358 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
359 sizeof(struct lpfc_name));
360 else
361 memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
362 sizeof(struct lpfc_name));
363}
364
365/**
James Smart3621a712009-04-06 18:47:14 -0400366 * lpfc_config_port_post - Perform lpfc initialization after config port
James Smarte59058c2008-08-24 21:49:00 -0400367 * @phba: pointer to lpfc hba data structure.
368 *
369 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
370 * command call. It performs all internal resource and state setups on the
371 * port: post IOCB buffers, enable appropriate host interrupt attentions,
372 * ELS ring timers, etc.
373 *
374 * Return codes
375 * 0 - success.
376 * Any other value - error.
377 **/
dea31012005-04-17 16:05:31 -0500378int
James Smart2e0fef82007-06-17 19:56:36 -0500379lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500380{
James Smart2e0fef82007-06-17 19:56:36 -0500381 struct lpfc_vport *vport = phba->pport;
James Smarta257bf92009-04-06 18:48:10 -0400382 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -0500383 LPFC_MBOXQ_t *pmb;
384 MAILBOX_t *mb;
385 struct lpfc_dmabuf *mp;
386 struct lpfc_sli *psli = &phba->sli;
387 uint32_t status, timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500388 int i, j;
389 int rc;
dea31012005-04-17 16:05:31 -0500390
James Smart7af67052007-10-27 13:38:11 -0400391 spin_lock_irq(&phba->hbalock);
392 /*
393 * If the Config port completed correctly the HBA is not
394 * over heated any more.
395 */
396 if (phba->over_temp_state == HBA_OVER_TEMP)
397 phba->over_temp_state = HBA_NORMAL_TEMP;
398 spin_unlock_irq(&phba->hbalock);
399
dea31012005-04-17 16:05:31 -0500400 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
401 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500402 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500403 return -ENOMEM;
404 }
James Smart04c68492009-05-22 14:52:52 -0400405 mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500406
dea31012005-04-17 16:05:31 -0500407 /* Get login parameters for NID. */
James Smart9f1177a2010-02-26 14:12:57 -0500408 rc = lpfc_read_sparam(phba, pmb, 0);
409 if (rc) {
410 mempool_free(pmb, phba->mbox_mem_pool);
411 return -ENOMEM;
412 }
413
James Smarted957682007-06-17 19:56:37 -0500414 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500415 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500416 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400417 "0448 Adapter failed init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500418 "READ_SPARM mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500419 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500420 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500421 mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart9f1177a2010-02-26 14:12:57 -0500422 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500423 lpfc_mbuf_free(phba, mp->virt, mp->phys);
424 kfree(mp);
425 return -EIO;
426 }
427
428 mp = (struct lpfc_dmabuf *) pmb->context1;
429
James Smart2e0fef82007-06-17 19:56:36 -0500430 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500431 lpfc_mbuf_free(phba, mp->virt, mp->phys);
432 kfree(mp);
433 pmb->context1 = NULL;
James Smart05580562011-05-24 11:40:48 -0400434 lpfc_update_vport_wwn(vport);
James Smarta257bf92009-04-06 18:48:10 -0400435
436 /* Update the fc_host data structures with new wwn. */
437 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
438 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart21e9a0a2009-05-22 14:53:21 -0400439 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smarta257bf92009-04-06 18:48:10 -0400440
dea31012005-04-17 16:05:31 -0500441 /* If no serial number in VPD data, use low 6 bytes of WWNN */
442 /* This should be consolidated into parse_vpd ? - mr */
443 if (phba->SerialNumber[0] == 0) {
444 uint8_t *outptr;
445
James Smart2e0fef82007-06-17 19:56:36 -0500446 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500447 for (i = 0; i < 12; i++) {
448 status = *outptr++;
449 j = ((status & 0xf0) >> 4);
450 if (j <= 9)
451 phba->SerialNumber[i] =
452 (char)((uint8_t) 0x30 + (uint8_t) j);
453 else
454 phba->SerialNumber[i] =
455 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
456 i++;
457 j = (status & 0xf);
458 if (j <= 9)
459 phba->SerialNumber[i] =
460 (char)((uint8_t) 0x30 + (uint8_t) j);
461 else
462 phba->SerialNumber[i] =
463 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
464 }
465 }
466
dea31012005-04-17 16:05:31 -0500467 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500468 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500469 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500470 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400471 "0453 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500472 "READ_CONFIG, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500473 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500474 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500475 mempool_free( pmb, phba->mbox_mem_pool);
476 return -EIO;
477 }
478
James Smarta0c87cb2009-07-19 10:01:10 -0400479 /* Check if the port is disabled */
480 lpfc_sli_read_link_ste(phba);
481
dea31012005-04-17 16:05:31 -0500482 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -0400483 i = (mb->un.varRdConfig.max_xri + 1);
484 if (phba->cfg_hba_queue_depth > i) {
485 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
486 "3359 HBA queue depth changed from %d to %d\n",
487 phba->cfg_hba_queue_depth, i);
488 phba->cfg_hba_queue_depth = i;
489 }
490
491 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
492 i = (mb->un.varRdConfig.max_xri >> 3);
493 if (phba->pport->cfg_lun_queue_depth > i) {
494 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
495 "3360 LUN queue depth changed from %d to %d\n",
496 phba->pport->cfg_lun_queue_depth, i);
497 phba->pport->cfg_lun_queue_depth = i;
498 }
dea31012005-04-17 16:05:31 -0500499
500 phba->lmt = mb->un.varRdConfig.lmt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500501
502 /* Get the default values for Model Name and Description */
503 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
504
James Smart2e0fef82007-06-17 19:56:36 -0500505 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500506
James Smart0b727fe2007-10-27 13:37:25 -0400507 /* Only process IOCBs on ELS ring till hba_state is READY */
James Smart895427b2017-02-12 13:52:30 -0800508 if (psli->sli3_ring[LPFC_EXTRA_RING].sli.sli3.cmdringaddr)
509 psli->sli3_ring[LPFC_EXTRA_RING].flag |= LPFC_STOP_IOCB_EVENT;
510 if (psli->sli3_ring[LPFC_FCP_RING].sli.sli3.cmdringaddr)
511 psli->sli3_ring[LPFC_FCP_RING].flag |= LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500512
513 /* Post receive buffers for desired rings */
James Smarted957682007-06-17 19:56:37 -0500514 if (phba->sli_rev != 3)
515 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500516
James Smart93996272008-08-24 21:50:30 -0400517 /*
518 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
519 */
520 if (phba->intr_type == MSIX) {
521 rc = lpfc_config_msi(phba, pmb);
522 if (rc) {
523 mempool_free(pmb, phba->mbox_mem_pool);
524 return -EIO;
525 }
526 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
527 if (rc != MBX_SUCCESS) {
528 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
529 "0352 Config MSI mailbox command "
530 "failed, mbxCmd x%x, mbxStatus x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400531 pmb->u.mb.mbxCommand,
532 pmb->u.mb.mbxStatus);
James Smart93996272008-08-24 21:50:30 -0400533 mempool_free(pmb, phba->mbox_mem_pool);
534 return -EIO;
535 }
536 }
537
James Smart04c68492009-05-22 14:52:52 -0400538 spin_lock_irq(&phba->hbalock);
James Smart93996272008-08-24 21:50:30 -0400539 /* Initialize ERATT handling flag */
540 phba->hba_flag &= ~HBA_ERATT_HANDLED;
541
dea31012005-04-17 16:05:31 -0500542 /* Enable appropriate host interrupts */
James Smart9940b972011-03-11 16:06:12 -0500543 if (lpfc_readl(phba->HCregaddr, &status)) {
544 spin_unlock_irq(&phba->hbalock);
545 return -EIO;
546 }
dea31012005-04-17 16:05:31 -0500547 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
548 if (psli->num_rings > 0)
549 status |= HC_R0INT_ENA;
550 if (psli->num_rings > 1)
551 status |= HC_R1INT_ENA;
552 if (psli->num_rings > 2)
553 status |= HC_R2INT_ENA;
554 if (psli->num_rings > 3)
555 status |= HC_R3INT_ENA;
556
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500557 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
558 (phba->cfg_poll & DISABLE_FCP_RING_INT))
James Smart93996272008-08-24 21:50:30 -0400559 status &= ~(HC_R0INT_ENA);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500560
dea31012005-04-17 16:05:31 -0500561 writel(status, phba->HCregaddr);
562 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500563 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500564
James Smart93996272008-08-24 21:50:30 -0400565 /* Set up ring-0 (ELS) timer */
566 timeout = phba->fc_ratov * 2;
James Smart256ec0d2013-04-17 20:14:58 -0400567 mod_timer(&vport->els_tmofunc,
568 jiffies + msecs_to_jiffies(1000 * timeout));
James Smart93996272008-08-24 21:50:30 -0400569 /* Set up heart beat (HB) timer */
James Smart256ec0d2013-04-17 20:14:58 -0400570 mod_timer(&phba->hb_tmofunc,
571 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -0500572 phba->hb_outstanding = 0;
573 phba->last_completion_time = jiffies;
James Smart93996272008-08-24 21:50:30 -0400574 /* Set up error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -0400575 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -0700576 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
dea31012005-04-17 16:05:31 -0500577
James Smarta0c87cb2009-07-19 10:01:10 -0400578 if (phba->hba_flag & LINK_DISABLED) {
579 lpfc_printf_log(phba,
580 KERN_ERR, LOG_INIT,
581 "2598 Adapter Link is disabled.\n");
582 lpfc_down_link(phba, pmb);
583 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
584 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
585 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
586 lpfc_printf_log(phba,
587 KERN_ERR, LOG_INIT,
588 "2599 Adapter failed to issue DOWN_LINK"
589 " mbox command rc 0x%x\n", rc);
590
591 mempool_free(pmb, phba->mbox_mem_pool);
592 return -EIO;
593 }
James Smarte40a02c2010-02-26 14:13:54 -0500594 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart026abb82011-12-13 13:20:45 -0500595 mempool_free(pmb, phba->mbox_mem_pool);
596 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
597 if (rc)
598 return rc;
dea31012005-04-17 16:05:31 -0500599 }
600 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400601 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500602 if (!pmb) {
603 phba->link_state = LPFC_HBA_ERROR;
604 return -ENOMEM;
605 }
606
James Smart57127f12007-10-27 13:37:05 -0400607 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
608 pmb->mbox_cmpl = lpfc_config_async_cmpl;
609 pmb->vport = phba->pport;
610 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500611
James Smart57127f12007-10-27 13:37:05 -0400612 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
613 lpfc_printf_log(phba,
614 KERN_ERR,
615 LOG_INIT,
616 "0456 Adapter failed to issue "
James Smarte4e74272009-07-19 10:01:38 -0400617 "ASYNCEVT_ENABLE mbox status x%x\n",
James Smart57127f12007-10-27 13:37:05 -0400618 rc);
619 mempool_free(pmb, phba->mbox_mem_pool);
620 }
James Smart97207482008-12-04 22:39:19 -0500621
622 /* Get Option rom version */
623 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500624 if (!pmb) {
625 phba->link_state = LPFC_HBA_ERROR;
626 return -ENOMEM;
627 }
628
James Smart97207482008-12-04 22:39:19 -0500629 lpfc_dump_wakeup_param(phba, pmb);
630 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
631 pmb->vport = phba->pport;
632 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
633
634 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
635 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
James Smarte4e74272009-07-19 10:01:38 -0400636 "to get Option ROM version status x%x\n", rc);
James Smart97207482008-12-04 22:39:19 -0500637 mempool_free(pmb, phba->mbox_mem_pool);
638 }
639
James Smartd7c255b2008-08-24 21:50:00 -0400640 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400641}
642
James Smarte59058c2008-08-24 21:49:00 -0400643/**
James Smart84d1b002010-02-12 14:42:33 -0500644 * lpfc_hba_init_link - Initialize the FC link
645 * @phba: pointer to lpfc hba data structure.
James Smart6e7288d2010-06-07 15:23:35 -0400646 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500647 *
648 * This routine will issue the INIT_LINK mailbox command call.
649 * It is available to other drivers through the lpfc_hba data
650 * structure for use as a delayed link up mechanism with the
651 * module parameter lpfc_suppress_link_up.
652 *
653 * Return code
654 * 0 - success
655 * Any other value - error
656 **/
Rashika Kheriae399b222014-09-03 12:55:28 -0400657static int
James Smart6e7288d2010-06-07 15:23:35 -0400658lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500659{
James Smart1b511972011-12-13 13:23:09 -0500660 return lpfc_hba_init_link_fc_topology(phba, phba->cfg_topology, flag);
661}
662
663/**
664 * lpfc_hba_init_link_fc_topology - Initialize FC link with desired topology
665 * @phba: pointer to lpfc hba data structure.
666 * @fc_topology: desired fc topology.
667 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
668 *
669 * This routine will issue the INIT_LINK mailbox command call.
670 * It is available to other drivers through the lpfc_hba data
671 * structure for use as a delayed link up mechanism with the
672 * module parameter lpfc_suppress_link_up.
673 *
674 * Return code
675 * 0 - success
676 * Any other value - error
677 **/
678int
679lpfc_hba_init_link_fc_topology(struct lpfc_hba *phba, uint32_t fc_topology,
680 uint32_t flag)
681{
James Smart84d1b002010-02-12 14:42:33 -0500682 struct lpfc_vport *vport = phba->pport;
683 LPFC_MBOXQ_t *pmb;
684 MAILBOX_t *mb;
685 int rc;
686
687 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
688 if (!pmb) {
689 phba->link_state = LPFC_HBA_ERROR;
690 return -ENOMEM;
691 }
692 mb = &pmb->u.mb;
693 pmb->vport = vport;
694
James Smart026abb82011-12-13 13:20:45 -0500695 if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_MAX) ||
696 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G) &&
697 !(phba->lmt & LMT_1Gb)) ||
698 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G) &&
699 !(phba->lmt & LMT_2Gb)) ||
700 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G) &&
701 !(phba->lmt & LMT_4Gb)) ||
702 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G) &&
703 !(phba->lmt & LMT_8Gb)) ||
704 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G) &&
705 !(phba->lmt & LMT_10Gb)) ||
706 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G) &&
James Smartd38dd522015-08-31 16:48:17 -0400707 !(phba->lmt & LMT_16Gb)) ||
708 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_32G) &&
709 !(phba->lmt & LMT_32Gb))) {
James Smart026abb82011-12-13 13:20:45 -0500710 /* Reset link speed to auto */
711 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
712 "1302 Invalid speed for this board:%d "
713 "Reset link speed to auto.\n",
714 phba->cfg_link_speed);
715 phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
716 }
James Smart1b511972011-12-13 13:23:09 -0500717 lpfc_init_link(phba, pmb, fc_topology, phba->cfg_link_speed);
James Smart84d1b002010-02-12 14:42:33 -0500718 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart1b511972011-12-13 13:23:09 -0500719 if (phba->sli_rev < LPFC_SLI_REV4)
720 lpfc_set_loopback_flag(phba);
James Smart6e7288d2010-06-07 15:23:35 -0400721 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart76a95d72010-11-20 23:11:48 -0500722 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
James Smart84d1b002010-02-12 14:42:33 -0500723 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
724 "0498 Adapter failed to init, mbxCmd x%x "
725 "INIT_LINK, mbxStatus x%x\n",
726 mb->mbxCommand, mb->mbxStatus);
James Smart76a95d72010-11-20 23:11:48 -0500727 if (phba->sli_rev <= LPFC_SLI_REV3) {
728 /* Clear all interrupt enable conditions */
729 writel(0, phba->HCregaddr);
730 readl(phba->HCregaddr); /* flush */
731 /* Clear all pending interrupts */
732 writel(0xffffffff, phba->HAregaddr);
733 readl(phba->HAregaddr); /* flush */
734 }
James Smart84d1b002010-02-12 14:42:33 -0500735 phba->link_state = LPFC_HBA_ERROR;
James Smart6e7288d2010-06-07 15:23:35 -0400736 if (rc != MBX_BUSY || flag == MBX_POLL)
James Smart84d1b002010-02-12 14:42:33 -0500737 mempool_free(pmb, phba->mbox_mem_pool);
738 return -EIO;
739 }
James Smarte40a02c2010-02-26 14:13:54 -0500740 phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
James Smart6e7288d2010-06-07 15:23:35 -0400741 if (flag == MBX_POLL)
742 mempool_free(pmb, phba->mbox_mem_pool);
James Smart84d1b002010-02-12 14:42:33 -0500743
744 return 0;
745}
746
747/**
748 * lpfc_hba_down_link - this routine downs the FC link
James Smart6e7288d2010-06-07 15:23:35 -0400749 * @phba: pointer to lpfc hba data structure.
750 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500751 *
752 * This routine will issue the DOWN_LINK mailbox command call.
753 * It is available to other drivers through the lpfc_hba data
754 * structure for use to stop the link.
755 *
756 * Return code
757 * 0 - success
758 * Any other value - error
759 **/
Rashika Kheriae399b222014-09-03 12:55:28 -0400760static int
James Smart6e7288d2010-06-07 15:23:35 -0400761lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500762{
763 LPFC_MBOXQ_t *pmb;
764 int rc;
765
766 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
767 if (!pmb) {
768 phba->link_state = LPFC_HBA_ERROR;
769 return -ENOMEM;
770 }
771
772 lpfc_printf_log(phba,
773 KERN_ERR, LOG_INIT,
774 "0491 Adapter Link is disabled.\n");
775 lpfc_down_link(phba, pmb);
776 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6e7288d2010-06-07 15:23:35 -0400777 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart84d1b002010-02-12 14:42:33 -0500778 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
779 lpfc_printf_log(phba,
780 KERN_ERR, LOG_INIT,
781 "2522 Adapter failed to issue DOWN_LINK"
782 " mbox command rc 0x%x\n", rc);
783
784 mempool_free(pmb, phba->mbox_mem_pool);
785 return -EIO;
786 }
James Smart6e7288d2010-06-07 15:23:35 -0400787 if (flag == MBX_POLL)
788 mempool_free(pmb, phba->mbox_mem_pool);
789
James Smart84d1b002010-02-12 14:42:33 -0500790 return 0;
791}
792
793/**
James Smart3621a712009-04-06 18:47:14 -0400794 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400795 * @phba: pointer to lpfc HBA data structure.
796 *
797 * This routine will do LPFC uninitialization before the HBA is reset when
798 * bringing down the SLI Layer.
799 *
800 * Return codes
801 * 0 - success.
802 * Any other value - error.
803 **/
dea31012005-04-17 16:05:31 -0500804int
James Smart2e0fef82007-06-17 19:56:36 -0500805lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500806{
James Smart1b32f6a2008-02-08 18:49:39 -0500807 struct lpfc_vport **vports;
808 int i;
James Smart3772a992009-05-22 14:50:54 -0400809
810 if (phba->sli_rev <= LPFC_SLI_REV3) {
811 /* Disable interrupts */
812 writel(0, phba->HCregaddr);
813 readl(phba->HCregaddr); /* flush */
814 }
dea31012005-04-17 16:05:31 -0500815
James Smart1b32f6a2008-02-08 18:49:39 -0500816 if (phba->pport->load_flag & FC_UNLOADING)
817 lpfc_cleanup_discovery_resources(phba->pport);
818 else {
819 vports = lpfc_create_vport_work_array(phba);
820 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400821 for (i = 0; i <= phba->max_vports &&
822 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500823 lpfc_cleanup_discovery_resources(vports[i]);
824 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500825 }
826 return 0;
dea31012005-04-17 16:05:31 -0500827}
828
James Smarte59058c2008-08-24 21:49:00 -0400829/**
James Smart68e814f2014-05-21 08:04:59 -0400830 * lpfc_sli4_free_sp_events - Cleanup sp_queue_events to free
831 * rspiocb which got deferred
832 *
833 * @phba: pointer to lpfc HBA data structure.
834 *
835 * This routine will cleanup completed slow path events after HBA is reset
836 * when bringing down the SLI Layer.
837 *
838 *
839 * Return codes
840 * void.
841 **/
842static void
843lpfc_sli4_free_sp_events(struct lpfc_hba *phba)
844{
845 struct lpfc_iocbq *rspiocbq;
846 struct hbq_dmabuf *dmabuf;
847 struct lpfc_cq_event *cq_event;
848
849 spin_lock_irq(&phba->hbalock);
850 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
851 spin_unlock_irq(&phba->hbalock);
852
853 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
854 /* Get the response iocb from the head of work queue */
855 spin_lock_irq(&phba->hbalock);
856 list_remove_head(&phba->sli4_hba.sp_queue_event,
857 cq_event, struct lpfc_cq_event, list);
858 spin_unlock_irq(&phba->hbalock);
859
860 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
861 case CQE_CODE_COMPL_WQE:
862 rspiocbq = container_of(cq_event, struct lpfc_iocbq,
863 cq_event);
864 lpfc_sli_release_iocbq(phba, rspiocbq);
865 break;
866 case CQE_CODE_RECEIVE:
867 case CQE_CODE_RECEIVE_V1:
868 dmabuf = container_of(cq_event, struct hbq_dmabuf,
869 cq_event);
870 lpfc_in_buf_free(phba, &dmabuf->dbuf);
871 }
872 }
873}
874
875/**
James Smartbcece5f2014-04-04 13:51:34 -0400876 * lpfc_hba_free_post_buf - Perform lpfc uninitialization after HBA reset
877 * @phba: pointer to lpfc HBA data structure.
878 *
879 * This routine will cleanup posted ELS buffers after the HBA is reset
880 * when bringing down the SLI Layer.
881 *
882 *
883 * Return codes
884 * void.
885 **/
886static void
887lpfc_hba_free_post_buf(struct lpfc_hba *phba)
888{
889 struct lpfc_sli *psli = &phba->sli;
890 struct lpfc_sli_ring *pring;
891 struct lpfc_dmabuf *mp, *next_mp;
James Smart07eab622014-04-04 13:51:44 -0400892 LIST_HEAD(buflist);
893 int count;
James Smartbcece5f2014-04-04 13:51:34 -0400894
895 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
896 lpfc_sli_hbqbuf_free_all(phba);
897 else {
898 /* Cleanup preposted buffers on the ELS ring */
James Smart895427b2017-02-12 13:52:30 -0800899 pring = &psli->sli3_ring[LPFC_ELS_RING];
James Smart07eab622014-04-04 13:51:44 -0400900 spin_lock_irq(&phba->hbalock);
901 list_splice_init(&pring->postbufq, &buflist);
902 spin_unlock_irq(&phba->hbalock);
903
904 count = 0;
905 list_for_each_entry_safe(mp, next_mp, &buflist, list) {
James Smartbcece5f2014-04-04 13:51:34 -0400906 list_del(&mp->list);
James Smart07eab622014-04-04 13:51:44 -0400907 count++;
James Smartbcece5f2014-04-04 13:51:34 -0400908 lpfc_mbuf_free(phba, mp->virt, mp->phys);
909 kfree(mp);
910 }
James Smart07eab622014-04-04 13:51:44 -0400911
912 spin_lock_irq(&phba->hbalock);
913 pring->postbufq_cnt -= count;
James Smartbcece5f2014-04-04 13:51:34 -0400914 spin_unlock_irq(&phba->hbalock);
915 }
916}
917
918/**
919 * lpfc_hba_clean_txcmplq - Perform lpfc uninitialization after HBA reset
920 * @phba: pointer to lpfc HBA data structure.
921 *
922 * This routine will cleanup the txcmplq after the HBA is reset when bringing
923 * down the SLI Layer.
924 *
925 * Return codes
926 * void
927 **/
928static void
929lpfc_hba_clean_txcmplq(struct lpfc_hba *phba)
930{
931 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -0800932 struct lpfc_queue *qp = NULL;
James Smartbcece5f2014-04-04 13:51:34 -0400933 struct lpfc_sli_ring *pring;
934 LIST_HEAD(completions);
935 int i;
936
James Smart895427b2017-02-12 13:52:30 -0800937 if (phba->sli_rev != LPFC_SLI_REV4) {
938 for (i = 0; i < psli->num_rings; i++) {
939 pring = &psli->sli3_ring[i];
James Smartbcece5f2014-04-04 13:51:34 -0400940 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -0800941 /* At this point in time the HBA is either reset or DOA
942 * Nothing should be on txcmplq as it will
943 * NEVER complete.
944 */
945 list_splice_init(&pring->txcmplq, &completions);
946 pring->txcmplq_cnt = 0;
James Smartbcece5f2014-04-04 13:51:34 -0400947 spin_unlock_irq(&phba->hbalock);
948
James Smart895427b2017-02-12 13:52:30 -0800949 lpfc_sli_abort_iocb_ring(phba, pring);
950 }
James Smartbcece5f2014-04-04 13:51:34 -0400951 /* Cancel all the IOCBs from the completions list */
James Smart895427b2017-02-12 13:52:30 -0800952 lpfc_sli_cancel_iocbs(phba, &completions,
953 IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
954 return;
955 }
956 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
957 pring = qp->pring;
958 if (!pring)
959 continue;
960 spin_lock_irq(&pring->ring_lock);
961 list_splice_init(&pring->txcmplq, &completions);
962 pring->txcmplq_cnt = 0;
963 spin_unlock_irq(&pring->ring_lock);
James Smartbcece5f2014-04-04 13:51:34 -0400964 lpfc_sli_abort_iocb_ring(phba, pring);
965 }
James Smart895427b2017-02-12 13:52:30 -0800966 /* Cancel all the IOCBs from the completions list */
967 lpfc_sli_cancel_iocbs(phba, &completions,
968 IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
James Smartbcece5f2014-04-04 13:51:34 -0400969}
970
971/**
James Smart3772a992009-05-22 14:50:54 -0400972 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smartbcece5f2014-04-04 13:51:34 -0400973 int i;
James Smarte59058c2008-08-24 21:49:00 -0400974 * @phba: pointer to lpfc HBA data structure.
975 *
976 * This routine will do uninitialization after the HBA is reset when bring
977 * down the SLI Layer.
978 *
979 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200980 * 0 - success.
James Smarte59058c2008-08-24 21:49:00 -0400981 * Any other value - error.
982 **/
James Smart3772a992009-05-22 14:50:54 -0400983static int
984lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -0500985{
James Smartbcece5f2014-04-04 13:51:34 -0400986 lpfc_hba_free_post_buf(phba);
987 lpfc_hba_clean_txcmplq(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500988 return 0;
989}
James Smart5af5eee2010-10-22 11:06:38 -0400990
James Smartda0436e2009-05-22 14:51:39 -0400991/**
992 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
993 * @phba: pointer to lpfc HBA data structure.
994 *
995 * This routine will do uninitialization after the HBA is reset when bring
996 * down the SLI Layer.
997 *
998 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200999 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001000 * Any other value - error.
1001 **/
1002static int
1003lpfc_hba_down_post_s4(struct lpfc_hba *phba)
1004{
1005 struct lpfc_scsi_buf *psb, *psb_next;
1006 LIST_HEAD(aborts);
James Smart895427b2017-02-12 13:52:30 -08001007 LIST_HEAD(nvme_aborts);
James Smartda0436e2009-05-22 14:51:39 -04001008 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -05001009 struct lpfc_sglq *sglq_entry = NULL;
1010
James Smart895427b2017-02-12 13:52:30 -08001011
1012 lpfc_sli_hbqbuf_free_all(phba);
James Smartbcece5f2014-04-04 13:51:34 -04001013 lpfc_hba_clean_txcmplq(phba);
1014
James Smartda0436e2009-05-22 14:51:39 -04001015 /* At this point in time the HBA is either reset or DOA. Either
1016 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
James Smart895427b2017-02-12 13:52:30 -08001017 * on the lpfc_els_sgl_list so that it can either be freed if the
James Smartda0436e2009-05-22 14:51:39 -04001018 * driver is unloading or reposted if the driver is restarting
1019 * the port.
1020 */
James Smart895427b2017-02-12 13:52:30 -08001021 spin_lock_irq(&phba->hbalock); /* required for lpfc_els_sgl_list and */
James Smartda0436e2009-05-22 14:51:39 -04001022 /* scsl_buf_list */
James Smart895427b2017-02-12 13:52:30 -08001023 /* sgl_list_lock required because worker thread uses this
James Smartda0436e2009-05-22 14:51:39 -04001024 * list.
1025 */
James Smart895427b2017-02-12 13:52:30 -08001026 spin_lock(&phba->sli4_hba.sgl_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -05001027 list_for_each_entry(sglq_entry,
1028 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
1029 sglq_entry->state = SGL_FREED;
James Smartf358dd02017-02-12 13:52:34 -08001030 list_for_each_entry(sglq_entry,
1031 &phba->sli4_hba.lpfc_abts_nvmet_sgl_list, list)
1032 sglq_entry->state = SGL_FREED;
James Smart0f65ff62010-02-26 14:14:23 -05001033
James Smartda0436e2009-05-22 14:51:39 -04001034 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
James Smart895427b2017-02-12 13:52:30 -08001035 &phba->sli4_hba.lpfc_els_sgl_list);
1036
James Smartf358dd02017-02-12 13:52:34 -08001037 if (phba->sli4_hba.nvme_wq)
1038 list_splice_init(&phba->sli4_hba.lpfc_abts_nvmet_sgl_list,
1039 &phba->sli4_hba.lpfc_nvmet_sgl_list);
1040
James Smart895427b2017-02-12 13:52:30 -08001041 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04001042 /* abts_scsi_buf_list_lock required because worker thread uses this
1043 * list.
1044 */
James Smart895427b2017-02-12 13:52:30 -08001045 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
1046 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
1047 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
1048 &aborts);
1049 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
1050 }
1051
1052 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
1053 spin_lock(&phba->sli4_hba.abts_nvme_buf_list_lock);
1054 list_splice_init(&phba->sli4_hba.lpfc_abts_nvme_buf_list,
1055 &nvme_aborts);
1056 spin_unlock(&phba->sli4_hba.abts_nvme_buf_list_lock);
1057 }
1058
James Smartda0436e2009-05-22 14:51:39 -04001059 spin_unlock_irq(&phba->hbalock);
1060
1061 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
1062 psb->pCmd = NULL;
1063 psb->status = IOSTAT_SUCCESS;
1064 }
James Smarta40fc5f2013-04-17 20:17:40 -04001065 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
1066 list_splice(&aborts, &phba->lpfc_scsi_buf_list_put);
1067 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
James Smart68e814f2014-05-21 08:04:59 -04001068
James Smart895427b2017-02-12 13:52:30 -08001069 list_for_each_entry_safe(psb, psb_next, &nvme_aborts, list) {
1070 psb->pCmd = NULL;
1071 psb->status = IOSTAT_SUCCESS;
1072 }
1073 spin_lock_irqsave(&phba->nvme_buf_list_put_lock, iflag);
1074 list_splice(&nvme_aborts, &phba->lpfc_nvme_buf_list_put);
1075 spin_unlock_irqrestore(&phba->nvme_buf_list_put_lock, iflag);
1076
James Smart68e814f2014-05-21 08:04:59 -04001077 lpfc_sli4_free_sp_events(phba);
James Smartda0436e2009-05-22 14:51:39 -04001078 return 0;
1079}
1080
1081/**
1082 * lpfc_hba_down_post - Wrapper func for hba down post routine
1083 * @phba: pointer to lpfc HBA data structure.
1084 *
1085 * This routine wraps the actual SLI3 or SLI4 routine for performing
1086 * uninitialization after the HBA is reset when bring down the SLI Layer.
1087 *
1088 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001089 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001090 * Any other value - error.
1091 **/
1092int
1093lpfc_hba_down_post(struct lpfc_hba *phba)
1094{
1095 return (*phba->lpfc_hba_down_post)(phba);
1096}
Jamie Wellnitz41415862006-02-28 19:25:27 -05001097
James Smarte59058c2008-08-24 21:49:00 -04001098/**
James Smart3621a712009-04-06 18:47:14 -04001099 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001100 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1101 *
1102 * This is the HBA-timer timeout handler registered to the lpfc driver. When
1103 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
1104 * work-port-events bitmap and the worker thread is notified. This timeout
1105 * event will be used by the worker thread to invoke the actual timeout
1106 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
1107 * be performed in the timeout handler and the HBA timeout event bit shall
1108 * be cleared by the worker thread after it has taken the event bitmap out.
1109 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001110static void
James Smart858c9f62007-06-17 19:56:39 -05001111lpfc_hb_timeout(unsigned long ptr)
1112{
1113 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -04001114 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -05001115 unsigned long iflag;
1116
1117 phba = (struct lpfc_hba *)ptr;
James Smart93996272008-08-24 21:50:30 -04001118
1119 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -05001120 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04001121 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
1122 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -05001123 phba->pport->work_port_events |= WORKER_HB_TMO;
1124 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
1125
James Smart93996272008-08-24 21:50:30 -04001126 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -04001127 if (!tmo_posted)
1128 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -05001129 return;
1130}
1131
James Smarte59058c2008-08-24 21:49:00 -04001132/**
James Smart19ca7602010-11-20 23:11:55 -05001133 * lpfc_rrq_timeout - The RRQ-timer timeout handler
1134 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1135 *
1136 * This is the RRQ-timer timeout handler registered to the lpfc driver. When
1137 * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
1138 * work-port-events bitmap and the worker thread is notified. This timeout
1139 * event will be used by the worker thread to invoke the actual timeout
1140 * handler routine, lpfc_rrq_handler. Any periodical operations will
1141 * be performed in the timeout handler and the RRQ timeout event bit shall
1142 * be cleared by the worker thread after it has taken the event bitmap out.
1143 **/
1144static void
1145lpfc_rrq_timeout(unsigned long ptr)
1146{
1147 struct lpfc_hba *phba;
James Smart19ca7602010-11-20 23:11:55 -05001148 unsigned long iflag;
1149
1150 phba = (struct lpfc_hba *)ptr;
1151 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001152 if (!(phba->pport->load_flag & FC_UNLOADING))
1153 phba->hba_flag |= HBA_RRQ_ACTIVE;
1154 else
1155 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart19ca7602010-11-20 23:11:55 -05001156 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001157
1158 if (!(phba->pport->load_flag & FC_UNLOADING))
1159 lpfc_worker_wake_up(phba);
James Smart19ca7602010-11-20 23:11:55 -05001160}
1161
1162/**
James Smart3621a712009-04-06 18:47:14 -04001163 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -04001164 * @phba: pointer to lpfc hba data structure.
1165 * @pmboxq: pointer to the driver internal queue element for mailbox command.
1166 *
1167 * This is the callback function to the lpfc heart-beat mailbox command.
1168 * If configured, the lpfc driver issues the heart-beat mailbox command to
1169 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
1170 * heart-beat mailbox command is issued, the driver shall set up heart-beat
1171 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
1172 * heart-beat outstanding state. Once the mailbox command comes back and
1173 * no error conditions detected, the heart-beat mailbox command timer is
1174 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1175 * state is cleared for the next heart-beat. If the timer expired with the
1176 * heart-beat outstanding state set, the driver will put the HBA offline.
1177 **/
James Smart858c9f62007-06-17 19:56:39 -05001178static void
1179lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1180{
1181 unsigned long drvr_flag;
1182
1183 spin_lock_irqsave(&phba->hbalock, drvr_flag);
1184 phba->hb_outstanding = 0;
1185 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1186
James Smart93996272008-08-24 21:50:30 -04001187 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -05001188 mempool_free(pmboxq, phba->mbox_mem_pool);
1189 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1190 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -04001191 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -05001192 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001193 jiffies +
1194 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001195 return;
1196}
1197
James Smarte59058c2008-08-24 21:49:00 -04001198/**
James Smart3621a712009-04-06 18:47:14 -04001199 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001200 * @phba: pointer to lpfc hba data structure.
1201 *
1202 * This is the actual HBA-timer timeout handler to be invoked by the worker
1203 * thread whenever the HBA timer fired and HBA-timeout event posted. This
1204 * handler performs any periodic operations needed for the device. If such
1205 * periodic event has already been attended to either in the interrupt handler
1206 * or by processing slow-ring or fast-ring events within the HBA-timer
1207 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1208 * the timer for the next timeout period. If lpfc heart-beat mailbox command
1209 * is configured and there is no heart-beat mailbox command outstanding, a
1210 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1211 * has been a heart-beat mailbox command outstanding, the HBA shall be put
1212 * to offline.
1213 **/
James Smart858c9f62007-06-17 19:56:39 -05001214void
1215lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1216{
James Smart45ed1192009-10-02 15:17:02 -04001217 struct lpfc_vport **vports;
James Smart858c9f62007-06-17 19:56:39 -05001218 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -05001219 struct lpfc_dmabuf *buf_ptr;
James Smart45ed1192009-10-02 15:17:02 -04001220 int retval, i;
James Smart858c9f62007-06-17 19:56:39 -05001221 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -05001222 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -05001223
James Smart45ed1192009-10-02 15:17:02 -04001224 vports = lpfc_create_vport_work_array(phba);
1225 if (vports != NULL)
James Smart4258e982015-12-16 18:11:58 -05001226 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart45ed1192009-10-02 15:17:02 -04001227 lpfc_rcv_seq_check_edtov(vports[i]);
James Smart4258e982015-12-16 18:11:58 -05001228 lpfc_fdmi_num_disc_check(vports[i]);
1229 }
James Smart45ed1192009-10-02 15:17:02 -04001230 lpfc_destroy_vport_work_array(phba, vports);
1231
James Smart858c9f62007-06-17 19:56:39 -05001232 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -04001233 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -05001234 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1235 return;
1236
1237 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -05001238
James Smart256ec0d2013-04-17 20:14:58 -04001239 if (time_after(phba->last_completion_time +
1240 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL),
1241 jiffies)) {
James Smart858c9f62007-06-17 19:56:39 -05001242 spin_unlock_irq(&phba->pport->work_port_lock);
1243 if (!phba->hb_outstanding)
1244 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001245 jiffies +
1246 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001247 else
1248 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001249 jiffies +
1250 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001251 return;
1252 }
1253 spin_unlock_irq(&phba->pport->work_port_lock);
1254
James Smart0ff10d42008-01-11 01:52:36 -05001255 if (phba->elsbuf_cnt &&
1256 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1257 spin_lock_irq(&phba->hbalock);
1258 list_splice_init(&phba->elsbuf, &completions);
1259 phba->elsbuf_cnt = 0;
1260 phba->elsbuf_prev_cnt = 0;
1261 spin_unlock_irq(&phba->hbalock);
1262
1263 while (!list_empty(&completions)) {
1264 list_remove_head(&completions, buf_ptr,
1265 struct lpfc_dmabuf, list);
1266 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1267 kfree(buf_ptr);
1268 }
1269 }
1270 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1271
James Smart858c9f62007-06-17 19:56:39 -05001272 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -05001273 if (phba->cfg_enable_hba_heartbeat) {
1274 if (!phba->hb_outstanding) {
James Smartbc739052010-08-04 16:11:18 -04001275 if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1276 (list_empty(&psli->mboxq))) {
1277 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1278 GFP_KERNEL);
1279 if (!pmboxq) {
1280 mod_timer(&phba->hb_tmofunc,
1281 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001282 msecs_to_jiffies(1000 *
1283 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001284 return;
1285 }
James Smart13815c82008-01-11 01:52:48 -05001286
James Smartbc739052010-08-04 16:11:18 -04001287 lpfc_heart_beat(phba, pmboxq);
1288 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1289 pmboxq->vport = phba->pport;
1290 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1291 MBX_NOWAIT);
James Smart13815c82008-01-11 01:52:48 -05001292
James Smartbc739052010-08-04 16:11:18 -04001293 if (retval != MBX_BUSY &&
1294 retval != MBX_SUCCESS) {
1295 mempool_free(pmboxq,
1296 phba->mbox_mem_pool);
1297 mod_timer(&phba->hb_tmofunc,
1298 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001299 msecs_to_jiffies(1000 *
1300 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001301 return;
1302 }
1303 phba->skipped_hb = 0;
1304 phba->hb_outstanding = 1;
1305 } else if (time_before_eq(phba->last_completion_time,
1306 phba->skipped_hb)) {
1307 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1308 "2857 Last completion time not "
1309 " updated in %d ms\n",
1310 jiffies_to_msecs(jiffies
1311 - phba->last_completion_time));
1312 } else
1313 phba->skipped_hb = jiffies;
1314
James Smart858c9f62007-06-17 19:56:39 -05001315 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001316 jiffies +
1317 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001318 return;
James Smart13815c82008-01-11 01:52:48 -05001319 } else {
1320 /*
1321 * If heart beat timeout called with hb_outstanding set
James Smartdcf2a4e2010-09-29 11:18:53 -04001322 * we need to give the hb mailbox cmd a chance to
1323 * complete or TMO.
James Smart13815c82008-01-11 01:52:48 -05001324 */
James Smartdcf2a4e2010-09-29 11:18:53 -04001325 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1326 "0459 Adapter heartbeat still out"
1327 "standing:last compl time was %d ms.\n",
1328 jiffies_to_msecs(jiffies
1329 - phba->last_completion_time));
1330 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001331 jiffies +
1332 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001333 }
James Smart4258e982015-12-16 18:11:58 -05001334 } else {
1335 mod_timer(&phba->hb_tmofunc,
1336 jiffies +
1337 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001338 }
1339}
1340
James Smarte59058c2008-08-24 21:49:00 -04001341/**
James Smart3621a712009-04-06 18:47:14 -04001342 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -04001343 * @phba: pointer to lpfc hba data structure.
1344 *
1345 * This routine is called to bring the HBA offline when HBA hardware error
1346 * other than Port Error 6 has been detected.
1347 **/
James Smart09372822008-01-11 01:52:54 -05001348static void
1349lpfc_offline_eratt(struct lpfc_hba *phba)
1350{
1351 struct lpfc_sli *psli = &phba->sli;
1352
1353 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001354 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -05001355 spin_unlock_irq(&phba->hbalock);
James Smart618a5232012-06-12 13:54:36 -04001356 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart09372822008-01-11 01:52:54 -05001357
1358 lpfc_offline(phba);
1359 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001360 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001361 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001362 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001363 lpfc_hba_down_post(phba);
1364 lpfc_sli_brdready(phba, HS_MBRDY);
1365 lpfc_unblock_mgmt_io(phba);
1366 phba->link_state = LPFC_HBA_ERROR;
1367 return;
1368}
1369
James Smarte59058c2008-08-24 21:49:00 -04001370/**
James Smartda0436e2009-05-22 14:51:39 -04001371 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1372 * @phba: pointer to lpfc hba data structure.
1373 *
1374 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1375 * other than Port Error 6 has been detected.
1376 **/
James Smarta88dbb62013-04-17 20:18:39 -04001377void
James Smartda0436e2009-05-22 14:51:39 -04001378lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1379{
James Smart946727d2015-04-07 15:07:09 -04001380 spin_lock_irq(&phba->hbalock);
1381 phba->link_state = LPFC_HBA_ERROR;
1382 spin_unlock_irq(&phba->hbalock);
1383
James Smart618a5232012-06-12 13:54:36 -04001384 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smartda0436e2009-05-22 14:51:39 -04001385 lpfc_offline(phba);
James Smartda0436e2009-05-22 14:51:39 -04001386 lpfc_hba_down_post(phba);
James Smartda0436e2009-05-22 14:51:39 -04001387 lpfc_unblock_mgmt_io(phba);
James Smartda0436e2009-05-22 14:51:39 -04001388}
1389
1390/**
James Smarta257bf92009-04-06 18:48:10 -04001391 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1392 * @phba: pointer to lpfc hba data structure.
1393 *
1394 * This routine is invoked to handle the deferred HBA hardware error
1395 * conditions. This type of error is indicated by HBA by setting ER1
1396 * and another ER bit in the host status register. The driver will
1397 * wait until the ER1 bit clears before handling the error condition.
1398 **/
1399static void
1400lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1401{
1402 uint32_t old_host_status = phba->work_hs;
James Smarta257bf92009-04-06 18:48:10 -04001403 struct lpfc_sli *psli = &phba->sli;
1404
James Smartf4b4c682009-05-22 14:53:12 -04001405 /* If the pci channel is offline, ignore possible errors,
1406 * since we cannot communicate with the pci card anyway.
1407 */
1408 if (pci_channel_offline(phba->pcidev)) {
1409 spin_lock_irq(&phba->hbalock);
1410 phba->hba_flag &= ~DEFER_ERATT;
1411 spin_unlock_irq(&phba->hbalock);
1412 return;
1413 }
1414
James Smarta257bf92009-04-06 18:48:10 -04001415 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1416 "0479 Deferred Adapter Hardware Error "
1417 "Data: x%x x%x x%x\n",
1418 phba->work_hs,
1419 phba->work_status[0], phba->work_status[1]);
1420
1421 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001422 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001423 spin_unlock_irq(&phba->hbalock);
1424
1425
1426 /*
1427 * Firmware stops when it triggred erratt. That could cause the I/Os
1428 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1429 * SCSI layer retry it after re-establishing link.
1430 */
James Smartdb55fba2014-04-04 13:52:02 -04001431 lpfc_sli_abort_fcp_rings(phba);
James Smarta257bf92009-04-06 18:48:10 -04001432
1433 /*
1434 * There was a firmware error. Take the hba offline and then
1435 * attempt to restart it.
1436 */
James Smart618a5232012-06-12 13:54:36 -04001437 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smarta257bf92009-04-06 18:48:10 -04001438 lpfc_offline(phba);
1439
1440 /* Wait for the ER1 bit to clear.*/
1441 while (phba->work_hs & HS_FFER1) {
1442 msleep(100);
James Smart9940b972011-03-11 16:06:12 -05001443 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1444 phba->work_hs = UNPLUG_ERR ;
1445 break;
1446 }
James Smarta257bf92009-04-06 18:48:10 -04001447 /* If driver is unloading let the worker thread continue */
1448 if (phba->pport->load_flag & FC_UNLOADING) {
1449 phba->work_hs = 0;
1450 break;
1451 }
1452 }
1453
1454 /*
1455 * This is to ptrotect against a race condition in which
1456 * first write to the host attention register clear the
1457 * host status register.
1458 */
1459 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1460 phba->work_hs = old_host_status & ~HS_FFER1;
1461
James Smart3772a992009-05-22 14:50:54 -04001462 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001463 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001464 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001465 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1466 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1467}
1468
James Smart3772a992009-05-22 14:50:54 -04001469static void
1470lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1471{
1472 struct lpfc_board_event_header board_event;
1473 struct Scsi_Host *shost;
1474
1475 board_event.event_type = FC_REG_BOARD_EVENT;
1476 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1477 shost = lpfc_shost_from_vport(phba->pport);
1478 fc_host_post_vendor_event(shost, fc_get_event_number(),
1479 sizeof(board_event),
1480 (char *) &board_event,
1481 LPFC_NL_VENDOR_ID);
1482}
1483
James Smarta257bf92009-04-06 18:48:10 -04001484/**
James Smart3772a992009-05-22 14:50:54 -04001485 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001486 * @phba: pointer to lpfc hba data structure.
1487 *
1488 * This routine is invoked to handle the following HBA hardware error
1489 * conditions:
1490 * 1 - HBA error attention interrupt
1491 * 2 - DMA ring index out of range
1492 * 3 - Mailbox command came back as unknown
1493 **/
James Smart3772a992009-05-22 14:50:54 -04001494static void
1495lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001496{
James Smart2e0fef82007-06-17 19:56:36 -05001497 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001498 struct lpfc_sli *psli = &phba->sli;
James Smartd2873e42006-08-18 17:46:43 -04001499 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001500 unsigned long temperature;
1501 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001502 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001503
Linas Vepstas8d63f372007-02-14 14:28:36 -06001504 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001505 * since we cannot communicate with the pci card anyway.
1506 */
1507 if (pci_channel_offline(phba->pcidev)) {
1508 spin_lock_irq(&phba->hbalock);
1509 phba->hba_flag &= ~DEFER_ERATT;
1510 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001511 return;
James Smart3772a992009-05-22 14:50:54 -04001512 }
1513
James Smart13815c82008-01-11 01:52:48 -05001514 /* If resets are disabled then leave the HBA alone and return */
1515 if (!phba->cfg_enable_hba_reset)
1516 return;
dea31012005-04-17 16:05:31 -05001517
James Smartea2151b2008-09-07 11:52:10 -04001518 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001519 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001520
James Smarta257bf92009-04-06 18:48:10 -04001521 if (phba->hba_flag & DEFER_ERATT)
1522 lpfc_handle_deferred_eratt(phba);
1523
James Smartdcf2a4e2010-09-29 11:18:53 -04001524 if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1525 if (phba->work_hs & HS_FFER6)
1526 /* Re-establishing Link */
1527 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1528 "1301 Re-establishing Link "
1529 "Data: x%x x%x x%x\n",
1530 phba->work_hs, phba->work_status[0],
1531 phba->work_status[1]);
1532 if (phba->work_hs & HS_FFER8)
1533 /* Device Zeroization */
1534 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1535 "2861 Host Authentication device "
1536 "zeroization Data:x%x x%x x%x\n",
1537 phba->work_hs, phba->work_status[0],
1538 phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001539
James Smart92d7f7b2007-06-17 19:56:38 -05001540 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001541 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001542 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001543
1544 /*
1545 * Firmware stops when it triggled erratt with HS_FFER6.
1546 * That could cause the I/Os dropped by the firmware.
1547 * Error iocb (I/O) on txcmplq and let the SCSI layer
1548 * retry it after re-establishing link.
1549 */
James Smartdb55fba2014-04-04 13:52:02 -04001550 lpfc_sli_abort_fcp_rings(phba);
dea31012005-04-17 16:05:31 -05001551
dea31012005-04-17 16:05:31 -05001552 /*
1553 * There was a firmware error. Take the hba offline and then
1554 * attempt to restart it.
1555 */
James Smart618a5232012-06-12 13:54:36 -04001556 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
dea31012005-04-17 16:05:31 -05001557 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001558 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001559 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001560 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001561 return;
1562 }
James Smart46fa3112007-04-25 09:51:45 -04001563 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001564 } else if (phba->work_hs & HS_CRIT_TEMP) {
1565 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1566 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1567 temp_event_data.event_code = LPFC_CRIT_TEMP;
1568 temp_event_data.data = (uint32_t)temperature;
1569
1570 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001571 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001572 "(%ld), taking this port offline "
1573 "Data: x%x x%x x%x\n",
1574 temperature, phba->work_hs,
1575 phba->work_status[0], phba->work_status[1]);
1576
1577 shost = lpfc_shost_from_vport(phba->pport);
1578 fc_host_post_vendor_event(shost, fc_get_event_number(),
1579 sizeof(temp_event_data),
1580 (char *) &temp_event_data,
1581 SCSI_NL_VID_TYPE_PCI
1582 | PCI_VENDOR_ID_EMULEX);
1583
James Smart7af67052007-10-27 13:38:11 -04001584 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001585 phba->over_temp_state = HBA_OVER_TEMP;
1586 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001587 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001588
dea31012005-04-17 16:05:31 -05001589 } else {
1590 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001591 * failure is a value other than FFER6. Do not call the offline
1592 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001593 */
1594 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001595 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001596 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001597 phba->work_hs,
dea31012005-04-17 16:05:31 -05001598 phba->work_status[0], phba->work_status[1]);
1599
James Smartd2873e42006-08-18 17:46:43 -04001600 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001601 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001602 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001603 sizeof(event_data), (char *) &event_data,
1604 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1605
James Smart09372822008-01-11 01:52:54 -05001606 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001607 }
James Smart93996272008-08-24 21:50:30 -04001608 return;
dea31012005-04-17 16:05:31 -05001609}
1610
James Smarte59058c2008-08-24 21:49:00 -04001611/**
James Smart618a5232012-06-12 13:54:36 -04001612 * lpfc_sli4_port_sta_fn_reset - The SLI4 function reset due to port status reg
1613 * @phba: pointer to lpfc hba data structure.
1614 * @mbx_action: flag for mailbox shutdown action.
1615 *
1616 * This routine is invoked to perform an SLI4 port PCI function reset in
1617 * response to port status register polling attention. It waits for port
1618 * status register (ERR, RDY, RN) bits before proceeding with function reset.
1619 * During this process, interrupt vectors are freed and later requested
1620 * for handling possible port resource change.
1621 **/
1622static int
James Smarte10b2022014-02-20 09:57:43 -05001623lpfc_sli4_port_sta_fn_reset(struct lpfc_hba *phba, int mbx_action,
1624 bool en_rn_msg)
James Smart618a5232012-06-12 13:54:36 -04001625{
1626 int rc;
1627 uint32_t intr_mode;
1628
James Smart65791f12016-07-06 12:35:56 -07001629 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
1630 LPFC_SLI_INTF_IF_TYPE_2) {
1631 /*
1632 * On error status condition, driver need to wait for port
1633 * ready before performing reset.
1634 */
1635 rc = lpfc_sli4_pdev_status_reg_wait(phba);
James Smart0e916ee2016-07-06 12:36:06 -07001636 if (rc)
James Smart65791f12016-07-06 12:35:56 -07001637 return rc;
James Smart618a5232012-06-12 13:54:36 -04001638 }
James Smart0e916ee2016-07-06 12:36:06 -07001639
James Smart65791f12016-07-06 12:35:56 -07001640 /* need reset: attempt for port recovery */
1641 if (en_rn_msg)
1642 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1643 "2887 Reset Needed: Attempting Port "
1644 "Recovery...\n");
1645 lpfc_offline_prep(phba, mbx_action);
1646 lpfc_offline(phba);
1647 /* release interrupt for possible resource change */
1648 lpfc_sli4_disable_intr(phba);
1649 lpfc_sli_brdrestart(phba);
1650 /* request and enable interrupt */
1651 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
1652 if (intr_mode == LPFC_INTR_ERROR) {
1653 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1654 "3175 Failed to enable interrupt\n");
1655 return -EIO;
1656 }
1657 phba->intr_mode = intr_mode;
1658 rc = lpfc_online(phba);
1659 if (rc == 0)
1660 lpfc_unblock_mgmt_io(phba);
1661
James Smart618a5232012-06-12 13:54:36 -04001662 return rc;
1663}
1664
1665/**
James Smartda0436e2009-05-22 14:51:39 -04001666 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1667 * @phba: pointer to lpfc hba data structure.
1668 *
1669 * This routine is invoked to handle the SLI4 HBA hardware error attention
1670 * conditions.
1671 **/
1672static void
1673lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1674{
1675 struct lpfc_vport *vport = phba->pport;
1676 uint32_t event_data;
1677 struct Scsi_Host *shost;
James Smart2fcee4b2010-12-15 17:57:46 -05001678 uint32_t if_type;
James Smart2e90f4b2011-12-13 13:22:37 -05001679 struct lpfc_register portstat_reg = {0};
1680 uint32_t reg_err1, reg_err2;
1681 uint32_t uerrlo_reg, uemasklo_reg;
James Smart65791f12016-07-06 12:35:56 -07001682 uint32_t smphr_port_status = 0, pci_rd_rc1, pci_rd_rc2;
James Smarte10b2022014-02-20 09:57:43 -05001683 bool en_rn_msg = true;
James Smart946727d2015-04-07 15:07:09 -04001684 struct temp_event temp_event_data;
James Smart65791f12016-07-06 12:35:56 -07001685 struct lpfc_register portsmphr_reg;
1686 int rc, i;
James Smartda0436e2009-05-22 14:51:39 -04001687
1688 /* If the pci channel is offline, ignore possible errors, since
1689 * we cannot communicate with the pci card anyway.
1690 */
1691 if (pci_channel_offline(phba->pcidev))
1692 return;
James Smartda0436e2009-05-22 14:51:39 -04001693
James Smart65791f12016-07-06 12:35:56 -07001694 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
James Smart2fcee4b2010-12-15 17:57:46 -05001695 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1696 switch (if_type) {
1697 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart2e90f4b2011-12-13 13:22:37 -05001698 pci_rd_rc1 = lpfc_readl(
1699 phba->sli4_hba.u.if_type0.UERRLOregaddr,
1700 &uerrlo_reg);
1701 pci_rd_rc2 = lpfc_readl(
1702 phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
1703 &uemasklo_reg);
1704 /* consider PCI bus read error as pci_channel_offline */
1705 if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
1706 return;
James Smart65791f12016-07-06 12:35:56 -07001707 if (!(phba->hba_flag & HBA_RECOVERABLE_UE)) {
1708 lpfc_sli4_offline_eratt(phba);
1709 return;
1710 }
1711 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1712 "7623 Checking UE recoverable");
1713
1714 for (i = 0; i < phba->sli4_hba.ue_to_sr / 1000; i++) {
1715 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1716 &portsmphr_reg.word0))
1717 continue;
1718
1719 smphr_port_status = bf_get(lpfc_port_smphr_port_status,
1720 &portsmphr_reg);
1721 if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1722 LPFC_PORT_SEM_UE_RECOVERABLE)
1723 break;
1724 /*Sleep for 1Sec, before checking SEMAPHORE */
1725 msleep(1000);
1726 }
1727
1728 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1729 "4827 smphr_port_status x%x : Waited %dSec",
1730 smphr_port_status, i);
1731
1732 /* Recoverable UE, reset the HBA device */
1733 if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1734 LPFC_PORT_SEM_UE_RECOVERABLE) {
1735 for (i = 0; i < 20; i++) {
1736 msleep(1000);
1737 if (!lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1738 &portsmphr_reg.word0) &&
1739 (LPFC_POST_STAGE_PORT_READY ==
1740 bf_get(lpfc_port_smphr_port_status,
1741 &portsmphr_reg))) {
1742 rc = lpfc_sli4_port_sta_fn_reset(phba,
1743 LPFC_MBX_NO_WAIT, en_rn_msg);
1744 if (rc == 0)
1745 return;
1746 lpfc_printf_log(phba,
1747 KERN_ERR, LOG_INIT,
1748 "4215 Failed to recover UE");
1749 break;
1750 }
1751 }
1752 }
1753 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1754 "7624 Firmware not ready: Failing UE recovery,"
1755 " waited %dSec", i);
James Smart2fcee4b2010-12-15 17:57:46 -05001756 lpfc_sli4_offline_eratt(phba);
1757 break;
James Smart946727d2015-04-07 15:07:09 -04001758
James Smart2fcee4b2010-12-15 17:57:46 -05001759 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart2e90f4b2011-12-13 13:22:37 -05001760 pci_rd_rc1 = lpfc_readl(
1761 phba->sli4_hba.u.if_type2.STATUSregaddr,
1762 &portstat_reg.word0);
1763 /* consider PCI bus read error as pci_channel_offline */
James Smart6b5151f2012-01-18 16:24:06 -05001764 if (pci_rd_rc1 == -EIO) {
1765 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1766 "3151 PCI bus read access failure: x%x\n",
1767 readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
James Smart2e90f4b2011-12-13 13:22:37 -05001768 return;
James Smart6b5151f2012-01-18 16:24:06 -05001769 }
James Smart2e90f4b2011-12-13 13:22:37 -05001770 reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
1771 reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
James Smart2fcee4b2010-12-15 17:57:46 -05001772 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
James Smart2fcee4b2010-12-15 17:57:46 -05001773 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1774 "2889 Port Overtemperature event, "
James Smart946727d2015-04-07 15:07:09 -04001775 "taking port offline Data: x%x x%x\n",
1776 reg_err1, reg_err2);
1777
James Smart310429e2016-07-06 12:35:54 -07001778 phba->sfp_alarm |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
James Smart946727d2015-04-07 15:07:09 -04001779 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1780 temp_event_data.event_code = LPFC_CRIT_TEMP;
1781 temp_event_data.data = 0xFFFFFFFF;
1782
1783 shost = lpfc_shost_from_vport(phba->pport);
1784 fc_host_post_vendor_event(shost, fc_get_event_number(),
1785 sizeof(temp_event_data),
1786 (char *)&temp_event_data,
1787 SCSI_NL_VID_TYPE_PCI
1788 | PCI_VENDOR_ID_EMULEX);
1789
James Smart2fcee4b2010-12-15 17:57:46 -05001790 spin_lock_irq(&phba->hbalock);
1791 phba->over_temp_state = HBA_OVER_TEMP;
1792 spin_unlock_irq(&phba->hbalock);
1793 lpfc_sli4_offline_eratt(phba);
James Smart946727d2015-04-07 15:07:09 -04001794 return;
James Smart2fcee4b2010-12-15 17:57:46 -05001795 }
James Smart2e90f4b2011-12-13 13:22:37 -05001796 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smarte10b2022014-02-20 09:57:43 -05001797 reg_err2 == SLIPORT_ERR2_REG_FW_RESTART) {
James Smart2e90f4b2011-12-13 13:22:37 -05001798 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte10b2022014-02-20 09:57:43 -05001799 "3143 Port Down: Firmware Update "
1800 "Detected\n");
1801 en_rn_msg = false;
1802 } else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smart2e90f4b2011-12-13 13:22:37 -05001803 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1804 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1805 "3144 Port Down: Debug Dump\n");
1806 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1807 reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
1808 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1809 "3145 Port Down: Provisioning\n");
James Smart618a5232012-06-12 13:54:36 -04001810
James Smart946727d2015-04-07 15:07:09 -04001811 /* If resets are disabled then leave the HBA alone and return */
1812 if (!phba->cfg_enable_hba_reset)
1813 return;
1814
James Smart618a5232012-06-12 13:54:36 -04001815 /* Check port status register for function reset */
James Smarte10b2022014-02-20 09:57:43 -05001816 rc = lpfc_sli4_port_sta_fn_reset(phba, LPFC_MBX_NO_WAIT,
1817 en_rn_msg);
James Smart618a5232012-06-12 13:54:36 -04001818 if (rc == 0) {
1819 /* don't report event on forced debug dump */
1820 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1821 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1822 return;
1823 else
1824 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001825 }
James Smart618a5232012-06-12 13:54:36 -04001826 /* fall through for not able to recover */
James Smart6b5151f2012-01-18 16:24:06 -05001827 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1828 "3152 Unrecoverable error, bring the port "
1829 "offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001830 lpfc_sli4_offline_eratt(phba);
1831 break;
1832 case LPFC_SLI_INTF_IF_TYPE_1:
1833 default:
1834 break;
1835 }
James Smart2e90f4b2011-12-13 13:22:37 -05001836 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1837 "3123 Report dump event to upper layer\n");
1838 /* Send an internal error event to mgmt application */
1839 lpfc_board_errevt_to_mgmt(phba);
1840
1841 event_data = FC_REG_DUMP_EVENT;
1842 shost = lpfc_shost_from_vport(vport);
1843 fc_host_post_vendor_event(shost, fc_get_event_number(),
1844 sizeof(event_data), (char *) &event_data,
1845 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
James Smartda0436e2009-05-22 14:51:39 -04001846}
1847
1848/**
1849 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1850 * @phba: pointer to lpfc HBA data structure.
1851 *
1852 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1853 * routine from the API jump table function pointer from the lpfc_hba struct.
1854 *
1855 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001856 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001857 * Any other value - error.
1858 **/
1859void
1860lpfc_handle_eratt(struct lpfc_hba *phba)
1861{
1862 (*phba->lpfc_handle_eratt)(phba);
1863}
1864
1865/**
James Smart3621a712009-04-06 18:47:14 -04001866 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04001867 * @phba: pointer to lpfc hba data structure.
1868 *
1869 * This routine is invoked from the worker thread to handle a HBA host
James Smart895427b2017-02-12 13:52:30 -08001870 * attention link event. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04001871 **/
dea31012005-04-17 16:05:31 -05001872void
James Smart2e0fef82007-06-17 19:56:36 -05001873lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001874{
James Smart2e0fef82007-06-17 19:56:36 -05001875 struct lpfc_vport *vport = phba->pport;
1876 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001877 LPFC_MBOXQ_t *pmb;
1878 volatile uint32_t control;
1879 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05001880 int rc = 0;
dea31012005-04-17 16:05:31 -05001881
1882 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001883 if (!pmb) {
1884 rc = 1;
dea31012005-04-17 16:05:31 -05001885 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05001886 }
dea31012005-04-17 16:05:31 -05001887
1888 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001889 if (!mp) {
1890 rc = 2;
dea31012005-04-17 16:05:31 -05001891 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05001892 }
dea31012005-04-17 16:05:31 -05001893
1894 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05001895 if (!mp->virt) {
1896 rc = 3;
dea31012005-04-17 16:05:31 -05001897 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05001898 }
dea31012005-04-17 16:05:31 -05001899
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05001900 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04001901 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05001902
1903 psli->slistat.link_event++;
James Smart76a95d72010-11-20 23:11:48 -05001904 lpfc_read_topology(phba, pmb, mp);
1905 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smart2e0fef82007-06-17 19:56:36 -05001906 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04001907 /* Block ELS IOCBs until we have processed this mbox command */
James Smart895427b2017-02-12 13:52:30 -08001908 phba->sli.sli3_ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04001909 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05001910 if (rc == MBX_NOT_FINISHED) {
1911 rc = 4;
James Smart14691152006-12-02 13:34:28 -05001912 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05001913 }
dea31012005-04-17 16:05:31 -05001914
1915 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05001916 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001917 writel(HA_LATT, phba->HAregaddr);
1918 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001919 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001920
1921 return;
1922
James Smart14691152006-12-02 13:34:28 -05001923lpfc_handle_latt_free_mbuf:
James Smart895427b2017-02-12 13:52:30 -08001924 phba->sli.sli3_ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05001925 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001926lpfc_handle_latt_free_mp:
1927 kfree(mp);
1928lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04001929 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001930lpfc_handle_latt_err_exit:
1931 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001932 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001933 psli->sli_flag |= LPFC_PROCESS_LA;
1934 control = readl(phba->HCregaddr);
1935 control |= HC_LAINT_ENA;
1936 writel(control, phba->HCregaddr);
1937 readl(phba->HCregaddr); /* flush */
1938
1939 /* Clear Link Attention in HA REG */
1940 writel(HA_LATT, phba->HAregaddr);
1941 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001942 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001943 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001944 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001945
James Smart09372822008-01-11 01:52:54 -05001946 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1947 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05001948
1949 return;
1950}
1951
James Smarte59058c2008-08-24 21:49:00 -04001952/**
James Smart3621a712009-04-06 18:47:14 -04001953 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04001954 * @phba: pointer to lpfc hba data structure.
1955 * @vpd: pointer to the vital product data.
1956 * @len: length of the vital product data in bytes.
1957 *
1958 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1959 * an array of characters. In this routine, the ModelName, ProgramType, and
1960 * ModelDesc, etc. fields of the phba data structure will be populated.
1961 *
1962 * Return codes
1963 * 0 - pointer to the VPD passed in is NULL
1964 * 1 - success
1965 **/
James Smart3772a992009-05-22 14:50:54 -04001966int
James Smart2e0fef82007-06-17 19:56:36 -05001967lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05001968{
1969 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05001970 int Length;
dea31012005-04-17 16:05:31 -05001971 int i, j;
1972 int finished = 0;
1973 int index = 0;
1974
1975 if (!vpd)
1976 return 0;
1977
1978 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05001979 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001980 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05001981 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1982 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001983 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05001984 switch (vpd[index]) {
1985 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001986 case 0x91:
dea31012005-04-17 16:05:31 -05001987 index += 1;
1988 lenlo = vpd[index];
1989 index += 1;
1990 lenhi = vpd[index];
1991 index += 1;
1992 i = ((((unsigned short)lenhi) << 8) + lenlo);
1993 index += i;
1994 break;
1995 case 0x90:
1996 index += 1;
1997 lenlo = vpd[index];
1998 index += 1;
1999 lenhi = vpd[index];
2000 index += 1;
2001 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002002 if (Length > len - index)
2003 Length = len - index;
dea31012005-04-17 16:05:31 -05002004 while (Length > 0) {
2005 /* Look for Serial Number */
2006 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
2007 index += 2;
2008 i = vpd[index];
2009 index += 1;
2010 j = 0;
2011 Length -= (3+i);
2012 while(i--) {
2013 phba->SerialNumber[j++] = vpd[index++];
2014 if (j == 31)
2015 break;
2016 }
2017 phba->SerialNumber[j] = 0;
2018 continue;
2019 }
2020 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
2021 phba->vpd_flag |= VPD_MODEL_DESC;
2022 index += 2;
2023 i = vpd[index];
2024 index += 1;
2025 j = 0;
2026 Length -= (3+i);
2027 while(i--) {
2028 phba->ModelDesc[j++] = vpd[index++];
2029 if (j == 255)
2030 break;
2031 }
2032 phba->ModelDesc[j] = 0;
2033 continue;
2034 }
2035 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
2036 phba->vpd_flag |= VPD_MODEL_NAME;
2037 index += 2;
2038 i = vpd[index];
2039 index += 1;
2040 j = 0;
2041 Length -= (3+i);
2042 while(i--) {
2043 phba->ModelName[j++] = vpd[index++];
2044 if (j == 79)
2045 break;
2046 }
2047 phba->ModelName[j] = 0;
2048 continue;
2049 }
2050 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
2051 phba->vpd_flag |= VPD_PROGRAM_TYPE;
2052 index += 2;
2053 i = vpd[index];
2054 index += 1;
2055 j = 0;
2056 Length -= (3+i);
2057 while(i--) {
2058 phba->ProgramType[j++] = vpd[index++];
2059 if (j == 255)
2060 break;
2061 }
2062 phba->ProgramType[j] = 0;
2063 continue;
2064 }
2065 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
2066 phba->vpd_flag |= VPD_PORT;
2067 index += 2;
2068 i = vpd[index];
2069 index += 1;
2070 j = 0;
2071 Length -= (3+i);
2072 while(i--) {
James Smartcd1c8302011-10-10 21:33:25 -04002073 if ((phba->sli_rev == LPFC_SLI_REV4) &&
2074 (phba->sli4_hba.pport_name_sta ==
2075 LPFC_SLI4_PPNAME_GET)) {
2076 j++;
2077 index++;
2078 } else
2079 phba->Port[j++] = vpd[index++];
2080 if (j == 19)
2081 break;
dea31012005-04-17 16:05:31 -05002082 }
James Smartcd1c8302011-10-10 21:33:25 -04002083 if ((phba->sli_rev != LPFC_SLI_REV4) ||
2084 (phba->sli4_hba.pport_name_sta ==
2085 LPFC_SLI4_PPNAME_NON))
2086 phba->Port[j] = 0;
dea31012005-04-17 16:05:31 -05002087 continue;
2088 }
2089 else {
2090 index += 2;
2091 i = vpd[index];
2092 index += 1;
2093 index += i;
2094 Length -= (3 + i);
2095 }
2096 }
2097 finished = 0;
2098 break;
2099 case 0x78:
2100 finished = 1;
2101 break;
2102 default:
2103 index ++;
2104 break;
2105 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002106 }
dea31012005-04-17 16:05:31 -05002107
2108 return(1);
2109}
2110
James Smarte59058c2008-08-24 21:49:00 -04002111/**
James Smart3621a712009-04-06 18:47:14 -04002112 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04002113 * @phba: pointer to lpfc hba data structure.
2114 * @mdp: pointer to the data structure to hold the derived model name.
2115 * @descp: pointer to the data structure to hold the derived description.
2116 *
2117 * This routine retrieves HBA's description based on its registered PCI device
2118 * ID. The @descp passed into this function points to an array of 256 chars. It
2119 * shall be returned with the model name, maximum speed, and the host bus type.
2120 * The @mdp passed into this function points to an array of 80 chars. When the
2121 * function returns, the @mdp will be filled with the model name.
2122 **/
dea31012005-04-17 16:05:31 -05002123static void
James Smart2e0fef82007-06-17 19:56:36 -05002124lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05002125{
2126 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05002127 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002128 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04002129 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04002130 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002131 struct {
James Smarta747c9c2009-11-18 15:41:10 -05002132 char *name;
2133 char *bus;
2134 char *function;
2135 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002136
2137 if (mdp && mdp[0] != '\0'
2138 && descp && descp[0] != '\0')
2139 return;
2140
James Smartd38dd522015-08-31 16:48:17 -04002141 if (phba->lmt & LMT_32Gb)
2142 max_speed = 32;
2143 else if (phba->lmt & LMT_16Gb)
James Smartc0c11512011-05-24 11:41:34 -04002144 max_speed = 16;
2145 else if (phba->lmt & LMT_10Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002146 max_speed = 10;
2147 else if (phba->lmt & LMT_8Gb)
2148 max_speed = 8;
2149 else if (phba->lmt & LMT_4Gb)
2150 max_speed = 4;
2151 else if (phba->lmt & LMT_2Gb)
2152 max_speed = 2;
James Smart4169d862012-09-29 11:32:09 -04002153 else if (phba->lmt & LMT_1Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002154 max_speed = 1;
James Smart4169d862012-09-29 11:32:09 -04002155 else
2156 max_speed = 0;
dea31012005-04-17 16:05:31 -05002157
2158 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05002159
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002160 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002161 case PCI_DEVICE_ID_FIREFLY:
James Smart12222f42014-05-21 08:05:19 -04002162 m = (typeof(m)){"LP6000", "PCI",
2163 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002164 break;
dea31012005-04-17 16:05:31 -05002165 case PCI_DEVICE_ID_SUPERFLY:
2166 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smart12222f42014-05-21 08:05:19 -04002167 m = (typeof(m)){"LP7000", "PCI", ""};
dea31012005-04-17 16:05:31 -05002168 else
James Smart12222f42014-05-21 08:05:19 -04002169 m = (typeof(m)){"LP7000E", "PCI", ""};
2170 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
dea31012005-04-17 16:05:31 -05002171 break;
2172 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002173 m = (typeof(m)){"LP8000", "PCI",
James Smart12222f42014-05-21 08:05:19 -04002174 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002175 break;
2176 case PCI_DEVICE_ID_CENTAUR:
2177 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smart12222f42014-05-21 08:05:19 -04002178 m = (typeof(m)){"LP9002", "PCI", ""};
dea31012005-04-17 16:05:31 -05002179 else
James Smart12222f42014-05-21 08:05:19 -04002180 m = (typeof(m)){"LP9000", "PCI", ""};
2181 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
dea31012005-04-17 16:05:31 -05002182 break;
2183 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002184 m = (typeof(m)){"LP952", "PCI",
James Smart12222f42014-05-21 08:05:19 -04002185 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002186 break;
2187 case PCI_DEVICE_ID_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05002188 m = (typeof(m)){"LP9802", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002189 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002190 break;
2191 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05002192 m = (typeof(m)){"LP10000", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002193 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002194 break;
2195 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05002196 m = (typeof(m)){"LPX1000", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002197 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002198 break;
2199 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002200 m = (typeof(m)){"LP982", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002201 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002202 break;
2203 case PCI_DEVICE_ID_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002204 m = (typeof(m)){"LP1050", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002205 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002206 break;
2207 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05002208 m = (typeof(m)){"LP11000", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002209 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002210 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002211 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002212 m = (typeof(m)){"LP11000-SP", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002213 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002214 break;
2215 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002216 m = (typeof(m)){"LP11002-SP", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002217 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002218 break;
2219 case PCI_DEVICE_ID_NEPTUNE:
James Smart12222f42014-05-21 08:05:19 -04002220 m = (typeof(m)){"LPe1000", "PCIe",
2221 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002222 break;
2223 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smart12222f42014-05-21 08:05:19 -04002224 m = (typeof(m)){"LPe1000-SP", "PCIe",
2225 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002226 break;
2227 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smart12222f42014-05-21 08:05:19 -04002228 m = (typeof(m)){"LPe1002-SP", "PCIe",
2229 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002230 break;
dea31012005-04-17 16:05:31 -05002231 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05002232 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002233 break;
2234 case PCI_DEVICE_ID_BSMB:
James Smart12222f42014-05-21 08:05:19 -04002235 m = (typeof(m)){"LP111", "PCI-X2",
2236 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002237 break;
2238 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05002239 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002240 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002241 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002242 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002243 break;
2244 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002245 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04002246 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002247 break;
dea31012005-04-17 16:05:31 -05002248 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05002249 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002250 break;
2251 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05002252 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002253 break;
2254 case PCI_DEVICE_ID_LP101:
James Smart12222f42014-05-21 08:05:19 -04002255 m = (typeof(m)){"LP101", "PCI-X",
2256 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002257 break;
2258 case PCI_DEVICE_ID_LP10000S:
James Smart12222f42014-05-21 08:05:19 -04002259 m = (typeof(m)){"LP10000-S", "PCI",
2260 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002261 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002262 case PCI_DEVICE_ID_LP11000S:
James Smart12222f42014-05-21 08:05:19 -04002263 m = (typeof(m)){"LP11000-S", "PCI-X2",
2264 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05002265 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002266 case PCI_DEVICE_ID_LPE11000S:
James Smart12222f42014-05-21 08:05:19 -04002267 m = (typeof(m)){"LPe11000-S", "PCIe",
2268 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002269 break;
James Smartb87eab32007-04-25 09:53:28 -04002270 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05002271 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002272 break;
2273 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05002274 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002275 break;
2276 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05002277 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002278 break;
2279 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002280 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002281 break;
2282 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002283 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002284 break;
2285 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05002286 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002287 break;
James Smart84774a42008-08-24 21:50:06 -04002288 case PCI_DEVICE_ID_HORNET:
James Smart12222f42014-05-21 08:05:19 -04002289 m = (typeof(m)){"LP21000", "PCIe",
2290 "Obsolete, Unsupported FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002291 GE = 1;
2292 break;
2293 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05002294 m = (typeof(m)){"LPev12000", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002295 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002296 break;
2297 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05002298 m = (typeof(m)){"LPev12000", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002299 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002300 break;
2301 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05002302 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002303 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002304 break;
James Smartda0436e2009-05-22 14:51:39 -04002305 case PCI_DEVICE_ID_TIGERSHARK:
2306 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002307 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04002308 break;
James Smarta747c9c2009-11-18 15:41:10 -05002309 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04002310 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002311 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
2312 break;
2313 case PCI_DEVICE_ID_FALCON:
2314 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
2315 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04002316 break;
James Smart98fc5dd2010-06-07 15:24:29 -04002317 case PCI_DEVICE_ID_BALIUS:
2318 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
James Smart12222f42014-05-21 08:05:19 -04002319 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart98fc5dd2010-06-07 15:24:29 -04002320 break;
James Smart085c6472010-11-20 23:11:37 -05002321 case PCI_DEVICE_ID_LANCER_FC:
James Smartc0c11512011-05-24 11:41:34 -04002322 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
James Smart085c6472010-11-20 23:11:37 -05002323 break;
James Smart12222f42014-05-21 08:05:19 -04002324 case PCI_DEVICE_ID_LANCER_FC_VF:
2325 m = (typeof(m)){"LPe16000", "PCIe",
2326 "Obsolete, Unsupported Fibre Channel Adapter"};
2327 break;
James Smart085c6472010-11-20 23:11:37 -05002328 case PCI_DEVICE_ID_LANCER_FCOE:
2329 oneConnect = 1;
James Smart079b5c92011-08-21 21:48:49 -04002330 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
James Smart085c6472010-11-20 23:11:37 -05002331 break;
James Smart12222f42014-05-21 08:05:19 -04002332 case PCI_DEVICE_ID_LANCER_FCOE_VF:
2333 oneConnect = 1;
2334 m = (typeof(m)){"OCe15100", "PCIe",
2335 "Obsolete, Unsupported FCoE"};
2336 break;
James Smartd38dd522015-08-31 16:48:17 -04002337 case PCI_DEVICE_ID_LANCER_G6_FC:
2338 m = (typeof(m)){"LPe32000", "PCIe", "Fibre Channel Adapter"};
2339 break;
James Smartf8cafd32012-08-03 12:42:51 -04002340 case PCI_DEVICE_ID_SKYHAWK:
2341 case PCI_DEVICE_ID_SKYHAWK_VF:
2342 oneConnect = 1;
2343 m = (typeof(m)){"OCe14000", "PCIe", "FCoE"};
2344 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002345 default:
James Smarta747c9c2009-11-18 15:41:10 -05002346 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002347 break;
dea31012005-04-17 16:05:31 -05002348 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002349
2350 if (mdp && mdp[0] == '\0')
2351 snprintf(mdp, 79,"%s", m.name);
James Smartc0c11512011-05-24 11:41:34 -04002352 /*
2353 * oneConnect hba requires special processing, they are all initiators
James Smartda0436e2009-05-22 14:51:39 -04002354 * and we put the port number on the end
2355 */
2356 if (descp && descp[0] == '\0') {
2357 if (oneConnect)
2358 snprintf(descp, 255,
James Smart4169d862012-09-29 11:32:09 -04002359 "Emulex OneConnect %s, %s Initiator %s",
James Smarta747c9c2009-11-18 15:41:10 -05002360 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04002361 phba->Port);
James Smart4169d862012-09-29 11:32:09 -04002362 else if (max_speed == 0)
2363 snprintf(descp, 255,
Sebastian Herbszt290237d2015-08-31 16:48:08 -04002364 "Emulex %s %s %s",
James Smart4169d862012-09-29 11:32:09 -04002365 m.name, m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002366 else
2367 snprintf(descp, 255,
2368 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05002369 m.name, max_speed, (GE) ? "GE" : "Gb",
2370 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002371 }
dea31012005-04-17 16:05:31 -05002372}
2373
James Smarte59058c2008-08-24 21:49:00 -04002374/**
James Smart3621a712009-04-06 18:47:14 -04002375 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04002376 * @phba: pointer to lpfc hba data structure.
2377 * @pring: pointer to a IOCB ring.
2378 * @cnt: the number of IOCBs to be posted to the IOCB ring.
2379 *
2380 * This routine posts a given number of IOCBs with the associated DMA buffer
2381 * descriptors specified by the cnt argument to the given IOCB ring.
2382 *
2383 * Return codes
2384 * The number of IOCBs NOT able to be posted to the IOCB ring.
2385 **/
dea31012005-04-17 16:05:31 -05002386int
James Smart495a7142008-06-14 22:52:59 -04002387lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05002388{
2389 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002390 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05002391 struct lpfc_dmabuf *mp1, *mp2;
2392
2393 cnt += pring->missbufcnt;
2394
2395 /* While there are buffers to post */
2396 while (cnt > 0) {
2397 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002398 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002399 if (iocb == NULL) {
2400 pring->missbufcnt = cnt;
2401 return cnt;
2402 }
dea31012005-04-17 16:05:31 -05002403 icmd = &iocb->iocb;
2404
2405 /* 2 buffers can be posted per command */
2406 /* Allocate buffer to post */
2407 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2408 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04002409 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2410 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002411 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002412 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002413 pring->missbufcnt = cnt;
2414 return cnt;
2415 }
2416
2417 INIT_LIST_HEAD(&mp1->list);
2418 /* Allocate buffer to post */
2419 if (cnt > 1) {
2420 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2421 if (mp2)
2422 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2423 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04002424 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002425 kfree(mp2);
dea31012005-04-17 16:05:31 -05002426 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2427 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002428 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002429 pring->missbufcnt = cnt;
2430 return cnt;
2431 }
2432
2433 INIT_LIST_HEAD(&mp2->list);
2434 } else {
2435 mp2 = NULL;
2436 }
2437
2438 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2439 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2440 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2441 icmd->ulpBdeCount = 1;
2442 cnt--;
2443 if (mp2) {
2444 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2445 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2446 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2447 cnt--;
2448 icmd->ulpBdeCount = 2;
2449 }
2450
2451 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2452 icmd->ulpLe = 1;
2453
James Smart3772a992009-05-22 14:50:54 -04002454 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2455 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05002456 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2457 kfree(mp1);
2458 cnt++;
2459 if (mp2) {
2460 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2461 kfree(mp2);
2462 cnt++;
2463 }
James Bottomley604a3e32005-10-29 10:28:33 -05002464 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002465 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05002466 return cnt;
2467 }
dea31012005-04-17 16:05:31 -05002468 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05002469 if (mp2)
dea31012005-04-17 16:05:31 -05002470 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05002471 }
2472 pring->missbufcnt = 0;
2473 return 0;
2474}
2475
James Smarte59058c2008-08-24 21:49:00 -04002476/**
James Smart3621a712009-04-06 18:47:14 -04002477 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04002478 * @phba: pointer to lpfc hba data structure.
2479 *
2480 * This routine posts initial receive IOCB buffers to the ELS ring. The
2481 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
James Smart895427b2017-02-12 13:52:30 -08002482 * set to 64 IOCBs. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04002483 *
2484 * Return codes
2485 * 0 - success (currently always success)
2486 **/
dea31012005-04-17 16:05:31 -05002487static int
James Smart2e0fef82007-06-17 19:56:36 -05002488lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002489{
2490 struct lpfc_sli *psli = &phba->sli;
2491
2492 /* Ring 0, ELS / CT buffers */
James Smart895427b2017-02-12 13:52:30 -08002493 lpfc_post_buffer(phba, &psli->sli3_ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05002494 /* Ring 2 - FCP no buffers needed */
2495
2496 return 0;
2497}
2498
2499#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2500
James Smarte59058c2008-08-24 21:49:00 -04002501/**
James Smart3621a712009-04-06 18:47:14 -04002502 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04002503 * @HashResultPointer: pointer to an array as hash table.
2504 *
2505 * This routine sets up the initial values to the array of hash table entries
2506 * for the LC HBAs.
2507 **/
dea31012005-04-17 16:05:31 -05002508static void
2509lpfc_sha_init(uint32_t * HashResultPointer)
2510{
2511 HashResultPointer[0] = 0x67452301;
2512 HashResultPointer[1] = 0xEFCDAB89;
2513 HashResultPointer[2] = 0x98BADCFE;
2514 HashResultPointer[3] = 0x10325476;
2515 HashResultPointer[4] = 0xC3D2E1F0;
2516}
2517
James Smarte59058c2008-08-24 21:49:00 -04002518/**
James Smart3621a712009-04-06 18:47:14 -04002519 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04002520 * @HashResultPointer: pointer to an initial/result hash table.
2521 * @HashWorkingPointer: pointer to an working hash table.
2522 *
2523 * This routine iterates an initial hash table pointed by @HashResultPointer
2524 * with the values from the working hash table pointeed by @HashWorkingPointer.
2525 * The results are putting back to the initial hash table, returned through
2526 * the @HashResultPointer as the result hash table.
2527 **/
dea31012005-04-17 16:05:31 -05002528static void
2529lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2530{
2531 int t;
2532 uint32_t TEMP;
2533 uint32_t A, B, C, D, E;
2534 t = 16;
2535 do {
2536 HashWorkingPointer[t] =
2537 S(1,
2538 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2539 8] ^
2540 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2541 } while (++t <= 79);
2542 t = 0;
2543 A = HashResultPointer[0];
2544 B = HashResultPointer[1];
2545 C = HashResultPointer[2];
2546 D = HashResultPointer[3];
2547 E = HashResultPointer[4];
2548
2549 do {
2550 if (t < 20) {
2551 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2552 } else if (t < 40) {
2553 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2554 } else if (t < 60) {
2555 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2556 } else {
2557 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2558 }
2559 TEMP += S(5, A) + E + HashWorkingPointer[t];
2560 E = D;
2561 D = C;
2562 C = S(30, B);
2563 B = A;
2564 A = TEMP;
2565 } while (++t <= 79);
2566
2567 HashResultPointer[0] += A;
2568 HashResultPointer[1] += B;
2569 HashResultPointer[2] += C;
2570 HashResultPointer[3] += D;
2571 HashResultPointer[4] += E;
2572
2573}
2574
James Smarte59058c2008-08-24 21:49:00 -04002575/**
James Smart3621a712009-04-06 18:47:14 -04002576 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04002577 * @RandomChallenge: pointer to the entry of host challenge random number array.
2578 * @HashWorking: pointer to the entry of the working hash array.
2579 *
2580 * This routine calculates the working hash array referred by @HashWorking
2581 * from the challenge random numbers associated with the host, referred by
2582 * @RandomChallenge. The result is put into the entry of the working hash
2583 * array and returned by reference through @HashWorking.
2584 **/
dea31012005-04-17 16:05:31 -05002585static void
2586lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2587{
2588 *HashWorking = (*RandomChallenge ^ *HashWorking);
2589}
2590
James Smarte59058c2008-08-24 21:49:00 -04002591/**
James Smart3621a712009-04-06 18:47:14 -04002592 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04002593 * @phba: pointer to lpfc hba data structure.
2594 * @hbainit: pointer to an array of unsigned 32-bit integers.
2595 *
2596 * This routine performs the special handling for LC HBA initialization.
2597 **/
dea31012005-04-17 16:05:31 -05002598void
2599lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2600{
2601 int t;
2602 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05002603 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05002604
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002605 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002606 if (!HashWorking)
2607 return;
2608
dea31012005-04-17 16:05:31 -05002609 HashWorking[0] = HashWorking[78] = *pwwnn++;
2610 HashWorking[1] = HashWorking[79] = *pwwnn;
2611
2612 for (t = 0; t < 7; t++)
2613 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2614
2615 lpfc_sha_init(hbainit);
2616 lpfc_sha_iterate(hbainit, HashWorking);
2617 kfree(HashWorking);
2618}
2619
James Smarte59058c2008-08-24 21:49:00 -04002620/**
James Smart3621a712009-04-06 18:47:14 -04002621 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04002622 * @vport: pointer to a virtual N_Port data structure.
2623 *
2624 * This routine performs the necessary cleanups before deleting the @vport.
2625 * It invokes the discovery state machine to perform necessary state
2626 * transitions and to release the ndlps associated with the @vport. Note,
2627 * the physical port is treated as @vport 0.
2628 **/
James Smart87af33f2007-10-27 13:37:43 -04002629void
James Smart2e0fef82007-06-17 19:56:36 -05002630lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002631{
James Smart87af33f2007-10-27 13:37:43 -04002632 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002633 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04002634 int i = 0;
dea31012005-04-17 16:05:31 -05002635
James Smart87af33f2007-10-27 13:37:43 -04002636 if (phba->link_state > LPFC_LINK_DOWN)
2637 lpfc_port_link_failure(vport);
2638
2639 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002640 if (!NLP_CHK_NODE_ACT(ndlp)) {
2641 ndlp = lpfc_enable_node(vport, ndlp,
2642 NLP_STE_UNUSED_NODE);
2643 if (!ndlp)
2644 continue;
2645 spin_lock_irq(&phba->ndlp_lock);
2646 NLP_SET_FREE_REQ(ndlp);
2647 spin_unlock_irq(&phba->ndlp_lock);
2648 /* Trigger the release of the ndlp memory */
2649 lpfc_nlp_put(ndlp);
2650 continue;
2651 }
2652 spin_lock_irq(&phba->ndlp_lock);
2653 if (NLP_CHK_FREE_REQ(ndlp)) {
2654 /* The ndlp should not be in memory free mode already */
2655 spin_unlock_irq(&phba->ndlp_lock);
2656 continue;
2657 } else
2658 /* Indicate request for freeing ndlp memory */
2659 NLP_SET_FREE_REQ(ndlp);
2660 spin_unlock_irq(&phba->ndlp_lock);
2661
James Smart58da1ff2008-04-07 10:15:56 -04002662 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2663 ndlp->nlp_DID == Fabric_DID) {
2664 /* Just free up ndlp with Fabric_DID for vports */
2665 lpfc_nlp_put(ndlp);
2666 continue;
2667 }
2668
James Smarteff4a012012-01-18 16:25:25 -05002669 /* take care of nodes in unused state before the state
2670 * machine taking action.
2671 */
2672 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
2673 lpfc_nlp_put(ndlp);
2674 continue;
2675 }
2676
James Smart87af33f2007-10-27 13:37:43 -04002677 if (ndlp->nlp_type & NLP_FABRIC)
2678 lpfc_disc_state_machine(vport, ndlp, NULL,
2679 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002680
James Smarta0f2d3e2017-02-12 13:52:31 -08002681 if (ndlp->nlp_fc4_type & NLP_FC4_NVME) {
2682 /* Remove the NVME transport reference now and
2683 * continue to remove the node.
2684 */
2685 lpfc_nlp_put(ndlp);
2686 }
2687
James Smart87af33f2007-10-27 13:37:43 -04002688 lpfc_disc_state_machine(vport, ndlp, NULL,
2689 NLP_EVT_DEVICE_RM);
2690 }
2691
James Smarta8adb832007-10-27 13:37:53 -04002692 /* At this point, ALL ndlp's should be gone
2693 * because of the previous NLP_EVT_DEVICE_RM.
2694 * Lets wait for this to happen, if needed.
2695 */
James Smart87af33f2007-10-27 13:37:43 -04002696 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002697 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002698 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002699 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002700 list_for_each_entry_safe(ndlp, next_ndlp,
2701 &vport->fc_nodes, nlp_listp) {
2702 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2703 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002704 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002705 "usgmap:x%x refcnt:%d\n",
2706 ndlp->nlp_DID, (void *)ndlp,
2707 ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01002708 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05002709 }
James Smarta8adb832007-10-27 13:37:53 -04002710 break;
James Smart87af33f2007-10-27 13:37:43 -04002711 }
James Smarta8adb832007-10-27 13:37:53 -04002712
2713 /* Wait for any activity on ndlps to settle */
2714 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002715 }
James Smart1151e3e2011-02-16 12:39:35 -05002716 lpfc_cleanup_vports_rrqs(vport, NULL);
dea31012005-04-17 16:05:31 -05002717}
2718
James Smarte59058c2008-08-24 21:49:00 -04002719/**
James Smart3621a712009-04-06 18:47:14 -04002720 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002721 * @vport: pointer to a virtual N_Port data structure.
2722 *
2723 * This routine stops all the timers associated with a @vport. This function
2724 * is invoked before disabling or deleting a @vport. Note that the physical
2725 * port is treated as @vport 0.
2726 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002727void
2728lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002729{
James Smart92d7f7b2007-06-17 19:56:38 -05002730 del_timer_sync(&vport->els_tmofunc);
James Smart92494142011-02-16 12:39:44 -05002731 del_timer_sync(&vport->delayed_disc_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002732 lpfc_can_disctmo(vport);
2733 return;
dea31012005-04-17 16:05:31 -05002734}
2735
James Smarte59058c2008-08-24 21:49:00 -04002736/**
James Smartecfd03c2010-02-12 14:41:27 -05002737 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2738 * @phba: pointer to lpfc hba data structure.
2739 *
2740 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2741 * caller of this routine should already hold the host lock.
2742 **/
2743void
2744__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2745{
James Smart5ac6b302010-10-22 11:05:36 -04002746 /* Clear pending FCF rediscovery wait flag */
2747 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2748
James Smartecfd03c2010-02-12 14:41:27 -05002749 /* Now, try to stop the timer */
2750 del_timer(&phba->fcf.redisc_wait);
2751}
2752
2753/**
2754 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2755 * @phba: pointer to lpfc hba data structure.
2756 *
2757 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2758 * checks whether the FCF rediscovery wait timer is pending with the host
2759 * lock held before proceeding with disabling the timer and clearing the
2760 * wait timer pendig flag.
2761 **/
2762void
2763lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2764{
2765 spin_lock_irq(&phba->hbalock);
2766 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2767 /* FCF rediscovery timer already fired or stopped */
2768 spin_unlock_irq(&phba->hbalock);
2769 return;
2770 }
2771 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart5ac6b302010-10-22 11:05:36 -04002772 /* Clear failover in progress flags */
2773 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
James Smartecfd03c2010-02-12 14:41:27 -05002774 spin_unlock_irq(&phba->hbalock);
2775}
2776
2777/**
James Smart3772a992009-05-22 14:50:54 -04002778 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002779 * @phba: pointer to lpfc hba data structure.
2780 *
2781 * This routine stops all the timers associated with a HBA. This function is
2782 * invoked before either putting a HBA offline or unloading the driver.
2783 **/
James Smart3772a992009-05-22 14:50:54 -04002784void
2785lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002786{
James Smart51ef4c22007-08-02 11:10:31 -04002787 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05002788 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002789 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002790 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002791 del_timer_sync(&phba->hb_tmofunc);
James Smart1151e3e2011-02-16 12:39:35 -05002792 if (phba->sli_rev == LPFC_SLI_REV4) {
2793 del_timer_sync(&phba->rrq_tmr);
2794 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2795 }
James Smart3772a992009-05-22 14:50:54 -04002796 phba->hb_outstanding = 0;
2797
2798 switch (phba->pci_dev_grp) {
2799 case LPFC_PCI_DEV_LP:
2800 /* Stop any LightPulse device specific driver timers */
2801 del_timer_sync(&phba->fcp_poll_timer);
2802 break;
2803 case LPFC_PCI_DEV_OC:
2804 /* Stop any OneConnect device sepcific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05002805 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04002806 break;
2807 default:
2808 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2809 "0297 Invalid device group (x%x)\n",
2810 phba->pci_dev_grp);
2811 break;
2812 }
James Smart2e0fef82007-06-17 19:56:36 -05002813 return;
dea31012005-04-17 16:05:31 -05002814}
2815
James Smarte59058c2008-08-24 21:49:00 -04002816/**
James Smart3621a712009-04-06 18:47:14 -04002817 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002818 * @phba: pointer to lpfc hba data structure.
2819 *
2820 * This routine marks a HBA's management interface as blocked. Once the HBA's
2821 * management interface is marked as blocked, all the user space access to
2822 * the HBA, whether they are from sysfs interface or libdfc interface will
2823 * all be blocked. The HBA is set to block the management interface when the
2824 * driver prepares the HBA interface for online or offline.
2825 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002826static void
James Smart618a5232012-06-12 13:54:36 -04002827lpfc_block_mgmt_io(struct lpfc_hba *phba, int mbx_action)
Adrian Bunka6ababd2007-11-05 18:07:33 +01002828{
2829 unsigned long iflag;
James Smart6e7288d2010-06-07 15:23:35 -04002830 uint8_t actcmd = MBX_HEARTBEAT;
2831 unsigned long timeout;
2832
Adrian Bunka6ababd2007-11-05 18:07:33 +01002833 spin_lock_irqsave(&phba->hbalock, iflag);
2834 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
James Smart618a5232012-06-12 13:54:36 -04002835 spin_unlock_irqrestore(&phba->hbalock, iflag);
2836 if (mbx_action == LPFC_MBX_NO_WAIT)
2837 return;
2838 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
2839 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002840 if (phba->sli.mbox_active) {
James Smart6e7288d2010-06-07 15:23:35 -04002841 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
James Smarta183a152011-10-10 21:32:43 -04002842 /* Determine how long we might wait for the active mailbox
2843 * command to be gracefully completed by firmware.
2844 */
2845 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
2846 phba->sli.mbox_active) * 1000) + jiffies;
2847 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002848 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002849
James Smart6e7288d2010-06-07 15:23:35 -04002850 /* Wait for the outstnading mailbox command to complete */
2851 while (phba->sli.mbox_active) {
2852 /* Check active mailbox complete status every 2ms */
2853 msleep(2);
2854 if (time_after(jiffies, timeout)) {
2855 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2856 "2813 Mgmt IO is Blocked %x "
2857 "- mbox cmd %x still active\n",
2858 phba->sli.sli_flag, actcmd);
2859 break;
2860 }
2861 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002862}
2863
James Smarte59058c2008-08-24 21:49:00 -04002864/**
James Smart6b5151f2012-01-18 16:24:06 -05002865 * lpfc_sli4_node_prep - Assign RPIs for active nodes.
2866 * @phba: pointer to lpfc hba data structure.
2867 *
2868 * Allocate RPIs for all active remote nodes. This is needed whenever
2869 * an SLI4 adapter is reset and the driver is not unloading. Its purpose
2870 * is to fixup the temporary rpi assignments.
2871 **/
2872void
2873lpfc_sli4_node_prep(struct lpfc_hba *phba)
2874{
2875 struct lpfc_nodelist *ndlp, *next_ndlp;
2876 struct lpfc_vport **vports;
2877 int i;
2878
2879 if (phba->sli_rev != LPFC_SLI_REV4)
2880 return;
2881
2882 vports = lpfc_create_vport_work_array(phba);
2883 if (vports != NULL) {
2884 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2885 if (vports[i]->load_flag & FC_UNLOADING)
2886 continue;
2887
2888 list_for_each_entry_safe(ndlp, next_ndlp,
2889 &vports[i]->fc_nodes,
2890 nlp_listp) {
James Smartbe6bb942015-04-07 15:07:22 -04002891 if (NLP_CHK_NODE_ACT(ndlp)) {
James Smart6b5151f2012-01-18 16:24:06 -05002892 ndlp->nlp_rpi =
2893 lpfc_sli4_alloc_rpi(phba);
James Smartbe6bb942015-04-07 15:07:22 -04002894 lpfc_printf_vlog(ndlp->vport, KERN_INFO,
2895 LOG_NODE,
2896 "0009 rpi:%x DID:%x "
2897 "flg:%x map:%x %p\n",
2898 ndlp->nlp_rpi,
2899 ndlp->nlp_DID,
2900 ndlp->nlp_flag,
2901 ndlp->nlp_usg_map,
2902 ndlp);
2903 }
James Smart6b5151f2012-01-18 16:24:06 -05002904 }
2905 }
2906 }
2907 lpfc_destroy_vport_work_array(phba, vports);
2908}
2909
2910/**
James Smart3621a712009-04-06 18:47:14 -04002911 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04002912 * @phba: pointer to lpfc hba data structure.
2913 *
2914 * This routine initializes the HBA and brings a HBA online. During this
2915 * process, the management interface is blocked to prevent user space access
2916 * to the HBA interfering with the driver initialization.
2917 *
2918 * Return codes
2919 * 0 - successful
2920 * 1 - failed
2921 **/
dea31012005-04-17 16:05:31 -05002922int
James Smart2e0fef82007-06-17 19:56:36 -05002923lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002924{
Julia Lawall372bd282008-12-16 16:15:08 +01002925 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04002926 struct lpfc_vport **vports;
2927 int i;
James Smart16a3a202013-04-17 20:14:38 -04002928 bool vpis_cleared = false;
James Smart2e0fef82007-06-17 19:56:36 -05002929
dea31012005-04-17 16:05:31 -05002930 if (!phba)
2931 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01002932 vport = phba->pport;
dea31012005-04-17 16:05:31 -05002933
James Smart2e0fef82007-06-17 19:56:36 -05002934 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05002935 return 0;
2936
James Smarted957682007-06-17 19:56:37 -05002937 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002938 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05002939
James Smart618a5232012-06-12 13:54:36 -04002940 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05002941
James Smartda0436e2009-05-22 14:51:39 -04002942 if (phba->sli_rev == LPFC_SLI_REV4) {
2943 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2944 lpfc_unblock_mgmt_io(phba);
2945 return 1;
2946 }
James Smart16a3a202013-04-17 20:14:38 -04002947 spin_lock_irq(&phba->hbalock);
2948 if (!phba->sli4_hba.max_cfg_param.vpi_used)
2949 vpis_cleared = true;
2950 spin_unlock_irq(&phba->hbalock);
James Smartda0436e2009-05-22 14:51:39 -04002951 } else {
James Smart895427b2017-02-12 13:52:30 -08002952 lpfc_sli_queue_init(phba);
James Smartda0436e2009-05-22 14:51:39 -04002953 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2954 lpfc_unblock_mgmt_io(phba);
2955 return 1;
2956 }
James Smart46fa3112007-04-25 09:51:45 -04002957 }
dea31012005-04-17 16:05:31 -05002958
James Smart549e55c2007-08-02 11:09:51 -04002959 vports = lpfc_create_vport_work_array(phba);
Arnd Bergmannaeb66412016-03-14 15:29:44 +01002960 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04002961 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002962 struct Scsi_Host *shost;
2963 shost = lpfc_shost_from_vport(vports[i]);
2964 spin_lock_irq(shost->host_lock);
2965 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2966 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2967 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart16a3a202013-04-17 20:14:38 -04002968 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart1c6834a2009-07-19 10:01:26 -04002969 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart16a3a202013-04-17 20:14:38 -04002970 if ((vpis_cleared) &&
2971 (vports[i]->port_type !=
2972 LPFC_PHYSICAL_PORT))
2973 vports[i]->vpi = 0;
2974 }
James Smart549e55c2007-08-02 11:09:51 -04002975 spin_unlock_irq(shost->host_lock);
2976 }
Arnd Bergmannaeb66412016-03-14 15:29:44 +01002977 }
2978 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002979
James Smart46fa3112007-04-25 09:51:45 -04002980 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002981 return 0;
2982}
2983
James Smarte59058c2008-08-24 21:49:00 -04002984/**
James Smart3621a712009-04-06 18:47:14 -04002985 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04002986 * @phba: pointer to lpfc hba data structure.
2987 *
2988 * This routine marks a HBA's management interface as not blocked. Once the
2989 * HBA's management interface is marked as not blocked, all the user space
2990 * access to the HBA, whether they are from sysfs interface or libdfc
2991 * interface will be allowed. The HBA is set to block the management interface
2992 * when the driver prepares the HBA interface for online or offline and then
2993 * set to unblock the management interface afterwards.
2994 **/
James Smart46fa3112007-04-25 09:51:45 -04002995void
James Smart46fa3112007-04-25 09:51:45 -04002996lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2997{
2998 unsigned long iflag;
2999
James Smart2e0fef82007-06-17 19:56:36 -05003000 spin_lock_irqsave(&phba->hbalock, iflag);
3001 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
3002 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04003003}
3004
James Smarte59058c2008-08-24 21:49:00 -04003005/**
James Smart3621a712009-04-06 18:47:14 -04003006 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04003007 * @phba: pointer to lpfc hba data structure.
3008 *
3009 * This routine is invoked to prepare a HBA to be brought offline. It performs
3010 * unregistration login to all the nodes on all vports and flushes the mailbox
3011 * queue to make it ready to be brought offline.
3012 **/
James Smart46fa3112007-04-25 09:51:45 -04003013void
James Smart618a5232012-06-12 13:54:36 -04003014lpfc_offline_prep(struct lpfc_hba *phba, int mbx_action)
James Smart46fa3112007-04-25 09:51:45 -04003015{
James Smart2e0fef82007-06-17 19:56:36 -05003016 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04003017 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04003018 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05003019 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04003020 int i;
dea31012005-04-17 16:05:31 -05003021
James Smart2e0fef82007-06-17 19:56:36 -05003022 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04003023 return;
dea31012005-04-17 16:05:31 -05003024
James Smart618a5232012-06-12 13:54:36 -04003025 lpfc_block_mgmt_io(phba, mbx_action);
dea31012005-04-17 16:05:31 -05003026
3027 lpfc_linkdown(phba);
3028
James Smart87af33f2007-10-27 13:37:43 -04003029 /* Issue an unreg_login to all nodes on all vports */
3030 vports = lpfc_create_vport_work_array(phba);
3031 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04003032 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04003033 if (vports[i]->load_flag & FC_UNLOADING)
3034 continue;
James Smart72100cc2010-02-12 14:43:01 -05003035 shost = lpfc_shost_from_vport(vports[i]);
3036 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003037 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05003038 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
3039 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05003040 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05003041
James Smart87af33f2007-10-27 13:37:43 -04003042 shost = lpfc_shost_from_vport(vports[i]);
3043 list_for_each_entry_safe(ndlp, next_ndlp,
3044 &vports[i]->fc_nodes,
3045 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05003046 if (!NLP_CHK_NODE_ACT(ndlp))
3047 continue;
James Smart87af33f2007-10-27 13:37:43 -04003048 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
3049 continue;
3050 if (ndlp->nlp_type & NLP_FABRIC) {
3051 lpfc_disc_state_machine(vports[i], ndlp,
3052 NULL, NLP_EVT_DEVICE_RECOVERY);
3053 lpfc_disc_state_machine(vports[i], ndlp,
3054 NULL, NLP_EVT_DEVICE_RM);
3055 }
3056 spin_lock_irq(shost->host_lock);
3057 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart401ee0c2012-03-01 22:35:34 -05003058 spin_unlock_irq(shost->host_lock);
James Smart6b5151f2012-01-18 16:24:06 -05003059 /*
3060 * Whenever an SLI4 port goes offline, free the
James Smart401ee0c2012-03-01 22:35:34 -05003061 * RPI. Get a new RPI when the adapter port
3062 * comes back online.
James Smart6b5151f2012-01-18 16:24:06 -05003063 */
James Smartbe6bb942015-04-07 15:07:22 -04003064 if (phba->sli_rev == LPFC_SLI_REV4) {
3065 lpfc_printf_vlog(ndlp->vport,
3066 KERN_INFO, LOG_NODE,
3067 "0011 lpfc_offline: "
3068 "ndlp:x%p did %x "
3069 "usgmap:x%x rpi:%x\n",
3070 ndlp, ndlp->nlp_DID,
3071 ndlp->nlp_usg_map,
3072 ndlp->nlp_rpi);
3073
James Smart6b5151f2012-01-18 16:24:06 -05003074 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smartbe6bb942015-04-07 15:07:22 -04003075 }
James Smart87af33f2007-10-27 13:37:43 -04003076 lpfc_unreg_rpi(vports[i], ndlp);
3077 }
3078 }
3079 }
James Smart09372822008-01-11 01:52:54 -05003080 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05003081
James Smart618a5232012-06-12 13:54:36 -04003082 lpfc_sli_mbox_sys_shutdown(phba, mbx_action);
James Smart46fa3112007-04-25 09:51:45 -04003083}
3084
James Smarte59058c2008-08-24 21:49:00 -04003085/**
James Smart3621a712009-04-06 18:47:14 -04003086 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04003087 * @phba: pointer to lpfc hba data structure.
3088 *
3089 * This routine actually brings a HBA offline. It stops all the timers
3090 * associated with the HBA, brings down the SLI layer, and eventually
3091 * marks the HBA as in offline state for the upper layer protocol.
3092 **/
James Smart46fa3112007-04-25 09:51:45 -04003093void
James Smart2e0fef82007-06-17 19:56:36 -05003094lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04003095{
James Smart549e55c2007-08-02 11:09:51 -04003096 struct Scsi_Host *shost;
3097 struct lpfc_vport **vports;
3098 int i;
James Smart46fa3112007-04-25 09:51:45 -04003099
James Smart549e55c2007-08-02 11:09:51 -04003100 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04003101 return;
James Smart688a8862006-07-06 15:49:56 -04003102
James Smartda0436e2009-05-22 14:51:39 -04003103 /* stop port and all timers associated with this hba */
3104 lpfc_stop_port(phba);
James Smart51ef4c22007-08-02 11:10:31 -04003105 vports = lpfc_create_vport_work_array(phba);
3106 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04003107 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04003108 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05003109 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05003110 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003111 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05003112 /* Bring down the SLI Layer and cleanup. The HBA is offline
3113 now. */
3114 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003115 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04003116 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003117 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04003118 vports = lpfc_create_vport_work_array(phba);
3119 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04003120 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04003121 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04003122 spin_lock_irq(shost->host_lock);
3123 vports[i]->work_port_events = 0;
3124 vports[i]->fc_flag |= FC_OFFLINE_MODE;
3125 spin_unlock_irq(shost->host_lock);
3126 }
James Smart09372822008-01-11 01:52:54 -05003127 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05003128}
3129
James Smarte59058c2008-08-24 21:49:00 -04003130/**
James Smart3621a712009-04-06 18:47:14 -04003131 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04003132 * @phba: pointer to lpfc hba data structure.
3133 *
3134 * This routine is to free all the SCSI buffers and IOCBs from the driver
3135 * list back to kernel. It is called from lpfc_pci_remove_one to free
3136 * the internal resources before the device is removed from the system.
James Smarte59058c2008-08-24 21:49:00 -04003137 **/
James Smart8a9d2e82012-05-09 21:16:12 -04003138static void
James Smart2e0fef82007-06-17 19:56:36 -05003139lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003140{
3141 struct lpfc_scsi_buf *sb, *sb_next;
dea31012005-04-17 16:05:31 -05003142
James Smart895427b2017-02-12 13:52:30 -08003143 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
3144 return;
3145
James Smart2e0fef82007-06-17 19:56:36 -05003146 spin_lock_irq(&phba->hbalock);
James Smarta40fc5f2013-04-17 20:17:40 -04003147
dea31012005-04-17 16:05:31 -05003148 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smarta40fc5f2013-04-17 20:17:40 -04003149
3150 spin_lock(&phba->scsi_buf_list_put_lock);
3151 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_put,
3152 list) {
dea31012005-04-17 16:05:31 -05003153 list_del(&sb->list);
James Smart895427b2017-02-12 13:52:30 -08003154 pci_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05003155 sb->dma_handle);
dea31012005-04-17 16:05:31 -05003156 kfree(sb);
3157 phba->total_scsi_bufs--;
3158 }
James Smarta40fc5f2013-04-17 20:17:40 -04003159 spin_unlock(&phba->scsi_buf_list_put_lock);
3160
3161 spin_lock(&phba->scsi_buf_list_get_lock);
3162 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_get,
3163 list) {
3164 list_del(&sb->list);
James Smart895427b2017-02-12 13:52:30 -08003165 pci_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
James Smarta40fc5f2013-04-17 20:17:40 -04003166 sb->dma_handle);
3167 kfree(sb);
3168 phba->total_scsi_bufs--;
3169 }
3170 spin_unlock(&phba->scsi_buf_list_get_lock);
James Smart2e0fef82007-06-17 19:56:36 -05003171 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003172}
James Smart8a9d2e82012-05-09 21:16:12 -04003173/**
James Smart895427b2017-02-12 13:52:30 -08003174 * lpfc_nvme_free - Free all the NVME buffers and IOCBs from driver lists
3175 * @phba: pointer to lpfc hba data structure.
3176 *
3177 * This routine is to free all the NVME buffers and IOCBs from the driver
3178 * list back to kernel. It is called from lpfc_pci_remove_one to free
3179 * the internal resources before the device is removed from the system.
3180 **/
3181static void
3182lpfc_nvme_free(struct lpfc_hba *phba)
3183{
3184 struct lpfc_nvme_buf *lpfc_ncmd, *lpfc_ncmd_next;
James Smart895427b2017-02-12 13:52:30 -08003185
3186 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
3187 return;
3188
3189 spin_lock_irq(&phba->hbalock);
3190
3191 /* Release all the lpfc_nvme_bufs maintained by this host. */
3192 spin_lock(&phba->nvme_buf_list_put_lock);
3193 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3194 &phba->lpfc_nvme_buf_list_put, list) {
3195 list_del(&lpfc_ncmd->list);
3196 pci_pool_free(phba->lpfc_sg_dma_buf_pool, lpfc_ncmd->data,
3197 lpfc_ncmd->dma_handle);
3198 kfree(lpfc_ncmd);
3199 phba->total_nvme_bufs--;
3200 }
3201 spin_unlock(&phba->nvme_buf_list_put_lock);
3202
3203 spin_lock(&phba->nvme_buf_list_get_lock);
3204 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3205 &phba->lpfc_nvme_buf_list_get, list) {
3206 list_del(&lpfc_ncmd->list);
3207 pci_pool_free(phba->lpfc_sg_dma_buf_pool, lpfc_ncmd->data,
3208 lpfc_ncmd->dma_handle);
3209 kfree(lpfc_ncmd);
3210 phba->total_nvme_bufs--;
3211 }
3212 spin_unlock(&phba->nvme_buf_list_get_lock);
James Smart895427b2017-02-12 13:52:30 -08003213 spin_unlock_irq(&phba->hbalock);
3214}
3215/**
3216 * lpfc_sli4_els_sgl_update - update ELS xri-sgl sizing and mapping
James Smart8a9d2e82012-05-09 21:16:12 -04003217 * @phba: pointer to lpfc hba data structure.
3218 *
3219 * This routine first calculates the sizes of the current els and allocated
3220 * scsi sgl lists, and then goes through all sgls to updates the physical
3221 * XRIs assigned due to port function reset. During port initialization, the
3222 * current els and allocated scsi sgl lists are 0s.
3223 *
3224 * Return codes
3225 * 0 - successful (for now, it always returns 0)
3226 **/
3227int
James Smart895427b2017-02-12 13:52:30 -08003228lpfc_sli4_els_sgl_update(struct lpfc_hba *phba)
James Smart8a9d2e82012-05-09 21:16:12 -04003229{
3230 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
James Smart895427b2017-02-12 13:52:30 -08003231 uint16_t i, lxri, xri_cnt, els_xri_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04003232 LIST_HEAD(els_sgl_list);
James Smart8a9d2e82012-05-09 21:16:12 -04003233 int rc;
3234
3235 /*
3236 * update on pci function's els xri-sgl list
3237 */
3238 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
James Smart895427b2017-02-12 13:52:30 -08003239
James Smart8a9d2e82012-05-09 21:16:12 -04003240 if (els_xri_cnt > phba->sli4_hba.els_xri_cnt) {
3241 /* els xri-sgl expanded */
3242 xri_cnt = els_xri_cnt - phba->sli4_hba.els_xri_cnt;
3243 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3244 "3157 ELS xri-sgl count increased from "
3245 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3246 els_xri_cnt);
3247 /* allocate the additional els sgls */
3248 for (i = 0; i < xri_cnt; i++) {
3249 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3250 GFP_KERNEL);
3251 if (sglq_entry == NULL) {
3252 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3253 "2562 Failure to allocate an "
3254 "ELS sgl entry:%d\n", i);
3255 rc = -ENOMEM;
3256 goto out_free_mem;
3257 }
3258 sglq_entry->buff_type = GEN_BUFF_TYPE;
3259 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0,
3260 &sglq_entry->phys);
3261 if (sglq_entry->virt == NULL) {
3262 kfree(sglq_entry);
3263 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3264 "2563 Failure to allocate an "
3265 "ELS mbuf:%d\n", i);
3266 rc = -ENOMEM;
3267 goto out_free_mem;
3268 }
3269 sglq_entry->sgl = sglq_entry->virt;
3270 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
3271 sglq_entry->state = SGL_FREED;
3272 list_add_tail(&sglq_entry->list, &els_sgl_list);
3273 }
James Smart38c20672013-03-01 16:37:44 -05003274 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08003275 spin_lock(&phba->sli4_hba.sgl_list_lock);
3276 list_splice_init(&els_sgl_list,
3277 &phba->sli4_hba.lpfc_els_sgl_list);
3278 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05003279 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003280 } else if (els_xri_cnt < phba->sli4_hba.els_xri_cnt) {
3281 /* els xri-sgl shrinked */
3282 xri_cnt = phba->sli4_hba.els_xri_cnt - els_xri_cnt;
3283 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3284 "3158 ELS xri-sgl count decreased from "
3285 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3286 els_xri_cnt);
3287 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08003288 spin_lock(&phba->sli4_hba.sgl_list_lock);
3289 list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list,
3290 &els_sgl_list);
James Smart8a9d2e82012-05-09 21:16:12 -04003291 /* release extra els sgls from list */
3292 for (i = 0; i < xri_cnt; i++) {
3293 list_remove_head(&els_sgl_list,
3294 sglq_entry, struct lpfc_sglq, list);
3295 if (sglq_entry) {
James Smart895427b2017-02-12 13:52:30 -08003296 __lpfc_mbuf_free(phba, sglq_entry->virt,
3297 sglq_entry->phys);
James Smart8a9d2e82012-05-09 21:16:12 -04003298 kfree(sglq_entry);
3299 }
3300 }
James Smart895427b2017-02-12 13:52:30 -08003301 list_splice_init(&els_sgl_list,
3302 &phba->sli4_hba.lpfc_els_sgl_list);
3303 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003304 spin_unlock_irq(&phba->hbalock);
3305 } else
3306 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3307 "3163 ELS xri-sgl count unchanged: %d\n",
3308 els_xri_cnt);
3309 phba->sli4_hba.els_xri_cnt = els_xri_cnt;
3310
3311 /* update xris to els sgls on the list */
3312 sglq_entry = NULL;
3313 sglq_entry_next = NULL;
3314 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
James Smart895427b2017-02-12 13:52:30 -08003315 &phba->sli4_hba.lpfc_els_sgl_list, list) {
James Smart8a9d2e82012-05-09 21:16:12 -04003316 lxri = lpfc_sli4_next_xritag(phba);
3317 if (lxri == NO_XRI) {
3318 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3319 "2400 Failed to allocate xri for "
3320 "ELS sgl\n");
3321 rc = -ENOMEM;
3322 goto out_free_mem;
3323 }
3324 sglq_entry->sli4_lxritag = lxri;
3325 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3326 }
James Smart895427b2017-02-12 13:52:30 -08003327 return 0;
3328
3329out_free_mem:
3330 lpfc_free_els_sgl_list(phba);
3331 return rc;
3332}
3333
3334/**
James Smartf358dd02017-02-12 13:52:34 -08003335 * lpfc_sli4_nvmet_sgl_update - update xri-sgl sizing and mapping
3336 * @phba: pointer to lpfc hba data structure.
3337 *
3338 * This routine first calculates the sizes of the current els and allocated
3339 * scsi sgl lists, and then goes through all sgls to updates the physical
3340 * XRIs assigned due to port function reset. During port initialization, the
3341 * current els and allocated scsi sgl lists are 0s.
3342 *
3343 * Return codes
3344 * 0 - successful (for now, it always returns 0)
3345 **/
3346int
3347lpfc_sli4_nvmet_sgl_update(struct lpfc_hba *phba)
3348{
3349 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
3350 uint16_t i, lxri, xri_cnt, els_xri_cnt;
3351 uint16_t nvmet_xri_cnt, tot_cnt;
3352 LIST_HEAD(nvmet_sgl_list);
3353 int rc;
3354
3355 /*
3356 * update on pci function's nvmet xri-sgl list
3357 */
3358 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
James Smart2d7dbc42017-02-12 13:52:35 -08003359 nvmet_xri_cnt = phba->cfg_nvmet_mrq * phba->cfg_nvmet_mrq_post;
James Smartf358dd02017-02-12 13:52:34 -08003360 tot_cnt = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
James Smart2d7dbc42017-02-12 13:52:35 -08003361 if (nvmet_xri_cnt > tot_cnt) {
3362 phba->cfg_nvmet_mrq_post = tot_cnt / phba->cfg_nvmet_mrq;
3363 nvmet_xri_cnt = phba->cfg_nvmet_mrq * phba->cfg_nvmet_mrq_post;
3364 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3365 "6301 NVMET post-sgl count changed to %d\n",
3366 phba->cfg_nvmet_mrq_post);
3367 }
James Smartf358dd02017-02-12 13:52:34 -08003368
3369 if (nvmet_xri_cnt > phba->sli4_hba.nvmet_xri_cnt) {
3370 /* els xri-sgl expanded */
3371 xri_cnt = nvmet_xri_cnt - phba->sli4_hba.nvmet_xri_cnt;
3372 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3373 "6302 NVMET xri-sgl cnt grew from %d to %d\n",
3374 phba->sli4_hba.nvmet_xri_cnt, nvmet_xri_cnt);
3375 /* allocate the additional nvmet sgls */
3376 for (i = 0; i < xri_cnt; i++) {
3377 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3378 GFP_KERNEL);
3379 if (sglq_entry == NULL) {
3380 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3381 "6303 Failure to allocate an "
3382 "NVMET sgl entry:%d\n", i);
3383 rc = -ENOMEM;
3384 goto out_free_mem;
3385 }
3386 sglq_entry->buff_type = NVMET_BUFF_TYPE;
3387 sglq_entry->virt = lpfc_nvmet_buf_alloc(phba, 0,
3388 &sglq_entry->phys);
3389 if (sglq_entry->virt == NULL) {
3390 kfree(sglq_entry);
3391 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3392 "6304 Failure to allocate an "
3393 "NVMET buf:%d\n", i);
3394 rc = -ENOMEM;
3395 goto out_free_mem;
3396 }
3397 sglq_entry->sgl = sglq_entry->virt;
3398 memset(sglq_entry->sgl, 0,
3399 phba->cfg_sg_dma_buf_size);
3400 sglq_entry->state = SGL_FREED;
3401 list_add_tail(&sglq_entry->list, &nvmet_sgl_list);
3402 }
3403 spin_lock_irq(&phba->hbalock);
3404 spin_lock(&phba->sli4_hba.sgl_list_lock);
3405 list_splice_init(&nvmet_sgl_list,
3406 &phba->sli4_hba.lpfc_nvmet_sgl_list);
3407 spin_unlock(&phba->sli4_hba.sgl_list_lock);
3408 spin_unlock_irq(&phba->hbalock);
3409 } else if (nvmet_xri_cnt < phba->sli4_hba.nvmet_xri_cnt) {
3410 /* nvmet xri-sgl shrunk */
3411 xri_cnt = phba->sli4_hba.nvmet_xri_cnt - nvmet_xri_cnt;
3412 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3413 "6305 NVMET xri-sgl count decreased from "
3414 "%d to %d\n", phba->sli4_hba.nvmet_xri_cnt,
3415 nvmet_xri_cnt);
3416 spin_lock_irq(&phba->hbalock);
3417 spin_lock(&phba->sli4_hba.sgl_list_lock);
3418 list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list,
3419 &nvmet_sgl_list);
3420 /* release extra nvmet sgls from list */
3421 for (i = 0; i < xri_cnt; i++) {
3422 list_remove_head(&nvmet_sgl_list,
3423 sglq_entry, struct lpfc_sglq, list);
3424 if (sglq_entry) {
3425 lpfc_nvmet_buf_free(phba, sglq_entry->virt,
3426 sglq_entry->phys);
3427 kfree(sglq_entry);
3428 }
3429 }
3430 list_splice_init(&nvmet_sgl_list,
3431 &phba->sli4_hba.lpfc_nvmet_sgl_list);
3432 spin_unlock(&phba->sli4_hba.sgl_list_lock);
3433 spin_unlock_irq(&phba->hbalock);
3434 } else
3435 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3436 "6306 NVMET xri-sgl count unchanged: %d\n",
3437 nvmet_xri_cnt);
3438 phba->sli4_hba.nvmet_xri_cnt = nvmet_xri_cnt;
3439
3440 /* update xris to nvmet sgls on the list */
3441 sglq_entry = NULL;
3442 sglq_entry_next = NULL;
3443 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
3444 &phba->sli4_hba.lpfc_nvmet_sgl_list, list) {
3445 lxri = lpfc_sli4_next_xritag(phba);
3446 if (lxri == NO_XRI) {
3447 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3448 "6307 Failed to allocate xri for "
3449 "NVMET sgl\n");
3450 rc = -ENOMEM;
3451 goto out_free_mem;
3452 }
3453 sglq_entry->sli4_lxritag = lxri;
3454 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3455 }
3456 return 0;
3457
3458out_free_mem:
3459 lpfc_free_nvmet_sgl_list(phba);
3460 return rc;
3461}
3462
3463/**
James Smart895427b2017-02-12 13:52:30 -08003464 * lpfc_sli4_scsi_sgl_update - update xri-sgl sizing and mapping
3465 * @phba: pointer to lpfc hba data structure.
3466 *
3467 * This routine first calculates the sizes of the current els and allocated
3468 * scsi sgl lists, and then goes through all sgls to updates the physical
3469 * XRIs assigned due to port function reset. During port initialization, the
3470 * current els and allocated scsi sgl lists are 0s.
3471 *
3472 * Return codes
3473 * 0 - successful (for now, it always returns 0)
3474 **/
3475int
3476lpfc_sli4_scsi_sgl_update(struct lpfc_hba *phba)
3477{
3478 struct lpfc_scsi_buf *psb, *psb_next;
3479 uint16_t i, lxri, els_xri_cnt, scsi_xri_cnt;
3480 LIST_HEAD(scsi_sgl_list);
3481 int rc;
3482
3483 /*
3484 * update on pci function's els xri-sgl list
3485 */
3486 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3487 phba->total_scsi_bufs = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04003488
3489 /*
3490 * update on pci function's allocated scsi xri-sgl list
3491 */
James Smart8a9d2e82012-05-09 21:16:12 -04003492 /* maximum number of xris available for scsi buffers */
3493 phba->sli4_hba.scsi_xri_max = phba->sli4_hba.max_cfg_param.max_xri -
3494 els_xri_cnt;
3495
James Smart895427b2017-02-12 13:52:30 -08003496 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
3497 return 0;
3498
3499 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3500 phba->sli4_hba.scsi_xri_max = /* Split them up */
3501 (phba->sli4_hba.scsi_xri_max *
3502 phba->cfg_xri_split) / 100;
James Smart8a9d2e82012-05-09 21:16:12 -04003503
James Smarta40fc5f2013-04-17 20:17:40 -04003504 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart164cecd2013-09-06 12:22:38 -04003505 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003506 list_splice_init(&phba->lpfc_scsi_buf_list_get, &scsi_sgl_list);
3507 list_splice(&phba->lpfc_scsi_buf_list_put, &scsi_sgl_list);
James Smart164cecd2013-09-06 12:22:38 -04003508 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003509 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003510
James Smarte8c0a772017-04-21 16:04:49 -07003511 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3512 "6060 Current allocated SCSI xri-sgl count:%d, "
3513 "maximum SCSI xri count:%d (split:%d)\n",
3514 phba->sli4_hba.scsi_xri_cnt,
3515 phba->sli4_hba.scsi_xri_max, phba->cfg_xri_split);
3516
James Smart8a9d2e82012-05-09 21:16:12 -04003517 if (phba->sli4_hba.scsi_xri_cnt > phba->sli4_hba.scsi_xri_max) {
3518 /* max scsi xri shrinked below the allocated scsi buffers */
3519 scsi_xri_cnt = phba->sli4_hba.scsi_xri_cnt -
3520 phba->sli4_hba.scsi_xri_max;
3521 /* release the extra allocated scsi buffers */
3522 for (i = 0; i < scsi_xri_cnt; i++) {
3523 list_remove_head(&scsi_sgl_list, psb,
3524 struct lpfc_scsi_buf, list);
James Smarta2fc4aef2014-09-03 12:57:55 -04003525 if (psb) {
James Smart895427b2017-02-12 13:52:30 -08003526 pci_pool_free(phba->lpfc_sg_dma_buf_pool,
James Smarta2fc4aef2014-09-03 12:57:55 -04003527 psb->data, psb->dma_handle);
3528 kfree(psb);
3529 }
James Smart8a9d2e82012-05-09 21:16:12 -04003530 }
James Smarta40fc5f2013-04-17 20:17:40 -04003531 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003532 phba->sli4_hba.scsi_xri_cnt -= scsi_xri_cnt;
James Smarta40fc5f2013-04-17 20:17:40 -04003533 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003534 }
3535
3536 /* update xris associated to remaining allocated scsi buffers */
3537 psb = NULL;
3538 psb_next = NULL;
3539 list_for_each_entry_safe(psb, psb_next, &scsi_sgl_list, list) {
3540 lxri = lpfc_sli4_next_xritag(phba);
3541 if (lxri == NO_XRI) {
3542 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3543 "2560 Failed to allocate xri for "
3544 "scsi buffer\n");
3545 rc = -ENOMEM;
3546 goto out_free_mem;
3547 }
3548 psb->cur_iocbq.sli4_lxritag = lxri;
3549 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3550 }
James Smarta40fc5f2013-04-17 20:17:40 -04003551 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart164cecd2013-09-06 12:22:38 -04003552 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003553 list_splice_init(&scsi_sgl_list, &phba->lpfc_scsi_buf_list_get);
3554 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
James Smart164cecd2013-09-06 12:22:38 -04003555 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003556 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
dea31012005-04-17 16:05:31 -05003557 return 0;
James Smart8a9d2e82012-05-09 21:16:12 -04003558
3559out_free_mem:
James Smart8a9d2e82012-05-09 21:16:12 -04003560 lpfc_scsi_free(phba);
3561 return rc;
dea31012005-04-17 16:05:31 -05003562}
3563
James Smart96418b52017-03-04 09:30:31 -08003564static uint64_t
3565lpfc_get_wwpn(struct lpfc_hba *phba)
3566{
3567 uint64_t wwn;
3568 int rc;
3569 LPFC_MBOXQ_t *mboxq;
3570 MAILBOX_t *mb;
3571
3572
3573 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
3574 GFP_KERNEL);
3575 if (!mboxq)
3576 return (uint64_t)-1;
3577
3578 /* First get WWN of HBA instance */
3579 lpfc_read_nv(phba, mboxq);
3580 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
3581 if (rc != MBX_SUCCESS) {
3582 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3583 "6019 Mailbox failed , mbxCmd x%x "
3584 "READ_NV, mbxStatus x%x\n",
3585 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
3586 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
3587 mempool_free(mboxq, phba->mbox_mem_pool);
3588 return (uint64_t) -1;
3589 }
3590 mb = &mboxq->u.mb;
3591 memcpy(&wwn, (char *)mb->un.varRDnvp.portname, sizeof(uint64_t));
3592 /* wwn is WWPN of HBA instance */
3593 mempool_free(mboxq, phba->mbox_mem_pool);
3594 if (phba->sli_rev == LPFC_SLI_REV4)
3595 return be64_to_cpu(wwn);
3596 else
3597 return (((wwn & 0xffffffff00000000) >> 32) |
3598 ((wwn & 0x00000000ffffffff) << 32));
3599
3600}
3601
James Smarte59058c2008-08-24 21:49:00 -04003602/**
James Smart895427b2017-02-12 13:52:30 -08003603 * lpfc_sli4_nvme_sgl_update - update xri-sgl sizing and mapping
3604 * @phba: pointer to lpfc hba data structure.
3605 *
3606 * This routine first calculates the sizes of the current els and allocated
3607 * scsi sgl lists, and then goes through all sgls to updates the physical
3608 * XRIs assigned due to port function reset. During port initialization, the
3609 * current els and allocated scsi sgl lists are 0s.
3610 *
3611 * Return codes
3612 * 0 - successful (for now, it always returns 0)
3613 **/
3614int
3615lpfc_sli4_nvme_sgl_update(struct lpfc_hba *phba)
3616{
3617 struct lpfc_nvme_buf *lpfc_ncmd = NULL, *lpfc_ncmd_next = NULL;
3618 uint16_t i, lxri, els_xri_cnt;
3619 uint16_t nvme_xri_cnt, nvme_xri_max;
3620 LIST_HEAD(nvme_sgl_list);
3621 int rc;
3622
3623 phba->total_nvme_bufs = 0;
3624
3625 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
3626 return 0;
3627 /*
3628 * update on pci function's allocated nvme xri-sgl list
3629 */
3630
3631 /* maximum number of xris available for nvme buffers */
3632 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3633 nvme_xri_max = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
3634 phba->sli4_hba.nvme_xri_max = nvme_xri_max;
3635 phba->sli4_hba.nvme_xri_max -= phba->sli4_hba.scsi_xri_max;
3636
3637 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3638 "6074 Current allocated NVME xri-sgl count:%d, "
3639 "maximum NVME xri count:%d\n",
3640 phba->sli4_hba.nvme_xri_cnt,
3641 phba->sli4_hba.nvme_xri_max);
3642
3643 spin_lock_irq(&phba->nvme_buf_list_get_lock);
3644 spin_lock(&phba->nvme_buf_list_put_lock);
3645 list_splice_init(&phba->lpfc_nvme_buf_list_get, &nvme_sgl_list);
3646 list_splice(&phba->lpfc_nvme_buf_list_put, &nvme_sgl_list);
3647 spin_unlock(&phba->nvme_buf_list_put_lock);
3648 spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3649
3650 if (phba->sli4_hba.nvme_xri_cnt > phba->sli4_hba.nvme_xri_max) {
3651 /* max nvme xri shrunk below the allocated nvme buffers */
3652 spin_lock_irq(&phba->nvme_buf_list_get_lock);
3653 nvme_xri_cnt = phba->sli4_hba.nvme_xri_cnt -
3654 phba->sli4_hba.nvme_xri_max;
3655 spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3656 /* release the extra allocated nvme buffers */
3657 for (i = 0; i < nvme_xri_cnt; i++) {
3658 list_remove_head(&nvme_sgl_list, lpfc_ncmd,
3659 struct lpfc_nvme_buf, list);
3660 if (lpfc_ncmd) {
3661 pci_pool_free(phba->lpfc_sg_dma_buf_pool,
3662 lpfc_ncmd->data,
3663 lpfc_ncmd->dma_handle);
3664 kfree(lpfc_ncmd);
3665 }
3666 }
3667 spin_lock_irq(&phba->nvme_buf_list_get_lock);
3668 phba->sli4_hba.nvme_xri_cnt -= nvme_xri_cnt;
3669 spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3670 }
3671
3672 /* update xris associated to remaining allocated nvme buffers */
3673 lpfc_ncmd = NULL;
3674 lpfc_ncmd_next = NULL;
3675 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3676 &nvme_sgl_list, list) {
3677 lxri = lpfc_sli4_next_xritag(phba);
3678 if (lxri == NO_XRI) {
3679 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3680 "6075 Failed to allocate xri for "
3681 "nvme buffer\n");
3682 rc = -ENOMEM;
3683 goto out_free_mem;
3684 }
3685 lpfc_ncmd->cur_iocbq.sli4_lxritag = lxri;
3686 lpfc_ncmd->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3687 }
3688 spin_lock_irq(&phba->nvme_buf_list_get_lock);
3689 spin_lock(&phba->nvme_buf_list_put_lock);
3690 list_splice_init(&nvme_sgl_list, &phba->lpfc_nvme_buf_list_get);
3691 INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_put);
3692 spin_unlock(&phba->nvme_buf_list_put_lock);
3693 spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3694 return 0;
3695
3696out_free_mem:
3697 lpfc_nvme_free(phba);
3698 return rc;
3699}
3700
3701/**
James Smart3621a712009-04-06 18:47:14 -04003702 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04003703 * @phba: pointer to lpfc hba data structure.
3704 * @instance: a unique integer ID to this FC port.
3705 * @dev: pointer to the device data structure.
3706 *
3707 * This routine creates a FC port for the upper layer protocol. The FC port
3708 * can be created on top of either a physical port or a virtual port provided
3709 * by the HBA. This routine also allocates a SCSI host data structure (shost)
3710 * and associates the FC port created before adding the shost into the SCSI
3711 * layer.
3712 *
3713 * Return codes
3714 * @vport - pointer to the virtual N_Port data structure.
3715 * NULL - port create failed.
3716 **/
James Smart2e0fef82007-06-17 19:56:36 -05003717struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04003718lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04003719{
James Smart2e0fef82007-06-17 19:56:36 -05003720 struct lpfc_vport *vport;
James Smart895427b2017-02-12 13:52:30 -08003721 struct Scsi_Host *shost = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003722 int error = 0;
James Smart96418b52017-03-04 09:30:31 -08003723 int i;
3724 uint64_t wwn;
3725 bool use_no_reset_hba = false;
3726
3727 wwn = lpfc_get_wwpn(phba);
3728
3729 for (i = 0; i < lpfc_no_hba_reset_cnt; i++) {
3730 if (wwn == lpfc_no_hba_reset[i]) {
3731 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3732 "6020 Setting use_no_reset port=%llx\n",
3733 wwn);
3734 use_no_reset_hba = true;
3735 break;
3736 }
3737 }
James Smart47a86172007-04-25 09:53:22 -04003738
James Smart895427b2017-02-12 13:52:30 -08003739 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
3740 if (dev != &phba->pcidev->dev) {
3741 shost = scsi_host_alloc(&lpfc_vport_template,
3742 sizeof(struct lpfc_vport));
3743 } else {
James Smart96418b52017-03-04 09:30:31 -08003744 if (!use_no_reset_hba)
James Smart895427b2017-02-12 13:52:30 -08003745 shost = scsi_host_alloc(&lpfc_template,
3746 sizeof(struct lpfc_vport));
3747 else
James Smart96418b52017-03-04 09:30:31 -08003748 shost = scsi_host_alloc(&lpfc_template_no_hr,
James Smart895427b2017-02-12 13:52:30 -08003749 sizeof(struct lpfc_vport));
3750 }
3751 } else if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
3752 shost = scsi_host_alloc(&lpfc_template_nvme,
James Smartea4142f2015-04-07 15:07:13 -04003753 sizeof(struct lpfc_vport));
3754 }
James Smart2e0fef82007-06-17 19:56:36 -05003755 if (!shost)
3756 goto out;
James Smart47a86172007-04-25 09:53:22 -04003757
James Smart2e0fef82007-06-17 19:56:36 -05003758 vport = (struct lpfc_vport *) shost->hostdata;
3759 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05003760 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05003761 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05003762 vport->fc_rscn_flush = 0;
James Smart3de2a652007-08-02 11:09:59 -04003763 lpfc_get_vport_cfgparam(vport);
James Smart895427b2017-02-12 13:52:30 -08003764
James Smart2e0fef82007-06-17 19:56:36 -05003765 shost->unique_id = instance;
3766 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04003767 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05003768 shost->this_id = -1;
3769 shost->max_cmd_len = 16;
James Smart8b0dff12015-05-22 10:42:38 -04003770 shost->nr_hw_queues = phba->cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04003771 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart28baac72010-02-12 14:42:03 -05003772 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04003773 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smartda0436e2009-05-22 14:51:39 -04003774 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
3775 }
James Smart81301a92008-12-04 22:39:46 -05003776
James Smart47a86172007-04-25 09:53:22 -04003777 /*
James Smart2e0fef82007-06-17 19:56:36 -05003778 * Set initial can_queue value since 0 is no longer supported and
3779 * scsi_add_host will fail. This will be adjusted later based on the
3780 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04003781 */
James Smart2e0fef82007-06-17 19:56:36 -05003782 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04003783 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05003784 shost->transportt = lpfc_vport_transport_template;
3785 vport->port_type = LPFC_NPIV_PORT;
3786 } else {
3787 shost->transportt = lpfc_transport_template;
3788 vport->port_type = LPFC_PHYSICAL_PORT;
3789 }
James Smart47a86172007-04-25 09:53:22 -04003790
James Smart2e0fef82007-06-17 19:56:36 -05003791 /* Initialize all internally managed lists. */
3792 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04003793 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05003794 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04003795
Tomas Jasek33cc5592017-03-03 13:45:48 +01003796 setup_timer(&vport->fc_disctmo, lpfc_disc_timeout,
3797 (unsigned long)vport);
James Smart47a86172007-04-25 09:53:22 -04003798
Tomas Jasek33cc5592017-03-03 13:45:48 +01003799 setup_timer(&vport->els_tmofunc, lpfc_els_timeout,
3800 (unsigned long)vport);
James Smart92494142011-02-16 12:39:44 -05003801
Tomas Jasek33cc5592017-03-03 13:45:48 +01003802 setup_timer(&vport->delayed_disc_tmo, lpfc_delayed_disc_tmo,
3803 (unsigned long)vport);
James Smart92494142011-02-16 12:39:44 -05003804
James Bottomleyd139b9b2009-11-05 13:33:12 -06003805 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05003806 if (error)
3807 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04003808
James Smart549e55c2007-08-02 11:09:51 -04003809 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003810 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04003811 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003812 return vport;
James Smart47a86172007-04-25 09:53:22 -04003813
James Smart2e0fef82007-06-17 19:56:36 -05003814out_put_shost:
3815 scsi_host_put(shost);
3816out:
3817 return NULL;
James Smart47a86172007-04-25 09:53:22 -04003818}
3819
James Smarte59058c2008-08-24 21:49:00 -04003820/**
James Smart3621a712009-04-06 18:47:14 -04003821 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04003822 * @vport: pointer to an lpfc virtual N_Port data structure.
3823 *
3824 * This routine destroys a FC port from the upper layer protocol. All the
3825 * resources associated with the port are released.
3826 **/
James Smart2e0fef82007-06-17 19:56:36 -05003827void
3828destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04003829{
James Smart92d7f7b2007-06-17 19:56:38 -05003830 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3831 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003832
James Smart858c9f62007-06-17 19:56:39 -05003833 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05003834 fc_remove_host(shost);
3835 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04003836
James Smart92d7f7b2007-06-17 19:56:38 -05003837 spin_lock_irq(&phba->hbalock);
3838 list_del_init(&vport->listentry);
3839 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04003840
James Smart92d7f7b2007-06-17 19:56:38 -05003841 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04003842 return;
James Smart47a86172007-04-25 09:53:22 -04003843}
3844
James Smarte59058c2008-08-24 21:49:00 -04003845/**
James Smart3621a712009-04-06 18:47:14 -04003846 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04003847 *
3848 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
3849 * uses the kernel idr facility to perform the task.
3850 *
3851 * Return codes:
3852 * instance - a unique integer ID allocated as the new instance.
3853 * -1 - lpfc get instance failed.
3854 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003855int
3856lpfc_get_instance(void)
3857{
Tejun Heoab516032013-02-27 17:04:44 -08003858 int ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003859
Tejun Heoab516032013-02-27 17:04:44 -08003860 ret = idr_alloc(&lpfc_hba_index, NULL, 0, 0, GFP_KERNEL);
3861 return ret < 0 ? -1 : ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003862}
3863
James Smarte59058c2008-08-24 21:49:00 -04003864/**
James Smart3621a712009-04-06 18:47:14 -04003865 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04003866 * @shost: pointer to SCSI host data structure.
3867 * @time: elapsed time of the scan in jiffies.
3868 *
3869 * This routine is called by the SCSI layer with a SCSI host to determine
3870 * whether the scan host is finished.
3871 *
3872 * Note: there is no scan_start function as adapter initialization will have
3873 * asynchronously kicked off the link initialization.
3874 *
3875 * Return codes
3876 * 0 - SCSI host scan is not over yet.
3877 * 1 - SCSI host scan is over.
3878 **/
James Smart47a86172007-04-25 09:53:22 -04003879int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
3880{
James Smart2e0fef82007-06-17 19:56:36 -05003881 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3882 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05003883 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04003884
James Smart858c9f62007-06-17 19:56:39 -05003885 spin_lock_irq(shost->host_lock);
3886
James Smart51ef4c22007-08-02 11:10:31 -04003887 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05003888 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003889 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05003890 }
James Smart256ec0d2013-04-17 20:14:58 -04003891 if (time >= msecs_to_jiffies(30 * 1000)) {
James Smart2e0fef82007-06-17 19:56:36 -05003892 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003893 "0461 Scanning longer than 30 "
3894 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003895 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003896 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003897 }
James Smart256ec0d2013-04-17 20:14:58 -04003898 if (time >= msecs_to_jiffies(15 * 1000) &&
3899 phba->link_state <= LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -05003900 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003901 "0465 Link down longer than 15 "
3902 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003903 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05003904 goto finished;
James Smart47a86172007-04-25 09:53:22 -04003905 }
3906
James Smart2e0fef82007-06-17 19:56:36 -05003907 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05003908 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003909 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05003910 goto finished;
James Smart256ec0d2013-04-17 20:14:58 -04003911 if (vport->fc_map_cnt == 0 && time < msecs_to_jiffies(2 * 1000))
James Smart858c9f62007-06-17 19:56:39 -05003912 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003913 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05003914 goto finished;
3915
3916 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003917
3918finished:
James Smart858c9f62007-06-17 19:56:39 -05003919 spin_unlock_irq(shost->host_lock);
3920 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05003921}
3922
James Smarte59058c2008-08-24 21:49:00 -04003923/**
James Smart3621a712009-04-06 18:47:14 -04003924 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04003925 * @shost: pointer to SCSI host data structure.
3926 *
3927 * This routine initializes a given SCSI host attributes on a FC port. The
3928 * SCSI host can be either on top of a physical port or a virtual port.
3929 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003930void lpfc_host_attrib_init(struct Scsi_Host *shost)
3931{
3932 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3933 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003934 /*
James Smart2e0fef82007-06-17 19:56:36 -05003935 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04003936 */
3937
James Smart2e0fef82007-06-17 19:56:36 -05003938 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
3939 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04003940 fc_host_supported_classes(shost) = FC_COS_CLASS3;
3941
3942 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003943 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003944 fc_host_supported_fc4s(shost)[2] = 1;
3945 fc_host_supported_fc4s(shost)[7] = 1;
3946
James Smart92d7f7b2007-06-17 19:56:38 -05003947 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
3948 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04003949
3950 fc_host_supported_speeds(shost) = 0;
James Smartd38dd522015-08-31 16:48:17 -04003951 if (phba->lmt & LMT_32Gb)
3952 fc_host_supported_speeds(shost) |= FC_PORTSPEED_32GBIT;
James Smart88a2cfb2011-07-22 18:36:33 -04003953 if (phba->lmt & LMT_16Gb)
3954 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
James Smart47a86172007-04-25 09:53:22 -04003955 if (phba->lmt & LMT_10Gb)
3956 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04003957 if (phba->lmt & LMT_8Gb)
3958 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04003959 if (phba->lmt & LMT_4Gb)
3960 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
3961 if (phba->lmt & LMT_2Gb)
3962 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
3963 if (phba->lmt & LMT_1Gb)
3964 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
3965
3966 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05003967 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
3968 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04003969
Mike Christie0af5d702010-09-15 16:52:31 -05003970 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
3971
James Smart47a86172007-04-25 09:53:22 -04003972 /* This value is also unchanging */
3973 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003974 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003975 fc_host_active_fc4s(shost)[2] = 1;
3976 fc_host_active_fc4s(shost)[7] = 1;
3977
James Smart92d7f7b2007-06-17 19:56:38 -05003978 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04003979 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04003980 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04003981 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04003982}
dea31012005-04-17 16:05:31 -05003983
James Smarte59058c2008-08-24 21:49:00 -04003984/**
James Smartda0436e2009-05-22 14:51:39 -04003985 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04003986 * @phba: pointer to lpfc hba data structure.
3987 *
James Smartda0436e2009-05-22 14:51:39 -04003988 * This routine is invoked to stop an SLI3 device port, it stops the device
3989 * from generating interrupts and stops the device driver's timers for the
3990 * device.
James Smarte59058c2008-08-24 21:49:00 -04003991 **/
James Smartdb2378e2008-02-08 18:49:51 -05003992static void
James Smartda0436e2009-05-22 14:51:39 -04003993lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05003994{
James Smartda0436e2009-05-22 14:51:39 -04003995 /* Clear all interrupt enable conditions */
3996 writel(0, phba->HCregaddr);
3997 readl(phba->HCregaddr); /* flush */
3998 /* Clear all pending interrupts */
3999 writel(0xffffffff, phba->HAregaddr);
4000 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04004001
James Smartda0436e2009-05-22 14:51:39 -04004002 /* Reset some HBA SLI setup states */
4003 lpfc_stop_hba_timers(phba);
4004 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05004005}
4006
James Smarte59058c2008-08-24 21:49:00 -04004007/**
James Smartda0436e2009-05-22 14:51:39 -04004008 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05004009 * @phba: pointer to lpfc hba data structure.
4010 *
James Smartda0436e2009-05-22 14:51:39 -04004011 * This routine is invoked to stop an SLI4 device port, it stops the device
4012 * from generating interrupts and stops the device driver's timers for the
4013 * device.
4014 **/
4015static void
4016lpfc_stop_port_s4(struct lpfc_hba *phba)
4017{
4018 /* Reset some HBA SLI4 setup states */
4019 lpfc_stop_hba_timers(phba);
4020 phba->pport->work_port_events = 0;
4021 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04004022}
4023
4024/**
4025 * lpfc_stop_port - Wrapper function for stopping hba port
4026 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05004027 *
James Smartda0436e2009-05-22 14:51:39 -04004028 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
4029 * the API jump table function pointer from the lpfc_hba struct.
4030 **/
4031void
4032lpfc_stop_port(struct lpfc_hba *phba)
4033{
4034 phba->lpfc_stop_port(phba);
4035}
4036
4037/**
James Smartecfd03c2010-02-12 14:41:27 -05004038 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
4039 * @phba: Pointer to hba for which this call is being executed.
4040 *
4041 * This routine starts the timer waiting for the FCF rediscovery to complete.
4042 **/
4043void
4044lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
4045{
4046 unsigned long fcf_redisc_wait_tmo =
4047 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
4048 /* Start fcf rediscovery wait period timer */
4049 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
4050 spin_lock_irq(&phba->hbalock);
4051 /* Allow action to new fcf asynchronous event */
4052 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
4053 /* Mark the FCF rediscovery pending state */
4054 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
4055 spin_unlock_irq(&phba->hbalock);
4056}
4057
4058/**
4059 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
4060 * @ptr: Map to lpfc_hba data structure pointer.
4061 *
4062 * This routine is invoked when waiting for FCF table rediscover has been
4063 * timed out. If new FCF record(s) has (have) been discovered during the
4064 * wait period, a new FCF event shall be added to the FCOE async event
4065 * list, and then worker thread shall be waked up for processing from the
4066 * worker thread context.
4067 **/
Rashika Kheriae399b222014-09-03 12:55:28 -04004068static void
James Smartecfd03c2010-02-12 14:41:27 -05004069lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
4070{
4071 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
4072
4073 /* Don't send FCF rediscovery event if timer cancelled */
4074 spin_lock_irq(&phba->hbalock);
4075 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
4076 spin_unlock_irq(&phba->hbalock);
4077 return;
4078 }
4079 /* Clear FCF rediscovery timer pending flag */
4080 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
4081 /* FCF rediscovery event to worker thread */
4082 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
4083 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004084 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04004085 "2776 FCF rediscover quiescent timer expired\n");
James Smartecfd03c2010-02-12 14:41:27 -05004086 /* wake up worker thread */
4087 lpfc_worker_wake_up(phba);
4088}
4089
4090/**
James Smartda0436e2009-05-22 14:51:39 -04004091 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
4092 * @phba: pointer to lpfc hba data structure.
4093 * @acqe_link: pointer to the async link completion queue entry.
4094 *
4095 * This routine is to parse the SLI4 link-attention link fault code and
4096 * translate it into the base driver's read link attention mailbox command
4097 * status.
4098 *
4099 * Return: Link-attention status in terms of base driver's coding.
4100 **/
4101static uint16_t
4102lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
4103 struct lpfc_acqe_link *acqe_link)
4104{
4105 uint16_t latt_fault;
4106
4107 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
4108 case LPFC_ASYNC_LINK_FAULT_NONE:
4109 case LPFC_ASYNC_LINK_FAULT_LOCAL:
4110 case LPFC_ASYNC_LINK_FAULT_REMOTE:
4111 latt_fault = 0;
4112 break;
4113 default:
4114 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4115 "0398 Invalid link fault code: x%x\n",
4116 bf_get(lpfc_acqe_link_fault, acqe_link));
4117 latt_fault = MBXERR_ERROR;
4118 break;
4119 }
4120 return latt_fault;
4121}
4122
4123/**
4124 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
4125 * @phba: pointer to lpfc hba data structure.
4126 * @acqe_link: pointer to the async link completion queue entry.
4127 *
4128 * This routine is to parse the SLI4 link attention type and translate it
4129 * into the base driver's link attention type coding.
4130 *
4131 * Return: Link attention type in terms of base driver's coding.
4132 **/
4133static uint8_t
4134lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
4135 struct lpfc_acqe_link *acqe_link)
4136{
4137 uint8_t att_type;
4138
4139 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
4140 case LPFC_ASYNC_LINK_STATUS_DOWN:
4141 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
James Smart76a95d72010-11-20 23:11:48 -05004142 att_type = LPFC_ATT_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04004143 break;
4144 case LPFC_ASYNC_LINK_STATUS_UP:
4145 /* Ignore physical link up events - wait for logical link up */
James Smart76a95d72010-11-20 23:11:48 -05004146 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04004147 break;
4148 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
James Smart76a95d72010-11-20 23:11:48 -05004149 att_type = LPFC_ATT_LINK_UP;
James Smartda0436e2009-05-22 14:51:39 -04004150 break;
4151 default:
4152 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4153 "0399 Invalid link attention type: x%x\n",
4154 bf_get(lpfc_acqe_link_status, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05004155 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04004156 break;
4157 }
4158 return att_type;
4159}
4160
4161/**
James Smart8b68cd52012-09-29 11:32:37 -04004162 * lpfc_sli_port_speed_get - Get sli3 link speed code to link speed
4163 * @phba: pointer to lpfc hba data structure.
4164 *
4165 * This routine is to get an SLI3 FC port's link speed in Mbps.
4166 *
4167 * Return: link speed in terms of Mbps.
4168 **/
4169uint32_t
4170lpfc_sli_port_speed_get(struct lpfc_hba *phba)
4171{
4172 uint32_t link_speed;
4173
4174 if (!lpfc_is_link_up(phba))
4175 return 0;
4176
James Smarta085e872015-12-16 18:12:02 -05004177 if (phba->sli_rev <= LPFC_SLI_REV3) {
4178 switch (phba->fc_linkspeed) {
4179 case LPFC_LINK_SPEED_1GHZ:
4180 link_speed = 1000;
4181 break;
4182 case LPFC_LINK_SPEED_2GHZ:
4183 link_speed = 2000;
4184 break;
4185 case LPFC_LINK_SPEED_4GHZ:
4186 link_speed = 4000;
4187 break;
4188 case LPFC_LINK_SPEED_8GHZ:
4189 link_speed = 8000;
4190 break;
4191 case LPFC_LINK_SPEED_10GHZ:
4192 link_speed = 10000;
4193 break;
4194 case LPFC_LINK_SPEED_16GHZ:
4195 link_speed = 16000;
4196 break;
4197 default:
4198 link_speed = 0;
4199 }
4200 } else {
4201 if (phba->sli4_hba.link_state.logical_speed)
4202 link_speed =
4203 phba->sli4_hba.link_state.logical_speed;
4204 else
4205 link_speed = phba->sli4_hba.link_state.speed;
James Smart8b68cd52012-09-29 11:32:37 -04004206 }
4207 return link_speed;
4208}
4209
4210/**
4211 * lpfc_sli4_port_speed_parse - Parse async evt link speed code to link speed
4212 * @phba: pointer to lpfc hba data structure.
4213 * @evt_code: asynchronous event code.
4214 * @speed_code: asynchronous event link speed code.
4215 *
4216 * This routine is to parse the giving SLI4 async event link speed code into
4217 * value of Mbps for the link speed.
4218 *
4219 * Return: link speed in terms of Mbps.
4220 **/
4221static uint32_t
4222lpfc_sli4_port_speed_parse(struct lpfc_hba *phba, uint32_t evt_code,
4223 uint8_t speed_code)
4224{
4225 uint32_t port_speed;
4226
4227 switch (evt_code) {
4228 case LPFC_TRAILER_CODE_LINK:
4229 switch (speed_code) {
James Smart26d830e2015-04-07 15:07:17 -04004230 case LPFC_ASYNC_LINK_SPEED_ZERO:
James Smart8b68cd52012-09-29 11:32:37 -04004231 port_speed = 0;
4232 break;
James Smart26d830e2015-04-07 15:07:17 -04004233 case LPFC_ASYNC_LINK_SPEED_10MBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004234 port_speed = 10;
4235 break;
James Smart26d830e2015-04-07 15:07:17 -04004236 case LPFC_ASYNC_LINK_SPEED_100MBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004237 port_speed = 100;
4238 break;
James Smart26d830e2015-04-07 15:07:17 -04004239 case LPFC_ASYNC_LINK_SPEED_1GBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004240 port_speed = 1000;
4241 break;
James Smart26d830e2015-04-07 15:07:17 -04004242 case LPFC_ASYNC_LINK_SPEED_10GBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004243 port_speed = 10000;
4244 break;
James Smart26d830e2015-04-07 15:07:17 -04004245 case LPFC_ASYNC_LINK_SPEED_20GBPS:
4246 port_speed = 20000;
4247 break;
4248 case LPFC_ASYNC_LINK_SPEED_25GBPS:
4249 port_speed = 25000;
4250 break;
4251 case LPFC_ASYNC_LINK_SPEED_40GBPS:
4252 port_speed = 40000;
4253 break;
James Smart8b68cd52012-09-29 11:32:37 -04004254 default:
4255 port_speed = 0;
4256 }
4257 break;
4258 case LPFC_TRAILER_CODE_FC:
4259 switch (speed_code) {
James Smart26d830e2015-04-07 15:07:17 -04004260 case LPFC_FC_LA_SPEED_UNKNOWN:
James Smart8b68cd52012-09-29 11:32:37 -04004261 port_speed = 0;
4262 break;
James Smart26d830e2015-04-07 15:07:17 -04004263 case LPFC_FC_LA_SPEED_1G:
James Smart8b68cd52012-09-29 11:32:37 -04004264 port_speed = 1000;
4265 break;
James Smart26d830e2015-04-07 15:07:17 -04004266 case LPFC_FC_LA_SPEED_2G:
James Smart8b68cd52012-09-29 11:32:37 -04004267 port_speed = 2000;
4268 break;
James Smart26d830e2015-04-07 15:07:17 -04004269 case LPFC_FC_LA_SPEED_4G:
James Smart8b68cd52012-09-29 11:32:37 -04004270 port_speed = 4000;
4271 break;
James Smart26d830e2015-04-07 15:07:17 -04004272 case LPFC_FC_LA_SPEED_8G:
James Smart8b68cd52012-09-29 11:32:37 -04004273 port_speed = 8000;
4274 break;
James Smart26d830e2015-04-07 15:07:17 -04004275 case LPFC_FC_LA_SPEED_10G:
James Smart8b68cd52012-09-29 11:32:37 -04004276 port_speed = 10000;
4277 break;
James Smart26d830e2015-04-07 15:07:17 -04004278 case LPFC_FC_LA_SPEED_16G:
James Smart8b68cd52012-09-29 11:32:37 -04004279 port_speed = 16000;
4280 break;
James Smartd38dd522015-08-31 16:48:17 -04004281 case LPFC_FC_LA_SPEED_32G:
4282 port_speed = 32000;
4283 break;
James Smart8b68cd52012-09-29 11:32:37 -04004284 default:
4285 port_speed = 0;
4286 }
4287 break;
4288 default:
4289 port_speed = 0;
4290 }
4291 return port_speed;
4292}
4293
4294/**
James Smart70f3c072010-12-15 17:57:33 -05004295 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
James Smartda0436e2009-05-22 14:51:39 -04004296 * @phba: pointer to lpfc hba data structure.
4297 * @acqe_link: pointer to the async link completion queue entry.
4298 *
James Smart70f3c072010-12-15 17:57:33 -05004299 * This routine is to handle the SLI4 asynchronous FCoE link event.
James Smartda0436e2009-05-22 14:51:39 -04004300 **/
4301static void
4302lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
4303 struct lpfc_acqe_link *acqe_link)
4304{
4305 struct lpfc_dmabuf *mp;
4306 LPFC_MBOXQ_t *pmb;
4307 MAILBOX_t *mb;
James Smart76a95d72010-11-20 23:11:48 -05004308 struct lpfc_mbx_read_top *la;
James Smartda0436e2009-05-22 14:51:39 -04004309 uint8_t att_type;
James Smart76a95d72010-11-20 23:11:48 -05004310 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004311
4312 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05004313 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
James Smartda0436e2009-05-22 14:51:39 -04004314 return;
James Smart32b97932009-07-19 10:01:21 -04004315 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004316 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4317 if (!pmb) {
4318 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4319 "0395 The mboxq allocation failed\n");
4320 return;
4321 }
4322 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4323 if (!mp) {
4324 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4325 "0396 The lpfc_dmabuf allocation failed\n");
4326 goto out_free_pmb;
4327 }
4328 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
4329 if (!mp->virt) {
4330 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4331 "0397 The mbuf allocation failed\n");
4332 goto out_free_dmabuf;
4333 }
4334
4335 /* Cleanup any outstanding ELS commands */
4336 lpfc_els_flush_all_cmd(phba);
4337
4338 /* Block ELS IOCBs until we have done process link event */
James Smart895427b2017-02-12 13:52:30 -08004339 phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
James Smartda0436e2009-05-22 14:51:39 -04004340
4341 /* Update link event statistics */
4342 phba->sli.slistat.link_event++;
4343
James Smart76a95d72010-11-20 23:11:48 -05004344 /* Create lpfc_handle_latt mailbox command from link ACQE */
4345 lpfc_read_topology(phba, pmb, mp);
4346 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smartda0436e2009-05-22 14:51:39 -04004347 pmb->vport = phba->pport;
4348
James Smartda0436e2009-05-22 14:51:39 -04004349 /* Keep the link status for extra SLI4 state machine reference */
4350 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04004351 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_LINK,
4352 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smartda0436e2009-05-22 14:51:39 -04004353 phba->sli4_hba.link_state.duplex =
4354 bf_get(lpfc_acqe_link_duplex, acqe_link);
4355 phba->sli4_hba.link_state.status =
4356 bf_get(lpfc_acqe_link_status, acqe_link);
James Smart70f3c072010-12-15 17:57:33 -05004357 phba->sli4_hba.link_state.type =
4358 bf_get(lpfc_acqe_link_type, acqe_link);
4359 phba->sli4_hba.link_state.number =
4360 bf_get(lpfc_acqe_link_number, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04004361 phba->sli4_hba.link_state.fault =
4362 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05004363 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04004364 bf_get(lpfc_acqe_logical_link_speed, acqe_link) * 10;
4365
James Smart70f3c072010-12-15 17:57:33 -05004366 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartc31098c2011-04-16 11:03:33 -04004367 "2900 Async FC/FCoE Link event - Speed:%dGBit "
4368 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
4369 "Logical speed:%dMbps Fault:%d\n",
James Smart70f3c072010-12-15 17:57:33 -05004370 phba->sli4_hba.link_state.speed,
4371 phba->sli4_hba.link_state.topology,
4372 phba->sli4_hba.link_state.status,
4373 phba->sli4_hba.link_state.type,
4374 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04004375 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05004376 phba->sli4_hba.link_state.fault);
James Smart76a95d72010-11-20 23:11:48 -05004377 /*
4378 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
4379 * topology info. Note: Optional for non FC-AL ports.
4380 */
4381 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
4382 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4383 if (rc == MBX_NOT_FINISHED)
4384 goto out_free_dmabuf;
4385 return;
4386 }
4387 /*
4388 * For FCoE Mode: fill in all the topology information we need and call
4389 * the READ_TOPOLOGY completion routine to continue without actually
4390 * sending the READ_TOPOLOGY mailbox command to the port.
4391 */
4392 /* Parse and translate status field */
4393 mb = &pmb->u.mb;
4394 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
4395
4396 /* Parse and translate link attention fields */
4397 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
4398 la->eventTag = acqe_link->event_tag;
4399 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
4400 bf_set(lpfc_mbx_read_top_link_spd, la,
James Smarta085e872015-12-16 18:12:02 -05004401 (bf_get(lpfc_acqe_link_speed, acqe_link)));
James Smart76a95d72010-11-20 23:11:48 -05004402
4403 /* Fake the the following irrelvant fields */
4404 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
4405 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
4406 bf_set(lpfc_mbx_read_top_il, la, 0);
4407 bf_set(lpfc_mbx_read_top_pb, la, 0);
4408 bf_set(lpfc_mbx_read_top_fa, la, 0);
4409 bf_set(lpfc_mbx_read_top_mm, la, 0);
James Smartda0436e2009-05-22 14:51:39 -04004410
4411 /* Invoke the lpfc_handle_latt mailbox command callback function */
James Smart76a95d72010-11-20 23:11:48 -05004412 lpfc_mbx_cmpl_read_topology(phba, pmb);
James Smartda0436e2009-05-22 14:51:39 -04004413
4414 return;
4415
4416out_free_dmabuf:
4417 kfree(mp);
4418out_free_pmb:
4419 mempool_free(pmb, phba->mbox_mem_pool);
4420}
4421
4422/**
James Smart70f3c072010-12-15 17:57:33 -05004423 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
4424 * @phba: pointer to lpfc hba data structure.
4425 * @acqe_fc: pointer to the async fc completion queue entry.
4426 *
4427 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
4428 * that the event was received and then issue a read_topology mailbox command so
4429 * that the rest of the driver will treat it the same as SLI3.
4430 **/
4431static void
4432lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
4433{
4434 struct lpfc_dmabuf *mp;
4435 LPFC_MBOXQ_t *pmb;
James Smart7bdedb32016-07-06 12:36:00 -07004436 MAILBOX_t *mb;
4437 struct lpfc_mbx_read_top *la;
James Smart70f3c072010-12-15 17:57:33 -05004438 int rc;
4439
4440 if (bf_get(lpfc_trailer_type, acqe_fc) !=
4441 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
4442 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4443 "2895 Non FC link Event detected.(%d)\n",
4444 bf_get(lpfc_trailer_type, acqe_fc));
4445 return;
4446 }
4447 /* Keep the link status for extra SLI4 state machine reference */
4448 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04004449 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
4450 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
James Smart70f3c072010-12-15 17:57:33 -05004451 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
4452 phba->sli4_hba.link_state.topology =
4453 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
4454 phba->sli4_hba.link_state.status =
4455 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
4456 phba->sli4_hba.link_state.type =
4457 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
4458 phba->sli4_hba.link_state.number =
4459 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
4460 phba->sli4_hba.link_state.fault =
4461 bf_get(lpfc_acqe_link_fault, acqe_fc);
4462 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04004463 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
James Smart70f3c072010-12-15 17:57:33 -05004464 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4465 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
4466 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
4467 "%dMbps Fault:%d\n",
4468 phba->sli4_hba.link_state.speed,
4469 phba->sli4_hba.link_state.topology,
4470 phba->sli4_hba.link_state.status,
4471 phba->sli4_hba.link_state.type,
4472 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04004473 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05004474 phba->sli4_hba.link_state.fault);
4475 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4476 if (!pmb) {
4477 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4478 "2897 The mboxq allocation failed\n");
4479 return;
4480 }
4481 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4482 if (!mp) {
4483 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4484 "2898 The lpfc_dmabuf allocation failed\n");
4485 goto out_free_pmb;
4486 }
4487 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
4488 if (!mp->virt) {
4489 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4490 "2899 The mbuf allocation failed\n");
4491 goto out_free_dmabuf;
4492 }
4493
4494 /* Cleanup any outstanding ELS commands */
4495 lpfc_els_flush_all_cmd(phba);
4496
4497 /* Block ELS IOCBs until we have done process link event */
James Smart895427b2017-02-12 13:52:30 -08004498 phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
James Smart70f3c072010-12-15 17:57:33 -05004499
4500 /* Update link event statistics */
4501 phba->sli.slistat.link_event++;
4502
4503 /* Create lpfc_handle_latt mailbox command from link ACQE */
4504 lpfc_read_topology(phba, pmb, mp);
4505 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
4506 pmb->vport = phba->pport;
4507
James Smart7bdedb32016-07-06 12:36:00 -07004508 if (phba->sli4_hba.link_state.status != LPFC_FC_LA_TYPE_LINK_UP) {
4509 /* Parse and translate status field */
4510 mb = &pmb->u.mb;
4511 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba,
4512 (void *)acqe_fc);
4513
4514 /* Parse and translate link attention fields */
4515 la = (struct lpfc_mbx_read_top *)&pmb->u.mb.un.varReadTop;
4516 la->eventTag = acqe_fc->event_tag;
4517 bf_set(lpfc_mbx_read_top_att_type, la,
4518 LPFC_FC_LA_TYPE_LINK_DOWN);
4519
4520 /* Invoke the mailbox command callback function */
4521 lpfc_mbx_cmpl_read_topology(phba, pmb);
4522
4523 return;
4524 }
4525
James Smart70f3c072010-12-15 17:57:33 -05004526 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4527 if (rc == MBX_NOT_FINISHED)
4528 goto out_free_dmabuf;
4529 return;
4530
4531out_free_dmabuf:
4532 kfree(mp);
4533out_free_pmb:
4534 mempool_free(pmb, phba->mbox_mem_pool);
4535}
4536
4537/**
4538 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
4539 * @phba: pointer to lpfc hba data structure.
4540 * @acqe_fc: pointer to the async SLI completion queue entry.
4541 *
4542 * This routine is to handle the SLI4 asynchronous SLI events.
4543 **/
4544static void
4545lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
4546{
James Smart4b8bae02012-06-12 13:55:07 -04004547 char port_name;
James Smart8c1312e2012-10-31 14:45:09 -04004548 char message[128];
James Smart4b8bae02012-06-12 13:55:07 -04004549 uint8_t status;
James Smart946727d2015-04-07 15:07:09 -04004550 uint8_t evt_type;
James Smart448193b2015-12-16 18:12:05 -05004551 uint8_t operational = 0;
James Smart946727d2015-04-07 15:07:09 -04004552 struct temp_event temp_event_data;
James Smart4b8bae02012-06-12 13:55:07 -04004553 struct lpfc_acqe_misconfigured_event *misconfigured;
James Smart946727d2015-04-07 15:07:09 -04004554 struct Scsi_Host *shost;
James Smart4b8bae02012-06-12 13:55:07 -04004555
James Smart946727d2015-04-07 15:07:09 -04004556 evt_type = bf_get(lpfc_trailer_type, acqe_sli);
4557
James Smart448193b2015-12-16 18:12:05 -05004558 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4559 "2901 Async SLI event - Event Data1:x%08x Event Data2:"
4560 "x%08x SLI Event Type:%d\n",
4561 acqe_sli->event_data1, acqe_sli->event_data2,
4562 evt_type);
James Smart4b8bae02012-06-12 13:55:07 -04004563
4564 port_name = phba->Port[0];
4565 if (port_name == 0x00)
4566 port_name = '?'; /* get port name is empty */
4567
James Smart946727d2015-04-07 15:07:09 -04004568 switch (evt_type) {
4569 case LPFC_SLI_EVENT_TYPE_OVER_TEMP:
4570 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
4571 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
4572 temp_event_data.data = (uint32_t)acqe_sli->event_data1;
4573
4574 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4575 "3190 Over Temperature:%d Celsius- Port Name %c\n",
4576 acqe_sli->event_data1, port_name);
4577
James Smart310429e2016-07-06 12:35:54 -07004578 phba->sfp_warning |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
James Smart946727d2015-04-07 15:07:09 -04004579 shost = lpfc_shost_from_vport(phba->pport);
4580 fc_host_post_vendor_event(shost, fc_get_event_number(),
4581 sizeof(temp_event_data),
4582 (char *)&temp_event_data,
4583 SCSI_NL_VID_TYPE_PCI
4584 | PCI_VENDOR_ID_EMULEX);
4585 break;
4586 case LPFC_SLI_EVENT_TYPE_NORM_TEMP:
4587 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
4588 temp_event_data.event_code = LPFC_NORMAL_TEMP;
4589 temp_event_data.data = (uint32_t)acqe_sli->event_data1;
4590
4591 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4592 "3191 Normal Temperature:%d Celsius - Port Name %c\n",
4593 acqe_sli->event_data1, port_name);
4594
4595 shost = lpfc_shost_from_vport(phba->pport);
4596 fc_host_post_vendor_event(shost, fc_get_event_number(),
4597 sizeof(temp_event_data),
4598 (char *)&temp_event_data,
4599 SCSI_NL_VID_TYPE_PCI
4600 | PCI_VENDOR_ID_EMULEX);
4601 break;
4602 case LPFC_SLI_EVENT_TYPE_MISCONFIGURED:
4603 misconfigured = (struct lpfc_acqe_misconfigured_event *)
James Smart4b8bae02012-06-12 13:55:07 -04004604 &acqe_sli->event_data1;
4605
James Smart946727d2015-04-07 15:07:09 -04004606 /* fetch the status for this port */
4607 switch (phba->sli4_hba.lnk_info.lnk_no) {
4608 case LPFC_LINK_NUMBER_0:
James Smart448193b2015-12-16 18:12:05 -05004609 status = bf_get(lpfc_sli_misconfigured_port0_state,
4610 &misconfigured->theEvent);
4611 operational = bf_get(lpfc_sli_misconfigured_port0_op,
James Smart4b8bae02012-06-12 13:55:07 -04004612 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04004613 break;
4614 case LPFC_LINK_NUMBER_1:
James Smart448193b2015-12-16 18:12:05 -05004615 status = bf_get(lpfc_sli_misconfigured_port1_state,
4616 &misconfigured->theEvent);
4617 operational = bf_get(lpfc_sli_misconfigured_port1_op,
James Smart4b8bae02012-06-12 13:55:07 -04004618 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04004619 break;
4620 case LPFC_LINK_NUMBER_2:
James Smart448193b2015-12-16 18:12:05 -05004621 status = bf_get(lpfc_sli_misconfigured_port2_state,
4622 &misconfigured->theEvent);
4623 operational = bf_get(lpfc_sli_misconfigured_port2_op,
James Smart4b8bae02012-06-12 13:55:07 -04004624 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04004625 break;
4626 case LPFC_LINK_NUMBER_3:
James Smart448193b2015-12-16 18:12:05 -05004627 status = bf_get(lpfc_sli_misconfigured_port3_state,
4628 &misconfigured->theEvent);
4629 operational = bf_get(lpfc_sli_misconfigured_port3_op,
James Smart4b8bae02012-06-12 13:55:07 -04004630 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04004631 break;
4632 default:
James Smart448193b2015-12-16 18:12:05 -05004633 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4634 "3296 "
4635 "LPFC_SLI_EVENT_TYPE_MISCONFIGURED "
4636 "event: Invalid link %d",
4637 phba->sli4_hba.lnk_info.lnk_no);
4638 return;
James Smart946727d2015-04-07 15:07:09 -04004639 }
James Smart4b8bae02012-06-12 13:55:07 -04004640
James Smart448193b2015-12-16 18:12:05 -05004641 /* Skip if optic state unchanged */
4642 if (phba->sli4_hba.lnk_info.optic_state == status)
4643 return;
4644
James Smart946727d2015-04-07 15:07:09 -04004645 switch (status) {
4646 case LPFC_SLI_EVENT_STATUS_VALID:
James Smart448193b2015-12-16 18:12:05 -05004647 sprintf(message, "Physical Link is functional");
4648 break;
James Smart946727d2015-04-07 15:07:09 -04004649 case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
4650 sprintf(message, "Optics faulted/incorrectly "
4651 "installed/not installed - Reseat optics, "
4652 "if issue not resolved, replace.");
4653 break;
4654 case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
4655 sprintf(message,
4656 "Optics of two types installed - Remove one "
4657 "optic or install matching pair of optics.");
4658 break;
4659 case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
4660 sprintf(message, "Incompatible optics - Replace with "
James Smart292098b2012-09-29 11:31:41 -04004661 "compatible optics for card to function.");
James Smart946727d2015-04-07 15:07:09 -04004662 break;
James Smart448193b2015-12-16 18:12:05 -05004663 case LPFC_SLI_EVENT_STATUS_UNQUALIFIED:
4664 sprintf(message, "Unqualified optics - Replace with "
4665 "Avago optics for Warranty and Technical "
4666 "Support - Link is%s operational",
James Smart2ea259e2017-02-12 13:52:27 -08004667 (operational) ? " not" : "");
James Smart448193b2015-12-16 18:12:05 -05004668 break;
4669 case LPFC_SLI_EVENT_STATUS_UNCERTIFIED:
4670 sprintf(message, "Uncertified optics - Replace with "
4671 "Avago-certified optics to enable link "
4672 "operation - Link is%s operational",
James Smart2ea259e2017-02-12 13:52:27 -08004673 (operational) ? " not" : "");
James Smart448193b2015-12-16 18:12:05 -05004674 break;
James Smart946727d2015-04-07 15:07:09 -04004675 default:
4676 /* firmware is reporting a status we don't know about */
4677 sprintf(message, "Unknown event status x%02x", status);
4678 break;
4679 }
James Smart448193b2015-12-16 18:12:05 -05004680 phba->sli4_hba.lnk_info.optic_state = status;
James Smart946727d2015-04-07 15:07:09 -04004681 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart448193b2015-12-16 18:12:05 -05004682 "3176 Port Name %c %s\n", port_name, message);
James Smart946727d2015-04-07 15:07:09 -04004683 break;
4684 case LPFC_SLI_EVENT_TYPE_REMOTE_DPORT:
4685 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4686 "3192 Remote DPort Test Initiated - "
4687 "Event Data1:x%08x Event Data2: x%08x\n",
4688 acqe_sli->event_data1, acqe_sli->event_data2);
James Smart4b8bae02012-06-12 13:55:07 -04004689 break;
4690 default:
James Smart946727d2015-04-07 15:07:09 -04004691 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4692 "3193 Async SLI event - Event Data1:x%08x Event Data2:"
4693 "x%08x SLI Event Type:%d\n",
4694 acqe_sli->event_data1, acqe_sli->event_data2,
4695 evt_type);
James Smart4b8bae02012-06-12 13:55:07 -04004696 break;
4697 }
James Smart70f3c072010-12-15 17:57:33 -05004698}
4699
4700/**
James Smartfc2b9892010-02-26 14:15:29 -05004701 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
4702 * @vport: pointer to vport data structure.
4703 *
4704 * This routine is to perform Clear Virtual Link (CVL) on a vport in
4705 * response to a CVL event.
4706 *
4707 * Return the pointer to the ndlp with the vport if successful, otherwise
4708 * return NULL.
4709 **/
4710static struct lpfc_nodelist *
4711lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
4712{
4713 struct lpfc_nodelist *ndlp;
4714 struct Scsi_Host *shost;
4715 struct lpfc_hba *phba;
4716
4717 if (!vport)
4718 return NULL;
James Smartfc2b9892010-02-26 14:15:29 -05004719 phba = vport->phba;
4720 if (!phba)
4721 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04004722 ndlp = lpfc_findnode_did(vport, Fabric_DID);
4723 if (!ndlp) {
4724 /* Cannot find existing Fabric ndlp, so allocate a new one */
4725 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
4726 if (!ndlp)
4727 return 0;
4728 lpfc_nlp_init(vport, ndlp, Fabric_DID);
4729 /* Set the node type */
4730 ndlp->nlp_type |= NLP_FABRIC;
4731 /* Put ndlp onto node list */
4732 lpfc_enqueue_node(vport, ndlp);
4733 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
4734 /* re-setup ndlp without removing from node list */
4735 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
4736 if (!ndlp)
4737 return 0;
4738 }
James Smart63e801c2010-11-20 23:14:19 -05004739 if ((phba->pport->port_state < LPFC_FLOGI) &&
4740 (phba->pport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05004741 return NULL;
4742 /* If virtual link is not yet instantiated ignore CVL */
James Smart63e801c2010-11-20 23:14:19 -05004743 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
4744 && (vport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05004745 return NULL;
4746 shost = lpfc_shost_from_vport(vport);
4747 if (!shost)
4748 return NULL;
4749 lpfc_linkdown_port(vport);
4750 lpfc_cleanup_pending_mbox(vport);
4751 spin_lock_irq(shost->host_lock);
4752 vport->fc_flag |= FC_VPORT_CVL_RCVD;
4753 spin_unlock_irq(shost->host_lock);
4754
4755 return ndlp;
4756}
4757
4758/**
4759 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
4760 * @vport: pointer to lpfc hba data structure.
4761 *
4762 * This routine is to perform Clear Virtual Link (CVL) on all vports in
4763 * response to a FCF dead event.
4764 **/
4765static void
4766lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
4767{
4768 struct lpfc_vport **vports;
4769 int i;
4770
4771 vports = lpfc_create_vport_work_array(phba);
4772 if (vports)
4773 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
4774 lpfc_sli4_perform_vport_cvl(vports[i]);
4775 lpfc_destroy_vport_work_array(phba, vports);
4776}
4777
4778/**
James Smart76a95d72010-11-20 23:11:48 -05004779 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
James Smartda0436e2009-05-22 14:51:39 -04004780 * @phba: pointer to lpfc hba data structure.
4781 * @acqe_link: pointer to the async fcoe completion queue entry.
4782 *
4783 * This routine is to handle the SLI4 asynchronous fcoe event.
4784 **/
4785static void
James Smart76a95d72010-11-20 23:11:48 -05004786lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
James Smart70f3c072010-12-15 17:57:33 -05004787 struct lpfc_acqe_fip *acqe_fip)
James Smartda0436e2009-05-22 14:51:39 -04004788{
James Smart70f3c072010-12-15 17:57:33 -05004789 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004790 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04004791 struct lpfc_vport *vport;
4792 struct lpfc_nodelist *ndlp;
4793 struct Scsi_Host *shost;
James Smart695a8142010-01-26 23:08:03 -05004794 int active_vlink_present;
4795 struct lpfc_vport **vports;
4796 int i;
James Smartda0436e2009-05-22 14:51:39 -04004797
James Smart70f3c072010-12-15 17:57:33 -05004798 phba->fc_eventTag = acqe_fip->event_tag;
4799 phba->fcoe_eventtag = acqe_fip->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004800 switch (event_type) {
James Smart70f3c072010-12-15 17:57:33 -05004801 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
4802 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
4803 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
James Smart999d8132010-03-15 11:24:56 -04004804 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4805 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004806 "2546 New FCF event, evt_tag:x%x, "
4807 "index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004808 acqe_fip->event_tag,
4809 acqe_fip->index);
James Smart999d8132010-03-15 11:24:56 -04004810 else
4811 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
4812 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004813 "2788 FCF param modified event, "
4814 "evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004815 acqe_fip->event_tag,
4816 acqe_fip->index);
James Smart38b92ef2010-08-04 16:11:39 -04004817 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004818 /*
4819 * During period of FCF discovery, read the FCF
4820 * table record indexed by the event to update
James Smarta93ff372010-10-22 11:06:08 -04004821 * FCF roundrobin failover eligible FCF bmask.
James Smart0c9ab6f2010-02-26 14:15:57 -05004822 */
4823 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4824 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004825 "2779 Read FCF (x%x) for updating "
4826 "roundrobin FCF failover bmask\n",
James Smart70f3c072010-12-15 17:57:33 -05004827 acqe_fip->index);
4828 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05004829 }
James Smart38b92ef2010-08-04 16:11:39 -04004830
4831 /* If the FCF discovery is in progress, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04004832 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04004833 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart38b92ef2010-08-04 16:11:39 -04004834 spin_unlock_irq(&phba->hbalock);
4835 break;
4836 }
4837 /* If fast FCF failover rescan event is pending, do nothing */
4838 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
4839 spin_unlock_irq(&phba->hbalock);
4840 break;
4841 }
4842
James Smartc2b97122013-05-31 17:05:36 -04004843 /* If the FCF has been in discovered state, do nothing. */
4844 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
James Smart3804dc82010-07-14 15:31:37 -04004845 spin_unlock_irq(&phba->hbalock);
4846 break;
4847 }
4848 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04004849
James Smart0c9ab6f2010-02-26 14:15:57 -05004850 /* Otherwise, scan the entire FCF table and re-discover SAN */
4851 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004852 "2770 Start FCF table scan per async FCF "
4853 "event, evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004854 acqe_fip->event_tag, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05004855 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
4856 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04004857 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05004858 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4859 "2547 Issue FCF scan read FCF mailbox "
James Smarta93ff372010-10-22 11:06:08 -04004860 "command failed (x%x)\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004861 break;
4862
James Smart70f3c072010-12-15 17:57:33 -05004863 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
James Smartda0436e2009-05-22 14:51:39 -04004864 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04004865 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004866 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
4867 acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04004868 break;
4869
James Smart70f3c072010-12-15 17:57:33 -05004870 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
James Smart80c17842012-03-01 22:35:45 -05004871 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05004872 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004873 "2549 FCF (x%x) disconnected from network, "
James Smart70f3c072010-12-15 17:57:33 -05004874 "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart38b92ef2010-08-04 16:11:39 -04004875 /*
4876 * If we are in the middle of FCF failover process, clear
4877 * the corresponding FCF bit in the roundrobin bitmap.
James Smartda0436e2009-05-22 14:51:39 -04004878 */
James Smartfc2b9892010-02-26 14:15:29 -05004879 spin_lock_irq(&phba->hbalock);
James Smarta1cadfe2016-07-06 12:36:02 -07004880 if ((phba->fcf.fcf_flag & FCF_DISCOVERY) &&
4881 (phba->fcf.current_rec.fcf_indx != acqe_fip->index)) {
James Smartfc2b9892010-02-26 14:15:29 -05004882 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004883 /* Update FLOGI FCF failover eligible FCF bmask */
James Smart70f3c072010-12-15 17:57:33 -05004884 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004885 break;
4886 }
James Smart38b92ef2010-08-04 16:11:39 -04004887 spin_unlock_irq(&phba->hbalock);
4888
4889 /* If the event is not for currently used fcf do nothing */
James Smart70f3c072010-12-15 17:57:33 -05004890 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
James Smart38b92ef2010-08-04 16:11:39 -04004891 break;
4892
4893 /*
4894 * Otherwise, request the port to rediscover the entire FCF
4895 * table for a fast recovery from case that the current FCF
4896 * is no longer valid as we are not in the middle of FCF
4897 * failover process already.
4898 */
James Smartc2b97122013-05-31 17:05:36 -04004899 spin_lock_irq(&phba->hbalock);
4900 /* Mark the fast failover process in progress */
4901 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
4902 spin_unlock_irq(&phba->hbalock);
4903
4904 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
4905 "2771 Start FCF fast failover process due to "
4906 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
4907 "\n", acqe_fip->event_tag, acqe_fip->index);
4908 rc = lpfc_sli4_redisc_fcf_table(phba);
4909 if (rc) {
4910 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4911 LOG_DISCOVERY,
4912 "2772 Issue FCF rediscover mabilbox "
4913 "command failed, fail through to FCF "
4914 "dead event\n");
4915 spin_lock_irq(&phba->hbalock);
4916 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
4917 spin_unlock_irq(&phba->hbalock);
4918 /*
4919 * Last resort will fail over by treating this
4920 * as a link down to FCF registration.
4921 */
4922 lpfc_sli4_fcf_dead_failthrough(phba);
4923 } else {
4924 /* Reset FCF roundrobin bmask for new discovery */
4925 lpfc_sli4_clear_fcf_rr_bmask(phba);
4926 /*
4927 * Handling fast FCF failover to a DEAD FCF event is
4928 * considered equalivant to receiving CVL to all vports.
4929 */
4930 lpfc_sli4_perform_all_vport_cvl(phba);
4931 }
James Smartda0436e2009-05-22 14:51:39 -04004932 break;
James Smart70f3c072010-12-15 17:57:33 -05004933 case LPFC_FIP_EVENT_TYPE_CVL:
James Smart80c17842012-03-01 22:35:45 -05004934 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05004935 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6669f9b2009-10-02 15:16:45 -04004936 "2718 Clear Virtual Link Received for VPI 0x%x"
James Smart70f3c072010-12-15 17:57:33 -05004937 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart6d368e52011-05-24 11:44:12 -04004938
James Smart6669f9b2009-10-02 15:16:45 -04004939 vport = lpfc_find_vport_by_vpid(phba,
James Smart5248a742011-07-22 18:37:06 -04004940 acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004941 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04004942 if (!ndlp)
4943 break;
James Smart695a8142010-01-26 23:08:03 -05004944 active_vlink_present = 0;
4945
4946 vports = lpfc_create_vport_work_array(phba);
4947 if (vports) {
4948 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
4949 i++) {
4950 if ((!(vports[i]->fc_flag &
4951 FC_VPORT_CVL_RCVD)) &&
4952 (vports[i]->port_state > LPFC_FDISC)) {
4953 active_vlink_present = 1;
4954 break;
4955 }
4956 }
4957 lpfc_destroy_vport_work_array(phba, vports);
4958 }
4959
James Smartcc823552015-05-21 13:55:26 -04004960 /*
4961 * Don't re-instantiate if vport is marked for deletion.
4962 * If we are here first then vport_delete is going to wait
4963 * for discovery to complete.
4964 */
4965 if (!(vport->load_flag & FC_UNLOADING) &&
4966 active_vlink_present) {
James Smart695a8142010-01-26 23:08:03 -05004967 /*
4968 * If there are other active VLinks present,
4969 * re-instantiate the Vlink using FDISC.
4970 */
James Smart256ec0d2013-04-17 20:14:58 -04004971 mod_timer(&ndlp->nlp_delayfunc,
4972 jiffies + msecs_to_jiffies(1000));
James Smartfc2b9892010-02-26 14:15:29 -05004973 shost = lpfc_shost_from_vport(vport);
James Smart6669f9b2009-10-02 15:16:45 -04004974 spin_lock_irq(shost->host_lock);
4975 ndlp->nlp_flag |= NLP_DELAY_TMO;
4976 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05004977 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
4978 vport->port_state = LPFC_FDISC;
4979 } else {
James Smartecfd03c2010-02-12 14:41:27 -05004980 /*
4981 * Otherwise, we request port to rediscover
4982 * the entire FCF table for a fast recovery
4983 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05004984 * is no longer valid if we are not already
4985 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05004986 */
James Smartfc2b9892010-02-26 14:15:29 -05004987 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004988 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05004989 spin_unlock_irq(&phba->hbalock);
4990 break;
4991 }
4992 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05004993 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004994 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004995 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4996 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004997 "2773 Start FCF failover per CVL, "
James Smart70f3c072010-12-15 17:57:33 -05004998 "evt_tag:x%x\n", acqe_fip->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05004999 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05005000 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05005001 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
5002 LOG_DISCOVERY,
5003 "2774 Issue FCF rediscover "
5004 "mabilbox command failed, "
5005 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05005006 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05005007 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05005008 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05005009 /*
5010 * Last resort will be re-try on the
5011 * the current registered FCF entry.
5012 */
5013 lpfc_retry_pport_discovery(phba);
James Smart38b92ef2010-08-04 16:11:39 -04005014 } else
5015 /*
5016 * Reset FCF roundrobin bmask for new
5017 * discovery.
5018 */
James Smart7d791df2011-07-22 18:37:52 -04005019 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart6669f9b2009-10-02 15:16:45 -04005020 }
5021 break;
James Smartda0436e2009-05-22 14:51:39 -04005022 default:
5023 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5024 "0288 Unknown FCoE event type 0x%x event tag "
James Smart70f3c072010-12-15 17:57:33 -05005025 "0x%x\n", event_type, acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04005026 break;
5027 }
5028}
5029
5030/**
5031 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
5032 * @phba: pointer to lpfc hba data structure.
5033 * @acqe_link: pointer to the async dcbx completion queue entry.
5034 *
5035 * This routine is to handle the SLI4 asynchronous dcbx event.
5036 **/
5037static void
5038lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
5039 struct lpfc_acqe_dcbx *acqe_dcbx)
5040{
James Smart4d9ab992009-10-02 15:16:39 -04005041 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04005042 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5043 "0290 The SLI4 DCBX asynchronous event is not "
5044 "handled yet\n");
5045}
5046
5047/**
James Smartb19a0612010-04-06 14:48:51 -04005048 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
5049 * @phba: pointer to lpfc hba data structure.
5050 * @acqe_link: pointer to the async grp5 completion queue entry.
5051 *
5052 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
5053 * is an asynchronous notified of a logical link speed change. The Port
5054 * reports the logical link speed in units of 10Mbps.
5055 **/
5056static void
5057lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
5058 struct lpfc_acqe_grp5 *acqe_grp5)
5059{
5060 uint16_t prev_ll_spd;
5061
5062 phba->fc_eventTag = acqe_grp5->event_tag;
5063 phba->fcoe_eventtag = acqe_grp5->event_tag;
5064 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
5065 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04005066 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5)) * 10;
James Smartb19a0612010-04-06 14:48:51 -04005067 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5068 "2789 GRP5 Async Event: Updating logical link speed "
James Smart8b68cd52012-09-29 11:32:37 -04005069 "from %dMbps to %dMbps\n", prev_ll_spd,
5070 phba->sli4_hba.link_state.logical_speed);
James Smartb19a0612010-04-06 14:48:51 -04005071}
5072
5073/**
James Smartda0436e2009-05-22 14:51:39 -04005074 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
5075 * @phba: pointer to lpfc hba data structure.
5076 *
5077 * This routine is invoked by the worker thread to process all the pending
5078 * SLI4 asynchronous events.
5079 **/
5080void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
5081{
5082 struct lpfc_cq_event *cq_event;
5083
5084 /* First, declare the async event has been handled */
5085 spin_lock_irq(&phba->hbalock);
5086 phba->hba_flag &= ~ASYNC_EVENT;
5087 spin_unlock_irq(&phba->hbalock);
5088 /* Now, handle all the async events */
5089 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
5090 /* Get the first event from the head of the event queue */
5091 spin_lock_irq(&phba->hbalock);
5092 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
5093 cq_event, struct lpfc_cq_event, list);
5094 spin_unlock_irq(&phba->hbalock);
5095 /* Process the asynchronous event */
5096 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
5097 case LPFC_TRAILER_CODE_LINK:
5098 lpfc_sli4_async_link_evt(phba,
5099 &cq_event->cqe.acqe_link);
5100 break;
5101 case LPFC_TRAILER_CODE_FCOE:
James Smart70f3c072010-12-15 17:57:33 -05005102 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04005103 break;
5104 case LPFC_TRAILER_CODE_DCBX:
5105 lpfc_sli4_async_dcbx_evt(phba,
5106 &cq_event->cqe.acqe_dcbx);
5107 break;
James Smartb19a0612010-04-06 14:48:51 -04005108 case LPFC_TRAILER_CODE_GRP5:
5109 lpfc_sli4_async_grp5_evt(phba,
5110 &cq_event->cqe.acqe_grp5);
5111 break;
James Smart70f3c072010-12-15 17:57:33 -05005112 case LPFC_TRAILER_CODE_FC:
5113 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
5114 break;
5115 case LPFC_TRAILER_CODE_SLI:
5116 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
5117 break;
James Smartda0436e2009-05-22 14:51:39 -04005118 default:
5119 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5120 "1804 Invalid asynchrous event code: "
5121 "x%x\n", bf_get(lpfc_trailer_code,
5122 &cq_event->cqe.mcqe_cmpl));
5123 break;
5124 }
5125 /* Free the completion event processed to the free pool */
5126 lpfc_sli4_cq_event_release(phba, cq_event);
5127 }
5128}
5129
5130/**
James Smartecfd03c2010-02-12 14:41:27 -05005131 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
5132 * @phba: pointer to lpfc hba data structure.
5133 *
5134 * This routine is invoked by the worker thread to process FCF table
5135 * rediscovery pending completion event.
5136 **/
5137void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
5138{
5139 int rc;
5140
5141 spin_lock_irq(&phba->hbalock);
5142 /* Clear FCF rediscovery timeout event */
5143 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
5144 /* Clear driver fast failover FCF record flag */
5145 phba->fcf.failover_rec.flag = 0;
5146 /* Set state for FCF fast failover */
5147 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
5148 spin_unlock_irq(&phba->hbalock);
5149
5150 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05005151 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04005152 "2777 Start post-quiescent FCF table scan\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05005153 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05005154 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05005155 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
5156 "2747 Issue FCF scan read FCF mailbox "
5157 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05005158}
5159
5160/**
James Smartda0436e2009-05-22 14:51:39 -04005161 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
5162 * @phba: pointer to lpfc hba data structure.
5163 * @dev_grp: The HBA PCI-Device group number.
5164 *
5165 * This routine is invoked to set up the per HBA PCI-Device group function
5166 * API jump table entries.
5167 *
5168 * Return: 0 if success, otherwise -ENODEV
5169 **/
5170int
5171lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05005172{
5173 int rc;
5174
James Smartda0436e2009-05-22 14:51:39 -04005175 /* Set up lpfc PCI-device group */
5176 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05005177
James Smartda0436e2009-05-22 14:51:39 -04005178 /* The LPFC_PCI_DEV_OC uses SLI4 */
5179 if (dev_grp == LPFC_PCI_DEV_OC)
5180 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05005181
James Smartda0436e2009-05-22 14:51:39 -04005182 /* Set up device INIT API function jump table */
5183 rc = lpfc_init_api_table_setup(phba, dev_grp);
5184 if (rc)
5185 return -ENODEV;
5186 /* Set up SCSI API function jump table */
5187 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
5188 if (rc)
5189 return -ENODEV;
5190 /* Set up SLI API function jump table */
5191 rc = lpfc_sli_api_table_setup(phba, dev_grp);
5192 if (rc)
5193 return -ENODEV;
5194 /* Set up MBOX API function jump table */
5195 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
5196 if (rc)
5197 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05005198
James Smartda0436e2009-05-22 14:51:39 -04005199 return 0;
James Smart5b75da22008-12-04 22:39:35 -05005200}
5201
5202/**
James Smart3621a712009-04-06 18:47:14 -04005203 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05005204 * @phba: pointer to lpfc hba data structure.
5205 * @intr_mode: active interrupt mode adopted.
5206 *
5207 * This routine it invoked to log the currently used active interrupt mode
5208 * to the device.
James Smart3772a992009-05-22 14:50:54 -04005209 **/
5210static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05005211{
5212 switch (intr_mode) {
5213 case 0:
5214 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5215 "0470 Enable INTx interrupt mode.\n");
5216 break;
5217 case 1:
5218 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5219 "0481 Enabled MSI interrupt mode.\n");
5220 break;
5221 case 2:
5222 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5223 "0480 Enabled MSI-X interrupt mode.\n");
5224 break;
5225 default:
5226 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5227 "0482 Illegal interrupt mode.\n");
5228 break;
5229 }
5230 return;
5231}
5232
James Smart5b75da22008-12-04 22:39:35 -05005233/**
James Smart3772a992009-05-22 14:50:54 -04005234 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05005235 * @phba: pointer to lpfc hba data structure.
5236 *
James Smart3772a992009-05-22 14:50:54 -04005237 * This routine is invoked to enable the PCI device that is common to all
5238 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05005239 *
5240 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005241 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005242 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05005243 **/
James Smart3772a992009-05-22 14:50:54 -04005244static int
5245lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05005246{
James Smart3772a992009-05-22 14:50:54 -04005247 struct pci_dev *pdev;
James Smart5b75da22008-12-04 22:39:35 -05005248
James Smart3772a992009-05-22 14:50:54 -04005249 /* Obtain PCI device reference */
5250 if (!phba->pcidev)
5251 goto out_error;
5252 else
5253 pdev = phba->pcidev;
James Smart3772a992009-05-22 14:50:54 -04005254 /* Enable PCI device */
5255 if (pci_enable_device_mem(pdev))
5256 goto out_error;
5257 /* Request PCI resource for the device */
Johannes Thumshirne0c04832016-06-07 09:44:03 +02005258 if (pci_request_mem_regions(pdev, LPFC_DRIVER_NAME))
James Smart3772a992009-05-22 14:50:54 -04005259 goto out_disable_device;
5260 /* Set up device as PCI master and save state for EEH */
5261 pci_set_master(pdev);
5262 pci_try_set_mwi(pdev);
5263 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05005264
James Smart05580562011-05-24 11:40:48 -04005265 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
Jon Mason453193e2013-09-11 15:38:13 -07005266 if (pci_is_pcie(pdev))
James Smart05580562011-05-24 11:40:48 -04005267 pdev->needs_freset = 1;
5268
James Smart3772a992009-05-22 14:50:54 -04005269 return 0;
James Smart5b75da22008-12-04 22:39:35 -05005270
James Smart3772a992009-05-22 14:50:54 -04005271out_disable_device:
5272 pci_disable_device(pdev);
5273out_error:
James Smart079b5c92011-08-21 21:48:49 -04005274 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
Johannes Thumshirne0c04832016-06-07 09:44:03 +02005275 "1401 Failed to enable pci device\n");
James Smart3772a992009-05-22 14:50:54 -04005276 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05005277}
5278
5279/**
James Smart3772a992009-05-22 14:50:54 -04005280 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05005281 * @phba: pointer to lpfc hba data structure.
5282 *
James Smart3772a992009-05-22 14:50:54 -04005283 * This routine is invoked to disable the PCI device that is common to all
5284 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05005285 **/
5286static void
James Smart3772a992009-05-22 14:50:54 -04005287lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05005288{
James Smart3772a992009-05-22 14:50:54 -04005289 struct pci_dev *pdev;
James Smart5b75da22008-12-04 22:39:35 -05005290
James Smart3772a992009-05-22 14:50:54 -04005291 /* Obtain PCI device reference */
5292 if (!phba->pcidev)
5293 return;
5294 else
5295 pdev = phba->pcidev;
James Smart3772a992009-05-22 14:50:54 -04005296 /* Release PCI resource and disable PCI device */
Johannes Thumshirne0c04832016-06-07 09:44:03 +02005297 pci_release_mem_regions(pdev);
James Smart3772a992009-05-22 14:50:54 -04005298 pci_disable_device(pdev);
James Smart5b75da22008-12-04 22:39:35 -05005299
5300 return;
5301}
5302
5303/**
James Smart3772a992009-05-22 14:50:54 -04005304 * lpfc_reset_hba - Reset a hba
5305 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04005306 *
James Smart3772a992009-05-22 14:50:54 -04005307 * This routine is invoked to reset a hba device. It brings the HBA
5308 * offline, performs a board restart, and then brings the board back
5309 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
5310 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04005311 **/
James Smart3772a992009-05-22 14:50:54 -04005312void
5313lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05005314{
James Smart3772a992009-05-22 14:50:54 -04005315 /* If resets are disabled then set error state and return. */
5316 if (!phba->cfg_enable_hba_reset) {
5317 phba->link_state = LPFC_HBA_ERROR;
5318 return;
5319 }
James Smartee620212014-04-04 13:51:53 -04005320 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
5321 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
5322 else
5323 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart3772a992009-05-22 14:50:54 -04005324 lpfc_offline(phba);
5325 lpfc_sli_brdrestart(phba);
5326 lpfc_online(phba);
5327 lpfc_unblock_mgmt_io(phba);
5328}
dea31012005-04-17 16:05:31 -05005329
James Smart3772a992009-05-22 14:50:54 -04005330/**
James Smart0a96e972011-07-22 18:37:28 -04005331 * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
5332 * @phba: pointer to lpfc hba data structure.
5333 *
5334 * This function enables the PCI SR-IOV virtual functions to a physical
5335 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
5336 * enable the number of virtual functions to the physical function. As
5337 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
5338 * API call does not considered as an error condition for most of the device.
5339 **/
5340uint16_t
5341lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
5342{
5343 struct pci_dev *pdev = phba->pcidev;
5344 uint16_t nr_virtfn;
5345 int pos;
5346
James Smart0a96e972011-07-22 18:37:28 -04005347 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
5348 if (pos == 0)
5349 return 0;
5350
5351 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
5352 return nr_virtfn;
5353}
5354
5355/**
James Smart912e3ac2011-05-24 11:42:11 -04005356 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
5357 * @phba: pointer to lpfc hba data structure.
5358 * @nr_vfn: number of virtual functions to be enabled.
5359 *
5360 * This function enables the PCI SR-IOV virtual functions to a physical
5361 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
5362 * enable the number of virtual functions to the physical function. As
5363 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
5364 * API call does not considered as an error condition for most of the device.
5365 **/
5366int
5367lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
5368{
5369 struct pci_dev *pdev = phba->pcidev;
James Smart0a96e972011-07-22 18:37:28 -04005370 uint16_t max_nr_vfn;
James Smart912e3ac2011-05-24 11:42:11 -04005371 int rc;
5372
James Smart0a96e972011-07-22 18:37:28 -04005373 max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
5374 if (nr_vfn > max_nr_vfn) {
5375 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5376 "3057 Requested vfs (%d) greater than "
5377 "supported vfs (%d)", nr_vfn, max_nr_vfn);
5378 return -EINVAL;
5379 }
5380
James Smart912e3ac2011-05-24 11:42:11 -04005381 rc = pci_enable_sriov(pdev, nr_vfn);
5382 if (rc) {
5383 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5384 "2806 Failed to enable sriov on this device "
5385 "with vfn number nr_vf:%d, rc:%d\n",
5386 nr_vfn, rc);
5387 } else
5388 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5389 "2807 Successful enable sriov on this device "
5390 "with vfn number nr_vf:%d\n", nr_vfn);
5391 return rc;
5392}
5393
5394/**
James Smart895427b2017-02-12 13:52:30 -08005395 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
James Smart3772a992009-05-22 14:50:54 -04005396 * @phba: pointer to lpfc hba data structure.
5397 *
James Smart895427b2017-02-12 13:52:30 -08005398 * This routine is invoked to set up the driver internal resources before the
5399 * device specific resource setup to support the HBA device it attached to.
James Smart3772a992009-05-22 14:50:54 -04005400 *
5401 * Return codes
James Smart895427b2017-02-12 13:52:30 -08005402 * 0 - successful
5403 * other values - error
James Smart3772a992009-05-22 14:50:54 -04005404 **/
5405static int
James Smart895427b2017-02-12 13:52:30 -08005406lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
James Smart3772a992009-05-22 14:50:54 -04005407{
James Smart895427b2017-02-12 13:52:30 -08005408 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05005409
James Smart3772a992009-05-22 14:50:54 -04005410 /*
James Smart895427b2017-02-12 13:52:30 -08005411 * Driver resources common to all SLI revisions
James Smart3772a992009-05-22 14:50:54 -04005412 */
James Smart895427b2017-02-12 13:52:30 -08005413 atomic_set(&phba->fast_event_count, 0);
5414 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005415
James Smart895427b2017-02-12 13:52:30 -08005416 /* Initialize ndlp management spinlock */
5417 spin_lock_init(&phba->ndlp_lock);
James Smart3772a992009-05-22 14:50:54 -04005418
James Smart895427b2017-02-12 13:52:30 -08005419 INIT_LIST_HEAD(&phba->port_list);
5420 INIT_LIST_HEAD(&phba->work_list);
5421 init_waitqueue_head(&phba->wait_4_mlo_m_q);
5422
5423 /* Initialize the wait queue head for the kernel thread */
5424 init_waitqueue_head(&phba->work_waitq);
5425
5426 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smartf358dd02017-02-12 13:52:34 -08005427 "1403 Protocols supported %s %s %s\n",
James Smart895427b2017-02-12 13:52:30 -08005428 ((phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) ?
5429 "SCSI" : " "),
5430 ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) ?
James Smartf358dd02017-02-12 13:52:34 -08005431 "NVME" : " "),
5432 (phba->nvmet_support ? "NVMET" : " "));
James Smart895427b2017-02-12 13:52:30 -08005433
5434 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
5435 /* Initialize the scsi buffer list used by driver for scsi IO */
5436 spin_lock_init(&phba->scsi_buf_list_get_lock);
5437 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_get);
5438 spin_lock_init(&phba->scsi_buf_list_put_lock);
5439 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
5440 }
5441
5442 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
5443 (phba->nvmet_support == 0)) {
5444 /* Initialize the NVME buffer list used by driver for NVME IO */
5445 spin_lock_init(&phba->nvme_buf_list_get_lock);
5446 INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_get);
5447 spin_lock_init(&phba->nvme_buf_list_put_lock);
5448 INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_put);
5449 }
5450
5451 /* Initialize the fabric iocb list */
5452 INIT_LIST_HEAD(&phba->fabric_iocb_list);
5453
5454 /* Initialize list to save ELS buffers */
5455 INIT_LIST_HEAD(&phba->elsbuf);
5456
5457 /* Initialize FCF connection rec list */
5458 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
5459
5460 /* Initialize OAS configuration list */
5461 spin_lock_init(&phba->devicelock);
5462 INIT_LIST_HEAD(&phba->luns);
5463
James Smart3772a992009-05-22 14:50:54 -04005464 /* MBOX heartbeat timer */
Tomas Jasek33cc5592017-03-03 13:45:48 +01005465 setup_timer(&psli->mbox_tmo, lpfc_mbox_timeout, (unsigned long)phba);
James Smart3772a992009-05-22 14:50:54 -04005466 /* Fabric block timer */
Tomas Jasek33cc5592017-03-03 13:45:48 +01005467 setup_timer(&phba->fabric_block_timer, lpfc_fabric_block_timeout,
5468 (unsigned long)phba);
James Smart3772a992009-05-22 14:50:54 -04005469 /* EA polling mode timer */
Tomas Jasek33cc5592017-03-03 13:45:48 +01005470 setup_timer(&phba->eratt_poll, lpfc_poll_eratt,
5471 (unsigned long)phba);
James Smart895427b2017-02-12 13:52:30 -08005472 /* Heartbeat timer */
Tomas Jasek33cc5592017-03-03 13:45:48 +01005473 setup_timer(&phba->hb_tmofunc, lpfc_hb_timeout, (unsigned long)phba);
James Smart895427b2017-02-12 13:52:30 -08005474
5475 return 0;
5476}
5477
5478/**
5479 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev
5480 * @phba: pointer to lpfc hba data structure.
5481 *
5482 * This routine is invoked to set up the driver internal resources specific to
5483 * support the SLI-3 HBA device it attached to.
5484 *
5485 * Return codes
5486 * 0 - successful
5487 * other values - error
5488 **/
5489static int
5490lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
5491{
5492 int rc;
5493
5494 /*
5495 * Initialize timers used by driver
5496 */
5497
5498 /* FCP polling mode timer */
Tomas Jasek33cc5592017-03-03 13:45:48 +01005499 setup_timer(&phba->fcp_poll_timer, lpfc_poll_timeout,
5500 (unsigned long)phba);
James Smart3772a992009-05-22 14:50:54 -04005501
5502 /* Host attention work mask setup */
5503 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
5504 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
5505
5506 /* Get all the module params for configuring this host */
5507 lpfc_get_cfgparam(phba);
James Smart895427b2017-02-12 13:52:30 -08005508 /* Set up phase-1 common device driver resources */
5509
5510 rc = lpfc_setup_driver_resource_phase1(phba);
5511 if (rc)
5512 return -ENODEV;
5513
James Smart49198b32010-04-06 15:04:33 -04005514 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
5515 phba->menlo_flag |= HBA_MENLO_SUPPORT;
5516 /* check for menlo minimum sg count */
5517 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
5518 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
5519 }
5520
James Smart895427b2017-02-12 13:52:30 -08005521 if (!phba->sli.sli3_ring)
5522 phba->sli.sli3_ring = kzalloc(LPFC_SLI3_MAX_RING *
James Smart2a76a282012-08-03 12:35:54 -04005523 sizeof(struct lpfc_sli_ring), GFP_KERNEL);
James Smart895427b2017-02-12 13:52:30 -08005524 if (!phba->sli.sli3_ring)
James Smart2a76a282012-08-03 12:35:54 -04005525 return -ENOMEM;
5526
James Smart3772a992009-05-22 14:50:54 -04005527 /*
James Smart96f70772013-04-17 20:16:15 -04005528 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smart3772a992009-05-22 14:50:54 -04005529 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smart3772a992009-05-22 14:50:54 -04005530 */
James Smart3772a992009-05-22 14:50:54 -04005531
James Smart96f70772013-04-17 20:16:15 -04005532 /* Initialize the host templates the configured values. */
James Smart3772a992009-05-22 14:50:54 -04005533 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
James Smart96418b52017-03-04 09:30:31 -08005534 lpfc_template_no_hr.sg_tablesize = phba->cfg_sg_seg_cnt;
5535 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
James Smart3772a992009-05-22 14:50:54 -04005536
James Smart96f70772013-04-17 20:16:15 -04005537 /* There are going to be 2 reserved BDEs: 1 FCP cmnd + 1 FCP rsp */
5538 if (phba->cfg_enable_bg) {
5539 /*
5540 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
5541 * the FCP rsp, and a BDE for each. Sice we have no control
5542 * over how many protection data segments the SCSI Layer
5543 * will hand us (ie: there could be one for every block
5544 * in the IO), we just allocate enough BDEs to accomidate
5545 * our max amount and we need to limit lpfc_sg_seg_cnt to
5546 * minimize the risk of running out.
5547 */
5548 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5549 sizeof(struct fcp_rsp) +
5550 (LPFC_MAX_SG_SEG_CNT * sizeof(struct ulp_bde64));
5551
5552 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SEG_CNT_DIF)
5553 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT_DIF;
5554
5555 /* Total BDEs in BPL for scsi_sg_list and scsi_sg_prot_list */
5556 phba->cfg_total_seg_cnt = LPFC_MAX_SG_SEG_CNT;
5557 } else {
5558 /*
5559 * The scsi_buf for a regular I/O will hold the FCP cmnd,
5560 * the FCP rsp, a BDE for each, and a BDE for up to
5561 * cfg_sg_seg_cnt data segments.
5562 */
5563 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5564 sizeof(struct fcp_rsp) +
5565 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
5566
5567 /* Total BDEs in BPL for scsi_sg_list */
5568 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
5569 }
5570
5571 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
5572 "9088 sg_tablesize:%d dmabuf_size:%d total_bde:%d\n",
5573 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
5574 phba->cfg_total_seg_cnt);
5575
James Smart3772a992009-05-22 14:50:54 -04005576 phba->max_vpi = LPFC_MAX_VPI;
5577 /* This will be set to correct value after config_port mbox */
5578 phba->max_vports = 0;
5579
5580 /*
5581 * Initialize the SLI Layer to run with lpfc HBAs.
5582 */
5583 lpfc_sli_setup(phba);
James Smart895427b2017-02-12 13:52:30 -08005584 lpfc_sli_queue_init(phba);
James Smart3772a992009-05-22 14:50:54 -04005585
5586 /* Allocate device driver memory */
5587 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
5588 return -ENOMEM;
5589
James Smart912e3ac2011-05-24 11:42:11 -04005590 /*
5591 * Enable sr-iov virtual functions if supported and configured
5592 * through the module parameter.
5593 */
5594 if (phba->cfg_sriov_nr_virtfn > 0) {
5595 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
5596 phba->cfg_sriov_nr_virtfn);
5597 if (rc) {
5598 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5599 "2808 Requested number of SR-IOV "
5600 "virtual functions (%d) is not "
5601 "supported\n",
5602 phba->cfg_sriov_nr_virtfn);
5603 phba->cfg_sriov_nr_virtfn = 0;
5604 }
5605 }
5606
James Smart3772a992009-05-22 14:50:54 -04005607 return 0;
5608}
5609
5610/**
5611 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
5612 * @phba: pointer to lpfc hba data structure.
5613 *
5614 * This routine is invoked to unset the driver internal resources set up
5615 * specific for supporting the SLI-3 HBA device it attached to.
5616 **/
5617static void
5618lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
5619{
5620 /* Free device driver memory allocated */
5621 lpfc_mem_free_all(phba);
5622
5623 return;
5624}
5625
5626/**
James Smartda0436e2009-05-22 14:51:39 -04005627 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
5628 * @phba: pointer to lpfc hba data structure.
5629 *
5630 * This routine is invoked to set up the driver internal resources specific to
5631 * support the SLI-4 HBA device it attached to.
5632 *
5633 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005634 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005635 * other values - error
5636 **/
5637static int
5638lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
5639{
James Smart28baac72010-02-12 14:42:03 -05005640 LPFC_MBOXQ_t *mboxq;
James Smartf358dd02017-02-12 13:52:34 -08005641 MAILBOX_t *mb;
James Smart895427b2017-02-12 13:52:30 -08005642 int rc, i, max_buf_size;
James Smart28baac72010-02-12 14:42:03 -05005643 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
5644 struct lpfc_mqe *mqe;
James Smart09294d42013-04-17 20:16:05 -04005645 int longs;
James Smart1ba981f2014-02-20 09:56:45 -05005646 int fof_vectors = 0;
James Smartf358dd02017-02-12 13:52:34 -08005647 uint64_t wwn;
James Smartda0436e2009-05-22 14:51:39 -04005648
James Smart895427b2017-02-12 13:52:30 -08005649 phba->sli4_hba.num_online_cpu = num_online_cpus();
5650 phba->sli4_hba.num_present_cpu = lpfc_present_cpu;
5651 phba->sli4_hba.curr_disp_cpu = 0;
5652
James Smart716d3bc2013-09-06 12:18:28 -04005653 /* Get all the module params for configuring this host */
5654 lpfc_get_cfgparam(phba);
5655
James Smart895427b2017-02-12 13:52:30 -08005656 /* Set up phase-1 common device driver resources */
5657 rc = lpfc_setup_driver_resource_phase1(phba);
5658 if (rc)
5659 return -ENODEV;
5660
James Smartda0436e2009-05-22 14:51:39 -04005661 /* Before proceed, wait for POST done and device ready */
5662 rc = lpfc_sli4_post_status_check(phba);
5663 if (rc)
5664 return -ENODEV;
5665
5666 /*
5667 * Initialize timers used by driver
5668 */
5669
Tomas Jasek33cc5592017-03-03 13:45:48 +01005670 setup_timer(&phba->rrq_tmr, lpfc_rrq_timeout, (unsigned long)phba);
James Smartda0436e2009-05-22 14:51:39 -04005671
James Smartecfd03c2010-02-12 14:41:27 -05005672 /* FCF rediscover timer */
Tomas Jasek33cc5592017-03-03 13:45:48 +01005673 setup_timer(&phba->fcf.redisc_wait, lpfc_sli4_fcf_redisc_wait_tmo,
5674 (unsigned long)phba);
James Smartecfd03c2010-02-12 14:41:27 -05005675
James Smartda0436e2009-05-22 14:51:39 -04005676 /*
James Smart7ad20aa2011-05-24 11:44:28 -04005677 * Control structure for handling external multi-buffer mailbox
5678 * command pass-through.
5679 */
5680 memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
5681 sizeof(struct lpfc_mbox_ext_buf_ctx));
5682 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
5683
James Smartda0436e2009-05-22 14:51:39 -04005684 phba->max_vpi = LPFC_MAX_VPI;
James Smart67d12732012-08-03 12:36:13 -04005685
James Smartda0436e2009-05-22 14:51:39 -04005686 /* This will be set to correct value after the read_config mbox */
5687 phba->max_vports = 0;
5688
5689 /* Program the default value of vlan_id and fc_map */
5690 phba->valid_vlan = 0;
5691 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5692 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5693 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5694
5695 /*
James Smart2a76a282012-08-03 12:35:54 -04005696 * For SLI4, instead of using ring 0 (LPFC_FCP_RING) for FCP commands
James Smart895427b2017-02-12 13:52:30 -08005697 * we will associate a new ring, for each EQ/CQ/WQ tuple.
5698 * The WQ create will allocate the ring.
James Smart2a76a282012-08-03 12:35:54 -04005699 */
James Smart085c6472010-11-20 23:11:37 -05005700
James Smart09294d42013-04-17 20:16:05 -04005701 /*
5702 * It doesn't matter what family our adapter is in, we are
5703 * limited to 2 Pages, 512 SGEs, for our SGL.
5704 * There are going to be 2 reserved SGEs: 1 FCP cmnd + 1 FCP rsp
5705 */
5706 max_buf_size = (2 * SLI4_PAGE_SIZE);
5707 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SGL_SEG_CNT - 2)
5708 phba->cfg_sg_seg_cnt = LPFC_MAX_SGL_SEG_CNT - 2;
James Smart09294d42013-04-17 20:16:05 -04005709
James Smartda0436e2009-05-22 14:51:39 -04005710 /*
James Smart895427b2017-02-12 13:52:30 -08005711 * Since lpfc_sg_seg_cnt is module param, the sg_dma_buf_size
5712 * used to create the sg_dma_buf_pool must be calculated.
James Smart28baac72010-02-12 14:42:03 -05005713 */
James Smart96f70772013-04-17 20:16:15 -04005714 if (phba->cfg_enable_bg) {
5715 /*
James Smart895427b2017-02-12 13:52:30 -08005716 * The scsi_buf for a T10-DIF I/O holds the FCP cmnd,
5717 * the FCP rsp, and a SGE. Sice we have no control
5718 * over how many protection segments the SCSI Layer
James Smart96f70772013-04-17 20:16:15 -04005719 * will hand us (ie: there could be one for every block
James Smart895427b2017-02-12 13:52:30 -08005720 * in the IO), just allocate enough SGEs to accomidate
5721 * our max amount and we need to limit lpfc_sg_seg_cnt
5722 * to minimize the risk of running out.
James Smart96f70772013-04-17 20:16:15 -04005723 */
5724 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
James Smart895427b2017-02-12 13:52:30 -08005725 sizeof(struct fcp_rsp) + max_buf_size;
James Smart96f70772013-04-17 20:16:15 -04005726
5727 /* Total SGEs for scsi_sg_list and scsi_sg_prot_list */
5728 phba->cfg_total_seg_cnt = LPFC_MAX_SGL_SEG_CNT;
5729
5730 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SLI4_SEG_CNT_DIF)
James Smart895427b2017-02-12 13:52:30 -08005731 phba->cfg_sg_seg_cnt =
5732 LPFC_MAX_SG_SLI4_SEG_CNT_DIF;
James Smart96f70772013-04-17 20:16:15 -04005733 } else {
5734 /*
James Smart895427b2017-02-12 13:52:30 -08005735 * The scsi_buf for a regular I/O holds the FCP cmnd,
James Smart96f70772013-04-17 20:16:15 -04005736 * the FCP rsp, a SGE for each, and a SGE for up to
5737 * cfg_sg_seg_cnt data segments.
5738 */
5739 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
James Smart895427b2017-02-12 13:52:30 -08005740 sizeof(struct fcp_rsp) +
5741 ((phba->cfg_sg_seg_cnt + 2) *
5742 sizeof(struct sli4_sge));
James Smart96f70772013-04-17 20:16:15 -04005743
5744 /* Total SGEs for scsi_sg_list */
5745 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
James Smart895427b2017-02-12 13:52:30 -08005746
James Smart96f70772013-04-17 20:16:15 -04005747 /*
James Smart895427b2017-02-12 13:52:30 -08005748 * NOTE: if (phba->cfg_sg_seg_cnt + 2) <= 256 we only
5749 * need to post 1 page for the SGL.
James Smart96f70772013-04-17 20:16:15 -04005750 */
James Smart085c6472010-11-20 23:11:37 -05005751 }
James Smartacd68592012-01-18 16:25:09 -05005752
James Smart96f70772013-04-17 20:16:15 -04005753 /* Initialize the host templates with the updated values. */
5754 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5755 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
James Smart96418b52017-03-04 09:30:31 -08005756 lpfc_template_no_hr.sg_tablesize = phba->cfg_sg_seg_cnt;
James Smart96f70772013-04-17 20:16:15 -04005757
5758 if (phba->cfg_sg_dma_buf_size <= LPFC_MIN_SG_SLI4_BUF_SZ)
5759 phba->cfg_sg_dma_buf_size = LPFC_MIN_SG_SLI4_BUF_SZ;
5760 else
5761 phba->cfg_sg_dma_buf_size =
5762 SLI4_PAGE_ALIGN(phba->cfg_sg_dma_buf_size);
5763
5764 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
5765 "9087 sg_tablesize:%d dmabuf_size:%d total_sge:%d\n",
5766 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
5767 phba->cfg_total_seg_cnt);
James Smartda0436e2009-05-22 14:51:39 -04005768
5769 /* Initialize buffer queue management fields */
James Smart895427b2017-02-12 13:52:30 -08005770 INIT_LIST_HEAD(&phba->hbqs[LPFC_ELS_HBQ].hbq_buffer_list);
James Smartda0436e2009-05-22 14:51:39 -04005771 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
5772 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
5773
5774 /*
5775 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
5776 */
James Smart895427b2017-02-12 13:52:30 -08005777 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
5778 /* Initialize the Abort scsi buffer list used by driver */
5779 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
5780 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
5781 }
5782
5783 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
5784 /* Initialize the Abort nvme buffer list used by driver */
5785 spin_lock_init(&phba->sli4_hba.abts_nvme_buf_list_lock);
5786 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvme_buf_list);
James Smart318083a2017-03-04 09:30:30 -08005787 /* Fast-path XRI aborted CQ Event work queue list */
5788 INIT_LIST_HEAD(&phba->sli4_hba.sp_nvme_xri_aborted_work_queue);
James Smart895427b2017-02-12 13:52:30 -08005789 }
5790
James Smartda0436e2009-05-22 14:51:39 -04005791 /* This abort list used by worker thread */
James Smart895427b2017-02-12 13:52:30 -08005792 spin_lock_init(&phba->sli4_hba.sgl_list_lock);
James Smartf358dd02017-02-12 13:52:34 -08005793 spin_lock_init(&phba->sli4_hba.nvmet_io_lock);
James Smartda0436e2009-05-22 14:51:39 -04005794
5795 /*
James Smart6d368e52011-05-24 11:44:12 -04005796 * Initialize driver internal slow-path work queues
James Smartda0436e2009-05-22 14:51:39 -04005797 */
5798
5799 /* Driver internel slow-path CQ Event pool */
5800 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
5801 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04005802 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04005803 /* Asynchronous event CQ Event work queue list */
5804 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
5805 /* Fast-path XRI aborted CQ Event work queue list */
5806 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
5807 /* Slow-path XRI aborted CQ Event work queue list */
5808 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
5809 /* Receive queue CQ Event work queue list */
5810 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
5811
James Smart6d368e52011-05-24 11:44:12 -04005812 /* Initialize extent block lists. */
5813 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
5814 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
5815 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
5816 INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
5817
James Smart448193b2015-12-16 18:12:05 -05005818 /* initialize optic_state to 0xFF */
5819 phba->sli4_hba.lnk_info.optic_state = 0xff;
5820
James Smartda0436e2009-05-22 14:51:39 -04005821 /* Allocate device driver memory */
5822 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
5823 if (rc)
5824 return -ENOMEM;
5825
James Smart2fcee4b2010-12-15 17:57:46 -05005826 /* IF Type 2 ports get initialized now. */
5827 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
5828 LPFC_SLI_INTF_IF_TYPE_2) {
5829 rc = lpfc_pci_function_reset(phba);
James Smart895427b2017-02-12 13:52:30 -08005830 if (unlikely(rc)) {
5831 rc = -ENODEV;
5832 goto out_free_mem;
5833 }
James Smart946727d2015-04-07 15:07:09 -04005834 phba->temp_sensor_support = 1;
James Smart2fcee4b2010-12-15 17:57:46 -05005835 }
5836
James Smartda0436e2009-05-22 14:51:39 -04005837 /* Create the bootstrap mailbox command */
5838 rc = lpfc_create_bootstrap_mbox(phba);
5839 if (unlikely(rc))
5840 goto out_free_mem;
5841
5842 /* Set up the host's endian order with the device. */
5843 rc = lpfc_setup_endian_order(phba);
5844 if (unlikely(rc))
5845 goto out_free_bsmbx;
5846
5847 /* Set up the hba's configuration parameters. */
5848 rc = lpfc_sli4_read_config(phba);
5849 if (unlikely(rc))
5850 goto out_free_bsmbx;
James Smartcff261f2013-12-17 20:29:47 -05005851 rc = lpfc_mem_alloc_active_rrq_pool_s4(phba);
5852 if (unlikely(rc))
5853 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04005854
James Smart2fcee4b2010-12-15 17:57:46 -05005855 /* IF Type 0 ports get initialized now. */
5856 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
5857 LPFC_SLI_INTF_IF_TYPE_0) {
5858 rc = lpfc_pci_function_reset(phba);
5859 if (unlikely(rc))
5860 goto out_free_bsmbx;
5861 }
James Smartda0436e2009-05-22 14:51:39 -04005862
James Smartcb5172e2010-03-15 11:25:07 -04005863 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
5864 GFP_KERNEL);
5865 if (!mboxq) {
5866 rc = -ENOMEM;
5867 goto out_free_bsmbx;
5868 }
5869
James Smartf358dd02017-02-12 13:52:34 -08005870 /* Check for NVMET being configured */
James Smart895427b2017-02-12 13:52:30 -08005871 phba->nvmet_support = 0;
James Smartf358dd02017-02-12 13:52:34 -08005872 if (lpfc_enable_nvmet_cnt) {
5873
5874 /* First get WWN of HBA instance */
5875 lpfc_read_nv(phba, mboxq);
5876 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5877 if (rc != MBX_SUCCESS) {
5878 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5879 "6016 Mailbox failed , mbxCmd x%x "
5880 "READ_NV, mbxStatus x%x\n",
5881 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5882 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
5883 rc = -EIO;
5884 goto out_free_bsmbx;
5885 }
5886 mb = &mboxq->u.mb;
5887 memcpy(&wwn, (char *)mb->un.varRDnvp.nodename,
5888 sizeof(uint64_t));
5889 wwn = cpu_to_be64(wwn);
5890 phba->sli4_hba.wwnn.u.name = wwn;
5891 memcpy(&wwn, (char *)mb->un.varRDnvp.portname,
5892 sizeof(uint64_t));
5893 /* wwn is WWPN of HBA instance */
5894 wwn = cpu_to_be64(wwn);
5895 phba->sli4_hba.wwpn.u.name = wwn;
5896
5897 /* Check to see if it matches any module parameter */
5898 for (i = 0; i < lpfc_enable_nvmet_cnt; i++) {
5899 if (wwn == lpfc_enable_nvmet[i]) {
James Smart7d708032017-03-08 14:36:01 -08005900#if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
James Smartf358dd02017-02-12 13:52:34 -08005901 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5902 "6017 NVME Target %016llx\n",
5903 wwn);
5904 phba->nvmet_support = 1; /* a match */
James Smart7d708032017-03-08 14:36:01 -08005905#else
5906 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5907 "6021 Can't enable NVME Target."
5908 " NVME_TARGET_FC infrastructure"
5909 " is not in kernel\n");
5910#endif
James Smartf358dd02017-02-12 13:52:34 -08005911 }
5912 }
5913 }
James Smart895427b2017-02-12 13:52:30 -08005914
5915 lpfc_nvme_mod_param_dep(phba);
5916
James Smartfedd3b72011-02-16 12:39:24 -05005917 /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
James Smartcb5172e2010-03-15 11:25:07 -04005918 lpfc_supported_pages(mboxq);
5919 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smartfedd3b72011-02-16 12:39:24 -05005920 if (!rc) {
5921 mqe = &mboxq->u.mqe;
5922 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
5923 LPFC_MAX_SUPPORTED_PAGES);
5924 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
5925 switch (pn_page[i]) {
5926 case LPFC_SLI4_PARAMETERS:
5927 phba->sli4_hba.pc_sli4_params.supported = 1;
5928 break;
5929 default:
5930 break;
5931 }
5932 }
5933 /* Read the port's SLI4 Parameters capabilities if supported. */
5934 if (phba->sli4_hba.pc_sli4_params.supported)
5935 rc = lpfc_pc_sli4_params_get(phba, mboxq);
5936 if (rc) {
5937 mempool_free(mboxq, phba->mbox_mem_pool);
5938 rc = -EIO;
5939 goto out_free_bsmbx;
James Smartcb5172e2010-03-15 11:25:07 -04005940 }
5941 }
James Smart65791f12016-07-06 12:35:56 -07005942
James Smartfedd3b72011-02-16 12:39:24 -05005943 /*
5944 * Get sli4 parameters that override parameters from Port capabilities.
James Smart6d368e52011-05-24 11:44:12 -04005945 * If this call fails, it isn't critical unless the SLI4 parameters come
5946 * back in conflict.
James Smartfedd3b72011-02-16 12:39:24 -05005947 */
James Smart6d368e52011-05-24 11:44:12 -04005948 rc = lpfc_get_sli4_parameters(phba, mboxq);
5949 if (rc) {
5950 if (phba->sli4_hba.extents_in_use &&
5951 phba->sli4_hba.rpi_hdrs_in_use) {
5952 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5953 "2999 Unsupported SLI4 Parameters "
5954 "Extents and RPI headers enabled.\n");
James Smart6d368e52011-05-24 11:44:12 -04005955 }
James Smart895427b2017-02-12 13:52:30 -08005956 mempool_free(mboxq, phba->mbox_mem_pool);
5957 goto out_free_bsmbx;
James Smart6d368e52011-05-24 11:44:12 -04005958 }
James Smart895427b2017-02-12 13:52:30 -08005959
James Smartcb5172e2010-03-15 11:25:07 -04005960 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1ba981f2014-02-20 09:56:45 -05005961
5962 /* Verify OAS is supported */
5963 lpfc_sli4_oas_verify(phba);
5964 if (phba->cfg_fof)
5965 fof_vectors = 1;
5966
James Smart5350d872011-10-10 21:33:49 -04005967 /* Verify all the SLI4 queues */
5968 rc = lpfc_sli4_queue_verify(phba);
James Smartda0436e2009-05-22 14:51:39 -04005969 if (rc)
5970 goto out_free_bsmbx;
5971
5972 /* Create driver internal CQE event pool */
5973 rc = lpfc_sli4_cq_event_pool_create(phba);
5974 if (rc)
James Smart5350d872011-10-10 21:33:49 -04005975 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04005976
James Smart8a9d2e82012-05-09 21:16:12 -04005977 /* Initialize sgl lists per host */
5978 lpfc_init_sgl_list(phba);
5979
5980 /* Allocate and initialize active sgl array */
James Smartda0436e2009-05-22 14:51:39 -04005981 rc = lpfc_init_active_sgl_array(phba);
5982 if (rc) {
5983 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5984 "1430 Failed to initialize sgl list.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04005985 goto out_destroy_cq_event_pool;
James Smartda0436e2009-05-22 14:51:39 -04005986 }
James Smartda0436e2009-05-22 14:51:39 -04005987 rc = lpfc_sli4_init_rpi_hdrs(phba);
5988 if (rc) {
5989 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5990 "1432 Failed to initialize rpi headers.\n");
5991 goto out_free_active_sgl;
5992 }
5993
James Smarta93ff372010-10-22 11:06:08 -04005994 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
James Smart0c9ab6f2010-02-26 14:15:57 -05005995 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
5996 phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
5997 GFP_KERNEL);
5998 if (!phba->fcf.fcf_rr_bmask) {
5999 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6000 "2759 Failed allocate memory for FCF round "
6001 "robin failover bmask\n");
James Smart05580562011-05-24 11:40:48 -04006002 rc = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -05006003 goto out_remove_rpi_hdrs;
6004 }
6005
James Smart895427b2017-02-12 13:52:30 -08006006 phba->sli4_hba.hba_eq_hdl = kcalloc(fof_vectors + phba->io_channel_irqs,
6007 sizeof(struct lpfc_hba_eq_hdl),
6008 GFP_KERNEL);
6009 if (!phba->sli4_hba.hba_eq_hdl) {
James Smart67d12732012-08-03 12:36:13 -04006010 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6011 "2572 Failed allocate memory for "
6012 "fast-path per-EQ handle array\n");
6013 rc = -ENOMEM;
6014 goto out_free_fcf_rr_bmask;
James Smartda0436e2009-05-22 14:51:39 -04006015 }
6016
James Smart895427b2017-02-12 13:52:30 -08006017 phba->sli4_hba.cpu_map = kcalloc(phba->sli4_hba.num_present_cpu,
6018 sizeof(struct lpfc_vector_map_info),
6019 GFP_KERNEL);
James Smart7bb03bb2013-04-17 20:19:16 -04006020 if (!phba->sli4_hba.cpu_map) {
6021 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6022 "3327 Failed allocate memory for msi-x "
6023 "interrupt vector mapping\n");
6024 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08006025 goto out_free_hba_eq_hdl;
James Smart7bb03bb2013-04-17 20:19:16 -04006026 }
James Smartb246de12013-05-31 17:03:07 -04006027 if (lpfc_used_cpu == NULL) {
James Smart895427b2017-02-12 13:52:30 -08006028 lpfc_used_cpu = kcalloc(lpfc_present_cpu, sizeof(uint16_t),
6029 GFP_KERNEL);
James Smartb246de12013-05-31 17:03:07 -04006030 if (!lpfc_used_cpu) {
6031 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6032 "3335 Failed allocate memory for msi-x "
6033 "interrupt vector mapping\n");
6034 kfree(phba->sli4_hba.cpu_map);
6035 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08006036 goto out_free_hba_eq_hdl;
James Smartb246de12013-05-31 17:03:07 -04006037 }
6038 for (i = 0; i < lpfc_present_cpu; i++)
6039 lpfc_used_cpu[i] = LPFC_VECTOR_MAP_EMPTY;
6040 }
6041
James Smart912e3ac2011-05-24 11:42:11 -04006042 /*
6043 * Enable sr-iov virtual functions if supported and configured
6044 * through the module parameter.
6045 */
6046 if (phba->cfg_sriov_nr_virtfn > 0) {
6047 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
6048 phba->cfg_sriov_nr_virtfn);
6049 if (rc) {
6050 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6051 "3020 Requested number of SR-IOV "
6052 "virtual functions (%d) is not "
6053 "supported\n",
6054 phba->cfg_sriov_nr_virtfn);
6055 phba->cfg_sriov_nr_virtfn = 0;
6056 }
6057 }
6058
James Smart5248a742011-07-22 18:37:06 -04006059 return 0;
James Smartda0436e2009-05-22 14:51:39 -04006060
James Smart895427b2017-02-12 13:52:30 -08006061out_free_hba_eq_hdl:
6062 kfree(phba->sli4_hba.hba_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05006063out_free_fcf_rr_bmask:
6064 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04006065out_remove_rpi_hdrs:
6066 lpfc_sli4_remove_rpi_hdrs(phba);
6067out_free_active_sgl:
6068 lpfc_free_active_sgl(phba);
James Smartda0436e2009-05-22 14:51:39 -04006069out_destroy_cq_event_pool:
6070 lpfc_sli4_cq_event_pool_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04006071out_free_bsmbx:
6072 lpfc_destroy_bootstrap_mbox(phba);
6073out_free_mem:
6074 lpfc_mem_free(phba);
6075 return rc;
6076}
6077
6078/**
6079 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
6080 * @phba: pointer to lpfc hba data structure.
6081 *
6082 * This routine is invoked to unset the driver internal resources set up
6083 * specific for supporting the SLI-4 HBA device it attached to.
6084 **/
6085static void
6086lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
6087{
6088 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6089
James Smart7bb03bb2013-04-17 20:19:16 -04006090 /* Free memory allocated for msi-x interrupt vector to CPU mapping */
6091 kfree(phba->sli4_hba.cpu_map);
6092 phba->sli4_hba.num_present_cpu = 0;
6093 phba->sli4_hba.num_online_cpu = 0;
James Smart76fd07a2014-02-20 09:57:18 -05006094 phba->sli4_hba.curr_disp_cpu = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04006095
James Smartda0436e2009-05-22 14:51:39 -04006096 /* Free memory allocated for fast-path work queue handles */
James Smart895427b2017-02-12 13:52:30 -08006097 kfree(phba->sli4_hba.hba_eq_hdl);
James Smartda0436e2009-05-22 14:51:39 -04006098
6099 /* Free the allocated rpi headers. */
6100 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04006101 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04006102
James Smart0c9ab6f2010-02-26 14:15:57 -05006103 /* Free eligible FCF index bmask */
6104 kfree(phba->fcf.fcf_rr_bmask);
6105
James Smartda0436e2009-05-22 14:51:39 -04006106 /* Free the ELS sgl list */
6107 lpfc_free_active_sgl(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04006108 lpfc_free_els_sgl_list(phba);
James Smartf358dd02017-02-12 13:52:34 -08006109 lpfc_free_nvmet_sgl_list(phba);
James Smartda0436e2009-05-22 14:51:39 -04006110
James Smartda0436e2009-05-22 14:51:39 -04006111 /* Free the completion queue EQ event pool */
6112 lpfc_sli4_cq_event_release_all(phba);
6113 lpfc_sli4_cq_event_pool_destroy(phba);
6114
James Smart6d368e52011-05-24 11:44:12 -04006115 /* Release resource identifiers. */
6116 lpfc_sli4_dealloc_resource_identifiers(phba);
6117
James Smartda0436e2009-05-22 14:51:39 -04006118 /* Free the bsmbx region. */
6119 lpfc_destroy_bootstrap_mbox(phba);
6120
6121 /* Free the SLI Layer memory with SLI4 HBAs */
6122 lpfc_mem_free_all(phba);
6123
6124 /* Free the current connect table */
6125 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04006126 &phba->fcf_conn_rec_list, list) {
6127 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04006128 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04006129 }
James Smartda0436e2009-05-22 14:51:39 -04006130
6131 return;
6132}
6133
6134/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006135 * lpfc_init_api_table_setup - Set up init api function jump table
James Smart3772a992009-05-22 14:50:54 -04006136 * @phba: The hba struct for which this call is being executed.
6137 * @dev_grp: The HBA PCI-Device group number.
6138 *
6139 * This routine sets up the device INIT interface API function jump table
6140 * in @phba struct.
6141 *
6142 * Returns: 0 - success, -ENODEV - failure.
6143 **/
6144int
6145lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6146{
James Smart84d1b002010-02-12 14:42:33 -05006147 phba->lpfc_hba_init_link = lpfc_hba_init_link;
6148 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart7f860592011-03-11 16:05:52 -05006149 phba->lpfc_selective_reset = lpfc_selective_reset;
James Smart3772a992009-05-22 14:50:54 -04006150 switch (dev_grp) {
6151 case LPFC_PCI_DEV_LP:
6152 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
6153 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
6154 phba->lpfc_stop_port = lpfc_stop_port_s3;
6155 break;
James Smartda0436e2009-05-22 14:51:39 -04006156 case LPFC_PCI_DEV_OC:
6157 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
6158 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
6159 phba->lpfc_stop_port = lpfc_stop_port_s4;
6160 break;
James Smart3772a992009-05-22 14:50:54 -04006161 default:
6162 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6163 "1431 Invalid HBA PCI-device group: 0x%x\n",
6164 dev_grp);
6165 return -ENODEV;
6166 break;
6167 }
6168 return 0;
6169}
6170
6171/**
James Smart3772a992009-05-22 14:50:54 -04006172 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
6173 * @phba: pointer to lpfc hba data structure.
6174 *
6175 * This routine is invoked to set up the driver internal resources after the
6176 * device specific resource setup to support the HBA device it attached to.
6177 *
6178 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006179 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006180 * other values - error
6181 **/
6182static int
6183lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
6184{
6185 int error;
6186
6187 /* Startup the kernel thread for this host adapter. */
6188 phba->worker_thread = kthread_run(lpfc_do_work, phba,
6189 "lpfc_worker_%d", phba->brd_no);
6190 if (IS_ERR(phba->worker_thread)) {
6191 error = PTR_ERR(phba->worker_thread);
6192 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05006193 }
6194
James Smart3772a992009-05-22 14:50:54 -04006195 return 0;
6196}
6197
6198/**
6199 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
6200 * @phba: pointer to lpfc hba data structure.
6201 *
6202 * This routine is invoked to unset the driver internal resources set up after
6203 * the device specific resource setup for supporting the HBA device it
6204 * attached to.
6205 **/
6206static void
6207lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
6208{
6209 /* Stop kernel worker thread */
6210 kthread_stop(phba->worker_thread);
6211}
6212
6213/**
6214 * lpfc_free_iocb_list - Free iocb list.
6215 * @phba: pointer to lpfc hba data structure.
6216 *
6217 * This routine is invoked to free the driver's IOCB list and memory.
6218 **/
6219static void
6220lpfc_free_iocb_list(struct lpfc_hba *phba)
6221{
6222 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
6223
6224 spin_lock_irq(&phba->hbalock);
6225 list_for_each_entry_safe(iocbq_entry, iocbq_next,
6226 &phba->lpfc_iocb_list, list) {
6227 list_del(&iocbq_entry->list);
6228 kfree(iocbq_entry);
6229 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05006230 }
James Smart3772a992009-05-22 14:50:54 -04006231 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006232
James Smart3772a992009-05-22 14:50:54 -04006233 return;
6234}
dea31012005-04-17 16:05:31 -05006235
James Smart3772a992009-05-22 14:50:54 -04006236/**
6237 * lpfc_init_iocb_list - Allocate and initialize iocb list.
6238 * @phba: pointer to lpfc hba data structure.
6239 *
6240 * This routine is invoked to allocate and initizlize the driver's IOCB
6241 * list and set up the IOCB tag array accordingly.
6242 *
6243 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006244 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006245 * other values - error
6246 **/
6247static int
6248lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
6249{
6250 struct lpfc_iocbq *iocbq_entry = NULL;
6251 uint16_t iotag;
6252 int i;
dea31012005-04-17 16:05:31 -05006253
6254 /* Initialize and populate the iocb list per host. */
6255 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04006256 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07006257 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05006258 if (iocbq_entry == NULL) {
6259 printk(KERN_ERR "%s: only allocated %d iocbs of "
6260 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07006261 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05006262 goto out_free_iocbq;
6263 }
6264
James Bottomley604a3e32005-10-29 10:28:33 -05006265 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
6266 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04006267 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05006268 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04006269 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05006270 goto out_free_iocbq;
6271 }
James Smart6d368e52011-05-24 11:44:12 -04006272 iocbq_entry->sli4_lxritag = NO_XRI;
James Smart3772a992009-05-22 14:50:54 -04006273 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05006274
6275 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006276 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
6277 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05006278 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006279 }
6280
James Smart3772a992009-05-22 14:50:54 -04006281 return 0;
6282
6283out_free_iocbq:
6284 lpfc_free_iocb_list(phba);
6285
6286 return -ENOMEM;
6287}
6288
6289/**
James Smart8a9d2e82012-05-09 21:16:12 -04006290 * lpfc_free_sgl_list - Free a given sgl list.
James Smartda0436e2009-05-22 14:51:39 -04006291 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -04006292 * @sglq_list: pointer to the head of sgl list.
James Smartda0436e2009-05-22 14:51:39 -04006293 *
James Smart8a9d2e82012-05-09 21:16:12 -04006294 * This routine is invoked to free a give sgl list and memory.
James Smartda0436e2009-05-22 14:51:39 -04006295 **/
James Smart8a9d2e82012-05-09 21:16:12 -04006296void
6297lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
James Smartda0436e2009-05-22 14:51:39 -04006298{
6299 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart8a9d2e82012-05-09 21:16:12 -04006300
6301 list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
6302 list_del(&sglq_entry->list);
6303 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
6304 kfree(sglq_entry);
6305 }
6306}
6307
6308/**
6309 * lpfc_free_els_sgl_list - Free els sgl list.
6310 * @phba: pointer to lpfc hba data structure.
6311 *
6312 * This routine is invoked to free the driver's els sgl list and memory.
6313 **/
6314static void
6315lpfc_free_els_sgl_list(struct lpfc_hba *phba)
6316{
James Smartda0436e2009-05-22 14:51:39 -04006317 LIST_HEAD(sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04006318
James Smart8a9d2e82012-05-09 21:16:12 -04006319 /* Retrieve all els sgls from driver list */
James Smartda0436e2009-05-22 14:51:39 -04006320 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006321 spin_lock(&phba->sli4_hba.sgl_list_lock);
6322 list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list, &sglq_list);
6323 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04006324 spin_unlock_irq(&phba->hbalock);
6325
James Smart8a9d2e82012-05-09 21:16:12 -04006326 /* Now free the sgl list */
6327 lpfc_free_sgl_list(phba, &sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04006328}
6329
6330/**
James Smartf358dd02017-02-12 13:52:34 -08006331 * lpfc_free_nvmet_sgl_list - Free nvmet sgl list.
6332 * @phba: pointer to lpfc hba data structure.
6333 *
6334 * This routine is invoked to free the driver's nvmet sgl list and memory.
6335 **/
6336static void
6337lpfc_free_nvmet_sgl_list(struct lpfc_hba *phba)
6338{
6339 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
6340 LIST_HEAD(sglq_list);
6341
6342 /* Retrieve all nvmet sgls from driver list */
6343 spin_lock_irq(&phba->hbalock);
6344 spin_lock(&phba->sli4_hba.sgl_list_lock);
6345 list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list, &sglq_list);
6346 spin_unlock(&phba->sli4_hba.sgl_list_lock);
6347 spin_unlock_irq(&phba->hbalock);
6348
6349 /* Now free the sgl list */
6350 list_for_each_entry_safe(sglq_entry, sglq_next, &sglq_list, list) {
6351 list_del(&sglq_entry->list);
6352 lpfc_nvmet_buf_free(phba, sglq_entry->virt, sglq_entry->phys);
6353 kfree(sglq_entry);
6354 }
6355}
6356
6357/**
James Smartda0436e2009-05-22 14:51:39 -04006358 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
6359 * @phba: pointer to lpfc hba data structure.
6360 *
6361 * This routine is invoked to allocate the driver's active sgl memory.
6362 * This array will hold the sglq_entry's for active IOs.
6363 **/
6364static int
6365lpfc_init_active_sgl_array(struct lpfc_hba *phba)
6366{
6367 int size;
6368 size = sizeof(struct lpfc_sglq *);
6369 size *= phba->sli4_hba.max_cfg_param.max_xri;
6370
6371 phba->sli4_hba.lpfc_sglq_active_list =
6372 kzalloc(size, GFP_KERNEL);
6373 if (!phba->sli4_hba.lpfc_sglq_active_list)
6374 return -ENOMEM;
6375 return 0;
6376}
6377
6378/**
6379 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
6380 * @phba: pointer to lpfc hba data structure.
6381 *
6382 * This routine is invoked to walk through the array of active sglq entries
6383 * and free all of the resources.
6384 * This is just a place holder for now.
6385 **/
6386static void
6387lpfc_free_active_sgl(struct lpfc_hba *phba)
6388{
6389 kfree(phba->sli4_hba.lpfc_sglq_active_list);
6390}
6391
6392/**
6393 * lpfc_init_sgl_list - Allocate and initialize sgl list.
6394 * @phba: pointer to lpfc hba data structure.
6395 *
6396 * This routine is invoked to allocate and initizlize the driver's sgl
6397 * list and set up the sgl xritag tag array accordingly.
6398 *
James Smartda0436e2009-05-22 14:51:39 -04006399 **/
James Smart8a9d2e82012-05-09 21:16:12 -04006400static void
James Smartda0436e2009-05-22 14:51:39 -04006401lpfc_init_sgl_list(struct lpfc_hba *phba)
6402{
James Smartda0436e2009-05-22 14:51:39 -04006403 /* Initialize and populate the sglq list per host/VF. */
James Smart895427b2017-02-12 13:52:30 -08006404 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_els_sgl_list);
James Smartda0436e2009-05-22 14:51:39 -04006405 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smartf358dd02017-02-12 13:52:34 -08006406 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_sgl_list);
6407 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_sgl_list);
James Smartda0436e2009-05-22 14:51:39 -04006408
James Smart8a9d2e82012-05-09 21:16:12 -04006409 /* els xri-sgl book keeping */
6410 phba->sli4_hba.els_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04006411
James Smart8a9d2e82012-05-09 21:16:12 -04006412 /* scsi xri-buffer book keeping */
James Smartda0436e2009-05-22 14:51:39 -04006413 phba->sli4_hba.scsi_xri_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -08006414
6415 /* nvme xri-buffer book keeping */
6416 phba->sli4_hba.nvme_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04006417}
6418
6419/**
6420 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
6421 * @phba: pointer to lpfc hba data structure.
6422 *
6423 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -04006424 * port for those SLI4 ports that do not support extents. This routine
James Smartda0436e2009-05-22 14:51:39 -04006425 * posts a PAGE_SIZE memory region to the port to hold up to
James Smart88a2cfb2011-07-22 18:36:33 -04006426 * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
6427 * and should be called only when interrupts are disabled.
James Smartda0436e2009-05-22 14:51:39 -04006428 *
6429 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006430 * 0 - successful
James Smart88a2cfb2011-07-22 18:36:33 -04006431 * -ERROR - otherwise.
James Smartda0436e2009-05-22 14:51:39 -04006432 **/
6433int
6434lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
6435{
6436 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04006437 struct lpfc_rpi_hdr *rpi_hdr;
6438
6439 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
James Smartff78d8f2011-12-13 13:21:35 -05006440 if (!phba->sli4_hba.rpi_hdrs_in_use)
James Smart6d368e52011-05-24 11:44:12 -04006441 return rc;
James Smart6d368e52011-05-24 11:44:12 -04006442 if (phba->sli4_hba.extents_in_use)
6443 return -EIO;
James Smartda0436e2009-05-22 14:51:39 -04006444
6445 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
6446 if (!rpi_hdr) {
6447 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6448 "0391 Error during rpi post operation\n");
6449 lpfc_sli4_remove_rpis(phba);
6450 rc = -ENODEV;
6451 }
6452
6453 return rc;
6454}
6455
6456/**
6457 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
6458 * @phba: pointer to lpfc hba data structure.
6459 *
6460 * This routine is invoked to allocate a single 4KB memory region to
6461 * support rpis and stores them in the phba. This single region
6462 * provides support for up to 64 rpis. The region is used globally
6463 * by the device.
6464 *
6465 * Returns:
6466 * A valid rpi hdr on success.
6467 * A NULL pointer on any failure.
6468 **/
6469struct lpfc_rpi_hdr *
6470lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
6471{
6472 uint16_t rpi_limit, curr_rpi_range;
6473 struct lpfc_dmabuf *dmabuf;
6474 struct lpfc_rpi_hdr *rpi_hdr;
James Smart9589b0622011-04-16 11:03:17 -04006475 uint32_t rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04006476
James Smart6d368e52011-05-24 11:44:12 -04006477 /*
6478 * If the SLI4 port supports extents, posting the rpi header isn't
6479 * required. Set the expected maximum count and let the actual value
6480 * get set when extents are fully allocated.
6481 */
6482 if (!phba->sli4_hba.rpi_hdrs_in_use)
6483 return NULL;
6484 if (phba->sli4_hba.extents_in_use)
6485 return NULL;
6486
6487 /* The limit on the logical index is just the max_rpi count. */
James Smartda0436e2009-05-22 14:51:39 -04006488 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
James Smart6d368e52011-05-24 11:44:12 -04006489 phba->sli4_hba.max_cfg_param.max_rpi - 1;
James Smartda0436e2009-05-22 14:51:39 -04006490
6491 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04006492 /*
6493 * Establish the starting RPI in this header block. The starting
6494 * rpi is normalized to a zero base because the physical rpi is
6495 * port based.
6496 */
James Smart97f2ecf2012-03-01 22:35:23 -05006497 curr_rpi_range = phba->sli4_hba.next_rpi;
James Smartda0436e2009-05-22 14:51:39 -04006498 spin_unlock_irq(&phba->hbalock);
6499
6500 /*
6501 * The port has a limited number of rpis. The increment here
6502 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
6503 * and to allow the full max_rpi range per port.
6504 */
6505 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
James Smart9589b0622011-04-16 11:03:17 -04006506 rpi_count = rpi_limit - curr_rpi_range;
6507 else
6508 rpi_count = LPFC_RPI_HDR_COUNT;
James Smartda0436e2009-05-22 14:51:39 -04006509
James Smart6d368e52011-05-24 11:44:12 -04006510 if (!rpi_count)
6511 return NULL;
James Smartda0436e2009-05-22 14:51:39 -04006512 /*
6513 * First allocate the protocol header region for the port. The
6514 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
6515 */
6516 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
6517 if (!dmabuf)
6518 return NULL;
6519
Joe Perches1aee3832014-09-03 12:56:12 -04006520 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
6521 LPFC_HDR_TEMPLATE_SIZE,
6522 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04006523 if (!dmabuf->virt) {
6524 rpi_hdr = NULL;
6525 goto err_free_dmabuf;
6526 }
6527
James Smartda0436e2009-05-22 14:51:39 -04006528 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
6529 rpi_hdr = NULL;
6530 goto err_free_coherent;
6531 }
6532
6533 /* Save the rpi header data for cleanup later. */
6534 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
6535 if (!rpi_hdr)
6536 goto err_free_coherent;
6537
6538 rpi_hdr->dmabuf = dmabuf;
6539 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
6540 rpi_hdr->page_count = 1;
6541 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04006542
6543 /* The rpi_hdr stores the logical index only. */
6544 rpi_hdr->start_rpi = curr_rpi_range;
James Smartda0436e2009-05-22 14:51:39 -04006545 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
6546
6547 /*
James Smart6d368e52011-05-24 11:44:12 -04006548 * The next_rpi stores the next logical module-64 rpi value used
6549 * to post physical rpis in subsequent rpi postings.
James Smartda0436e2009-05-22 14:51:39 -04006550 */
James Smart9589b0622011-04-16 11:03:17 -04006551 phba->sli4_hba.next_rpi += rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04006552 spin_unlock_irq(&phba->hbalock);
6553 return rpi_hdr;
6554
6555 err_free_coherent:
6556 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
6557 dmabuf->virt, dmabuf->phys);
6558 err_free_dmabuf:
6559 kfree(dmabuf);
6560 return NULL;
6561}
6562
6563/**
6564 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
6565 * @phba: pointer to lpfc hba data structure.
6566 *
6567 * This routine is invoked to remove all memory resources allocated
James Smart6d368e52011-05-24 11:44:12 -04006568 * to support rpis for SLI4 ports not supporting extents. This routine
6569 * presumes the caller has released all rpis consumed by fabric or port
6570 * logins and is prepared to have the header pages removed.
James Smartda0436e2009-05-22 14:51:39 -04006571 **/
6572void
6573lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
6574{
6575 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
6576
James Smart6d368e52011-05-24 11:44:12 -04006577 if (!phba->sli4_hba.rpi_hdrs_in_use)
6578 goto exit;
6579
James Smartda0436e2009-05-22 14:51:39 -04006580 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
6581 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
6582 list_del(&rpi_hdr->list);
6583 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
6584 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
6585 kfree(rpi_hdr->dmabuf);
6586 kfree(rpi_hdr);
6587 }
James Smart6d368e52011-05-24 11:44:12 -04006588 exit:
6589 /* There are no rpis available to the port now. */
6590 phba->sli4_hba.next_rpi = 0;
James Smartda0436e2009-05-22 14:51:39 -04006591}
6592
6593/**
James Smart3772a992009-05-22 14:50:54 -04006594 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
6595 * @pdev: pointer to pci device data structure.
6596 *
6597 * This routine is invoked to allocate the driver hba data structure for an
6598 * HBA device. If the allocation is successful, the phba reference to the
6599 * PCI device data structure is set.
6600 *
6601 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006602 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04006603 * NULL - error
6604 **/
6605static struct lpfc_hba *
6606lpfc_hba_alloc(struct pci_dev *pdev)
6607{
6608 struct lpfc_hba *phba;
6609
6610 /* Allocate memory for HBA structure */
6611 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
6612 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02006613 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04006614 return NULL;
6615 }
6616
6617 /* Set reference to PCI device in HBA structure */
6618 phba->pcidev = pdev;
6619
6620 /* Assign an unused board number */
6621 phba->brd_no = lpfc_get_instance();
6622 if (phba->brd_no < 0) {
6623 kfree(phba);
6624 return NULL;
6625 }
James Smart65791f12016-07-06 12:35:56 -07006626 phba->eratt_poll_interval = LPFC_ERATT_POLL_INTERVAL;
James Smart3772a992009-05-22 14:50:54 -04006627
James Smart4fede782010-01-26 23:08:55 -05006628 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04006629 INIT_LIST_HEAD(&phba->ct_ev_waiters);
6630
James Smart3772a992009-05-22 14:50:54 -04006631 return phba;
6632}
6633
6634/**
6635 * lpfc_hba_free - Free driver hba data structure with a device.
6636 * @phba: pointer to lpfc hba data structure.
6637 *
6638 * This routine is invoked to free the driver hba data structure with an
6639 * HBA device.
6640 **/
6641static void
6642lpfc_hba_free(struct lpfc_hba *phba)
6643{
6644 /* Release the driver assigned board number */
6645 idr_remove(&lpfc_hba_index, phba->brd_no);
6646
James Smart895427b2017-02-12 13:52:30 -08006647 /* Free memory allocated with sli3 rings */
6648 kfree(phba->sli.sli3_ring);
6649 phba->sli.sli3_ring = NULL;
James Smart2a76a282012-08-03 12:35:54 -04006650
James Smart3772a992009-05-22 14:50:54 -04006651 kfree(phba);
6652 return;
6653}
6654
6655/**
6656 * lpfc_create_shost - Create hba physical port with associated scsi host.
6657 * @phba: pointer to lpfc hba data structure.
6658 *
6659 * This routine is invoked to create HBA physical port and associate a SCSI
6660 * host with it.
6661 *
6662 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006663 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006664 * other values - error
6665 **/
6666static int
6667lpfc_create_shost(struct lpfc_hba *phba)
6668{
6669 struct lpfc_vport *vport;
6670 struct Scsi_Host *shost;
6671
6672 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05006673 phba->fc_edtov = FF_DEF_EDTOV;
6674 phba->fc_ratov = FF_DEF_RATOV;
6675 phba->fc_altov = FF_DEF_ALTOV;
6676 phba->fc_arbtov = FF_DEF_ARBTOV;
6677
James Smartd7c47992010-06-08 18:31:54 -04006678 atomic_set(&phba->sdev_cnt, 0);
James Smart3de2a652007-08-02 11:09:59 -04006679 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05006680 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04006681 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05006682
6683 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05006684 phba->pport = vport;
James Smart2ea259e2017-02-12 13:52:27 -08006685
James Smartf358dd02017-02-12 13:52:34 -08006686 if (phba->nvmet_support) {
6687 /* Only 1 vport (pport) will support NVME target */
6688 if (phba->txrdy_payload_pool == NULL) {
6689 phba->txrdy_payload_pool = pci_pool_create(
6690 "txrdy_pool", phba->pcidev,
6691 TXRDY_PAYLOAD_LEN, 16, 0);
6692 if (phba->txrdy_payload_pool) {
6693 phba->targetport = NULL;
6694 phba->cfg_enable_fc4_type = LPFC_ENABLE_NVME;
6695 lpfc_printf_log(phba, KERN_INFO,
6696 LOG_INIT | LOG_NVME_DISC,
6697 "6076 NVME Target Found\n");
6698 }
6699 }
6700 }
6701
James Smart858c9f62007-06-17 19:56:39 -05006702 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04006703 /* Put reference to SCSI host to driver's device private data */
6704 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05006705
James Smart4258e982015-12-16 18:11:58 -05006706 /*
6707 * At this point we are fully registered with PSA. In addition,
6708 * any initial discovery should be completed.
6709 */
6710 vport->load_flag |= FC_ALLOW_FDMI;
James Smart8663cbb2016-03-31 14:12:33 -07006711 if (phba->cfg_enable_SmartSAN ||
6712 (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
James Smart4258e982015-12-16 18:11:58 -05006713
6714 /* Setup appropriate attribute masks */
6715 vport->fdmi_hba_mask = LPFC_FDMI2_HBA_ATTR;
James Smart8663cbb2016-03-31 14:12:33 -07006716 if (phba->cfg_enable_SmartSAN)
James Smart4258e982015-12-16 18:11:58 -05006717 vport->fdmi_port_mask = LPFC_FDMI2_SMART_ATTR;
6718 else
6719 vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
6720 }
James Smart3772a992009-05-22 14:50:54 -04006721 return 0;
6722}
dea31012005-04-17 16:05:31 -05006723
James Smart3772a992009-05-22 14:50:54 -04006724/**
6725 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
6726 * @phba: pointer to lpfc hba data structure.
6727 *
6728 * This routine is invoked to destroy HBA physical port and the associated
6729 * SCSI host.
6730 **/
6731static void
6732lpfc_destroy_shost(struct lpfc_hba *phba)
6733{
6734 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04006735
James Smart3772a992009-05-22 14:50:54 -04006736 /* Destroy physical port that associated with the SCSI host */
6737 destroy_port(vport);
6738
6739 return;
6740}
6741
6742/**
6743 * lpfc_setup_bg - Setup Block guard structures and debug areas.
6744 * @phba: pointer to lpfc hba data structure.
6745 * @shost: the shost to be used to detect Block guard settings.
6746 *
6747 * This routine sets up the local Block guard protocol settings for @shost.
6748 * This routine also allocates memory for debugging bg buffers.
6749 **/
6750static void
6751lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
6752{
James Smartbbeb79b2012-06-12 13:54:27 -04006753 uint32_t old_mask;
6754 uint32_t old_guard;
6755
James Smart3772a992009-05-22 14:50:54 -04006756 int pagecnt = 10;
James Smartb3b98b72016-10-13 15:06:06 -07006757 if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
James Smart3772a992009-05-22 14:50:54 -04006758 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6759 "1478 Registering BlockGuard with the "
6760 "SCSI layer\n");
James Smartbbeb79b2012-06-12 13:54:27 -04006761
James Smartb3b98b72016-10-13 15:06:06 -07006762 old_mask = phba->cfg_prot_mask;
6763 old_guard = phba->cfg_prot_guard;
James Smartbbeb79b2012-06-12 13:54:27 -04006764
6765 /* Only allow supported values */
James Smartb3b98b72016-10-13 15:06:06 -07006766 phba->cfg_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
James Smartbbeb79b2012-06-12 13:54:27 -04006767 SHOST_DIX_TYPE0_PROTECTION |
6768 SHOST_DIX_TYPE1_PROTECTION);
James Smartb3b98b72016-10-13 15:06:06 -07006769 phba->cfg_prot_guard &= (SHOST_DIX_GUARD_IP |
6770 SHOST_DIX_GUARD_CRC);
James Smartbbeb79b2012-06-12 13:54:27 -04006771
6772 /* DIF Type 1 protection for profiles AST1/C1 is end to end */
James Smartb3b98b72016-10-13 15:06:06 -07006773 if (phba->cfg_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
6774 phba->cfg_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
James Smartbbeb79b2012-06-12 13:54:27 -04006775
James Smartb3b98b72016-10-13 15:06:06 -07006776 if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
6777 if ((old_mask != phba->cfg_prot_mask) ||
6778 (old_guard != phba->cfg_prot_guard))
James Smartbbeb79b2012-06-12 13:54:27 -04006779 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6780 "1475 Registering BlockGuard with the "
6781 "SCSI layer: mask %d guard %d\n",
James Smartb3b98b72016-10-13 15:06:06 -07006782 phba->cfg_prot_mask,
6783 phba->cfg_prot_guard);
James Smartbbeb79b2012-06-12 13:54:27 -04006784
James Smartb3b98b72016-10-13 15:06:06 -07006785 scsi_host_set_prot(shost, phba->cfg_prot_mask);
6786 scsi_host_set_guard(shost, phba->cfg_prot_guard);
James Smartbbeb79b2012-06-12 13:54:27 -04006787 } else
6788 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6789 "1479 Not Registering BlockGuard with the SCSI "
6790 "layer, Bad protection parameters: %d %d\n",
6791 old_mask, old_guard);
James Smart98c9ea52007-10-27 13:37:33 -04006792 }
James Smartbbeb79b2012-06-12 13:54:27 -04006793
James Smart81301a92008-12-04 22:39:46 -05006794 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05006795 while (pagecnt) {
6796 spin_lock_init(&_dump_buf_lock);
6797 _dump_buf_data =
6798 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
6799 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04006800 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6801 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04006802 "_dump_buf_data at 0x%p\n",
6803 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05006804 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04006805 memset(_dump_buf_data, 0,
6806 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05006807 break;
James Smart3772a992009-05-22 14:50:54 -04006808 } else
James Smart81301a92008-12-04 22:39:46 -05006809 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05006810 }
James Smart81301a92008-12-04 22:39:46 -05006811 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04006812 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6813 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04006814 "memory for hexdump\n");
6815 } else
James Smart6a9c52c2009-10-02 15:16:51 -04006816 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6817 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05006818 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05006819 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05006820 while (pagecnt) {
6821 _dump_buf_dif =
6822 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
6823 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04006824 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6825 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04006826 "_dump_buf_dif at 0x%p\n",
6827 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05006828 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04006829 memset(_dump_buf_dif, 0,
6830 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05006831 break;
James Smart3772a992009-05-22 14:50:54 -04006832 } else
James Smart81301a92008-12-04 22:39:46 -05006833 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05006834 }
James Smart81301a92008-12-04 22:39:46 -05006835 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04006836 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6837 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04006838 "memory for hexdump\n");
6839 } else
James Smart6a9c52c2009-10-02 15:16:51 -04006840 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6841 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04006842 _dump_buf_dif);
6843}
6844
6845/**
6846 * lpfc_post_init_setup - Perform necessary device post initialization setup.
6847 * @phba: pointer to lpfc hba data structure.
6848 *
6849 * This routine is invoked to perform all the necessary post initialization
6850 * setup for the device.
6851 **/
6852static void
6853lpfc_post_init_setup(struct lpfc_hba *phba)
6854{
6855 struct Scsi_Host *shost;
6856 struct lpfc_adapter_event_header adapter_event;
6857
6858 /* Get the default values for Model Name and Description */
6859 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
6860
6861 /*
6862 * hba setup may have changed the hba_queue_depth so we need to
6863 * adjust the value of can_queue.
6864 */
6865 shost = pci_get_drvdata(phba->pcidev);
6866 shost->can_queue = phba->cfg_hba_queue_depth - 10;
6867 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
6868 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05006869
6870 lpfc_host_attrib_init(shost);
6871
James Smart2e0fef82007-06-17 19:56:36 -05006872 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
6873 spin_lock_irq(shost->host_lock);
6874 lpfc_poll_start_timer(phba);
6875 spin_unlock_irq(shost->host_lock);
6876 }
James Smart8f6d98d2006-08-01 07:34:00 -04006877
James Smart93996272008-08-24 21:50:30 -04006878 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6879 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04006880 /* Send board arrival event to upper layer */
6881 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
6882 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
6883 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04006884 sizeof(adapter_event),
6885 (char *) &adapter_event,
6886 LPFC_NL_VENDOR_ID);
6887 return;
6888}
6889
6890/**
6891 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
6892 * @phba: pointer to lpfc hba data structure.
6893 *
6894 * This routine is invoked to set up the PCI device memory space for device
6895 * with SLI-3 interface spec.
6896 *
6897 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006898 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006899 * other values - error
6900 **/
6901static int
6902lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
6903{
6904 struct pci_dev *pdev;
6905 unsigned long bar0map_len, bar2map_len;
6906 int i, hbq_count;
6907 void *ptr;
6908 int error = -ENODEV;
6909
6910 /* Obtain PCI device reference */
6911 if (!phba->pcidev)
6912 return error;
6913 else
6914 pdev = phba->pcidev;
6915
6916 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05006917 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
6918 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
6919 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
6920 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smart3772a992009-05-22 14:50:54 -04006921 return error;
Michael Reed8e685972009-09-18 12:02:05 -05006922 }
6923 }
James Smart3772a992009-05-22 14:50:54 -04006924
6925 /* Get the bus address of Bar0 and Bar2 and the number of bytes
6926 * required by each mapping.
6927 */
6928 phba->pci_bar0_map = pci_resource_start(pdev, 0);
6929 bar0map_len = pci_resource_len(pdev, 0);
6930
6931 phba->pci_bar2_map = pci_resource_start(pdev, 2);
6932 bar2map_len = pci_resource_len(pdev, 2);
6933
6934 /* Map HBA SLIM to a kernel virtual address. */
6935 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
6936 if (!phba->slim_memmap_p) {
6937 dev_printk(KERN_ERR, &pdev->dev,
6938 "ioremap failed for SLIM memory.\n");
6939 goto out;
6940 }
6941
6942 /* Map HBA Control Registers to a kernel virtual address. */
6943 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
6944 if (!phba->ctrl_regs_memmap_p) {
6945 dev_printk(KERN_ERR, &pdev->dev,
6946 "ioremap failed for HBA control registers.\n");
6947 goto out_iounmap_slim;
6948 }
6949
6950 /* Allocate memory for SLI-2 structures */
Joe Perches1aee3832014-09-03 12:56:12 -04006951 phba->slim2p.virt = dma_zalloc_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6952 &phba->slim2p.phys, GFP_KERNEL);
James Smart3772a992009-05-22 14:50:54 -04006953 if (!phba->slim2p.virt)
6954 goto out_iounmap;
6955
James Smart3772a992009-05-22 14:50:54 -04006956 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04006957 phba->mbox_ext = (phba->slim2p.virt +
6958 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04006959 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
6960 phba->IOCBs = (phba->slim2p.virt +
6961 offsetof(struct lpfc_sli2_slim, IOCBs));
6962
6963 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
6964 lpfc_sli_hbq_size(),
6965 &phba->hbqslimp.phys,
6966 GFP_KERNEL);
6967 if (!phba->hbqslimp.virt)
6968 goto out_free_slim;
6969
6970 hbq_count = lpfc_sli_hbq_count();
6971 ptr = phba->hbqslimp.virt;
6972 for (i = 0; i < hbq_count; ++i) {
6973 phba->hbqs[i].hbq_virt = ptr;
6974 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
6975 ptr += (lpfc_hbq_defs[i]->entry_count *
6976 sizeof(struct lpfc_hbq_entry));
6977 }
6978 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
6979 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
6980
6981 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
6982
James Smart3772a992009-05-22 14:50:54 -04006983 phba->MBslimaddr = phba->slim_memmap_p;
6984 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
6985 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
6986 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
6987 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04006988
dea31012005-04-17 16:05:31 -05006989 return 0;
6990
dea31012005-04-17 16:05:31 -05006991out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04006992 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6993 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05006994out_iounmap:
6995 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05006996out_iounmap_slim:
dea31012005-04-17 16:05:31 -05006997 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05006998out:
6999 return error;
7000}
7001
James Smarte59058c2008-08-24 21:49:00 -04007002/**
James Smart3772a992009-05-22 14:50:54 -04007003 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
7004 * @phba: pointer to lpfc hba data structure.
7005 *
7006 * This routine is invoked to unset the PCI device memory space for device
7007 * with SLI-3 interface spec.
7008 **/
7009static void
7010lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
7011{
7012 struct pci_dev *pdev;
7013
7014 /* Obtain PCI device reference */
7015 if (!phba->pcidev)
7016 return;
7017 else
7018 pdev = phba->pcidev;
7019
7020 /* Free coherent DMA memory allocated */
7021 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
7022 phba->hbqslimp.virt, phba->hbqslimp.phys);
7023 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7024 phba->slim2p.virt, phba->slim2p.phys);
7025
7026 /* I/O memory unmap */
7027 iounmap(phba->ctrl_regs_memmap_p);
7028 iounmap(phba->slim_memmap_p);
7029
7030 return;
7031}
7032
7033/**
James Smartda0436e2009-05-22 14:51:39 -04007034 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
7035 * @phba: pointer to lpfc hba data structure.
7036 *
7037 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
7038 * done and check status.
7039 *
7040 * Return 0 if successful, otherwise -ENODEV.
7041 **/
7042int
7043lpfc_sli4_post_status_check(struct lpfc_hba *phba)
7044{
James Smart2fcee4b2010-12-15 17:57:46 -05007045 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
7046 struct lpfc_register reg_data;
7047 int i, port_error = 0;
7048 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04007049
James Smart9940b972011-03-11 16:06:12 -05007050 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
7051 memset(&reg_data, 0, sizeof(reg_data));
James Smart2fcee4b2010-12-15 17:57:46 -05007052 if (!phba->sli4_hba.PSMPHRregaddr)
James Smartda0436e2009-05-22 14:51:39 -04007053 return -ENODEV;
7054
James Smartda0436e2009-05-22 14:51:39 -04007055 /* Wait up to 30 seconds for the SLI Port POST done and ready */
7056 for (i = 0; i < 3000; i++) {
James Smart9940b972011-03-11 16:06:12 -05007057 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
7058 &portsmphr_reg.word0) ||
7059 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007060 /* Port has a fatal POST error, break out */
James Smartda0436e2009-05-22 14:51:39 -04007061 port_error = -ENODEV;
7062 break;
7063 }
James Smart2fcee4b2010-12-15 17:57:46 -05007064 if (LPFC_POST_STAGE_PORT_READY ==
7065 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
James Smartda0436e2009-05-22 14:51:39 -04007066 break;
James Smartda0436e2009-05-22 14:51:39 -04007067 msleep(10);
7068 }
7069
James Smart2fcee4b2010-12-15 17:57:46 -05007070 /*
7071 * If there was a port error during POST, then don't proceed with
7072 * other register reads as the data may not be valid. Just exit.
7073 */
7074 if (port_error) {
James Smartda0436e2009-05-22 14:51:39 -04007075 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05007076 "1408 Port Failed POST - portsmphr=0x%x, "
7077 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
7078 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
7079 portsmphr_reg.word0,
7080 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
7081 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
7082 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
7083 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
7084 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
7085 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
7086 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
7087 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
7088 } else {
James Smart28baac72010-02-12 14:42:03 -05007089 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05007090 "2534 Device Info: SLIFamily=0x%x, "
7091 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
7092 "SLIHint_2=0x%x, FT=0x%x\n",
James Smart28baac72010-02-12 14:42:03 -05007093 bf_get(lpfc_sli_intf_sli_family,
7094 &phba->sli4_hba.sli_intf),
7095 bf_get(lpfc_sli_intf_slirev,
7096 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05007097 bf_get(lpfc_sli_intf_if_type,
James Smart28baac72010-02-12 14:42:03 -05007098 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05007099 bf_get(lpfc_sli_intf_sli_hint1,
7100 &phba->sli4_hba.sli_intf),
7101 bf_get(lpfc_sli_intf_sli_hint2,
7102 &phba->sli4_hba.sli_intf),
7103 bf_get(lpfc_sli_intf_func_type,
James Smart28baac72010-02-12 14:42:03 -05007104 &phba->sli4_hba.sli_intf));
James Smart2fcee4b2010-12-15 17:57:46 -05007105 /*
7106 * Check for other Port errors during the initialization
7107 * process. Fail the load if the port did not come up
7108 * correctly.
7109 */
7110 if_type = bf_get(lpfc_sli_intf_if_type,
7111 &phba->sli4_hba.sli_intf);
7112 switch (if_type) {
7113 case LPFC_SLI_INTF_IF_TYPE_0:
7114 phba->sli4_hba.ue_mask_lo =
7115 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
7116 phba->sli4_hba.ue_mask_hi =
7117 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
7118 uerrlo_reg.word0 =
7119 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
7120 uerrhi_reg.word0 =
7121 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
7122 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
7123 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
7124 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7125 "1422 Unrecoverable Error "
7126 "Detected during POST "
7127 "uerr_lo_reg=0x%x, "
7128 "uerr_hi_reg=0x%x, "
7129 "ue_mask_lo_reg=0x%x, "
7130 "ue_mask_hi_reg=0x%x\n",
7131 uerrlo_reg.word0,
7132 uerrhi_reg.word0,
7133 phba->sli4_hba.ue_mask_lo,
7134 phba->sli4_hba.ue_mask_hi);
7135 port_error = -ENODEV;
7136 }
7137 break;
7138 case LPFC_SLI_INTF_IF_TYPE_2:
7139 /* Final checks. The port status should be clean. */
James Smart9940b972011-03-11 16:06:12 -05007140 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
7141 &reg_data.word0) ||
James Smart05580562011-05-24 11:40:48 -04007142 (bf_get(lpfc_sliport_status_err, &reg_data) &&
7143 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007144 phba->work_status[0] =
7145 readl(phba->sli4_hba.u.if_type2.
7146 ERR1regaddr);
7147 phba->work_status[1] =
7148 readl(phba->sli4_hba.u.if_type2.
7149 ERR2regaddr);
7150 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05007151 "2888 Unrecoverable port error "
7152 "following POST: port status reg "
7153 "0x%x, port_smphr reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05007154 "error 1=0x%x, error 2=0x%x\n",
7155 reg_data.word0,
7156 portsmphr_reg.word0,
7157 phba->work_status[0],
7158 phba->work_status[1]);
7159 port_error = -ENODEV;
7160 }
7161 break;
7162 case LPFC_SLI_INTF_IF_TYPE_1:
7163 default:
7164 break;
7165 }
James Smart28baac72010-02-12 14:42:03 -05007166 }
James Smartda0436e2009-05-22 14:51:39 -04007167 return port_error;
7168}
7169
7170/**
7171 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
7172 * @phba: pointer to lpfc hba data structure.
James Smart2fcee4b2010-12-15 17:57:46 -05007173 * @if_type: The SLI4 interface type getting configured.
James Smartda0436e2009-05-22 14:51:39 -04007174 *
7175 * This routine is invoked to set up SLI4 BAR0 PCI config space register
7176 * memory map.
7177 **/
7178static void
James Smart2fcee4b2010-12-15 17:57:46 -05007179lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04007180{
James Smart2fcee4b2010-12-15 17:57:46 -05007181 switch (if_type) {
7182 case LPFC_SLI_INTF_IF_TYPE_0:
7183 phba->sli4_hba.u.if_type0.UERRLOregaddr =
7184 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
7185 phba->sli4_hba.u.if_type0.UERRHIregaddr =
7186 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
7187 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
7188 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
7189 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
7190 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
7191 phba->sli4_hba.SLIINTFregaddr =
7192 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
7193 break;
7194 case LPFC_SLI_INTF_IF_TYPE_2:
7195 phba->sli4_hba.u.if_type2.ERR1regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04007196 phba->sli4_hba.conf_regs_memmap_p +
7197 LPFC_CTL_PORT_ER1_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05007198 phba->sli4_hba.u.if_type2.ERR2regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04007199 phba->sli4_hba.conf_regs_memmap_p +
7200 LPFC_CTL_PORT_ER2_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05007201 phba->sli4_hba.u.if_type2.CTRLregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04007202 phba->sli4_hba.conf_regs_memmap_p +
7203 LPFC_CTL_PORT_CTL_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05007204 phba->sli4_hba.u.if_type2.STATUSregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04007205 phba->sli4_hba.conf_regs_memmap_p +
7206 LPFC_CTL_PORT_STA_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05007207 phba->sli4_hba.SLIINTFregaddr =
7208 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
7209 phba->sli4_hba.PSMPHRregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04007210 phba->sli4_hba.conf_regs_memmap_p +
7211 LPFC_CTL_PORT_SEM_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05007212 phba->sli4_hba.RQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05007213 phba->sli4_hba.conf_regs_memmap_p +
7214 LPFC_ULP0_RQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05007215 phba->sli4_hba.WQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05007216 phba->sli4_hba.conf_regs_memmap_p +
7217 LPFC_ULP0_WQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05007218 phba->sli4_hba.EQCQDBregaddr =
7219 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
7220 phba->sli4_hba.MQDBregaddr =
7221 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
7222 phba->sli4_hba.BMBXregaddr =
7223 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
7224 break;
7225 case LPFC_SLI_INTF_IF_TYPE_1:
7226 default:
7227 dev_printk(KERN_ERR, &phba->pcidev->dev,
7228 "FATAL - unsupported SLI4 interface type - %d\n",
7229 if_type);
7230 break;
7231 }
James Smartda0436e2009-05-22 14:51:39 -04007232}
7233
7234/**
7235 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
7236 * @phba: pointer to lpfc hba data structure.
7237 *
7238 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
7239 * memory map.
7240 **/
7241static void
7242lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
7243{
James Smart2fcee4b2010-12-15 17:57:46 -05007244 phba->sli4_hba.PSMPHRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
7245 LPFC_SLIPORT_IF0_SMPHR;
James Smartda0436e2009-05-22 14:51:39 -04007246 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05007247 LPFC_HST_ISR0;
James Smartda0436e2009-05-22 14:51:39 -04007248 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05007249 LPFC_HST_IMR0;
James Smartda0436e2009-05-22 14:51:39 -04007250 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05007251 LPFC_HST_ISCR0;
James Smartda0436e2009-05-22 14:51:39 -04007252}
7253
7254/**
7255 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
7256 * @phba: pointer to lpfc hba data structure.
7257 * @vf: virtual function number
7258 *
7259 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
7260 * based on the given viftual function number, @vf.
7261 *
7262 * Return 0 if successful, otherwise -ENODEV.
7263 **/
7264static int
7265lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
7266{
7267 if (vf > LPFC_VIR_FUNC_MAX)
7268 return -ENODEV;
7269
7270 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05007271 vf * LPFC_VFR_PAGE_SIZE +
7272 LPFC_ULP0_RQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04007273 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05007274 vf * LPFC_VFR_PAGE_SIZE +
7275 LPFC_ULP0_WQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04007276 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7277 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
7278 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7279 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
7280 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7281 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
7282 return 0;
7283}
7284
7285/**
7286 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
7287 * @phba: pointer to lpfc hba data structure.
7288 *
7289 * This routine is invoked to create the bootstrap mailbox
7290 * region consistent with the SLI-4 interface spec. This
7291 * routine allocates all memory necessary to communicate
7292 * mailbox commands to the port and sets up all alignment
7293 * needs. No locks are expected to be held when calling
7294 * this routine.
7295 *
7296 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007297 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04007298 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04007299 **/
7300static int
7301lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
7302{
7303 uint32_t bmbx_size;
7304 struct lpfc_dmabuf *dmabuf;
7305 struct dma_address *dma_address;
7306 uint32_t pa_addr;
7307 uint64_t phys_addr;
7308
7309 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
7310 if (!dmabuf)
7311 return -ENOMEM;
7312
7313 /*
7314 * The bootstrap mailbox region is comprised of 2 parts
7315 * plus an alignment restriction of 16 bytes.
7316 */
7317 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
Joe Perches1aee3832014-09-03 12:56:12 -04007318 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, bmbx_size,
7319 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04007320 if (!dmabuf->virt) {
7321 kfree(dmabuf);
7322 return -ENOMEM;
7323 }
James Smartda0436e2009-05-22 14:51:39 -04007324
7325 /*
7326 * Initialize the bootstrap mailbox pointers now so that the register
7327 * operations are simple later. The mailbox dma address is required
7328 * to be 16-byte aligned. Also align the virtual memory as each
7329 * maibox is copied into the bmbx mailbox region before issuing the
7330 * command to the port.
7331 */
7332 phba->sli4_hba.bmbx.dmabuf = dmabuf;
7333 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
7334
7335 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
7336 LPFC_ALIGN_16_BYTE);
7337 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
7338 LPFC_ALIGN_16_BYTE);
7339
7340 /*
7341 * Set the high and low physical addresses now. The SLI4 alignment
7342 * requirement is 16 bytes and the mailbox is posted to the port
7343 * as two 30-bit addresses. The other data is a bit marking whether
7344 * the 30-bit address is the high or low address.
7345 * Upcast bmbx aphys to 64bits so shift instruction compiles
7346 * clean on 32 bit machines.
7347 */
7348 dma_address = &phba->sli4_hba.bmbx.dma_address;
7349 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
7350 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
7351 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
7352 LPFC_BMBX_BIT1_ADDR_HI);
7353
7354 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
7355 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
7356 LPFC_BMBX_BIT1_ADDR_LO);
7357 return 0;
7358}
7359
7360/**
7361 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
7362 * @phba: pointer to lpfc hba data structure.
7363 *
7364 * This routine is invoked to teardown the bootstrap mailbox
7365 * region and release all host resources. This routine requires
7366 * the caller to ensure all mailbox commands recovered, no
7367 * additional mailbox comands are sent, and interrupts are disabled
7368 * before calling this routine.
7369 *
7370 **/
7371static void
7372lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
7373{
7374 dma_free_coherent(&phba->pcidev->dev,
7375 phba->sli4_hba.bmbx.bmbx_size,
7376 phba->sli4_hba.bmbx.dmabuf->virt,
7377 phba->sli4_hba.bmbx.dmabuf->phys);
7378
7379 kfree(phba->sli4_hba.bmbx.dmabuf);
7380 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
7381}
7382
7383/**
7384 * lpfc_sli4_read_config - Get the config parameters.
7385 * @phba: pointer to lpfc hba data structure.
7386 *
7387 * This routine is invoked to read the configuration parameters from the HBA.
7388 * The configuration parameters are used to set the base and maximum values
7389 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
7390 * allocation for the port.
7391 *
7392 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007393 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007394 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007395 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007396 **/
James Smartff78d8f2011-12-13 13:21:35 -05007397int
James Smartda0436e2009-05-22 14:51:39 -04007398lpfc_sli4_read_config(struct lpfc_hba *phba)
7399{
7400 LPFC_MBOXQ_t *pmb;
7401 struct lpfc_mbx_read_config *rd_config;
James Smart912e3ac2011-05-24 11:42:11 -04007402 union lpfc_sli4_cfg_shdr *shdr;
7403 uint32_t shdr_status, shdr_add_status;
7404 struct lpfc_mbx_get_func_cfg *get_func_cfg;
7405 struct lpfc_rsrc_desc_fcfcoe *desc;
James Smart8aa134a2012-08-14 14:25:29 -04007406 char *pdesc_0;
James Smartc6918162016-10-13 15:06:16 -07007407 uint16_t forced_link_speed;
7408 uint32_t if_type;
James Smart8aa134a2012-08-14 14:25:29 -04007409 int length, i, rc = 0, rc2;
James Smartda0436e2009-05-22 14:51:39 -04007410
7411 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7412 if (!pmb) {
7413 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7414 "2011 Unable to allocate memory for issuing "
7415 "SLI_CONFIG_SPECIAL mailbox command\n");
7416 return -ENOMEM;
7417 }
7418
7419 lpfc_read_config(phba, pmb);
7420
7421 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
7422 if (rc != MBX_SUCCESS) {
7423 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7424 "2012 Mailbox failed , mbxCmd x%x "
7425 "READ_CONFIG, mbxStatus x%x\n",
7426 bf_get(lpfc_mqe_command, &pmb->u.mqe),
7427 bf_get(lpfc_mqe_status, &pmb->u.mqe));
7428 rc = -EIO;
7429 } else {
7430 rd_config = &pmb->u.mqe.un.rd_config;
James Smartff78d8f2011-12-13 13:21:35 -05007431 if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
7432 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
7433 phba->sli4_hba.lnk_info.lnk_tp =
7434 bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
7435 phba->sli4_hba.lnk_info.lnk_no =
7436 bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
7437 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7438 "3081 lnk_type:%d, lnk_numb:%d\n",
7439 phba->sli4_hba.lnk_info.lnk_tp,
7440 phba->sli4_hba.lnk_info.lnk_no);
7441 } else
7442 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
7443 "3082 Mailbox (x%x) returned ldv:x0\n",
7444 bf_get(lpfc_mqe_command, &pmb->u.mqe));
James Smart6d368e52011-05-24 11:44:12 -04007445 phba->sli4_hba.extents_in_use =
7446 bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04007447 phba->sli4_hba.max_cfg_param.max_xri =
7448 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
7449 phba->sli4_hba.max_cfg_param.xri_base =
7450 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
7451 phba->sli4_hba.max_cfg_param.max_vpi =
7452 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
7453 phba->sli4_hba.max_cfg_param.vpi_base =
7454 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
7455 phba->sli4_hba.max_cfg_param.max_rpi =
7456 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
7457 phba->sli4_hba.max_cfg_param.rpi_base =
7458 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
7459 phba->sli4_hba.max_cfg_param.max_vfi =
7460 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
7461 phba->sli4_hba.max_cfg_param.vfi_base =
7462 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
7463 phba->sli4_hba.max_cfg_param.max_fcfi =
7464 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04007465 phba->sli4_hba.max_cfg_param.max_eq =
7466 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
7467 phba->sli4_hba.max_cfg_param.max_rq =
7468 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
7469 phba->sli4_hba.max_cfg_param.max_wq =
7470 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
7471 phba->sli4_hba.max_cfg_param.max_cq =
7472 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
7473 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
7474 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
7475 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
7476 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
James Smart5ffc2662009-11-18 15:39:44 -05007477 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
7478 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04007479 phba->max_vports = phba->max_vpi;
7480 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart6d368e52011-05-24 11:44:12 -04007481 "2003 cfg params Extents? %d "
7482 "XRI(B:%d M:%d), "
James Smartda0436e2009-05-22 14:51:39 -04007483 "VPI(B:%d M:%d) "
7484 "VFI(B:%d M:%d) "
7485 "RPI(B:%d M:%d) "
James Smart2ea259e2017-02-12 13:52:27 -08007486 "FCFI:%d EQ:%d CQ:%d WQ:%d RQ:%d\n",
James Smart6d368e52011-05-24 11:44:12 -04007487 phba->sli4_hba.extents_in_use,
James Smartda0436e2009-05-22 14:51:39 -04007488 phba->sli4_hba.max_cfg_param.xri_base,
7489 phba->sli4_hba.max_cfg_param.max_xri,
7490 phba->sli4_hba.max_cfg_param.vpi_base,
7491 phba->sli4_hba.max_cfg_param.max_vpi,
7492 phba->sli4_hba.max_cfg_param.vfi_base,
7493 phba->sli4_hba.max_cfg_param.max_vfi,
7494 phba->sli4_hba.max_cfg_param.rpi_base,
7495 phba->sli4_hba.max_cfg_param.max_rpi,
James Smart2ea259e2017-02-12 13:52:27 -08007496 phba->sli4_hba.max_cfg_param.max_fcfi,
7497 phba->sli4_hba.max_cfg_param.max_eq,
7498 phba->sli4_hba.max_cfg_param.max_cq,
7499 phba->sli4_hba.max_cfg_param.max_wq,
7500 phba->sli4_hba.max_cfg_param.max_rq);
7501
James Smartda0436e2009-05-22 14:51:39 -04007502 }
James Smart912e3ac2011-05-24 11:42:11 -04007503
7504 if (rc)
7505 goto read_cfg_out;
James Smartda0436e2009-05-22 14:51:39 -04007506
James Smartc6918162016-10-13 15:06:16 -07007507 /* Update link speed if forced link speed is supported */
7508 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7509 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
7510 forced_link_speed =
7511 bf_get(lpfc_mbx_rd_conf_link_speed, rd_config);
7512 if (forced_link_speed) {
7513 phba->hba_flag |= HBA_FORCED_LINK_SPEED;
7514
7515 switch (forced_link_speed) {
7516 case LINK_SPEED_1G:
7517 phba->cfg_link_speed =
7518 LPFC_USER_LINK_SPEED_1G;
7519 break;
7520 case LINK_SPEED_2G:
7521 phba->cfg_link_speed =
7522 LPFC_USER_LINK_SPEED_2G;
7523 break;
7524 case LINK_SPEED_4G:
7525 phba->cfg_link_speed =
7526 LPFC_USER_LINK_SPEED_4G;
7527 break;
7528 case LINK_SPEED_8G:
7529 phba->cfg_link_speed =
7530 LPFC_USER_LINK_SPEED_8G;
7531 break;
7532 case LINK_SPEED_10G:
7533 phba->cfg_link_speed =
7534 LPFC_USER_LINK_SPEED_10G;
7535 break;
7536 case LINK_SPEED_16G:
7537 phba->cfg_link_speed =
7538 LPFC_USER_LINK_SPEED_16G;
7539 break;
7540 case LINK_SPEED_32G:
7541 phba->cfg_link_speed =
7542 LPFC_USER_LINK_SPEED_32G;
7543 break;
7544 case 0xffff:
7545 phba->cfg_link_speed =
7546 LPFC_USER_LINK_SPEED_AUTO;
7547 break;
7548 default:
7549 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7550 "0047 Unrecognized link "
7551 "speed : %d\n",
7552 forced_link_speed);
7553 phba->cfg_link_speed =
7554 LPFC_USER_LINK_SPEED_AUTO;
7555 }
7556 }
7557 }
7558
James Smartda0436e2009-05-22 14:51:39 -04007559 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -04007560 length = phba->sli4_hba.max_cfg_param.max_xri -
7561 lpfc_sli4_get_els_iocb_cnt(phba);
7562 if (phba->cfg_hba_queue_depth > length) {
7563 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7564 "3361 HBA queue depth changed from %d to %d\n",
7565 phba->cfg_hba_queue_depth, length);
7566 phba->cfg_hba_queue_depth = length;
7567 }
James Smart912e3ac2011-05-24 11:42:11 -04007568
7569 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
7570 LPFC_SLI_INTF_IF_TYPE_2)
7571 goto read_cfg_out;
7572
7573 /* get the pf# and vf# for SLI4 if_type 2 port */
7574 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
7575 sizeof(struct lpfc_sli4_cfg_mhdr));
7576 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
7577 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
7578 length, LPFC_SLI4_MBX_EMBED);
7579
James Smart8aa134a2012-08-14 14:25:29 -04007580 rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart912e3ac2011-05-24 11:42:11 -04007581 shdr = (union lpfc_sli4_cfg_shdr *)
7582 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
7583 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7584 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart8aa134a2012-08-14 14:25:29 -04007585 if (rc2 || shdr_status || shdr_add_status) {
James Smart912e3ac2011-05-24 11:42:11 -04007586 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7587 "3026 Mailbox failed , mbxCmd x%x "
7588 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
7589 bf_get(lpfc_mqe_command, &pmb->u.mqe),
7590 bf_get(lpfc_mqe_status, &pmb->u.mqe));
James Smart912e3ac2011-05-24 11:42:11 -04007591 goto read_cfg_out;
7592 }
7593
7594 /* search for fc_fcoe resrouce descriptor */
7595 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
James Smart912e3ac2011-05-24 11:42:11 -04007596
James Smart8aa134a2012-08-14 14:25:29 -04007597 pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
7598 desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
7599 length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
7600 if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
7601 length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
7602 else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
7603 goto read_cfg_out;
7604
James Smart912e3ac2011-05-24 11:42:11 -04007605 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
James Smart8aa134a2012-08-14 14:25:29 -04007606 desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
James Smart912e3ac2011-05-24 11:42:11 -04007607 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
James Smart8aa134a2012-08-14 14:25:29 -04007608 bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
James Smart912e3ac2011-05-24 11:42:11 -04007609 phba->sli4_hba.iov.pf_number =
7610 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
7611 phba->sli4_hba.iov.vf_number =
7612 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
7613 break;
7614 }
7615 }
7616
7617 if (i < LPFC_RSRC_DESC_MAX_NUM)
7618 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7619 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
7620 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
7621 phba->sli4_hba.iov.vf_number);
James Smart8aa134a2012-08-14 14:25:29 -04007622 else
James Smart912e3ac2011-05-24 11:42:11 -04007623 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7624 "3028 GET_FUNCTION_CONFIG: failed to find "
7625 "Resrouce Descriptor:x%x\n",
7626 LPFC_RSRC_DESC_TYPE_FCFCOE);
James Smart912e3ac2011-05-24 11:42:11 -04007627
7628read_cfg_out:
7629 mempool_free(pmb, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04007630 return rc;
7631}
7632
7633/**
James Smart2fcee4b2010-12-15 17:57:46 -05007634 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
James Smartda0436e2009-05-22 14:51:39 -04007635 * @phba: pointer to lpfc hba data structure.
7636 *
James Smart2fcee4b2010-12-15 17:57:46 -05007637 * This routine is invoked to setup the port-side endian order when
7638 * the port if_type is 0. This routine has no function for other
7639 * if_types.
James Smartda0436e2009-05-22 14:51:39 -04007640 *
7641 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007642 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007643 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007644 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007645 **/
7646static int
7647lpfc_setup_endian_order(struct lpfc_hba *phba)
7648{
7649 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05007650 uint32_t if_type, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04007651 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
7652 HOST_ENDIAN_HIGH_WORD1};
7653
James Smart2fcee4b2010-12-15 17:57:46 -05007654 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7655 switch (if_type) {
7656 case LPFC_SLI_INTF_IF_TYPE_0:
7657 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
7658 GFP_KERNEL);
7659 if (!mboxq) {
7660 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7661 "0492 Unable to allocate memory for "
7662 "issuing SLI_CONFIG_SPECIAL mailbox "
7663 "command\n");
7664 return -ENOMEM;
7665 }
James Smartda0436e2009-05-22 14:51:39 -04007666
James Smart2fcee4b2010-12-15 17:57:46 -05007667 /*
7668 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
7669 * two words to contain special data values and no other data.
7670 */
7671 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
7672 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
7673 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7674 if (rc != MBX_SUCCESS) {
7675 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7676 "0493 SLI_CONFIG_SPECIAL mailbox "
7677 "failed with status x%x\n",
7678 rc);
7679 rc = -EIO;
7680 }
7681 mempool_free(mboxq, phba->mbox_mem_pool);
7682 break;
7683 case LPFC_SLI_INTF_IF_TYPE_2:
7684 case LPFC_SLI_INTF_IF_TYPE_1:
7685 default:
7686 break;
James Smartda0436e2009-05-22 14:51:39 -04007687 }
James Smartda0436e2009-05-22 14:51:39 -04007688 return rc;
7689}
7690
7691/**
James Smart895427b2017-02-12 13:52:30 -08007692 * lpfc_sli4_queue_verify - Verify and update EQ counts
James Smartda0436e2009-05-22 14:51:39 -04007693 * @phba: pointer to lpfc hba data structure.
7694 *
James Smart895427b2017-02-12 13:52:30 -08007695 * This routine is invoked to check the user settable queue counts for EQs.
7696 * After this routine is called the counts will be set to valid values that
James Smart5350d872011-10-10 21:33:49 -04007697 * adhere to the constraints of the system's interrupt vectors and the port's
7698 * queue resources.
James Smartda0436e2009-05-22 14:51:39 -04007699 *
7700 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007701 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007702 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04007703 **/
7704static int
James Smart5350d872011-10-10 21:33:49 -04007705lpfc_sli4_queue_verify(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04007706{
James Smart895427b2017-02-12 13:52:30 -08007707 int io_channel;
James Smart1ba981f2014-02-20 09:56:45 -05007708 int fof_vectors = phba->cfg_fof ? 1 : 0;
James Smartda0436e2009-05-22 14:51:39 -04007709
7710 /*
James Smart67d12732012-08-03 12:36:13 -04007711 * Sanity check for configured queue parameters against the run-time
James Smartda0436e2009-05-22 14:51:39 -04007712 * device parameters
7713 */
7714
James Smart67d12732012-08-03 12:36:13 -04007715 /* Sanity check on HBA EQ parameters */
James Smart895427b2017-02-12 13:52:30 -08007716 io_channel = phba->io_channel_irqs;
James Smartda0436e2009-05-22 14:51:39 -04007717
James Smart895427b2017-02-12 13:52:30 -08007718 if (phba->sli4_hba.num_online_cpu < io_channel) {
James Smart82c3e9b2012-09-29 11:29:50 -04007719 lpfc_printf_log(phba,
7720 KERN_ERR, LOG_INIT,
James Smart90695ee2012-08-14 14:25:36 -04007721 "3188 Reducing IO channels to match number of "
James Smart7bb03bb2013-04-17 20:19:16 -04007722 "online CPUs: from %d to %d\n",
James Smart895427b2017-02-12 13:52:30 -08007723 io_channel, phba->sli4_hba.num_online_cpu);
7724 io_channel = phba->sli4_hba.num_online_cpu;
James Smart90695ee2012-08-14 14:25:36 -04007725 }
7726
James Smart895427b2017-02-12 13:52:30 -08007727 if (io_channel + fof_vectors > phba->sli4_hba.max_cfg_param.max_eq) {
James Smart82c3e9b2012-09-29 11:29:50 -04007728 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7729 "2575 Reducing IO channels to match number of "
7730 "available EQs: from %d to %d\n",
James Smart895427b2017-02-12 13:52:30 -08007731 io_channel,
James Smart82c3e9b2012-09-29 11:29:50 -04007732 phba->sli4_hba.max_cfg_param.max_eq);
James Smart895427b2017-02-12 13:52:30 -08007733 io_channel = phba->sli4_hba.max_cfg_param.max_eq - fof_vectors;
James Smartda0436e2009-05-22 14:51:39 -04007734 }
James Smart67d12732012-08-03 12:36:13 -04007735
James Smart895427b2017-02-12 13:52:30 -08007736 /* The actual number of FCP / NVME event queues adopted */
7737 if (io_channel != phba->io_channel_irqs)
7738 phba->io_channel_irqs = io_channel;
7739 if (phba->cfg_fcp_io_channel > io_channel)
7740 phba->cfg_fcp_io_channel = io_channel;
7741 if (phba->cfg_nvme_io_channel > io_channel)
7742 phba->cfg_nvme_io_channel = io_channel;
James Smart2d7dbc42017-02-12 13:52:35 -08007743 if (phba->cfg_nvme_io_channel < phba->cfg_nvmet_mrq)
7744 phba->cfg_nvmet_mrq = phba->cfg_nvme_io_channel;
James Smart895427b2017-02-12 13:52:30 -08007745
7746 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2d7dbc42017-02-12 13:52:35 -08007747 "2574 IO channels: irqs %d fcp %d nvme %d MRQ: %d\n",
James Smart895427b2017-02-12 13:52:30 -08007748 phba->io_channel_irqs, phba->cfg_fcp_io_channel,
James Smart2d7dbc42017-02-12 13:52:35 -08007749 phba->cfg_nvme_io_channel, phba->cfg_nvmet_mrq);
James Smartda0436e2009-05-22 14:51:39 -04007750
James Smartda0436e2009-05-22 14:51:39 -04007751 /* Get EQ depth from module parameter, fake the default for now */
7752 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
7753 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
7754
James Smart5350d872011-10-10 21:33:49 -04007755 /* Get CQ depth from module parameter, fake the default for now */
7756 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
7757 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
James Smart5350d872011-10-10 21:33:49 -04007758 return 0;
James Smart895427b2017-02-12 13:52:30 -08007759}
7760
7761static int
7762lpfc_alloc_nvme_wq_cq(struct lpfc_hba *phba, int wqidx)
7763{
7764 struct lpfc_queue *qdesc;
7765 int cnt;
7766
7767 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7768 phba->sli4_hba.cq_ecount);
7769 if (!qdesc) {
7770 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7771 "0508 Failed allocate fast-path NVME CQ (%d)\n",
7772 wqidx);
7773 return 1;
7774 }
7775 phba->sli4_hba.nvme_cq[wqidx] = qdesc;
7776
7777 cnt = LPFC_NVME_WQSIZE;
7778 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_WQE128_SIZE, cnt);
7779 if (!qdesc) {
7780 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7781 "0509 Failed allocate fast-path NVME WQ (%d)\n",
7782 wqidx);
7783 return 1;
7784 }
7785 phba->sli4_hba.nvme_wq[wqidx] = qdesc;
7786 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
7787 return 0;
7788}
7789
7790static int
7791lpfc_alloc_fcp_wq_cq(struct lpfc_hba *phba, int wqidx)
7792{
7793 struct lpfc_queue *qdesc;
7794 uint32_t wqesize;
7795
7796 /* Create Fast Path FCP CQs */
7797 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7798 phba->sli4_hba.cq_ecount);
7799 if (!qdesc) {
7800 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7801 "0499 Failed allocate fast-path FCP CQ (%d)\n", wqidx);
7802 return 1;
7803 }
7804 phba->sli4_hba.fcp_cq[wqidx] = qdesc;
7805
7806 /* Create Fast Path FCP WQs */
7807 wqesize = (phba->fcp_embed_io) ?
7808 LPFC_WQE128_SIZE : phba->sli4_hba.wq_esize;
7809 qdesc = lpfc_sli4_queue_alloc(phba, wqesize, phba->sli4_hba.wq_ecount);
7810 if (!qdesc) {
7811 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7812 "0503 Failed allocate fast-path FCP WQ (%d)\n",
7813 wqidx);
7814 return 1;
7815 }
7816 phba->sli4_hba.fcp_wq[wqidx] = qdesc;
7817 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
7818 return 0;
James Smart5350d872011-10-10 21:33:49 -04007819}
7820
7821/**
7822 * lpfc_sli4_queue_create - Create all the SLI4 queues
7823 * @phba: pointer to lpfc hba data structure.
7824 *
7825 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
7826 * operation. For each SLI4 queue type, the parameters such as queue entry
7827 * count (queue depth) shall be taken from the module parameter. For now,
7828 * we just use some constant number as place holder.
7829 *
7830 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07007831 * 0 - successful
James Smart5350d872011-10-10 21:33:49 -04007832 * -ENOMEM - No availble memory
7833 * -EIO - The mailbox failed to complete successfully.
7834 **/
7835int
7836lpfc_sli4_queue_create(struct lpfc_hba *phba)
7837{
7838 struct lpfc_queue *qdesc;
James Smart2d7dbc42017-02-12 13:52:35 -08007839 int idx, io_channel, max;
James Smart5350d872011-10-10 21:33:49 -04007840
7841 /*
James Smart67d12732012-08-03 12:36:13 -04007842 * Create HBA Record arrays.
James Smart895427b2017-02-12 13:52:30 -08007843 * Both NVME and FCP will share that same vectors / EQs
James Smart5350d872011-10-10 21:33:49 -04007844 */
James Smart895427b2017-02-12 13:52:30 -08007845 io_channel = phba->io_channel_irqs;
7846 if (!io_channel)
James Smart67d12732012-08-03 12:36:13 -04007847 return -ERANGE;
James Smart5350d872011-10-10 21:33:49 -04007848
James Smart67d12732012-08-03 12:36:13 -04007849 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
7850 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
7851 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
7852 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
7853 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
7854 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
James Smart895427b2017-02-12 13:52:30 -08007855 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
7856 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
7857 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
7858 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
James Smart67d12732012-08-03 12:36:13 -04007859
James Smart895427b2017-02-12 13:52:30 -08007860 phba->sli4_hba.hba_eq = kcalloc(io_channel,
7861 sizeof(struct lpfc_queue *),
7862 GFP_KERNEL);
James Smart67d12732012-08-03 12:36:13 -04007863 if (!phba->sli4_hba.hba_eq) {
James Smartda0436e2009-05-22 14:51:39 -04007864 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04007865 "2576 Failed allocate memory for "
7866 "fast-path EQ record array\n");
James Smartda0436e2009-05-22 14:51:39 -04007867 goto out_error;
7868 }
James Smart67d12732012-08-03 12:36:13 -04007869
James Smart895427b2017-02-12 13:52:30 -08007870 if (phba->cfg_fcp_io_channel) {
7871 phba->sli4_hba.fcp_cq = kcalloc(phba->cfg_fcp_io_channel,
7872 sizeof(struct lpfc_queue *),
7873 GFP_KERNEL);
7874 if (!phba->sli4_hba.fcp_cq) {
7875 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7876 "2577 Failed allocate memory for "
7877 "fast-path CQ record array\n");
7878 goto out_error;
7879 }
7880 phba->sli4_hba.fcp_wq = kcalloc(phba->cfg_fcp_io_channel,
7881 sizeof(struct lpfc_queue *),
7882 GFP_KERNEL);
7883 if (!phba->sli4_hba.fcp_wq) {
7884 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7885 "2578 Failed allocate memory for "
7886 "fast-path FCP WQ record array\n");
7887 goto out_error;
7888 }
7889 /*
7890 * Since the first EQ can have multiple CQs associated with it,
7891 * this array is used to quickly see if we have a FCP fast-path
7892 * CQ match.
7893 */
7894 phba->sli4_hba.fcp_cq_map = kcalloc(phba->cfg_fcp_io_channel,
7895 sizeof(uint16_t),
7896 GFP_KERNEL);
7897 if (!phba->sli4_hba.fcp_cq_map) {
7898 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7899 "2545 Failed allocate memory for "
7900 "fast-path CQ map\n");
7901 goto out_error;
7902 }
James Smart67d12732012-08-03 12:36:13 -04007903 }
7904
James Smart895427b2017-02-12 13:52:30 -08007905 if (phba->cfg_nvme_io_channel) {
7906 phba->sli4_hba.nvme_cq = kcalloc(phba->cfg_nvme_io_channel,
7907 sizeof(struct lpfc_queue *),
7908 GFP_KERNEL);
7909 if (!phba->sli4_hba.nvme_cq) {
7910 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7911 "6077 Failed allocate memory for "
7912 "fast-path CQ record array\n");
7913 goto out_error;
7914 }
7915
James Smart895427b2017-02-12 13:52:30 -08007916 phba->sli4_hba.nvme_wq = kcalloc(phba->cfg_nvme_io_channel,
7917 sizeof(struct lpfc_queue *),
7918 GFP_KERNEL);
7919 if (!phba->sli4_hba.nvme_wq) {
7920 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7921 "2581 Failed allocate memory for "
7922 "fast-path NVME WQ record array\n");
7923 goto out_error;
7924 }
7925
7926 /*
7927 * Since the first EQ can have multiple CQs associated with it,
7928 * this array is used to quickly see if we have a NVME fast-path
7929 * CQ match.
7930 */
7931 phba->sli4_hba.nvme_cq_map = kcalloc(phba->cfg_nvme_io_channel,
7932 sizeof(uint16_t),
7933 GFP_KERNEL);
7934 if (!phba->sli4_hba.nvme_cq_map) {
7935 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7936 "6078 Failed allocate memory for "
7937 "fast-path CQ map\n");
7938 goto out_error;
7939 }
James Smart2d7dbc42017-02-12 13:52:35 -08007940
7941 if (phba->nvmet_support) {
7942 phba->sli4_hba.nvmet_cqset = kcalloc(
7943 phba->cfg_nvmet_mrq,
7944 sizeof(struct lpfc_queue *),
7945 GFP_KERNEL);
7946 if (!phba->sli4_hba.nvmet_cqset) {
7947 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7948 "3121 Fail allocate memory for "
7949 "fast-path CQ set array\n");
7950 goto out_error;
7951 }
7952 phba->sli4_hba.nvmet_mrq_hdr = kcalloc(
7953 phba->cfg_nvmet_mrq,
7954 sizeof(struct lpfc_queue *),
7955 GFP_KERNEL);
7956 if (!phba->sli4_hba.nvmet_mrq_hdr) {
7957 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7958 "3122 Fail allocate memory for "
7959 "fast-path RQ set hdr array\n");
7960 goto out_error;
7961 }
7962 phba->sli4_hba.nvmet_mrq_data = kcalloc(
7963 phba->cfg_nvmet_mrq,
7964 sizeof(struct lpfc_queue *),
7965 GFP_KERNEL);
7966 if (!phba->sli4_hba.nvmet_mrq_data) {
7967 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7968 "3124 Fail allocate memory for "
7969 "fast-path RQ set data array\n");
7970 goto out_error;
7971 }
7972 }
James Smart67d12732012-08-03 12:36:13 -04007973 }
James Smartda0436e2009-05-22 14:51:39 -04007974
James Smart895427b2017-02-12 13:52:30 -08007975 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
James Smart67d12732012-08-03 12:36:13 -04007976
James Smart895427b2017-02-12 13:52:30 -08007977 /* Create HBA Event Queues (EQs) */
7978 for (idx = 0; idx < io_channel; idx++) {
James Smart67d12732012-08-03 12:36:13 -04007979 /* Create EQs */
James Smartda0436e2009-05-22 14:51:39 -04007980 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
7981 phba->sli4_hba.eq_ecount);
7982 if (!qdesc) {
7983 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04007984 "0497 Failed allocate EQ (%d)\n", idx);
7985 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007986 }
James Smart67d12732012-08-03 12:36:13 -04007987 phba->sli4_hba.hba_eq[idx] = qdesc;
James Smartda0436e2009-05-22 14:51:39 -04007988 }
7989
James Smart895427b2017-02-12 13:52:30 -08007990 /* FCP and NVME io channels are not required to be balanced */
7991
7992 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++)
7993 if (lpfc_alloc_fcp_wq_cq(phba, idx))
7994 goto out_error;
7995
7996 for (idx = 0; idx < phba->cfg_nvme_io_channel; idx++)
7997 if (lpfc_alloc_nvme_wq_cq(phba, idx))
7998 goto out_error;
James Smart67d12732012-08-03 12:36:13 -04007999
James Smart2d7dbc42017-02-12 13:52:35 -08008000 /* allocate MRQ CQs */
8001 max = phba->cfg_nvme_io_channel;
8002 if (max < phba->cfg_nvmet_mrq)
8003 max = phba->cfg_nvmet_mrq;
8004
8005 for (idx = 0; idx < max; idx++)
8006 if (lpfc_alloc_nvme_wq_cq(phba, idx))
8007 goto out_error;
8008
8009 if (phba->nvmet_support) {
8010 for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
8011 qdesc = lpfc_sli4_queue_alloc(phba,
8012 phba->sli4_hba.cq_esize,
8013 phba->sli4_hba.cq_ecount);
8014 if (!qdesc) {
8015 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8016 "3142 Failed allocate NVME "
8017 "CQ Set (%d)\n", idx);
8018 goto out_error;
8019 }
8020 phba->sli4_hba.nvmet_cqset[idx] = qdesc;
8021 }
8022 }
8023
James Smartda0436e2009-05-22 14:51:39 -04008024 /*
James Smart67d12732012-08-03 12:36:13 -04008025 * Create Slow Path Completion Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04008026 */
8027
James Smartda0436e2009-05-22 14:51:39 -04008028 /* Create slow-path Mailbox Command Complete Queue */
8029 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
8030 phba->sli4_hba.cq_ecount);
8031 if (!qdesc) {
8032 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8033 "0500 Failed allocate slow-path mailbox CQ\n");
James Smart67d12732012-08-03 12:36:13 -04008034 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008035 }
8036 phba->sli4_hba.mbx_cq = qdesc;
8037
8038 /* Create slow-path ELS Complete Queue */
8039 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
8040 phba->sli4_hba.cq_ecount);
8041 if (!qdesc) {
8042 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8043 "0501 Failed allocate slow-path ELS CQ\n");
James Smart67d12732012-08-03 12:36:13 -04008044 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008045 }
8046 phba->sli4_hba.els_cq = qdesc;
8047
James Smartda0436e2009-05-22 14:51:39 -04008048
James Smart5350d872011-10-10 21:33:49 -04008049 /*
James Smart67d12732012-08-03 12:36:13 -04008050 * Create Slow Path Work Queues (WQs)
James Smart5350d872011-10-10 21:33:49 -04008051 */
James Smartda0436e2009-05-22 14:51:39 -04008052
8053 /* Create Mailbox Command Queue */
James Smartda0436e2009-05-22 14:51:39 -04008054
8055 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
8056 phba->sli4_hba.mq_ecount);
8057 if (!qdesc) {
8058 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8059 "0505 Failed allocate slow-path MQ\n");
James Smart67d12732012-08-03 12:36:13 -04008060 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008061 }
8062 phba->sli4_hba.mbx_wq = qdesc;
8063
8064 /*
James Smart67d12732012-08-03 12:36:13 -04008065 * Create ELS Work Queues
James Smartda0436e2009-05-22 14:51:39 -04008066 */
James Smartda0436e2009-05-22 14:51:39 -04008067
8068 /* Create slow-path ELS Work Queue */
8069 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
8070 phba->sli4_hba.wq_ecount);
8071 if (!qdesc) {
8072 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8073 "0504 Failed allocate slow-path ELS WQ\n");
James Smart67d12732012-08-03 12:36:13 -04008074 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008075 }
8076 phba->sli4_hba.els_wq = qdesc;
James Smart895427b2017-02-12 13:52:30 -08008077 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8078
8079 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
8080 /* Create NVME LS Complete Queue */
8081 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
8082 phba->sli4_hba.cq_ecount);
8083 if (!qdesc) {
8084 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8085 "6079 Failed allocate NVME LS CQ\n");
8086 goto out_error;
8087 }
8088 phba->sli4_hba.nvmels_cq = qdesc;
8089
8090 /* Create NVME LS Work Queue */
8091 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
8092 phba->sli4_hba.wq_ecount);
8093 if (!qdesc) {
8094 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8095 "6080 Failed allocate NVME LS WQ\n");
8096 goto out_error;
8097 }
8098 phba->sli4_hba.nvmels_wq = qdesc;
8099 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8100 }
James Smartda0436e2009-05-22 14:51:39 -04008101
James Smartda0436e2009-05-22 14:51:39 -04008102 /*
8103 * Create Receive Queue (RQ)
8104 */
James Smartda0436e2009-05-22 14:51:39 -04008105
8106 /* Create Receive Queue for header */
8107 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
8108 phba->sli4_hba.rq_ecount);
8109 if (!qdesc) {
8110 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8111 "0506 Failed allocate receive HRQ\n");
James Smart67d12732012-08-03 12:36:13 -04008112 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008113 }
8114 phba->sli4_hba.hdr_rq = qdesc;
8115
8116 /* Create Receive Queue for data */
8117 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
8118 phba->sli4_hba.rq_ecount);
8119 if (!qdesc) {
8120 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8121 "0507 Failed allocate receive DRQ\n");
James Smart67d12732012-08-03 12:36:13 -04008122 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04008123 }
8124 phba->sli4_hba.dat_rq = qdesc;
8125
James Smart2d7dbc42017-02-12 13:52:35 -08008126 if (phba->nvmet_support) {
8127 for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
8128 /* Create NVMET Receive Queue for header */
8129 qdesc = lpfc_sli4_queue_alloc(phba,
8130 phba->sli4_hba.rq_esize,
8131 phba->sli4_hba.rq_ecount);
8132 if (!qdesc) {
8133 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8134 "3146 Failed allocate "
8135 "receive HRQ\n");
8136 goto out_error;
8137 }
8138 phba->sli4_hba.nvmet_mrq_hdr[idx] = qdesc;
8139
8140 /* Only needed for header of RQ pair */
8141 qdesc->rqbp = kzalloc(sizeof(struct lpfc_rqb),
8142 GFP_KERNEL);
8143 if (qdesc->rqbp == NULL) {
8144 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8145 "6131 Failed allocate "
8146 "Header RQBP\n");
8147 goto out_error;
8148 }
8149
8150 /* Create NVMET Receive Queue for data */
8151 qdesc = lpfc_sli4_queue_alloc(phba,
8152 phba->sli4_hba.rq_esize,
8153 phba->sli4_hba.rq_ecount);
8154 if (!qdesc) {
8155 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8156 "3156 Failed allocate "
8157 "receive DRQ\n");
8158 goto out_error;
8159 }
8160 phba->sli4_hba.nvmet_mrq_data[idx] = qdesc;
8161 }
8162 }
8163
James Smart1ba981f2014-02-20 09:56:45 -05008164 /* Create the Queues needed for Flash Optimized Fabric operations */
8165 if (phba->cfg_fof)
8166 lpfc_fof_queue_create(phba);
James Smartda0436e2009-05-22 14:51:39 -04008167 return 0;
8168
James Smartda0436e2009-05-22 14:51:39 -04008169out_error:
James Smart67d12732012-08-03 12:36:13 -04008170 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04008171 return -ENOMEM;
8172}
8173
James Smart895427b2017-02-12 13:52:30 -08008174static inline void
8175__lpfc_sli4_release_queue(struct lpfc_queue **qp)
8176{
8177 if (*qp != NULL) {
8178 lpfc_sli4_queue_free(*qp);
8179 *qp = NULL;
8180 }
8181}
8182
8183static inline void
8184lpfc_sli4_release_queues(struct lpfc_queue ***qs, int max)
8185{
8186 int idx;
8187
8188 if (*qs == NULL)
8189 return;
8190
8191 for (idx = 0; idx < max; idx++)
8192 __lpfc_sli4_release_queue(&(*qs)[idx]);
8193
8194 kfree(*qs);
8195 *qs = NULL;
8196}
8197
8198static inline void
8199lpfc_sli4_release_queue_map(uint16_t **qmap)
8200{
8201 if (*qmap != NULL) {
8202 kfree(*qmap);
8203 *qmap = NULL;
8204 }
8205}
8206
James Smartda0436e2009-05-22 14:51:39 -04008207/**
8208 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
8209 * @phba: pointer to lpfc hba data structure.
8210 *
8211 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
8212 * operation.
8213 *
8214 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008215 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008216 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04008217 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04008218 **/
James Smart5350d872011-10-10 21:33:49 -04008219void
James Smartda0436e2009-05-22 14:51:39 -04008220lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
8221{
James Smart1ba981f2014-02-20 09:56:45 -05008222 if (phba->cfg_fof)
8223 lpfc_fof_queue_destroy(phba);
8224
James Smart895427b2017-02-12 13:52:30 -08008225 /* Release HBA eqs */
8226 lpfc_sli4_release_queues(&phba->sli4_hba.hba_eq, phba->io_channel_irqs);
James Smart67d12732012-08-03 12:36:13 -04008227
James Smart895427b2017-02-12 13:52:30 -08008228 /* Release FCP cqs */
8229 lpfc_sli4_release_queues(&phba->sli4_hba.fcp_cq,
8230 phba->cfg_fcp_io_channel);
James Smart67d12732012-08-03 12:36:13 -04008231
James Smart895427b2017-02-12 13:52:30 -08008232 /* Release FCP wqs */
8233 lpfc_sli4_release_queues(&phba->sli4_hba.fcp_wq,
8234 phba->cfg_fcp_io_channel);
James Smart67d12732012-08-03 12:36:13 -04008235
8236 /* Release FCP CQ mapping array */
James Smart895427b2017-02-12 13:52:30 -08008237 lpfc_sli4_release_queue_map(&phba->sli4_hba.fcp_cq_map);
8238
8239 /* Release NVME cqs */
8240 lpfc_sli4_release_queues(&phba->sli4_hba.nvme_cq,
8241 phba->cfg_nvme_io_channel);
8242
8243 /* Release NVME wqs */
8244 lpfc_sli4_release_queues(&phba->sli4_hba.nvme_wq,
8245 phba->cfg_nvme_io_channel);
8246
8247 /* Release NVME CQ mapping array */
8248 lpfc_sli4_release_queue_map(&phba->sli4_hba.nvme_cq_map);
James Smartda0436e2009-05-22 14:51:39 -04008249
James Smart2d7dbc42017-02-12 13:52:35 -08008250 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_cqset,
8251 phba->cfg_nvmet_mrq);
8252
8253 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_hdr,
8254 phba->cfg_nvmet_mrq);
8255 lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_data,
8256 phba->cfg_nvmet_mrq);
8257
James Smartda0436e2009-05-22 14:51:39 -04008258 /* Release mailbox command work queue */
James Smart895427b2017-02-12 13:52:30 -08008259 __lpfc_sli4_release_queue(&phba->sli4_hba.mbx_wq);
James Smartda0436e2009-05-22 14:51:39 -04008260
8261 /* Release ELS work queue */
James Smart895427b2017-02-12 13:52:30 -08008262 __lpfc_sli4_release_queue(&phba->sli4_hba.els_wq);
8263
8264 /* Release ELS work queue */
8265 __lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_wq);
James Smartda0436e2009-05-22 14:51:39 -04008266
8267 /* Release unsolicited receive queue */
James Smart895427b2017-02-12 13:52:30 -08008268 __lpfc_sli4_release_queue(&phba->sli4_hba.hdr_rq);
8269 __lpfc_sli4_release_queue(&phba->sli4_hba.dat_rq);
James Smartda0436e2009-05-22 14:51:39 -04008270
James Smartda0436e2009-05-22 14:51:39 -04008271 /* Release ELS complete queue */
James Smart895427b2017-02-12 13:52:30 -08008272 __lpfc_sli4_release_queue(&phba->sli4_hba.els_cq);
8273
8274 /* Release NVME LS complete queue */
8275 __lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_cq);
James Smartda0436e2009-05-22 14:51:39 -04008276
8277 /* Release mailbox command complete queue */
James Smart895427b2017-02-12 13:52:30 -08008278 __lpfc_sli4_release_queue(&phba->sli4_hba.mbx_cq);
8279
8280 /* Everything on this list has been freed */
8281 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
8282}
8283
8284int
8285lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
8286 struct lpfc_queue *drq, int count)
8287{
8288 int rc, i;
8289 struct lpfc_rqe hrqe;
8290 struct lpfc_rqe drqe;
8291 struct lpfc_rqb *rqbp;
8292 struct rqb_dmabuf *rqb_buffer;
8293 LIST_HEAD(rqb_buf_list);
8294
8295 rqbp = hrq->rqbp;
8296 for (i = 0; i < count; i++) {
8297 rqb_buffer = (rqbp->rqb_alloc_buffer)(phba);
8298 if (!rqb_buffer)
8299 break;
8300 rqb_buffer->hrq = hrq;
8301 rqb_buffer->drq = drq;
8302 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
8303 }
8304 while (!list_empty(&rqb_buf_list)) {
8305 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
8306 hbuf.list);
8307
8308 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
8309 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
8310 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
8311 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
8312 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
8313 if (rc < 0) {
8314 (rqbp->rqb_free_buffer)(phba, rqb_buffer);
8315 } else {
8316 list_add_tail(&rqb_buffer->hbuf.list,
8317 &rqbp->rqb_buffer_list);
8318 rqbp->buffer_count++;
8319 }
8320 }
8321 return 1;
8322}
8323
8324int
8325lpfc_free_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *rq)
8326{
8327 struct lpfc_rqb *rqbp;
8328 struct lpfc_dmabuf *h_buf;
8329 struct rqb_dmabuf *rqb_buffer;
8330
8331 rqbp = rq->rqbp;
8332 while (!list_empty(&rqbp->rqb_buffer_list)) {
8333 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
8334 struct lpfc_dmabuf, list);
8335
8336 rqb_buffer = container_of(h_buf, struct rqb_dmabuf, hbuf);
8337 (rqbp->rqb_free_buffer)(phba, rqb_buffer);
8338 rqbp->buffer_count--;
8339 }
8340 return 1;
8341}
8342
8343static int
8344lpfc_create_wq_cq(struct lpfc_hba *phba, struct lpfc_queue *eq,
8345 struct lpfc_queue *cq, struct lpfc_queue *wq, uint16_t *cq_map,
8346 int qidx, uint32_t qtype)
8347{
8348 struct lpfc_sli_ring *pring;
8349 int rc;
8350
8351 if (!eq || !cq || !wq) {
8352 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8353 "6085 Fast-path %s (%d) not allocated\n",
8354 ((eq) ? ((cq) ? "WQ" : "CQ") : "EQ"), qidx);
8355 return -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04008356 }
James Smartda0436e2009-05-22 14:51:39 -04008357
James Smart895427b2017-02-12 13:52:30 -08008358 /* create the Cq first */
8359 rc = lpfc_cq_create(phba, cq, eq,
8360 (qtype == LPFC_MBOX) ? LPFC_MCQ : LPFC_WCQ, qtype);
8361 if (rc) {
8362 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8363 "6086 Failed setup of CQ (%d), rc = 0x%x\n",
8364 qidx, (uint32_t)rc);
8365 return rc;
8366 }
8367
8368 if (qtype != LPFC_MBOX) {
8369 /* Setup nvme_cq_map for fast lookup */
8370 if (cq_map)
8371 *cq_map = cq->queue_id;
8372
8373 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8374 "6087 CQ setup: cq[%d]-id=%d, parent eq[%d]-id=%d\n",
8375 qidx, cq->queue_id, qidx, eq->queue_id);
8376
8377 /* create the wq */
8378 rc = lpfc_wq_create(phba, wq, cq, qtype);
8379 if (rc) {
8380 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8381 "6123 Fail setup fastpath WQ (%d), rc = 0x%x\n",
8382 qidx, (uint32_t)rc);
8383 /* no need to tear down cq - caller will do so */
8384 return rc;
8385 }
8386
8387 /* Bind this CQ/WQ to the NVME ring */
8388 pring = wq->pring;
8389 pring->sli.sli4.wqp = (void *)wq;
8390 cq->pring = pring;
8391
8392 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8393 "2593 WQ setup: wq[%d]-id=%d assoc=%d, cq[%d]-id=%d\n",
8394 qidx, wq->queue_id, wq->assoc_qid, qidx, cq->queue_id);
8395 } else {
8396 rc = lpfc_mq_create(phba, wq, cq, LPFC_MBOX);
8397 if (rc) {
8398 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8399 "0539 Failed setup of slow-path MQ: "
8400 "rc = 0x%x\n", rc);
8401 /* no need to tear down cq - caller will do so */
8402 return rc;
8403 }
8404
8405 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8406 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
8407 phba->sli4_hba.mbx_wq->queue_id,
8408 phba->sli4_hba.mbx_cq->queue_id);
8409 }
8410
8411 return 0;
James Smartda0436e2009-05-22 14:51:39 -04008412}
8413
8414/**
8415 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
8416 * @phba: pointer to lpfc hba data structure.
8417 *
8418 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
8419 * operation.
8420 *
8421 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008422 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008423 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04008424 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04008425 **/
8426int
8427lpfc_sli4_queue_setup(struct lpfc_hba *phba)
8428{
James Smart962bc512013-01-03 15:44:00 -05008429 uint32_t shdr_status, shdr_add_status;
8430 union lpfc_sli4_cfg_shdr *shdr;
8431 LPFC_MBOXQ_t *mboxq;
James Smart895427b2017-02-12 13:52:30 -08008432 int qidx;
8433 uint32_t length, io_channel;
8434 int rc = -ENOMEM;
James Smart962bc512013-01-03 15:44:00 -05008435
8436 /* Check for dual-ULP support */
8437 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8438 if (!mboxq) {
8439 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8440 "3249 Unable to allocate memory for "
8441 "QUERY_FW_CFG mailbox command\n");
8442 return -ENOMEM;
8443 }
8444 length = (sizeof(struct lpfc_mbx_query_fw_config) -
8445 sizeof(struct lpfc_sli4_cfg_mhdr));
8446 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
8447 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
8448 length, LPFC_SLI4_MBX_EMBED);
8449
8450 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8451
8452 shdr = (union lpfc_sli4_cfg_shdr *)
8453 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
8454 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
8455 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
8456 if (shdr_status || shdr_add_status || rc) {
8457 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8458 "3250 QUERY_FW_CFG mailbox failed with status "
8459 "x%x add_status x%x, mbx status x%x\n",
8460 shdr_status, shdr_add_status, rc);
8461 if (rc != MBX_TIMEOUT)
8462 mempool_free(mboxq, phba->mbox_mem_pool);
8463 rc = -ENXIO;
8464 goto out_error;
8465 }
8466
8467 phba->sli4_hba.fw_func_mode =
8468 mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
8469 phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
8470 phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
James Smart8b017a32015-05-21 13:55:18 -04008471 phba->sli4_hba.physical_port =
8472 mboxq->u.mqe.un.query_fw_cfg.rsp.physical_port;
James Smart962bc512013-01-03 15:44:00 -05008473 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8474 "3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
8475 "ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
8476 phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
8477
8478 if (rc != MBX_TIMEOUT)
8479 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04008480
8481 /*
James Smart67d12732012-08-03 12:36:13 -04008482 * Set up HBA Event Queues (EQs)
James Smartda0436e2009-05-22 14:51:39 -04008483 */
James Smart895427b2017-02-12 13:52:30 -08008484 io_channel = phba->io_channel_irqs;
James Smartda0436e2009-05-22 14:51:39 -04008485
James Smart67d12732012-08-03 12:36:13 -04008486 /* Set up HBA event queue */
James Smart895427b2017-02-12 13:52:30 -08008487 if (io_channel && !phba->sli4_hba.hba_eq) {
James Smart2e90f4b2011-12-13 13:22:37 -05008488 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8489 "3147 Fast-path EQs not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05008490 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04008491 goto out_error;
James Smart2e90f4b2011-12-13 13:22:37 -05008492 }
James Smart895427b2017-02-12 13:52:30 -08008493 for (qidx = 0; qidx < io_channel; qidx++) {
8494 if (!phba->sli4_hba.hba_eq[qidx]) {
James Smartda0436e2009-05-22 14:51:39 -04008495 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8496 "0522 Fast-path EQ (%d) not "
James Smart895427b2017-02-12 13:52:30 -08008497 "allocated\n", qidx);
James Smart1b511972011-12-13 13:23:09 -05008498 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08008499 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008500 }
James Smart895427b2017-02-12 13:52:30 -08008501 rc = lpfc_eq_create(phba, phba->sli4_hba.hba_eq[qidx],
8502 phba->cfg_fcp_imax);
James Smartda0436e2009-05-22 14:51:39 -04008503 if (rc) {
8504 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8505 "0523 Failed setup of fast-path EQ "
James Smart895427b2017-02-12 13:52:30 -08008506 "(%d), rc = 0x%x\n", qidx,
James Smarta2fc4aef2014-09-03 12:57:55 -04008507 (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -08008508 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008509 }
8510 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008511 "2584 HBA EQ setup: queue[%d]-id=%d\n",
8512 qidx, phba->sli4_hba.hba_eq[qidx]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04008513 }
8514
James Smart895427b2017-02-12 13:52:30 -08008515 if (phba->cfg_nvme_io_channel) {
8516 if (!phba->sli4_hba.nvme_cq || !phba->sli4_hba.nvme_wq) {
James Smart67d12732012-08-03 12:36:13 -04008517 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008518 "6084 Fast-path NVME %s array not allocated\n",
8519 (phba->sli4_hba.nvme_cq) ? "CQ" : "WQ");
James Smart67d12732012-08-03 12:36:13 -04008520 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08008521 goto out_destroy;
James Smart67d12732012-08-03 12:36:13 -04008522 }
8523
James Smart895427b2017-02-12 13:52:30 -08008524 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++) {
8525 rc = lpfc_create_wq_cq(phba,
8526 phba->sli4_hba.hba_eq[
8527 qidx % io_channel],
8528 phba->sli4_hba.nvme_cq[qidx],
8529 phba->sli4_hba.nvme_wq[qidx],
8530 &phba->sli4_hba.nvme_cq_map[qidx],
8531 qidx, LPFC_NVME);
8532 if (rc) {
8533 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8534 "6123 Failed to setup fastpath "
8535 "NVME WQ/CQ (%d), rc = 0x%x\n",
8536 qidx, (uint32_t)rc);
8537 goto out_destroy;
8538 }
8539 }
James Smart67d12732012-08-03 12:36:13 -04008540 }
8541
James Smart895427b2017-02-12 13:52:30 -08008542 if (phba->cfg_fcp_io_channel) {
8543 /* Set up fast-path FCP Response Complete Queue */
8544 if (!phba->sli4_hba.fcp_cq || !phba->sli4_hba.fcp_wq) {
James Smart67d12732012-08-03 12:36:13 -04008545 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008546 "3148 Fast-path FCP %s array not allocated\n",
8547 phba->sli4_hba.fcp_cq ? "WQ" : "CQ");
James Smart67d12732012-08-03 12:36:13 -04008548 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08008549 goto out_destroy;
James Smart67d12732012-08-03 12:36:13 -04008550 }
8551
James Smart895427b2017-02-12 13:52:30 -08008552 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++) {
8553 rc = lpfc_create_wq_cq(phba,
8554 phba->sli4_hba.hba_eq[
8555 qidx % io_channel],
8556 phba->sli4_hba.fcp_cq[qidx],
8557 phba->sli4_hba.fcp_wq[qidx],
8558 &phba->sli4_hba.fcp_cq_map[qidx],
8559 qidx, LPFC_FCP);
8560 if (rc) {
8561 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8562 "0535 Failed to setup fastpath "
8563 "FCP WQ/CQ (%d), rc = 0x%x\n",
8564 qidx, (uint32_t)rc);
8565 goto out_destroy;
8566 }
8567 }
James Smart67d12732012-08-03 12:36:13 -04008568 }
James Smartda0436e2009-05-22 14:51:39 -04008569
8570 /*
James Smart895427b2017-02-12 13:52:30 -08008571 * Set up Slow Path Complete Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04008572 */
8573
James Smart895427b2017-02-12 13:52:30 -08008574 /* Set up slow-path MBOX CQ/MQ */
8575
8576 if (!phba->sli4_hba.mbx_cq || !phba->sli4_hba.mbx_wq) {
James Smartda0436e2009-05-22 14:51:39 -04008577 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008578 "0528 %s not allocated\n",
8579 phba->sli4_hba.mbx_cq ?
8580 "Mailbox WQ" : "Mailbox CQ");
James Smart1b511972011-12-13 13:23:09 -05008581 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08008582 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008583 }
James Smart895427b2017-02-12 13:52:30 -08008584
8585 rc = lpfc_create_wq_cq(phba, phba->sli4_hba.hba_eq[0],
8586 phba->sli4_hba.mbx_cq,
8587 phba->sli4_hba.mbx_wq,
8588 NULL, 0, LPFC_MBOX);
James Smartda0436e2009-05-22 14:51:39 -04008589 if (rc) {
8590 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008591 "0529 Failed setup of mailbox WQ/CQ: rc = 0x%x\n",
8592 (uint32_t)rc);
8593 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008594 }
James Smart2d7dbc42017-02-12 13:52:35 -08008595 if (phba->nvmet_support) {
8596 if (!phba->sli4_hba.nvmet_cqset) {
8597 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8598 "3165 Fast-path NVME CQ Set "
8599 "array not allocated\n");
8600 rc = -ENOMEM;
8601 goto out_destroy;
8602 }
8603 if (phba->cfg_nvmet_mrq > 1) {
8604 rc = lpfc_cq_create_set(phba,
8605 phba->sli4_hba.nvmet_cqset,
8606 phba->sli4_hba.hba_eq,
8607 LPFC_WCQ, LPFC_NVMET);
8608 if (rc) {
8609 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8610 "3164 Failed setup of NVME CQ "
8611 "Set, rc = 0x%x\n",
8612 (uint32_t)rc);
8613 goto out_destroy;
8614 }
8615 } else {
8616 /* Set up NVMET Receive Complete Queue */
8617 rc = lpfc_cq_create(phba, phba->sli4_hba.nvmet_cqset[0],
8618 phba->sli4_hba.hba_eq[0],
8619 LPFC_WCQ, LPFC_NVMET);
8620 if (rc) {
8621 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8622 "6089 Failed setup NVMET CQ: "
8623 "rc = 0x%x\n", (uint32_t)rc);
8624 goto out_destroy;
8625 }
8626 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8627 "6090 NVMET CQ setup: cq-id=%d, "
8628 "parent eq-id=%d\n",
8629 phba->sli4_hba.nvmet_cqset[0]->queue_id,
8630 phba->sli4_hba.hba_eq[0]->queue_id);
8631 }
8632 }
James Smartda0436e2009-05-22 14:51:39 -04008633
James Smart895427b2017-02-12 13:52:30 -08008634 /* Set up slow-path ELS WQ/CQ */
8635 if (!phba->sli4_hba.els_cq || !phba->sli4_hba.els_wq) {
James Smartda0436e2009-05-22 14:51:39 -04008636 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008637 "0530 ELS %s not allocated\n",
8638 phba->sli4_hba.els_cq ? "WQ" : "CQ");
James Smart1b511972011-12-13 13:23:09 -05008639 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08008640 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008641 }
James Smart895427b2017-02-12 13:52:30 -08008642 rc = lpfc_create_wq_cq(phba, phba->sli4_hba.hba_eq[0],
8643 phba->sli4_hba.els_cq,
8644 phba->sli4_hba.els_wq,
8645 NULL, 0, LPFC_ELS);
James Smartda0436e2009-05-22 14:51:39 -04008646 if (rc) {
8647 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008648 "0529 Failed setup of ELS WQ/CQ: rc = 0x%x\n",
8649 (uint32_t)rc);
8650 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008651 }
8652 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8653 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
8654 phba->sli4_hba.els_wq->queue_id,
8655 phba->sli4_hba.els_cq->queue_id);
8656
James Smart895427b2017-02-12 13:52:30 -08008657 if (phba->cfg_nvme_io_channel) {
8658 /* Set up NVME LS Complete Queue */
8659 if (!phba->sli4_hba.nvmels_cq || !phba->sli4_hba.nvmels_wq) {
8660 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8661 "6091 LS %s not allocated\n",
8662 phba->sli4_hba.nvmels_cq ? "WQ" : "CQ");
8663 rc = -ENOMEM;
8664 goto out_destroy;
8665 }
8666 rc = lpfc_create_wq_cq(phba, phba->sli4_hba.hba_eq[0],
8667 phba->sli4_hba.nvmels_cq,
8668 phba->sli4_hba.nvmels_wq,
8669 NULL, 0, LPFC_NVME_LS);
8670 if (rc) {
8671 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8672 "0529 Failed setup of NVVME LS WQ/CQ: "
8673 "rc = 0x%x\n", (uint32_t)rc);
8674 goto out_destroy;
8675 }
8676
8677 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8678 "6096 ELS WQ setup: wq-id=%d, "
8679 "parent cq-id=%d\n",
8680 phba->sli4_hba.nvmels_wq->queue_id,
8681 phba->sli4_hba.nvmels_cq->queue_id);
8682 }
8683
James Smart2d7dbc42017-02-12 13:52:35 -08008684 /*
8685 * Create NVMET Receive Queue (RQ)
8686 */
8687 if (phba->nvmet_support) {
8688 if ((!phba->sli4_hba.nvmet_cqset) ||
8689 (!phba->sli4_hba.nvmet_mrq_hdr) ||
8690 (!phba->sli4_hba.nvmet_mrq_data)) {
8691 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8692 "6130 MRQ CQ Queues not "
8693 "allocated\n");
8694 rc = -ENOMEM;
8695 goto out_destroy;
8696 }
8697 if (phba->cfg_nvmet_mrq > 1) {
8698 rc = lpfc_mrq_create(phba,
8699 phba->sli4_hba.nvmet_mrq_hdr,
8700 phba->sli4_hba.nvmet_mrq_data,
8701 phba->sli4_hba.nvmet_cqset,
8702 LPFC_NVMET);
8703 if (rc) {
8704 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8705 "6098 Failed setup of NVMET "
8706 "MRQ: rc = 0x%x\n",
8707 (uint32_t)rc);
8708 goto out_destroy;
8709 }
8710
8711 } else {
8712 rc = lpfc_rq_create(phba,
8713 phba->sli4_hba.nvmet_mrq_hdr[0],
8714 phba->sli4_hba.nvmet_mrq_data[0],
8715 phba->sli4_hba.nvmet_cqset[0],
8716 LPFC_NVMET);
8717 if (rc) {
8718 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8719 "6057 Failed setup of NVMET "
8720 "Receive Queue: rc = 0x%x\n",
8721 (uint32_t)rc);
8722 goto out_destroy;
8723 }
8724
8725 lpfc_printf_log(
8726 phba, KERN_INFO, LOG_INIT,
8727 "6099 NVMET RQ setup: hdr-rq-id=%d, "
8728 "dat-rq-id=%d parent cq-id=%d\n",
8729 phba->sli4_hba.nvmet_mrq_hdr[0]->queue_id,
8730 phba->sli4_hba.nvmet_mrq_data[0]->queue_id,
8731 phba->sli4_hba.nvmet_cqset[0]->queue_id);
8732
8733 }
8734 }
8735
James Smartda0436e2009-05-22 14:51:39 -04008736 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
8737 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8738 "0540 Receive Queue not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05008739 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08008740 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008741 }
James Smart73d91e52011-10-10 21:32:10 -04008742
8743 lpfc_rq_adjust_repost(phba, phba->sli4_hba.hdr_rq, LPFC_ELS_HBQ);
8744 lpfc_rq_adjust_repost(phba, phba->sli4_hba.dat_rq, LPFC_ELS_HBQ);
8745
James Smartda0436e2009-05-22 14:51:39 -04008746 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04008747 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04008748 if (rc) {
8749 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8750 "0541 Failed setup of Receive Queue: "
James Smarta2fc4aef2014-09-03 12:57:55 -04008751 "rc = 0x%x\n", (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -08008752 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008753 }
James Smart73d91e52011-10-10 21:32:10 -04008754
James Smartda0436e2009-05-22 14:51:39 -04008755 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8756 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
8757 "parent cq-id=%d\n",
8758 phba->sli4_hba.hdr_rq->queue_id,
8759 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04008760 phba->sli4_hba.els_cq->queue_id);
James Smart1ba981f2014-02-20 09:56:45 -05008761
8762 if (phba->cfg_fof) {
8763 rc = lpfc_fof_queue_setup(phba);
8764 if (rc) {
8765 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8766 "0549 Failed setup of FOF Queues: "
8767 "rc = 0x%x\n", rc);
James Smart895427b2017-02-12 13:52:30 -08008768 goto out_destroy;
James Smart1ba981f2014-02-20 09:56:45 -05008769 }
8770 }
James Smart2c9c5a02015-04-07 15:07:15 -04008771
James Smart43140ca2017-03-04 09:30:34 -08008772 for (qidx = 0; qidx < io_channel; qidx += LPFC_MAX_EQ_DELAY_EQID_CNT)
James Smart895427b2017-02-12 13:52:30 -08008773 lpfc_modify_hba_eq_delay(phba, qidx);
James Smart43140ca2017-03-04 09:30:34 -08008774
James Smartda0436e2009-05-22 14:51:39 -04008775 return 0;
8776
James Smart895427b2017-02-12 13:52:30 -08008777out_destroy:
8778 lpfc_sli4_queue_unset(phba);
James Smartda0436e2009-05-22 14:51:39 -04008779out_error:
8780 return rc;
8781}
8782
8783/**
8784 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
8785 * @phba: pointer to lpfc hba data structure.
8786 *
8787 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
8788 * operation.
8789 *
8790 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008791 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008792 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04008793 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04008794 **/
8795void
8796lpfc_sli4_queue_unset(struct lpfc_hba *phba)
8797{
James Smart895427b2017-02-12 13:52:30 -08008798 int qidx;
James Smartda0436e2009-05-22 14:51:39 -04008799
James Smart1ba981f2014-02-20 09:56:45 -05008800 /* Unset the queues created for Flash Optimized Fabric operations */
8801 if (phba->cfg_fof)
8802 lpfc_fof_queue_destroy(phba);
James Smart895427b2017-02-12 13:52:30 -08008803
James Smartda0436e2009-05-22 14:51:39 -04008804 /* Unset mailbox command work queue */
James Smart895427b2017-02-12 13:52:30 -08008805 if (phba->sli4_hba.mbx_wq)
8806 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
8807
8808 /* Unset NVME LS work queue */
8809 if (phba->sli4_hba.nvmels_wq)
8810 lpfc_wq_destroy(phba, phba->sli4_hba.nvmels_wq);
8811
James Smartda0436e2009-05-22 14:51:39 -04008812 /* Unset ELS work queue */
James Smart895427b2017-02-12 13:52:30 -08008813 if (phba->sli4_hba.els_cq)
8814 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
8815
James Smartda0436e2009-05-22 14:51:39 -04008816 /* Unset unsolicited receive queue */
James Smart895427b2017-02-12 13:52:30 -08008817 if (phba->sli4_hba.hdr_rq)
8818 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq,
8819 phba->sli4_hba.dat_rq);
8820
James Smartda0436e2009-05-22 14:51:39 -04008821 /* Unset FCP work queue */
James Smart895427b2017-02-12 13:52:30 -08008822 if (phba->sli4_hba.fcp_wq)
8823 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
8824 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[qidx]);
8825
8826 /* Unset NVME work queue */
8827 if (phba->sli4_hba.nvme_wq) {
8828 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
8829 lpfc_wq_destroy(phba, phba->sli4_hba.nvme_wq[qidx]);
James Smart67d12732012-08-03 12:36:13 -04008830 }
James Smart895427b2017-02-12 13:52:30 -08008831
James Smartda0436e2009-05-22 14:51:39 -04008832 /* Unset mailbox command complete queue */
James Smart895427b2017-02-12 13:52:30 -08008833 if (phba->sli4_hba.mbx_cq)
8834 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
8835
James Smartda0436e2009-05-22 14:51:39 -04008836 /* Unset ELS complete queue */
James Smart895427b2017-02-12 13:52:30 -08008837 if (phba->sli4_hba.els_cq)
8838 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
8839
8840 /* Unset NVME LS complete queue */
8841 if (phba->sli4_hba.nvmels_cq)
8842 lpfc_cq_destroy(phba, phba->sli4_hba.nvmels_cq);
8843
8844 /* Unset NVME response complete queue */
8845 if (phba->sli4_hba.nvme_cq)
8846 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
8847 lpfc_cq_destroy(phba, phba->sli4_hba.nvme_cq[qidx]);
8848
James Smart2d7dbc42017-02-12 13:52:35 -08008849 /* Unset NVMET MRQ queue */
8850 if (phba->sli4_hba.nvmet_mrq_hdr) {
8851 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
8852 lpfc_rq_destroy(phba,
8853 phba->sli4_hba.nvmet_mrq_hdr[qidx],
8854 phba->sli4_hba.nvmet_mrq_data[qidx]);
8855 }
8856
8857 /* Unset NVMET CQ Set complete queue */
8858 if (phba->sli4_hba.nvmet_cqset) {
8859 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
8860 lpfc_cq_destroy(phba,
8861 phba->sli4_hba.nvmet_cqset[qidx]);
8862 }
8863
James Smartda0436e2009-05-22 14:51:39 -04008864 /* Unset FCP response complete queue */
James Smart895427b2017-02-12 13:52:30 -08008865 if (phba->sli4_hba.fcp_cq)
8866 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
8867 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[qidx]);
8868
James Smartda0436e2009-05-22 14:51:39 -04008869 /* Unset fast-path event queue */
James Smart895427b2017-02-12 13:52:30 -08008870 if (phba->sli4_hba.hba_eq)
8871 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
8872 lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[qidx]);
James Smartda0436e2009-05-22 14:51:39 -04008873}
8874
8875/**
8876 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
8877 * @phba: pointer to lpfc hba data structure.
8878 *
8879 * This routine is invoked to allocate and set up a pool of completion queue
8880 * events. The body of the completion queue event is a completion queue entry
8881 * CQE. For now, this pool is used for the interrupt service routine to queue
8882 * the following HBA completion queue events for the worker thread to process:
8883 * - Mailbox asynchronous events
8884 * - Receive queue completion unsolicited events
8885 * Later, this can be used for all the slow-path events.
8886 *
8887 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008888 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008889 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04008890 **/
8891static int
8892lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
8893{
8894 struct lpfc_cq_event *cq_event;
8895 int i;
8896
8897 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
8898 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
8899 if (!cq_event)
8900 goto out_pool_create_fail;
8901 list_add_tail(&cq_event->list,
8902 &phba->sli4_hba.sp_cqe_event_pool);
8903 }
8904 return 0;
8905
8906out_pool_create_fail:
8907 lpfc_sli4_cq_event_pool_destroy(phba);
8908 return -ENOMEM;
8909}
8910
8911/**
8912 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
8913 * @phba: pointer to lpfc hba data structure.
8914 *
8915 * This routine is invoked to free the pool of completion queue events at
8916 * driver unload time. Note that, it is the responsibility of the driver
8917 * cleanup routine to free all the outstanding completion-queue events
8918 * allocated from this pool back into the pool before invoking this routine
8919 * to destroy the pool.
8920 **/
8921static void
8922lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
8923{
8924 struct lpfc_cq_event *cq_event, *next_cq_event;
8925
8926 list_for_each_entry_safe(cq_event, next_cq_event,
8927 &phba->sli4_hba.sp_cqe_event_pool, list) {
8928 list_del(&cq_event->list);
8929 kfree(cq_event);
8930 }
8931}
8932
8933/**
8934 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
8935 * @phba: pointer to lpfc hba data structure.
8936 *
8937 * This routine is the lock free version of the API invoked to allocate a
8938 * completion-queue event from the free pool.
8939 *
8940 * Return: Pointer to the newly allocated completion-queue event if successful
8941 * NULL otherwise.
8942 **/
8943struct lpfc_cq_event *
8944__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
8945{
8946 struct lpfc_cq_event *cq_event = NULL;
8947
8948 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
8949 struct lpfc_cq_event, list);
8950 return cq_event;
8951}
8952
8953/**
8954 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
8955 * @phba: pointer to lpfc hba data structure.
8956 *
8957 * This routine is the lock version of the API invoked to allocate a
8958 * completion-queue event from the free pool.
8959 *
8960 * Return: Pointer to the newly allocated completion-queue event if successful
8961 * NULL otherwise.
8962 **/
8963struct lpfc_cq_event *
8964lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
8965{
8966 struct lpfc_cq_event *cq_event;
8967 unsigned long iflags;
8968
8969 spin_lock_irqsave(&phba->hbalock, iflags);
8970 cq_event = __lpfc_sli4_cq_event_alloc(phba);
8971 spin_unlock_irqrestore(&phba->hbalock, iflags);
8972 return cq_event;
8973}
8974
8975/**
8976 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
8977 * @phba: pointer to lpfc hba data structure.
8978 * @cq_event: pointer to the completion queue event to be freed.
8979 *
8980 * This routine is the lock free version of the API invoked to release a
8981 * completion-queue event back into the free pool.
8982 **/
8983void
8984__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
8985 struct lpfc_cq_event *cq_event)
8986{
8987 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
8988}
8989
8990/**
8991 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
8992 * @phba: pointer to lpfc hba data structure.
8993 * @cq_event: pointer to the completion queue event to be freed.
8994 *
8995 * This routine is the lock version of the API invoked to release a
8996 * completion-queue event back into the free pool.
8997 **/
8998void
8999lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
9000 struct lpfc_cq_event *cq_event)
9001{
9002 unsigned long iflags;
9003 spin_lock_irqsave(&phba->hbalock, iflags);
9004 __lpfc_sli4_cq_event_release(phba, cq_event);
9005 spin_unlock_irqrestore(&phba->hbalock, iflags);
9006}
9007
9008/**
9009 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
9010 * @phba: pointer to lpfc hba data structure.
9011 *
9012 * This routine is to free all the pending completion-queue events to the
9013 * back into the free pool for device reset.
9014 **/
9015static void
9016lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
9017{
9018 LIST_HEAD(cqelist);
9019 struct lpfc_cq_event *cqe;
9020 unsigned long iflags;
9021
9022 /* Retrieve all the pending WCQEs from pending WCQE lists */
9023 spin_lock_irqsave(&phba->hbalock, iflags);
9024 /* Pending FCP XRI abort events */
9025 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
9026 &cqelist);
9027 /* Pending ELS XRI abort events */
9028 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
9029 &cqelist);
James Smart318083a2017-03-04 09:30:30 -08009030 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
9031 /* Pending NVME XRI abort events */
9032 list_splice_init(&phba->sli4_hba.sp_nvme_xri_aborted_work_queue,
9033 &cqelist);
9034 }
James Smartda0436e2009-05-22 14:51:39 -04009035 /* Pending asynnc events */
9036 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
9037 &cqelist);
9038 spin_unlock_irqrestore(&phba->hbalock, iflags);
9039
9040 while (!list_empty(&cqelist)) {
9041 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
9042 lpfc_sli4_cq_event_release(phba, cqe);
9043 }
9044}
9045
9046/**
9047 * lpfc_pci_function_reset - Reset pci function.
9048 * @phba: pointer to lpfc hba data structure.
9049 *
9050 * This routine is invoked to request a PCI function reset. It will destroys
9051 * all resources assigned to the PCI function which originates this request.
9052 *
9053 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009054 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009055 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04009056 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04009057 **/
9058int
9059lpfc_pci_function_reset(struct lpfc_hba *phba)
9060{
9061 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05009062 uint32_t rc = 0, if_type;
James Smartda0436e2009-05-22 14:51:39 -04009063 uint32_t shdr_status, shdr_add_status;
James Smart2f6fa2c2014-09-03 12:57:08 -04009064 uint32_t rdy_chk;
9065 uint32_t port_reset = 0;
James Smartda0436e2009-05-22 14:51:39 -04009066 union lpfc_sli4_cfg_shdr *shdr;
James Smart2fcee4b2010-12-15 17:57:46 -05009067 struct lpfc_register reg_data;
James Smart2b81f942012-03-01 22:37:18 -05009068 uint16_t devid;
James Smartda0436e2009-05-22 14:51:39 -04009069
James Smart2fcee4b2010-12-15 17:57:46 -05009070 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9071 switch (if_type) {
9072 case LPFC_SLI_INTF_IF_TYPE_0:
9073 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
9074 GFP_KERNEL);
9075 if (!mboxq) {
9076 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9077 "0494 Unable to allocate memory for "
9078 "issuing SLI_FUNCTION_RESET mailbox "
9079 "command\n");
9080 return -ENOMEM;
9081 }
9082
9083 /* Setup PCI function reset mailbox-ioctl command */
9084 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
9085 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
9086 LPFC_SLI4_MBX_EMBED);
9087 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9088 shdr = (union lpfc_sli4_cfg_shdr *)
9089 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
9090 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9091 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
9092 &shdr->response);
9093 if (rc != MBX_TIMEOUT)
9094 mempool_free(mboxq, phba->mbox_mem_pool);
9095 if (shdr_status || shdr_add_status || rc) {
9096 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9097 "0495 SLI_FUNCTION_RESET mailbox "
9098 "failed with status x%x add_status x%x,"
9099 " mbx status x%x\n",
9100 shdr_status, shdr_add_status, rc);
9101 rc = -ENXIO;
9102 }
9103 break;
9104 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart2f6fa2c2014-09-03 12:57:08 -04009105wait:
9106 /*
9107 * Poll the Port Status Register and wait for RDY for
9108 * up to 30 seconds. If the port doesn't respond, treat
9109 * it as an error.
9110 */
James Smart77d093f2015-04-07 15:07:08 -04009111 for (rdy_chk = 0; rdy_chk < 1500; rdy_chk++) {
James Smart2f6fa2c2014-09-03 12:57:08 -04009112 if (lpfc_readl(phba->sli4_hba.u.if_type2.
9113 STATUSregaddr, &reg_data.word0)) {
9114 rc = -ENODEV;
9115 goto out;
9116 }
9117 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
9118 break;
9119 msleep(20);
9120 }
9121
9122 if (!bf_get(lpfc_sliport_status_rdy, &reg_data)) {
9123 phba->work_status[0] = readl(
9124 phba->sli4_hba.u.if_type2.ERR1regaddr);
9125 phba->work_status[1] = readl(
9126 phba->sli4_hba.u.if_type2.ERR2regaddr);
9127 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9128 "2890 Port not ready, port status reg "
9129 "0x%x error 1=0x%x, error 2=0x%x\n",
9130 reg_data.word0,
9131 phba->work_status[0],
9132 phba->work_status[1]);
9133 rc = -ENODEV;
9134 goto out;
9135 }
9136
9137 if (!port_reset) {
9138 /*
9139 * Reset the port now
9140 */
James Smart2fcee4b2010-12-15 17:57:46 -05009141 reg_data.word0 = 0;
9142 bf_set(lpfc_sliport_ctrl_end, &reg_data,
9143 LPFC_SLIPORT_LITTLE_ENDIAN);
9144 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
9145 LPFC_SLIPORT_INIT_PORT);
9146 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
9147 CTRLregaddr);
James Smart8fcb8ac2012-03-01 22:35:58 -05009148 /* flush */
James Smart2b81f942012-03-01 22:37:18 -05009149 pci_read_config_word(phba->pcidev,
9150 PCI_DEVICE_ID, &devid);
James Smart2fcee4b2010-12-15 17:57:46 -05009151
James Smart2f6fa2c2014-09-03 12:57:08 -04009152 port_reset = 1;
9153 msleep(20);
9154 goto wait;
9155 } else if (bf_get(lpfc_sliport_status_rn, &reg_data)) {
9156 rc = -ENODEV;
9157 goto out;
James Smart2fcee4b2010-12-15 17:57:46 -05009158 }
9159 break;
James Smart2f6fa2c2014-09-03 12:57:08 -04009160
James Smart2fcee4b2010-12-15 17:57:46 -05009161 case LPFC_SLI_INTF_IF_TYPE_1:
9162 default:
9163 break;
James Smartda0436e2009-05-22 14:51:39 -04009164 }
9165
James Smart73d91e52011-10-10 21:32:10 -04009166out:
James Smart2fcee4b2010-12-15 17:57:46 -05009167 /* Catch the not-ready port failure after a port reset. */
James Smart2f6fa2c2014-09-03 12:57:08 -04009168 if (rc) {
James Smart229adb02013-04-17 20:16:51 -04009169 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9170 "3317 HBA not functional: IP Reset Failed "
James Smart2f6fa2c2014-09-03 12:57:08 -04009171 "try: echo fw_reset > board_mode\n");
James Smart2fcee4b2010-12-15 17:57:46 -05009172 rc = -ENODEV;
James Smart229adb02013-04-17 20:16:51 -04009173 }
James Smart2fcee4b2010-12-15 17:57:46 -05009174
James Smartda0436e2009-05-22 14:51:39 -04009175 return rc;
9176}
9177
9178/**
James Smartda0436e2009-05-22 14:51:39 -04009179 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
9180 * @phba: pointer to lpfc hba data structure.
9181 *
9182 * This routine is invoked to set up the PCI device memory space for device
9183 * with SLI-4 interface spec.
9184 *
9185 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009186 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04009187 * other values - error
9188 **/
9189static int
9190lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
9191{
9192 struct pci_dev *pdev;
9193 unsigned long bar0map_len, bar1map_len, bar2map_len;
9194 int error = -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -05009195 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04009196
9197 /* Obtain PCI device reference */
9198 if (!phba->pcidev)
9199 return error;
9200 else
9201 pdev = phba->pcidev;
9202
9203 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05009204 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
9205 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
9206 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
9207 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smartda0436e2009-05-22 14:51:39 -04009208 return error;
Michael Reed8e685972009-09-18 12:02:05 -05009209 }
9210 }
James Smartda0436e2009-05-22 14:51:39 -04009211
James Smart2fcee4b2010-12-15 17:57:46 -05009212 /*
9213 * The BARs and register set definitions and offset locations are
9214 * dependent on the if_type.
9215 */
9216 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
9217 &phba->sli4_hba.sli_intf.word0)) {
9218 return error;
9219 }
9220
9221 /* There is no SLI3 failback for SLI4 devices. */
9222 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
9223 LPFC_SLI_INTF_VALID) {
9224 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9225 "2894 SLI_INTF reg contents invalid "
9226 "sli_intf reg 0x%x\n",
9227 phba->sli4_hba.sli_intf.word0);
9228 return error;
9229 }
9230
9231 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9232 /*
9233 * Get the bus address of SLI4 device Bar regions and the
9234 * number of bytes required by each mapping. The mapping of the
9235 * particular PCI BARs regions is dependent on the type of
9236 * SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -04009237 */
James Smartf5ca6f22013-09-06 12:21:09 -04009238 if (pci_resource_start(pdev, PCI_64BIT_BAR0)) {
9239 phba->pci_bar0_map = pci_resource_start(pdev, PCI_64BIT_BAR0);
9240 bar0map_len = pci_resource_len(pdev, PCI_64BIT_BAR0);
James Smart2fcee4b2010-12-15 17:57:46 -05009241
9242 /*
9243 * Map SLI4 PCI Config Space Register base to a kernel virtual
9244 * addr
9245 */
9246 phba->sli4_hba.conf_regs_memmap_p =
9247 ioremap(phba->pci_bar0_map, bar0map_len);
9248 if (!phba->sli4_hba.conf_regs_memmap_p) {
9249 dev_printk(KERN_ERR, &pdev->dev,
9250 "ioremap failed for SLI4 PCI config "
9251 "registers.\n");
9252 goto out;
9253 }
James Smartf5ca6f22013-09-06 12:21:09 -04009254 phba->pci_bar0_memmap_p = phba->sli4_hba.conf_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05009255 /* Set up BAR0 PCI config space register memory map */
9256 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smart1dfb5a42010-02-12 14:40:50 -05009257 } else {
9258 phba->pci_bar0_map = pci_resource_start(pdev, 1);
9259 bar0map_len = pci_resource_len(pdev, 1);
James Smart2fcee4b2010-12-15 17:57:46 -05009260 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
9261 dev_printk(KERN_ERR, &pdev->dev,
9262 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
9263 goto out;
9264 }
9265 phba->sli4_hba.conf_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04009266 ioremap(phba->pci_bar0_map, bar0map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05009267 if (!phba->sli4_hba.conf_regs_memmap_p) {
9268 dev_printk(KERN_ERR, &pdev->dev,
9269 "ioremap failed for SLI4 PCI config "
9270 "registers.\n");
9271 goto out;
9272 }
9273 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smartda0436e2009-05-22 14:51:39 -04009274 }
9275
James Smartc31098c2011-04-16 11:03:33 -04009276 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
James Smartf5ca6f22013-09-06 12:21:09 -04009277 (pci_resource_start(pdev, PCI_64BIT_BAR2))) {
James Smart2fcee4b2010-12-15 17:57:46 -05009278 /*
9279 * Map SLI4 if type 0 HBA Control Register base to a kernel
9280 * virtual address and setup the registers.
9281 */
James Smartf5ca6f22013-09-06 12:21:09 -04009282 phba->pci_bar1_map = pci_resource_start(pdev, PCI_64BIT_BAR2);
9283 bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
James Smart2fcee4b2010-12-15 17:57:46 -05009284 phba->sli4_hba.ctrl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04009285 ioremap(phba->pci_bar1_map, bar1map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05009286 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
9287 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04009288 "ioremap failed for SLI4 HBA control registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05009289 goto out_iounmap_conf;
9290 }
James Smartf5ca6f22013-09-06 12:21:09 -04009291 phba->pci_bar2_memmap_p = phba->sli4_hba.ctrl_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05009292 lpfc_sli4_bar1_register_memmap(phba);
James Smartda0436e2009-05-22 14:51:39 -04009293 }
9294
James Smartc31098c2011-04-16 11:03:33 -04009295 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
James Smartf5ca6f22013-09-06 12:21:09 -04009296 (pci_resource_start(pdev, PCI_64BIT_BAR4))) {
James Smart2fcee4b2010-12-15 17:57:46 -05009297 /*
9298 * Map SLI4 if type 0 HBA Doorbell Register base to a kernel
9299 * virtual address and setup the registers.
9300 */
James Smartf5ca6f22013-09-06 12:21:09 -04009301 phba->pci_bar2_map = pci_resource_start(pdev, PCI_64BIT_BAR4);
9302 bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
James Smart2fcee4b2010-12-15 17:57:46 -05009303 phba->sli4_hba.drbl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04009304 ioremap(phba->pci_bar2_map, bar2map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05009305 if (!phba->sli4_hba.drbl_regs_memmap_p) {
9306 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04009307 "ioremap failed for SLI4 HBA doorbell registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05009308 goto out_iounmap_ctrl;
9309 }
James Smartf5ca6f22013-09-06 12:21:09 -04009310 phba->pci_bar4_memmap_p = phba->sli4_hba.drbl_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05009311 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
9312 if (error)
9313 goto out_iounmap_all;
James Smartda0436e2009-05-22 14:51:39 -04009314 }
9315
James Smartda0436e2009-05-22 14:51:39 -04009316 return 0;
9317
9318out_iounmap_all:
9319 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
9320out_iounmap_ctrl:
9321 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
9322out_iounmap_conf:
9323 iounmap(phba->sli4_hba.conf_regs_memmap_p);
9324out:
9325 return error;
9326}
9327
9328/**
9329 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
9330 * @phba: pointer to lpfc hba data structure.
9331 *
9332 * This routine is invoked to unset the PCI device memory space for device
9333 * with SLI-4 interface spec.
9334 **/
9335static void
9336lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
9337{
James Smart2e90f4b2011-12-13 13:22:37 -05009338 uint32_t if_type;
9339 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smartda0436e2009-05-22 14:51:39 -04009340
James Smart2e90f4b2011-12-13 13:22:37 -05009341 switch (if_type) {
9342 case LPFC_SLI_INTF_IF_TYPE_0:
9343 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
9344 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
9345 iounmap(phba->sli4_hba.conf_regs_memmap_p);
9346 break;
9347 case LPFC_SLI_INTF_IF_TYPE_2:
9348 iounmap(phba->sli4_hba.conf_regs_memmap_p);
9349 break;
9350 case LPFC_SLI_INTF_IF_TYPE_1:
9351 default:
9352 dev_printk(KERN_ERR, &phba->pcidev->dev,
9353 "FATAL - unsupported SLI4 interface type - %d\n",
9354 if_type);
9355 break;
9356 }
James Smartda0436e2009-05-22 14:51:39 -04009357}
9358
9359/**
James Smart3772a992009-05-22 14:50:54 -04009360 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
9361 * @phba: pointer to lpfc hba data structure.
9362 *
9363 * This routine is invoked to enable the MSI-X interrupt vectors to device
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009364 * with SLI-3 interface specs.
James Smart3772a992009-05-22 14:50:54 -04009365 *
9366 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009367 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04009368 * other values - error
9369 **/
9370static int
9371lpfc_sli_enable_msix(struct lpfc_hba *phba)
9372{
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009373 int rc;
James Smart3772a992009-05-22 14:50:54 -04009374 LPFC_MBOXQ_t *pmb;
9375
9376 /* Set up MSI-X multi-message vectors */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009377 rc = pci_alloc_irq_vectors(phba->pcidev,
9378 LPFC_MSIX_VECTORS, LPFC_MSIX_VECTORS, PCI_IRQ_MSIX);
9379 if (rc < 0) {
James Smart3772a992009-05-22 14:50:54 -04009380 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9381 "0420 PCI enable MSI-X failed (%d)\n", rc);
Alexander Gordeev029165a2014-07-16 20:05:15 +02009382 goto vec_fail_out;
James Smart3772a992009-05-22 14:50:54 -04009383 }
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009384
James Smart3772a992009-05-22 14:50:54 -04009385 /*
9386 * Assign MSI-X vectors to interrupt handlers
9387 */
9388
9389 /* vector-0 is associated to slow-path handler */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009390 rc = request_irq(pci_irq_vector(phba->pcidev, 0),
James Smarted243d32015-05-21 13:55:25 -04009391 &lpfc_sli_sp_intr_handler, 0,
James Smart3772a992009-05-22 14:50:54 -04009392 LPFC_SP_DRIVER_HANDLER_NAME, phba);
9393 if (rc) {
9394 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9395 "0421 MSI-X slow-path request_irq failed "
9396 "(%d)\n", rc);
9397 goto msi_fail_out;
9398 }
9399
9400 /* vector-1 is associated to fast-path handler */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009401 rc = request_irq(pci_irq_vector(phba->pcidev, 1),
James Smarted243d32015-05-21 13:55:25 -04009402 &lpfc_sli_fp_intr_handler, 0,
James Smart3772a992009-05-22 14:50:54 -04009403 LPFC_FP_DRIVER_HANDLER_NAME, phba);
9404
9405 if (rc) {
9406 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9407 "0429 MSI-X fast-path request_irq failed "
9408 "(%d)\n", rc);
9409 goto irq_fail_out;
9410 }
9411
9412 /*
9413 * Configure HBA MSI-X attention conditions to messages
9414 */
9415 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9416
9417 if (!pmb) {
9418 rc = -ENOMEM;
9419 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9420 "0474 Unable to allocate memory for issuing "
9421 "MBOX_CONFIG_MSI command\n");
9422 goto mem_fail_out;
9423 }
9424 rc = lpfc_config_msi(phba, pmb);
9425 if (rc)
9426 goto mbx_fail_out;
9427 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
9428 if (rc != MBX_SUCCESS) {
9429 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
9430 "0351 Config MSI mailbox command failed, "
9431 "mbxCmd x%x, mbxStatus x%x\n",
9432 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
9433 goto mbx_fail_out;
9434 }
9435
9436 /* Free memory allocated for mailbox command */
9437 mempool_free(pmb, phba->mbox_mem_pool);
9438 return rc;
9439
9440mbx_fail_out:
9441 /* Free memory allocated for mailbox command */
9442 mempool_free(pmb, phba->mbox_mem_pool);
9443
9444mem_fail_out:
9445 /* free the irq already requested */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009446 free_irq(pci_irq_vector(phba->pcidev, 1), phba);
James Smart3772a992009-05-22 14:50:54 -04009447
9448irq_fail_out:
9449 /* free the irq already requested */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009450 free_irq(pci_irq_vector(phba->pcidev, 0), phba);
James Smart3772a992009-05-22 14:50:54 -04009451
9452msi_fail_out:
9453 /* Unconfigure MSI-X capability structure */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009454 pci_free_irq_vectors(phba->pcidev);
Alexander Gordeev029165a2014-07-16 20:05:15 +02009455
9456vec_fail_out:
James Smart3772a992009-05-22 14:50:54 -04009457 return rc;
9458}
9459
9460/**
James Smart3772a992009-05-22 14:50:54 -04009461 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
9462 * @phba: pointer to lpfc hba data structure.
9463 *
9464 * This routine is invoked to enable the MSI interrupt mode to device with
9465 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
9466 * enable the MSI vector. The device driver is responsible for calling the
9467 * request_irq() to register MSI vector with a interrupt the handler, which
9468 * is done in this function.
9469 *
9470 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009471 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04009472 * other values - error
9473 */
9474static int
9475lpfc_sli_enable_msi(struct lpfc_hba *phba)
9476{
9477 int rc;
9478
9479 rc = pci_enable_msi(phba->pcidev);
9480 if (!rc)
9481 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9482 "0462 PCI enable MSI mode success.\n");
9483 else {
9484 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9485 "0471 PCI enable MSI mode failed (%d)\n", rc);
9486 return rc;
9487 }
9488
9489 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
James Smarted243d32015-05-21 13:55:25 -04009490 0, LPFC_DRIVER_NAME, phba);
James Smart3772a992009-05-22 14:50:54 -04009491 if (rc) {
9492 pci_disable_msi(phba->pcidev);
9493 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9494 "0478 MSI request_irq failed (%d)\n", rc);
9495 }
9496 return rc;
9497}
9498
9499/**
James Smart3772a992009-05-22 14:50:54 -04009500 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
9501 * @phba: pointer to lpfc hba data structure.
9502 *
9503 * This routine is invoked to enable device interrupt and associate driver's
9504 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
9505 * spec. Depends on the interrupt mode configured to the driver, the driver
9506 * will try to fallback from the configured interrupt mode to an interrupt
9507 * mode which is supported by the platform, kernel, and device in the order
9508 * of:
9509 * MSI-X -> MSI -> IRQ.
9510 *
9511 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009512 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04009513 * other values - error
9514 **/
9515static uint32_t
9516lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
9517{
9518 uint32_t intr_mode = LPFC_INTR_ERROR;
9519 int retval;
9520
9521 if (cfg_mode == 2) {
9522 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
9523 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
9524 if (!retval) {
9525 /* Now, try to enable MSI-X interrupt mode */
9526 retval = lpfc_sli_enable_msix(phba);
9527 if (!retval) {
9528 /* Indicate initialization to MSI-X mode */
9529 phba->intr_type = MSIX;
9530 intr_mode = 2;
9531 }
9532 }
9533 }
9534
9535 /* Fallback to MSI if MSI-X initialization failed */
9536 if (cfg_mode >= 1 && phba->intr_type == NONE) {
9537 retval = lpfc_sli_enable_msi(phba);
9538 if (!retval) {
9539 /* Indicate initialization to MSI mode */
9540 phba->intr_type = MSI;
9541 intr_mode = 1;
9542 }
9543 }
9544
9545 /* Fallback to INTx if both MSI-X/MSI initalization failed */
9546 if (phba->intr_type == NONE) {
9547 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
9548 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
9549 if (!retval) {
9550 /* Indicate initialization to INTx mode */
9551 phba->intr_type = INTx;
9552 intr_mode = 0;
9553 }
9554 }
9555 return intr_mode;
9556}
9557
9558/**
9559 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
9560 * @phba: pointer to lpfc hba data structure.
9561 *
9562 * This routine is invoked to disable device interrupt and disassociate the
9563 * driver's interrupt handler(s) from interrupt vector(s) to device with
9564 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
9565 * release the interrupt vector(s) for the message signaled interrupt.
9566 **/
9567static void
9568lpfc_sli_disable_intr(struct lpfc_hba *phba)
9569{
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009570 int nr_irqs, i;
9571
James Smart3772a992009-05-22 14:50:54 -04009572 if (phba->intr_type == MSIX)
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009573 nr_irqs = LPFC_MSIX_VECTORS;
9574 else
9575 nr_irqs = 1;
9576
9577 for (i = 0; i < nr_irqs; i++)
9578 free_irq(pci_irq_vector(phba->pcidev, i), phba);
9579 pci_free_irq_vectors(phba->pcidev);
James Smart3772a992009-05-22 14:50:54 -04009580
9581 /* Reset interrupt management states */
9582 phba->intr_type = NONE;
9583 phba->sli.slistat.sli_intr = 0;
James Smart3772a992009-05-22 14:50:54 -04009584}
9585
9586/**
James Smart895427b2017-02-12 13:52:30 -08009587 * lpfc_cpu_affinity_check - Check vector CPU affinity mappings
James Smart7bb03bb2013-04-17 20:19:16 -04009588 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -08009589 * @vectors: number of msix vectors allocated.
James Smart7bb03bb2013-04-17 20:19:16 -04009590 *
James Smart895427b2017-02-12 13:52:30 -08009591 * The routine will figure out the CPU affinity assignment for every
9592 * MSI-X vector allocated for the HBA. The hba_eq_hdl will be updated
9593 * with a pointer to the CPU mask that defines ALL the CPUs this vector
9594 * can be associated with. If the vector can be unquely associated with
9595 * a single CPU, that CPU will be recorded in hba_eq_hdl[index].cpu.
9596 * In addition, the CPU to IO channel mapping will be calculated
9597 * and the phba->sli4_hba.cpu_map array will reflect this.
James Smart7bb03bb2013-04-17 20:19:16 -04009598 */
James Smart895427b2017-02-12 13:52:30 -08009599static void
9600lpfc_cpu_affinity_check(struct lpfc_hba *phba, int vectors)
James Smart7bb03bb2013-04-17 20:19:16 -04009601{
9602 struct lpfc_vector_map_info *cpup;
James Smart895427b2017-02-12 13:52:30 -08009603 int index = 0;
9604 int vec = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04009605 int cpu;
James Smart7bb03bb2013-04-17 20:19:16 -04009606#ifdef CONFIG_X86
9607 struct cpuinfo_x86 *cpuinfo;
9608#endif
James Smart7bb03bb2013-04-17 20:19:16 -04009609
9610 /* Init cpu_map array */
9611 memset(phba->sli4_hba.cpu_map, 0xff,
9612 (sizeof(struct lpfc_vector_map_info) *
James Smart895427b2017-02-12 13:52:30 -08009613 phba->sli4_hba.num_present_cpu));
James Smart7bb03bb2013-04-17 20:19:16 -04009614
9615 /* Update CPU map with physical id and core id of each CPU */
9616 cpup = phba->sli4_hba.cpu_map;
9617 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
9618#ifdef CONFIG_X86
9619 cpuinfo = &cpu_data(cpu);
9620 cpup->phys_id = cpuinfo->phys_proc_id;
9621 cpup->core_id = cpuinfo->cpu_core_id;
9622#else
9623 /* No distinction between CPUs for other platforms */
9624 cpup->phys_id = 0;
9625 cpup->core_id = 0;
9626#endif
James Smart895427b2017-02-12 13:52:30 -08009627 cpup->channel_id = index; /* For now round robin */
9628 cpup->irq = pci_irq_vector(phba->pcidev, vec);
9629 vec++;
9630 if (vec >= vectors)
9631 vec = 0;
9632 index++;
9633 if (index >= phba->cfg_fcp_io_channel)
9634 index = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04009635 cpup++;
9636 }
James Smart7bb03bb2013-04-17 20:19:16 -04009637}
9638
9639
9640/**
James Smartda0436e2009-05-22 14:51:39 -04009641 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
9642 * @phba: pointer to lpfc hba data structure.
9643 *
9644 * This routine is invoked to enable the MSI-X interrupt vectors to device
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009645 * with SLI-4 interface spec.
James Smartda0436e2009-05-22 14:51:39 -04009646 *
9647 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009648 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04009649 * other values - error
9650 **/
9651static int
9652lpfc_sli4_enable_msix(struct lpfc_hba *phba)
9653{
James Smart75baf692010-06-08 18:31:21 -04009654 int vectors, rc, index;
James Smartda0436e2009-05-22 14:51:39 -04009655
9656 /* Set up MSI-X multi-message vectors */
James Smart895427b2017-02-12 13:52:30 -08009657 vectors = phba->io_channel_irqs;
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009658 if (phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05009659 vectors++;
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009660
James Smartf358dd02017-02-12 13:52:34 -08009661 rc = pci_alloc_irq_vectors(phba->pcidev,
9662 (phba->nvmet_support) ? 1 : 2,
9663 vectors, PCI_IRQ_MSIX | PCI_IRQ_AFFINITY);
Alexander Gordeev4f871e12014-09-03 12:56:29 -04009664 if (rc < 0) {
James Smartda0436e2009-05-22 14:51:39 -04009665 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9666 "0484 PCI enable MSI-X failed (%d)\n", rc);
Alexander Gordeev029165a2014-07-16 20:05:15 +02009667 goto vec_fail_out;
James Smartda0436e2009-05-22 14:51:39 -04009668 }
Alexander Gordeev4f871e12014-09-03 12:56:29 -04009669 vectors = rc;
James Smart75baf692010-06-08 18:31:21 -04009670
James Smart7bb03bb2013-04-17 20:19:16 -04009671 /* Assign MSI-X vectors to interrupt handlers */
James Smart67d12732012-08-03 12:36:13 -04009672 for (index = 0; index < vectors; index++) {
James Smart4305f182012-08-03 12:36:33 -04009673 memset(&phba->sli4_hba.handler_name[index], 0, 16);
James Smarta2fc4aef2014-09-03 12:57:55 -04009674 snprintf((char *)&phba->sli4_hba.handler_name[index],
9675 LPFC_SLI4_HANDLER_NAME_SZ,
James Smart4305f182012-08-03 12:36:33 -04009676 LPFC_DRIVER_HANDLER_NAME"%d", index);
James Smartda0436e2009-05-22 14:51:39 -04009677
James Smart895427b2017-02-12 13:52:30 -08009678 phba->sli4_hba.hba_eq_hdl[index].idx = index;
9679 phba->sli4_hba.hba_eq_hdl[index].phba = phba;
9680 atomic_set(&phba->sli4_hba.hba_eq_hdl[index].hba_eq_in_use, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009681 if (phba->cfg_fof && (index == (vectors - 1)))
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009682 rc = request_irq(pci_irq_vector(phba->pcidev, index),
James Smarted243d32015-05-21 13:55:25 -04009683 &lpfc_sli4_fof_intr_handler, 0,
James Smart1ba981f2014-02-20 09:56:45 -05009684 (char *)&phba->sli4_hba.handler_name[index],
James Smart895427b2017-02-12 13:52:30 -08009685 &phba->sli4_hba.hba_eq_hdl[index]);
James Smart1ba981f2014-02-20 09:56:45 -05009686 else
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009687 rc = request_irq(pci_irq_vector(phba->pcidev, index),
James Smarted243d32015-05-21 13:55:25 -04009688 &lpfc_sli4_hba_intr_handler, 0,
James Smart4305f182012-08-03 12:36:33 -04009689 (char *)&phba->sli4_hba.handler_name[index],
James Smart895427b2017-02-12 13:52:30 -08009690 &phba->sli4_hba.hba_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -04009691 if (rc) {
9692 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9693 "0486 MSI-X fast-path (%d) "
9694 "request_irq failed (%d)\n", index, rc);
9695 goto cfg_fail_out;
9696 }
9697 }
9698
James Smart1ba981f2014-02-20 09:56:45 -05009699 if (phba->cfg_fof)
9700 vectors--;
9701
James Smart895427b2017-02-12 13:52:30 -08009702 if (vectors != phba->io_channel_irqs) {
James Smart82c3e9b2012-09-29 11:29:50 -04009703 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9704 "3238 Reducing IO channels to match number of "
9705 "MSI-X vectors, requested %d got %d\n",
James Smart895427b2017-02-12 13:52:30 -08009706 phba->io_channel_irqs, vectors);
9707 if (phba->cfg_fcp_io_channel > vectors)
9708 phba->cfg_fcp_io_channel = vectors;
9709 if (phba->cfg_nvme_io_channel > vectors)
9710 phba->cfg_nvme_io_channel = vectors;
9711 if (phba->cfg_fcp_io_channel > phba->cfg_nvme_io_channel)
9712 phba->io_channel_irqs = phba->cfg_fcp_io_channel;
9713 else
9714 phba->io_channel_irqs = phba->cfg_nvme_io_channel;
James Smart82c3e9b2012-09-29 11:29:50 -04009715 }
James Smart895427b2017-02-12 13:52:30 -08009716 lpfc_cpu_affinity_check(phba, vectors);
James Smart7bb03bb2013-04-17 20:19:16 -04009717
James Smartda0436e2009-05-22 14:51:39 -04009718 return rc;
9719
9720cfg_fail_out:
9721 /* free the irq already requested */
James Smart895427b2017-02-12 13:52:30 -08009722 for (--index; index >= 0; index--)
9723 free_irq(pci_irq_vector(phba->pcidev, index),
9724 &phba->sli4_hba.hba_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -04009725
James Smartda0436e2009-05-22 14:51:39 -04009726 /* Unconfigure MSI-X capability structure */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009727 pci_free_irq_vectors(phba->pcidev);
Alexander Gordeev029165a2014-07-16 20:05:15 +02009728
9729vec_fail_out:
James Smartda0436e2009-05-22 14:51:39 -04009730 return rc;
9731}
9732
9733/**
James Smartda0436e2009-05-22 14:51:39 -04009734 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
9735 * @phba: pointer to lpfc hba data structure.
9736 *
9737 * This routine is invoked to enable the MSI interrupt mode to device with
9738 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
9739 * to enable the MSI vector. The device driver is responsible for calling
9740 * the request_irq() to register MSI vector with a interrupt the handler,
9741 * which is done in this function.
9742 *
9743 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009744 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04009745 * other values - error
9746 **/
9747static int
9748lpfc_sli4_enable_msi(struct lpfc_hba *phba)
9749{
9750 int rc, index;
9751
9752 rc = pci_enable_msi(phba->pcidev);
9753 if (!rc)
9754 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9755 "0487 PCI enable MSI mode success.\n");
9756 else {
9757 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9758 "0488 PCI enable MSI mode failed (%d)\n", rc);
9759 return rc;
9760 }
9761
9762 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
James Smarted243d32015-05-21 13:55:25 -04009763 0, LPFC_DRIVER_NAME, phba);
James Smartda0436e2009-05-22 14:51:39 -04009764 if (rc) {
9765 pci_disable_msi(phba->pcidev);
9766 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9767 "0490 MSI request_irq failed (%d)\n", rc);
James Smart75baf692010-06-08 18:31:21 -04009768 return rc;
James Smartda0436e2009-05-22 14:51:39 -04009769 }
9770
James Smart895427b2017-02-12 13:52:30 -08009771 for (index = 0; index < phba->io_channel_irqs; index++) {
9772 phba->sli4_hba.hba_eq_hdl[index].idx = index;
9773 phba->sli4_hba.hba_eq_hdl[index].phba = phba;
James Smartda0436e2009-05-22 14:51:39 -04009774 }
9775
James Smart1ba981f2014-02-20 09:56:45 -05009776 if (phba->cfg_fof) {
James Smart895427b2017-02-12 13:52:30 -08009777 phba->sli4_hba.hba_eq_hdl[index].idx = index;
9778 phba->sli4_hba.hba_eq_hdl[index].phba = phba;
James Smart1ba981f2014-02-20 09:56:45 -05009779 }
James Smart75baf692010-06-08 18:31:21 -04009780 return 0;
James Smartda0436e2009-05-22 14:51:39 -04009781}
9782
9783/**
James Smartda0436e2009-05-22 14:51:39 -04009784 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
9785 * @phba: pointer to lpfc hba data structure.
9786 *
9787 * This routine is invoked to enable device interrupt and associate driver's
9788 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
9789 * interface spec. Depends on the interrupt mode configured to the driver,
9790 * the driver will try to fallback from the configured interrupt mode to an
9791 * interrupt mode which is supported by the platform, kernel, and device in
9792 * the order of:
9793 * MSI-X -> MSI -> IRQ.
9794 *
9795 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009796 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04009797 * other values - error
9798 **/
9799static uint32_t
9800lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
9801{
9802 uint32_t intr_mode = LPFC_INTR_ERROR;
James Smart895427b2017-02-12 13:52:30 -08009803 int retval, idx;
James Smartda0436e2009-05-22 14:51:39 -04009804
9805 if (cfg_mode == 2) {
9806 /* Preparation before conf_msi mbox cmd */
9807 retval = 0;
9808 if (!retval) {
9809 /* Now, try to enable MSI-X interrupt mode */
9810 retval = lpfc_sli4_enable_msix(phba);
9811 if (!retval) {
9812 /* Indicate initialization to MSI-X mode */
9813 phba->intr_type = MSIX;
9814 intr_mode = 2;
9815 }
9816 }
9817 }
9818
9819 /* Fallback to MSI if MSI-X initialization failed */
9820 if (cfg_mode >= 1 && phba->intr_type == NONE) {
9821 retval = lpfc_sli4_enable_msi(phba);
9822 if (!retval) {
9823 /* Indicate initialization to MSI mode */
9824 phba->intr_type = MSI;
9825 intr_mode = 1;
9826 }
9827 }
9828
9829 /* Fallback to INTx if both MSI-X/MSI initalization failed */
9830 if (phba->intr_type == NONE) {
9831 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
9832 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
9833 if (!retval) {
James Smart895427b2017-02-12 13:52:30 -08009834 struct lpfc_hba_eq_hdl *eqhdl;
9835
James Smartda0436e2009-05-22 14:51:39 -04009836 /* Indicate initialization to INTx mode */
9837 phba->intr_type = INTx;
9838 intr_mode = 0;
James Smart895427b2017-02-12 13:52:30 -08009839
9840 for (idx = 0; idx < phba->io_channel_irqs; idx++) {
9841 eqhdl = &phba->sli4_hba.hba_eq_hdl[idx];
9842 eqhdl->idx = idx;
9843 eqhdl->phba = phba;
9844 atomic_set(&eqhdl->hba_eq_in_use, 1);
James Smartda0436e2009-05-22 14:51:39 -04009845 }
James Smart1ba981f2014-02-20 09:56:45 -05009846 if (phba->cfg_fof) {
James Smart895427b2017-02-12 13:52:30 -08009847 eqhdl = &phba->sli4_hba.hba_eq_hdl[idx];
9848 eqhdl->idx = idx;
9849 eqhdl->phba = phba;
9850 atomic_set(&eqhdl->hba_eq_in_use, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009851 }
James Smartda0436e2009-05-22 14:51:39 -04009852 }
9853 }
9854 return intr_mode;
9855}
9856
9857/**
9858 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
9859 * @phba: pointer to lpfc hba data structure.
9860 *
9861 * This routine is invoked to disable device interrupt and disassociate
9862 * the driver's interrupt handler(s) from interrupt vector(s) to device
9863 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
9864 * will release the interrupt vector(s) for the message signaled interrupt.
9865 **/
9866static void
9867lpfc_sli4_disable_intr(struct lpfc_hba *phba)
9868{
9869 /* Disable the currently initialized interrupt mode */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009870 if (phba->intr_type == MSIX) {
9871 int index;
9872
9873 /* Free up MSI-X multi-message vectors */
James Smart895427b2017-02-12 13:52:30 -08009874 for (index = 0; index < phba->io_channel_irqs; index++)
9875 free_irq(pci_irq_vector(phba->pcidev, index),
9876 &phba->sli4_hba.hba_eq_hdl[index]);
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009877
9878 if (phba->cfg_fof)
James Smart895427b2017-02-12 13:52:30 -08009879 free_irq(pci_irq_vector(phba->pcidev, index),
9880 &phba->sli4_hba.hba_eq_hdl[index]);
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009881 } else {
James Smartda0436e2009-05-22 14:51:39 -04009882 free_irq(phba->pcidev->irq, phba);
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009883 }
9884
9885 pci_free_irq_vectors(phba->pcidev);
James Smartda0436e2009-05-22 14:51:39 -04009886
9887 /* Reset interrupt management states */
9888 phba->intr_type = NONE;
9889 phba->sli.slistat.sli_intr = 0;
James Smartda0436e2009-05-22 14:51:39 -04009890}
9891
9892/**
James Smart3772a992009-05-22 14:50:54 -04009893 * lpfc_unset_hba - Unset SLI3 hba device initialization
9894 * @phba: pointer to lpfc hba data structure.
9895 *
9896 * This routine is invoked to unset the HBA device initialization steps to
9897 * a device with SLI-3 interface spec.
9898 **/
9899static void
9900lpfc_unset_hba(struct lpfc_hba *phba)
9901{
9902 struct lpfc_vport *vport = phba->pport;
9903 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
9904
9905 spin_lock_irq(shost->host_lock);
9906 vport->load_flag |= FC_UNLOADING;
9907 spin_unlock_irq(shost->host_lock);
9908
James Smart72859902012-01-18 16:25:38 -05009909 kfree(phba->vpi_bmask);
9910 kfree(phba->vpi_ids);
9911
James Smart3772a992009-05-22 14:50:54 -04009912 lpfc_stop_hba_timers(phba);
9913
9914 phba->pport->work_port_events = 0;
9915
9916 lpfc_sli_hba_down(phba);
9917
9918 lpfc_sli_brdrestart(phba);
9919
9920 lpfc_sli_disable_intr(phba);
9921
9922 return;
9923}
9924
9925/**
James Smart5af5eee2010-10-22 11:06:38 -04009926 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
9927 * @phba: Pointer to HBA context object.
9928 *
9929 * This function is called in the SLI4 code path to wait for completion
9930 * of device's XRIs exchange busy. It will check the XRI exchange busy
9931 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
9932 * that, it will check the XRI exchange busy on outstanding FCP and ELS
9933 * I/Os every 30 seconds, log error message, and wait forever. Only when
9934 * all XRI exchange busy complete, the driver unload shall proceed with
9935 * invoking the function reset ioctl mailbox command to the CNA and the
9936 * the rest of the driver unload resource release.
9937 **/
9938static void
9939lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
9940{
9941 int wait_time = 0;
James Smart895427b2017-02-12 13:52:30 -08009942 int nvme_xri_cmpl = 1;
9943 int fcp_xri_cmpl = 1;
James Smart5af5eee2010-10-22 11:06:38 -04009944 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smartf358dd02017-02-12 13:52:34 -08009945 int nvmet_xri_cmpl =
9946 list_empty(&phba->sli4_hba.lpfc_abts_nvmet_sgl_list);
James Smart5af5eee2010-10-22 11:06:38 -04009947
James Smart895427b2017-02-12 13:52:30 -08009948 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP)
9949 fcp_xri_cmpl =
9950 list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
9951 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
9952 nvme_xri_cmpl =
9953 list_empty(&phba->sli4_hba.lpfc_abts_nvme_buf_list);
9954
James Smartf358dd02017-02-12 13:52:34 -08009955 while (!fcp_xri_cmpl || !els_xri_cmpl || !nvme_xri_cmpl ||
9956 !nvmet_xri_cmpl) {
James Smart5af5eee2010-10-22 11:06:38 -04009957 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
James Smart895427b2017-02-12 13:52:30 -08009958 if (!nvme_xri_cmpl)
9959 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9960 "6100 NVME XRI exchange busy "
9961 "wait time: %d seconds.\n",
9962 wait_time/1000);
James Smart5af5eee2010-10-22 11:06:38 -04009963 if (!fcp_xri_cmpl)
9964 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9965 "2877 FCP XRI exchange busy "
9966 "wait time: %d seconds.\n",
9967 wait_time/1000);
9968 if (!els_xri_cmpl)
9969 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9970 "2878 ELS XRI exchange busy "
9971 "wait time: %d seconds.\n",
9972 wait_time/1000);
9973 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
9974 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
9975 } else {
9976 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
9977 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
9978 }
James Smart895427b2017-02-12 13:52:30 -08009979 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
9980 nvme_xri_cmpl = list_empty(
9981 &phba->sli4_hba.lpfc_abts_nvme_buf_list);
9982
9983 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP)
9984 fcp_xri_cmpl = list_empty(
9985 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
9986
James Smart5af5eee2010-10-22 11:06:38 -04009987 els_xri_cmpl =
9988 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smartf358dd02017-02-12 13:52:34 -08009989
9990 nvmet_xri_cmpl =
9991 list_empty(&phba->sli4_hba.lpfc_abts_nvmet_sgl_list);
James Smart5af5eee2010-10-22 11:06:38 -04009992 }
9993}
9994
9995/**
James Smartda0436e2009-05-22 14:51:39 -04009996 * lpfc_sli4_hba_unset - Unset the fcoe hba
9997 * @phba: Pointer to HBA context object.
9998 *
9999 * This function is called in the SLI4 code path to reset the HBA's FCoE
10000 * function. The caller is not required to hold any lock. This routine
10001 * issues PCI function reset mailbox command to reset the FCoE function.
10002 * At the end of the function, it calls lpfc_hba_down_post function to
10003 * free any pending commands.
10004 **/
10005static void
10006lpfc_sli4_hba_unset(struct lpfc_hba *phba)
10007{
10008 int wait_cnt = 0;
10009 LPFC_MBOXQ_t *mboxq;
James Smart912e3ac2011-05-24 11:42:11 -040010010 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -040010011
10012 lpfc_stop_hba_timers(phba);
10013 phba->sli4_hba.intr_enable = 0;
10014
10015 /*
10016 * Gracefully wait out the potential current outstanding asynchronous
10017 * mailbox command.
10018 */
10019
10020 /* First, block any pending async mailbox command from posted */
10021 spin_lock_irq(&phba->hbalock);
10022 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
10023 spin_unlock_irq(&phba->hbalock);
10024 /* Now, trying to wait it out if we can */
10025 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
10026 msleep(10);
10027 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
10028 break;
10029 }
10030 /* Forcefully release the outstanding mailbox command if timed out */
10031 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
10032 spin_lock_irq(&phba->hbalock);
10033 mboxq = phba->sli.mbox_active;
10034 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
10035 __lpfc_mbox_cmpl_put(phba, mboxq);
10036 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10037 phba->sli.mbox_active = NULL;
10038 spin_unlock_irq(&phba->hbalock);
10039 }
10040
James Smart5af5eee2010-10-22 11:06:38 -040010041 /* Abort all iocbs associated with the hba */
10042 lpfc_sli_hba_iocb_abort(phba);
10043
10044 /* Wait for completion of device XRI exchange busy */
10045 lpfc_sli4_xri_exchange_busy_wait(phba);
10046
James Smartda0436e2009-05-22 14:51:39 -040010047 /* Disable PCI subsystem interrupt */
10048 lpfc_sli4_disable_intr(phba);
10049
James Smart912e3ac2011-05-24 11:42:11 -040010050 /* Disable SR-IOV if enabled */
10051 if (phba->cfg_sriov_nr_virtfn)
10052 pci_disable_sriov(pdev);
10053
James Smartda0436e2009-05-22 14:51:39 -040010054 /* Stop kthread signal shall trigger work_done one more time */
10055 kthread_stop(phba->worker_thread);
10056
James Smart3677a3a2010-09-29 11:19:14 -040010057 /* Reset SLI4 HBA FCoE function */
10058 lpfc_pci_function_reset(phba);
James Smart5350d872011-10-10 21:33:49 -040010059 lpfc_sli4_queue_destroy(phba);
James Smart3677a3a2010-09-29 11:19:14 -040010060
James Smartda0436e2009-05-22 14:51:39 -040010061 /* Stop the SLI4 device port */
10062 phba->pport->work_port_events = 0;
10063}
10064
James Smart28baac72010-02-12 14:42:03 -050010065 /**
10066 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
10067 * @phba: Pointer to HBA context object.
10068 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
10069 *
10070 * This function is called in the SLI4 code path to read the port's
10071 * sli4 capabilities.
10072 *
10073 * This function may be be called from any context that can block-wait
10074 * for the completion. The expectation is that this routine is called
10075 * typically from probe_one or from the online routine.
10076 **/
10077int
10078lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
10079{
10080 int rc;
10081 struct lpfc_mqe *mqe;
10082 struct lpfc_pc_sli4_params *sli4_params;
10083 uint32_t mbox_tmo;
10084
10085 rc = 0;
10086 mqe = &mboxq->u.mqe;
10087
10088 /* Read the port's SLI4 Parameters port capabilities */
James Smartfedd3b72011-02-16 12:39:24 -050010089 lpfc_pc_sli4_params(mboxq);
James Smart28baac72010-02-12 14:42:03 -050010090 if (!phba->sli4_hba.intr_enable)
10091 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
10092 else {
James Smarta183a152011-10-10 21:32:43 -040010093 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart28baac72010-02-12 14:42:03 -050010094 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
10095 }
10096
10097 if (unlikely(rc))
10098 return 1;
10099
10100 sli4_params = &phba->sli4_hba.pc_sli4_params;
10101 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
10102 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
10103 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
10104 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
10105 &mqe->un.sli4_params);
10106 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
10107 &mqe->un.sli4_params);
10108 sli4_params->proto_types = mqe->un.sli4_params.word3;
10109 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
10110 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
10111 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
10112 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
10113 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
10114 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
10115 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
10116 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
10117 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
10118 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
10119 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
10120 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
10121 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
10122 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
10123 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
10124 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
10125 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
10126 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
10127 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
10128 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
James Smart05580562011-05-24 11:40:48 -040010129
10130 /* Make sure that sge_supp_len can be handled by the driver */
10131 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
10132 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
10133
James Smart28baac72010-02-12 14:42:03 -050010134 return rc;
10135}
10136
James Smartda0436e2009-05-22 14:51:39 -040010137/**
James Smartfedd3b72011-02-16 12:39:24 -050010138 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
10139 * @phba: Pointer to HBA context object.
10140 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
10141 *
10142 * This function is called in the SLI4 code path to read the port's
10143 * sli4 capabilities.
10144 *
10145 * This function may be be called from any context that can block-wait
10146 * for the completion. The expectation is that this routine is called
10147 * typically from probe_one or from the online routine.
10148 **/
10149int
10150lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
10151{
10152 int rc;
10153 struct lpfc_mqe *mqe = &mboxq->u.mqe;
10154 struct lpfc_pc_sli4_params *sli4_params;
James Smarta183a152011-10-10 21:32:43 -040010155 uint32_t mbox_tmo;
James Smartfedd3b72011-02-16 12:39:24 -050010156 int length;
10157 struct lpfc_sli4_parameters *mbx_sli4_parameters;
10158
James Smart6d368e52011-05-24 11:44:12 -040010159 /*
10160 * By default, the driver assumes the SLI4 port requires RPI
10161 * header postings. The SLI4_PARAM response will correct this
10162 * assumption.
10163 */
10164 phba->sli4_hba.rpi_hdrs_in_use = 1;
10165
James Smartfedd3b72011-02-16 12:39:24 -050010166 /* Read the port's SLI4 Config Parameters */
10167 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
10168 sizeof(struct lpfc_sli4_cfg_mhdr));
10169 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
10170 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
10171 length, LPFC_SLI4_MBX_EMBED);
10172 if (!phba->sli4_hba.intr_enable)
10173 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -040010174 else {
10175 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
10176 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
10177 }
James Smartfedd3b72011-02-16 12:39:24 -050010178 if (unlikely(rc))
10179 return rc;
10180 sli4_params = &phba->sli4_hba.pc_sli4_params;
10181 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
10182 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
10183 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
10184 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
10185 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
10186 mbx_sli4_parameters);
10187 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
10188 mbx_sli4_parameters);
10189 if (bf_get(cfg_phwq, mbx_sli4_parameters))
10190 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
10191 else
10192 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
10193 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
10194 sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
James Smart1ba981f2014-02-20 09:56:45 -050010195 sli4_params->oas_supported = bf_get(cfg_oas, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -050010196 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
10197 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
10198 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
10199 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
James Smart0c651872013-07-15 18:33:23 -040010200 sli4_params->wqsize = bf_get(cfg_wqsize, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -050010201 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
10202 mbx_sli4_parameters);
James Smart895427b2017-02-12 13:52:30 -080010203 sli4_params->wqpcnt = bf_get(cfg_wqpcnt, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -050010204 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
10205 mbx_sli4_parameters);
James Smart6d368e52011-05-24 11:44:12 -040010206 phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
10207 phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
James Smart895427b2017-02-12 13:52:30 -080010208 phba->nvme_support = (bf_get(cfg_nvme, mbx_sli4_parameters) &&
10209 bf_get(cfg_xib, mbx_sli4_parameters));
10210
10211 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP) ||
10212 !phba->nvme_support) {
10213 phba->nvme_support = 0;
10214 phba->nvmet_support = 0;
James Smart2d7dbc42017-02-12 13:52:35 -080010215 phba->cfg_nvmet_mrq = 0;
James Smart895427b2017-02-12 13:52:30 -080010216 phba->cfg_nvme_io_channel = 0;
10217 phba->io_channel_irqs = phba->cfg_fcp_io_channel;
10218 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_NVME,
10219 "6101 Disabling NVME support: "
10220 "Not supported by firmware: %d %d\n",
10221 bf_get(cfg_nvme, mbx_sli4_parameters),
10222 bf_get(cfg_xib, mbx_sli4_parameters));
10223
10224 /* If firmware doesn't support NVME, just use SCSI support */
10225 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
10226 return -ENODEV;
10227 phba->cfg_enable_fc4_type = LPFC_ENABLE_FCP;
10228 }
James Smart05580562011-05-24 11:40:48 -040010229
James Smartf358dd02017-02-12 13:52:34 -080010230 if (bf_get(cfg_xib, mbx_sli4_parameters) && phba->cfg_suppress_rsp)
10231 phba->sli.sli_flag |= LPFC_SLI_SUPPRESS_RSP;
10232
James Smart05580562011-05-24 11:40:48 -040010233 /* Make sure that sge_supp_len can be handled by the driver */
10234 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
10235 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
10236
James Smartb5c53952016-03-31 14:12:30 -070010237 /*
10238 * Issue IOs with CDB embedded in WQE to minimized the number
10239 * of DMAs the firmware has to do. Setting this to 1 also forces
10240 * the driver to use 128 bytes WQEs for FCP IOs.
10241 */
10242 if (bf_get(cfg_ext_embed_cb, mbx_sli4_parameters))
10243 phba->fcp_embed_io = 1;
10244 else
10245 phba->fcp_embed_io = 0;
James Smart7bdedb32016-07-06 12:36:00 -070010246
10247 /*
10248 * Check if the SLI port supports MDS Diagnostics
10249 */
10250 if (bf_get(cfg_mds_diags, mbx_sli4_parameters))
10251 phba->mds_diags_support = 1;
10252 else
10253 phba->mds_diags_support = 0;
James Smartfedd3b72011-02-16 12:39:24 -050010254 return 0;
10255}
10256
10257/**
James Smart3772a992009-05-22 14:50:54 -040010258 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
10259 * @pdev: pointer to PCI device
10260 * @pid: pointer to PCI device identifier
10261 *
10262 * This routine is to be called to attach a device with SLI-3 interface spec
10263 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
10264 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
10265 * information of the device and driver to see if the driver state that it can
10266 * support this kind of device. If the match is successful, the driver core
10267 * invokes this routine. If this routine determines it can claim the HBA, it
10268 * does all the initialization that it needs to do to handle the HBA properly.
10269 *
10270 * Return code
10271 * 0 - driver can claim the device
10272 * negative value - driver can not claim the device
10273 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010274static int
James Smart3772a992009-05-22 14:50:54 -040010275lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
10276{
10277 struct lpfc_hba *phba;
10278 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -040010279 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -040010280 int error;
10281 uint32_t cfg_mode, intr_mode;
10282
10283 /* Allocate memory for HBA structure */
10284 phba = lpfc_hba_alloc(pdev);
10285 if (!phba)
10286 return -ENOMEM;
10287
10288 /* Perform generic PCI device enabling operation */
10289 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -040010290 if (error)
James Smart3772a992009-05-22 14:50:54 -040010291 goto out_free_phba;
James Smart3772a992009-05-22 14:50:54 -040010292
10293 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
10294 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
10295 if (error)
10296 goto out_disable_pci_dev;
10297
10298 /* Set up SLI-3 specific device PCI memory space */
10299 error = lpfc_sli_pci_mem_setup(phba);
10300 if (error) {
10301 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10302 "1402 Failed to set up pci memory space.\n");
10303 goto out_disable_pci_dev;
10304 }
10305
James Smart3772a992009-05-22 14:50:54 -040010306 /* Set up SLI-3 specific device driver resources */
10307 error = lpfc_sli_driver_resource_setup(phba);
10308 if (error) {
10309 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10310 "1404 Failed to set up driver resource.\n");
10311 goto out_unset_pci_mem_s3;
10312 }
10313
10314 /* Initialize and populate the iocb list per host */
10315 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
10316 if (error) {
10317 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10318 "1405 Failed to initialize iocb list.\n");
10319 goto out_unset_driver_resource_s3;
10320 }
10321
10322 /* Set up common device driver resources */
10323 error = lpfc_setup_driver_resource_phase2(phba);
10324 if (error) {
10325 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10326 "1406 Failed to set up driver resource.\n");
10327 goto out_free_iocb_list;
10328 }
10329
James Smart079b5c92011-08-21 21:48:49 -040010330 /* Get the default values for Model Name and Description */
10331 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
10332
James Smart3772a992009-05-22 14:50:54 -040010333 /* Create SCSI host to the physical port */
10334 error = lpfc_create_shost(phba);
10335 if (error) {
10336 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10337 "1407 Failed to create scsi host.\n");
10338 goto out_unset_driver_resource;
10339 }
10340
10341 /* Configure sysfs attributes */
10342 vport = phba->pport;
10343 error = lpfc_alloc_sysfs_attr(vport);
10344 if (error) {
10345 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10346 "1476 Failed to allocate sysfs attr\n");
10347 goto out_destroy_shost;
10348 }
10349
James Smart6669f9b2009-10-02 15:16:45 -040010350 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -040010351 /* Now, trying to enable interrupt and bring up the device */
10352 cfg_mode = phba->cfg_use_msi;
10353 while (true) {
10354 /* Put device to a known state before enabling interrupt */
10355 lpfc_stop_port(phba);
10356 /* Configure and enable interrupt */
10357 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
10358 if (intr_mode == LPFC_INTR_ERROR) {
10359 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10360 "0431 Failed to enable interrupt.\n");
10361 error = -ENODEV;
10362 goto out_free_sysfs_attr;
10363 }
10364 /* SLI-3 HBA setup */
10365 if (lpfc_sli_hba_setup(phba)) {
10366 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10367 "1477 Failed to set up hba\n");
10368 error = -ENODEV;
10369 goto out_remove_device;
10370 }
10371
10372 /* Wait 50ms for the interrupts of previous mailbox commands */
10373 msleep(50);
10374 /* Check active interrupts on message signaled interrupts */
10375 if (intr_mode == 0 ||
10376 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
10377 /* Log the current active interrupt mode */
10378 phba->intr_mode = intr_mode;
10379 lpfc_log_intr_mode(phba, intr_mode);
10380 break;
10381 } else {
10382 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10383 "0447 Configure interrupt mode (%d) "
10384 "failed active interrupt test.\n",
10385 intr_mode);
10386 /* Disable the current interrupt mode */
10387 lpfc_sli_disable_intr(phba);
10388 /* Try next level of interrupt mode */
10389 cfg_mode = --intr_mode;
10390 }
10391 }
10392
10393 /* Perform post initialization setup */
10394 lpfc_post_init_setup(phba);
10395
10396 /* Check if there are static vports to be created. */
10397 lpfc_create_static_vport(phba);
10398
10399 return 0;
10400
10401out_remove_device:
10402 lpfc_unset_hba(phba);
10403out_free_sysfs_attr:
10404 lpfc_free_sysfs_attr(vport);
10405out_destroy_shost:
10406 lpfc_destroy_shost(phba);
10407out_unset_driver_resource:
10408 lpfc_unset_driver_resource_phase2(phba);
10409out_free_iocb_list:
10410 lpfc_free_iocb_list(phba);
10411out_unset_driver_resource_s3:
10412 lpfc_sli_driver_resource_unset(phba);
10413out_unset_pci_mem_s3:
10414 lpfc_sli_pci_mem_unset(phba);
10415out_disable_pci_dev:
10416 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040010417 if (shost)
10418 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -040010419out_free_phba:
10420 lpfc_hba_free(phba);
10421 return error;
10422}
10423
10424/**
10425 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -040010426 * @pdev: pointer to PCI device
10427 *
James Smart3772a992009-05-22 14:50:54 -040010428 * This routine is to be called to disattach a device with SLI-3 interface
10429 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
10430 * removed from PCI bus, it performs all the necessary cleanup for the HBA
10431 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -040010432 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010433static void
James Smart3772a992009-05-22 14:50:54 -040010434lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -050010435{
James Smart2e0fef82007-06-17 19:56:36 -050010436 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10437 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -050010438 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -050010439 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -050010440 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -050010441
James Smart549e55c2007-08-02 11:09:51 -040010442 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -040010443 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -040010444 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010445
James Smart858c9f62007-06-17 19:56:39 -050010446 lpfc_free_sysfs_attr(vport);
10447
James Smarteada2722008-12-04 22:39:13 -050010448 /* Release all the vports against this physical port */
10449 vports = lpfc_create_vport_work_array(phba);
10450 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040010451 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
10452 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
10453 continue;
James Smarteada2722008-12-04 22:39:13 -050010454 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040010455 }
James Smarteada2722008-12-04 22:39:13 -050010456 lpfc_destroy_vport_work_array(phba, vports);
10457
10458 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -050010459 fc_remove_host(shost);
10460 scsi_remove_host(shost);
James Smartd613b6a2017-02-12 13:52:37 -080010461
James Smart87af33f2007-10-27 13:37:43 -040010462 lpfc_cleanup(vport);
10463
James Smart2e0fef82007-06-17 19:56:36 -050010464 /*
10465 * Bring down the SLI Layer. This step disable all interrupts,
10466 * clears the rings, discards all mailbox commands, and resets
10467 * the HBA.
10468 */
James Smarta257bf92009-04-06 18:48:10 -040010469
Justin P. Mattock48e34d02010-12-30 15:07:58 -080010470 /* HBA interrupt will be disabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -050010471 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -040010472 /* Stop kthread signal shall trigger work_done one more time */
10473 kthread_stop(phba->worker_thread);
10474 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -050010475 lpfc_sli_brdrestart(phba);
10476
James Smart72859902012-01-18 16:25:38 -050010477 kfree(phba->vpi_bmask);
10478 kfree(phba->vpi_ids);
10479
James Smart3772a992009-05-22 14:50:54 -040010480 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -050010481 spin_lock_irq(&phba->hbalock);
10482 list_del_init(&vport->listentry);
10483 spin_unlock_irq(&phba->hbalock);
10484
James Smart858c9f62007-06-17 19:56:39 -050010485 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -050010486
James Smart912e3ac2011-05-24 11:42:11 -040010487 /* Disable SR-IOV if enabled */
10488 if (phba->cfg_sriov_nr_virtfn)
10489 pci_disable_sriov(pdev);
10490
James Smart5b75da22008-12-04 22:39:35 -050010491 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -040010492 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -050010493
James Smart858c9f62007-06-17 19:56:39 -050010494 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -050010495
10496 /*
10497 * Call scsi_free before mem_free since scsi bufs are released to their
10498 * corresponding pools here.
10499 */
10500 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -040010501 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -050010502
James Smart34b02dc2008-08-24 21:49:55 -040010503 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
10504 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -050010505
James Smart2e0fef82007-06-17 19:56:36 -050010506 /* Free resources associated with SLI2 interface */
10507 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -040010508 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -050010509
10510 /* unmap adapter SLIM and Control Registers */
10511 iounmap(phba->ctrl_regs_memmap_p);
10512 iounmap(phba->slim_memmap_p);
10513
James Smart3772a992009-05-22 14:50:54 -040010514 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -050010515
Johannes Thumshirne0c04832016-06-07 09:44:03 +020010516 pci_release_mem_regions(pdev);
James Smart2e0fef82007-06-17 19:56:36 -050010517 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -050010518}
10519
Linas Vepstas8d63f372007-02-14 14:28:36 -060010520/**
James Smart3772a992009-05-22 14:50:54 -040010521 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -050010522 * @pdev: pointer to PCI device
10523 * @msg: power management message
10524 *
James Smart3772a992009-05-22 14:50:54 -040010525 * This routine is to be called from the kernel's PCI subsystem to support
10526 * system Power Management (PM) to device with SLI-3 interface spec. When
10527 * PM invokes this method, it quiesces the device by stopping the driver's
10528 * worker thread for the device, turning off device's interrupt and DMA,
10529 * and bring the device offline. Note that as the driver implements the
10530 * minimum PM requirements to a power-aware driver's PM support for the
10531 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
10532 * to the suspend() method call will be treated as SUSPEND and the driver will
10533 * fully reinitialize its device during resume() method call, the driver will
10534 * set device to PCI_D3hot state in PCI config space instead of setting it
10535 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -050010536 *
10537 * Return code
James Smart3772a992009-05-22 14:50:54 -040010538 * 0 - driver suspended the device
10539 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -050010540 **/
10541static int
James Smart3772a992009-05-22 14:50:54 -040010542lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -050010543{
10544 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10545 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10546
10547 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10548 "0473 PCI device Power Management suspend.\n");
10549
10550 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040010551 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart3a55b532008-12-04 22:38:54 -050010552 lpfc_offline(phba);
10553 kthread_stop(phba->worker_thread);
10554
10555 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -040010556 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -050010557
10558 /* Save device state to PCI config space */
10559 pci_save_state(pdev);
10560 pci_set_power_state(pdev, PCI_D3hot);
10561
10562 return 0;
10563}
10564
10565/**
James Smart3772a992009-05-22 14:50:54 -040010566 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -050010567 * @pdev: pointer to PCI device
10568 *
James Smart3772a992009-05-22 14:50:54 -040010569 * This routine is to be called from the kernel's PCI subsystem to support
10570 * system Power Management (PM) to device with SLI-3 interface spec. When PM
10571 * invokes this method, it restores the device's PCI config space state and
10572 * fully reinitializes the device and brings it online. Note that as the
10573 * driver implements the minimum PM requirements to a power-aware driver's
10574 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
10575 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
10576 * driver will fully reinitialize its device during resume() method call,
10577 * the device will be set to PCI_D0 directly in PCI config space before
10578 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -050010579 *
10580 * Return code
James Smart3772a992009-05-22 14:50:54 -040010581 * 0 - driver suspended the device
10582 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -050010583 **/
10584static int
James Smart3772a992009-05-22 14:50:54 -040010585lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -050010586{
10587 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10588 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -050010589 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -050010590 int error;
10591
10592 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10593 "0452 PCI device Power Management resume.\n");
10594
10595 /* Restore device state from PCI config space */
10596 pci_set_power_state(pdev, PCI_D0);
10597 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -040010598
James Smart1dfb5a42010-02-12 14:40:50 -050010599 /*
10600 * As the new kernel behavior of pci_restore_state() API call clears
10601 * device saved_state flag, need to save the restored state again.
10602 */
10603 pci_save_state(pdev);
10604
James Smart3a55b532008-12-04 22:38:54 -050010605 if (pdev->is_busmaster)
10606 pci_set_master(pdev);
10607
10608 /* Startup the kernel thread for this host adapter. */
10609 phba->worker_thread = kthread_run(lpfc_do_work, phba,
10610 "lpfc_worker_%d", phba->brd_no);
10611 if (IS_ERR(phba->worker_thread)) {
10612 error = PTR_ERR(phba->worker_thread);
10613 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10614 "0434 PM resume failed to start worker "
10615 "thread: error=x%x.\n", error);
10616 return error;
10617 }
10618
James Smart5b75da22008-12-04 22:39:35 -050010619 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -040010620 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -050010621 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -050010622 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -050010623 "0430 PM resume Failed to enable interrupt\n");
10624 return -EIO;
10625 } else
10626 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -050010627
10628 /* Restart HBA and bring it online */
10629 lpfc_sli_brdrestart(phba);
10630 lpfc_online(phba);
10631
James Smart5b75da22008-12-04 22:39:35 -050010632 /* Log the current active interrupt mode */
10633 lpfc_log_intr_mode(phba, phba->intr_mode);
10634
James Smart3a55b532008-12-04 22:38:54 -050010635 return 0;
10636}
10637
10638/**
James Smart891478a2009-11-18 15:40:23 -050010639 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
10640 * @phba: pointer to lpfc hba data structure.
10641 *
10642 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -040010643 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -050010644 **/
10645static void
10646lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
10647{
10648 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10649 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -040010650
10651 /*
10652 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
10653 * and let the SCSI mid-layer to retry them to recover.
10654 */
James Smartdb55fba2014-04-04 13:52:02 -040010655 lpfc_sli_abort_fcp_rings(phba);
James Smart891478a2009-11-18 15:40:23 -050010656}
10657
10658/**
James Smart0d878412009-10-02 15:16:56 -040010659 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
10660 * @phba: pointer to lpfc hba data structure.
10661 *
10662 * This routine is called to prepare the SLI3 device for PCI slot reset. It
10663 * disables the device interrupt and pci device, and aborts the internal FCP
10664 * pending I/Os.
10665 **/
10666static void
10667lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
10668{
James Smart0d878412009-10-02 15:16:56 -040010669 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -050010670 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -040010671
James Smart75baf692010-06-08 18:31:21 -040010672 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040010673 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040010674
James Smarte2af0d22010-03-15 11:25:32 -040010675 /* Block all SCSI devices' I/Os on the host */
10676 lpfc_scsi_dev_block(phba);
10677
James Smartea714f32013-04-17 20:18:47 -040010678 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
10679 lpfc_sli_flush_fcp_rings(phba);
10680
James Smarte2af0d22010-03-15 11:25:32 -040010681 /* stop all timers */
10682 lpfc_stop_hba_timers(phba);
10683
James Smart0d878412009-10-02 15:16:56 -040010684 /* Disable interrupt and pci device */
10685 lpfc_sli_disable_intr(phba);
10686 pci_disable_device(phba->pcidev);
James Smart0d878412009-10-02 15:16:56 -040010687}
10688
10689/**
10690 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
10691 * @phba: pointer to lpfc hba data structure.
10692 *
10693 * This routine is called to prepare the SLI3 device for PCI slot permanently
10694 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
10695 * pending I/Os.
10696 **/
10697static void
James Smart75baf692010-06-08 18:31:21 -040010698lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
James Smart0d878412009-10-02 15:16:56 -040010699{
10700 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -050010701 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -040010702 /* Block all SCSI devices' I/Os on the host */
10703 lpfc_scsi_dev_block(phba);
10704
10705 /* stop all timers */
10706 lpfc_stop_hba_timers(phba);
10707
James Smart0d878412009-10-02 15:16:56 -040010708 /* Clean up all driver's outstanding SCSI I/Os */
10709 lpfc_sli_flush_fcp_rings(phba);
10710}
10711
10712/**
James Smart3772a992009-05-22 14:50:54 -040010713 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -040010714 * @pdev: pointer to PCI device.
10715 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -060010716 *
James Smart3772a992009-05-22 14:50:54 -040010717 * This routine is called from the PCI subsystem for I/O error handling to
10718 * device with SLI-3 interface spec. This function is called by the PCI
10719 * subsystem after a PCI bus error affecting this device has been detected.
10720 * When this function is invoked, it will need to stop all the I/Os and
10721 * interrupt(s) to the device. Once that is done, it will return
10722 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
10723 * as desired.
James Smarte59058c2008-08-24 21:49:00 -040010724 *
10725 * Return codes
James Smart0d878412009-10-02 15:16:56 -040010726 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -040010727 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10728 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -040010729 **/
James Smart3772a992009-05-22 14:50:54 -040010730static pci_ers_result_t
10731lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -060010732{
James Smart51ef4c22007-08-02 11:10:31 -040010733 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10734 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060010735
James Smart0d878412009-10-02 15:16:56 -040010736 switch (state) {
10737 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -050010738 /* Non-fatal error, prepare for recovery */
10739 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -040010740 return PCI_ERS_RESULT_CAN_RECOVER;
10741 case pci_channel_io_frozen:
10742 /* Fatal error, prepare for slot reset */
10743 lpfc_sli_prep_dev_for_reset(phba);
10744 return PCI_ERS_RESULT_NEED_RESET;
10745 case pci_channel_io_perm_failure:
10746 /* Permanent failure, prepare for device down */
James Smart75baf692010-06-08 18:31:21 -040010747 lpfc_sli_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -060010748 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -040010749 default:
10750 /* Unknown state, prepare and request slot reset */
10751 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10752 "0472 Unknown PCI error state: x%x\n", state);
10753 lpfc_sli_prep_dev_for_reset(phba);
10754 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -040010755 }
Linas Vepstas8d63f372007-02-14 14:28:36 -060010756}
10757
10758/**
James Smart3772a992009-05-22 14:50:54 -040010759 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -040010760 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -060010761 *
James Smart3772a992009-05-22 14:50:54 -040010762 * This routine is called from the PCI subsystem for error handling to
10763 * device with SLI-3 interface spec. This is called after PCI bus has been
10764 * reset to restart the PCI card from scratch, as if from a cold-boot.
10765 * During the PCI subsystem error recovery, after driver returns
10766 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
10767 * recovery and then call this routine before calling the .resume method
10768 * to recover the device. This function will initialize the HBA device,
10769 * enable the interrupt, but it will just put the HBA to offline state
10770 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -040010771 *
10772 * Return codes
James Smart3772a992009-05-22 14:50:54 -040010773 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10774 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -060010775 */
James Smart3772a992009-05-22 14:50:54 -040010776static pci_ers_result_t
10777lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -060010778{
James Smart51ef4c22007-08-02 11:10:31 -040010779 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10780 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060010781 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -050010782 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -060010783
10784 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +110010785 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -060010786 printk(KERN_ERR "lpfc: Cannot re-enable "
10787 "PCI device after reset.\n");
10788 return PCI_ERS_RESULT_DISCONNECT;
10789 }
10790
James Smart97207482008-12-04 22:39:19 -050010791 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050010792
10793 /*
10794 * As the new kernel behavior of pci_restore_state() API call clears
10795 * device saved_state flag, need to save the restored state again.
10796 */
10797 pci_save_state(pdev);
10798
James Smart97207482008-12-04 22:39:19 -050010799 if (pdev->is_busmaster)
10800 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -060010801
James Smart92d7f7b2007-06-17 19:56:38 -050010802 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010803 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -050010804 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -060010805
James Smart5b75da22008-12-04 22:39:35 -050010806 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -040010807 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -050010808 if (intr_mode == LPFC_INTR_ERROR) {
10809 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10810 "0427 Cannot re-enable interrupt after "
10811 "slot reset.\n");
10812 return PCI_ERS_RESULT_DISCONNECT;
10813 } else
10814 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -060010815
James Smart75baf692010-06-08 18:31:21 -040010816 /* Take device offline, it will perform cleanup */
James Smart618a5232012-06-12 13:54:36 -040010817 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
Linas Vepstas8d63f372007-02-14 14:28:36 -060010818 lpfc_offline(phba);
10819 lpfc_sli_brdrestart(phba);
10820
James Smart5b75da22008-12-04 22:39:35 -050010821 /* Log the current active interrupt mode */
10822 lpfc_log_intr_mode(phba, phba->intr_mode);
10823
Linas Vepstas8d63f372007-02-14 14:28:36 -060010824 return PCI_ERS_RESULT_RECOVERED;
10825}
10826
10827/**
James Smart3772a992009-05-22 14:50:54 -040010828 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -040010829 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -060010830 *
James Smart3772a992009-05-22 14:50:54 -040010831 * This routine is called from the PCI subsystem for error handling to device
10832 * with SLI-3 interface spec. It is called when kernel error recovery tells
10833 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
10834 * error recovery. After this call, traffic can start to flow from this device
10835 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -060010836 */
James Smart3772a992009-05-22 14:50:54 -040010837static void
10838lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -060010839{
James Smart51ef4c22007-08-02 11:10:31 -040010840 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10841 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060010842
James Smarte2af0d22010-03-15 11:25:32 -040010843 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -040010844 lpfc_online(phba);
James Smart0d878412009-10-02 15:16:56 -040010845
10846 /* Clean up Advanced Error Reporting (AER) if needed */
10847 if (phba->hba_flag & HBA_AER_ENABLED)
10848 pci_cleanup_aer_uncorrect_error_status(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -060010849}
10850
James Smart3772a992009-05-22 14:50:54 -040010851/**
James Smartda0436e2009-05-22 14:51:39 -040010852 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
10853 * @phba: pointer to lpfc hba data structure.
10854 *
10855 * returns the number of ELS/CT IOCBs to reserve
10856 **/
10857int
10858lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
10859{
10860 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
10861
James Smartf1126682009-06-10 17:22:44 -040010862 if (phba->sli_rev == LPFC_SLI_REV4) {
10863 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -040010864 return 10;
James Smartf1126682009-06-10 17:22:44 -040010865 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -040010866 return 25;
James Smartf1126682009-06-10 17:22:44 -040010867 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -040010868 return 50;
James Smartf1126682009-06-10 17:22:44 -040010869 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -040010870 return 100;
James Smart8a9d2e82012-05-09 21:16:12 -040010871 else if (max_xri <= 1536)
James Smart6a9c52c2009-10-02 15:16:51 -040010872 return 150;
James Smart8a9d2e82012-05-09 21:16:12 -040010873 else if (max_xri <= 2048)
10874 return 200;
10875 else
10876 return 250;
James Smartf1126682009-06-10 17:22:44 -040010877 } else
10878 return 0;
James Smartda0436e2009-05-22 14:51:39 -040010879}
10880
10881/**
James Smart895427b2017-02-12 13:52:30 -080010882 * lpfc_sli4_get_iocb_cnt - Calculate the # of total IOCBs to reserve
10883 * @phba: pointer to lpfc hba data structure.
10884 *
James Smartf358dd02017-02-12 13:52:34 -080010885 * returns the number of ELS/CT + NVMET IOCBs to reserve
James Smart895427b2017-02-12 13:52:30 -080010886 **/
10887int
10888lpfc_sli4_get_iocb_cnt(struct lpfc_hba *phba)
10889{
10890 int max_xri = lpfc_sli4_get_els_iocb_cnt(phba);
10891
James Smartf358dd02017-02-12 13:52:34 -080010892 if (phba->nvmet_support)
10893 max_xri += LPFC_NVMET_BUF_POST;
James Smart895427b2017-02-12 13:52:30 -080010894 return max_xri;
10895}
10896
10897
10898/**
James Smart52d52442011-05-24 11:42:45 -040010899 * lpfc_write_firmware - attempt to write a firmware image to the port
James Smart52d52442011-05-24 11:42:45 -040010900 * @fw: pointer to firmware image returned from request_firmware.
James Smartce396282012-09-29 11:30:56 -040010901 * @phba: pointer to lpfc hba data structure.
James Smart52d52442011-05-24 11:42:45 -040010902 *
James Smart52d52442011-05-24 11:42:45 -040010903 **/
James Smartce396282012-09-29 11:30:56 -040010904static void
10905lpfc_write_firmware(const struct firmware *fw, void *context)
James Smart52d52442011-05-24 11:42:45 -040010906{
James Smartce396282012-09-29 11:30:56 -040010907 struct lpfc_hba *phba = (struct lpfc_hba *)context;
James Smart6b5151f2012-01-18 16:24:06 -050010908 char fwrev[FW_REV_STR_SIZE];
James Smartce396282012-09-29 11:30:56 -040010909 struct lpfc_grp_hdr *image;
James Smart52d52442011-05-24 11:42:45 -040010910 struct list_head dma_buffer_list;
10911 int i, rc = 0;
10912 struct lpfc_dmabuf *dmabuf, *next;
10913 uint32_t offset = 0, temp_offset = 0;
James Smart6b6ef5d2016-10-13 15:06:17 -070010914 uint32_t magic_number, ftype, fid, fsize;
James Smart52d52442011-05-24 11:42:45 -040010915
James Smartc71ab862012-10-31 14:44:33 -040010916 /* It can be null in no-wait mode, sanity check */
James Smartce396282012-09-29 11:30:56 -040010917 if (!fw) {
10918 rc = -ENXIO;
10919 goto out;
10920 }
10921 image = (struct lpfc_grp_hdr *)fw->data;
10922
James Smart6b6ef5d2016-10-13 15:06:17 -070010923 magic_number = be32_to_cpu(image->magic_number);
10924 ftype = bf_get_be32(lpfc_grp_hdr_file_type, image);
10925 fid = bf_get_be32(lpfc_grp_hdr_id, image),
10926 fsize = be32_to_cpu(image->size);
10927
James Smart52d52442011-05-24 11:42:45 -040010928 INIT_LIST_HEAD(&dma_buffer_list);
James Smart6b6ef5d2016-10-13 15:06:17 -070010929 if ((magic_number != LPFC_GROUP_OJECT_MAGIC_G5 &&
10930 magic_number != LPFC_GROUP_OJECT_MAGIC_G6) ||
10931 ftype != LPFC_FILE_TYPE_GROUP || fsize != fw->size) {
James Smart52d52442011-05-24 11:42:45 -040010932 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10933 "3022 Invalid FW image found. "
Arnd Bergmannefe583c2016-10-17 14:35:46 +020010934 "Magic:%x Type:%x ID:%x Size %d %zd\n",
James Smart6b6ef5d2016-10-13 15:06:17 -070010935 magic_number, ftype, fid, fsize, fw->size);
James Smartce396282012-09-29 11:30:56 -040010936 rc = -EINVAL;
10937 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010938 }
10939 lpfc_decode_firmware_rev(phba, fwrev, 1);
James Smart88a2cfb2011-07-22 18:36:33 -040010940 if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
James Smart52d52442011-05-24 11:42:45 -040010941 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartce396282012-09-29 11:30:56 -040010942 "3023 Updating Firmware, Current Version:%s "
James Smart52d52442011-05-24 11:42:45 -040010943 "New Version:%s\n",
James Smart88a2cfb2011-07-22 18:36:33 -040010944 fwrev, image->revision);
James Smart52d52442011-05-24 11:42:45 -040010945 for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
10946 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
10947 GFP_KERNEL);
10948 if (!dmabuf) {
10949 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040010950 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010951 }
10952 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
10953 SLI4_PAGE_SIZE,
10954 &dmabuf->phys,
10955 GFP_KERNEL);
10956 if (!dmabuf->virt) {
10957 kfree(dmabuf);
10958 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040010959 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010960 }
10961 list_add_tail(&dmabuf->list, &dma_buffer_list);
10962 }
10963 while (offset < fw->size) {
10964 temp_offset = offset;
10965 list_for_each_entry(dmabuf, &dma_buffer_list, list) {
James Smart079b5c92011-08-21 21:48:49 -040010966 if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
James Smart52d52442011-05-24 11:42:45 -040010967 memcpy(dmabuf->virt,
10968 fw->data + temp_offset,
James Smart079b5c92011-08-21 21:48:49 -040010969 fw->size - temp_offset);
10970 temp_offset = fw->size;
James Smart52d52442011-05-24 11:42:45 -040010971 break;
10972 }
James Smart52d52442011-05-24 11:42:45 -040010973 memcpy(dmabuf->virt, fw->data + temp_offset,
10974 SLI4_PAGE_SIZE);
James Smart88a2cfb2011-07-22 18:36:33 -040010975 temp_offset += SLI4_PAGE_SIZE;
James Smart52d52442011-05-24 11:42:45 -040010976 }
10977 rc = lpfc_wr_object(phba, &dma_buffer_list,
10978 (fw->size - offset), &offset);
James Smartce396282012-09-29 11:30:56 -040010979 if (rc)
10980 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010981 }
10982 rc = offset;
10983 }
James Smartce396282012-09-29 11:30:56 -040010984
10985release_out:
James Smart52d52442011-05-24 11:42:45 -040010986 list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
10987 list_del(&dmabuf->list);
10988 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
10989 dmabuf->virt, dmabuf->phys);
10990 kfree(dmabuf);
10991 }
James Smartce396282012-09-29 11:30:56 -040010992 release_firmware(fw);
10993out:
10994 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartc71ab862012-10-31 14:44:33 -040010995 "3024 Firmware update done: %d.\n", rc);
James Smartce396282012-09-29 11:30:56 -040010996 return;
James Smart52d52442011-05-24 11:42:45 -040010997}
10998
10999/**
James Smartc71ab862012-10-31 14:44:33 -040011000 * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
11001 * @phba: pointer to lpfc hba data structure.
11002 *
11003 * This routine is called to perform Linux generic firmware upgrade on device
11004 * that supports such feature.
11005 **/
11006int
11007lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
11008{
11009 uint8_t file_name[ELX_MODEL_NAME_SIZE];
11010 int ret;
11011 const struct firmware *fw;
11012
11013 /* Only supported on SLI4 interface type 2 for now */
11014 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
11015 LPFC_SLI_INTF_IF_TYPE_2)
11016 return -EPERM;
11017
11018 snprintf(file_name, ELX_MODEL_NAME_SIZE, "%s.grp", phba->ModelName);
11019
11020 if (fw_upgrade == INT_FW_UPGRADE) {
11021 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
11022 file_name, &phba->pcidev->dev,
11023 GFP_KERNEL, (void *)phba,
11024 lpfc_write_firmware);
11025 } else if (fw_upgrade == RUN_FW_UPGRADE) {
11026 ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
11027 if (!ret)
11028 lpfc_write_firmware(fw, (void *)phba);
11029 } else {
11030 ret = -EINVAL;
11031 }
11032
11033 return ret;
11034}
11035
11036/**
James Smartda0436e2009-05-22 14:51:39 -040011037 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
11038 * @pdev: pointer to PCI device
11039 * @pid: pointer to PCI device identifier
11040 *
11041 * This routine is called from the kernel's PCI subsystem to device with
11042 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
11043 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
11044 * information of the device and driver to see if the driver state that it
11045 * can support this kind of device. If the match is successful, the driver
11046 * core invokes this routine. If this routine determines it can claim the HBA,
11047 * it does all the initialization that it needs to do to handle the HBA
11048 * properly.
11049 *
11050 * Return code
11051 * 0 - driver can claim the device
11052 * negative value - driver can not claim the device
11053 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011054static int
James Smartda0436e2009-05-22 14:51:39 -040011055lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
11056{
11057 struct lpfc_hba *phba;
11058 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -040011059 struct Scsi_Host *shost = NULL;
Sebastian Herbsztdb6f1c22015-08-31 16:48:14 -040011060 int error;
James Smartda0436e2009-05-22 14:51:39 -040011061 uint32_t cfg_mode, intr_mode;
James Smartda0436e2009-05-22 14:51:39 -040011062
11063 /* Allocate memory for HBA structure */
11064 phba = lpfc_hba_alloc(pdev);
11065 if (!phba)
11066 return -ENOMEM;
11067
11068 /* Perform generic PCI device enabling operation */
11069 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -040011070 if (error)
James Smartda0436e2009-05-22 14:51:39 -040011071 goto out_free_phba;
James Smartda0436e2009-05-22 14:51:39 -040011072
11073 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
11074 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
11075 if (error)
11076 goto out_disable_pci_dev;
11077
11078 /* Set up SLI-4 specific device PCI memory space */
11079 error = lpfc_sli4_pci_mem_setup(phba);
11080 if (error) {
11081 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11082 "1410 Failed to set up pci memory space.\n");
11083 goto out_disable_pci_dev;
11084 }
11085
James Smartda0436e2009-05-22 14:51:39 -040011086 /* Set up SLI-4 Specific device driver resources */
11087 error = lpfc_sli4_driver_resource_setup(phba);
11088 if (error) {
11089 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11090 "1412 Failed to set up driver resource.\n");
11091 goto out_unset_pci_mem_s4;
11092 }
11093
11094 /* Initialize and populate the iocb list per host */
James Smart2a9bf3d2010-06-07 15:24:45 -040011095
11096 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11097 "2821 initialize iocb list %d.\n",
11098 phba->cfg_iocb_cnt*1024);
11099 error = lpfc_init_iocb_list(phba, phba->cfg_iocb_cnt*1024);
11100
James Smartda0436e2009-05-22 14:51:39 -040011101 if (error) {
11102 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11103 "1413 Failed to initialize iocb list.\n");
11104 goto out_unset_driver_resource_s4;
11105 }
11106
James Smart19ca7602010-11-20 23:11:55 -050011107 INIT_LIST_HEAD(&phba->active_rrq_list);
James Smart7d791df2011-07-22 18:37:52 -040011108 INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
James Smart19ca7602010-11-20 23:11:55 -050011109
James Smartda0436e2009-05-22 14:51:39 -040011110 /* Set up common device driver resources */
11111 error = lpfc_setup_driver_resource_phase2(phba);
11112 if (error) {
11113 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11114 "1414 Failed to set up driver resource.\n");
11115 goto out_free_iocb_list;
11116 }
11117
James Smart079b5c92011-08-21 21:48:49 -040011118 /* Get the default values for Model Name and Description */
11119 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
11120
James Smartda0436e2009-05-22 14:51:39 -040011121 /* Create SCSI host to the physical port */
11122 error = lpfc_create_shost(phba);
11123 if (error) {
11124 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11125 "1415 Failed to create scsi host.\n");
11126 goto out_unset_driver_resource;
11127 }
11128
11129 /* Configure sysfs attributes */
11130 vport = phba->pport;
11131 error = lpfc_alloc_sysfs_attr(vport);
11132 if (error) {
11133 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11134 "1416 Failed to allocate sysfs attr\n");
11135 goto out_destroy_shost;
11136 }
11137
James Smart6669f9b2009-10-02 15:16:45 -040011138 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -040011139 /* Now, trying to enable interrupt and bring up the device */
11140 cfg_mode = phba->cfg_use_msi;
James Smartda0436e2009-05-22 14:51:39 -040011141
James Smart7b15db32013-01-03 15:43:29 -050011142 /* Put device to a known state before enabling interrupt */
11143 lpfc_stop_port(phba);
James Smart895427b2017-02-12 13:52:30 -080011144
James Smart7b15db32013-01-03 15:43:29 -050011145 /* Configure and enable interrupt */
11146 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
11147 if (intr_mode == LPFC_INTR_ERROR) {
11148 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11149 "0426 Failed to enable interrupt.\n");
11150 error = -ENODEV;
11151 goto out_free_sysfs_attr;
James Smartda0436e2009-05-22 14:51:39 -040011152 }
James Smart7b15db32013-01-03 15:43:29 -050011153 /* Default to single EQ for non-MSI-X */
James Smart895427b2017-02-12 13:52:30 -080011154 if (phba->intr_type != MSIX) {
11155 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP)
11156 phba->cfg_fcp_io_channel = 1;
James Smart2d7dbc42017-02-12 13:52:35 -080011157 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart895427b2017-02-12 13:52:30 -080011158 phba->cfg_nvme_io_channel = 1;
James Smart2d7dbc42017-02-12 13:52:35 -080011159 if (phba->nvmet_support)
11160 phba->cfg_nvmet_mrq = 1;
11161 }
James Smart895427b2017-02-12 13:52:30 -080011162 phba->io_channel_irqs = 1;
11163 }
11164
James Smart7b15db32013-01-03 15:43:29 -050011165 /* Set up SLI-4 HBA */
11166 if (lpfc_sli4_hba_setup(phba)) {
11167 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11168 "1421 Failed to set up hba\n");
11169 error = -ENODEV;
11170 goto out_disable_intr;
11171 }
11172
11173 /* Log the current active interrupt mode */
11174 phba->intr_mode = intr_mode;
11175 lpfc_log_intr_mode(phba, intr_mode);
James Smartda0436e2009-05-22 14:51:39 -040011176
11177 /* Perform post initialization setup */
11178 lpfc_post_init_setup(phba);
11179
James Smart01649562017-02-12 13:52:32 -080011180 /* NVME support in FW earlier in the driver load corrects the
11181 * FC4 type making a check for nvme_support unnecessary.
11182 */
11183 if ((phba->nvmet_support == 0) &&
11184 (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)) {
11185 /* Create NVME binding with nvme_fc_transport. This
11186 * ensures the vport is initialized.
11187 */
11188 error = lpfc_nvme_create_localport(vport);
11189 if (error) {
11190 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11191 "6004 NVME registration failed, "
11192 "error x%x\n",
11193 error);
11194 goto out_disable_intr;
11195 }
11196 }
James Smart895427b2017-02-12 13:52:30 -080011197
James Smartc71ab862012-10-31 14:44:33 -040011198 /* check for firmware upgrade or downgrade */
11199 if (phba->cfg_request_firmware_upgrade)
Sebastian Herbsztdb6f1c22015-08-31 16:48:14 -040011200 lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
James Smart52d52442011-05-24 11:42:45 -040011201
James Smart1c6834a2009-07-19 10:01:26 -040011202 /* Check if there are static vports to be created. */
11203 lpfc_create_static_vport(phba);
James Smartda0436e2009-05-22 14:51:39 -040011204 return 0;
11205
11206out_disable_intr:
11207 lpfc_sli4_disable_intr(phba);
11208out_free_sysfs_attr:
11209 lpfc_free_sysfs_attr(vport);
11210out_destroy_shost:
11211 lpfc_destroy_shost(phba);
11212out_unset_driver_resource:
11213 lpfc_unset_driver_resource_phase2(phba);
11214out_free_iocb_list:
11215 lpfc_free_iocb_list(phba);
11216out_unset_driver_resource_s4:
11217 lpfc_sli4_driver_resource_unset(phba);
11218out_unset_pci_mem_s4:
11219 lpfc_sli4_pci_mem_unset(phba);
11220out_disable_pci_dev:
11221 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040011222 if (shost)
11223 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -040011224out_free_phba:
11225 lpfc_hba_free(phba);
11226 return error;
11227}
11228
11229/**
11230 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
11231 * @pdev: pointer to PCI device
11232 *
11233 * This routine is called from the kernel's PCI subsystem to device with
11234 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
11235 * removed from PCI bus, it performs all the necessary cleanup for the HBA
11236 * device to be removed from the PCI subsystem properly.
11237 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011238static void
James Smartda0436e2009-05-22 14:51:39 -040011239lpfc_pci_remove_one_s4(struct pci_dev *pdev)
11240{
11241 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11242 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
11243 struct lpfc_vport **vports;
11244 struct lpfc_hba *phba = vport->phba;
11245 int i;
11246
11247 /* Mark the device unloading flag */
11248 spin_lock_irq(&phba->hbalock);
11249 vport->load_flag |= FC_UNLOADING;
11250 spin_unlock_irq(&phba->hbalock);
11251
11252 /* Free the HBA sysfs attributes */
11253 lpfc_free_sysfs_attr(vport);
11254
11255 /* Release all the vports against this physical port */
11256 vports = lpfc_create_vport_work_array(phba);
11257 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040011258 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
11259 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
11260 continue;
James Smartda0436e2009-05-22 14:51:39 -040011261 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040011262 }
James Smartda0436e2009-05-22 14:51:39 -040011263 lpfc_destroy_vport_work_array(phba, vports);
11264
11265 /* Remove FC host and then SCSI host with the physical port */
11266 fc_remove_host(shost);
11267 scsi_remove_host(shost);
11268
James Smartd613b6a2017-02-12 13:52:37 -080011269 /* Perform ndlp cleanup on the physical port. The nvme and nvmet
11270 * localports are destroyed after to cleanup all transport memory.
James Smart895427b2017-02-12 13:52:30 -080011271 */
James Smartda0436e2009-05-22 14:51:39 -040011272 lpfc_cleanup(vport);
James Smartd613b6a2017-02-12 13:52:37 -080011273 lpfc_nvmet_destroy_targetport(phba);
11274 lpfc_nvme_destroy_localport(vport);
James Smartda0436e2009-05-22 14:51:39 -040011275
11276 /*
11277 * Bring down the SLI Layer. This step disables all interrupts,
11278 * clears the rings, discards all mailbox commands, and resets
11279 * the HBA FCoE function.
11280 */
11281 lpfc_debugfs_terminate(vport);
11282 lpfc_sli4_hba_unset(phba);
11283
11284 spin_lock_irq(&phba->hbalock);
11285 list_del_init(&vport->listentry);
11286 spin_unlock_irq(&phba->hbalock);
11287
James Smart3677a3a2010-09-29 11:19:14 -040011288 /* Perform scsi free before driver resource_unset since scsi
James Smartda0436e2009-05-22 14:51:39 -040011289 * buffers are released to their corresponding pools here.
11290 */
11291 lpfc_scsi_free(phba);
James Smart895427b2017-02-12 13:52:30 -080011292 lpfc_nvme_free(phba);
James Smart01649562017-02-12 13:52:32 -080011293 lpfc_free_iocb_list(phba);
James Smart67d12732012-08-03 12:36:13 -040011294
James Smartda0436e2009-05-22 14:51:39 -040011295 lpfc_sli4_driver_resource_unset(phba);
11296
11297 /* Unmap adapter Control and Doorbell registers */
11298 lpfc_sli4_pci_mem_unset(phba);
11299
11300 /* Release PCI resources and disable device's PCI function */
11301 scsi_host_put(shost);
11302 lpfc_disable_pci_dev(phba);
11303
11304 /* Finally, free the driver's device data structure */
11305 lpfc_hba_free(phba);
11306
11307 return;
11308}
11309
11310/**
11311 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
11312 * @pdev: pointer to PCI device
11313 * @msg: power management message
11314 *
11315 * This routine is called from the kernel's PCI subsystem to support system
11316 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
11317 * this method, it quiesces the device by stopping the driver's worker
11318 * thread for the device, turning off device's interrupt and DMA, and bring
11319 * the device offline. Note that as the driver implements the minimum PM
11320 * requirements to a power-aware driver's PM support for suspend/resume -- all
11321 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
11322 * method call will be treated as SUSPEND and the driver will fully
11323 * reinitialize its device during resume() method call, the driver will set
11324 * device to PCI_D3hot state in PCI config space instead of setting it
11325 * according to the @msg provided by the PM.
11326 *
11327 * Return code
11328 * 0 - driver suspended the device
11329 * Error otherwise
11330 **/
11331static int
11332lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
11333{
11334 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11335 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11336
11337 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart75baf692010-06-08 18:31:21 -040011338 "2843 PCI device Power Management suspend.\n");
James Smartda0436e2009-05-22 14:51:39 -040011339
11340 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040011341 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smartda0436e2009-05-22 14:51:39 -040011342 lpfc_offline(phba);
11343 kthread_stop(phba->worker_thread);
11344
11345 /* Disable interrupt from device */
11346 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040011347 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -040011348
11349 /* Save device state to PCI config space */
11350 pci_save_state(pdev);
11351 pci_set_power_state(pdev, PCI_D3hot);
11352
11353 return 0;
11354}
11355
11356/**
11357 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
11358 * @pdev: pointer to PCI device
11359 *
11360 * This routine is called from the kernel's PCI subsystem to support system
11361 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
11362 * this method, it restores the device's PCI config space state and fully
11363 * reinitializes the device and brings it online. Note that as the driver
11364 * implements the minimum PM requirements to a power-aware driver's PM for
11365 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
11366 * to the suspend() method call will be treated as SUSPEND and the driver
11367 * will fully reinitialize its device during resume() method call, the device
11368 * will be set to PCI_D0 directly in PCI config space before restoring the
11369 * state.
11370 *
11371 * Return code
11372 * 0 - driver suspended the device
11373 * Error otherwise
11374 **/
11375static int
11376lpfc_pci_resume_one_s4(struct pci_dev *pdev)
11377{
11378 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11379 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11380 uint32_t intr_mode;
11381 int error;
11382
11383 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11384 "0292 PCI device Power Management resume.\n");
11385
11386 /* Restore device state from PCI config space */
11387 pci_set_power_state(pdev, PCI_D0);
11388 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050011389
11390 /*
11391 * As the new kernel behavior of pci_restore_state() API call clears
11392 * device saved_state flag, need to save the restored state again.
11393 */
11394 pci_save_state(pdev);
11395
James Smartda0436e2009-05-22 14:51:39 -040011396 if (pdev->is_busmaster)
11397 pci_set_master(pdev);
11398
11399 /* Startup the kernel thread for this host adapter. */
11400 phba->worker_thread = kthread_run(lpfc_do_work, phba,
11401 "lpfc_worker_%d", phba->brd_no);
11402 if (IS_ERR(phba->worker_thread)) {
11403 error = PTR_ERR(phba->worker_thread);
11404 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11405 "0293 PM resume failed to start worker "
11406 "thread: error=x%x.\n", error);
11407 return error;
11408 }
11409
11410 /* Configure and enable interrupt */
11411 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
11412 if (intr_mode == LPFC_INTR_ERROR) {
11413 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11414 "0294 PM resume Failed to enable interrupt\n");
11415 return -EIO;
11416 } else
11417 phba->intr_mode = intr_mode;
11418
11419 /* Restart HBA and bring it online */
11420 lpfc_sli_brdrestart(phba);
11421 lpfc_online(phba);
11422
11423 /* Log the current active interrupt mode */
11424 lpfc_log_intr_mode(phba, phba->intr_mode);
11425
11426 return 0;
11427}
11428
11429/**
James Smart75baf692010-06-08 18:31:21 -040011430 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
11431 * @phba: pointer to lpfc hba data structure.
11432 *
11433 * This routine is called to prepare the SLI4 device for PCI slot recover. It
11434 * aborts all the outstanding SCSI I/Os to the pci device.
11435 **/
11436static void
11437lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
11438{
James Smart75baf692010-06-08 18:31:21 -040011439 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11440 "2828 PCI channel I/O abort preparing for recovery\n");
11441 /*
11442 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
11443 * and let the SCSI mid-layer to retry them to recover.
11444 */
James Smartdb55fba2014-04-04 13:52:02 -040011445 lpfc_sli_abort_fcp_rings(phba);
James Smart75baf692010-06-08 18:31:21 -040011446}
11447
11448/**
11449 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
11450 * @phba: pointer to lpfc hba data structure.
11451 *
11452 * This routine is called to prepare the SLI4 device for PCI slot reset. It
11453 * disables the device interrupt and pci device, and aborts the internal FCP
11454 * pending I/Os.
11455 **/
11456static void
11457lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
11458{
11459 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11460 "2826 PCI channel disable preparing for reset\n");
11461
11462 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040011463 lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
James Smart75baf692010-06-08 18:31:21 -040011464
11465 /* Block all SCSI devices' I/Os on the host */
11466 lpfc_scsi_dev_block(phba);
11467
James Smartea714f32013-04-17 20:18:47 -040011468 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
11469 lpfc_sli_flush_fcp_rings(phba);
11470
James Smart75baf692010-06-08 18:31:21 -040011471 /* stop all timers */
11472 lpfc_stop_hba_timers(phba);
11473
11474 /* Disable interrupt and pci device */
11475 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040011476 lpfc_sli4_queue_destroy(phba);
James Smart75baf692010-06-08 18:31:21 -040011477 pci_disable_device(phba->pcidev);
James Smart75baf692010-06-08 18:31:21 -040011478}
11479
11480/**
11481 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
11482 * @phba: pointer to lpfc hba data structure.
11483 *
11484 * This routine is called to prepare the SLI4 device for PCI slot permanently
11485 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
11486 * pending I/Os.
11487 **/
11488static void
11489lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
11490{
11491 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11492 "2827 PCI channel permanent disable for failure\n");
11493
11494 /* Block all SCSI devices' I/Os on the host */
11495 lpfc_scsi_dev_block(phba);
11496
11497 /* stop all timers */
11498 lpfc_stop_hba_timers(phba);
11499
11500 /* Clean up all driver's outstanding SCSI I/Os */
11501 lpfc_sli_flush_fcp_rings(phba);
11502}
11503
11504/**
James Smartda0436e2009-05-22 14:51:39 -040011505 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
11506 * @pdev: pointer to PCI device.
11507 * @state: the current PCI connection state.
11508 *
11509 * This routine is called from the PCI subsystem for error handling to device
11510 * with SLI-4 interface spec. This function is called by the PCI subsystem
11511 * after a PCI bus error affecting this device has been detected. When this
11512 * function is invoked, it will need to stop all the I/Os and interrupt(s)
11513 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
11514 * for the PCI subsystem to perform proper recovery as desired.
11515 *
11516 * Return codes
11517 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
11518 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11519 **/
11520static pci_ers_result_t
11521lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
11522{
James Smart75baf692010-06-08 18:31:21 -040011523 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11524 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11525
11526 switch (state) {
11527 case pci_channel_io_normal:
11528 /* Non-fatal error, prepare for recovery */
11529 lpfc_sli4_prep_dev_for_recover(phba);
11530 return PCI_ERS_RESULT_CAN_RECOVER;
11531 case pci_channel_io_frozen:
11532 /* Fatal error, prepare for slot reset */
11533 lpfc_sli4_prep_dev_for_reset(phba);
11534 return PCI_ERS_RESULT_NEED_RESET;
11535 case pci_channel_io_perm_failure:
11536 /* Permanent failure, prepare for device down */
11537 lpfc_sli4_prep_dev_for_perm_failure(phba);
11538 return PCI_ERS_RESULT_DISCONNECT;
11539 default:
11540 /* Unknown state, prepare and request slot reset */
11541 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11542 "2825 Unknown PCI error state: x%x\n", state);
11543 lpfc_sli4_prep_dev_for_reset(phba);
11544 return PCI_ERS_RESULT_NEED_RESET;
11545 }
James Smartda0436e2009-05-22 14:51:39 -040011546}
11547
11548/**
11549 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
11550 * @pdev: pointer to PCI device.
11551 *
11552 * This routine is called from the PCI subsystem for error handling to device
11553 * with SLI-4 interface spec. It is called after PCI bus has been reset to
11554 * restart the PCI card from scratch, as if from a cold-boot. During the
11555 * PCI subsystem error recovery, after the driver returns
11556 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
11557 * recovery and then call this routine before calling the .resume method to
11558 * recover the device. This function will initialize the HBA device, enable
11559 * the interrupt, but it will just put the HBA to offline state without
11560 * passing any I/O traffic.
11561 *
11562 * Return codes
11563 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
11564 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11565 */
11566static pci_ers_result_t
11567lpfc_io_slot_reset_s4(struct pci_dev *pdev)
11568{
James Smart75baf692010-06-08 18:31:21 -040011569 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11570 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11571 struct lpfc_sli *psli = &phba->sli;
11572 uint32_t intr_mode;
11573
11574 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
11575 if (pci_enable_device_mem(pdev)) {
11576 printk(KERN_ERR "lpfc: Cannot re-enable "
11577 "PCI device after reset.\n");
11578 return PCI_ERS_RESULT_DISCONNECT;
11579 }
11580
11581 pci_restore_state(pdev);
James Smart0a96e972011-07-22 18:37:28 -040011582
11583 /*
11584 * As the new kernel behavior of pci_restore_state() API call clears
11585 * device saved_state flag, need to save the restored state again.
11586 */
11587 pci_save_state(pdev);
11588
James Smart75baf692010-06-08 18:31:21 -040011589 if (pdev->is_busmaster)
11590 pci_set_master(pdev);
11591
11592 spin_lock_irq(&phba->hbalock);
11593 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
11594 spin_unlock_irq(&phba->hbalock);
11595
11596 /* Configure and enable interrupt */
11597 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
11598 if (intr_mode == LPFC_INTR_ERROR) {
11599 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11600 "2824 Cannot re-enable interrupt after "
11601 "slot reset.\n");
11602 return PCI_ERS_RESULT_DISCONNECT;
11603 } else
11604 phba->intr_mode = intr_mode;
11605
11606 /* Log the current active interrupt mode */
11607 lpfc_log_intr_mode(phba, phba->intr_mode);
11608
James Smartda0436e2009-05-22 14:51:39 -040011609 return PCI_ERS_RESULT_RECOVERED;
11610}
11611
11612/**
11613 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
11614 * @pdev: pointer to PCI device
11615 *
11616 * This routine is called from the PCI subsystem for error handling to device
11617 * with SLI-4 interface spec. It is called when kernel error recovery tells
11618 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
11619 * error recovery. After this call, traffic can start to flow from this device
11620 * again.
11621 **/
11622static void
11623lpfc_io_resume_s4(struct pci_dev *pdev)
11624{
James Smart75baf692010-06-08 18:31:21 -040011625 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11626 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11627
11628 /*
11629 * In case of slot reset, as function reset is performed through
11630 * mailbox command which needs DMA to be enabled, this operation
11631 * has to be moved to the io resume phase. Taking device offline
11632 * will perform the necessary cleanup.
11633 */
11634 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
11635 /* Perform device reset */
James Smart618a5232012-06-12 13:54:36 -040011636 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040011637 lpfc_offline(phba);
11638 lpfc_sli_brdrestart(phba);
11639 /* Bring the device back online */
11640 lpfc_online(phba);
11641 }
11642
11643 /* Clean up Advanced Error Reporting (AER) if needed */
11644 if (phba->hba_flag & HBA_AER_ENABLED)
11645 pci_cleanup_aer_uncorrect_error_status(pdev);
James Smartda0436e2009-05-22 14:51:39 -040011646}
11647
11648/**
James Smart3772a992009-05-22 14:50:54 -040011649 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
11650 * @pdev: pointer to PCI device
11651 * @pid: pointer to PCI device identifier
11652 *
11653 * This routine is to be registered to the kernel's PCI subsystem. When an
11654 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
11655 * at PCI device-specific information of the device and driver to see if the
11656 * driver state that it can support this kind of device. If the match is
11657 * successful, the driver core invokes this routine. This routine dispatches
11658 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
11659 * do all the initialization that it needs to do to handle the HBA device
11660 * properly.
11661 *
11662 * Return code
11663 * 0 - driver can claim the device
11664 * negative value - driver can not claim the device
11665 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011666static int
James Smart3772a992009-05-22 14:50:54 -040011667lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
11668{
11669 int rc;
James Smart8fa38512009-07-19 10:01:03 -040011670 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -040011671
James Smart28baac72010-02-12 14:42:03 -050011672 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -040011673 return -ENODEV;
11674
James Smart8fa38512009-07-19 10:01:03 -040011675 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -050011676 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -040011677 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040011678 else
James Smart3772a992009-05-22 14:50:54 -040011679 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040011680
James Smart3772a992009-05-22 14:50:54 -040011681 return rc;
11682}
11683
11684/**
11685 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
11686 * @pdev: pointer to PCI device
11687 *
11688 * This routine is to be registered to the kernel's PCI subsystem. When an
11689 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
11690 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
11691 * remove routine, which will perform all the necessary cleanup for the
11692 * device to be removed from the PCI subsystem properly.
11693 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011694static void
James Smart3772a992009-05-22 14:50:54 -040011695lpfc_pci_remove_one(struct pci_dev *pdev)
11696{
11697 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11698 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11699
11700 switch (phba->pci_dev_grp) {
11701 case LPFC_PCI_DEV_LP:
11702 lpfc_pci_remove_one_s3(pdev);
11703 break;
James Smartda0436e2009-05-22 14:51:39 -040011704 case LPFC_PCI_DEV_OC:
11705 lpfc_pci_remove_one_s4(pdev);
11706 break;
James Smart3772a992009-05-22 14:50:54 -040011707 default:
11708 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11709 "1424 Invalid PCI device group: 0x%x\n",
11710 phba->pci_dev_grp);
11711 break;
11712 }
11713 return;
11714}
11715
11716/**
11717 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
11718 * @pdev: pointer to PCI device
11719 * @msg: power management message
11720 *
11721 * This routine is to be registered to the kernel's PCI subsystem to support
11722 * system Power Management (PM). When PM invokes this method, it dispatches
11723 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
11724 * suspend the device.
11725 *
11726 * Return code
11727 * 0 - driver suspended the device
11728 * Error otherwise
11729 **/
11730static int
11731lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
11732{
11733 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11734 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11735 int rc = -ENODEV;
11736
11737 switch (phba->pci_dev_grp) {
11738 case LPFC_PCI_DEV_LP:
11739 rc = lpfc_pci_suspend_one_s3(pdev, msg);
11740 break;
James Smartda0436e2009-05-22 14:51:39 -040011741 case LPFC_PCI_DEV_OC:
11742 rc = lpfc_pci_suspend_one_s4(pdev, msg);
11743 break;
James Smart3772a992009-05-22 14:50:54 -040011744 default:
11745 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11746 "1425 Invalid PCI device group: 0x%x\n",
11747 phba->pci_dev_grp);
11748 break;
11749 }
11750 return rc;
11751}
11752
11753/**
11754 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
11755 * @pdev: pointer to PCI device
11756 *
11757 * This routine is to be registered to the kernel's PCI subsystem to support
11758 * system Power Management (PM). When PM invokes this method, it dispatches
11759 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
11760 * resume the device.
11761 *
11762 * Return code
11763 * 0 - driver suspended the device
11764 * Error otherwise
11765 **/
11766static int
11767lpfc_pci_resume_one(struct pci_dev *pdev)
11768{
11769 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11770 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11771 int rc = -ENODEV;
11772
11773 switch (phba->pci_dev_grp) {
11774 case LPFC_PCI_DEV_LP:
11775 rc = lpfc_pci_resume_one_s3(pdev);
11776 break;
James Smartda0436e2009-05-22 14:51:39 -040011777 case LPFC_PCI_DEV_OC:
11778 rc = lpfc_pci_resume_one_s4(pdev);
11779 break;
James Smart3772a992009-05-22 14:50:54 -040011780 default:
11781 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11782 "1426 Invalid PCI device group: 0x%x\n",
11783 phba->pci_dev_grp);
11784 break;
11785 }
11786 return rc;
11787}
11788
11789/**
11790 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
11791 * @pdev: pointer to PCI device.
11792 * @state: the current PCI connection state.
11793 *
11794 * This routine is registered to the PCI subsystem for error handling. This
11795 * function is called by the PCI subsystem after a PCI bus error affecting
11796 * this device has been detected. When this routine is invoked, it dispatches
11797 * the action to the proper SLI-3 or SLI-4 device error detected handling
11798 * routine, which will perform the proper error detected operation.
11799 *
11800 * Return codes
11801 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
11802 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11803 **/
11804static pci_ers_result_t
11805lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
11806{
11807 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11808 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11809 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
11810
11811 switch (phba->pci_dev_grp) {
11812 case LPFC_PCI_DEV_LP:
11813 rc = lpfc_io_error_detected_s3(pdev, state);
11814 break;
James Smartda0436e2009-05-22 14:51:39 -040011815 case LPFC_PCI_DEV_OC:
11816 rc = lpfc_io_error_detected_s4(pdev, state);
11817 break;
James Smart3772a992009-05-22 14:50:54 -040011818 default:
11819 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11820 "1427 Invalid PCI device group: 0x%x\n",
11821 phba->pci_dev_grp);
11822 break;
11823 }
11824 return rc;
11825}
11826
11827/**
11828 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
11829 * @pdev: pointer to PCI device.
11830 *
11831 * This routine is registered to the PCI subsystem for error handling. This
11832 * function is called after PCI bus has been reset to restart the PCI card
11833 * from scratch, as if from a cold-boot. When this routine is invoked, it
11834 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
11835 * routine, which will perform the proper device reset.
11836 *
11837 * Return codes
11838 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
11839 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11840 **/
11841static pci_ers_result_t
11842lpfc_io_slot_reset(struct pci_dev *pdev)
11843{
11844 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11845 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11846 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
11847
11848 switch (phba->pci_dev_grp) {
11849 case LPFC_PCI_DEV_LP:
11850 rc = lpfc_io_slot_reset_s3(pdev);
11851 break;
James Smartda0436e2009-05-22 14:51:39 -040011852 case LPFC_PCI_DEV_OC:
11853 rc = lpfc_io_slot_reset_s4(pdev);
11854 break;
James Smart3772a992009-05-22 14:50:54 -040011855 default:
11856 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11857 "1428 Invalid PCI device group: 0x%x\n",
11858 phba->pci_dev_grp);
11859 break;
11860 }
11861 return rc;
11862}
11863
11864/**
11865 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
11866 * @pdev: pointer to PCI device
11867 *
11868 * This routine is registered to the PCI subsystem for error handling. It
11869 * is called when kernel error recovery tells the lpfc driver that it is
11870 * OK to resume normal PCI operation after PCI bus error recovery. When
11871 * this routine is invoked, it dispatches the action to the proper SLI-3
11872 * or SLI-4 device io_resume routine, which will resume the device operation.
11873 **/
11874static void
11875lpfc_io_resume(struct pci_dev *pdev)
11876{
11877 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11878 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11879
11880 switch (phba->pci_dev_grp) {
11881 case LPFC_PCI_DEV_LP:
11882 lpfc_io_resume_s3(pdev);
11883 break;
James Smartda0436e2009-05-22 14:51:39 -040011884 case LPFC_PCI_DEV_OC:
11885 lpfc_io_resume_s4(pdev);
11886 break;
James Smart3772a992009-05-22 14:50:54 -040011887 default:
11888 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11889 "1429 Invalid PCI device group: 0x%x\n",
11890 phba->pci_dev_grp);
11891 break;
11892 }
11893 return;
11894}
11895
James Smart1ba981f2014-02-20 09:56:45 -050011896/**
11897 * lpfc_sli4_oas_verify - Verify OAS is supported by this adapter
11898 * @phba: pointer to lpfc hba data structure.
11899 *
11900 * This routine checks to see if OAS is supported for this adapter. If
11901 * supported, the configure Flash Optimized Fabric flag is set. Otherwise,
11902 * the enable oas flag is cleared and the pool created for OAS device data
11903 * is destroyed.
11904 *
11905 **/
11906void
11907lpfc_sli4_oas_verify(struct lpfc_hba *phba)
11908{
11909
11910 if (!phba->cfg_EnableXLane)
11911 return;
11912
11913 if (phba->sli4_hba.pc_sli4_params.oas_supported) {
11914 phba->cfg_fof = 1;
11915 } else {
James Smartf38fa0b2014-04-04 13:52:21 -040011916 phba->cfg_fof = 0;
James Smart1ba981f2014-02-20 09:56:45 -050011917 if (phba->device_data_mem_pool)
11918 mempool_destroy(phba->device_data_mem_pool);
11919 phba->device_data_mem_pool = NULL;
11920 }
11921
11922 return;
11923}
11924
11925/**
11926 * lpfc_fof_queue_setup - Set up all the fof queues
11927 * @phba: pointer to lpfc hba data structure.
11928 *
11929 * This routine is invoked to set up all the fof queues for the FC HBA
11930 * operation.
11931 *
11932 * Return codes
11933 * 0 - successful
11934 * -ENOMEM - No available memory
11935 **/
11936int
11937lpfc_fof_queue_setup(struct lpfc_hba *phba)
11938{
James Smart895427b2017-02-12 13:52:30 -080011939 struct lpfc_sli_ring *pring;
James Smart1ba981f2014-02-20 09:56:45 -050011940 int rc;
11941
11942 rc = lpfc_eq_create(phba, phba->sli4_hba.fof_eq, LPFC_MAX_IMAX);
11943 if (rc)
11944 return -ENOMEM;
11945
James Smartf38fa0b2014-04-04 13:52:21 -040011946 if (phba->cfg_fof) {
James Smart1ba981f2014-02-20 09:56:45 -050011947
11948 rc = lpfc_cq_create(phba, phba->sli4_hba.oas_cq,
11949 phba->sli4_hba.fof_eq, LPFC_WCQ, LPFC_FCP);
11950 if (rc)
11951 goto out_oas_cq;
11952
11953 rc = lpfc_wq_create(phba, phba->sli4_hba.oas_wq,
11954 phba->sli4_hba.oas_cq, LPFC_FCP);
11955 if (rc)
11956 goto out_oas_wq;
11957
James Smart895427b2017-02-12 13:52:30 -080011958 /* Bind this CQ/WQ to the NVME ring */
11959 pring = phba->sli4_hba.oas_wq->pring;
11960 pring->sli.sli4.wqp =
11961 (void *)phba->sli4_hba.oas_wq;
11962 phba->sli4_hba.oas_cq->pring = pring;
James Smart1ba981f2014-02-20 09:56:45 -050011963 }
11964
11965 return 0;
11966
11967out_oas_wq:
James Smartf38fa0b2014-04-04 13:52:21 -040011968 lpfc_cq_destroy(phba, phba->sli4_hba.oas_cq);
James Smart1ba981f2014-02-20 09:56:45 -050011969out_oas_cq:
11970 lpfc_eq_destroy(phba, phba->sli4_hba.fof_eq);
11971 return rc;
11972
11973}
11974
11975/**
11976 * lpfc_fof_queue_create - Create all the fof queues
11977 * @phba: pointer to lpfc hba data structure.
11978 *
11979 * This routine is invoked to allocate all the fof queues for the FC HBA
11980 * operation. For each SLI4 queue type, the parameters such as queue entry
11981 * count (queue depth) shall be taken from the module parameter. For now,
11982 * we just use some constant number as place holder.
11983 *
11984 * Return codes
11985 * 0 - successful
11986 * -ENOMEM - No availble memory
11987 * -EIO - The mailbox failed to complete successfully.
11988 **/
11989int
11990lpfc_fof_queue_create(struct lpfc_hba *phba)
11991{
11992 struct lpfc_queue *qdesc;
11993
11994 /* Create FOF EQ */
11995 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
11996 phba->sli4_hba.eq_ecount);
11997 if (!qdesc)
11998 goto out_error;
11999
12000 phba->sli4_hba.fof_eq = qdesc;
12001
James Smartf38fa0b2014-04-04 13:52:21 -040012002 if (phba->cfg_fof) {
James Smart1ba981f2014-02-20 09:56:45 -050012003
12004 /* Create OAS CQ */
12005 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
12006 phba->sli4_hba.cq_ecount);
12007 if (!qdesc)
12008 goto out_error;
12009
12010 phba->sli4_hba.oas_cq = qdesc;
12011
12012 /* Create OAS WQ */
12013 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
12014 phba->sli4_hba.wq_ecount);
12015 if (!qdesc)
12016 goto out_error;
12017
12018 phba->sli4_hba.oas_wq = qdesc;
James Smart895427b2017-02-12 13:52:30 -080012019 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
James Smart1ba981f2014-02-20 09:56:45 -050012020
12021 }
12022 return 0;
12023
12024out_error:
12025 lpfc_fof_queue_destroy(phba);
12026 return -ENOMEM;
12027}
12028
12029/**
12030 * lpfc_fof_queue_destroy - Destroy all the fof queues
12031 * @phba: pointer to lpfc hba data structure.
12032 *
12033 * This routine is invoked to release all the SLI4 queues with the FC HBA
12034 * operation.
12035 *
12036 * Return codes
12037 * 0 - successful
12038 **/
12039int
12040lpfc_fof_queue_destroy(struct lpfc_hba *phba)
12041{
12042 /* Release FOF Event queue */
12043 if (phba->sli4_hba.fof_eq != NULL) {
12044 lpfc_sli4_queue_free(phba->sli4_hba.fof_eq);
12045 phba->sli4_hba.fof_eq = NULL;
12046 }
12047
12048 /* Release OAS Completion queue */
12049 if (phba->sli4_hba.oas_cq != NULL) {
12050 lpfc_sli4_queue_free(phba->sli4_hba.oas_cq);
12051 phba->sli4_hba.oas_cq = NULL;
12052 }
12053
12054 /* Release OAS Work queue */
12055 if (phba->sli4_hba.oas_wq != NULL) {
12056 lpfc_sli4_queue_free(phba->sli4_hba.oas_wq);
12057 phba->sli4_hba.oas_wq = NULL;
12058 }
12059 return 0;
12060}
12061
dea31012005-04-17 16:05:31 -050012062MODULE_DEVICE_TABLE(pci, lpfc_id_table);
12063
Stephen Hemmingera55b2d22012-09-07 09:33:16 -070012064static const struct pci_error_handlers lpfc_err_handler = {
Linas Vepstas8d63f372007-02-14 14:28:36 -060012065 .error_detected = lpfc_io_error_detected,
12066 .slot_reset = lpfc_io_slot_reset,
12067 .resume = lpfc_io_resume,
12068};
12069
dea31012005-04-17 16:05:31 -050012070static struct pci_driver lpfc_driver = {
12071 .name = LPFC_DRIVER_NAME,
12072 .id_table = lpfc_id_table,
12073 .probe = lpfc_pci_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080012074 .remove = lpfc_pci_remove_one,
Anton Blanchard85e8a232017-02-13 08:49:20 +110012075 .shutdown = lpfc_pci_remove_one,
James Smart3a55b532008-12-04 22:38:54 -050012076 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -040012077 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -050012078 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -050012079};
12080
James Smart3ef6d242012-01-18 16:23:48 -050012081static const struct file_operations lpfc_mgmt_fop = {
Al Viro858feac2013-04-14 22:39:37 -040012082 .owner = THIS_MODULE,
James Smart3ef6d242012-01-18 16:23:48 -050012083};
12084
12085static struct miscdevice lpfc_mgmt_dev = {
12086 .minor = MISC_DYNAMIC_MINOR,
12087 .name = "lpfcmgmt",
12088 .fops = &lpfc_mgmt_fop,
12089};
12090
James Smarte59058c2008-08-24 21:49:00 -040012091/**
James Smart3621a712009-04-06 18:47:14 -040012092 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -040012093 *
12094 * This routine is to be invoked when the lpfc module is loaded into the
12095 * kernel. The special kernel macro module_init() is used to indicate the
12096 * role of this routine to the kernel as lpfc module entry point.
12097 *
12098 * Return codes
12099 * 0 - successful
12100 * -ENOMEM - FC attach transport failed
12101 * all others - failed
12102 */
dea31012005-04-17 16:05:31 -050012103static int __init
12104lpfc_init(void)
12105{
12106 int error = 0;
12107
12108 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040012109 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -050012110
James Smart3ef6d242012-01-18 16:23:48 -050012111 error = misc_register(&lpfc_mgmt_dev);
12112 if (error)
12113 printk(KERN_ERR "Could not register lpfcmgmt device, "
12114 "misc_register returned with status %d", error);
12115
James Smart458c0832016-07-06 12:36:07 -070012116 lpfc_transport_functions.vport_create = lpfc_vport_create;
12117 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
dea31012005-04-17 16:05:31 -050012118 lpfc_transport_template =
12119 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -040012120 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -050012121 return -ENOMEM;
James Smart458c0832016-07-06 12:36:07 -070012122 lpfc_vport_transport_template =
12123 fc_attach_transport(&lpfc_vport_transport_functions);
12124 if (lpfc_vport_transport_template == NULL) {
12125 fc_release_transport(lpfc_transport_template);
12126 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -040012127 }
James Smart7bb03bb2013-04-17 20:19:16 -040012128
12129 /* Initialize in case vector mapping is needed */
James Smartb246de12013-05-31 17:03:07 -040012130 lpfc_used_cpu = NULL;
James Smart2ea259e2017-02-12 13:52:27 -080012131 lpfc_present_cpu = num_present_cpus();
James Smart7bb03bb2013-04-17 20:19:16 -040012132
dea31012005-04-17 16:05:31 -050012133 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -050012134 if (error) {
dea31012005-04-17 16:05:31 -050012135 fc_release_transport(lpfc_transport_template);
James Smart458c0832016-07-06 12:36:07 -070012136 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -050012137 }
dea31012005-04-17 16:05:31 -050012138
12139 return error;
12140}
12141
James Smarte59058c2008-08-24 21:49:00 -040012142/**
James Smart3621a712009-04-06 18:47:14 -040012143 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -040012144 *
12145 * This routine is invoked when the lpfc module is removed from the kernel.
12146 * The special kernel macro module_exit() is used to indicate the role of
12147 * this routine to the kernel as lpfc module exit point.
12148 */
dea31012005-04-17 16:05:31 -050012149static void __exit
12150lpfc_exit(void)
12151{
James Smart3ef6d242012-01-18 16:23:48 -050012152 misc_deregister(&lpfc_mgmt_dev);
dea31012005-04-17 16:05:31 -050012153 pci_unregister_driver(&lpfc_driver);
12154 fc_release_transport(lpfc_transport_template);
James Smart458c0832016-07-06 12:36:07 -070012155 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -050012156 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040012157 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
12158 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050012159 (1L << _dump_buf_data_order), _dump_buf_data);
12160 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
12161 }
12162
12163 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -040012164 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
12165 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050012166 (1L << _dump_buf_dif_order), _dump_buf_dif);
12167 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
12168 }
James Smartb246de12013-05-31 17:03:07 -040012169 kfree(lpfc_used_cpu);
Johannes Thumshirn79739672015-08-31 16:48:11 -040012170 idr_destroy(&lpfc_hba_index);
dea31012005-04-17 16:05:31 -050012171}
12172
12173module_init(lpfc_init);
12174module_exit(lpfc_exit);
12175MODULE_LICENSE("GPL");
12176MODULE_DESCRIPTION(LPFC_MODULE_DESC);
James Smartd080abe2017-02-12 13:52:39 -080012177MODULE_AUTHOR("Broadcom");
dea31012005-04-17 16:05:31 -050012178MODULE_VERSION("0:" LPFC_DRIVER_VERSION);