blob: 57087ba4834fa1c73ac93ef5f8a65611ac93cbdc [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 Smart50611572016-03-31 14:12:34 -07004 * Copyright (C) 2004-2016 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/delay.h>
24#include <linux/dma-mapping.h>
25#include <linux/idr.h>
26#include <linux/interrupt.h>
Paul Gortmakeracf3368f2011-05-27 09:47:43 -040027#include <linux/module.h>
dea31012005-04-17 16:05:31 -050028#include <linux/kthread.h>
29#include <linux/pci.h>
30#include <linux/spinlock.h>
James Smart92d7f7b2007-06-17 19:56:38 -050031#include <linux/ctype.h>
James Smart0d878412009-10-02 15:16:56 -040032#include <linux/aer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
James Smart52d52442011-05-24 11:42:45 -040034#include <linux/firmware.h>
James Smart3ef6d242012-01-18 16:23:48 -050035#include <linux/miscdevice.h>
James Smart7bb03bb2013-04-17 20:19:16 -040036#include <linux/percpu.h>
James Smart895427b2017-02-12 13:52:30 -080037#include <linux/msi.h>
dea31012005-04-17 16:05:31 -050038
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040039#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050040#include <scsi/scsi_device.h>
41#include <scsi/scsi_host.h>
42#include <scsi/scsi_transport_fc.h>
43
James Smartda0436e2009-05-22 14:51:39 -040044#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050045#include "lpfc_hw.h"
46#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040047#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040048#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050049#include "lpfc_disc.h"
dea31012005-04-17 16:05:31 -050050#include "lpfc.h"
James Smart895427b2017-02-12 13:52:30 -080051#include "lpfc_scsi.h"
52#include "lpfc_nvme.h"
dea31012005-04-17 16:05:31 -050053#include "lpfc_logmsg.h"
54#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050055#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050056#include "lpfc_version.h"
James Smart12f44452016-07-06 12:36:03 -070057#include "lpfc_ids.h"
dea31012005-04-17 16:05:31 -050058
James Smart81301a92008-12-04 22:39:46 -050059char *_dump_buf_data;
60unsigned long _dump_buf_data_order;
61char *_dump_buf_dif;
62unsigned long _dump_buf_dif_order;
63spinlock_t _dump_buf_lock;
64
James Smart7bb03bb2013-04-17 20:19:16 -040065/* Used when mapping IRQ vectors in a driver centric manner */
James Smartb246de12013-05-31 17:03:07 -040066uint16_t *lpfc_used_cpu;
67uint32_t lpfc_present_cpu;
James Smart7bb03bb2013-04-17 20:19:16 -040068
dea31012005-04-17 16:05:31 -050069static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
70static int lpfc_post_rcv_buf(struct lpfc_hba *);
James Smart5350d872011-10-10 21:33:49 -040071static int lpfc_sli4_queue_verify(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040072static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
73static int lpfc_setup_endian_order(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040074static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
James Smart8a9d2e82012-05-09 21:16:12 -040075static void lpfc_free_els_sgl_list(struct lpfc_hba *);
76static void lpfc_init_sgl_list(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040077static int lpfc_init_active_sgl_array(struct lpfc_hba *);
78static void lpfc_free_active_sgl(struct lpfc_hba *);
79static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
80static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
81static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
82static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
83static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
James Smart618a5232012-06-12 13:54:36 -040084static void lpfc_sli4_disable_intr(struct lpfc_hba *);
85static uint32_t lpfc_sli4_enable_intr(struct lpfc_hba *, uint32_t);
James Smart1ba981f2014-02-20 09:56:45 -050086static void lpfc_sli4_oas_verify(struct lpfc_hba *phba);
dea31012005-04-17 16:05:31 -050087
88static struct scsi_transport_template *lpfc_transport_template = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050089static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea31012005-04-17 16:05:31 -050090static DEFINE_IDR(lpfc_hba_index);
91
James Smarte59058c2008-08-24 21:49:00 -040092/**
James Smart3621a712009-04-06 18:47:14 -040093 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
James Smarte59058c2008-08-24 21:49:00 -040094 * @phba: pointer to lpfc hba data structure.
95 *
96 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
97 * mailbox command. It retrieves the revision information from the HBA and
98 * collects the Vital Product Data (VPD) about the HBA for preparing the
99 * configuration of the HBA.
100 *
101 * Return codes:
102 * 0 - success.
103 * -ERESTART - requests the SLI layer to reset the HBA and try again.
104 * Any other value - indicates an error.
105 **/
dea31012005-04-17 16:05:31 -0500106int
James Smart2e0fef82007-06-17 19:56:36 -0500107lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500108{
109 lpfc_vpd_t *vp = &phba->vpd;
110 int i = 0, rc;
111 LPFC_MBOXQ_t *pmb;
112 MAILBOX_t *mb;
113 char *lpfc_vpd_data = NULL;
114 uint16_t offset = 0;
115 static char licensed[56] =
116 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -0400117 static int init_key = 1;
dea31012005-04-17 16:05:31 -0500118
119 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
120 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500121 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500122 return -ENOMEM;
123 }
124
James Smart04c68492009-05-22 14:52:52 -0400125 mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -0500126 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500127
128 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -0400129 if (init_key) {
130 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -0500131
James Smart65a29c12006-07-06 15:50:50 -0400132 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
133 *ptext = cpu_to_be32(*ptext);
134 init_key = 0;
135 }
dea31012005-04-17 16:05:31 -0500136
137 lpfc_read_nv(phba, pmb);
138 memset((char*)mb->un.varRDnvp.rsvd3, 0,
139 sizeof (mb->un.varRDnvp.rsvd3));
140 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
141 sizeof (licensed));
142
143 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
144
145 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500146 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smarte8b62012007-08-02 11:10:09 -0400147 "0324 Config Port initialization "
dea31012005-04-17 16:05:31 -0500148 "error, mbxCmd x%x READ_NVPARM, "
149 "mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500150 mb->mbxCommand, mb->mbxStatus);
151 mempool_free(pmb, phba->mbox_mem_pool);
152 return -ERESTART;
153 }
154 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
James Smart2e0fef82007-06-17 19:56:36 -0500155 sizeof(phba->wwnn));
156 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
157 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500158 }
159
James Smart92d7f7b2007-06-17 19:56:38 -0500160 phba->sli3_options = 0x0;
161
dea31012005-04-17 16:05:31 -0500162 /* Setup and issue mailbox READ REV command */
163 lpfc_read_rev(phba, pmb);
164 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
165 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500166 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400167 "0439 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500168 "READ_REV, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500169 mb->mbxCommand, mb->mbxStatus);
170 mempool_free( pmb, phba->mbox_mem_pool);
171 return -ERESTART;
172 }
173
James Smart92d7f7b2007-06-17 19:56:38 -0500174
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500175 /*
176 * The value of rr must be 1 since the driver set the cv field to 1.
177 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500178 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500179 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500180 vp->rev.rBit = 0;
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500181 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400182 "0440 Adapter failed to init, READ_REV has "
183 "missing revision information.\n");
dea31012005-04-17 16:05:31 -0500184 mempool_free(pmb, phba->mbox_mem_pool);
185 return -ERESTART;
dea31012005-04-17 16:05:31 -0500186 }
187
James Smart495a7142008-06-14 22:52:59 -0400188 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
189 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -0500190 return -EINVAL;
James Smart495a7142008-06-14 22:52:59 -0400191 }
James Smarted957682007-06-17 19:56:37 -0500192
dea31012005-04-17 16:05:31 -0500193 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500194 vp->rev.rBit = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500195 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500196 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
197 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
198 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
199 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500200 vp->rev.biuRev = mb->un.varRdRev.biuRev;
201 vp->rev.smRev = mb->un.varRdRev.smRev;
202 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
203 vp->rev.endecRev = mb->un.varRdRev.endecRev;
204 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
205 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
206 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
207 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
208 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
209 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
210
James Smart92d7f7b2007-06-17 19:56:38 -0500211 /* If the sli feature level is less then 9, we must
212 * tear down all RPIs and VPIs on link down if NPIV
213 * is enabled.
214 */
215 if (vp->rev.feaLevelHigh < 9)
216 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
217
dea31012005-04-17 16:05:31 -0500218 if (lpfc_is_LC_HBA(phba->pcidev->device))
219 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
220 sizeof (phba->RandomData));
221
dea31012005-04-17 16:05:31 -0500222 /* Get adapter VPD information */
dea31012005-04-17 16:05:31 -0500223 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
224 if (!lpfc_vpd_data)
James Smartd7c255b2008-08-24 21:50:00 -0400225 goto out_free_mbox;
dea31012005-04-17 16:05:31 -0500226 do {
James Smarta0c87cb2009-07-19 10:01:10 -0400227 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
dea31012005-04-17 16:05:31 -0500228 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
229
230 if (rc != MBX_SUCCESS) {
231 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400232 "0441 VPD not present on adapter, "
dea31012005-04-17 16:05:31 -0500233 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500234 mb->mbxCommand, mb->mbxStatus);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500235 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500236 }
James Smart04c68492009-05-22 14:52:52 -0400237 /* dump mem may return a zero when finished or we got a
238 * mailbox error, either way we are done.
239 */
240 if (mb->un.varDmp.word_cnt == 0)
241 break;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500242 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
243 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
James Smartd7c255b2008-08-24 21:50:00 -0400244 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
245 lpfc_vpd_data + offset,
James Smart92d7f7b2007-06-17 19:56:38 -0500246 mb->un.varDmp.word_cnt);
dea31012005-04-17 16:05:31 -0500247 offset += mb->un.varDmp.word_cnt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500248 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
249 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500250
251 kfree(lpfc_vpd_data);
dea31012005-04-17 16:05:31 -0500252out_free_mbox:
253 mempool_free(pmb, phba->mbox_mem_pool);
254 return 0;
255}
256
James Smarte59058c2008-08-24 21:49:00 -0400257/**
James Smart3621a712009-04-06 18:47:14 -0400258 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
James Smarte59058c2008-08-24 21:49:00 -0400259 * @phba: pointer to lpfc hba data structure.
260 * @pmboxq: pointer to the driver internal queue element for mailbox command.
261 *
262 * This is the completion handler for driver's configuring asynchronous event
263 * mailbox command to the device. If the mailbox command returns successfully,
264 * it will set internal async event support flag to 1; otherwise, it will
265 * set internal async event support flag to 0.
266 **/
James Smart57127f12007-10-27 13:37:05 -0400267static void
268lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
269{
James Smart04c68492009-05-22 14:52:52 -0400270 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
James Smart57127f12007-10-27 13:37:05 -0400271 phba->temp_sensor_support = 1;
272 else
273 phba->temp_sensor_support = 0;
274 mempool_free(pmboxq, phba->mbox_mem_pool);
275 return;
276}
277
James Smarte59058c2008-08-24 21:49:00 -0400278/**
James Smart3621a712009-04-06 18:47:14 -0400279 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
James Smart97207482008-12-04 22:39:19 -0500280 * @phba: pointer to lpfc hba data structure.
281 * @pmboxq: pointer to the driver internal queue element for mailbox command.
282 *
283 * This is the completion handler for dump mailbox command for getting
284 * wake up parameters. When this command complete, the response contain
285 * Option rom version of the HBA. This function translate the version number
286 * into a human readable string and store it in OptionROMVersion.
287 **/
288static void
289lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
290{
291 struct prog_id *prg;
292 uint32_t prog_id_word;
293 char dist = ' ';
294 /* character array used for decoding dist type. */
295 char dist_char[] = "nabx";
296
James Smart04c68492009-05-22 14:52:52 -0400297 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
James Smart9f1e1b52008-12-04 22:39:40 -0500298 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500299 return;
James Smart9f1e1b52008-12-04 22:39:40 -0500300 }
James Smart97207482008-12-04 22:39:19 -0500301
302 prg = (struct prog_id *) &prog_id_word;
303
304 /* word 7 contain option rom version */
James Smart04c68492009-05-22 14:52:52 -0400305 prog_id_word = pmboxq->u.mb.un.varWords[7];
James Smart97207482008-12-04 22:39:19 -0500306
307 /* Decode the Option rom version word to a readable string */
308 if (prg->dist < 4)
309 dist = dist_char[prg->dist];
310
311 if ((prg->dist == 3) && (prg->num == 0))
James Smarta2fc4aef2014-09-03 12:57:55 -0400312 snprintf(phba->OptionROMVersion, 32, "%d.%d%d",
James Smart97207482008-12-04 22:39:19 -0500313 prg->ver, prg->rev, prg->lev);
314 else
James Smarta2fc4aef2014-09-03 12:57:55 -0400315 snprintf(phba->OptionROMVersion, 32, "%d.%d%d%c%d",
James Smart97207482008-12-04 22:39:19 -0500316 prg->ver, prg->rev, prg->lev,
317 dist, prg->num);
James Smart9f1e1b52008-12-04 22:39:40 -0500318 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500319 return;
320}
321
322/**
James Smart05580562011-05-24 11:40:48 -0400323 * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
324 * cfg_soft_wwnn, cfg_soft_wwpn
325 * @vport: pointer to lpfc vport data structure.
326 *
327 *
328 * Return codes
329 * None.
330 **/
331void
332lpfc_update_vport_wwn(struct lpfc_vport *vport)
333{
334 /* If the soft name exists then update it using the service params */
335 if (vport->phba->cfg_soft_wwnn)
336 u64_to_wwn(vport->phba->cfg_soft_wwnn,
337 vport->fc_sparam.nodeName.u.wwn);
338 if (vport->phba->cfg_soft_wwpn)
339 u64_to_wwn(vport->phba->cfg_soft_wwpn,
340 vport->fc_sparam.portName.u.wwn);
341
342 /*
343 * If the name is empty or there exists a soft name
344 * then copy the service params name, otherwise use the fc name
345 */
346 if (vport->fc_nodename.u.wwn[0] == 0 || vport->phba->cfg_soft_wwnn)
347 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
348 sizeof(struct lpfc_name));
349 else
350 memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
351 sizeof(struct lpfc_name));
352
353 if (vport->fc_portname.u.wwn[0] == 0 || vport->phba->cfg_soft_wwpn)
354 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
355 sizeof(struct lpfc_name));
356 else
357 memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
358 sizeof(struct lpfc_name));
359}
360
361/**
James Smart3621a712009-04-06 18:47:14 -0400362 * lpfc_config_port_post - Perform lpfc initialization after config port
James Smarte59058c2008-08-24 21:49:00 -0400363 * @phba: pointer to lpfc hba data structure.
364 *
365 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
366 * command call. It performs all internal resource and state setups on the
367 * port: post IOCB buffers, enable appropriate host interrupt attentions,
368 * ELS ring timers, etc.
369 *
370 * Return codes
371 * 0 - success.
372 * Any other value - error.
373 **/
dea31012005-04-17 16:05:31 -0500374int
James Smart2e0fef82007-06-17 19:56:36 -0500375lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500376{
James Smart2e0fef82007-06-17 19:56:36 -0500377 struct lpfc_vport *vport = phba->pport;
James Smarta257bf92009-04-06 18:48:10 -0400378 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -0500379 LPFC_MBOXQ_t *pmb;
380 MAILBOX_t *mb;
381 struct lpfc_dmabuf *mp;
382 struct lpfc_sli *psli = &phba->sli;
383 uint32_t status, timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500384 int i, j;
385 int rc;
dea31012005-04-17 16:05:31 -0500386
James Smart7af67052007-10-27 13:38:11 -0400387 spin_lock_irq(&phba->hbalock);
388 /*
389 * If the Config port completed correctly the HBA is not
390 * over heated any more.
391 */
392 if (phba->over_temp_state == HBA_OVER_TEMP)
393 phba->over_temp_state = HBA_NORMAL_TEMP;
394 spin_unlock_irq(&phba->hbalock);
395
dea31012005-04-17 16:05:31 -0500396 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
397 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500398 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500399 return -ENOMEM;
400 }
James Smart04c68492009-05-22 14:52:52 -0400401 mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500402
dea31012005-04-17 16:05:31 -0500403 /* Get login parameters for NID. */
James Smart9f1177a2010-02-26 14:12:57 -0500404 rc = lpfc_read_sparam(phba, pmb, 0);
405 if (rc) {
406 mempool_free(pmb, phba->mbox_mem_pool);
407 return -ENOMEM;
408 }
409
James Smarted957682007-06-17 19:56:37 -0500410 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500411 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500412 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400413 "0448 Adapter failed init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500414 "READ_SPARM mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500415 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500416 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500417 mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart9f1177a2010-02-26 14:12:57 -0500418 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500419 lpfc_mbuf_free(phba, mp->virt, mp->phys);
420 kfree(mp);
421 return -EIO;
422 }
423
424 mp = (struct lpfc_dmabuf *) pmb->context1;
425
James Smart2e0fef82007-06-17 19:56:36 -0500426 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500427 lpfc_mbuf_free(phba, mp->virt, mp->phys);
428 kfree(mp);
429 pmb->context1 = NULL;
James Smart05580562011-05-24 11:40:48 -0400430 lpfc_update_vport_wwn(vport);
James Smarta257bf92009-04-06 18:48:10 -0400431
432 /* Update the fc_host data structures with new wwn. */
433 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
434 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart21e9a0a2009-05-22 14:53:21 -0400435 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smarta257bf92009-04-06 18:48:10 -0400436
dea31012005-04-17 16:05:31 -0500437 /* If no serial number in VPD data, use low 6 bytes of WWNN */
438 /* This should be consolidated into parse_vpd ? - mr */
439 if (phba->SerialNumber[0] == 0) {
440 uint8_t *outptr;
441
James Smart2e0fef82007-06-17 19:56:36 -0500442 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500443 for (i = 0; i < 12; i++) {
444 status = *outptr++;
445 j = ((status & 0xf0) >> 4);
446 if (j <= 9)
447 phba->SerialNumber[i] =
448 (char)((uint8_t) 0x30 + (uint8_t) j);
449 else
450 phba->SerialNumber[i] =
451 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
452 i++;
453 j = (status & 0xf);
454 if (j <= 9)
455 phba->SerialNumber[i] =
456 (char)((uint8_t) 0x30 + (uint8_t) j);
457 else
458 phba->SerialNumber[i] =
459 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
460 }
461 }
462
dea31012005-04-17 16:05:31 -0500463 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500464 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500465 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500466 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400467 "0453 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500468 "READ_CONFIG, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500469 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500470 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500471 mempool_free( pmb, phba->mbox_mem_pool);
472 return -EIO;
473 }
474
James Smarta0c87cb2009-07-19 10:01:10 -0400475 /* Check if the port is disabled */
476 lpfc_sli_read_link_ste(phba);
477
dea31012005-04-17 16:05:31 -0500478 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -0400479 i = (mb->un.varRdConfig.max_xri + 1);
480 if (phba->cfg_hba_queue_depth > i) {
481 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
482 "3359 HBA queue depth changed from %d to %d\n",
483 phba->cfg_hba_queue_depth, i);
484 phba->cfg_hba_queue_depth = i;
485 }
486
487 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
488 i = (mb->un.varRdConfig.max_xri >> 3);
489 if (phba->pport->cfg_lun_queue_depth > i) {
490 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
491 "3360 LUN queue depth changed from %d to %d\n",
492 phba->pport->cfg_lun_queue_depth, i);
493 phba->pport->cfg_lun_queue_depth = i;
494 }
dea31012005-04-17 16:05:31 -0500495
496 phba->lmt = mb->un.varRdConfig.lmt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500497
498 /* Get the default values for Model Name and Description */
499 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
500
James Smart2e0fef82007-06-17 19:56:36 -0500501 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500502
James Smart0b727fe2007-10-27 13:37:25 -0400503 /* Only process IOCBs on ELS ring till hba_state is READY */
James Smart895427b2017-02-12 13:52:30 -0800504 if (psli->sli3_ring[LPFC_EXTRA_RING].sli.sli3.cmdringaddr)
505 psli->sli3_ring[LPFC_EXTRA_RING].flag |= LPFC_STOP_IOCB_EVENT;
506 if (psli->sli3_ring[LPFC_FCP_RING].sli.sli3.cmdringaddr)
507 psli->sli3_ring[LPFC_FCP_RING].flag |= LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500508
509 /* Post receive buffers for desired rings */
James Smarted957682007-06-17 19:56:37 -0500510 if (phba->sli_rev != 3)
511 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500512
James Smart93996272008-08-24 21:50:30 -0400513 /*
514 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
515 */
516 if (phba->intr_type == MSIX) {
517 rc = lpfc_config_msi(phba, pmb);
518 if (rc) {
519 mempool_free(pmb, phba->mbox_mem_pool);
520 return -EIO;
521 }
522 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
523 if (rc != MBX_SUCCESS) {
524 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
525 "0352 Config MSI mailbox command "
526 "failed, mbxCmd x%x, mbxStatus x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400527 pmb->u.mb.mbxCommand,
528 pmb->u.mb.mbxStatus);
James Smart93996272008-08-24 21:50:30 -0400529 mempool_free(pmb, phba->mbox_mem_pool);
530 return -EIO;
531 }
532 }
533
James Smart04c68492009-05-22 14:52:52 -0400534 spin_lock_irq(&phba->hbalock);
James Smart93996272008-08-24 21:50:30 -0400535 /* Initialize ERATT handling flag */
536 phba->hba_flag &= ~HBA_ERATT_HANDLED;
537
dea31012005-04-17 16:05:31 -0500538 /* Enable appropriate host interrupts */
James Smart9940b972011-03-11 16:06:12 -0500539 if (lpfc_readl(phba->HCregaddr, &status)) {
540 spin_unlock_irq(&phba->hbalock);
541 return -EIO;
542 }
dea31012005-04-17 16:05:31 -0500543 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
544 if (psli->num_rings > 0)
545 status |= HC_R0INT_ENA;
546 if (psli->num_rings > 1)
547 status |= HC_R1INT_ENA;
548 if (psli->num_rings > 2)
549 status |= HC_R2INT_ENA;
550 if (psli->num_rings > 3)
551 status |= HC_R3INT_ENA;
552
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500553 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
554 (phba->cfg_poll & DISABLE_FCP_RING_INT))
James Smart93996272008-08-24 21:50:30 -0400555 status &= ~(HC_R0INT_ENA);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500556
dea31012005-04-17 16:05:31 -0500557 writel(status, phba->HCregaddr);
558 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500559 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500560
James Smart93996272008-08-24 21:50:30 -0400561 /* Set up ring-0 (ELS) timer */
562 timeout = phba->fc_ratov * 2;
James Smart256ec0d2013-04-17 20:14:58 -0400563 mod_timer(&vport->els_tmofunc,
564 jiffies + msecs_to_jiffies(1000 * timeout));
James Smart93996272008-08-24 21:50:30 -0400565 /* Set up heart beat (HB) timer */
James Smart256ec0d2013-04-17 20:14:58 -0400566 mod_timer(&phba->hb_tmofunc,
567 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -0500568 phba->hb_outstanding = 0;
569 phba->last_completion_time = jiffies;
James Smart93996272008-08-24 21:50:30 -0400570 /* Set up error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -0400571 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -0700572 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
dea31012005-04-17 16:05:31 -0500573
James Smarta0c87cb2009-07-19 10:01:10 -0400574 if (phba->hba_flag & LINK_DISABLED) {
575 lpfc_printf_log(phba,
576 KERN_ERR, LOG_INIT,
577 "2598 Adapter Link is disabled.\n");
578 lpfc_down_link(phba, pmb);
579 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
580 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
581 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
582 lpfc_printf_log(phba,
583 KERN_ERR, LOG_INIT,
584 "2599 Adapter failed to issue DOWN_LINK"
585 " mbox command rc 0x%x\n", rc);
586
587 mempool_free(pmb, phba->mbox_mem_pool);
588 return -EIO;
589 }
James Smarte40a02c2010-02-26 14:13:54 -0500590 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart026abb82011-12-13 13:20:45 -0500591 mempool_free(pmb, phba->mbox_mem_pool);
592 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
593 if (rc)
594 return rc;
dea31012005-04-17 16:05:31 -0500595 }
596 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400597 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500598 if (!pmb) {
599 phba->link_state = LPFC_HBA_ERROR;
600 return -ENOMEM;
601 }
602
James Smart57127f12007-10-27 13:37:05 -0400603 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
604 pmb->mbox_cmpl = lpfc_config_async_cmpl;
605 pmb->vport = phba->pport;
606 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500607
James Smart57127f12007-10-27 13:37:05 -0400608 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
609 lpfc_printf_log(phba,
610 KERN_ERR,
611 LOG_INIT,
612 "0456 Adapter failed to issue "
James Smarte4e74272009-07-19 10:01:38 -0400613 "ASYNCEVT_ENABLE mbox status x%x\n",
James Smart57127f12007-10-27 13:37:05 -0400614 rc);
615 mempool_free(pmb, phba->mbox_mem_pool);
616 }
James Smart97207482008-12-04 22:39:19 -0500617
618 /* Get Option rom version */
619 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500620 if (!pmb) {
621 phba->link_state = LPFC_HBA_ERROR;
622 return -ENOMEM;
623 }
624
James Smart97207482008-12-04 22:39:19 -0500625 lpfc_dump_wakeup_param(phba, pmb);
626 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
627 pmb->vport = phba->pport;
628 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
629
630 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
631 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
James Smarte4e74272009-07-19 10:01:38 -0400632 "to get Option ROM version status x%x\n", rc);
James Smart97207482008-12-04 22:39:19 -0500633 mempool_free(pmb, phba->mbox_mem_pool);
634 }
635
James Smartd7c255b2008-08-24 21:50:00 -0400636 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400637}
638
James Smarte59058c2008-08-24 21:49:00 -0400639/**
James Smart84d1b002010-02-12 14:42:33 -0500640 * lpfc_hba_init_link - Initialize the FC link
641 * @phba: pointer to lpfc hba data structure.
James Smart6e7288d2010-06-07 15:23:35 -0400642 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500643 *
644 * This routine will issue the INIT_LINK mailbox command call.
645 * It is available to other drivers through the lpfc_hba data
646 * structure for use as a delayed link up mechanism with the
647 * module parameter lpfc_suppress_link_up.
648 *
649 * Return code
650 * 0 - success
651 * Any other value - error
652 **/
Rashika Kheriae399b222014-09-03 12:55:28 -0400653static int
James Smart6e7288d2010-06-07 15:23:35 -0400654lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500655{
James Smart1b511972011-12-13 13:23:09 -0500656 return lpfc_hba_init_link_fc_topology(phba, phba->cfg_topology, flag);
657}
658
659/**
660 * lpfc_hba_init_link_fc_topology - Initialize FC link with desired topology
661 * @phba: pointer to lpfc hba data structure.
662 * @fc_topology: desired fc topology.
663 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
664 *
665 * This routine will issue the INIT_LINK mailbox command call.
666 * It is available to other drivers through the lpfc_hba data
667 * structure for use as a delayed link up mechanism with the
668 * module parameter lpfc_suppress_link_up.
669 *
670 * Return code
671 * 0 - success
672 * Any other value - error
673 **/
674int
675lpfc_hba_init_link_fc_topology(struct lpfc_hba *phba, uint32_t fc_topology,
676 uint32_t flag)
677{
James Smart84d1b002010-02-12 14:42:33 -0500678 struct lpfc_vport *vport = phba->pport;
679 LPFC_MBOXQ_t *pmb;
680 MAILBOX_t *mb;
681 int rc;
682
683 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
684 if (!pmb) {
685 phba->link_state = LPFC_HBA_ERROR;
686 return -ENOMEM;
687 }
688 mb = &pmb->u.mb;
689 pmb->vport = vport;
690
James Smart026abb82011-12-13 13:20:45 -0500691 if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_MAX) ||
692 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G) &&
693 !(phba->lmt & LMT_1Gb)) ||
694 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G) &&
695 !(phba->lmt & LMT_2Gb)) ||
696 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G) &&
697 !(phba->lmt & LMT_4Gb)) ||
698 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G) &&
699 !(phba->lmt & LMT_8Gb)) ||
700 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G) &&
701 !(phba->lmt & LMT_10Gb)) ||
702 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G) &&
James Smartd38dd522015-08-31 16:48:17 -0400703 !(phba->lmt & LMT_16Gb)) ||
704 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_32G) &&
705 !(phba->lmt & LMT_32Gb))) {
James Smart026abb82011-12-13 13:20:45 -0500706 /* Reset link speed to auto */
707 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
708 "1302 Invalid speed for this board:%d "
709 "Reset link speed to auto.\n",
710 phba->cfg_link_speed);
711 phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
712 }
James Smart1b511972011-12-13 13:23:09 -0500713 lpfc_init_link(phba, pmb, fc_topology, phba->cfg_link_speed);
James Smart84d1b002010-02-12 14:42:33 -0500714 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart1b511972011-12-13 13:23:09 -0500715 if (phba->sli_rev < LPFC_SLI_REV4)
716 lpfc_set_loopback_flag(phba);
James Smart6e7288d2010-06-07 15:23:35 -0400717 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart76a95d72010-11-20 23:11:48 -0500718 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
James Smart84d1b002010-02-12 14:42:33 -0500719 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
720 "0498 Adapter failed to init, mbxCmd x%x "
721 "INIT_LINK, mbxStatus x%x\n",
722 mb->mbxCommand, mb->mbxStatus);
James Smart76a95d72010-11-20 23:11:48 -0500723 if (phba->sli_rev <= LPFC_SLI_REV3) {
724 /* Clear all interrupt enable conditions */
725 writel(0, phba->HCregaddr);
726 readl(phba->HCregaddr); /* flush */
727 /* Clear all pending interrupts */
728 writel(0xffffffff, phba->HAregaddr);
729 readl(phba->HAregaddr); /* flush */
730 }
James Smart84d1b002010-02-12 14:42:33 -0500731 phba->link_state = LPFC_HBA_ERROR;
James Smart6e7288d2010-06-07 15:23:35 -0400732 if (rc != MBX_BUSY || flag == MBX_POLL)
James Smart84d1b002010-02-12 14:42:33 -0500733 mempool_free(pmb, phba->mbox_mem_pool);
734 return -EIO;
735 }
James Smarte40a02c2010-02-26 14:13:54 -0500736 phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
James Smart6e7288d2010-06-07 15:23:35 -0400737 if (flag == MBX_POLL)
738 mempool_free(pmb, phba->mbox_mem_pool);
James Smart84d1b002010-02-12 14:42:33 -0500739
740 return 0;
741}
742
743/**
744 * lpfc_hba_down_link - this routine downs the FC link
James Smart6e7288d2010-06-07 15:23:35 -0400745 * @phba: pointer to lpfc hba data structure.
746 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500747 *
748 * This routine will issue the DOWN_LINK mailbox command call.
749 * It is available to other drivers through the lpfc_hba data
750 * structure for use to stop the link.
751 *
752 * Return code
753 * 0 - success
754 * Any other value - error
755 **/
Rashika Kheriae399b222014-09-03 12:55:28 -0400756static int
James Smart6e7288d2010-06-07 15:23:35 -0400757lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500758{
759 LPFC_MBOXQ_t *pmb;
760 int rc;
761
762 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
763 if (!pmb) {
764 phba->link_state = LPFC_HBA_ERROR;
765 return -ENOMEM;
766 }
767
768 lpfc_printf_log(phba,
769 KERN_ERR, LOG_INIT,
770 "0491 Adapter Link is disabled.\n");
771 lpfc_down_link(phba, pmb);
772 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6e7288d2010-06-07 15:23:35 -0400773 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart84d1b002010-02-12 14:42:33 -0500774 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
775 lpfc_printf_log(phba,
776 KERN_ERR, LOG_INIT,
777 "2522 Adapter failed to issue DOWN_LINK"
778 " mbox command rc 0x%x\n", rc);
779
780 mempool_free(pmb, phba->mbox_mem_pool);
781 return -EIO;
782 }
James Smart6e7288d2010-06-07 15:23:35 -0400783 if (flag == MBX_POLL)
784 mempool_free(pmb, phba->mbox_mem_pool);
785
James Smart84d1b002010-02-12 14:42:33 -0500786 return 0;
787}
788
789/**
James Smart3621a712009-04-06 18:47:14 -0400790 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400791 * @phba: pointer to lpfc HBA data structure.
792 *
793 * This routine will do LPFC uninitialization before the HBA is reset when
794 * bringing down the SLI Layer.
795 *
796 * Return codes
797 * 0 - success.
798 * Any other value - error.
799 **/
dea31012005-04-17 16:05:31 -0500800int
James Smart2e0fef82007-06-17 19:56:36 -0500801lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500802{
James Smart1b32f6a2008-02-08 18:49:39 -0500803 struct lpfc_vport **vports;
804 int i;
James Smart3772a992009-05-22 14:50:54 -0400805
806 if (phba->sli_rev <= LPFC_SLI_REV3) {
807 /* Disable interrupts */
808 writel(0, phba->HCregaddr);
809 readl(phba->HCregaddr); /* flush */
810 }
dea31012005-04-17 16:05:31 -0500811
James Smart1b32f6a2008-02-08 18:49:39 -0500812 if (phba->pport->load_flag & FC_UNLOADING)
813 lpfc_cleanup_discovery_resources(phba->pport);
814 else {
815 vports = lpfc_create_vport_work_array(phba);
816 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400817 for (i = 0; i <= phba->max_vports &&
818 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500819 lpfc_cleanup_discovery_resources(vports[i]);
820 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500821 }
822 return 0;
dea31012005-04-17 16:05:31 -0500823}
824
James Smarte59058c2008-08-24 21:49:00 -0400825/**
James Smart68e814f2014-05-21 08:04:59 -0400826 * lpfc_sli4_free_sp_events - Cleanup sp_queue_events to free
827 * rspiocb which got deferred
828 *
829 * @phba: pointer to lpfc HBA data structure.
830 *
831 * This routine will cleanup completed slow path events after HBA is reset
832 * when bringing down the SLI Layer.
833 *
834 *
835 * Return codes
836 * void.
837 **/
838static void
839lpfc_sli4_free_sp_events(struct lpfc_hba *phba)
840{
841 struct lpfc_iocbq *rspiocbq;
842 struct hbq_dmabuf *dmabuf;
843 struct lpfc_cq_event *cq_event;
844
845 spin_lock_irq(&phba->hbalock);
846 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
847 spin_unlock_irq(&phba->hbalock);
848
849 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
850 /* Get the response iocb from the head of work queue */
851 spin_lock_irq(&phba->hbalock);
852 list_remove_head(&phba->sli4_hba.sp_queue_event,
853 cq_event, struct lpfc_cq_event, list);
854 spin_unlock_irq(&phba->hbalock);
855
856 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
857 case CQE_CODE_COMPL_WQE:
858 rspiocbq = container_of(cq_event, struct lpfc_iocbq,
859 cq_event);
860 lpfc_sli_release_iocbq(phba, rspiocbq);
861 break;
862 case CQE_CODE_RECEIVE:
863 case CQE_CODE_RECEIVE_V1:
864 dmabuf = container_of(cq_event, struct hbq_dmabuf,
865 cq_event);
866 lpfc_in_buf_free(phba, &dmabuf->dbuf);
867 }
868 }
869}
870
871/**
James Smartbcece5f2014-04-04 13:51:34 -0400872 * lpfc_hba_free_post_buf - Perform lpfc uninitialization after HBA reset
873 * @phba: pointer to lpfc HBA data structure.
874 *
875 * This routine will cleanup posted ELS buffers after the HBA is reset
876 * when bringing down the SLI Layer.
877 *
878 *
879 * Return codes
880 * void.
881 **/
882static void
883lpfc_hba_free_post_buf(struct lpfc_hba *phba)
884{
885 struct lpfc_sli *psli = &phba->sli;
886 struct lpfc_sli_ring *pring;
887 struct lpfc_dmabuf *mp, *next_mp;
James Smart07eab622014-04-04 13:51:44 -0400888 LIST_HEAD(buflist);
889 int count;
James Smartbcece5f2014-04-04 13:51:34 -0400890
891 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
892 lpfc_sli_hbqbuf_free_all(phba);
893 else {
894 /* Cleanup preposted buffers on the ELS ring */
James Smart895427b2017-02-12 13:52:30 -0800895 pring = &psli->sli3_ring[LPFC_ELS_RING];
James Smart07eab622014-04-04 13:51:44 -0400896 spin_lock_irq(&phba->hbalock);
897 list_splice_init(&pring->postbufq, &buflist);
898 spin_unlock_irq(&phba->hbalock);
899
900 count = 0;
901 list_for_each_entry_safe(mp, next_mp, &buflist, list) {
James Smartbcece5f2014-04-04 13:51:34 -0400902 list_del(&mp->list);
James Smart07eab622014-04-04 13:51:44 -0400903 count++;
James Smartbcece5f2014-04-04 13:51:34 -0400904 lpfc_mbuf_free(phba, mp->virt, mp->phys);
905 kfree(mp);
906 }
James Smart07eab622014-04-04 13:51:44 -0400907
908 spin_lock_irq(&phba->hbalock);
909 pring->postbufq_cnt -= count;
James Smartbcece5f2014-04-04 13:51:34 -0400910 spin_unlock_irq(&phba->hbalock);
911 }
912}
913
914/**
915 * lpfc_hba_clean_txcmplq - Perform lpfc uninitialization after HBA reset
916 * @phba: pointer to lpfc HBA data structure.
917 *
918 * This routine will cleanup the txcmplq after the HBA is reset when bringing
919 * down the SLI Layer.
920 *
921 * Return codes
922 * void
923 **/
924static void
925lpfc_hba_clean_txcmplq(struct lpfc_hba *phba)
926{
927 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -0800928 struct lpfc_queue *qp = NULL;
James Smartbcece5f2014-04-04 13:51:34 -0400929 struct lpfc_sli_ring *pring;
930 LIST_HEAD(completions);
931 int i;
932
James Smart895427b2017-02-12 13:52:30 -0800933 if (phba->sli_rev != LPFC_SLI_REV4) {
934 for (i = 0; i < psli->num_rings; i++) {
935 pring = &psli->sli3_ring[i];
James Smartbcece5f2014-04-04 13:51:34 -0400936 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -0800937 /* At this point in time the HBA is either reset or DOA
938 * Nothing should be on txcmplq as it will
939 * NEVER complete.
940 */
941 list_splice_init(&pring->txcmplq, &completions);
942 pring->txcmplq_cnt = 0;
James Smartbcece5f2014-04-04 13:51:34 -0400943 spin_unlock_irq(&phba->hbalock);
944
James Smart895427b2017-02-12 13:52:30 -0800945 lpfc_sli_abort_iocb_ring(phba, pring);
946 }
James Smartbcece5f2014-04-04 13:51:34 -0400947 /* Cancel all the IOCBs from the completions list */
James Smart895427b2017-02-12 13:52:30 -0800948 lpfc_sli_cancel_iocbs(phba, &completions,
949 IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
950 return;
951 }
952 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
953 pring = qp->pring;
954 if (!pring)
955 continue;
956 spin_lock_irq(&pring->ring_lock);
957 list_splice_init(&pring->txcmplq, &completions);
958 pring->txcmplq_cnt = 0;
959 spin_unlock_irq(&pring->ring_lock);
James Smartbcece5f2014-04-04 13:51:34 -0400960 lpfc_sli_abort_iocb_ring(phba, pring);
961 }
James Smart895427b2017-02-12 13:52:30 -0800962 /* Cancel all the IOCBs from the completions list */
963 lpfc_sli_cancel_iocbs(phba, &completions,
964 IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
James Smartbcece5f2014-04-04 13:51:34 -0400965}
966
967/**
James Smart3772a992009-05-22 14:50:54 -0400968 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smartbcece5f2014-04-04 13:51:34 -0400969 int i;
James Smarte59058c2008-08-24 21:49:00 -0400970 * @phba: pointer to lpfc HBA data structure.
971 *
972 * This routine will do uninitialization after the HBA is reset when bring
973 * down the SLI Layer.
974 *
975 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200976 * 0 - success.
James Smarte59058c2008-08-24 21:49:00 -0400977 * Any other value - error.
978 **/
James Smart3772a992009-05-22 14:50:54 -0400979static int
980lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -0500981{
James Smartbcece5f2014-04-04 13:51:34 -0400982 lpfc_hba_free_post_buf(phba);
983 lpfc_hba_clean_txcmplq(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500984 return 0;
985}
James Smart5af5eee2010-10-22 11:06:38 -0400986
James Smartda0436e2009-05-22 14:51:39 -0400987/**
988 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
989 * @phba: pointer to lpfc HBA data structure.
990 *
991 * This routine will do uninitialization after the HBA is reset when bring
992 * down the SLI Layer.
993 *
994 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200995 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400996 * Any other value - error.
997 **/
998static int
999lpfc_hba_down_post_s4(struct lpfc_hba *phba)
1000{
1001 struct lpfc_scsi_buf *psb, *psb_next;
1002 LIST_HEAD(aborts);
James Smart895427b2017-02-12 13:52:30 -08001003 LIST_HEAD(nvme_aborts);
James Smartda0436e2009-05-22 14:51:39 -04001004 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -05001005 struct lpfc_sglq *sglq_entry = NULL;
1006
James Smart895427b2017-02-12 13:52:30 -08001007
1008 lpfc_sli_hbqbuf_free_all(phba);
James Smartbcece5f2014-04-04 13:51:34 -04001009 lpfc_hba_clean_txcmplq(phba);
1010
James Smartda0436e2009-05-22 14:51:39 -04001011 /* At this point in time the HBA is either reset or DOA. Either
1012 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
James Smart895427b2017-02-12 13:52:30 -08001013 * on the lpfc_els_sgl_list so that it can either be freed if the
James Smartda0436e2009-05-22 14:51:39 -04001014 * driver is unloading or reposted if the driver is restarting
1015 * the port.
1016 */
James Smart895427b2017-02-12 13:52:30 -08001017 spin_lock_irq(&phba->hbalock); /* required for lpfc_els_sgl_list and */
James Smartda0436e2009-05-22 14:51:39 -04001018 /* scsl_buf_list */
James Smart895427b2017-02-12 13:52:30 -08001019 /* sgl_list_lock required because worker thread uses this
James Smartda0436e2009-05-22 14:51:39 -04001020 * list.
1021 */
James Smart895427b2017-02-12 13:52:30 -08001022 spin_lock(&phba->sli4_hba.sgl_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -05001023 list_for_each_entry(sglq_entry,
1024 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
1025 sglq_entry->state = SGL_FREED;
1026
James Smartda0436e2009-05-22 14:51:39 -04001027 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
James Smart895427b2017-02-12 13:52:30 -08001028 &phba->sli4_hba.lpfc_els_sgl_list);
1029
1030 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04001031 /* abts_scsi_buf_list_lock required because worker thread uses this
1032 * list.
1033 */
James Smart895427b2017-02-12 13:52:30 -08001034 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
1035 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
1036 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
1037 &aborts);
1038 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
1039 }
1040
1041 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
1042 spin_lock(&phba->sli4_hba.abts_nvme_buf_list_lock);
1043 list_splice_init(&phba->sli4_hba.lpfc_abts_nvme_buf_list,
1044 &nvme_aborts);
1045 spin_unlock(&phba->sli4_hba.abts_nvme_buf_list_lock);
1046 }
1047
James Smartda0436e2009-05-22 14:51:39 -04001048 spin_unlock_irq(&phba->hbalock);
1049
1050 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
1051 psb->pCmd = NULL;
1052 psb->status = IOSTAT_SUCCESS;
1053 }
James Smarta40fc5f2013-04-17 20:17:40 -04001054 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
1055 list_splice(&aborts, &phba->lpfc_scsi_buf_list_put);
1056 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
James Smart68e814f2014-05-21 08:04:59 -04001057
James Smart895427b2017-02-12 13:52:30 -08001058 list_for_each_entry_safe(psb, psb_next, &nvme_aborts, list) {
1059 psb->pCmd = NULL;
1060 psb->status = IOSTAT_SUCCESS;
1061 }
1062 spin_lock_irqsave(&phba->nvme_buf_list_put_lock, iflag);
1063 list_splice(&nvme_aborts, &phba->lpfc_nvme_buf_list_put);
1064 spin_unlock_irqrestore(&phba->nvme_buf_list_put_lock, iflag);
1065
James Smart68e814f2014-05-21 08:04:59 -04001066 lpfc_sli4_free_sp_events(phba);
James Smartda0436e2009-05-22 14:51:39 -04001067 return 0;
1068}
1069
1070/**
1071 * lpfc_hba_down_post - Wrapper func for hba down post routine
1072 * @phba: pointer to lpfc HBA data structure.
1073 *
1074 * This routine wraps the actual SLI3 or SLI4 routine for performing
1075 * uninitialization after the HBA is reset when bring down the SLI Layer.
1076 *
1077 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001078 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001079 * Any other value - error.
1080 **/
1081int
1082lpfc_hba_down_post(struct lpfc_hba *phba)
1083{
1084 return (*phba->lpfc_hba_down_post)(phba);
1085}
Jamie Wellnitz41415862006-02-28 19:25:27 -05001086
James Smarte59058c2008-08-24 21:49:00 -04001087/**
James Smart3621a712009-04-06 18:47:14 -04001088 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001089 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1090 *
1091 * This is the HBA-timer timeout handler registered to the lpfc driver. When
1092 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
1093 * work-port-events bitmap and the worker thread is notified. This timeout
1094 * event will be used by the worker thread to invoke the actual timeout
1095 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
1096 * be performed in the timeout handler and the HBA timeout event bit shall
1097 * be cleared by the worker thread after it has taken the event bitmap out.
1098 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001099static void
James Smart858c9f62007-06-17 19:56:39 -05001100lpfc_hb_timeout(unsigned long ptr)
1101{
1102 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -04001103 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -05001104 unsigned long iflag;
1105
1106 phba = (struct lpfc_hba *)ptr;
James Smart93996272008-08-24 21:50:30 -04001107
1108 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -05001109 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04001110 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
1111 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -05001112 phba->pport->work_port_events |= WORKER_HB_TMO;
1113 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
1114
James Smart93996272008-08-24 21:50:30 -04001115 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -04001116 if (!tmo_posted)
1117 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -05001118 return;
1119}
1120
James Smarte59058c2008-08-24 21:49:00 -04001121/**
James Smart19ca7602010-11-20 23:11:55 -05001122 * lpfc_rrq_timeout - The RRQ-timer timeout handler
1123 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1124 *
1125 * This is the RRQ-timer timeout handler registered to the lpfc driver. When
1126 * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
1127 * work-port-events bitmap and the worker thread is notified. This timeout
1128 * event will be used by the worker thread to invoke the actual timeout
1129 * handler routine, lpfc_rrq_handler. Any periodical operations will
1130 * be performed in the timeout handler and the RRQ timeout event bit shall
1131 * be cleared by the worker thread after it has taken the event bitmap out.
1132 **/
1133static void
1134lpfc_rrq_timeout(unsigned long ptr)
1135{
1136 struct lpfc_hba *phba;
James Smart19ca7602010-11-20 23:11:55 -05001137 unsigned long iflag;
1138
1139 phba = (struct lpfc_hba *)ptr;
1140 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001141 if (!(phba->pport->load_flag & FC_UNLOADING))
1142 phba->hba_flag |= HBA_RRQ_ACTIVE;
1143 else
1144 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart19ca7602010-11-20 23:11:55 -05001145 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001146
1147 if (!(phba->pport->load_flag & FC_UNLOADING))
1148 lpfc_worker_wake_up(phba);
James Smart19ca7602010-11-20 23:11:55 -05001149}
1150
1151/**
James Smart3621a712009-04-06 18:47:14 -04001152 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -04001153 * @phba: pointer to lpfc hba data structure.
1154 * @pmboxq: pointer to the driver internal queue element for mailbox command.
1155 *
1156 * This is the callback function to the lpfc heart-beat mailbox command.
1157 * If configured, the lpfc driver issues the heart-beat mailbox command to
1158 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
1159 * heart-beat mailbox command is issued, the driver shall set up heart-beat
1160 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
1161 * heart-beat outstanding state. Once the mailbox command comes back and
1162 * no error conditions detected, the heart-beat mailbox command timer is
1163 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1164 * state is cleared for the next heart-beat. If the timer expired with the
1165 * heart-beat outstanding state set, the driver will put the HBA offline.
1166 **/
James Smart858c9f62007-06-17 19:56:39 -05001167static void
1168lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1169{
1170 unsigned long drvr_flag;
1171
1172 spin_lock_irqsave(&phba->hbalock, drvr_flag);
1173 phba->hb_outstanding = 0;
1174 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1175
James Smart93996272008-08-24 21:50:30 -04001176 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -05001177 mempool_free(pmboxq, phba->mbox_mem_pool);
1178 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1179 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -04001180 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -05001181 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001182 jiffies +
1183 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001184 return;
1185}
1186
James Smarte59058c2008-08-24 21:49:00 -04001187/**
James Smart3621a712009-04-06 18:47:14 -04001188 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001189 * @phba: pointer to lpfc hba data structure.
1190 *
1191 * This is the actual HBA-timer timeout handler to be invoked by the worker
1192 * thread whenever the HBA timer fired and HBA-timeout event posted. This
1193 * handler performs any periodic operations needed for the device. If such
1194 * periodic event has already been attended to either in the interrupt handler
1195 * or by processing slow-ring or fast-ring events within the HBA-timer
1196 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1197 * the timer for the next timeout period. If lpfc heart-beat mailbox command
1198 * is configured and there is no heart-beat mailbox command outstanding, a
1199 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1200 * has been a heart-beat mailbox command outstanding, the HBA shall be put
1201 * to offline.
1202 **/
James Smart858c9f62007-06-17 19:56:39 -05001203void
1204lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1205{
James Smart45ed1192009-10-02 15:17:02 -04001206 struct lpfc_vport **vports;
James Smart858c9f62007-06-17 19:56:39 -05001207 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -05001208 struct lpfc_dmabuf *buf_ptr;
James Smart45ed1192009-10-02 15:17:02 -04001209 int retval, i;
James Smart858c9f62007-06-17 19:56:39 -05001210 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -05001211 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -05001212
James Smart45ed1192009-10-02 15:17:02 -04001213 vports = lpfc_create_vport_work_array(phba);
1214 if (vports != NULL)
James Smart4258e982015-12-16 18:11:58 -05001215 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart45ed1192009-10-02 15:17:02 -04001216 lpfc_rcv_seq_check_edtov(vports[i]);
James Smart4258e982015-12-16 18:11:58 -05001217 lpfc_fdmi_num_disc_check(vports[i]);
1218 }
James Smart45ed1192009-10-02 15:17:02 -04001219 lpfc_destroy_vport_work_array(phba, vports);
1220
James Smart858c9f62007-06-17 19:56:39 -05001221 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -04001222 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -05001223 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1224 return;
1225
1226 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -05001227
James Smart256ec0d2013-04-17 20:14:58 -04001228 if (time_after(phba->last_completion_time +
1229 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL),
1230 jiffies)) {
James Smart858c9f62007-06-17 19:56:39 -05001231 spin_unlock_irq(&phba->pport->work_port_lock);
1232 if (!phba->hb_outstanding)
1233 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001234 jiffies +
1235 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001236 else
1237 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001238 jiffies +
1239 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001240 return;
1241 }
1242 spin_unlock_irq(&phba->pport->work_port_lock);
1243
James Smart0ff10d42008-01-11 01:52:36 -05001244 if (phba->elsbuf_cnt &&
1245 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1246 spin_lock_irq(&phba->hbalock);
1247 list_splice_init(&phba->elsbuf, &completions);
1248 phba->elsbuf_cnt = 0;
1249 phba->elsbuf_prev_cnt = 0;
1250 spin_unlock_irq(&phba->hbalock);
1251
1252 while (!list_empty(&completions)) {
1253 list_remove_head(&completions, buf_ptr,
1254 struct lpfc_dmabuf, list);
1255 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1256 kfree(buf_ptr);
1257 }
1258 }
1259 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1260
James Smart858c9f62007-06-17 19:56:39 -05001261 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -05001262 if (phba->cfg_enable_hba_heartbeat) {
1263 if (!phba->hb_outstanding) {
James Smartbc739052010-08-04 16:11:18 -04001264 if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1265 (list_empty(&psli->mboxq))) {
1266 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1267 GFP_KERNEL);
1268 if (!pmboxq) {
1269 mod_timer(&phba->hb_tmofunc,
1270 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001271 msecs_to_jiffies(1000 *
1272 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001273 return;
1274 }
James Smart13815c82008-01-11 01:52:48 -05001275
James Smartbc739052010-08-04 16:11:18 -04001276 lpfc_heart_beat(phba, pmboxq);
1277 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1278 pmboxq->vport = phba->pport;
1279 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1280 MBX_NOWAIT);
James Smart13815c82008-01-11 01:52:48 -05001281
James Smartbc739052010-08-04 16:11:18 -04001282 if (retval != MBX_BUSY &&
1283 retval != MBX_SUCCESS) {
1284 mempool_free(pmboxq,
1285 phba->mbox_mem_pool);
1286 mod_timer(&phba->hb_tmofunc,
1287 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001288 msecs_to_jiffies(1000 *
1289 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001290 return;
1291 }
1292 phba->skipped_hb = 0;
1293 phba->hb_outstanding = 1;
1294 } else if (time_before_eq(phba->last_completion_time,
1295 phba->skipped_hb)) {
1296 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1297 "2857 Last completion time not "
1298 " updated in %d ms\n",
1299 jiffies_to_msecs(jiffies
1300 - phba->last_completion_time));
1301 } else
1302 phba->skipped_hb = jiffies;
1303
James Smart858c9f62007-06-17 19:56:39 -05001304 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001305 jiffies +
1306 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001307 return;
James Smart13815c82008-01-11 01:52:48 -05001308 } else {
1309 /*
1310 * If heart beat timeout called with hb_outstanding set
James Smartdcf2a4e2010-09-29 11:18:53 -04001311 * we need to give the hb mailbox cmd a chance to
1312 * complete or TMO.
James Smart13815c82008-01-11 01:52:48 -05001313 */
James Smartdcf2a4e2010-09-29 11:18:53 -04001314 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1315 "0459 Adapter heartbeat still out"
1316 "standing:last compl time was %d ms.\n",
1317 jiffies_to_msecs(jiffies
1318 - phba->last_completion_time));
1319 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001320 jiffies +
1321 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001322 }
James Smart4258e982015-12-16 18:11:58 -05001323 } else {
1324 mod_timer(&phba->hb_tmofunc,
1325 jiffies +
1326 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001327 }
1328}
1329
James Smarte59058c2008-08-24 21:49:00 -04001330/**
James Smart3621a712009-04-06 18:47:14 -04001331 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -04001332 * @phba: pointer to lpfc hba data structure.
1333 *
1334 * This routine is called to bring the HBA offline when HBA hardware error
1335 * other than Port Error 6 has been detected.
1336 **/
James Smart09372822008-01-11 01:52:54 -05001337static void
1338lpfc_offline_eratt(struct lpfc_hba *phba)
1339{
1340 struct lpfc_sli *psli = &phba->sli;
1341
1342 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001343 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -05001344 spin_unlock_irq(&phba->hbalock);
James Smart618a5232012-06-12 13:54:36 -04001345 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart09372822008-01-11 01:52:54 -05001346
1347 lpfc_offline(phba);
1348 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001349 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001350 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001351 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001352 lpfc_hba_down_post(phba);
1353 lpfc_sli_brdready(phba, HS_MBRDY);
1354 lpfc_unblock_mgmt_io(phba);
1355 phba->link_state = LPFC_HBA_ERROR;
1356 return;
1357}
1358
James Smarte59058c2008-08-24 21:49:00 -04001359/**
James Smartda0436e2009-05-22 14:51:39 -04001360 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1361 * @phba: pointer to lpfc hba data structure.
1362 *
1363 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1364 * other than Port Error 6 has been detected.
1365 **/
James Smarta88dbb62013-04-17 20:18:39 -04001366void
James Smartda0436e2009-05-22 14:51:39 -04001367lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1368{
James Smart946727d2015-04-07 15:07:09 -04001369 spin_lock_irq(&phba->hbalock);
1370 phba->link_state = LPFC_HBA_ERROR;
1371 spin_unlock_irq(&phba->hbalock);
1372
James Smart618a5232012-06-12 13:54:36 -04001373 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smartda0436e2009-05-22 14:51:39 -04001374 lpfc_offline(phba);
James Smartda0436e2009-05-22 14:51:39 -04001375 lpfc_hba_down_post(phba);
James Smartda0436e2009-05-22 14:51:39 -04001376 lpfc_unblock_mgmt_io(phba);
James Smartda0436e2009-05-22 14:51:39 -04001377}
1378
1379/**
James Smarta257bf92009-04-06 18:48:10 -04001380 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1381 * @phba: pointer to lpfc hba data structure.
1382 *
1383 * This routine is invoked to handle the deferred HBA hardware error
1384 * conditions. This type of error is indicated by HBA by setting ER1
1385 * and another ER bit in the host status register. The driver will
1386 * wait until the ER1 bit clears before handling the error condition.
1387 **/
1388static void
1389lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1390{
1391 uint32_t old_host_status = phba->work_hs;
James Smarta257bf92009-04-06 18:48:10 -04001392 struct lpfc_sli *psli = &phba->sli;
1393
James Smartf4b4c682009-05-22 14:53:12 -04001394 /* If the pci channel is offline, ignore possible errors,
1395 * since we cannot communicate with the pci card anyway.
1396 */
1397 if (pci_channel_offline(phba->pcidev)) {
1398 spin_lock_irq(&phba->hbalock);
1399 phba->hba_flag &= ~DEFER_ERATT;
1400 spin_unlock_irq(&phba->hbalock);
1401 return;
1402 }
1403
James Smarta257bf92009-04-06 18:48:10 -04001404 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1405 "0479 Deferred Adapter Hardware Error "
1406 "Data: x%x x%x x%x\n",
1407 phba->work_hs,
1408 phba->work_status[0], phba->work_status[1]);
1409
1410 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001411 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001412 spin_unlock_irq(&phba->hbalock);
1413
1414
1415 /*
1416 * Firmware stops when it triggred erratt. That could cause the I/Os
1417 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1418 * SCSI layer retry it after re-establishing link.
1419 */
James Smartdb55fba2014-04-04 13:52:02 -04001420 lpfc_sli_abort_fcp_rings(phba);
James Smarta257bf92009-04-06 18:48:10 -04001421
1422 /*
1423 * There was a firmware error. Take the hba offline and then
1424 * attempt to restart it.
1425 */
James Smart618a5232012-06-12 13:54:36 -04001426 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smarta257bf92009-04-06 18:48:10 -04001427 lpfc_offline(phba);
1428
1429 /* Wait for the ER1 bit to clear.*/
1430 while (phba->work_hs & HS_FFER1) {
1431 msleep(100);
James Smart9940b972011-03-11 16:06:12 -05001432 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1433 phba->work_hs = UNPLUG_ERR ;
1434 break;
1435 }
James Smarta257bf92009-04-06 18:48:10 -04001436 /* If driver is unloading let the worker thread continue */
1437 if (phba->pport->load_flag & FC_UNLOADING) {
1438 phba->work_hs = 0;
1439 break;
1440 }
1441 }
1442
1443 /*
1444 * This is to ptrotect against a race condition in which
1445 * first write to the host attention register clear the
1446 * host status register.
1447 */
1448 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1449 phba->work_hs = old_host_status & ~HS_FFER1;
1450
James Smart3772a992009-05-22 14:50:54 -04001451 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001452 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001453 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001454 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1455 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1456}
1457
James Smart3772a992009-05-22 14:50:54 -04001458static void
1459lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1460{
1461 struct lpfc_board_event_header board_event;
1462 struct Scsi_Host *shost;
1463
1464 board_event.event_type = FC_REG_BOARD_EVENT;
1465 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1466 shost = lpfc_shost_from_vport(phba->pport);
1467 fc_host_post_vendor_event(shost, fc_get_event_number(),
1468 sizeof(board_event),
1469 (char *) &board_event,
1470 LPFC_NL_VENDOR_ID);
1471}
1472
James Smarta257bf92009-04-06 18:48:10 -04001473/**
James Smart3772a992009-05-22 14:50:54 -04001474 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001475 * @phba: pointer to lpfc hba data structure.
1476 *
1477 * This routine is invoked to handle the following HBA hardware error
1478 * conditions:
1479 * 1 - HBA error attention interrupt
1480 * 2 - DMA ring index out of range
1481 * 3 - Mailbox command came back as unknown
1482 **/
James Smart3772a992009-05-22 14:50:54 -04001483static void
1484lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001485{
James Smart2e0fef82007-06-17 19:56:36 -05001486 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001487 struct lpfc_sli *psli = &phba->sli;
James Smartd2873e42006-08-18 17:46:43 -04001488 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001489 unsigned long temperature;
1490 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001491 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001492
Linas Vepstas8d63f372007-02-14 14:28:36 -06001493 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001494 * since we cannot communicate with the pci card anyway.
1495 */
1496 if (pci_channel_offline(phba->pcidev)) {
1497 spin_lock_irq(&phba->hbalock);
1498 phba->hba_flag &= ~DEFER_ERATT;
1499 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001500 return;
James Smart3772a992009-05-22 14:50:54 -04001501 }
1502
James Smart13815c82008-01-11 01:52:48 -05001503 /* If resets are disabled then leave the HBA alone and return */
1504 if (!phba->cfg_enable_hba_reset)
1505 return;
dea31012005-04-17 16:05:31 -05001506
James Smartea2151b2008-09-07 11:52:10 -04001507 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001508 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001509
James Smarta257bf92009-04-06 18:48:10 -04001510 if (phba->hba_flag & DEFER_ERATT)
1511 lpfc_handle_deferred_eratt(phba);
1512
James Smartdcf2a4e2010-09-29 11:18:53 -04001513 if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1514 if (phba->work_hs & HS_FFER6)
1515 /* Re-establishing Link */
1516 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1517 "1301 Re-establishing Link "
1518 "Data: x%x x%x x%x\n",
1519 phba->work_hs, phba->work_status[0],
1520 phba->work_status[1]);
1521 if (phba->work_hs & HS_FFER8)
1522 /* Device Zeroization */
1523 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1524 "2861 Host Authentication device "
1525 "zeroization Data:x%x x%x x%x\n",
1526 phba->work_hs, phba->work_status[0],
1527 phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001528
James Smart92d7f7b2007-06-17 19:56:38 -05001529 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001530 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001531 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001532
1533 /*
1534 * Firmware stops when it triggled erratt with HS_FFER6.
1535 * That could cause the I/Os dropped by the firmware.
1536 * Error iocb (I/O) on txcmplq and let the SCSI layer
1537 * retry it after re-establishing link.
1538 */
James Smartdb55fba2014-04-04 13:52:02 -04001539 lpfc_sli_abort_fcp_rings(phba);
dea31012005-04-17 16:05:31 -05001540
dea31012005-04-17 16:05:31 -05001541 /*
1542 * There was a firmware error. Take the hba offline and then
1543 * attempt to restart it.
1544 */
James Smart618a5232012-06-12 13:54:36 -04001545 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
dea31012005-04-17 16:05:31 -05001546 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001547 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001548 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001549 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001550 return;
1551 }
James Smart46fa3112007-04-25 09:51:45 -04001552 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001553 } else if (phba->work_hs & HS_CRIT_TEMP) {
1554 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1555 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1556 temp_event_data.event_code = LPFC_CRIT_TEMP;
1557 temp_event_data.data = (uint32_t)temperature;
1558
1559 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001560 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001561 "(%ld), taking this port offline "
1562 "Data: x%x x%x x%x\n",
1563 temperature, phba->work_hs,
1564 phba->work_status[0], phba->work_status[1]);
1565
1566 shost = lpfc_shost_from_vport(phba->pport);
1567 fc_host_post_vendor_event(shost, fc_get_event_number(),
1568 sizeof(temp_event_data),
1569 (char *) &temp_event_data,
1570 SCSI_NL_VID_TYPE_PCI
1571 | PCI_VENDOR_ID_EMULEX);
1572
James Smart7af67052007-10-27 13:38:11 -04001573 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001574 phba->over_temp_state = HBA_OVER_TEMP;
1575 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001576 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001577
dea31012005-04-17 16:05:31 -05001578 } else {
1579 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001580 * failure is a value other than FFER6. Do not call the offline
1581 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001582 */
1583 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001584 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001585 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001586 phba->work_hs,
dea31012005-04-17 16:05:31 -05001587 phba->work_status[0], phba->work_status[1]);
1588
James Smartd2873e42006-08-18 17:46:43 -04001589 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001590 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001591 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001592 sizeof(event_data), (char *) &event_data,
1593 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1594
James Smart09372822008-01-11 01:52:54 -05001595 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001596 }
James Smart93996272008-08-24 21:50:30 -04001597 return;
dea31012005-04-17 16:05:31 -05001598}
1599
James Smarte59058c2008-08-24 21:49:00 -04001600/**
James Smart618a5232012-06-12 13:54:36 -04001601 * lpfc_sli4_port_sta_fn_reset - The SLI4 function reset due to port status reg
1602 * @phba: pointer to lpfc hba data structure.
1603 * @mbx_action: flag for mailbox shutdown action.
1604 *
1605 * This routine is invoked to perform an SLI4 port PCI function reset in
1606 * response to port status register polling attention. It waits for port
1607 * status register (ERR, RDY, RN) bits before proceeding with function reset.
1608 * During this process, interrupt vectors are freed and later requested
1609 * for handling possible port resource change.
1610 **/
1611static int
James Smarte10b2022014-02-20 09:57:43 -05001612lpfc_sli4_port_sta_fn_reset(struct lpfc_hba *phba, int mbx_action,
1613 bool en_rn_msg)
James Smart618a5232012-06-12 13:54:36 -04001614{
1615 int rc;
1616 uint32_t intr_mode;
1617
James Smart65791f12016-07-06 12:35:56 -07001618 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
1619 LPFC_SLI_INTF_IF_TYPE_2) {
1620 /*
1621 * On error status condition, driver need to wait for port
1622 * ready before performing reset.
1623 */
1624 rc = lpfc_sli4_pdev_status_reg_wait(phba);
James Smart0e916ee2016-07-06 12:36:06 -07001625 if (rc)
James Smart65791f12016-07-06 12:35:56 -07001626 return rc;
James Smart618a5232012-06-12 13:54:36 -04001627 }
James Smart0e916ee2016-07-06 12:36:06 -07001628
James Smart65791f12016-07-06 12:35:56 -07001629 /* need reset: attempt for port recovery */
1630 if (en_rn_msg)
1631 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1632 "2887 Reset Needed: Attempting Port "
1633 "Recovery...\n");
1634 lpfc_offline_prep(phba, mbx_action);
1635 lpfc_offline(phba);
1636 /* release interrupt for possible resource change */
1637 lpfc_sli4_disable_intr(phba);
1638 lpfc_sli_brdrestart(phba);
1639 /* request and enable interrupt */
1640 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
1641 if (intr_mode == LPFC_INTR_ERROR) {
1642 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1643 "3175 Failed to enable interrupt\n");
1644 return -EIO;
1645 }
1646 phba->intr_mode = intr_mode;
1647 rc = lpfc_online(phba);
1648 if (rc == 0)
1649 lpfc_unblock_mgmt_io(phba);
1650
James Smart618a5232012-06-12 13:54:36 -04001651 return rc;
1652}
1653
1654/**
James Smartda0436e2009-05-22 14:51:39 -04001655 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1656 * @phba: pointer to lpfc hba data structure.
1657 *
1658 * This routine is invoked to handle the SLI4 HBA hardware error attention
1659 * conditions.
1660 **/
1661static void
1662lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1663{
1664 struct lpfc_vport *vport = phba->pport;
1665 uint32_t event_data;
1666 struct Scsi_Host *shost;
James Smart2fcee4b2010-12-15 17:57:46 -05001667 uint32_t if_type;
James Smart2e90f4b2011-12-13 13:22:37 -05001668 struct lpfc_register portstat_reg = {0};
1669 uint32_t reg_err1, reg_err2;
1670 uint32_t uerrlo_reg, uemasklo_reg;
James Smart65791f12016-07-06 12:35:56 -07001671 uint32_t smphr_port_status = 0, pci_rd_rc1, pci_rd_rc2;
James Smarte10b2022014-02-20 09:57:43 -05001672 bool en_rn_msg = true;
James Smart946727d2015-04-07 15:07:09 -04001673 struct temp_event temp_event_data;
James Smart65791f12016-07-06 12:35:56 -07001674 struct lpfc_register portsmphr_reg;
1675 int rc, i;
James Smartda0436e2009-05-22 14:51:39 -04001676
1677 /* If the pci channel is offline, ignore possible errors, since
1678 * we cannot communicate with the pci card anyway.
1679 */
1680 if (pci_channel_offline(phba->pcidev))
1681 return;
James Smartda0436e2009-05-22 14:51:39 -04001682
James Smart65791f12016-07-06 12:35:56 -07001683 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
James Smart2fcee4b2010-12-15 17:57:46 -05001684 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1685 switch (if_type) {
1686 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart2e90f4b2011-12-13 13:22:37 -05001687 pci_rd_rc1 = lpfc_readl(
1688 phba->sli4_hba.u.if_type0.UERRLOregaddr,
1689 &uerrlo_reg);
1690 pci_rd_rc2 = lpfc_readl(
1691 phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
1692 &uemasklo_reg);
1693 /* consider PCI bus read error as pci_channel_offline */
1694 if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
1695 return;
James Smart65791f12016-07-06 12:35:56 -07001696 if (!(phba->hba_flag & HBA_RECOVERABLE_UE)) {
1697 lpfc_sli4_offline_eratt(phba);
1698 return;
1699 }
1700 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1701 "7623 Checking UE recoverable");
1702
1703 for (i = 0; i < phba->sli4_hba.ue_to_sr / 1000; i++) {
1704 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1705 &portsmphr_reg.word0))
1706 continue;
1707
1708 smphr_port_status = bf_get(lpfc_port_smphr_port_status,
1709 &portsmphr_reg);
1710 if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1711 LPFC_PORT_SEM_UE_RECOVERABLE)
1712 break;
1713 /*Sleep for 1Sec, before checking SEMAPHORE */
1714 msleep(1000);
1715 }
1716
1717 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1718 "4827 smphr_port_status x%x : Waited %dSec",
1719 smphr_port_status, i);
1720
1721 /* Recoverable UE, reset the HBA device */
1722 if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1723 LPFC_PORT_SEM_UE_RECOVERABLE) {
1724 for (i = 0; i < 20; i++) {
1725 msleep(1000);
1726 if (!lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1727 &portsmphr_reg.word0) &&
1728 (LPFC_POST_STAGE_PORT_READY ==
1729 bf_get(lpfc_port_smphr_port_status,
1730 &portsmphr_reg))) {
1731 rc = lpfc_sli4_port_sta_fn_reset(phba,
1732 LPFC_MBX_NO_WAIT, en_rn_msg);
1733 if (rc == 0)
1734 return;
1735 lpfc_printf_log(phba,
1736 KERN_ERR, LOG_INIT,
1737 "4215 Failed to recover UE");
1738 break;
1739 }
1740 }
1741 }
1742 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1743 "7624 Firmware not ready: Failing UE recovery,"
1744 " waited %dSec", i);
James Smart2fcee4b2010-12-15 17:57:46 -05001745 lpfc_sli4_offline_eratt(phba);
1746 break;
James Smart946727d2015-04-07 15:07:09 -04001747
James Smart2fcee4b2010-12-15 17:57:46 -05001748 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart2e90f4b2011-12-13 13:22:37 -05001749 pci_rd_rc1 = lpfc_readl(
1750 phba->sli4_hba.u.if_type2.STATUSregaddr,
1751 &portstat_reg.word0);
1752 /* consider PCI bus read error as pci_channel_offline */
James Smart6b5151f2012-01-18 16:24:06 -05001753 if (pci_rd_rc1 == -EIO) {
1754 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1755 "3151 PCI bus read access failure: x%x\n",
1756 readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
James Smart2e90f4b2011-12-13 13:22:37 -05001757 return;
James Smart6b5151f2012-01-18 16:24:06 -05001758 }
James Smart2e90f4b2011-12-13 13:22:37 -05001759 reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
1760 reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
James Smart2fcee4b2010-12-15 17:57:46 -05001761 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
James Smart2fcee4b2010-12-15 17:57:46 -05001762 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1763 "2889 Port Overtemperature event, "
James Smart946727d2015-04-07 15:07:09 -04001764 "taking port offline Data: x%x x%x\n",
1765 reg_err1, reg_err2);
1766
James Smart310429e2016-07-06 12:35:54 -07001767 phba->sfp_alarm |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
James Smart946727d2015-04-07 15:07:09 -04001768 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1769 temp_event_data.event_code = LPFC_CRIT_TEMP;
1770 temp_event_data.data = 0xFFFFFFFF;
1771
1772 shost = lpfc_shost_from_vport(phba->pport);
1773 fc_host_post_vendor_event(shost, fc_get_event_number(),
1774 sizeof(temp_event_data),
1775 (char *)&temp_event_data,
1776 SCSI_NL_VID_TYPE_PCI
1777 | PCI_VENDOR_ID_EMULEX);
1778
James Smart2fcee4b2010-12-15 17:57:46 -05001779 spin_lock_irq(&phba->hbalock);
1780 phba->over_temp_state = HBA_OVER_TEMP;
1781 spin_unlock_irq(&phba->hbalock);
1782 lpfc_sli4_offline_eratt(phba);
James Smart946727d2015-04-07 15:07:09 -04001783 return;
James Smart2fcee4b2010-12-15 17:57:46 -05001784 }
James Smart2e90f4b2011-12-13 13:22:37 -05001785 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smarte10b2022014-02-20 09:57:43 -05001786 reg_err2 == SLIPORT_ERR2_REG_FW_RESTART) {
James Smart2e90f4b2011-12-13 13:22:37 -05001787 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte10b2022014-02-20 09:57:43 -05001788 "3143 Port Down: Firmware Update "
1789 "Detected\n");
1790 en_rn_msg = false;
1791 } else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smart2e90f4b2011-12-13 13:22:37 -05001792 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1793 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1794 "3144 Port Down: Debug Dump\n");
1795 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1796 reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
1797 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1798 "3145 Port Down: Provisioning\n");
James Smart618a5232012-06-12 13:54:36 -04001799
James Smart946727d2015-04-07 15:07:09 -04001800 /* If resets are disabled then leave the HBA alone and return */
1801 if (!phba->cfg_enable_hba_reset)
1802 return;
1803
James Smart618a5232012-06-12 13:54:36 -04001804 /* Check port status register for function reset */
James Smarte10b2022014-02-20 09:57:43 -05001805 rc = lpfc_sli4_port_sta_fn_reset(phba, LPFC_MBX_NO_WAIT,
1806 en_rn_msg);
James Smart618a5232012-06-12 13:54:36 -04001807 if (rc == 0) {
1808 /* don't report event on forced debug dump */
1809 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1810 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1811 return;
1812 else
1813 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001814 }
James Smart618a5232012-06-12 13:54:36 -04001815 /* fall through for not able to recover */
James Smart6b5151f2012-01-18 16:24:06 -05001816 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1817 "3152 Unrecoverable error, bring the port "
1818 "offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001819 lpfc_sli4_offline_eratt(phba);
1820 break;
1821 case LPFC_SLI_INTF_IF_TYPE_1:
1822 default:
1823 break;
1824 }
James Smart2e90f4b2011-12-13 13:22:37 -05001825 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1826 "3123 Report dump event to upper layer\n");
1827 /* Send an internal error event to mgmt application */
1828 lpfc_board_errevt_to_mgmt(phba);
1829
1830 event_data = FC_REG_DUMP_EVENT;
1831 shost = lpfc_shost_from_vport(vport);
1832 fc_host_post_vendor_event(shost, fc_get_event_number(),
1833 sizeof(event_data), (char *) &event_data,
1834 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
James Smartda0436e2009-05-22 14:51:39 -04001835}
1836
1837/**
1838 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1839 * @phba: pointer to lpfc HBA data structure.
1840 *
1841 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1842 * routine from the API jump table function pointer from the lpfc_hba struct.
1843 *
1844 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001845 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001846 * Any other value - error.
1847 **/
1848void
1849lpfc_handle_eratt(struct lpfc_hba *phba)
1850{
1851 (*phba->lpfc_handle_eratt)(phba);
1852}
1853
1854/**
James Smart3621a712009-04-06 18:47:14 -04001855 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04001856 * @phba: pointer to lpfc hba data structure.
1857 *
1858 * This routine is invoked from the worker thread to handle a HBA host
James Smart895427b2017-02-12 13:52:30 -08001859 * attention link event. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04001860 **/
dea31012005-04-17 16:05:31 -05001861void
James Smart2e0fef82007-06-17 19:56:36 -05001862lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001863{
James Smart2e0fef82007-06-17 19:56:36 -05001864 struct lpfc_vport *vport = phba->pport;
1865 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001866 LPFC_MBOXQ_t *pmb;
1867 volatile uint32_t control;
1868 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05001869 int rc = 0;
dea31012005-04-17 16:05:31 -05001870
1871 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001872 if (!pmb) {
1873 rc = 1;
dea31012005-04-17 16:05:31 -05001874 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05001875 }
dea31012005-04-17 16:05:31 -05001876
1877 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001878 if (!mp) {
1879 rc = 2;
dea31012005-04-17 16:05:31 -05001880 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05001881 }
dea31012005-04-17 16:05:31 -05001882
1883 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05001884 if (!mp->virt) {
1885 rc = 3;
dea31012005-04-17 16:05:31 -05001886 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05001887 }
dea31012005-04-17 16:05:31 -05001888
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05001889 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04001890 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05001891
1892 psli->slistat.link_event++;
James Smart76a95d72010-11-20 23:11:48 -05001893 lpfc_read_topology(phba, pmb, mp);
1894 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smart2e0fef82007-06-17 19:56:36 -05001895 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04001896 /* Block ELS IOCBs until we have processed this mbox command */
James Smart895427b2017-02-12 13:52:30 -08001897 phba->sli.sli3_ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04001898 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05001899 if (rc == MBX_NOT_FINISHED) {
1900 rc = 4;
James Smart14691152006-12-02 13:34:28 -05001901 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05001902 }
dea31012005-04-17 16:05:31 -05001903
1904 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05001905 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001906 writel(HA_LATT, phba->HAregaddr);
1907 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001908 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001909
1910 return;
1911
James Smart14691152006-12-02 13:34:28 -05001912lpfc_handle_latt_free_mbuf:
James Smart895427b2017-02-12 13:52:30 -08001913 phba->sli.sli3_ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05001914 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001915lpfc_handle_latt_free_mp:
1916 kfree(mp);
1917lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04001918 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001919lpfc_handle_latt_err_exit:
1920 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001921 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001922 psli->sli_flag |= LPFC_PROCESS_LA;
1923 control = readl(phba->HCregaddr);
1924 control |= HC_LAINT_ENA;
1925 writel(control, phba->HCregaddr);
1926 readl(phba->HCregaddr); /* flush */
1927
1928 /* Clear Link Attention in HA REG */
1929 writel(HA_LATT, phba->HAregaddr);
1930 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001931 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001932 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001933 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001934
James Smart09372822008-01-11 01:52:54 -05001935 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1936 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05001937
1938 return;
1939}
1940
James Smarte59058c2008-08-24 21:49:00 -04001941/**
James Smart3621a712009-04-06 18:47:14 -04001942 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04001943 * @phba: pointer to lpfc hba data structure.
1944 * @vpd: pointer to the vital product data.
1945 * @len: length of the vital product data in bytes.
1946 *
1947 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1948 * an array of characters. In this routine, the ModelName, ProgramType, and
1949 * ModelDesc, etc. fields of the phba data structure will be populated.
1950 *
1951 * Return codes
1952 * 0 - pointer to the VPD passed in is NULL
1953 * 1 - success
1954 **/
James Smart3772a992009-05-22 14:50:54 -04001955int
James Smart2e0fef82007-06-17 19:56:36 -05001956lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05001957{
1958 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05001959 int Length;
dea31012005-04-17 16:05:31 -05001960 int i, j;
1961 int finished = 0;
1962 int index = 0;
1963
1964 if (!vpd)
1965 return 0;
1966
1967 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05001968 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001969 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05001970 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1971 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001972 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05001973 switch (vpd[index]) {
1974 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001975 case 0x91:
dea31012005-04-17 16:05:31 -05001976 index += 1;
1977 lenlo = vpd[index];
1978 index += 1;
1979 lenhi = vpd[index];
1980 index += 1;
1981 i = ((((unsigned short)lenhi) << 8) + lenlo);
1982 index += i;
1983 break;
1984 case 0x90:
1985 index += 1;
1986 lenlo = vpd[index];
1987 index += 1;
1988 lenhi = vpd[index];
1989 index += 1;
1990 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001991 if (Length > len - index)
1992 Length = len - index;
dea31012005-04-17 16:05:31 -05001993 while (Length > 0) {
1994 /* Look for Serial Number */
1995 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1996 index += 2;
1997 i = vpd[index];
1998 index += 1;
1999 j = 0;
2000 Length -= (3+i);
2001 while(i--) {
2002 phba->SerialNumber[j++] = vpd[index++];
2003 if (j == 31)
2004 break;
2005 }
2006 phba->SerialNumber[j] = 0;
2007 continue;
2008 }
2009 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
2010 phba->vpd_flag |= VPD_MODEL_DESC;
2011 index += 2;
2012 i = vpd[index];
2013 index += 1;
2014 j = 0;
2015 Length -= (3+i);
2016 while(i--) {
2017 phba->ModelDesc[j++] = vpd[index++];
2018 if (j == 255)
2019 break;
2020 }
2021 phba->ModelDesc[j] = 0;
2022 continue;
2023 }
2024 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
2025 phba->vpd_flag |= VPD_MODEL_NAME;
2026 index += 2;
2027 i = vpd[index];
2028 index += 1;
2029 j = 0;
2030 Length -= (3+i);
2031 while(i--) {
2032 phba->ModelName[j++] = vpd[index++];
2033 if (j == 79)
2034 break;
2035 }
2036 phba->ModelName[j] = 0;
2037 continue;
2038 }
2039 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
2040 phba->vpd_flag |= VPD_PROGRAM_TYPE;
2041 index += 2;
2042 i = vpd[index];
2043 index += 1;
2044 j = 0;
2045 Length -= (3+i);
2046 while(i--) {
2047 phba->ProgramType[j++] = vpd[index++];
2048 if (j == 255)
2049 break;
2050 }
2051 phba->ProgramType[j] = 0;
2052 continue;
2053 }
2054 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
2055 phba->vpd_flag |= VPD_PORT;
2056 index += 2;
2057 i = vpd[index];
2058 index += 1;
2059 j = 0;
2060 Length -= (3+i);
2061 while(i--) {
James Smartcd1c8302011-10-10 21:33:25 -04002062 if ((phba->sli_rev == LPFC_SLI_REV4) &&
2063 (phba->sli4_hba.pport_name_sta ==
2064 LPFC_SLI4_PPNAME_GET)) {
2065 j++;
2066 index++;
2067 } else
2068 phba->Port[j++] = vpd[index++];
2069 if (j == 19)
2070 break;
dea31012005-04-17 16:05:31 -05002071 }
James Smartcd1c8302011-10-10 21:33:25 -04002072 if ((phba->sli_rev != LPFC_SLI_REV4) ||
2073 (phba->sli4_hba.pport_name_sta ==
2074 LPFC_SLI4_PPNAME_NON))
2075 phba->Port[j] = 0;
dea31012005-04-17 16:05:31 -05002076 continue;
2077 }
2078 else {
2079 index += 2;
2080 i = vpd[index];
2081 index += 1;
2082 index += i;
2083 Length -= (3 + i);
2084 }
2085 }
2086 finished = 0;
2087 break;
2088 case 0x78:
2089 finished = 1;
2090 break;
2091 default:
2092 index ++;
2093 break;
2094 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002095 }
dea31012005-04-17 16:05:31 -05002096
2097 return(1);
2098}
2099
James Smarte59058c2008-08-24 21:49:00 -04002100/**
James Smart3621a712009-04-06 18:47:14 -04002101 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04002102 * @phba: pointer to lpfc hba data structure.
2103 * @mdp: pointer to the data structure to hold the derived model name.
2104 * @descp: pointer to the data structure to hold the derived description.
2105 *
2106 * This routine retrieves HBA's description based on its registered PCI device
2107 * ID. The @descp passed into this function points to an array of 256 chars. It
2108 * shall be returned with the model name, maximum speed, and the host bus type.
2109 * The @mdp passed into this function points to an array of 80 chars. When the
2110 * function returns, the @mdp will be filled with the model name.
2111 **/
dea31012005-04-17 16:05:31 -05002112static void
James Smart2e0fef82007-06-17 19:56:36 -05002113lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05002114{
2115 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05002116 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002117 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04002118 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04002119 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002120 struct {
James Smarta747c9c2009-11-18 15:41:10 -05002121 char *name;
2122 char *bus;
2123 char *function;
2124 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002125
2126 if (mdp && mdp[0] != '\0'
2127 && descp && descp[0] != '\0')
2128 return;
2129
James Smartd38dd522015-08-31 16:48:17 -04002130 if (phba->lmt & LMT_32Gb)
2131 max_speed = 32;
2132 else if (phba->lmt & LMT_16Gb)
James Smartc0c11512011-05-24 11:41:34 -04002133 max_speed = 16;
2134 else if (phba->lmt & LMT_10Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002135 max_speed = 10;
2136 else if (phba->lmt & LMT_8Gb)
2137 max_speed = 8;
2138 else if (phba->lmt & LMT_4Gb)
2139 max_speed = 4;
2140 else if (phba->lmt & LMT_2Gb)
2141 max_speed = 2;
James Smart4169d862012-09-29 11:32:09 -04002142 else if (phba->lmt & LMT_1Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002143 max_speed = 1;
James Smart4169d862012-09-29 11:32:09 -04002144 else
2145 max_speed = 0;
dea31012005-04-17 16:05:31 -05002146
2147 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05002148
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002149 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002150 case PCI_DEVICE_ID_FIREFLY:
James Smart12222f42014-05-21 08:05:19 -04002151 m = (typeof(m)){"LP6000", "PCI",
2152 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002153 break;
dea31012005-04-17 16:05:31 -05002154 case PCI_DEVICE_ID_SUPERFLY:
2155 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smart12222f42014-05-21 08:05:19 -04002156 m = (typeof(m)){"LP7000", "PCI", ""};
dea31012005-04-17 16:05:31 -05002157 else
James Smart12222f42014-05-21 08:05:19 -04002158 m = (typeof(m)){"LP7000E", "PCI", ""};
2159 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
dea31012005-04-17 16:05:31 -05002160 break;
2161 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002162 m = (typeof(m)){"LP8000", "PCI",
James Smart12222f42014-05-21 08:05:19 -04002163 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002164 break;
2165 case PCI_DEVICE_ID_CENTAUR:
2166 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smart12222f42014-05-21 08:05:19 -04002167 m = (typeof(m)){"LP9002", "PCI", ""};
dea31012005-04-17 16:05:31 -05002168 else
James Smart12222f42014-05-21 08:05:19 -04002169 m = (typeof(m)){"LP9000", "PCI", ""};
2170 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
dea31012005-04-17 16:05:31 -05002171 break;
2172 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002173 m = (typeof(m)){"LP952", "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_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05002177 m = (typeof(m)){"LP9802", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002178 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002179 break;
2180 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05002181 m = (typeof(m)){"LP10000", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002182 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002183 break;
2184 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05002185 m = (typeof(m)){"LPX1000", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002186 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002187 break;
2188 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002189 m = (typeof(m)){"LP982", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002190 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002191 break;
2192 case PCI_DEVICE_ID_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002193 m = (typeof(m)){"LP1050", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002194 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002195 break;
2196 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05002197 m = (typeof(m)){"LP11000", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002198 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002199 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002200 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002201 m = (typeof(m)){"LP11000-SP", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002202 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002203 break;
2204 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002205 m = (typeof(m)){"LP11002-SP", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002206 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002207 break;
2208 case PCI_DEVICE_ID_NEPTUNE:
James Smart12222f42014-05-21 08:05:19 -04002209 m = (typeof(m)){"LPe1000", "PCIe",
2210 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002211 break;
2212 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smart12222f42014-05-21 08:05:19 -04002213 m = (typeof(m)){"LPe1000-SP", "PCIe",
2214 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002215 break;
2216 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smart12222f42014-05-21 08:05:19 -04002217 m = (typeof(m)){"LPe1002-SP", "PCIe",
2218 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002219 break;
dea31012005-04-17 16:05:31 -05002220 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05002221 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002222 break;
2223 case PCI_DEVICE_ID_BSMB:
James Smart12222f42014-05-21 08:05:19 -04002224 m = (typeof(m)){"LP111", "PCI-X2",
2225 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002226 break;
2227 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05002228 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002229 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002230 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002231 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002232 break;
2233 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002234 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04002235 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002236 break;
dea31012005-04-17 16:05:31 -05002237 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05002238 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002239 break;
2240 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05002241 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002242 break;
2243 case PCI_DEVICE_ID_LP101:
James Smart12222f42014-05-21 08:05:19 -04002244 m = (typeof(m)){"LP101", "PCI-X",
2245 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002246 break;
2247 case PCI_DEVICE_ID_LP10000S:
James Smart12222f42014-05-21 08:05:19 -04002248 m = (typeof(m)){"LP10000-S", "PCI",
2249 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002250 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002251 case PCI_DEVICE_ID_LP11000S:
James Smart12222f42014-05-21 08:05:19 -04002252 m = (typeof(m)){"LP11000-S", "PCI-X2",
2253 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05002254 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002255 case PCI_DEVICE_ID_LPE11000S:
James Smart12222f42014-05-21 08:05:19 -04002256 m = (typeof(m)){"LPe11000-S", "PCIe",
2257 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002258 break;
James Smartb87eab32007-04-25 09:53:28 -04002259 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05002260 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002261 break;
2262 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05002263 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002264 break;
2265 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05002266 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002267 break;
2268 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002269 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002270 break;
2271 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002272 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002273 break;
2274 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05002275 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002276 break;
James Smart84774a42008-08-24 21:50:06 -04002277 case PCI_DEVICE_ID_HORNET:
James Smart12222f42014-05-21 08:05:19 -04002278 m = (typeof(m)){"LP21000", "PCIe",
2279 "Obsolete, Unsupported FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002280 GE = 1;
2281 break;
2282 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05002283 m = (typeof(m)){"LPev12000", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002284 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002285 break;
2286 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05002287 m = (typeof(m)){"LPev12000", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002288 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002289 break;
2290 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05002291 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002292 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002293 break;
James Smartda0436e2009-05-22 14:51:39 -04002294 case PCI_DEVICE_ID_TIGERSHARK:
2295 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002296 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04002297 break;
James Smarta747c9c2009-11-18 15:41:10 -05002298 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04002299 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002300 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
2301 break;
2302 case PCI_DEVICE_ID_FALCON:
2303 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
2304 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04002305 break;
James Smart98fc5dd2010-06-07 15:24:29 -04002306 case PCI_DEVICE_ID_BALIUS:
2307 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
James Smart12222f42014-05-21 08:05:19 -04002308 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart98fc5dd2010-06-07 15:24:29 -04002309 break;
James Smart085c6472010-11-20 23:11:37 -05002310 case PCI_DEVICE_ID_LANCER_FC:
James Smartc0c11512011-05-24 11:41:34 -04002311 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
James Smart085c6472010-11-20 23:11:37 -05002312 break;
James Smart12222f42014-05-21 08:05:19 -04002313 case PCI_DEVICE_ID_LANCER_FC_VF:
2314 m = (typeof(m)){"LPe16000", "PCIe",
2315 "Obsolete, Unsupported Fibre Channel Adapter"};
2316 break;
James Smart085c6472010-11-20 23:11:37 -05002317 case PCI_DEVICE_ID_LANCER_FCOE:
2318 oneConnect = 1;
James Smart079b5c92011-08-21 21:48:49 -04002319 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
James Smart085c6472010-11-20 23:11:37 -05002320 break;
James Smart12222f42014-05-21 08:05:19 -04002321 case PCI_DEVICE_ID_LANCER_FCOE_VF:
2322 oneConnect = 1;
2323 m = (typeof(m)){"OCe15100", "PCIe",
2324 "Obsolete, Unsupported FCoE"};
2325 break;
James Smartd38dd522015-08-31 16:48:17 -04002326 case PCI_DEVICE_ID_LANCER_G6_FC:
2327 m = (typeof(m)){"LPe32000", "PCIe", "Fibre Channel Adapter"};
2328 break;
James Smartf8cafd32012-08-03 12:42:51 -04002329 case PCI_DEVICE_ID_SKYHAWK:
2330 case PCI_DEVICE_ID_SKYHAWK_VF:
2331 oneConnect = 1;
2332 m = (typeof(m)){"OCe14000", "PCIe", "FCoE"};
2333 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002334 default:
James Smarta747c9c2009-11-18 15:41:10 -05002335 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002336 break;
dea31012005-04-17 16:05:31 -05002337 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002338
2339 if (mdp && mdp[0] == '\0')
2340 snprintf(mdp, 79,"%s", m.name);
James Smartc0c11512011-05-24 11:41:34 -04002341 /*
2342 * oneConnect hba requires special processing, they are all initiators
James Smartda0436e2009-05-22 14:51:39 -04002343 * and we put the port number on the end
2344 */
2345 if (descp && descp[0] == '\0') {
2346 if (oneConnect)
2347 snprintf(descp, 255,
James Smart4169d862012-09-29 11:32:09 -04002348 "Emulex OneConnect %s, %s Initiator %s",
James Smarta747c9c2009-11-18 15:41:10 -05002349 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04002350 phba->Port);
James Smart4169d862012-09-29 11:32:09 -04002351 else if (max_speed == 0)
2352 snprintf(descp, 255,
Sebastian Herbszt290237d2015-08-31 16:48:08 -04002353 "Emulex %s %s %s",
James Smart4169d862012-09-29 11:32:09 -04002354 m.name, m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002355 else
2356 snprintf(descp, 255,
2357 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05002358 m.name, max_speed, (GE) ? "GE" : "Gb",
2359 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002360 }
dea31012005-04-17 16:05:31 -05002361}
2362
James Smarte59058c2008-08-24 21:49:00 -04002363/**
James Smart3621a712009-04-06 18:47:14 -04002364 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04002365 * @phba: pointer to lpfc hba data structure.
2366 * @pring: pointer to a IOCB ring.
2367 * @cnt: the number of IOCBs to be posted to the IOCB ring.
2368 *
2369 * This routine posts a given number of IOCBs with the associated DMA buffer
2370 * descriptors specified by the cnt argument to the given IOCB ring.
2371 *
2372 * Return codes
2373 * The number of IOCBs NOT able to be posted to the IOCB ring.
2374 **/
dea31012005-04-17 16:05:31 -05002375int
James Smart495a7142008-06-14 22:52:59 -04002376lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05002377{
2378 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002379 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05002380 struct lpfc_dmabuf *mp1, *mp2;
2381
2382 cnt += pring->missbufcnt;
2383
2384 /* While there are buffers to post */
2385 while (cnt > 0) {
2386 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002387 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002388 if (iocb == NULL) {
2389 pring->missbufcnt = cnt;
2390 return cnt;
2391 }
dea31012005-04-17 16:05:31 -05002392 icmd = &iocb->iocb;
2393
2394 /* 2 buffers can be posted per command */
2395 /* Allocate buffer to post */
2396 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2397 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04002398 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2399 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002400 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002401 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002402 pring->missbufcnt = cnt;
2403 return cnt;
2404 }
2405
2406 INIT_LIST_HEAD(&mp1->list);
2407 /* Allocate buffer to post */
2408 if (cnt > 1) {
2409 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2410 if (mp2)
2411 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2412 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04002413 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002414 kfree(mp2);
dea31012005-04-17 16:05:31 -05002415 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2416 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002417 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002418 pring->missbufcnt = cnt;
2419 return cnt;
2420 }
2421
2422 INIT_LIST_HEAD(&mp2->list);
2423 } else {
2424 mp2 = NULL;
2425 }
2426
2427 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2428 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2429 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2430 icmd->ulpBdeCount = 1;
2431 cnt--;
2432 if (mp2) {
2433 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2434 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2435 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2436 cnt--;
2437 icmd->ulpBdeCount = 2;
2438 }
2439
2440 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2441 icmd->ulpLe = 1;
2442
James Smart3772a992009-05-22 14:50:54 -04002443 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2444 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05002445 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2446 kfree(mp1);
2447 cnt++;
2448 if (mp2) {
2449 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2450 kfree(mp2);
2451 cnt++;
2452 }
James Bottomley604a3e32005-10-29 10:28:33 -05002453 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002454 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05002455 return cnt;
2456 }
dea31012005-04-17 16:05:31 -05002457 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05002458 if (mp2)
dea31012005-04-17 16:05:31 -05002459 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05002460 }
2461 pring->missbufcnt = 0;
2462 return 0;
2463}
2464
James Smarte59058c2008-08-24 21:49:00 -04002465/**
James Smart3621a712009-04-06 18:47:14 -04002466 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04002467 * @phba: pointer to lpfc hba data structure.
2468 *
2469 * This routine posts initial receive IOCB buffers to the ELS ring. The
2470 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
James Smart895427b2017-02-12 13:52:30 -08002471 * set to 64 IOCBs. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04002472 *
2473 * Return codes
2474 * 0 - success (currently always success)
2475 **/
dea31012005-04-17 16:05:31 -05002476static int
James Smart2e0fef82007-06-17 19:56:36 -05002477lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002478{
2479 struct lpfc_sli *psli = &phba->sli;
2480
2481 /* Ring 0, ELS / CT buffers */
James Smart895427b2017-02-12 13:52:30 -08002482 lpfc_post_buffer(phba, &psli->sli3_ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05002483 /* Ring 2 - FCP no buffers needed */
2484
2485 return 0;
2486}
2487
2488#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2489
James Smarte59058c2008-08-24 21:49:00 -04002490/**
James Smart3621a712009-04-06 18:47:14 -04002491 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04002492 * @HashResultPointer: pointer to an array as hash table.
2493 *
2494 * This routine sets up the initial values to the array of hash table entries
2495 * for the LC HBAs.
2496 **/
dea31012005-04-17 16:05:31 -05002497static void
2498lpfc_sha_init(uint32_t * HashResultPointer)
2499{
2500 HashResultPointer[0] = 0x67452301;
2501 HashResultPointer[1] = 0xEFCDAB89;
2502 HashResultPointer[2] = 0x98BADCFE;
2503 HashResultPointer[3] = 0x10325476;
2504 HashResultPointer[4] = 0xC3D2E1F0;
2505}
2506
James Smarte59058c2008-08-24 21:49:00 -04002507/**
James Smart3621a712009-04-06 18:47:14 -04002508 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04002509 * @HashResultPointer: pointer to an initial/result hash table.
2510 * @HashWorkingPointer: pointer to an working hash table.
2511 *
2512 * This routine iterates an initial hash table pointed by @HashResultPointer
2513 * with the values from the working hash table pointeed by @HashWorkingPointer.
2514 * The results are putting back to the initial hash table, returned through
2515 * the @HashResultPointer as the result hash table.
2516 **/
dea31012005-04-17 16:05:31 -05002517static void
2518lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2519{
2520 int t;
2521 uint32_t TEMP;
2522 uint32_t A, B, C, D, E;
2523 t = 16;
2524 do {
2525 HashWorkingPointer[t] =
2526 S(1,
2527 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2528 8] ^
2529 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2530 } while (++t <= 79);
2531 t = 0;
2532 A = HashResultPointer[0];
2533 B = HashResultPointer[1];
2534 C = HashResultPointer[2];
2535 D = HashResultPointer[3];
2536 E = HashResultPointer[4];
2537
2538 do {
2539 if (t < 20) {
2540 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2541 } else if (t < 40) {
2542 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2543 } else if (t < 60) {
2544 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2545 } else {
2546 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2547 }
2548 TEMP += S(5, A) + E + HashWorkingPointer[t];
2549 E = D;
2550 D = C;
2551 C = S(30, B);
2552 B = A;
2553 A = TEMP;
2554 } while (++t <= 79);
2555
2556 HashResultPointer[0] += A;
2557 HashResultPointer[1] += B;
2558 HashResultPointer[2] += C;
2559 HashResultPointer[3] += D;
2560 HashResultPointer[4] += E;
2561
2562}
2563
James Smarte59058c2008-08-24 21:49:00 -04002564/**
James Smart3621a712009-04-06 18:47:14 -04002565 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04002566 * @RandomChallenge: pointer to the entry of host challenge random number array.
2567 * @HashWorking: pointer to the entry of the working hash array.
2568 *
2569 * This routine calculates the working hash array referred by @HashWorking
2570 * from the challenge random numbers associated with the host, referred by
2571 * @RandomChallenge. The result is put into the entry of the working hash
2572 * array and returned by reference through @HashWorking.
2573 **/
dea31012005-04-17 16:05:31 -05002574static void
2575lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2576{
2577 *HashWorking = (*RandomChallenge ^ *HashWorking);
2578}
2579
James Smarte59058c2008-08-24 21:49:00 -04002580/**
James Smart3621a712009-04-06 18:47:14 -04002581 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04002582 * @phba: pointer to lpfc hba data structure.
2583 * @hbainit: pointer to an array of unsigned 32-bit integers.
2584 *
2585 * This routine performs the special handling for LC HBA initialization.
2586 **/
dea31012005-04-17 16:05:31 -05002587void
2588lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2589{
2590 int t;
2591 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05002592 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05002593
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002594 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002595 if (!HashWorking)
2596 return;
2597
dea31012005-04-17 16:05:31 -05002598 HashWorking[0] = HashWorking[78] = *pwwnn++;
2599 HashWorking[1] = HashWorking[79] = *pwwnn;
2600
2601 for (t = 0; t < 7; t++)
2602 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2603
2604 lpfc_sha_init(hbainit);
2605 lpfc_sha_iterate(hbainit, HashWorking);
2606 kfree(HashWorking);
2607}
2608
James Smarte59058c2008-08-24 21:49:00 -04002609/**
James Smart3621a712009-04-06 18:47:14 -04002610 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04002611 * @vport: pointer to a virtual N_Port data structure.
2612 *
2613 * This routine performs the necessary cleanups before deleting the @vport.
2614 * It invokes the discovery state machine to perform necessary state
2615 * transitions and to release the ndlps associated with the @vport. Note,
2616 * the physical port is treated as @vport 0.
2617 **/
James Smart87af33f2007-10-27 13:37:43 -04002618void
James Smart2e0fef82007-06-17 19:56:36 -05002619lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002620{
James Smart87af33f2007-10-27 13:37:43 -04002621 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002622 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04002623 int i = 0;
dea31012005-04-17 16:05:31 -05002624
James Smart87af33f2007-10-27 13:37:43 -04002625 if (phba->link_state > LPFC_LINK_DOWN)
2626 lpfc_port_link_failure(vport);
2627
2628 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002629 if (!NLP_CHK_NODE_ACT(ndlp)) {
2630 ndlp = lpfc_enable_node(vport, ndlp,
2631 NLP_STE_UNUSED_NODE);
2632 if (!ndlp)
2633 continue;
2634 spin_lock_irq(&phba->ndlp_lock);
2635 NLP_SET_FREE_REQ(ndlp);
2636 spin_unlock_irq(&phba->ndlp_lock);
2637 /* Trigger the release of the ndlp memory */
2638 lpfc_nlp_put(ndlp);
2639 continue;
2640 }
2641 spin_lock_irq(&phba->ndlp_lock);
2642 if (NLP_CHK_FREE_REQ(ndlp)) {
2643 /* The ndlp should not be in memory free mode already */
2644 spin_unlock_irq(&phba->ndlp_lock);
2645 continue;
2646 } else
2647 /* Indicate request for freeing ndlp memory */
2648 NLP_SET_FREE_REQ(ndlp);
2649 spin_unlock_irq(&phba->ndlp_lock);
2650
James Smart58da1ff2008-04-07 10:15:56 -04002651 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2652 ndlp->nlp_DID == Fabric_DID) {
2653 /* Just free up ndlp with Fabric_DID for vports */
2654 lpfc_nlp_put(ndlp);
2655 continue;
2656 }
2657
James Smarteff4a012012-01-18 16:25:25 -05002658 /* take care of nodes in unused state before the state
2659 * machine taking action.
2660 */
2661 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
2662 lpfc_nlp_put(ndlp);
2663 continue;
2664 }
2665
James Smart87af33f2007-10-27 13:37:43 -04002666 if (ndlp->nlp_type & NLP_FABRIC)
2667 lpfc_disc_state_machine(vport, ndlp, NULL,
2668 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002669
James Smart87af33f2007-10-27 13:37:43 -04002670 lpfc_disc_state_machine(vport, ndlp, NULL,
2671 NLP_EVT_DEVICE_RM);
2672 }
2673
James Smarta8adb832007-10-27 13:37:53 -04002674 /* At this point, ALL ndlp's should be gone
2675 * because of the previous NLP_EVT_DEVICE_RM.
2676 * Lets wait for this to happen, if needed.
2677 */
James Smart87af33f2007-10-27 13:37:43 -04002678 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002679 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002680 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002681 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002682 list_for_each_entry_safe(ndlp, next_ndlp,
2683 &vport->fc_nodes, nlp_listp) {
2684 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2685 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002686 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002687 "usgmap:x%x refcnt:%d\n",
2688 ndlp->nlp_DID, (void *)ndlp,
2689 ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01002690 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05002691 }
James Smarta8adb832007-10-27 13:37:53 -04002692 break;
James Smart87af33f2007-10-27 13:37:43 -04002693 }
James Smarta8adb832007-10-27 13:37:53 -04002694
2695 /* Wait for any activity on ndlps to settle */
2696 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002697 }
James Smart1151e3e2011-02-16 12:39:35 -05002698 lpfc_cleanup_vports_rrqs(vport, NULL);
dea31012005-04-17 16:05:31 -05002699}
2700
James Smarte59058c2008-08-24 21:49:00 -04002701/**
James Smart3621a712009-04-06 18:47:14 -04002702 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002703 * @vport: pointer to a virtual N_Port data structure.
2704 *
2705 * This routine stops all the timers associated with a @vport. This function
2706 * is invoked before disabling or deleting a @vport. Note that the physical
2707 * port is treated as @vport 0.
2708 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002709void
2710lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002711{
James Smart92d7f7b2007-06-17 19:56:38 -05002712 del_timer_sync(&vport->els_tmofunc);
James Smart92494142011-02-16 12:39:44 -05002713 del_timer_sync(&vport->delayed_disc_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002714 lpfc_can_disctmo(vport);
2715 return;
dea31012005-04-17 16:05:31 -05002716}
2717
James Smarte59058c2008-08-24 21:49:00 -04002718/**
James Smartecfd03c2010-02-12 14:41:27 -05002719 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2720 * @phba: pointer to lpfc hba data structure.
2721 *
2722 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2723 * caller of this routine should already hold the host lock.
2724 **/
2725void
2726__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2727{
James Smart5ac6b302010-10-22 11:05:36 -04002728 /* Clear pending FCF rediscovery wait flag */
2729 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2730
James Smartecfd03c2010-02-12 14:41:27 -05002731 /* Now, try to stop the timer */
2732 del_timer(&phba->fcf.redisc_wait);
2733}
2734
2735/**
2736 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2737 * @phba: pointer to lpfc hba data structure.
2738 *
2739 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2740 * checks whether the FCF rediscovery wait timer is pending with the host
2741 * lock held before proceeding with disabling the timer and clearing the
2742 * wait timer pendig flag.
2743 **/
2744void
2745lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2746{
2747 spin_lock_irq(&phba->hbalock);
2748 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2749 /* FCF rediscovery timer already fired or stopped */
2750 spin_unlock_irq(&phba->hbalock);
2751 return;
2752 }
2753 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart5ac6b302010-10-22 11:05:36 -04002754 /* Clear failover in progress flags */
2755 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
James Smartecfd03c2010-02-12 14:41:27 -05002756 spin_unlock_irq(&phba->hbalock);
2757}
2758
2759/**
James Smart3772a992009-05-22 14:50:54 -04002760 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002761 * @phba: pointer to lpfc hba data structure.
2762 *
2763 * This routine stops all the timers associated with a HBA. This function is
2764 * invoked before either putting a HBA offline or unloading the driver.
2765 **/
James Smart3772a992009-05-22 14:50:54 -04002766void
2767lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002768{
James Smart51ef4c22007-08-02 11:10:31 -04002769 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05002770 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002771 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002772 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002773 del_timer_sync(&phba->hb_tmofunc);
James Smart1151e3e2011-02-16 12:39:35 -05002774 if (phba->sli_rev == LPFC_SLI_REV4) {
2775 del_timer_sync(&phba->rrq_tmr);
2776 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2777 }
James Smart3772a992009-05-22 14:50:54 -04002778 phba->hb_outstanding = 0;
2779
2780 switch (phba->pci_dev_grp) {
2781 case LPFC_PCI_DEV_LP:
2782 /* Stop any LightPulse device specific driver timers */
2783 del_timer_sync(&phba->fcp_poll_timer);
2784 break;
2785 case LPFC_PCI_DEV_OC:
2786 /* Stop any OneConnect device sepcific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05002787 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04002788 break;
2789 default:
2790 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2791 "0297 Invalid device group (x%x)\n",
2792 phba->pci_dev_grp);
2793 break;
2794 }
James Smart2e0fef82007-06-17 19:56:36 -05002795 return;
dea31012005-04-17 16:05:31 -05002796}
2797
James Smarte59058c2008-08-24 21:49:00 -04002798/**
James Smart3621a712009-04-06 18:47:14 -04002799 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002800 * @phba: pointer to lpfc hba data structure.
2801 *
2802 * This routine marks a HBA's management interface as blocked. Once the HBA's
2803 * management interface is marked as blocked, all the user space access to
2804 * the HBA, whether they are from sysfs interface or libdfc interface will
2805 * all be blocked. The HBA is set to block the management interface when the
2806 * driver prepares the HBA interface for online or offline.
2807 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002808static void
James Smart618a5232012-06-12 13:54:36 -04002809lpfc_block_mgmt_io(struct lpfc_hba *phba, int mbx_action)
Adrian Bunka6ababd2007-11-05 18:07:33 +01002810{
2811 unsigned long iflag;
James Smart6e7288d2010-06-07 15:23:35 -04002812 uint8_t actcmd = MBX_HEARTBEAT;
2813 unsigned long timeout;
2814
Adrian Bunka6ababd2007-11-05 18:07:33 +01002815 spin_lock_irqsave(&phba->hbalock, iflag);
2816 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
James Smart618a5232012-06-12 13:54:36 -04002817 spin_unlock_irqrestore(&phba->hbalock, iflag);
2818 if (mbx_action == LPFC_MBX_NO_WAIT)
2819 return;
2820 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
2821 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002822 if (phba->sli.mbox_active) {
James Smart6e7288d2010-06-07 15:23:35 -04002823 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
James Smarta183a152011-10-10 21:32:43 -04002824 /* Determine how long we might wait for the active mailbox
2825 * command to be gracefully completed by firmware.
2826 */
2827 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
2828 phba->sli.mbox_active) * 1000) + jiffies;
2829 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002830 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002831
James Smart6e7288d2010-06-07 15:23:35 -04002832 /* Wait for the outstnading mailbox command to complete */
2833 while (phba->sli.mbox_active) {
2834 /* Check active mailbox complete status every 2ms */
2835 msleep(2);
2836 if (time_after(jiffies, timeout)) {
2837 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2838 "2813 Mgmt IO is Blocked %x "
2839 "- mbox cmd %x still active\n",
2840 phba->sli.sli_flag, actcmd);
2841 break;
2842 }
2843 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002844}
2845
James Smarte59058c2008-08-24 21:49:00 -04002846/**
James Smart6b5151f2012-01-18 16:24:06 -05002847 * lpfc_sli4_node_prep - Assign RPIs for active nodes.
2848 * @phba: pointer to lpfc hba data structure.
2849 *
2850 * Allocate RPIs for all active remote nodes. This is needed whenever
2851 * an SLI4 adapter is reset and the driver is not unloading. Its purpose
2852 * is to fixup the temporary rpi assignments.
2853 **/
2854void
2855lpfc_sli4_node_prep(struct lpfc_hba *phba)
2856{
2857 struct lpfc_nodelist *ndlp, *next_ndlp;
2858 struct lpfc_vport **vports;
2859 int i;
2860
2861 if (phba->sli_rev != LPFC_SLI_REV4)
2862 return;
2863
2864 vports = lpfc_create_vport_work_array(phba);
2865 if (vports != NULL) {
2866 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2867 if (vports[i]->load_flag & FC_UNLOADING)
2868 continue;
2869
2870 list_for_each_entry_safe(ndlp, next_ndlp,
2871 &vports[i]->fc_nodes,
2872 nlp_listp) {
James Smartbe6bb942015-04-07 15:07:22 -04002873 if (NLP_CHK_NODE_ACT(ndlp)) {
James Smart6b5151f2012-01-18 16:24:06 -05002874 ndlp->nlp_rpi =
2875 lpfc_sli4_alloc_rpi(phba);
James Smartbe6bb942015-04-07 15:07:22 -04002876 lpfc_printf_vlog(ndlp->vport, KERN_INFO,
2877 LOG_NODE,
2878 "0009 rpi:%x DID:%x "
2879 "flg:%x map:%x %p\n",
2880 ndlp->nlp_rpi,
2881 ndlp->nlp_DID,
2882 ndlp->nlp_flag,
2883 ndlp->nlp_usg_map,
2884 ndlp);
2885 }
James Smart6b5151f2012-01-18 16:24:06 -05002886 }
2887 }
2888 }
2889 lpfc_destroy_vport_work_array(phba, vports);
2890}
2891
2892/**
James Smart3621a712009-04-06 18:47:14 -04002893 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04002894 * @phba: pointer to lpfc hba data structure.
2895 *
2896 * This routine initializes the HBA and brings a HBA online. During this
2897 * process, the management interface is blocked to prevent user space access
2898 * to the HBA interfering with the driver initialization.
2899 *
2900 * Return codes
2901 * 0 - successful
2902 * 1 - failed
2903 **/
dea31012005-04-17 16:05:31 -05002904int
James Smart2e0fef82007-06-17 19:56:36 -05002905lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002906{
Julia Lawall372bd282008-12-16 16:15:08 +01002907 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04002908 struct lpfc_vport **vports;
2909 int i;
James Smart16a3a202013-04-17 20:14:38 -04002910 bool vpis_cleared = false;
James Smart2e0fef82007-06-17 19:56:36 -05002911
dea31012005-04-17 16:05:31 -05002912 if (!phba)
2913 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01002914 vport = phba->pport;
dea31012005-04-17 16:05:31 -05002915
James Smart2e0fef82007-06-17 19:56:36 -05002916 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05002917 return 0;
2918
James Smarted957682007-06-17 19:56:37 -05002919 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002920 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05002921
James Smart618a5232012-06-12 13:54:36 -04002922 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05002923
James Smartda0436e2009-05-22 14:51:39 -04002924 if (phba->sli_rev == LPFC_SLI_REV4) {
2925 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2926 lpfc_unblock_mgmt_io(phba);
2927 return 1;
2928 }
James Smart16a3a202013-04-17 20:14:38 -04002929 spin_lock_irq(&phba->hbalock);
2930 if (!phba->sli4_hba.max_cfg_param.vpi_used)
2931 vpis_cleared = true;
2932 spin_unlock_irq(&phba->hbalock);
James Smartda0436e2009-05-22 14:51:39 -04002933 } else {
James Smart895427b2017-02-12 13:52:30 -08002934 lpfc_sli_queue_init(phba);
James Smartda0436e2009-05-22 14:51:39 -04002935 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2936 lpfc_unblock_mgmt_io(phba);
2937 return 1;
2938 }
James Smart46fa3112007-04-25 09:51:45 -04002939 }
dea31012005-04-17 16:05:31 -05002940
James Smart549e55c2007-08-02 11:09:51 -04002941 vports = lpfc_create_vport_work_array(phba);
Arnd Bergmannaeb66412016-03-14 15:29:44 +01002942 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04002943 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002944 struct Scsi_Host *shost;
2945 shost = lpfc_shost_from_vport(vports[i]);
2946 spin_lock_irq(shost->host_lock);
2947 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2948 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2949 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart16a3a202013-04-17 20:14:38 -04002950 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart1c6834a2009-07-19 10:01:26 -04002951 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart16a3a202013-04-17 20:14:38 -04002952 if ((vpis_cleared) &&
2953 (vports[i]->port_type !=
2954 LPFC_PHYSICAL_PORT))
2955 vports[i]->vpi = 0;
2956 }
James Smart549e55c2007-08-02 11:09:51 -04002957 spin_unlock_irq(shost->host_lock);
2958 }
Arnd Bergmannaeb66412016-03-14 15:29:44 +01002959 }
2960 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002961
James Smart46fa3112007-04-25 09:51:45 -04002962 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002963 return 0;
2964}
2965
James Smarte59058c2008-08-24 21:49:00 -04002966/**
James Smart3621a712009-04-06 18:47:14 -04002967 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04002968 * @phba: pointer to lpfc hba data structure.
2969 *
2970 * This routine marks a HBA's management interface as not blocked. Once the
2971 * HBA's management interface is marked as not blocked, all the user space
2972 * access to the HBA, whether they are from sysfs interface or libdfc
2973 * interface will be allowed. The HBA is set to block the management interface
2974 * when the driver prepares the HBA interface for online or offline and then
2975 * set to unblock the management interface afterwards.
2976 **/
James Smart46fa3112007-04-25 09:51:45 -04002977void
James Smart46fa3112007-04-25 09:51:45 -04002978lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2979{
2980 unsigned long iflag;
2981
James Smart2e0fef82007-06-17 19:56:36 -05002982 spin_lock_irqsave(&phba->hbalock, iflag);
2983 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2984 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04002985}
2986
James Smarte59058c2008-08-24 21:49:00 -04002987/**
James Smart3621a712009-04-06 18:47:14 -04002988 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04002989 * @phba: pointer to lpfc hba data structure.
2990 *
2991 * This routine is invoked to prepare a HBA to be brought offline. It performs
2992 * unregistration login to all the nodes on all vports and flushes the mailbox
2993 * queue to make it ready to be brought offline.
2994 **/
James Smart46fa3112007-04-25 09:51:45 -04002995void
James Smart618a5232012-06-12 13:54:36 -04002996lpfc_offline_prep(struct lpfc_hba *phba, int mbx_action)
James Smart46fa3112007-04-25 09:51:45 -04002997{
James Smart2e0fef82007-06-17 19:56:36 -05002998 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04002999 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04003000 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05003001 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04003002 int i;
dea31012005-04-17 16:05:31 -05003003
James Smart2e0fef82007-06-17 19:56:36 -05003004 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04003005 return;
dea31012005-04-17 16:05:31 -05003006
James Smart618a5232012-06-12 13:54:36 -04003007 lpfc_block_mgmt_io(phba, mbx_action);
dea31012005-04-17 16:05:31 -05003008
3009 lpfc_linkdown(phba);
3010
James Smart87af33f2007-10-27 13:37:43 -04003011 /* Issue an unreg_login to all nodes on all vports */
3012 vports = lpfc_create_vport_work_array(phba);
3013 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04003014 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04003015 if (vports[i]->load_flag & FC_UNLOADING)
3016 continue;
James Smart72100cc2010-02-12 14:43:01 -05003017 shost = lpfc_shost_from_vport(vports[i]);
3018 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003019 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05003020 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
3021 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05003022 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05003023
James Smart87af33f2007-10-27 13:37:43 -04003024 shost = lpfc_shost_from_vport(vports[i]);
3025 list_for_each_entry_safe(ndlp, next_ndlp,
3026 &vports[i]->fc_nodes,
3027 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05003028 if (!NLP_CHK_NODE_ACT(ndlp))
3029 continue;
James Smart87af33f2007-10-27 13:37:43 -04003030 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
3031 continue;
3032 if (ndlp->nlp_type & NLP_FABRIC) {
3033 lpfc_disc_state_machine(vports[i], ndlp,
3034 NULL, NLP_EVT_DEVICE_RECOVERY);
3035 lpfc_disc_state_machine(vports[i], ndlp,
3036 NULL, NLP_EVT_DEVICE_RM);
3037 }
3038 spin_lock_irq(shost->host_lock);
3039 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart401ee0c2012-03-01 22:35:34 -05003040 spin_unlock_irq(shost->host_lock);
James Smart6b5151f2012-01-18 16:24:06 -05003041 /*
3042 * Whenever an SLI4 port goes offline, free the
James Smart401ee0c2012-03-01 22:35:34 -05003043 * RPI. Get a new RPI when the adapter port
3044 * comes back online.
James Smart6b5151f2012-01-18 16:24:06 -05003045 */
James Smartbe6bb942015-04-07 15:07:22 -04003046 if (phba->sli_rev == LPFC_SLI_REV4) {
3047 lpfc_printf_vlog(ndlp->vport,
3048 KERN_INFO, LOG_NODE,
3049 "0011 lpfc_offline: "
3050 "ndlp:x%p did %x "
3051 "usgmap:x%x rpi:%x\n",
3052 ndlp, ndlp->nlp_DID,
3053 ndlp->nlp_usg_map,
3054 ndlp->nlp_rpi);
3055
James Smart6b5151f2012-01-18 16:24:06 -05003056 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smartbe6bb942015-04-07 15:07:22 -04003057 }
James Smart87af33f2007-10-27 13:37:43 -04003058 lpfc_unreg_rpi(vports[i], ndlp);
3059 }
3060 }
3061 }
James Smart09372822008-01-11 01:52:54 -05003062 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05003063
James Smart618a5232012-06-12 13:54:36 -04003064 lpfc_sli_mbox_sys_shutdown(phba, mbx_action);
James Smart46fa3112007-04-25 09:51:45 -04003065}
3066
James Smarte59058c2008-08-24 21:49:00 -04003067/**
James Smart3621a712009-04-06 18:47:14 -04003068 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04003069 * @phba: pointer to lpfc hba data structure.
3070 *
3071 * This routine actually brings a HBA offline. It stops all the timers
3072 * associated with the HBA, brings down the SLI layer, and eventually
3073 * marks the HBA as in offline state for the upper layer protocol.
3074 **/
James Smart46fa3112007-04-25 09:51:45 -04003075void
James Smart2e0fef82007-06-17 19:56:36 -05003076lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04003077{
James Smart549e55c2007-08-02 11:09:51 -04003078 struct Scsi_Host *shost;
3079 struct lpfc_vport **vports;
3080 int i;
James Smart46fa3112007-04-25 09:51:45 -04003081
James Smart549e55c2007-08-02 11:09:51 -04003082 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04003083 return;
James Smart688a8862006-07-06 15:49:56 -04003084
James Smartda0436e2009-05-22 14:51:39 -04003085 /* stop port and all timers associated with this hba */
3086 lpfc_stop_port(phba);
James Smart51ef4c22007-08-02 11:10:31 -04003087 vports = lpfc_create_vport_work_array(phba);
3088 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04003089 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04003090 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05003091 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05003092 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003093 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05003094 /* Bring down the SLI Layer and cleanup. The HBA is offline
3095 now. */
3096 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003097 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04003098 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003099 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04003100 vports = lpfc_create_vport_work_array(phba);
3101 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04003102 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04003103 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04003104 spin_lock_irq(shost->host_lock);
3105 vports[i]->work_port_events = 0;
3106 vports[i]->fc_flag |= FC_OFFLINE_MODE;
3107 spin_unlock_irq(shost->host_lock);
3108 }
James Smart09372822008-01-11 01:52:54 -05003109 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05003110}
3111
James Smarte59058c2008-08-24 21:49:00 -04003112/**
James Smart3621a712009-04-06 18:47:14 -04003113 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04003114 * @phba: pointer to lpfc hba data structure.
3115 *
3116 * This routine is to free all the SCSI buffers and IOCBs from the driver
3117 * list back to kernel. It is called from lpfc_pci_remove_one to free
3118 * the internal resources before the device is removed from the system.
James Smarte59058c2008-08-24 21:49:00 -04003119 **/
James Smart8a9d2e82012-05-09 21:16:12 -04003120static void
James Smart2e0fef82007-06-17 19:56:36 -05003121lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003122{
3123 struct lpfc_scsi_buf *sb, *sb_next;
3124 struct lpfc_iocbq *io, *io_next;
3125
James Smart895427b2017-02-12 13:52:30 -08003126 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
3127 return;
3128
James Smart2e0fef82007-06-17 19:56:36 -05003129 spin_lock_irq(&phba->hbalock);
James Smarta40fc5f2013-04-17 20:17:40 -04003130
dea31012005-04-17 16:05:31 -05003131 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smarta40fc5f2013-04-17 20:17:40 -04003132
3133 spin_lock(&phba->scsi_buf_list_put_lock);
3134 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_put,
3135 list) {
dea31012005-04-17 16:05:31 -05003136 list_del(&sb->list);
James Smart895427b2017-02-12 13:52:30 -08003137 pci_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05003138 sb->dma_handle);
dea31012005-04-17 16:05:31 -05003139 kfree(sb);
3140 phba->total_scsi_bufs--;
3141 }
James Smarta40fc5f2013-04-17 20:17:40 -04003142 spin_unlock(&phba->scsi_buf_list_put_lock);
3143
3144 spin_lock(&phba->scsi_buf_list_get_lock);
3145 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_get,
3146 list) {
3147 list_del(&sb->list);
James Smart895427b2017-02-12 13:52:30 -08003148 pci_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
James Smarta40fc5f2013-04-17 20:17:40 -04003149 sb->dma_handle);
3150 kfree(sb);
3151 phba->total_scsi_bufs--;
3152 }
3153 spin_unlock(&phba->scsi_buf_list_get_lock);
dea31012005-04-17 16:05:31 -05003154
3155 /* Release all the lpfc_iocbq entries maintained by this host. */
3156 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
3157 list_del(&io->list);
3158 kfree(io);
3159 phba->total_iocbq_bufs--;
3160 }
James Smart6d368e52011-05-24 11:44:12 -04003161
James Smart2e0fef82007-06-17 19:56:36 -05003162 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003163}
James Smart8a9d2e82012-05-09 21:16:12 -04003164/**
James Smart895427b2017-02-12 13:52:30 -08003165 * lpfc_nvme_free - Free all the NVME buffers and IOCBs from driver lists
3166 * @phba: pointer to lpfc hba data structure.
3167 *
3168 * This routine is to free all the NVME buffers and IOCBs from the driver
3169 * list back to kernel. It is called from lpfc_pci_remove_one to free
3170 * the internal resources before the device is removed from the system.
3171 **/
3172static void
3173lpfc_nvme_free(struct lpfc_hba *phba)
3174{
3175 struct lpfc_nvme_buf *lpfc_ncmd, *lpfc_ncmd_next;
3176 struct lpfc_iocbq *io, *io_next;
3177
3178 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
3179 return;
3180
3181 spin_lock_irq(&phba->hbalock);
3182
3183 /* Release all the lpfc_nvme_bufs maintained by this host. */
3184 spin_lock(&phba->nvme_buf_list_put_lock);
3185 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3186 &phba->lpfc_nvme_buf_list_put, list) {
3187 list_del(&lpfc_ncmd->list);
3188 pci_pool_free(phba->lpfc_sg_dma_buf_pool, lpfc_ncmd->data,
3189 lpfc_ncmd->dma_handle);
3190 kfree(lpfc_ncmd);
3191 phba->total_nvme_bufs--;
3192 }
3193 spin_unlock(&phba->nvme_buf_list_put_lock);
3194
3195 spin_lock(&phba->nvme_buf_list_get_lock);
3196 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3197 &phba->lpfc_nvme_buf_list_get, list) {
3198 list_del(&lpfc_ncmd->list);
3199 pci_pool_free(phba->lpfc_sg_dma_buf_pool, lpfc_ncmd->data,
3200 lpfc_ncmd->dma_handle);
3201 kfree(lpfc_ncmd);
3202 phba->total_nvme_bufs--;
3203 }
3204 spin_unlock(&phba->nvme_buf_list_get_lock);
3205
3206 /* Release all the lpfc_iocbq entries maintained by this host. */
3207 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
3208 list_del(&io->list);
3209 kfree(io);
3210 phba->total_iocbq_bufs--;
3211 }
3212
3213 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/**
3335 * lpfc_sli4_scsi_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_scsi_sgl_update(struct lpfc_hba *phba)
3348{
3349 struct lpfc_scsi_buf *psb, *psb_next;
3350 uint16_t i, lxri, els_xri_cnt, scsi_xri_cnt;
3351 LIST_HEAD(scsi_sgl_list);
3352 int rc;
3353
3354 /*
3355 * update on pci function's els xri-sgl list
3356 */
3357 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3358 phba->total_scsi_bufs = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04003359
3360 /*
3361 * update on pci function's allocated scsi xri-sgl list
3362 */
James Smart8a9d2e82012-05-09 21:16:12 -04003363 /* maximum number of xris available for scsi buffers */
3364 phba->sli4_hba.scsi_xri_max = phba->sli4_hba.max_cfg_param.max_xri -
3365 els_xri_cnt;
3366
James Smart895427b2017-02-12 13:52:30 -08003367 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
3368 return 0;
3369
3370 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3371 phba->sli4_hba.scsi_xri_max = /* Split them up */
3372 (phba->sli4_hba.scsi_xri_max *
3373 phba->cfg_xri_split) / 100;
James Smart8a9d2e82012-05-09 21:16:12 -04003374
James Smarta40fc5f2013-04-17 20:17:40 -04003375 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart164cecd2013-09-06 12:22:38 -04003376 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003377 list_splice_init(&phba->lpfc_scsi_buf_list_get, &scsi_sgl_list);
3378 list_splice(&phba->lpfc_scsi_buf_list_put, &scsi_sgl_list);
James Smart164cecd2013-09-06 12:22:38 -04003379 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003380 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003381
3382 if (phba->sli4_hba.scsi_xri_cnt > phba->sli4_hba.scsi_xri_max) {
3383 /* max scsi xri shrinked below the allocated scsi buffers */
3384 scsi_xri_cnt = phba->sli4_hba.scsi_xri_cnt -
3385 phba->sli4_hba.scsi_xri_max;
3386 /* release the extra allocated scsi buffers */
3387 for (i = 0; i < scsi_xri_cnt; i++) {
3388 list_remove_head(&scsi_sgl_list, psb,
3389 struct lpfc_scsi_buf, list);
James Smarta2fc4aef2014-09-03 12:57:55 -04003390 if (psb) {
James Smart895427b2017-02-12 13:52:30 -08003391 pci_pool_free(phba->lpfc_sg_dma_buf_pool,
James Smarta2fc4aef2014-09-03 12:57:55 -04003392 psb->data, psb->dma_handle);
3393 kfree(psb);
3394 }
James Smart8a9d2e82012-05-09 21:16:12 -04003395 }
James Smarta40fc5f2013-04-17 20:17:40 -04003396 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003397 phba->sli4_hba.scsi_xri_cnt -= scsi_xri_cnt;
James Smarta40fc5f2013-04-17 20:17:40 -04003398 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003399 }
3400
3401 /* update xris associated to remaining allocated scsi buffers */
3402 psb = NULL;
3403 psb_next = NULL;
3404 list_for_each_entry_safe(psb, psb_next, &scsi_sgl_list, list) {
3405 lxri = lpfc_sli4_next_xritag(phba);
3406 if (lxri == NO_XRI) {
3407 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3408 "2560 Failed to allocate xri for "
3409 "scsi buffer\n");
3410 rc = -ENOMEM;
3411 goto out_free_mem;
3412 }
3413 psb->cur_iocbq.sli4_lxritag = lxri;
3414 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3415 }
James Smarta40fc5f2013-04-17 20:17:40 -04003416 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart164cecd2013-09-06 12:22:38 -04003417 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003418 list_splice_init(&scsi_sgl_list, &phba->lpfc_scsi_buf_list_get);
3419 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
James Smart164cecd2013-09-06 12:22:38 -04003420 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003421 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
dea31012005-04-17 16:05:31 -05003422 return 0;
James Smart8a9d2e82012-05-09 21:16:12 -04003423
3424out_free_mem:
James Smart8a9d2e82012-05-09 21:16:12 -04003425 lpfc_scsi_free(phba);
3426 return rc;
dea31012005-04-17 16:05:31 -05003427}
3428
James Smarte59058c2008-08-24 21:49:00 -04003429/**
James Smart895427b2017-02-12 13:52:30 -08003430 * lpfc_sli4_nvme_sgl_update - update xri-sgl sizing and mapping
3431 * @phba: pointer to lpfc hba data structure.
3432 *
3433 * This routine first calculates the sizes of the current els and allocated
3434 * scsi sgl lists, and then goes through all sgls to updates the physical
3435 * XRIs assigned due to port function reset. During port initialization, the
3436 * current els and allocated scsi sgl lists are 0s.
3437 *
3438 * Return codes
3439 * 0 - successful (for now, it always returns 0)
3440 **/
3441int
3442lpfc_sli4_nvme_sgl_update(struct lpfc_hba *phba)
3443{
3444 struct lpfc_nvme_buf *lpfc_ncmd = NULL, *lpfc_ncmd_next = NULL;
3445 uint16_t i, lxri, els_xri_cnt;
3446 uint16_t nvme_xri_cnt, nvme_xri_max;
3447 LIST_HEAD(nvme_sgl_list);
3448 int rc;
3449
3450 phba->total_nvme_bufs = 0;
3451
3452 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
3453 return 0;
3454 /*
3455 * update on pci function's allocated nvme xri-sgl list
3456 */
3457
3458 /* maximum number of xris available for nvme buffers */
3459 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3460 nvme_xri_max = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
3461 phba->sli4_hba.nvme_xri_max = nvme_xri_max;
3462 phba->sli4_hba.nvme_xri_max -= phba->sli4_hba.scsi_xri_max;
3463
3464 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3465 "6074 Current allocated NVME xri-sgl count:%d, "
3466 "maximum NVME xri count:%d\n",
3467 phba->sli4_hba.nvme_xri_cnt,
3468 phba->sli4_hba.nvme_xri_max);
3469
3470 spin_lock_irq(&phba->nvme_buf_list_get_lock);
3471 spin_lock(&phba->nvme_buf_list_put_lock);
3472 list_splice_init(&phba->lpfc_nvme_buf_list_get, &nvme_sgl_list);
3473 list_splice(&phba->lpfc_nvme_buf_list_put, &nvme_sgl_list);
3474 spin_unlock(&phba->nvme_buf_list_put_lock);
3475 spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3476
3477 if (phba->sli4_hba.nvme_xri_cnt > phba->sli4_hba.nvme_xri_max) {
3478 /* max nvme xri shrunk below the allocated nvme buffers */
3479 spin_lock_irq(&phba->nvme_buf_list_get_lock);
3480 nvme_xri_cnt = phba->sli4_hba.nvme_xri_cnt -
3481 phba->sli4_hba.nvme_xri_max;
3482 spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3483 /* release the extra allocated nvme buffers */
3484 for (i = 0; i < nvme_xri_cnt; i++) {
3485 list_remove_head(&nvme_sgl_list, lpfc_ncmd,
3486 struct lpfc_nvme_buf, list);
3487 if (lpfc_ncmd) {
3488 pci_pool_free(phba->lpfc_sg_dma_buf_pool,
3489 lpfc_ncmd->data,
3490 lpfc_ncmd->dma_handle);
3491 kfree(lpfc_ncmd);
3492 }
3493 }
3494 spin_lock_irq(&phba->nvme_buf_list_get_lock);
3495 phba->sli4_hba.nvme_xri_cnt -= nvme_xri_cnt;
3496 spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3497 }
3498
3499 /* update xris associated to remaining allocated nvme buffers */
3500 lpfc_ncmd = NULL;
3501 lpfc_ncmd_next = NULL;
3502 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3503 &nvme_sgl_list, list) {
3504 lxri = lpfc_sli4_next_xritag(phba);
3505 if (lxri == NO_XRI) {
3506 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3507 "6075 Failed to allocate xri for "
3508 "nvme buffer\n");
3509 rc = -ENOMEM;
3510 goto out_free_mem;
3511 }
3512 lpfc_ncmd->cur_iocbq.sli4_lxritag = lxri;
3513 lpfc_ncmd->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3514 }
3515 spin_lock_irq(&phba->nvme_buf_list_get_lock);
3516 spin_lock(&phba->nvme_buf_list_put_lock);
3517 list_splice_init(&nvme_sgl_list, &phba->lpfc_nvme_buf_list_get);
3518 INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_put);
3519 spin_unlock(&phba->nvme_buf_list_put_lock);
3520 spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3521 return 0;
3522
3523out_free_mem:
3524 lpfc_nvme_free(phba);
3525 return rc;
3526}
3527
3528/**
James Smart3621a712009-04-06 18:47:14 -04003529 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04003530 * @phba: pointer to lpfc hba data structure.
3531 * @instance: a unique integer ID to this FC port.
3532 * @dev: pointer to the device data structure.
3533 *
3534 * This routine creates a FC port for the upper layer protocol. The FC port
3535 * can be created on top of either a physical port or a virtual port provided
3536 * by the HBA. This routine also allocates a SCSI host data structure (shost)
3537 * and associates the FC port created before adding the shost into the SCSI
3538 * layer.
3539 *
3540 * Return codes
3541 * @vport - pointer to the virtual N_Port data structure.
3542 * NULL - port create failed.
3543 **/
James Smart2e0fef82007-06-17 19:56:36 -05003544struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04003545lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04003546{
James Smart2e0fef82007-06-17 19:56:36 -05003547 struct lpfc_vport *vport;
James Smart895427b2017-02-12 13:52:30 -08003548 struct Scsi_Host *shost = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003549 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04003550
James Smart895427b2017-02-12 13:52:30 -08003551 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
3552 if (dev != &phba->pcidev->dev) {
3553 shost = scsi_host_alloc(&lpfc_vport_template,
3554 sizeof(struct lpfc_vport));
3555 } else {
3556 if (phba->sli_rev == LPFC_SLI_REV4)
3557 shost = scsi_host_alloc(&lpfc_template,
3558 sizeof(struct lpfc_vport));
3559 else
3560 shost = scsi_host_alloc(&lpfc_template_s3,
3561 sizeof(struct lpfc_vport));
3562 }
3563 } else if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
3564 shost = scsi_host_alloc(&lpfc_template_nvme,
James Smartea4142f2015-04-07 15:07:13 -04003565 sizeof(struct lpfc_vport));
3566 }
James Smart2e0fef82007-06-17 19:56:36 -05003567 if (!shost)
3568 goto out;
James Smart47a86172007-04-25 09:53:22 -04003569
James Smart2e0fef82007-06-17 19:56:36 -05003570 vport = (struct lpfc_vport *) shost->hostdata;
3571 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05003572 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05003573 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05003574 vport->fc_rscn_flush = 0;
James Smart3de2a652007-08-02 11:09:59 -04003575 lpfc_get_vport_cfgparam(vport);
James Smart895427b2017-02-12 13:52:30 -08003576
James Smart2e0fef82007-06-17 19:56:36 -05003577 shost->unique_id = instance;
3578 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04003579 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05003580 shost->this_id = -1;
3581 shost->max_cmd_len = 16;
James Smart8b0dff12015-05-22 10:42:38 -04003582 shost->nr_hw_queues = phba->cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04003583 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart28baac72010-02-12 14:42:03 -05003584 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04003585 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smartda0436e2009-05-22 14:51:39 -04003586 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
3587 }
James Smart81301a92008-12-04 22:39:46 -05003588
James Smart47a86172007-04-25 09:53:22 -04003589 /*
James Smart2e0fef82007-06-17 19:56:36 -05003590 * Set initial can_queue value since 0 is no longer supported and
3591 * scsi_add_host will fail. This will be adjusted later based on the
3592 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04003593 */
James Smart2e0fef82007-06-17 19:56:36 -05003594 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04003595 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05003596 shost->transportt = lpfc_vport_transport_template;
3597 vport->port_type = LPFC_NPIV_PORT;
3598 } else {
3599 shost->transportt = lpfc_transport_template;
3600 vport->port_type = LPFC_PHYSICAL_PORT;
3601 }
James Smart47a86172007-04-25 09:53:22 -04003602
James Smart2e0fef82007-06-17 19:56:36 -05003603 /* Initialize all internally managed lists. */
3604 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04003605 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05003606 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04003607
James Smart2e0fef82007-06-17 19:56:36 -05003608 init_timer(&vport->fc_disctmo);
3609 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05003610 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04003611
James Smart2e0fef82007-06-17 19:56:36 -05003612 init_timer(&vport->els_tmofunc);
3613 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05003614 vport->els_tmofunc.data = (unsigned long)vport;
James Smart92494142011-02-16 12:39:44 -05003615
3616 init_timer(&vport->delayed_disc_tmo);
3617 vport->delayed_disc_tmo.function = lpfc_delayed_disc_tmo;
3618 vport->delayed_disc_tmo.data = (unsigned long)vport;
3619
James Bottomleyd139b9b2009-11-05 13:33:12 -06003620 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05003621 if (error)
3622 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04003623
James Smart549e55c2007-08-02 11:09:51 -04003624 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003625 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04003626 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003627 return vport;
James Smart47a86172007-04-25 09:53:22 -04003628
James Smart2e0fef82007-06-17 19:56:36 -05003629out_put_shost:
3630 scsi_host_put(shost);
3631out:
3632 return NULL;
James Smart47a86172007-04-25 09:53:22 -04003633}
3634
James Smarte59058c2008-08-24 21:49:00 -04003635/**
James Smart3621a712009-04-06 18:47:14 -04003636 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04003637 * @vport: pointer to an lpfc virtual N_Port data structure.
3638 *
3639 * This routine destroys a FC port from the upper layer protocol. All the
3640 * resources associated with the port are released.
3641 **/
James Smart2e0fef82007-06-17 19:56:36 -05003642void
3643destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04003644{
James Smart92d7f7b2007-06-17 19:56:38 -05003645 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3646 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003647
James Smart858c9f62007-06-17 19:56:39 -05003648 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05003649 fc_remove_host(shost);
3650 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04003651
James Smart92d7f7b2007-06-17 19:56:38 -05003652 spin_lock_irq(&phba->hbalock);
3653 list_del_init(&vport->listentry);
3654 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04003655
James Smart92d7f7b2007-06-17 19:56:38 -05003656 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04003657 return;
James Smart47a86172007-04-25 09:53:22 -04003658}
3659
James Smarte59058c2008-08-24 21:49:00 -04003660/**
James Smart3621a712009-04-06 18:47:14 -04003661 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04003662 *
3663 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
3664 * uses the kernel idr facility to perform the task.
3665 *
3666 * Return codes:
3667 * instance - a unique integer ID allocated as the new instance.
3668 * -1 - lpfc get instance failed.
3669 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003670int
3671lpfc_get_instance(void)
3672{
Tejun Heoab516032013-02-27 17:04:44 -08003673 int ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003674
Tejun Heoab516032013-02-27 17:04:44 -08003675 ret = idr_alloc(&lpfc_hba_index, NULL, 0, 0, GFP_KERNEL);
3676 return ret < 0 ? -1 : ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003677}
3678
James Smarte59058c2008-08-24 21:49:00 -04003679/**
James Smart3621a712009-04-06 18:47:14 -04003680 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04003681 * @shost: pointer to SCSI host data structure.
3682 * @time: elapsed time of the scan in jiffies.
3683 *
3684 * This routine is called by the SCSI layer with a SCSI host to determine
3685 * whether the scan host is finished.
3686 *
3687 * Note: there is no scan_start function as adapter initialization will have
3688 * asynchronously kicked off the link initialization.
3689 *
3690 * Return codes
3691 * 0 - SCSI host scan is not over yet.
3692 * 1 - SCSI host scan is over.
3693 **/
James Smart47a86172007-04-25 09:53:22 -04003694int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
3695{
James Smart2e0fef82007-06-17 19:56:36 -05003696 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3697 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05003698 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04003699
James Smart858c9f62007-06-17 19:56:39 -05003700 spin_lock_irq(shost->host_lock);
3701
James Smart51ef4c22007-08-02 11:10:31 -04003702 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05003703 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003704 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05003705 }
James Smart256ec0d2013-04-17 20:14:58 -04003706 if (time >= msecs_to_jiffies(30 * 1000)) {
James Smart2e0fef82007-06-17 19:56:36 -05003707 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003708 "0461 Scanning longer than 30 "
3709 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003710 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003711 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003712 }
James Smart256ec0d2013-04-17 20:14:58 -04003713 if (time >= msecs_to_jiffies(15 * 1000) &&
3714 phba->link_state <= LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -05003715 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003716 "0465 Link down longer than 15 "
3717 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003718 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05003719 goto finished;
James Smart47a86172007-04-25 09:53:22 -04003720 }
3721
James Smart2e0fef82007-06-17 19:56:36 -05003722 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05003723 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003724 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05003725 goto finished;
James Smart256ec0d2013-04-17 20:14:58 -04003726 if (vport->fc_map_cnt == 0 && time < msecs_to_jiffies(2 * 1000))
James Smart858c9f62007-06-17 19:56:39 -05003727 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003728 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05003729 goto finished;
3730
3731 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003732
3733finished:
James Smart858c9f62007-06-17 19:56:39 -05003734 spin_unlock_irq(shost->host_lock);
3735 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05003736}
3737
James Smarte59058c2008-08-24 21:49:00 -04003738/**
James Smart3621a712009-04-06 18:47:14 -04003739 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04003740 * @shost: pointer to SCSI host data structure.
3741 *
3742 * This routine initializes a given SCSI host attributes on a FC port. The
3743 * SCSI host can be either on top of a physical port or a virtual port.
3744 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003745void lpfc_host_attrib_init(struct Scsi_Host *shost)
3746{
3747 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3748 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003749 /*
James Smart2e0fef82007-06-17 19:56:36 -05003750 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04003751 */
3752
James Smart2e0fef82007-06-17 19:56:36 -05003753 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
3754 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04003755 fc_host_supported_classes(shost) = FC_COS_CLASS3;
3756
3757 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003758 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003759 fc_host_supported_fc4s(shost)[2] = 1;
3760 fc_host_supported_fc4s(shost)[7] = 1;
3761
James Smart92d7f7b2007-06-17 19:56:38 -05003762 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
3763 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04003764
3765 fc_host_supported_speeds(shost) = 0;
James Smartd38dd522015-08-31 16:48:17 -04003766 if (phba->lmt & LMT_32Gb)
3767 fc_host_supported_speeds(shost) |= FC_PORTSPEED_32GBIT;
James Smart88a2cfb2011-07-22 18:36:33 -04003768 if (phba->lmt & LMT_16Gb)
3769 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
James Smart47a86172007-04-25 09:53:22 -04003770 if (phba->lmt & LMT_10Gb)
3771 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04003772 if (phba->lmt & LMT_8Gb)
3773 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04003774 if (phba->lmt & LMT_4Gb)
3775 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
3776 if (phba->lmt & LMT_2Gb)
3777 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
3778 if (phba->lmt & LMT_1Gb)
3779 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
3780
3781 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05003782 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
3783 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04003784
Mike Christie0af5d702010-09-15 16:52:31 -05003785 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
3786
James Smart47a86172007-04-25 09:53:22 -04003787 /* This value is also unchanging */
3788 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003789 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003790 fc_host_active_fc4s(shost)[2] = 1;
3791 fc_host_active_fc4s(shost)[7] = 1;
3792
James Smart92d7f7b2007-06-17 19:56:38 -05003793 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04003794 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04003795 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04003796 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04003797}
dea31012005-04-17 16:05:31 -05003798
James Smarte59058c2008-08-24 21:49:00 -04003799/**
James Smartda0436e2009-05-22 14:51:39 -04003800 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04003801 * @phba: pointer to lpfc hba data structure.
3802 *
James Smartda0436e2009-05-22 14:51:39 -04003803 * This routine is invoked to stop an SLI3 device port, it stops the device
3804 * from generating interrupts and stops the device driver's timers for the
3805 * device.
James Smarte59058c2008-08-24 21:49:00 -04003806 **/
James Smartdb2378e2008-02-08 18:49:51 -05003807static void
James Smartda0436e2009-05-22 14:51:39 -04003808lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05003809{
James Smartda0436e2009-05-22 14:51:39 -04003810 /* Clear all interrupt enable conditions */
3811 writel(0, phba->HCregaddr);
3812 readl(phba->HCregaddr); /* flush */
3813 /* Clear all pending interrupts */
3814 writel(0xffffffff, phba->HAregaddr);
3815 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04003816
James Smartda0436e2009-05-22 14:51:39 -04003817 /* Reset some HBA SLI setup states */
3818 lpfc_stop_hba_timers(phba);
3819 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05003820}
3821
James Smarte59058c2008-08-24 21:49:00 -04003822/**
James Smartda0436e2009-05-22 14:51:39 -04003823 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05003824 * @phba: pointer to lpfc hba data structure.
3825 *
James Smartda0436e2009-05-22 14:51:39 -04003826 * This routine is invoked to stop an SLI4 device port, it stops the device
3827 * from generating interrupts and stops the device driver's timers for the
3828 * device.
3829 **/
3830static void
3831lpfc_stop_port_s4(struct lpfc_hba *phba)
3832{
3833 /* Reset some HBA SLI4 setup states */
3834 lpfc_stop_hba_timers(phba);
3835 phba->pport->work_port_events = 0;
3836 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04003837}
3838
3839/**
3840 * lpfc_stop_port - Wrapper function for stopping hba port
3841 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05003842 *
James Smartda0436e2009-05-22 14:51:39 -04003843 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
3844 * the API jump table function pointer from the lpfc_hba struct.
3845 **/
3846void
3847lpfc_stop_port(struct lpfc_hba *phba)
3848{
3849 phba->lpfc_stop_port(phba);
3850}
3851
3852/**
James Smartecfd03c2010-02-12 14:41:27 -05003853 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
3854 * @phba: Pointer to hba for which this call is being executed.
3855 *
3856 * This routine starts the timer waiting for the FCF rediscovery to complete.
3857 **/
3858void
3859lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
3860{
3861 unsigned long fcf_redisc_wait_tmo =
3862 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
3863 /* Start fcf rediscovery wait period timer */
3864 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
3865 spin_lock_irq(&phba->hbalock);
3866 /* Allow action to new fcf asynchronous event */
3867 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
3868 /* Mark the FCF rediscovery pending state */
3869 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
3870 spin_unlock_irq(&phba->hbalock);
3871}
3872
3873/**
3874 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
3875 * @ptr: Map to lpfc_hba data structure pointer.
3876 *
3877 * This routine is invoked when waiting for FCF table rediscover has been
3878 * timed out. If new FCF record(s) has (have) been discovered during the
3879 * wait period, a new FCF event shall be added to the FCOE async event
3880 * list, and then worker thread shall be waked up for processing from the
3881 * worker thread context.
3882 **/
Rashika Kheriae399b222014-09-03 12:55:28 -04003883static void
James Smartecfd03c2010-02-12 14:41:27 -05003884lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
3885{
3886 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
3887
3888 /* Don't send FCF rediscovery event if timer cancelled */
3889 spin_lock_irq(&phba->hbalock);
3890 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
3891 spin_unlock_irq(&phba->hbalock);
3892 return;
3893 }
3894 /* Clear FCF rediscovery timer pending flag */
3895 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
3896 /* FCF rediscovery event to worker thread */
3897 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
3898 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003899 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04003900 "2776 FCF rediscover quiescent timer expired\n");
James Smartecfd03c2010-02-12 14:41:27 -05003901 /* wake up worker thread */
3902 lpfc_worker_wake_up(phba);
3903}
3904
3905/**
James Smartda0436e2009-05-22 14:51:39 -04003906 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
3907 * @phba: pointer to lpfc hba data structure.
3908 * @acqe_link: pointer to the async link completion queue entry.
3909 *
3910 * This routine is to parse the SLI4 link-attention link fault code and
3911 * translate it into the base driver's read link attention mailbox command
3912 * status.
3913 *
3914 * Return: Link-attention status in terms of base driver's coding.
3915 **/
3916static uint16_t
3917lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
3918 struct lpfc_acqe_link *acqe_link)
3919{
3920 uint16_t latt_fault;
3921
3922 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
3923 case LPFC_ASYNC_LINK_FAULT_NONE:
3924 case LPFC_ASYNC_LINK_FAULT_LOCAL:
3925 case LPFC_ASYNC_LINK_FAULT_REMOTE:
3926 latt_fault = 0;
3927 break;
3928 default:
3929 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3930 "0398 Invalid link fault code: x%x\n",
3931 bf_get(lpfc_acqe_link_fault, acqe_link));
3932 latt_fault = MBXERR_ERROR;
3933 break;
3934 }
3935 return latt_fault;
3936}
3937
3938/**
3939 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
3940 * @phba: pointer to lpfc hba data structure.
3941 * @acqe_link: pointer to the async link completion queue entry.
3942 *
3943 * This routine is to parse the SLI4 link attention type and translate it
3944 * into the base driver's link attention type coding.
3945 *
3946 * Return: Link attention type in terms of base driver's coding.
3947 **/
3948static uint8_t
3949lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
3950 struct lpfc_acqe_link *acqe_link)
3951{
3952 uint8_t att_type;
3953
3954 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
3955 case LPFC_ASYNC_LINK_STATUS_DOWN:
3956 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
James Smart76a95d72010-11-20 23:11:48 -05003957 att_type = LPFC_ATT_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04003958 break;
3959 case LPFC_ASYNC_LINK_STATUS_UP:
3960 /* Ignore physical link up events - wait for logical link up */
James Smart76a95d72010-11-20 23:11:48 -05003961 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003962 break;
3963 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
James Smart76a95d72010-11-20 23:11:48 -05003964 att_type = LPFC_ATT_LINK_UP;
James Smartda0436e2009-05-22 14:51:39 -04003965 break;
3966 default:
3967 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3968 "0399 Invalid link attention type: x%x\n",
3969 bf_get(lpfc_acqe_link_status, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003970 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003971 break;
3972 }
3973 return att_type;
3974}
3975
3976/**
James Smart8b68cd52012-09-29 11:32:37 -04003977 * lpfc_sli_port_speed_get - Get sli3 link speed code to link speed
3978 * @phba: pointer to lpfc hba data structure.
3979 *
3980 * This routine is to get an SLI3 FC port's link speed in Mbps.
3981 *
3982 * Return: link speed in terms of Mbps.
3983 **/
3984uint32_t
3985lpfc_sli_port_speed_get(struct lpfc_hba *phba)
3986{
3987 uint32_t link_speed;
3988
3989 if (!lpfc_is_link_up(phba))
3990 return 0;
3991
James Smarta085e872015-12-16 18:12:02 -05003992 if (phba->sli_rev <= LPFC_SLI_REV3) {
3993 switch (phba->fc_linkspeed) {
3994 case LPFC_LINK_SPEED_1GHZ:
3995 link_speed = 1000;
3996 break;
3997 case LPFC_LINK_SPEED_2GHZ:
3998 link_speed = 2000;
3999 break;
4000 case LPFC_LINK_SPEED_4GHZ:
4001 link_speed = 4000;
4002 break;
4003 case LPFC_LINK_SPEED_8GHZ:
4004 link_speed = 8000;
4005 break;
4006 case LPFC_LINK_SPEED_10GHZ:
4007 link_speed = 10000;
4008 break;
4009 case LPFC_LINK_SPEED_16GHZ:
4010 link_speed = 16000;
4011 break;
4012 default:
4013 link_speed = 0;
4014 }
4015 } else {
4016 if (phba->sli4_hba.link_state.logical_speed)
4017 link_speed =
4018 phba->sli4_hba.link_state.logical_speed;
4019 else
4020 link_speed = phba->sli4_hba.link_state.speed;
James Smart8b68cd52012-09-29 11:32:37 -04004021 }
4022 return link_speed;
4023}
4024
4025/**
4026 * lpfc_sli4_port_speed_parse - Parse async evt link speed code to link speed
4027 * @phba: pointer to lpfc hba data structure.
4028 * @evt_code: asynchronous event code.
4029 * @speed_code: asynchronous event link speed code.
4030 *
4031 * This routine is to parse the giving SLI4 async event link speed code into
4032 * value of Mbps for the link speed.
4033 *
4034 * Return: link speed in terms of Mbps.
4035 **/
4036static uint32_t
4037lpfc_sli4_port_speed_parse(struct lpfc_hba *phba, uint32_t evt_code,
4038 uint8_t speed_code)
4039{
4040 uint32_t port_speed;
4041
4042 switch (evt_code) {
4043 case LPFC_TRAILER_CODE_LINK:
4044 switch (speed_code) {
James Smart26d830e2015-04-07 15:07:17 -04004045 case LPFC_ASYNC_LINK_SPEED_ZERO:
James Smart8b68cd52012-09-29 11:32:37 -04004046 port_speed = 0;
4047 break;
James Smart26d830e2015-04-07 15:07:17 -04004048 case LPFC_ASYNC_LINK_SPEED_10MBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004049 port_speed = 10;
4050 break;
James Smart26d830e2015-04-07 15:07:17 -04004051 case LPFC_ASYNC_LINK_SPEED_100MBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004052 port_speed = 100;
4053 break;
James Smart26d830e2015-04-07 15:07:17 -04004054 case LPFC_ASYNC_LINK_SPEED_1GBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004055 port_speed = 1000;
4056 break;
James Smart26d830e2015-04-07 15:07:17 -04004057 case LPFC_ASYNC_LINK_SPEED_10GBPS:
James Smart8b68cd52012-09-29 11:32:37 -04004058 port_speed = 10000;
4059 break;
James Smart26d830e2015-04-07 15:07:17 -04004060 case LPFC_ASYNC_LINK_SPEED_20GBPS:
4061 port_speed = 20000;
4062 break;
4063 case LPFC_ASYNC_LINK_SPEED_25GBPS:
4064 port_speed = 25000;
4065 break;
4066 case LPFC_ASYNC_LINK_SPEED_40GBPS:
4067 port_speed = 40000;
4068 break;
James Smart8b68cd52012-09-29 11:32:37 -04004069 default:
4070 port_speed = 0;
4071 }
4072 break;
4073 case LPFC_TRAILER_CODE_FC:
4074 switch (speed_code) {
James Smart26d830e2015-04-07 15:07:17 -04004075 case LPFC_FC_LA_SPEED_UNKNOWN:
James Smart8b68cd52012-09-29 11:32:37 -04004076 port_speed = 0;
4077 break;
James Smart26d830e2015-04-07 15:07:17 -04004078 case LPFC_FC_LA_SPEED_1G:
James Smart8b68cd52012-09-29 11:32:37 -04004079 port_speed = 1000;
4080 break;
James Smart26d830e2015-04-07 15:07:17 -04004081 case LPFC_FC_LA_SPEED_2G:
James Smart8b68cd52012-09-29 11:32:37 -04004082 port_speed = 2000;
4083 break;
James Smart26d830e2015-04-07 15:07:17 -04004084 case LPFC_FC_LA_SPEED_4G:
James Smart8b68cd52012-09-29 11:32:37 -04004085 port_speed = 4000;
4086 break;
James Smart26d830e2015-04-07 15:07:17 -04004087 case LPFC_FC_LA_SPEED_8G:
James Smart8b68cd52012-09-29 11:32:37 -04004088 port_speed = 8000;
4089 break;
James Smart26d830e2015-04-07 15:07:17 -04004090 case LPFC_FC_LA_SPEED_10G:
James Smart8b68cd52012-09-29 11:32:37 -04004091 port_speed = 10000;
4092 break;
James Smart26d830e2015-04-07 15:07:17 -04004093 case LPFC_FC_LA_SPEED_16G:
James Smart8b68cd52012-09-29 11:32:37 -04004094 port_speed = 16000;
4095 break;
James Smartd38dd522015-08-31 16:48:17 -04004096 case LPFC_FC_LA_SPEED_32G:
4097 port_speed = 32000;
4098 break;
James Smart8b68cd52012-09-29 11:32:37 -04004099 default:
4100 port_speed = 0;
4101 }
4102 break;
4103 default:
4104 port_speed = 0;
4105 }
4106 return port_speed;
4107}
4108
4109/**
James Smart70f3c072010-12-15 17:57:33 -05004110 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
James Smartda0436e2009-05-22 14:51:39 -04004111 * @phba: pointer to lpfc hba data structure.
4112 * @acqe_link: pointer to the async link completion queue entry.
4113 *
James Smart70f3c072010-12-15 17:57:33 -05004114 * This routine is to handle the SLI4 asynchronous FCoE link event.
James Smartda0436e2009-05-22 14:51:39 -04004115 **/
4116static void
4117lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
4118 struct lpfc_acqe_link *acqe_link)
4119{
4120 struct lpfc_dmabuf *mp;
4121 LPFC_MBOXQ_t *pmb;
4122 MAILBOX_t *mb;
James Smart76a95d72010-11-20 23:11:48 -05004123 struct lpfc_mbx_read_top *la;
James Smartda0436e2009-05-22 14:51:39 -04004124 uint8_t att_type;
James Smart76a95d72010-11-20 23:11:48 -05004125 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004126
4127 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05004128 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
James Smartda0436e2009-05-22 14:51:39 -04004129 return;
James Smart32b97932009-07-19 10:01:21 -04004130 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004131 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4132 if (!pmb) {
4133 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4134 "0395 The mboxq allocation failed\n");
4135 return;
4136 }
4137 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4138 if (!mp) {
4139 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4140 "0396 The lpfc_dmabuf allocation failed\n");
4141 goto out_free_pmb;
4142 }
4143 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
4144 if (!mp->virt) {
4145 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4146 "0397 The mbuf allocation failed\n");
4147 goto out_free_dmabuf;
4148 }
4149
4150 /* Cleanup any outstanding ELS commands */
4151 lpfc_els_flush_all_cmd(phba);
4152
4153 /* Block ELS IOCBs until we have done process link event */
James Smart895427b2017-02-12 13:52:30 -08004154 phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
James Smartda0436e2009-05-22 14:51:39 -04004155
4156 /* Update link event statistics */
4157 phba->sli.slistat.link_event++;
4158
James Smart76a95d72010-11-20 23:11:48 -05004159 /* Create lpfc_handle_latt mailbox command from link ACQE */
4160 lpfc_read_topology(phba, pmb, mp);
4161 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smartda0436e2009-05-22 14:51:39 -04004162 pmb->vport = phba->pport;
4163
James Smartda0436e2009-05-22 14:51:39 -04004164 /* Keep the link status for extra SLI4 state machine reference */
4165 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04004166 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_LINK,
4167 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smartda0436e2009-05-22 14:51:39 -04004168 phba->sli4_hba.link_state.duplex =
4169 bf_get(lpfc_acqe_link_duplex, acqe_link);
4170 phba->sli4_hba.link_state.status =
4171 bf_get(lpfc_acqe_link_status, acqe_link);
James Smart70f3c072010-12-15 17:57:33 -05004172 phba->sli4_hba.link_state.type =
4173 bf_get(lpfc_acqe_link_type, acqe_link);
4174 phba->sli4_hba.link_state.number =
4175 bf_get(lpfc_acqe_link_number, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04004176 phba->sli4_hba.link_state.fault =
4177 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05004178 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04004179 bf_get(lpfc_acqe_logical_link_speed, acqe_link) * 10;
4180
James Smart70f3c072010-12-15 17:57:33 -05004181 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartc31098c2011-04-16 11:03:33 -04004182 "2900 Async FC/FCoE Link event - Speed:%dGBit "
4183 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
4184 "Logical speed:%dMbps Fault:%d\n",
James Smart70f3c072010-12-15 17:57:33 -05004185 phba->sli4_hba.link_state.speed,
4186 phba->sli4_hba.link_state.topology,
4187 phba->sli4_hba.link_state.status,
4188 phba->sli4_hba.link_state.type,
4189 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04004190 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05004191 phba->sli4_hba.link_state.fault);
James Smart76a95d72010-11-20 23:11:48 -05004192 /*
4193 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
4194 * topology info. Note: Optional for non FC-AL ports.
4195 */
4196 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
4197 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4198 if (rc == MBX_NOT_FINISHED)
4199 goto out_free_dmabuf;
4200 return;
4201 }
4202 /*
4203 * For FCoE Mode: fill in all the topology information we need and call
4204 * the READ_TOPOLOGY completion routine to continue without actually
4205 * sending the READ_TOPOLOGY mailbox command to the port.
4206 */
4207 /* Parse and translate status field */
4208 mb = &pmb->u.mb;
4209 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
4210
4211 /* Parse and translate link attention fields */
4212 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
4213 la->eventTag = acqe_link->event_tag;
4214 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
4215 bf_set(lpfc_mbx_read_top_link_spd, la,
James Smarta085e872015-12-16 18:12:02 -05004216 (bf_get(lpfc_acqe_link_speed, acqe_link)));
James Smart76a95d72010-11-20 23:11:48 -05004217
4218 /* Fake the the following irrelvant fields */
4219 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
4220 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
4221 bf_set(lpfc_mbx_read_top_il, la, 0);
4222 bf_set(lpfc_mbx_read_top_pb, la, 0);
4223 bf_set(lpfc_mbx_read_top_fa, la, 0);
4224 bf_set(lpfc_mbx_read_top_mm, la, 0);
James Smartda0436e2009-05-22 14:51:39 -04004225
4226 /* Invoke the lpfc_handle_latt mailbox command callback function */
James Smart76a95d72010-11-20 23:11:48 -05004227 lpfc_mbx_cmpl_read_topology(phba, pmb);
James Smartda0436e2009-05-22 14:51:39 -04004228
4229 return;
4230
4231out_free_dmabuf:
4232 kfree(mp);
4233out_free_pmb:
4234 mempool_free(pmb, phba->mbox_mem_pool);
4235}
4236
4237/**
James Smart70f3c072010-12-15 17:57:33 -05004238 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
4239 * @phba: pointer to lpfc hba data structure.
4240 * @acqe_fc: pointer to the async fc completion queue entry.
4241 *
4242 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
4243 * that the event was received and then issue a read_topology mailbox command so
4244 * that the rest of the driver will treat it the same as SLI3.
4245 **/
4246static void
4247lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
4248{
4249 struct lpfc_dmabuf *mp;
4250 LPFC_MBOXQ_t *pmb;
James Smart7bdedb32016-07-06 12:36:00 -07004251 MAILBOX_t *mb;
4252 struct lpfc_mbx_read_top *la;
James Smart70f3c072010-12-15 17:57:33 -05004253 int rc;
4254
4255 if (bf_get(lpfc_trailer_type, acqe_fc) !=
4256 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
4257 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4258 "2895 Non FC link Event detected.(%d)\n",
4259 bf_get(lpfc_trailer_type, acqe_fc));
4260 return;
4261 }
4262 /* Keep the link status for extra SLI4 state machine reference */
4263 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04004264 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
4265 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
James Smart70f3c072010-12-15 17:57:33 -05004266 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
4267 phba->sli4_hba.link_state.topology =
4268 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
4269 phba->sli4_hba.link_state.status =
4270 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
4271 phba->sli4_hba.link_state.type =
4272 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
4273 phba->sli4_hba.link_state.number =
4274 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
4275 phba->sli4_hba.link_state.fault =
4276 bf_get(lpfc_acqe_link_fault, acqe_fc);
4277 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04004278 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
James Smart70f3c072010-12-15 17:57:33 -05004279 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4280 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
4281 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
4282 "%dMbps Fault:%d\n",
4283 phba->sli4_hba.link_state.speed,
4284 phba->sli4_hba.link_state.topology,
4285 phba->sli4_hba.link_state.status,
4286 phba->sli4_hba.link_state.type,
4287 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04004288 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05004289 phba->sli4_hba.link_state.fault);
4290 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4291 if (!pmb) {
4292 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4293 "2897 The mboxq allocation failed\n");
4294 return;
4295 }
4296 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4297 if (!mp) {
4298 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4299 "2898 The lpfc_dmabuf allocation failed\n");
4300 goto out_free_pmb;
4301 }
4302 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
4303 if (!mp->virt) {
4304 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4305 "2899 The mbuf allocation failed\n");
4306 goto out_free_dmabuf;
4307 }
4308
4309 /* Cleanup any outstanding ELS commands */
4310 lpfc_els_flush_all_cmd(phba);
4311
4312 /* Block ELS IOCBs until we have done process link event */
James Smart895427b2017-02-12 13:52:30 -08004313 phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
James Smart70f3c072010-12-15 17:57:33 -05004314
4315 /* Update link event statistics */
4316 phba->sli.slistat.link_event++;
4317
4318 /* Create lpfc_handle_latt mailbox command from link ACQE */
4319 lpfc_read_topology(phba, pmb, mp);
4320 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
4321 pmb->vport = phba->pport;
4322
James Smart7bdedb32016-07-06 12:36:00 -07004323 if (phba->sli4_hba.link_state.status != LPFC_FC_LA_TYPE_LINK_UP) {
4324 /* Parse and translate status field */
4325 mb = &pmb->u.mb;
4326 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba,
4327 (void *)acqe_fc);
4328
4329 /* Parse and translate link attention fields */
4330 la = (struct lpfc_mbx_read_top *)&pmb->u.mb.un.varReadTop;
4331 la->eventTag = acqe_fc->event_tag;
4332 bf_set(lpfc_mbx_read_top_att_type, la,
4333 LPFC_FC_LA_TYPE_LINK_DOWN);
4334
4335 /* Invoke the mailbox command callback function */
4336 lpfc_mbx_cmpl_read_topology(phba, pmb);
4337
4338 return;
4339 }
4340
James Smart70f3c072010-12-15 17:57:33 -05004341 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4342 if (rc == MBX_NOT_FINISHED)
4343 goto out_free_dmabuf;
4344 return;
4345
4346out_free_dmabuf:
4347 kfree(mp);
4348out_free_pmb:
4349 mempool_free(pmb, phba->mbox_mem_pool);
4350}
4351
4352/**
4353 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
4354 * @phba: pointer to lpfc hba data structure.
4355 * @acqe_fc: pointer to the async SLI completion queue entry.
4356 *
4357 * This routine is to handle the SLI4 asynchronous SLI events.
4358 **/
4359static void
4360lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
4361{
James Smart4b8bae02012-06-12 13:55:07 -04004362 char port_name;
James Smart8c1312e2012-10-31 14:45:09 -04004363 char message[128];
James Smart4b8bae02012-06-12 13:55:07 -04004364 uint8_t status;
James Smart946727d2015-04-07 15:07:09 -04004365 uint8_t evt_type;
James Smart448193b2015-12-16 18:12:05 -05004366 uint8_t operational = 0;
James Smart946727d2015-04-07 15:07:09 -04004367 struct temp_event temp_event_data;
James Smart4b8bae02012-06-12 13:55:07 -04004368 struct lpfc_acqe_misconfigured_event *misconfigured;
James Smart946727d2015-04-07 15:07:09 -04004369 struct Scsi_Host *shost;
James Smart4b8bae02012-06-12 13:55:07 -04004370
James Smart946727d2015-04-07 15:07:09 -04004371 evt_type = bf_get(lpfc_trailer_type, acqe_sli);
4372
James Smart448193b2015-12-16 18:12:05 -05004373 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4374 "2901 Async SLI event - Event Data1:x%08x Event Data2:"
4375 "x%08x SLI Event Type:%d\n",
4376 acqe_sli->event_data1, acqe_sli->event_data2,
4377 evt_type);
James Smart4b8bae02012-06-12 13:55:07 -04004378
4379 port_name = phba->Port[0];
4380 if (port_name == 0x00)
4381 port_name = '?'; /* get port name is empty */
4382
James Smart946727d2015-04-07 15:07:09 -04004383 switch (evt_type) {
4384 case LPFC_SLI_EVENT_TYPE_OVER_TEMP:
4385 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
4386 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
4387 temp_event_data.data = (uint32_t)acqe_sli->event_data1;
4388
4389 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4390 "3190 Over Temperature:%d Celsius- Port Name %c\n",
4391 acqe_sli->event_data1, port_name);
4392
James Smart310429e2016-07-06 12:35:54 -07004393 phba->sfp_warning |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
James Smart946727d2015-04-07 15:07:09 -04004394 shost = lpfc_shost_from_vport(phba->pport);
4395 fc_host_post_vendor_event(shost, fc_get_event_number(),
4396 sizeof(temp_event_data),
4397 (char *)&temp_event_data,
4398 SCSI_NL_VID_TYPE_PCI
4399 | PCI_VENDOR_ID_EMULEX);
4400 break;
4401 case LPFC_SLI_EVENT_TYPE_NORM_TEMP:
4402 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
4403 temp_event_data.event_code = LPFC_NORMAL_TEMP;
4404 temp_event_data.data = (uint32_t)acqe_sli->event_data1;
4405
4406 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4407 "3191 Normal Temperature:%d Celsius - Port Name %c\n",
4408 acqe_sli->event_data1, port_name);
4409
4410 shost = lpfc_shost_from_vport(phba->pport);
4411 fc_host_post_vendor_event(shost, fc_get_event_number(),
4412 sizeof(temp_event_data),
4413 (char *)&temp_event_data,
4414 SCSI_NL_VID_TYPE_PCI
4415 | PCI_VENDOR_ID_EMULEX);
4416 break;
4417 case LPFC_SLI_EVENT_TYPE_MISCONFIGURED:
4418 misconfigured = (struct lpfc_acqe_misconfigured_event *)
James Smart4b8bae02012-06-12 13:55:07 -04004419 &acqe_sli->event_data1;
4420
James Smart946727d2015-04-07 15:07:09 -04004421 /* fetch the status for this port */
4422 switch (phba->sli4_hba.lnk_info.lnk_no) {
4423 case LPFC_LINK_NUMBER_0:
James Smart448193b2015-12-16 18:12:05 -05004424 status = bf_get(lpfc_sli_misconfigured_port0_state,
4425 &misconfigured->theEvent);
4426 operational = bf_get(lpfc_sli_misconfigured_port0_op,
James Smart4b8bae02012-06-12 13:55:07 -04004427 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04004428 break;
4429 case LPFC_LINK_NUMBER_1:
James Smart448193b2015-12-16 18:12:05 -05004430 status = bf_get(lpfc_sli_misconfigured_port1_state,
4431 &misconfigured->theEvent);
4432 operational = bf_get(lpfc_sli_misconfigured_port1_op,
James Smart4b8bae02012-06-12 13:55:07 -04004433 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04004434 break;
4435 case LPFC_LINK_NUMBER_2:
James Smart448193b2015-12-16 18:12:05 -05004436 status = bf_get(lpfc_sli_misconfigured_port2_state,
4437 &misconfigured->theEvent);
4438 operational = bf_get(lpfc_sli_misconfigured_port2_op,
James Smart4b8bae02012-06-12 13:55:07 -04004439 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04004440 break;
4441 case LPFC_LINK_NUMBER_3:
James Smart448193b2015-12-16 18:12:05 -05004442 status = bf_get(lpfc_sli_misconfigured_port3_state,
4443 &misconfigured->theEvent);
4444 operational = bf_get(lpfc_sli_misconfigured_port3_op,
James Smart4b8bae02012-06-12 13:55:07 -04004445 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04004446 break;
4447 default:
James Smart448193b2015-12-16 18:12:05 -05004448 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4449 "3296 "
4450 "LPFC_SLI_EVENT_TYPE_MISCONFIGURED "
4451 "event: Invalid link %d",
4452 phba->sli4_hba.lnk_info.lnk_no);
4453 return;
James Smart946727d2015-04-07 15:07:09 -04004454 }
James Smart4b8bae02012-06-12 13:55:07 -04004455
James Smart448193b2015-12-16 18:12:05 -05004456 /* Skip if optic state unchanged */
4457 if (phba->sli4_hba.lnk_info.optic_state == status)
4458 return;
4459
James Smart946727d2015-04-07 15:07:09 -04004460 switch (status) {
4461 case LPFC_SLI_EVENT_STATUS_VALID:
James Smart448193b2015-12-16 18:12:05 -05004462 sprintf(message, "Physical Link is functional");
4463 break;
James Smart946727d2015-04-07 15:07:09 -04004464 case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
4465 sprintf(message, "Optics faulted/incorrectly "
4466 "installed/not installed - Reseat optics, "
4467 "if issue not resolved, replace.");
4468 break;
4469 case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
4470 sprintf(message,
4471 "Optics of two types installed - Remove one "
4472 "optic or install matching pair of optics.");
4473 break;
4474 case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
4475 sprintf(message, "Incompatible optics - Replace with "
James Smart292098b2012-09-29 11:31:41 -04004476 "compatible optics for card to function.");
James Smart946727d2015-04-07 15:07:09 -04004477 break;
James Smart448193b2015-12-16 18:12:05 -05004478 case LPFC_SLI_EVENT_STATUS_UNQUALIFIED:
4479 sprintf(message, "Unqualified optics - Replace with "
4480 "Avago optics for Warranty and Technical "
4481 "Support - Link is%s operational",
James Smart2ea259e2017-02-12 13:52:27 -08004482 (operational) ? " not" : "");
James Smart448193b2015-12-16 18:12:05 -05004483 break;
4484 case LPFC_SLI_EVENT_STATUS_UNCERTIFIED:
4485 sprintf(message, "Uncertified optics - Replace with "
4486 "Avago-certified optics to enable link "
4487 "operation - Link is%s operational",
James Smart2ea259e2017-02-12 13:52:27 -08004488 (operational) ? " not" : "");
James Smart448193b2015-12-16 18:12:05 -05004489 break;
James Smart946727d2015-04-07 15:07:09 -04004490 default:
4491 /* firmware is reporting a status we don't know about */
4492 sprintf(message, "Unknown event status x%02x", status);
4493 break;
4494 }
James Smart448193b2015-12-16 18:12:05 -05004495 phba->sli4_hba.lnk_info.optic_state = status;
James Smart946727d2015-04-07 15:07:09 -04004496 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart448193b2015-12-16 18:12:05 -05004497 "3176 Port Name %c %s\n", port_name, message);
James Smart946727d2015-04-07 15:07:09 -04004498 break;
4499 case LPFC_SLI_EVENT_TYPE_REMOTE_DPORT:
4500 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4501 "3192 Remote DPort Test Initiated - "
4502 "Event Data1:x%08x Event Data2: x%08x\n",
4503 acqe_sli->event_data1, acqe_sli->event_data2);
James Smart4b8bae02012-06-12 13:55:07 -04004504 break;
4505 default:
James Smart946727d2015-04-07 15:07:09 -04004506 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4507 "3193 Async SLI event - Event Data1:x%08x Event Data2:"
4508 "x%08x SLI Event Type:%d\n",
4509 acqe_sli->event_data1, acqe_sli->event_data2,
4510 evt_type);
James Smart4b8bae02012-06-12 13:55:07 -04004511 break;
4512 }
James Smart70f3c072010-12-15 17:57:33 -05004513}
4514
4515/**
James Smartfc2b9892010-02-26 14:15:29 -05004516 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
4517 * @vport: pointer to vport data structure.
4518 *
4519 * This routine is to perform Clear Virtual Link (CVL) on a vport in
4520 * response to a CVL event.
4521 *
4522 * Return the pointer to the ndlp with the vport if successful, otherwise
4523 * return NULL.
4524 **/
4525static struct lpfc_nodelist *
4526lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
4527{
4528 struct lpfc_nodelist *ndlp;
4529 struct Scsi_Host *shost;
4530 struct lpfc_hba *phba;
4531
4532 if (!vport)
4533 return NULL;
James Smartfc2b9892010-02-26 14:15:29 -05004534 phba = vport->phba;
4535 if (!phba)
4536 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04004537 ndlp = lpfc_findnode_did(vport, Fabric_DID);
4538 if (!ndlp) {
4539 /* Cannot find existing Fabric ndlp, so allocate a new one */
4540 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
4541 if (!ndlp)
4542 return 0;
4543 lpfc_nlp_init(vport, ndlp, Fabric_DID);
4544 /* Set the node type */
4545 ndlp->nlp_type |= NLP_FABRIC;
4546 /* Put ndlp onto node list */
4547 lpfc_enqueue_node(vport, ndlp);
4548 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
4549 /* re-setup ndlp without removing from node list */
4550 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
4551 if (!ndlp)
4552 return 0;
4553 }
James Smart63e801c2010-11-20 23:14:19 -05004554 if ((phba->pport->port_state < LPFC_FLOGI) &&
4555 (phba->pport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05004556 return NULL;
4557 /* If virtual link is not yet instantiated ignore CVL */
James Smart63e801c2010-11-20 23:14:19 -05004558 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
4559 && (vport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05004560 return NULL;
4561 shost = lpfc_shost_from_vport(vport);
4562 if (!shost)
4563 return NULL;
4564 lpfc_linkdown_port(vport);
4565 lpfc_cleanup_pending_mbox(vport);
4566 spin_lock_irq(shost->host_lock);
4567 vport->fc_flag |= FC_VPORT_CVL_RCVD;
4568 spin_unlock_irq(shost->host_lock);
4569
4570 return ndlp;
4571}
4572
4573/**
4574 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
4575 * @vport: pointer to lpfc hba data structure.
4576 *
4577 * This routine is to perform Clear Virtual Link (CVL) on all vports in
4578 * response to a FCF dead event.
4579 **/
4580static void
4581lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
4582{
4583 struct lpfc_vport **vports;
4584 int i;
4585
4586 vports = lpfc_create_vport_work_array(phba);
4587 if (vports)
4588 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
4589 lpfc_sli4_perform_vport_cvl(vports[i]);
4590 lpfc_destroy_vport_work_array(phba, vports);
4591}
4592
4593/**
James Smart76a95d72010-11-20 23:11:48 -05004594 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
James Smartda0436e2009-05-22 14:51:39 -04004595 * @phba: pointer to lpfc hba data structure.
4596 * @acqe_link: pointer to the async fcoe completion queue entry.
4597 *
4598 * This routine is to handle the SLI4 asynchronous fcoe event.
4599 **/
4600static void
James Smart76a95d72010-11-20 23:11:48 -05004601lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
James Smart70f3c072010-12-15 17:57:33 -05004602 struct lpfc_acqe_fip *acqe_fip)
James Smartda0436e2009-05-22 14:51:39 -04004603{
James Smart70f3c072010-12-15 17:57:33 -05004604 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004605 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04004606 struct lpfc_vport *vport;
4607 struct lpfc_nodelist *ndlp;
4608 struct Scsi_Host *shost;
James Smart695a8142010-01-26 23:08:03 -05004609 int active_vlink_present;
4610 struct lpfc_vport **vports;
4611 int i;
James Smartda0436e2009-05-22 14:51:39 -04004612
James Smart70f3c072010-12-15 17:57:33 -05004613 phba->fc_eventTag = acqe_fip->event_tag;
4614 phba->fcoe_eventtag = acqe_fip->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004615 switch (event_type) {
James Smart70f3c072010-12-15 17:57:33 -05004616 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
4617 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
4618 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
James Smart999d8132010-03-15 11:24:56 -04004619 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4620 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004621 "2546 New FCF event, evt_tag:x%x, "
4622 "index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004623 acqe_fip->event_tag,
4624 acqe_fip->index);
James Smart999d8132010-03-15 11:24:56 -04004625 else
4626 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
4627 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004628 "2788 FCF param modified event, "
4629 "evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004630 acqe_fip->event_tag,
4631 acqe_fip->index);
James Smart38b92ef2010-08-04 16:11:39 -04004632 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004633 /*
4634 * During period of FCF discovery, read the FCF
4635 * table record indexed by the event to update
James Smarta93ff372010-10-22 11:06:08 -04004636 * FCF roundrobin failover eligible FCF bmask.
James Smart0c9ab6f2010-02-26 14:15:57 -05004637 */
4638 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4639 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004640 "2779 Read FCF (x%x) for updating "
4641 "roundrobin FCF failover bmask\n",
James Smart70f3c072010-12-15 17:57:33 -05004642 acqe_fip->index);
4643 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05004644 }
James Smart38b92ef2010-08-04 16:11:39 -04004645
4646 /* If the FCF discovery is in progress, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04004647 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04004648 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart38b92ef2010-08-04 16:11:39 -04004649 spin_unlock_irq(&phba->hbalock);
4650 break;
4651 }
4652 /* If fast FCF failover rescan event is pending, do nothing */
4653 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
4654 spin_unlock_irq(&phba->hbalock);
4655 break;
4656 }
4657
James Smartc2b97122013-05-31 17:05:36 -04004658 /* If the FCF has been in discovered state, do nothing. */
4659 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
James Smart3804dc82010-07-14 15:31:37 -04004660 spin_unlock_irq(&phba->hbalock);
4661 break;
4662 }
4663 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04004664
James Smart0c9ab6f2010-02-26 14:15:57 -05004665 /* Otherwise, scan the entire FCF table and re-discover SAN */
4666 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004667 "2770 Start FCF table scan per async FCF "
4668 "event, evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004669 acqe_fip->event_tag, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05004670 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
4671 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04004672 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05004673 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4674 "2547 Issue FCF scan read FCF mailbox "
James Smarta93ff372010-10-22 11:06:08 -04004675 "command failed (x%x)\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004676 break;
4677
James Smart70f3c072010-12-15 17:57:33 -05004678 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
James Smartda0436e2009-05-22 14:51:39 -04004679 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04004680 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004681 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
4682 acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04004683 break;
4684
James Smart70f3c072010-12-15 17:57:33 -05004685 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
James Smart80c17842012-03-01 22:35:45 -05004686 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05004687 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004688 "2549 FCF (x%x) disconnected from network, "
James Smart70f3c072010-12-15 17:57:33 -05004689 "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart38b92ef2010-08-04 16:11:39 -04004690 /*
4691 * If we are in the middle of FCF failover process, clear
4692 * the corresponding FCF bit in the roundrobin bitmap.
James Smartda0436e2009-05-22 14:51:39 -04004693 */
James Smartfc2b9892010-02-26 14:15:29 -05004694 spin_lock_irq(&phba->hbalock);
James Smarta1cadfe2016-07-06 12:36:02 -07004695 if ((phba->fcf.fcf_flag & FCF_DISCOVERY) &&
4696 (phba->fcf.current_rec.fcf_indx != acqe_fip->index)) {
James Smartfc2b9892010-02-26 14:15:29 -05004697 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004698 /* Update FLOGI FCF failover eligible FCF bmask */
James Smart70f3c072010-12-15 17:57:33 -05004699 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004700 break;
4701 }
James Smart38b92ef2010-08-04 16:11:39 -04004702 spin_unlock_irq(&phba->hbalock);
4703
4704 /* If the event is not for currently used fcf do nothing */
James Smart70f3c072010-12-15 17:57:33 -05004705 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
James Smart38b92ef2010-08-04 16:11:39 -04004706 break;
4707
4708 /*
4709 * Otherwise, request the port to rediscover the entire FCF
4710 * table for a fast recovery from case that the current FCF
4711 * is no longer valid as we are not in the middle of FCF
4712 * failover process already.
4713 */
James Smartc2b97122013-05-31 17:05:36 -04004714 spin_lock_irq(&phba->hbalock);
4715 /* Mark the fast failover process in progress */
4716 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
4717 spin_unlock_irq(&phba->hbalock);
4718
4719 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
4720 "2771 Start FCF fast failover process due to "
4721 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
4722 "\n", acqe_fip->event_tag, acqe_fip->index);
4723 rc = lpfc_sli4_redisc_fcf_table(phba);
4724 if (rc) {
4725 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4726 LOG_DISCOVERY,
4727 "2772 Issue FCF rediscover mabilbox "
4728 "command failed, fail through to FCF "
4729 "dead event\n");
4730 spin_lock_irq(&phba->hbalock);
4731 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
4732 spin_unlock_irq(&phba->hbalock);
4733 /*
4734 * Last resort will fail over by treating this
4735 * as a link down to FCF registration.
4736 */
4737 lpfc_sli4_fcf_dead_failthrough(phba);
4738 } else {
4739 /* Reset FCF roundrobin bmask for new discovery */
4740 lpfc_sli4_clear_fcf_rr_bmask(phba);
4741 /*
4742 * Handling fast FCF failover to a DEAD FCF event is
4743 * considered equalivant to receiving CVL to all vports.
4744 */
4745 lpfc_sli4_perform_all_vport_cvl(phba);
4746 }
James Smartda0436e2009-05-22 14:51:39 -04004747 break;
James Smart70f3c072010-12-15 17:57:33 -05004748 case LPFC_FIP_EVENT_TYPE_CVL:
James Smart80c17842012-03-01 22:35:45 -05004749 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05004750 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6669f9b2009-10-02 15:16:45 -04004751 "2718 Clear Virtual Link Received for VPI 0x%x"
James Smart70f3c072010-12-15 17:57:33 -05004752 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart6d368e52011-05-24 11:44:12 -04004753
James Smart6669f9b2009-10-02 15:16:45 -04004754 vport = lpfc_find_vport_by_vpid(phba,
James Smart5248a742011-07-22 18:37:06 -04004755 acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004756 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04004757 if (!ndlp)
4758 break;
James Smart695a8142010-01-26 23:08:03 -05004759 active_vlink_present = 0;
4760
4761 vports = lpfc_create_vport_work_array(phba);
4762 if (vports) {
4763 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
4764 i++) {
4765 if ((!(vports[i]->fc_flag &
4766 FC_VPORT_CVL_RCVD)) &&
4767 (vports[i]->port_state > LPFC_FDISC)) {
4768 active_vlink_present = 1;
4769 break;
4770 }
4771 }
4772 lpfc_destroy_vport_work_array(phba, vports);
4773 }
4774
James Smartcc823552015-05-21 13:55:26 -04004775 /*
4776 * Don't re-instantiate if vport is marked for deletion.
4777 * If we are here first then vport_delete is going to wait
4778 * for discovery to complete.
4779 */
4780 if (!(vport->load_flag & FC_UNLOADING) &&
4781 active_vlink_present) {
James Smart695a8142010-01-26 23:08:03 -05004782 /*
4783 * If there are other active VLinks present,
4784 * re-instantiate the Vlink using FDISC.
4785 */
James Smart256ec0d2013-04-17 20:14:58 -04004786 mod_timer(&ndlp->nlp_delayfunc,
4787 jiffies + msecs_to_jiffies(1000));
James Smartfc2b9892010-02-26 14:15:29 -05004788 shost = lpfc_shost_from_vport(vport);
James Smart6669f9b2009-10-02 15:16:45 -04004789 spin_lock_irq(shost->host_lock);
4790 ndlp->nlp_flag |= NLP_DELAY_TMO;
4791 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05004792 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
4793 vport->port_state = LPFC_FDISC;
4794 } else {
James Smartecfd03c2010-02-12 14:41:27 -05004795 /*
4796 * Otherwise, we request port to rediscover
4797 * the entire FCF table for a fast recovery
4798 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05004799 * is no longer valid if we are not already
4800 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05004801 */
James Smartfc2b9892010-02-26 14:15:29 -05004802 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004803 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05004804 spin_unlock_irq(&phba->hbalock);
4805 break;
4806 }
4807 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05004808 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004809 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004810 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4811 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004812 "2773 Start FCF failover per CVL, "
James Smart70f3c072010-12-15 17:57:33 -05004813 "evt_tag:x%x\n", acqe_fip->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05004814 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05004815 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004816 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4817 LOG_DISCOVERY,
4818 "2774 Issue FCF rediscover "
4819 "mabilbox command failed, "
4820 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05004821 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004822 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004823 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05004824 /*
4825 * Last resort will be re-try on the
4826 * the current registered FCF entry.
4827 */
4828 lpfc_retry_pport_discovery(phba);
James Smart38b92ef2010-08-04 16:11:39 -04004829 } else
4830 /*
4831 * Reset FCF roundrobin bmask for new
4832 * discovery.
4833 */
James Smart7d791df2011-07-22 18:37:52 -04004834 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart6669f9b2009-10-02 15:16:45 -04004835 }
4836 break;
James Smartda0436e2009-05-22 14:51:39 -04004837 default:
4838 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4839 "0288 Unknown FCoE event type 0x%x event tag "
James Smart70f3c072010-12-15 17:57:33 -05004840 "0x%x\n", event_type, acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04004841 break;
4842 }
4843}
4844
4845/**
4846 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
4847 * @phba: pointer to lpfc hba data structure.
4848 * @acqe_link: pointer to the async dcbx completion queue entry.
4849 *
4850 * This routine is to handle the SLI4 asynchronous dcbx event.
4851 **/
4852static void
4853lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
4854 struct lpfc_acqe_dcbx *acqe_dcbx)
4855{
James Smart4d9ab992009-10-02 15:16:39 -04004856 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004857 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4858 "0290 The SLI4 DCBX asynchronous event is not "
4859 "handled yet\n");
4860}
4861
4862/**
James Smartb19a0612010-04-06 14:48:51 -04004863 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
4864 * @phba: pointer to lpfc hba data structure.
4865 * @acqe_link: pointer to the async grp5 completion queue entry.
4866 *
4867 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
4868 * is an asynchronous notified of a logical link speed change. The Port
4869 * reports the logical link speed in units of 10Mbps.
4870 **/
4871static void
4872lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
4873 struct lpfc_acqe_grp5 *acqe_grp5)
4874{
4875 uint16_t prev_ll_spd;
4876
4877 phba->fc_eventTag = acqe_grp5->event_tag;
4878 phba->fcoe_eventtag = acqe_grp5->event_tag;
4879 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
4880 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04004881 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5)) * 10;
James Smartb19a0612010-04-06 14:48:51 -04004882 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4883 "2789 GRP5 Async Event: Updating logical link speed "
James Smart8b68cd52012-09-29 11:32:37 -04004884 "from %dMbps to %dMbps\n", prev_ll_spd,
4885 phba->sli4_hba.link_state.logical_speed);
James Smartb19a0612010-04-06 14:48:51 -04004886}
4887
4888/**
James Smartda0436e2009-05-22 14:51:39 -04004889 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
4890 * @phba: pointer to lpfc hba data structure.
4891 *
4892 * This routine is invoked by the worker thread to process all the pending
4893 * SLI4 asynchronous events.
4894 **/
4895void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
4896{
4897 struct lpfc_cq_event *cq_event;
4898
4899 /* First, declare the async event has been handled */
4900 spin_lock_irq(&phba->hbalock);
4901 phba->hba_flag &= ~ASYNC_EVENT;
4902 spin_unlock_irq(&phba->hbalock);
4903 /* Now, handle all the async events */
4904 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
4905 /* Get the first event from the head of the event queue */
4906 spin_lock_irq(&phba->hbalock);
4907 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
4908 cq_event, struct lpfc_cq_event, list);
4909 spin_unlock_irq(&phba->hbalock);
4910 /* Process the asynchronous event */
4911 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
4912 case LPFC_TRAILER_CODE_LINK:
4913 lpfc_sli4_async_link_evt(phba,
4914 &cq_event->cqe.acqe_link);
4915 break;
4916 case LPFC_TRAILER_CODE_FCOE:
James Smart70f3c072010-12-15 17:57:33 -05004917 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004918 break;
4919 case LPFC_TRAILER_CODE_DCBX:
4920 lpfc_sli4_async_dcbx_evt(phba,
4921 &cq_event->cqe.acqe_dcbx);
4922 break;
James Smartb19a0612010-04-06 14:48:51 -04004923 case LPFC_TRAILER_CODE_GRP5:
4924 lpfc_sli4_async_grp5_evt(phba,
4925 &cq_event->cqe.acqe_grp5);
4926 break;
James Smart70f3c072010-12-15 17:57:33 -05004927 case LPFC_TRAILER_CODE_FC:
4928 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
4929 break;
4930 case LPFC_TRAILER_CODE_SLI:
4931 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
4932 break;
James Smartda0436e2009-05-22 14:51:39 -04004933 default:
4934 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4935 "1804 Invalid asynchrous event code: "
4936 "x%x\n", bf_get(lpfc_trailer_code,
4937 &cq_event->cqe.mcqe_cmpl));
4938 break;
4939 }
4940 /* Free the completion event processed to the free pool */
4941 lpfc_sli4_cq_event_release(phba, cq_event);
4942 }
4943}
4944
4945/**
James Smartecfd03c2010-02-12 14:41:27 -05004946 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
4947 * @phba: pointer to lpfc hba data structure.
4948 *
4949 * This routine is invoked by the worker thread to process FCF table
4950 * rediscovery pending completion event.
4951 **/
4952void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
4953{
4954 int rc;
4955
4956 spin_lock_irq(&phba->hbalock);
4957 /* Clear FCF rediscovery timeout event */
4958 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
4959 /* Clear driver fast failover FCF record flag */
4960 phba->fcf.failover_rec.flag = 0;
4961 /* Set state for FCF fast failover */
4962 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
4963 spin_unlock_irq(&phba->hbalock);
4964
4965 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05004966 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004967 "2777 Start post-quiescent FCF table scan\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05004968 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05004969 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05004970 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4971 "2747 Issue FCF scan read FCF mailbox "
4972 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05004973}
4974
4975/**
James Smartda0436e2009-05-22 14:51:39 -04004976 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
4977 * @phba: pointer to lpfc hba data structure.
4978 * @dev_grp: The HBA PCI-Device group number.
4979 *
4980 * This routine is invoked to set up the per HBA PCI-Device group function
4981 * API jump table entries.
4982 *
4983 * Return: 0 if success, otherwise -ENODEV
4984 **/
4985int
4986lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05004987{
4988 int rc;
4989
James Smartda0436e2009-05-22 14:51:39 -04004990 /* Set up lpfc PCI-device group */
4991 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05004992
James Smartda0436e2009-05-22 14:51:39 -04004993 /* The LPFC_PCI_DEV_OC uses SLI4 */
4994 if (dev_grp == LPFC_PCI_DEV_OC)
4995 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05004996
James Smartda0436e2009-05-22 14:51:39 -04004997 /* Set up device INIT API function jump table */
4998 rc = lpfc_init_api_table_setup(phba, dev_grp);
4999 if (rc)
5000 return -ENODEV;
5001 /* Set up SCSI API function jump table */
5002 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
5003 if (rc)
5004 return -ENODEV;
5005 /* Set up SLI API function jump table */
5006 rc = lpfc_sli_api_table_setup(phba, dev_grp);
5007 if (rc)
5008 return -ENODEV;
5009 /* Set up MBOX API function jump table */
5010 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
5011 if (rc)
5012 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05005013
James Smartda0436e2009-05-22 14:51:39 -04005014 return 0;
James Smart5b75da22008-12-04 22:39:35 -05005015}
5016
5017/**
James Smart3621a712009-04-06 18:47:14 -04005018 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05005019 * @phba: pointer to lpfc hba data structure.
5020 * @intr_mode: active interrupt mode adopted.
5021 *
5022 * This routine it invoked to log the currently used active interrupt mode
5023 * to the device.
James Smart3772a992009-05-22 14:50:54 -04005024 **/
5025static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05005026{
5027 switch (intr_mode) {
5028 case 0:
5029 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5030 "0470 Enable INTx interrupt mode.\n");
5031 break;
5032 case 1:
5033 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5034 "0481 Enabled MSI interrupt mode.\n");
5035 break;
5036 case 2:
5037 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5038 "0480 Enabled MSI-X interrupt mode.\n");
5039 break;
5040 default:
5041 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5042 "0482 Illegal interrupt mode.\n");
5043 break;
5044 }
5045 return;
5046}
5047
James Smart5b75da22008-12-04 22:39:35 -05005048/**
James Smart3772a992009-05-22 14:50:54 -04005049 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05005050 * @phba: pointer to lpfc hba data structure.
5051 *
James Smart3772a992009-05-22 14:50:54 -04005052 * This routine is invoked to enable the PCI device that is common to all
5053 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05005054 *
5055 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005056 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005057 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05005058 **/
James Smart3772a992009-05-22 14:50:54 -04005059static int
5060lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05005061{
James Smart3772a992009-05-22 14:50:54 -04005062 struct pci_dev *pdev;
James Smart5b75da22008-12-04 22:39:35 -05005063
James Smart3772a992009-05-22 14:50:54 -04005064 /* Obtain PCI device reference */
5065 if (!phba->pcidev)
5066 goto out_error;
5067 else
5068 pdev = phba->pcidev;
James Smart3772a992009-05-22 14:50:54 -04005069 /* Enable PCI device */
5070 if (pci_enable_device_mem(pdev))
5071 goto out_error;
5072 /* Request PCI resource for the device */
Johannes Thumshirne0c04832016-06-07 09:44:03 +02005073 if (pci_request_mem_regions(pdev, LPFC_DRIVER_NAME))
James Smart3772a992009-05-22 14:50:54 -04005074 goto out_disable_device;
5075 /* Set up device as PCI master and save state for EEH */
5076 pci_set_master(pdev);
5077 pci_try_set_mwi(pdev);
5078 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05005079
James Smart05580562011-05-24 11:40:48 -04005080 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
Jon Mason453193e2013-09-11 15:38:13 -07005081 if (pci_is_pcie(pdev))
James Smart05580562011-05-24 11:40:48 -04005082 pdev->needs_freset = 1;
5083
James Smart3772a992009-05-22 14:50:54 -04005084 return 0;
James Smart5b75da22008-12-04 22:39:35 -05005085
James Smart3772a992009-05-22 14:50:54 -04005086out_disable_device:
5087 pci_disable_device(pdev);
5088out_error:
James Smart079b5c92011-08-21 21:48:49 -04005089 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
Johannes Thumshirne0c04832016-06-07 09:44:03 +02005090 "1401 Failed to enable pci device\n");
James Smart3772a992009-05-22 14:50:54 -04005091 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05005092}
5093
5094/**
James Smart3772a992009-05-22 14:50:54 -04005095 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05005096 * @phba: pointer to lpfc hba data structure.
5097 *
James Smart3772a992009-05-22 14:50:54 -04005098 * This routine is invoked to disable the PCI device that is common to all
5099 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05005100 **/
5101static void
James Smart3772a992009-05-22 14:50:54 -04005102lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05005103{
James Smart3772a992009-05-22 14:50:54 -04005104 struct pci_dev *pdev;
James Smart5b75da22008-12-04 22:39:35 -05005105
James Smart3772a992009-05-22 14:50:54 -04005106 /* Obtain PCI device reference */
5107 if (!phba->pcidev)
5108 return;
5109 else
5110 pdev = phba->pcidev;
James Smart3772a992009-05-22 14:50:54 -04005111 /* Release PCI resource and disable PCI device */
Johannes Thumshirne0c04832016-06-07 09:44:03 +02005112 pci_release_mem_regions(pdev);
James Smart3772a992009-05-22 14:50:54 -04005113 pci_disable_device(pdev);
James Smart5b75da22008-12-04 22:39:35 -05005114
5115 return;
5116}
5117
5118/**
James Smart3772a992009-05-22 14:50:54 -04005119 * lpfc_reset_hba - Reset a hba
5120 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04005121 *
James Smart3772a992009-05-22 14:50:54 -04005122 * This routine is invoked to reset a hba device. It brings the HBA
5123 * offline, performs a board restart, and then brings the board back
5124 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
5125 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04005126 **/
James Smart3772a992009-05-22 14:50:54 -04005127void
5128lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05005129{
James Smart3772a992009-05-22 14:50:54 -04005130 /* If resets are disabled then set error state and return. */
5131 if (!phba->cfg_enable_hba_reset) {
5132 phba->link_state = LPFC_HBA_ERROR;
5133 return;
5134 }
James Smartee620212014-04-04 13:51:53 -04005135 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
5136 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
5137 else
5138 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart3772a992009-05-22 14:50:54 -04005139 lpfc_offline(phba);
5140 lpfc_sli_brdrestart(phba);
5141 lpfc_online(phba);
5142 lpfc_unblock_mgmt_io(phba);
5143}
dea31012005-04-17 16:05:31 -05005144
James Smart3772a992009-05-22 14:50:54 -04005145/**
James Smart0a96e972011-07-22 18:37:28 -04005146 * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
5147 * @phba: pointer to lpfc hba data structure.
5148 *
5149 * This function enables the PCI SR-IOV virtual functions to a physical
5150 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
5151 * enable the number of virtual functions to the physical function. As
5152 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
5153 * API call does not considered as an error condition for most of the device.
5154 **/
5155uint16_t
5156lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
5157{
5158 struct pci_dev *pdev = phba->pcidev;
5159 uint16_t nr_virtfn;
5160 int pos;
5161
James Smart0a96e972011-07-22 18:37:28 -04005162 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
5163 if (pos == 0)
5164 return 0;
5165
5166 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
5167 return nr_virtfn;
5168}
5169
5170/**
James Smart912e3ac2011-05-24 11:42:11 -04005171 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
5172 * @phba: pointer to lpfc hba data structure.
5173 * @nr_vfn: number of virtual functions to be enabled.
5174 *
5175 * This function enables the PCI SR-IOV virtual functions to a physical
5176 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
5177 * enable the number of virtual functions to the physical function. As
5178 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
5179 * API call does not considered as an error condition for most of the device.
5180 **/
5181int
5182lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
5183{
5184 struct pci_dev *pdev = phba->pcidev;
James Smart0a96e972011-07-22 18:37:28 -04005185 uint16_t max_nr_vfn;
James Smart912e3ac2011-05-24 11:42:11 -04005186 int rc;
5187
James Smart0a96e972011-07-22 18:37:28 -04005188 max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
5189 if (nr_vfn > max_nr_vfn) {
5190 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5191 "3057 Requested vfs (%d) greater than "
5192 "supported vfs (%d)", nr_vfn, max_nr_vfn);
5193 return -EINVAL;
5194 }
5195
James Smart912e3ac2011-05-24 11:42:11 -04005196 rc = pci_enable_sriov(pdev, nr_vfn);
5197 if (rc) {
5198 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5199 "2806 Failed to enable sriov on this device "
5200 "with vfn number nr_vf:%d, rc:%d\n",
5201 nr_vfn, rc);
5202 } else
5203 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5204 "2807 Successful enable sriov on this device "
5205 "with vfn number nr_vf:%d\n", nr_vfn);
5206 return rc;
5207}
5208
5209/**
James Smart895427b2017-02-12 13:52:30 -08005210 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
James Smart3772a992009-05-22 14:50:54 -04005211 * @phba: pointer to lpfc hba data structure.
5212 *
James Smart895427b2017-02-12 13:52:30 -08005213 * This routine is invoked to set up the driver internal resources before the
5214 * device specific resource setup to support the HBA device it attached to.
James Smart3772a992009-05-22 14:50:54 -04005215 *
5216 * Return codes
James Smart895427b2017-02-12 13:52:30 -08005217 * 0 - successful
5218 * other values - error
James Smart3772a992009-05-22 14:50:54 -04005219 **/
5220static int
James Smart895427b2017-02-12 13:52:30 -08005221lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
James Smart3772a992009-05-22 14:50:54 -04005222{
James Smart895427b2017-02-12 13:52:30 -08005223 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05005224
James Smart3772a992009-05-22 14:50:54 -04005225 /*
James Smart895427b2017-02-12 13:52:30 -08005226 * Driver resources common to all SLI revisions
James Smart3772a992009-05-22 14:50:54 -04005227 */
James Smart895427b2017-02-12 13:52:30 -08005228 atomic_set(&phba->fast_event_count, 0);
5229 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005230
James Smart895427b2017-02-12 13:52:30 -08005231 /* Initialize ndlp management spinlock */
5232 spin_lock_init(&phba->ndlp_lock);
James Smart3772a992009-05-22 14:50:54 -04005233
James Smart895427b2017-02-12 13:52:30 -08005234 INIT_LIST_HEAD(&phba->port_list);
5235 INIT_LIST_HEAD(&phba->work_list);
5236 init_waitqueue_head(&phba->wait_4_mlo_m_q);
5237
5238 /* Initialize the wait queue head for the kernel thread */
5239 init_waitqueue_head(&phba->work_waitq);
5240
5241 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5242 "1403 Protocols supported %s %s\n",
5243 ((phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) ?
5244 "SCSI" : " "),
5245 ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) ?
5246 "NVME" : " "));
5247
5248 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
5249 /* Initialize the scsi buffer list used by driver for scsi IO */
5250 spin_lock_init(&phba->scsi_buf_list_get_lock);
5251 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_get);
5252 spin_lock_init(&phba->scsi_buf_list_put_lock);
5253 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
5254 }
5255
5256 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
5257 (phba->nvmet_support == 0)) {
5258 /* Initialize the NVME buffer list used by driver for NVME IO */
5259 spin_lock_init(&phba->nvme_buf_list_get_lock);
5260 INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_get);
5261 spin_lock_init(&phba->nvme_buf_list_put_lock);
5262 INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_put);
5263 }
5264
5265 /* Initialize the fabric iocb list */
5266 INIT_LIST_HEAD(&phba->fabric_iocb_list);
5267
5268 /* Initialize list to save ELS buffers */
5269 INIT_LIST_HEAD(&phba->elsbuf);
5270
5271 /* Initialize FCF connection rec list */
5272 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
5273
5274 /* Initialize OAS configuration list */
5275 spin_lock_init(&phba->devicelock);
5276 INIT_LIST_HEAD(&phba->luns);
5277
James Smart3772a992009-05-22 14:50:54 -04005278 /* MBOX heartbeat timer */
5279 init_timer(&psli->mbox_tmo);
5280 psli->mbox_tmo.function = lpfc_mbox_timeout;
5281 psli->mbox_tmo.data = (unsigned long) phba;
James Smart3772a992009-05-22 14:50:54 -04005282 /* Fabric block timer */
5283 init_timer(&phba->fabric_block_timer);
5284 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
5285 phba->fabric_block_timer.data = (unsigned long) phba;
5286 /* EA polling mode timer */
5287 init_timer(&phba->eratt_poll);
5288 phba->eratt_poll.function = lpfc_poll_eratt;
5289 phba->eratt_poll.data = (unsigned long) phba;
James Smart895427b2017-02-12 13:52:30 -08005290 /* Heartbeat timer */
5291 init_timer(&phba->hb_tmofunc);
5292 phba->hb_tmofunc.function = lpfc_hb_timeout;
5293 phba->hb_tmofunc.data = (unsigned long)phba;
5294
5295 return 0;
5296}
5297
5298/**
5299 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev
5300 * @phba: pointer to lpfc hba data structure.
5301 *
5302 * This routine is invoked to set up the driver internal resources specific to
5303 * support the SLI-3 HBA device it attached to.
5304 *
5305 * Return codes
5306 * 0 - successful
5307 * other values - error
5308 **/
5309static int
5310lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
5311{
5312 int rc;
5313
5314 /*
5315 * Initialize timers used by driver
5316 */
5317
5318 /* FCP polling mode timer */
5319 init_timer(&phba->fcp_poll_timer);
5320 phba->fcp_poll_timer.function = lpfc_poll_timeout;
5321 phba->fcp_poll_timer.data = (unsigned long) phba;
James Smart3772a992009-05-22 14:50:54 -04005322
5323 /* Host attention work mask setup */
5324 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
5325 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
5326
5327 /* Get all the module params for configuring this host */
5328 lpfc_get_cfgparam(phba);
James Smart895427b2017-02-12 13:52:30 -08005329 /* Set up phase-1 common device driver resources */
5330
5331 rc = lpfc_setup_driver_resource_phase1(phba);
5332 if (rc)
5333 return -ENODEV;
5334
James Smart49198b32010-04-06 15:04:33 -04005335 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
5336 phba->menlo_flag |= HBA_MENLO_SUPPORT;
5337 /* check for menlo minimum sg count */
5338 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
5339 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
5340 }
5341
James Smart895427b2017-02-12 13:52:30 -08005342 if (!phba->sli.sli3_ring)
5343 phba->sli.sli3_ring = kzalloc(LPFC_SLI3_MAX_RING *
James Smart2a76a282012-08-03 12:35:54 -04005344 sizeof(struct lpfc_sli_ring), GFP_KERNEL);
James Smart895427b2017-02-12 13:52:30 -08005345 if (!phba->sli.sli3_ring)
James Smart2a76a282012-08-03 12:35:54 -04005346 return -ENOMEM;
5347
James Smart3772a992009-05-22 14:50:54 -04005348 /*
James Smart96f70772013-04-17 20:16:15 -04005349 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smart3772a992009-05-22 14:50:54 -04005350 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smart3772a992009-05-22 14:50:54 -04005351 */
James Smart3772a992009-05-22 14:50:54 -04005352
James Smart96f70772013-04-17 20:16:15 -04005353 /* Initialize the host templates the configured values. */
James Smart3772a992009-05-22 14:50:54 -04005354 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
Bodo Stroesser5f406fa2015-08-31 16:48:10 -04005355 lpfc_template_s3.sg_tablesize = phba->cfg_sg_seg_cnt;
James Smart3772a992009-05-22 14:50:54 -04005356
James Smart96f70772013-04-17 20:16:15 -04005357 /* There are going to be 2 reserved BDEs: 1 FCP cmnd + 1 FCP rsp */
5358 if (phba->cfg_enable_bg) {
5359 /*
5360 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
5361 * the FCP rsp, and a BDE for each. Sice we have no control
5362 * over how many protection data segments the SCSI Layer
5363 * will hand us (ie: there could be one for every block
5364 * in the IO), we just allocate enough BDEs to accomidate
5365 * our max amount and we need to limit lpfc_sg_seg_cnt to
5366 * minimize the risk of running out.
5367 */
5368 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5369 sizeof(struct fcp_rsp) +
5370 (LPFC_MAX_SG_SEG_CNT * sizeof(struct ulp_bde64));
5371
5372 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SEG_CNT_DIF)
5373 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT_DIF;
5374
5375 /* Total BDEs in BPL for scsi_sg_list and scsi_sg_prot_list */
5376 phba->cfg_total_seg_cnt = LPFC_MAX_SG_SEG_CNT;
5377 } else {
5378 /*
5379 * The scsi_buf for a regular I/O will hold the FCP cmnd,
5380 * the FCP rsp, a BDE for each, and a BDE for up to
5381 * cfg_sg_seg_cnt data segments.
5382 */
5383 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5384 sizeof(struct fcp_rsp) +
5385 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
5386
5387 /* Total BDEs in BPL for scsi_sg_list */
5388 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
5389 }
5390
5391 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
5392 "9088 sg_tablesize:%d dmabuf_size:%d total_bde:%d\n",
5393 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
5394 phba->cfg_total_seg_cnt);
5395
James Smart3772a992009-05-22 14:50:54 -04005396 phba->max_vpi = LPFC_MAX_VPI;
5397 /* This will be set to correct value after config_port mbox */
5398 phba->max_vports = 0;
5399
5400 /*
5401 * Initialize the SLI Layer to run with lpfc HBAs.
5402 */
5403 lpfc_sli_setup(phba);
James Smart895427b2017-02-12 13:52:30 -08005404 lpfc_sli_queue_init(phba);
James Smart3772a992009-05-22 14:50:54 -04005405
5406 /* Allocate device driver memory */
5407 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
5408 return -ENOMEM;
5409
James Smart912e3ac2011-05-24 11:42:11 -04005410 /*
5411 * Enable sr-iov virtual functions if supported and configured
5412 * through the module parameter.
5413 */
5414 if (phba->cfg_sriov_nr_virtfn > 0) {
5415 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
5416 phba->cfg_sriov_nr_virtfn);
5417 if (rc) {
5418 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5419 "2808 Requested number of SR-IOV "
5420 "virtual functions (%d) is not "
5421 "supported\n",
5422 phba->cfg_sriov_nr_virtfn);
5423 phba->cfg_sriov_nr_virtfn = 0;
5424 }
5425 }
5426
James Smart3772a992009-05-22 14:50:54 -04005427 return 0;
5428}
5429
5430/**
5431 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
5432 * @phba: pointer to lpfc hba data structure.
5433 *
5434 * This routine is invoked to unset the driver internal resources set up
5435 * specific for supporting the SLI-3 HBA device it attached to.
5436 **/
5437static void
5438lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
5439{
5440 /* Free device driver memory allocated */
5441 lpfc_mem_free_all(phba);
5442
5443 return;
5444}
5445
5446/**
James Smartda0436e2009-05-22 14:51:39 -04005447 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
5448 * @phba: pointer to lpfc hba data structure.
5449 *
5450 * This routine is invoked to set up the driver internal resources specific to
5451 * support the SLI-4 HBA device it attached to.
5452 *
5453 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005454 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005455 * other values - error
5456 **/
5457static int
5458lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
5459{
James Smart28baac72010-02-12 14:42:03 -05005460 LPFC_MBOXQ_t *mboxq;
James Smart895427b2017-02-12 13:52:30 -08005461 int rc, i, max_buf_size;
James Smart28baac72010-02-12 14:42:03 -05005462 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
5463 struct lpfc_mqe *mqe;
James Smart09294d42013-04-17 20:16:05 -04005464 int longs;
James Smart1ba981f2014-02-20 09:56:45 -05005465 int fof_vectors = 0;
James Smartda0436e2009-05-22 14:51:39 -04005466
James Smart895427b2017-02-12 13:52:30 -08005467 phba->sli4_hba.num_online_cpu = num_online_cpus();
5468 phba->sli4_hba.num_present_cpu = lpfc_present_cpu;
5469 phba->sli4_hba.curr_disp_cpu = 0;
5470
James Smart716d3bc2013-09-06 12:18:28 -04005471 /* Get all the module params for configuring this host */
5472 lpfc_get_cfgparam(phba);
5473
James Smart895427b2017-02-12 13:52:30 -08005474 /* Set up phase-1 common device driver resources */
5475 rc = lpfc_setup_driver_resource_phase1(phba);
5476 if (rc)
5477 return -ENODEV;
5478
James Smartda0436e2009-05-22 14:51:39 -04005479 /* Before proceed, wait for POST done and device ready */
5480 rc = lpfc_sli4_post_status_check(phba);
5481 if (rc)
5482 return -ENODEV;
5483
5484 /*
5485 * Initialize timers used by driver
5486 */
5487
James Smart19ca7602010-11-20 23:11:55 -05005488 init_timer(&phba->rrq_tmr);
5489 phba->rrq_tmr.function = lpfc_rrq_timeout;
5490 phba->rrq_tmr.data = (unsigned long)phba;
James Smartda0436e2009-05-22 14:51:39 -04005491
James Smartecfd03c2010-02-12 14:41:27 -05005492 /* FCF rediscover timer */
5493 init_timer(&phba->fcf.redisc_wait);
5494 phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
5495 phba->fcf.redisc_wait.data = (unsigned long)phba;
5496
James Smartda0436e2009-05-22 14:51:39 -04005497 /*
James Smart7ad20aa2011-05-24 11:44:28 -04005498 * Control structure for handling external multi-buffer mailbox
5499 * command pass-through.
5500 */
5501 memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
5502 sizeof(struct lpfc_mbox_ext_buf_ctx));
5503 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
5504
James Smartda0436e2009-05-22 14:51:39 -04005505 phba->max_vpi = LPFC_MAX_VPI;
James Smart67d12732012-08-03 12:36:13 -04005506
James Smartda0436e2009-05-22 14:51:39 -04005507 /* This will be set to correct value after the read_config mbox */
5508 phba->max_vports = 0;
5509
5510 /* Program the default value of vlan_id and fc_map */
5511 phba->valid_vlan = 0;
5512 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5513 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5514 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5515
5516 /*
James Smart2a76a282012-08-03 12:35:54 -04005517 * For SLI4, instead of using ring 0 (LPFC_FCP_RING) for FCP commands
James Smart895427b2017-02-12 13:52:30 -08005518 * we will associate a new ring, for each EQ/CQ/WQ tuple.
5519 * The WQ create will allocate the ring.
James Smart2a76a282012-08-03 12:35:54 -04005520 */
James Smart085c6472010-11-20 23:11:37 -05005521
James Smart09294d42013-04-17 20:16:05 -04005522 /*
5523 * It doesn't matter what family our adapter is in, we are
5524 * limited to 2 Pages, 512 SGEs, for our SGL.
5525 * There are going to be 2 reserved SGEs: 1 FCP cmnd + 1 FCP rsp
5526 */
5527 max_buf_size = (2 * SLI4_PAGE_SIZE);
5528 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SGL_SEG_CNT - 2)
5529 phba->cfg_sg_seg_cnt = LPFC_MAX_SGL_SEG_CNT - 2;
James Smart09294d42013-04-17 20:16:05 -04005530
James Smartda0436e2009-05-22 14:51:39 -04005531 /*
James Smart895427b2017-02-12 13:52:30 -08005532 * Since lpfc_sg_seg_cnt is module param, the sg_dma_buf_size
5533 * used to create the sg_dma_buf_pool must be calculated.
James Smart28baac72010-02-12 14:42:03 -05005534 */
James Smart96f70772013-04-17 20:16:15 -04005535 if (phba->cfg_enable_bg) {
5536 /*
James Smart895427b2017-02-12 13:52:30 -08005537 * The scsi_buf for a T10-DIF I/O holds the FCP cmnd,
5538 * the FCP rsp, and a SGE. Sice we have no control
5539 * over how many protection segments the SCSI Layer
James Smart96f70772013-04-17 20:16:15 -04005540 * will hand us (ie: there could be one for every block
James Smart895427b2017-02-12 13:52:30 -08005541 * in the IO), just allocate enough SGEs to accomidate
5542 * our max amount and we need to limit lpfc_sg_seg_cnt
5543 * to minimize the risk of running out.
James Smart96f70772013-04-17 20:16:15 -04005544 */
5545 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
James Smart895427b2017-02-12 13:52:30 -08005546 sizeof(struct fcp_rsp) + max_buf_size;
James Smart96f70772013-04-17 20:16:15 -04005547
5548 /* Total SGEs for scsi_sg_list and scsi_sg_prot_list */
5549 phba->cfg_total_seg_cnt = LPFC_MAX_SGL_SEG_CNT;
5550
5551 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SLI4_SEG_CNT_DIF)
James Smart895427b2017-02-12 13:52:30 -08005552 phba->cfg_sg_seg_cnt =
5553 LPFC_MAX_SG_SLI4_SEG_CNT_DIF;
James Smart96f70772013-04-17 20:16:15 -04005554 } else {
5555 /*
James Smart895427b2017-02-12 13:52:30 -08005556 * The scsi_buf for a regular I/O holds the FCP cmnd,
James Smart96f70772013-04-17 20:16:15 -04005557 * the FCP rsp, a SGE for each, and a SGE for up to
5558 * cfg_sg_seg_cnt data segments.
5559 */
5560 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
James Smart895427b2017-02-12 13:52:30 -08005561 sizeof(struct fcp_rsp) +
5562 ((phba->cfg_sg_seg_cnt + 2) *
5563 sizeof(struct sli4_sge));
James Smart96f70772013-04-17 20:16:15 -04005564
5565 /* Total SGEs for scsi_sg_list */
5566 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
James Smart895427b2017-02-12 13:52:30 -08005567
James Smart96f70772013-04-17 20:16:15 -04005568 /*
James Smart895427b2017-02-12 13:52:30 -08005569 * NOTE: if (phba->cfg_sg_seg_cnt + 2) <= 256 we only
5570 * need to post 1 page for the SGL.
James Smart96f70772013-04-17 20:16:15 -04005571 */
James Smart085c6472010-11-20 23:11:37 -05005572 }
James Smartacd68592012-01-18 16:25:09 -05005573
James Smart96f70772013-04-17 20:16:15 -04005574 /* Initialize the host templates with the updated values. */
5575 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5576 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5577
5578 if (phba->cfg_sg_dma_buf_size <= LPFC_MIN_SG_SLI4_BUF_SZ)
5579 phba->cfg_sg_dma_buf_size = LPFC_MIN_SG_SLI4_BUF_SZ;
5580 else
5581 phba->cfg_sg_dma_buf_size =
5582 SLI4_PAGE_ALIGN(phba->cfg_sg_dma_buf_size);
5583
5584 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
5585 "9087 sg_tablesize:%d dmabuf_size:%d total_sge:%d\n",
5586 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
5587 phba->cfg_total_seg_cnt);
James Smartda0436e2009-05-22 14:51:39 -04005588
5589 /* Initialize buffer queue management fields */
James Smart895427b2017-02-12 13:52:30 -08005590 INIT_LIST_HEAD(&phba->hbqs[LPFC_ELS_HBQ].hbq_buffer_list);
James Smartda0436e2009-05-22 14:51:39 -04005591 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
5592 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
5593
5594 /*
5595 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
5596 */
James Smart895427b2017-02-12 13:52:30 -08005597 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
5598 /* Initialize the Abort scsi buffer list used by driver */
5599 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
5600 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
5601 }
5602
5603 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
5604 /* Initialize the Abort nvme buffer list used by driver */
5605 spin_lock_init(&phba->sli4_hba.abts_nvme_buf_list_lock);
5606 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvme_buf_list);
5607 }
5608
James Smartda0436e2009-05-22 14:51:39 -04005609 /* This abort list used by worker thread */
James Smart895427b2017-02-12 13:52:30 -08005610 spin_lock_init(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04005611
5612 /*
James Smart6d368e52011-05-24 11:44:12 -04005613 * Initialize driver internal slow-path work queues
James Smartda0436e2009-05-22 14:51:39 -04005614 */
5615
5616 /* Driver internel slow-path CQ Event pool */
5617 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
5618 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04005619 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04005620 /* Asynchronous event CQ Event work queue list */
5621 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
5622 /* Fast-path XRI aborted CQ Event work queue list */
5623 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
5624 /* Slow-path XRI aborted CQ Event work queue list */
5625 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
5626 /* Receive queue CQ Event work queue list */
5627 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
5628
James Smart6d368e52011-05-24 11:44:12 -04005629 /* Initialize extent block lists. */
5630 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
5631 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
5632 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
5633 INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
5634
James Smart448193b2015-12-16 18:12:05 -05005635 /* initialize optic_state to 0xFF */
5636 phba->sli4_hba.lnk_info.optic_state = 0xff;
5637
James Smartda0436e2009-05-22 14:51:39 -04005638 /* Allocate device driver memory */
5639 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
5640 if (rc)
5641 return -ENOMEM;
5642
James Smart2fcee4b2010-12-15 17:57:46 -05005643 /* IF Type 2 ports get initialized now. */
5644 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
5645 LPFC_SLI_INTF_IF_TYPE_2) {
5646 rc = lpfc_pci_function_reset(phba);
James Smart895427b2017-02-12 13:52:30 -08005647 if (unlikely(rc)) {
5648 rc = -ENODEV;
5649 goto out_free_mem;
5650 }
James Smart946727d2015-04-07 15:07:09 -04005651 phba->temp_sensor_support = 1;
James Smart2fcee4b2010-12-15 17:57:46 -05005652 }
5653
James Smartda0436e2009-05-22 14:51:39 -04005654 /* Create the bootstrap mailbox command */
5655 rc = lpfc_create_bootstrap_mbox(phba);
5656 if (unlikely(rc))
5657 goto out_free_mem;
5658
5659 /* Set up the host's endian order with the device. */
5660 rc = lpfc_setup_endian_order(phba);
5661 if (unlikely(rc))
5662 goto out_free_bsmbx;
5663
5664 /* Set up the hba's configuration parameters. */
5665 rc = lpfc_sli4_read_config(phba);
5666 if (unlikely(rc))
5667 goto out_free_bsmbx;
James Smartcff261f2013-12-17 20:29:47 -05005668 rc = lpfc_mem_alloc_active_rrq_pool_s4(phba);
5669 if (unlikely(rc))
5670 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04005671
James Smart2fcee4b2010-12-15 17:57:46 -05005672 /* IF Type 0 ports get initialized now. */
5673 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
5674 LPFC_SLI_INTF_IF_TYPE_0) {
5675 rc = lpfc_pci_function_reset(phba);
5676 if (unlikely(rc))
5677 goto out_free_bsmbx;
5678 }
James Smartda0436e2009-05-22 14:51:39 -04005679
James Smartcb5172e2010-03-15 11:25:07 -04005680 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
5681 GFP_KERNEL);
5682 if (!mboxq) {
5683 rc = -ENOMEM;
5684 goto out_free_bsmbx;
5685 }
5686
James Smart895427b2017-02-12 13:52:30 -08005687 phba->nvmet_support = 0;
5688
5689 lpfc_nvme_mod_param_dep(phba);
5690
James Smartfedd3b72011-02-16 12:39:24 -05005691 /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
James Smartcb5172e2010-03-15 11:25:07 -04005692 lpfc_supported_pages(mboxq);
5693 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smartfedd3b72011-02-16 12:39:24 -05005694 if (!rc) {
5695 mqe = &mboxq->u.mqe;
5696 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
5697 LPFC_MAX_SUPPORTED_PAGES);
5698 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
5699 switch (pn_page[i]) {
5700 case LPFC_SLI4_PARAMETERS:
5701 phba->sli4_hba.pc_sli4_params.supported = 1;
5702 break;
5703 default:
5704 break;
5705 }
5706 }
5707 /* Read the port's SLI4 Parameters capabilities if supported. */
5708 if (phba->sli4_hba.pc_sli4_params.supported)
5709 rc = lpfc_pc_sli4_params_get(phba, mboxq);
5710 if (rc) {
5711 mempool_free(mboxq, phba->mbox_mem_pool);
5712 rc = -EIO;
5713 goto out_free_bsmbx;
James Smartcb5172e2010-03-15 11:25:07 -04005714 }
5715 }
James Smart65791f12016-07-06 12:35:56 -07005716
James Smartfedd3b72011-02-16 12:39:24 -05005717 /*
5718 * Get sli4 parameters that override parameters from Port capabilities.
James Smart6d368e52011-05-24 11:44:12 -04005719 * If this call fails, it isn't critical unless the SLI4 parameters come
5720 * back in conflict.
James Smartfedd3b72011-02-16 12:39:24 -05005721 */
James Smart6d368e52011-05-24 11:44:12 -04005722 rc = lpfc_get_sli4_parameters(phba, mboxq);
5723 if (rc) {
5724 if (phba->sli4_hba.extents_in_use &&
5725 phba->sli4_hba.rpi_hdrs_in_use) {
5726 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5727 "2999 Unsupported SLI4 Parameters "
5728 "Extents and RPI headers enabled.\n");
James Smart6d368e52011-05-24 11:44:12 -04005729 }
James Smart895427b2017-02-12 13:52:30 -08005730 mempool_free(mboxq, phba->mbox_mem_pool);
5731 goto out_free_bsmbx;
James Smart6d368e52011-05-24 11:44:12 -04005732 }
James Smart895427b2017-02-12 13:52:30 -08005733
James Smartcb5172e2010-03-15 11:25:07 -04005734 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1ba981f2014-02-20 09:56:45 -05005735
5736 /* Verify OAS is supported */
5737 lpfc_sli4_oas_verify(phba);
5738 if (phba->cfg_fof)
5739 fof_vectors = 1;
5740
James Smart5350d872011-10-10 21:33:49 -04005741 /* Verify all the SLI4 queues */
5742 rc = lpfc_sli4_queue_verify(phba);
James Smartda0436e2009-05-22 14:51:39 -04005743 if (rc)
5744 goto out_free_bsmbx;
5745
5746 /* Create driver internal CQE event pool */
5747 rc = lpfc_sli4_cq_event_pool_create(phba);
5748 if (rc)
James Smart5350d872011-10-10 21:33:49 -04005749 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04005750
James Smart8a9d2e82012-05-09 21:16:12 -04005751 /* Initialize sgl lists per host */
5752 lpfc_init_sgl_list(phba);
5753
5754 /* Allocate and initialize active sgl array */
James Smartda0436e2009-05-22 14:51:39 -04005755 rc = lpfc_init_active_sgl_array(phba);
5756 if (rc) {
5757 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5758 "1430 Failed to initialize sgl list.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04005759 goto out_destroy_cq_event_pool;
James Smartda0436e2009-05-22 14:51:39 -04005760 }
James Smartda0436e2009-05-22 14:51:39 -04005761 rc = lpfc_sli4_init_rpi_hdrs(phba);
5762 if (rc) {
5763 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5764 "1432 Failed to initialize rpi headers.\n");
5765 goto out_free_active_sgl;
5766 }
5767
James Smarta93ff372010-10-22 11:06:08 -04005768 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
James Smart0c9ab6f2010-02-26 14:15:57 -05005769 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
5770 phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
5771 GFP_KERNEL);
5772 if (!phba->fcf.fcf_rr_bmask) {
5773 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5774 "2759 Failed allocate memory for FCF round "
5775 "robin failover bmask\n");
James Smart05580562011-05-24 11:40:48 -04005776 rc = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -05005777 goto out_remove_rpi_hdrs;
5778 }
5779
James Smart895427b2017-02-12 13:52:30 -08005780 phba->sli4_hba.hba_eq_hdl = kcalloc(fof_vectors + phba->io_channel_irqs,
5781 sizeof(struct lpfc_hba_eq_hdl),
5782 GFP_KERNEL);
5783 if (!phba->sli4_hba.hba_eq_hdl) {
James Smart67d12732012-08-03 12:36:13 -04005784 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5785 "2572 Failed allocate memory for "
5786 "fast-path per-EQ handle array\n");
5787 rc = -ENOMEM;
5788 goto out_free_fcf_rr_bmask;
James Smartda0436e2009-05-22 14:51:39 -04005789 }
5790
James Smart895427b2017-02-12 13:52:30 -08005791 phba->sli4_hba.cpu_map = kcalloc(phba->sli4_hba.num_present_cpu,
5792 sizeof(struct lpfc_vector_map_info),
5793 GFP_KERNEL);
James Smart7bb03bb2013-04-17 20:19:16 -04005794 if (!phba->sli4_hba.cpu_map) {
5795 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5796 "3327 Failed allocate memory for msi-x "
5797 "interrupt vector mapping\n");
5798 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08005799 goto out_free_hba_eq_hdl;
James Smart7bb03bb2013-04-17 20:19:16 -04005800 }
James Smartb246de12013-05-31 17:03:07 -04005801 if (lpfc_used_cpu == NULL) {
James Smart895427b2017-02-12 13:52:30 -08005802 lpfc_used_cpu = kcalloc(lpfc_present_cpu, sizeof(uint16_t),
5803 GFP_KERNEL);
James Smartb246de12013-05-31 17:03:07 -04005804 if (!lpfc_used_cpu) {
5805 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5806 "3335 Failed allocate memory for msi-x "
5807 "interrupt vector mapping\n");
5808 kfree(phba->sli4_hba.cpu_map);
5809 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08005810 goto out_free_hba_eq_hdl;
James Smartb246de12013-05-31 17:03:07 -04005811 }
5812 for (i = 0; i < lpfc_present_cpu; i++)
5813 lpfc_used_cpu[i] = LPFC_VECTOR_MAP_EMPTY;
5814 }
5815
James Smart912e3ac2011-05-24 11:42:11 -04005816 /*
5817 * Enable sr-iov virtual functions if supported and configured
5818 * through the module parameter.
5819 */
5820 if (phba->cfg_sriov_nr_virtfn > 0) {
5821 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
5822 phba->cfg_sriov_nr_virtfn);
5823 if (rc) {
5824 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5825 "3020 Requested number of SR-IOV "
5826 "virtual functions (%d) is not "
5827 "supported\n",
5828 phba->cfg_sriov_nr_virtfn);
5829 phba->cfg_sriov_nr_virtfn = 0;
5830 }
5831 }
5832
James Smart5248a742011-07-22 18:37:06 -04005833 return 0;
James Smartda0436e2009-05-22 14:51:39 -04005834
James Smart895427b2017-02-12 13:52:30 -08005835out_free_hba_eq_hdl:
5836 kfree(phba->sli4_hba.hba_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05005837out_free_fcf_rr_bmask:
5838 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04005839out_remove_rpi_hdrs:
5840 lpfc_sli4_remove_rpi_hdrs(phba);
5841out_free_active_sgl:
5842 lpfc_free_active_sgl(phba);
James Smartda0436e2009-05-22 14:51:39 -04005843out_destroy_cq_event_pool:
5844 lpfc_sli4_cq_event_pool_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04005845out_free_bsmbx:
5846 lpfc_destroy_bootstrap_mbox(phba);
5847out_free_mem:
5848 lpfc_mem_free(phba);
5849 return rc;
5850}
5851
5852/**
5853 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
5854 * @phba: pointer to lpfc hba data structure.
5855 *
5856 * This routine is invoked to unset the driver internal resources set up
5857 * specific for supporting the SLI-4 HBA device it attached to.
5858 **/
5859static void
5860lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
5861{
5862 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
5863
James Smart7bb03bb2013-04-17 20:19:16 -04005864 /* Free memory allocated for msi-x interrupt vector to CPU mapping */
5865 kfree(phba->sli4_hba.cpu_map);
5866 phba->sli4_hba.num_present_cpu = 0;
5867 phba->sli4_hba.num_online_cpu = 0;
James Smart76fd07a2014-02-20 09:57:18 -05005868 phba->sli4_hba.curr_disp_cpu = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04005869
James Smartda0436e2009-05-22 14:51:39 -04005870 /* Free memory allocated for fast-path work queue handles */
James Smart895427b2017-02-12 13:52:30 -08005871 kfree(phba->sli4_hba.hba_eq_hdl);
James Smartda0436e2009-05-22 14:51:39 -04005872
5873 /* Free the allocated rpi headers. */
5874 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04005875 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04005876
James Smart0c9ab6f2010-02-26 14:15:57 -05005877 /* Free eligible FCF index bmask */
5878 kfree(phba->fcf.fcf_rr_bmask);
5879
James Smartda0436e2009-05-22 14:51:39 -04005880 /* Free the ELS sgl list */
5881 lpfc_free_active_sgl(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04005882 lpfc_free_els_sgl_list(phba);
James Smartda0436e2009-05-22 14:51:39 -04005883
James Smartda0436e2009-05-22 14:51:39 -04005884 /* Free the completion queue EQ event pool */
5885 lpfc_sli4_cq_event_release_all(phba);
5886 lpfc_sli4_cq_event_pool_destroy(phba);
5887
James Smart6d368e52011-05-24 11:44:12 -04005888 /* Release resource identifiers. */
5889 lpfc_sli4_dealloc_resource_identifiers(phba);
5890
James Smartda0436e2009-05-22 14:51:39 -04005891 /* Free the bsmbx region. */
5892 lpfc_destroy_bootstrap_mbox(phba);
5893
5894 /* Free the SLI Layer memory with SLI4 HBAs */
5895 lpfc_mem_free_all(phba);
5896
5897 /* Free the current connect table */
5898 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04005899 &phba->fcf_conn_rec_list, list) {
5900 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04005901 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04005902 }
James Smartda0436e2009-05-22 14:51:39 -04005903
5904 return;
5905}
5906
5907/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005908 * lpfc_init_api_table_setup - Set up init api function jump table
James Smart3772a992009-05-22 14:50:54 -04005909 * @phba: The hba struct for which this call is being executed.
5910 * @dev_grp: The HBA PCI-Device group number.
5911 *
5912 * This routine sets up the device INIT interface API function jump table
5913 * in @phba struct.
5914 *
5915 * Returns: 0 - success, -ENODEV - failure.
5916 **/
5917int
5918lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5919{
James Smart84d1b002010-02-12 14:42:33 -05005920 phba->lpfc_hba_init_link = lpfc_hba_init_link;
5921 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart7f860592011-03-11 16:05:52 -05005922 phba->lpfc_selective_reset = lpfc_selective_reset;
James Smart3772a992009-05-22 14:50:54 -04005923 switch (dev_grp) {
5924 case LPFC_PCI_DEV_LP:
5925 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
5926 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
5927 phba->lpfc_stop_port = lpfc_stop_port_s3;
5928 break;
James Smartda0436e2009-05-22 14:51:39 -04005929 case LPFC_PCI_DEV_OC:
5930 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
5931 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
5932 phba->lpfc_stop_port = lpfc_stop_port_s4;
5933 break;
James Smart3772a992009-05-22 14:50:54 -04005934 default:
5935 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5936 "1431 Invalid HBA PCI-device group: 0x%x\n",
5937 dev_grp);
5938 return -ENODEV;
5939 break;
5940 }
5941 return 0;
5942}
5943
5944/**
James Smart3772a992009-05-22 14:50:54 -04005945 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
5946 * @phba: pointer to lpfc hba data structure.
5947 *
5948 * This routine is invoked to set up the driver internal resources after the
5949 * device specific resource setup to support the HBA device it attached to.
5950 *
5951 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005952 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005953 * other values - error
5954 **/
5955static int
5956lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
5957{
5958 int error;
5959
5960 /* Startup the kernel thread for this host adapter. */
5961 phba->worker_thread = kthread_run(lpfc_do_work, phba,
5962 "lpfc_worker_%d", phba->brd_no);
5963 if (IS_ERR(phba->worker_thread)) {
5964 error = PTR_ERR(phba->worker_thread);
5965 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005966 }
5967
James Smart3772a992009-05-22 14:50:54 -04005968 return 0;
5969}
5970
5971/**
5972 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
5973 * @phba: pointer to lpfc hba data structure.
5974 *
5975 * This routine is invoked to unset the driver internal resources set up after
5976 * the device specific resource setup for supporting the HBA device it
5977 * attached to.
5978 **/
5979static void
5980lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
5981{
5982 /* Stop kernel worker thread */
5983 kthread_stop(phba->worker_thread);
5984}
5985
5986/**
5987 * lpfc_free_iocb_list - Free iocb list.
5988 * @phba: pointer to lpfc hba data structure.
5989 *
5990 * This routine is invoked to free the driver's IOCB list and memory.
5991 **/
5992static void
5993lpfc_free_iocb_list(struct lpfc_hba *phba)
5994{
5995 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
5996
5997 spin_lock_irq(&phba->hbalock);
5998 list_for_each_entry_safe(iocbq_entry, iocbq_next,
5999 &phba->lpfc_iocb_list, list) {
6000 list_del(&iocbq_entry->list);
6001 kfree(iocbq_entry);
6002 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05006003 }
James Smart3772a992009-05-22 14:50:54 -04006004 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006005
James Smart3772a992009-05-22 14:50:54 -04006006 return;
6007}
dea31012005-04-17 16:05:31 -05006008
James Smart3772a992009-05-22 14:50:54 -04006009/**
6010 * lpfc_init_iocb_list - Allocate and initialize iocb list.
6011 * @phba: pointer to lpfc hba data structure.
6012 *
6013 * This routine is invoked to allocate and initizlize the driver's IOCB
6014 * list and set up the IOCB tag array accordingly.
6015 *
6016 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006017 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006018 * other values - error
6019 **/
6020static int
6021lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
6022{
6023 struct lpfc_iocbq *iocbq_entry = NULL;
6024 uint16_t iotag;
6025 int i;
dea31012005-04-17 16:05:31 -05006026
6027 /* Initialize and populate the iocb list per host. */
6028 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04006029 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07006030 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05006031 if (iocbq_entry == NULL) {
6032 printk(KERN_ERR "%s: only allocated %d iocbs of "
6033 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07006034 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05006035 goto out_free_iocbq;
6036 }
6037
James Bottomley604a3e32005-10-29 10:28:33 -05006038 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
6039 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04006040 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05006041 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04006042 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05006043 goto out_free_iocbq;
6044 }
James Smart6d368e52011-05-24 11:44:12 -04006045 iocbq_entry->sli4_lxritag = NO_XRI;
James Smart3772a992009-05-22 14:50:54 -04006046 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05006047
6048 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006049 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
6050 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05006051 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006052 }
6053
James Smart3772a992009-05-22 14:50:54 -04006054 return 0;
6055
6056out_free_iocbq:
6057 lpfc_free_iocb_list(phba);
6058
6059 return -ENOMEM;
6060}
6061
6062/**
James Smart8a9d2e82012-05-09 21:16:12 -04006063 * lpfc_free_sgl_list - Free a given sgl list.
James Smartda0436e2009-05-22 14:51:39 -04006064 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -04006065 * @sglq_list: pointer to the head of sgl list.
James Smartda0436e2009-05-22 14:51:39 -04006066 *
James Smart8a9d2e82012-05-09 21:16:12 -04006067 * This routine is invoked to free a give sgl list and memory.
James Smartda0436e2009-05-22 14:51:39 -04006068 **/
James Smart8a9d2e82012-05-09 21:16:12 -04006069void
6070lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
James Smartda0436e2009-05-22 14:51:39 -04006071{
6072 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart8a9d2e82012-05-09 21:16:12 -04006073
6074 list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
6075 list_del(&sglq_entry->list);
6076 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
6077 kfree(sglq_entry);
6078 }
6079}
6080
6081/**
6082 * lpfc_free_els_sgl_list - Free els sgl list.
6083 * @phba: pointer to lpfc hba data structure.
6084 *
6085 * This routine is invoked to free the driver's els sgl list and memory.
6086 **/
6087static void
6088lpfc_free_els_sgl_list(struct lpfc_hba *phba)
6089{
James Smartda0436e2009-05-22 14:51:39 -04006090 LIST_HEAD(sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04006091
James Smart8a9d2e82012-05-09 21:16:12 -04006092 /* Retrieve all els sgls from driver list */
James Smartda0436e2009-05-22 14:51:39 -04006093 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006094 spin_lock(&phba->sli4_hba.sgl_list_lock);
6095 list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list, &sglq_list);
6096 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04006097 spin_unlock_irq(&phba->hbalock);
6098
James Smart8a9d2e82012-05-09 21:16:12 -04006099 /* Now free the sgl list */
6100 lpfc_free_sgl_list(phba, &sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04006101}
6102
6103/**
6104 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
6105 * @phba: pointer to lpfc hba data structure.
6106 *
6107 * This routine is invoked to allocate the driver's active sgl memory.
6108 * This array will hold the sglq_entry's for active IOs.
6109 **/
6110static int
6111lpfc_init_active_sgl_array(struct lpfc_hba *phba)
6112{
6113 int size;
6114 size = sizeof(struct lpfc_sglq *);
6115 size *= phba->sli4_hba.max_cfg_param.max_xri;
6116
6117 phba->sli4_hba.lpfc_sglq_active_list =
6118 kzalloc(size, GFP_KERNEL);
6119 if (!phba->sli4_hba.lpfc_sglq_active_list)
6120 return -ENOMEM;
6121 return 0;
6122}
6123
6124/**
6125 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
6126 * @phba: pointer to lpfc hba data structure.
6127 *
6128 * This routine is invoked to walk through the array of active sglq entries
6129 * and free all of the resources.
6130 * This is just a place holder for now.
6131 **/
6132static void
6133lpfc_free_active_sgl(struct lpfc_hba *phba)
6134{
6135 kfree(phba->sli4_hba.lpfc_sglq_active_list);
6136}
6137
6138/**
6139 * lpfc_init_sgl_list - Allocate and initialize sgl list.
6140 * @phba: pointer to lpfc hba data structure.
6141 *
6142 * This routine is invoked to allocate and initizlize the driver's sgl
6143 * list and set up the sgl xritag tag array accordingly.
6144 *
James Smartda0436e2009-05-22 14:51:39 -04006145 **/
James Smart8a9d2e82012-05-09 21:16:12 -04006146static void
James Smartda0436e2009-05-22 14:51:39 -04006147lpfc_init_sgl_list(struct lpfc_hba *phba)
6148{
James Smartda0436e2009-05-22 14:51:39 -04006149 /* Initialize and populate the sglq list per host/VF. */
James Smart895427b2017-02-12 13:52:30 -08006150 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_els_sgl_list);
James Smartda0436e2009-05-22 14:51:39 -04006151 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
6152
James Smart8a9d2e82012-05-09 21:16:12 -04006153 /* els xri-sgl book keeping */
6154 phba->sli4_hba.els_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04006155
James Smart8a9d2e82012-05-09 21:16:12 -04006156 /* scsi xri-buffer book keeping */
James Smartda0436e2009-05-22 14:51:39 -04006157 phba->sli4_hba.scsi_xri_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -08006158
6159 /* nvme xri-buffer book keeping */
6160 phba->sli4_hba.nvme_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04006161}
6162
6163/**
6164 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
6165 * @phba: pointer to lpfc hba data structure.
6166 *
6167 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -04006168 * port for those SLI4 ports that do not support extents. This routine
James Smartda0436e2009-05-22 14:51:39 -04006169 * posts a PAGE_SIZE memory region to the port to hold up to
James Smart88a2cfb2011-07-22 18:36:33 -04006170 * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
6171 * and should be called only when interrupts are disabled.
James Smartda0436e2009-05-22 14:51:39 -04006172 *
6173 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006174 * 0 - successful
James Smart88a2cfb2011-07-22 18:36:33 -04006175 * -ERROR - otherwise.
James Smartda0436e2009-05-22 14:51:39 -04006176 **/
6177int
6178lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
6179{
6180 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04006181 struct lpfc_rpi_hdr *rpi_hdr;
6182
6183 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
James Smartff78d8f2011-12-13 13:21:35 -05006184 if (!phba->sli4_hba.rpi_hdrs_in_use)
James Smart6d368e52011-05-24 11:44:12 -04006185 return rc;
James Smart6d368e52011-05-24 11:44:12 -04006186 if (phba->sli4_hba.extents_in_use)
6187 return -EIO;
James Smartda0436e2009-05-22 14:51:39 -04006188
6189 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
6190 if (!rpi_hdr) {
6191 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6192 "0391 Error during rpi post operation\n");
6193 lpfc_sli4_remove_rpis(phba);
6194 rc = -ENODEV;
6195 }
6196
6197 return rc;
6198}
6199
6200/**
6201 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
6202 * @phba: pointer to lpfc hba data structure.
6203 *
6204 * This routine is invoked to allocate a single 4KB memory region to
6205 * support rpis and stores them in the phba. This single region
6206 * provides support for up to 64 rpis. The region is used globally
6207 * by the device.
6208 *
6209 * Returns:
6210 * A valid rpi hdr on success.
6211 * A NULL pointer on any failure.
6212 **/
6213struct lpfc_rpi_hdr *
6214lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
6215{
6216 uint16_t rpi_limit, curr_rpi_range;
6217 struct lpfc_dmabuf *dmabuf;
6218 struct lpfc_rpi_hdr *rpi_hdr;
James Smart9589b0622011-04-16 11:03:17 -04006219 uint32_t rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04006220
James Smart6d368e52011-05-24 11:44:12 -04006221 /*
6222 * If the SLI4 port supports extents, posting the rpi header isn't
6223 * required. Set the expected maximum count and let the actual value
6224 * get set when extents are fully allocated.
6225 */
6226 if (!phba->sli4_hba.rpi_hdrs_in_use)
6227 return NULL;
6228 if (phba->sli4_hba.extents_in_use)
6229 return NULL;
6230
6231 /* The limit on the logical index is just the max_rpi count. */
James Smartda0436e2009-05-22 14:51:39 -04006232 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
James Smart6d368e52011-05-24 11:44:12 -04006233 phba->sli4_hba.max_cfg_param.max_rpi - 1;
James Smartda0436e2009-05-22 14:51:39 -04006234
6235 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04006236 /*
6237 * Establish the starting RPI in this header block. The starting
6238 * rpi is normalized to a zero base because the physical rpi is
6239 * port based.
6240 */
James Smart97f2ecf2012-03-01 22:35:23 -05006241 curr_rpi_range = phba->sli4_hba.next_rpi;
James Smartda0436e2009-05-22 14:51:39 -04006242 spin_unlock_irq(&phba->hbalock);
6243
6244 /*
6245 * The port has a limited number of rpis. The increment here
6246 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
6247 * and to allow the full max_rpi range per port.
6248 */
6249 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
James Smart9589b0622011-04-16 11:03:17 -04006250 rpi_count = rpi_limit - curr_rpi_range;
6251 else
6252 rpi_count = LPFC_RPI_HDR_COUNT;
James Smartda0436e2009-05-22 14:51:39 -04006253
James Smart6d368e52011-05-24 11:44:12 -04006254 if (!rpi_count)
6255 return NULL;
James Smartda0436e2009-05-22 14:51:39 -04006256 /*
6257 * First allocate the protocol header region for the port. The
6258 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
6259 */
6260 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
6261 if (!dmabuf)
6262 return NULL;
6263
Joe Perches1aee3832014-09-03 12:56:12 -04006264 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
6265 LPFC_HDR_TEMPLATE_SIZE,
6266 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04006267 if (!dmabuf->virt) {
6268 rpi_hdr = NULL;
6269 goto err_free_dmabuf;
6270 }
6271
James Smartda0436e2009-05-22 14:51:39 -04006272 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
6273 rpi_hdr = NULL;
6274 goto err_free_coherent;
6275 }
6276
6277 /* Save the rpi header data for cleanup later. */
6278 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
6279 if (!rpi_hdr)
6280 goto err_free_coherent;
6281
6282 rpi_hdr->dmabuf = dmabuf;
6283 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
6284 rpi_hdr->page_count = 1;
6285 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04006286
6287 /* The rpi_hdr stores the logical index only. */
6288 rpi_hdr->start_rpi = curr_rpi_range;
James Smartda0436e2009-05-22 14:51:39 -04006289 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
6290
6291 /*
James Smart6d368e52011-05-24 11:44:12 -04006292 * The next_rpi stores the next logical module-64 rpi value used
6293 * to post physical rpis in subsequent rpi postings.
James Smartda0436e2009-05-22 14:51:39 -04006294 */
James Smart9589b0622011-04-16 11:03:17 -04006295 phba->sli4_hba.next_rpi += rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04006296 spin_unlock_irq(&phba->hbalock);
6297 return rpi_hdr;
6298
6299 err_free_coherent:
6300 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
6301 dmabuf->virt, dmabuf->phys);
6302 err_free_dmabuf:
6303 kfree(dmabuf);
6304 return NULL;
6305}
6306
6307/**
6308 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
6309 * @phba: pointer to lpfc hba data structure.
6310 *
6311 * This routine is invoked to remove all memory resources allocated
James Smart6d368e52011-05-24 11:44:12 -04006312 * to support rpis for SLI4 ports not supporting extents. This routine
6313 * presumes the caller has released all rpis consumed by fabric or port
6314 * logins and is prepared to have the header pages removed.
James Smartda0436e2009-05-22 14:51:39 -04006315 **/
6316void
6317lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
6318{
6319 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
6320
James Smart6d368e52011-05-24 11:44:12 -04006321 if (!phba->sli4_hba.rpi_hdrs_in_use)
6322 goto exit;
6323
James Smartda0436e2009-05-22 14:51:39 -04006324 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
6325 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
6326 list_del(&rpi_hdr->list);
6327 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
6328 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
6329 kfree(rpi_hdr->dmabuf);
6330 kfree(rpi_hdr);
6331 }
James Smart6d368e52011-05-24 11:44:12 -04006332 exit:
6333 /* There are no rpis available to the port now. */
6334 phba->sli4_hba.next_rpi = 0;
James Smartda0436e2009-05-22 14:51:39 -04006335}
6336
6337/**
James Smart3772a992009-05-22 14:50:54 -04006338 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
6339 * @pdev: pointer to pci device data structure.
6340 *
6341 * This routine is invoked to allocate the driver hba data structure for an
6342 * HBA device. If the allocation is successful, the phba reference to the
6343 * PCI device data structure is set.
6344 *
6345 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006346 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04006347 * NULL - error
6348 **/
6349static struct lpfc_hba *
6350lpfc_hba_alloc(struct pci_dev *pdev)
6351{
6352 struct lpfc_hba *phba;
6353
6354 /* Allocate memory for HBA structure */
6355 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
6356 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02006357 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04006358 return NULL;
6359 }
6360
6361 /* Set reference to PCI device in HBA structure */
6362 phba->pcidev = pdev;
6363
6364 /* Assign an unused board number */
6365 phba->brd_no = lpfc_get_instance();
6366 if (phba->brd_no < 0) {
6367 kfree(phba);
6368 return NULL;
6369 }
James Smart65791f12016-07-06 12:35:56 -07006370 phba->eratt_poll_interval = LPFC_ERATT_POLL_INTERVAL;
James Smart3772a992009-05-22 14:50:54 -04006371
James Smart4fede782010-01-26 23:08:55 -05006372 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04006373 INIT_LIST_HEAD(&phba->ct_ev_waiters);
6374
James Smart3772a992009-05-22 14:50:54 -04006375 return phba;
6376}
6377
6378/**
6379 * lpfc_hba_free - Free driver hba data structure with a device.
6380 * @phba: pointer to lpfc hba data structure.
6381 *
6382 * This routine is invoked to free the driver hba data structure with an
6383 * HBA device.
6384 **/
6385static void
6386lpfc_hba_free(struct lpfc_hba *phba)
6387{
6388 /* Release the driver assigned board number */
6389 idr_remove(&lpfc_hba_index, phba->brd_no);
6390
James Smart895427b2017-02-12 13:52:30 -08006391 /* Free memory allocated with sli3 rings */
6392 kfree(phba->sli.sli3_ring);
6393 phba->sli.sli3_ring = NULL;
James Smart2a76a282012-08-03 12:35:54 -04006394
James Smart3772a992009-05-22 14:50:54 -04006395 kfree(phba);
6396 return;
6397}
6398
6399/**
6400 * lpfc_create_shost - Create hba physical port with associated scsi host.
6401 * @phba: pointer to lpfc hba data structure.
6402 *
6403 * This routine is invoked to create HBA physical port and associate a SCSI
6404 * host with it.
6405 *
6406 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006407 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006408 * other values - error
6409 **/
6410static int
6411lpfc_create_shost(struct lpfc_hba *phba)
6412{
6413 struct lpfc_vport *vport;
6414 struct Scsi_Host *shost;
6415
6416 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05006417 phba->fc_edtov = FF_DEF_EDTOV;
6418 phba->fc_ratov = FF_DEF_RATOV;
6419 phba->fc_altov = FF_DEF_ALTOV;
6420 phba->fc_arbtov = FF_DEF_ARBTOV;
6421
James Smartd7c47992010-06-08 18:31:54 -04006422 atomic_set(&phba->sdev_cnt, 0);
James Smart3de2a652007-08-02 11:09:59 -04006423 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05006424 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04006425 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05006426
6427 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05006428 phba->pport = vport;
James Smart2ea259e2017-02-12 13:52:27 -08006429
James Smart858c9f62007-06-17 19:56:39 -05006430 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04006431 /* Put reference to SCSI host to driver's device private data */
6432 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05006433
James Smart4258e982015-12-16 18:11:58 -05006434 /*
6435 * At this point we are fully registered with PSA. In addition,
6436 * any initial discovery should be completed.
6437 */
6438 vport->load_flag |= FC_ALLOW_FDMI;
James Smart8663cbb2016-03-31 14:12:33 -07006439 if (phba->cfg_enable_SmartSAN ||
6440 (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
James Smart4258e982015-12-16 18:11:58 -05006441
6442 /* Setup appropriate attribute masks */
6443 vport->fdmi_hba_mask = LPFC_FDMI2_HBA_ATTR;
James Smart8663cbb2016-03-31 14:12:33 -07006444 if (phba->cfg_enable_SmartSAN)
James Smart4258e982015-12-16 18:11:58 -05006445 vport->fdmi_port_mask = LPFC_FDMI2_SMART_ATTR;
6446 else
6447 vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
6448 }
James Smart3772a992009-05-22 14:50:54 -04006449 return 0;
6450}
dea31012005-04-17 16:05:31 -05006451
James Smart3772a992009-05-22 14:50:54 -04006452/**
6453 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
6454 * @phba: pointer to lpfc hba data structure.
6455 *
6456 * This routine is invoked to destroy HBA physical port and the associated
6457 * SCSI host.
6458 **/
6459static void
6460lpfc_destroy_shost(struct lpfc_hba *phba)
6461{
6462 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04006463
James Smart3772a992009-05-22 14:50:54 -04006464 /* Destroy physical port that associated with the SCSI host */
6465 destroy_port(vport);
6466
6467 return;
6468}
6469
6470/**
6471 * lpfc_setup_bg - Setup Block guard structures and debug areas.
6472 * @phba: pointer to lpfc hba data structure.
6473 * @shost: the shost to be used to detect Block guard settings.
6474 *
6475 * This routine sets up the local Block guard protocol settings for @shost.
6476 * This routine also allocates memory for debugging bg buffers.
6477 **/
6478static void
6479lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
6480{
James Smartbbeb79b2012-06-12 13:54:27 -04006481 uint32_t old_mask;
6482 uint32_t old_guard;
6483
James Smart3772a992009-05-22 14:50:54 -04006484 int pagecnt = 10;
James Smartb3b98b72016-10-13 15:06:06 -07006485 if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
James Smart3772a992009-05-22 14:50:54 -04006486 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6487 "1478 Registering BlockGuard with the "
6488 "SCSI layer\n");
James Smartbbeb79b2012-06-12 13:54:27 -04006489
James Smartb3b98b72016-10-13 15:06:06 -07006490 old_mask = phba->cfg_prot_mask;
6491 old_guard = phba->cfg_prot_guard;
James Smartbbeb79b2012-06-12 13:54:27 -04006492
6493 /* Only allow supported values */
James Smartb3b98b72016-10-13 15:06:06 -07006494 phba->cfg_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
James Smartbbeb79b2012-06-12 13:54:27 -04006495 SHOST_DIX_TYPE0_PROTECTION |
6496 SHOST_DIX_TYPE1_PROTECTION);
James Smartb3b98b72016-10-13 15:06:06 -07006497 phba->cfg_prot_guard &= (SHOST_DIX_GUARD_IP |
6498 SHOST_DIX_GUARD_CRC);
James Smartbbeb79b2012-06-12 13:54:27 -04006499
6500 /* DIF Type 1 protection for profiles AST1/C1 is end to end */
James Smartb3b98b72016-10-13 15:06:06 -07006501 if (phba->cfg_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
6502 phba->cfg_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
James Smartbbeb79b2012-06-12 13:54:27 -04006503
James Smartb3b98b72016-10-13 15:06:06 -07006504 if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
6505 if ((old_mask != phba->cfg_prot_mask) ||
6506 (old_guard != phba->cfg_prot_guard))
James Smartbbeb79b2012-06-12 13:54:27 -04006507 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6508 "1475 Registering BlockGuard with the "
6509 "SCSI layer: mask %d guard %d\n",
James Smartb3b98b72016-10-13 15:06:06 -07006510 phba->cfg_prot_mask,
6511 phba->cfg_prot_guard);
James Smartbbeb79b2012-06-12 13:54:27 -04006512
James Smartb3b98b72016-10-13 15:06:06 -07006513 scsi_host_set_prot(shost, phba->cfg_prot_mask);
6514 scsi_host_set_guard(shost, phba->cfg_prot_guard);
James Smartbbeb79b2012-06-12 13:54:27 -04006515 } else
6516 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6517 "1479 Not Registering BlockGuard with the SCSI "
6518 "layer, Bad protection parameters: %d %d\n",
6519 old_mask, old_guard);
James Smart98c9ea52007-10-27 13:37:33 -04006520 }
James Smartbbeb79b2012-06-12 13:54:27 -04006521
James Smart81301a92008-12-04 22:39:46 -05006522 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05006523 while (pagecnt) {
6524 spin_lock_init(&_dump_buf_lock);
6525 _dump_buf_data =
6526 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
6527 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04006528 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6529 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04006530 "_dump_buf_data at 0x%p\n",
6531 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05006532 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04006533 memset(_dump_buf_data, 0,
6534 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05006535 break;
James Smart3772a992009-05-22 14:50:54 -04006536 } else
James Smart81301a92008-12-04 22:39:46 -05006537 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05006538 }
James Smart81301a92008-12-04 22:39:46 -05006539 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04006540 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6541 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04006542 "memory for hexdump\n");
6543 } else
James Smart6a9c52c2009-10-02 15:16:51 -04006544 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6545 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05006546 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05006547 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05006548 while (pagecnt) {
6549 _dump_buf_dif =
6550 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
6551 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04006552 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6553 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04006554 "_dump_buf_dif at 0x%p\n",
6555 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05006556 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04006557 memset(_dump_buf_dif, 0,
6558 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05006559 break;
James Smart3772a992009-05-22 14:50:54 -04006560 } else
James Smart81301a92008-12-04 22:39:46 -05006561 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05006562 }
James Smart81301a92008-12-04 22:39:46 -05006563 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04006564 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6565 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04006566 "memory for hexdump\n");
6567 } else
James Smart6a9c52c2009-10-02 15:16:51 -04006568 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6569 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04006570 _dump_buf_dif);
6571}
6572
6573/**
6574 * lpfc_post_init_setup - Perform necessary device post initialization setup.
6575 * @phba: pointer to lpfc hba data structure.
6576 *
6577 * This routine is invoked to perform all the necessary post initialization
6578 * setup for the device.
6579 **/
6580static void
6581lpfc_post_init_setup(struct lpfc_hba *phba)
6582{
6583 struct Scsi_Host *shost;
6584 struct lpfc_adapter_event_header adapter_event;
6585
6586 /* Get the default values for Model Name and Description */
6587 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
6588
6589 /*
6590 * hba setup may have changed the hba_queue_depth so we need to
6591 * adjust the value of can_queue.
6592 */
6593 shost = pci_get_drvdata(phba->pcidev);
6594 shost->can_queue = phba->cfg_hba_queue_depth - 10;
6595 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
6596 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05006597
6598 lpfc_host_attrib_init(shost);
6599
James Smart2e0fef82007-06-17 19:56:36 -05006600 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
6601 spin_lock_irq(shost->host_lock);
6602 lpfc_poll_start_timer(phba);
6603 spin_unlock_irq(shost->host_lock);
6604 }
James Smart8f6d98d2006-08-01 07:34:00 -04006605
James Smart93996272008-08-24 21:50:30 -04006606 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6607 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04006608 /* Send board arrival event to upper layer */
6609 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
6610 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
6611 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04006612 sizeof(adapter_event),
6613 (char *) &adapter_event,
6614 LPFC_NL_VENDOR_ID);
6615 return;
6616}
6617
6618/**
6619 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
6620 * @phba: pointer to lpfc hba data structure.
6621 *
6622 * This routine is invoked to set up the PCI device memory space for device
6623 * with SLI-3 interface spec.
6624 *
6625 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006626 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006627 * other values - error
6628 **/
6629static int
6630lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
6631{
6632 struct pci_dev *pdev;
6633 unsigned long bar0map_len, bar2map_len;
6634 int i, hbq_count;
6635 void *ptr;
6636 int error = -ENODEV;
6637
6638 /* Obtain PCI device reference */
6639 if (!phba->pcidev)
6640 return error;
6641 else
6642 pdev = phba->pcidev;
6643
6644 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05006645 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
6646 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
6647 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
6648 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smart3772a992009-05-22 14:50:54 -04006649 return error;
Michael Reed8e685972009-09-18 12:02:05 -05006650 }
6651 }
James Smart3772a992009-05-22 14:50:54 -04006652
6653 /* Get the bus address of Bar0 and Bar2 and the number of bytes
6654 * required by each mapping.
6655 */
6656 phba->pci_bar0_map = pci_resource_start(pdev, 0);
6657 bar0map_len = pci_resource_len(pdev, 0);
6658
6659 phba->pci_bar2_map = pci_resource_start(pdev, 2);
6660 bar2map_len = pci_resource_len(pdev, 2);
6661
6662 /* Map HBA SLIM to a kernel virtual address. */
6663 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
6664 if (!phba->slim_memmap_p) {
6665 dev_printk(KERN_ERR, &pdev->dev,
6666 "ioremap failed for SLIM memory.\n");
6667 goto out;
6668 }
6669
6670 /* Map HBA Control Registers to a kernel virtual address. */
6671 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
6672 if (!phba->ctrl_regs_memmap_p) {
6673 dev_printk(KERN_ERR, &pdev->dev,
6674 "ioremap failed for HBA control registers.\n");
6675 goto out_iounmap_slim;
6676 }
6677
6678 /* Allocate memory for SLI-2 structures */
Joe Perches1aee3832014-09-03 12:56:12 -04006679 phba->slim2p.virt = dma_zalloc_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6680 &phba->slim2p.phys, GFP_KERNEL);
James Smart3772a992009-05-22 14:50:54 -04006681 if (!phba->slim2p.virt)
6682 goto out_iounmap;
6683
James Smart3772a992009-05-22 14:50:54 -04006684 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04006685 phba->mbox_ext = (phba->slim2p.virt +
6686 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04006687 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
6688 phba->IOCBs = (phba->slim2p.virt +
6689 offsetof(struct lpfc_sli2_slim, IOCBs));
6690
6691 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
6692 lpfc_sli_hbq_size(),
6693 &phba->hbqslimp.phys,
6694 GFP_KERNEL);
6695 if (!phba->hbqslimp.virt)
6696 goto out_free_slim;
6697
6698 hbq_count = lpfc_sli_hbq_count();
6699 ptr = phba->hbqslimp.virt;
6700 for (i = 0; i < hbq_count; ++i) {
6701 phba->hbqs[i].hbq_virt = ptr;
6702 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
6703 ptr += (lpfc_hbq_defs[i]->entry_count *
6704 sizeof(struct lpfc_hbq_entry));
6705 }
6706 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
6707 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
6708
6709 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
6710
James Smart3772a992009-05-22 14:50:54 -04006711 phba->MBslimaddr = phba->slim_memmap_p;
6712 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
6713 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
6714 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
6715 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04006716
dea31012005-04-17 16:05:31 -05006717 return 0;
6718
dea31012005-04-17 16:05:31 -05006719out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04006720 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6721 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05006722out_iounmap:
6723 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05006724out_iounmap_slim:
dea31012005-04-17 16:05:31 -05006725 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05006726out:
6727 return error;
6728}
6729
James Smarte59058c2008-08-24 21:49:00 -04006730/**
James Smart3772a992009-05-22 14:50:54 -04006731 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
6732 * @phba: pointer to lpfc hba data structure.
6733 *
6734 * This routine is invoked to unset the PCI device memory space for device
6735 * with SLI-3 interface spec.
6736 **/
6737static void
6738lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
6739{
6740 struct pci_dev *pdev;
6741
6742 /* Obtain PCI device reference */
6743 if (!phba->pcidev)
6744 return;
6745 else
6746 pdev = phba->pcidev;
6747
6748 /* Free coherent DMA memory allocated */
6749 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
6750 phba->hbqslimp.virt, phba->hbqslimp.phys);
6751 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6752 phba->slim2p.virt, phba->slim2p.phys);
6753
6754 /* I/O memory unmap */
6755 iounmap(phba->ctrl_regs_memmap_p);
6756 iounmap(phba->slim_memmap_p);
6757
6758 return;
6759}
6760
6761/**
James Smartda0436e2009-05-22 14:51:39 -04006762 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
6763 * @phba: pointer to lpfc hba data structure.
6764 *
6765 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
6766 * done and check status.
6767 *
6768 * Return 0 if successful, otherwise -ENODEV.
6769 **/
6770int
6771lpfc_sli4_post_status_check(struct lpfc_hba *phba)
6772{
James Smart2fcee4b2010-12-15 17:57:46 -05006773 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
6774 struct lpfc_register reg_data;
6775 int i, port_error = 0;
6776 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04006777
James Smart9940b972011-03-11 16:06:12 -05006778 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
6779 memset(&reg_data, 0, sizeof(reg_data));
James Smart2fcee4b2010-12-15 17:57:46 -05006780 if (!phba->sli4_hba.PSMPHRregaddr)
James Smartda0436e2009-05-22 14:51:39 -04006781 return -ENODEV;
6782
James Smartda0436e2009-05-22 14:51:39 -04006783 /* Wait up to 30 seconds for the SLI Port POST done and ready */
6784 for (i = 0; i < 3000; i++) {
James Smart9940b972011-03-11 16:06:12 -05006785 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
6786 &portsmphr_reg.word0) ||
6787 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
James Smart2fcee4b2010-12-15 17:57:46 -05006788 /* Port has a fatal POST error, break out */
James Smartda0436e2009-05-22 14:51:39 -04006789 port_error = -ENODEV;
6790 break;
6791 }
James Smart2fcee4b2010-12-15 17:57:46 -05006792 if (LPFC_POST_STAGE_PORT_READY ==
6793 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
James Smartda0436e2009-05-22 14:51:39 -04006794 break;
James Smartda0436e2009-05-22 14:51:39 -04006795 msleep(10);
6796 }
6797
James Smart2fcee4b2010-12-15 17:57:46 -05006798 /*
6799 * If there was a port error during POST, then don't proceed with
6800 * other register reads as the data may not be valid. Just exit.
6801 */
6802 if (port_error) {
James Smartda0436e2009-05-22 14:51:39 -04006803 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05006804 "1408 Port Failed POST - portsmphr=0x%x, "
6805 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
6806 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
6807 portsmphr_reg.word0,
6808 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
6809 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
6810 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
6811 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
6812 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
6813 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
6814 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
6815 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
6816 } else {
James Smart28baac72010-02-12 14:42:03 -05006817 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05006818 "2534 Device Info: SLIFamily=0x%x, "
6819 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
6820 "SLIHint_2=0x%x, FT=0x%x\n",
James Smart28baac72010-02-12 14:42:03 -05006821 bf_get(lpfc_sli_intf_sli_family,
6822 &phba->sli4_hba.sli_intf),
6823 bf_get(lpfc_sli_intf_slirev,
6824 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05006825 bf_get(lpfc_sli_intf_if_type,
James Smart28baac72010-02-12 14:42:03 -05006826 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05006827 bf_get(lpfc_sli_intf_sli_hint1,
6828 &phba->sli4_hba.sli_intf),
6829 bf_get(lpfc_sli_intf_sli_hint2,
6830 &phba->sli4_hba.sli_intf),
6831 bf_get(lpfc_sli_intf_func_type,
James Smart28baac72010-02-12 14:42:03 -05006832 &phba->sli4_hba.sli_intf));
James Smart2fcee4b2010-12-15 17:57:46 -05006833 /*
6834 * Check for other Port errors during the initialization
6835 * process. Fail the load if the port did not come up
6836 * correctly.
6837 */
6838 if_type = bf_get(lpfc_sli_intf_if_type,
6839 &phba->sli4_hba.sli_intf);
6840 switch (if_type) {
6841 case LPFC_SLI_INTF_IF_TYPE_0:
6842 phba->sli4_hba.ue_mask_lo =
6843 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
6844 phba->sli4_hba.ue_mask_hi =
6845 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
6846 uerrlo_reg.word0 =
6847 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
6848 uerrhi_reg.word0 =
6849 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
6850 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
6851 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
6852 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6853 "1422 Unrecoverable Error "
6854 "Detected during POST "
6855 "uerr_lo_reg=0x%x, "
6856 "uerr_hi_reg=0x%x, "
6857 "ue_mask_lo_reg=0x%x, "
6858 "ue_mask_hi_reg=0x%x\n",
6859 uerrlo_reg.word0,
6860 uerrhi_reg.word0,
6861 phba->sli4_hba.ue_mask_lo,
6862 phba->sli4_hba.ue_mask_hi);
6863 port_error = -ENODEV;
6864 }
6865 break;
6866 case LPFC_SLI_INTF_IF_TYPE_2:
6867 /* Final checks. The port status should be clean. */
James Smart9940b972011-03-11 16:06:12 -05006868 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
6869 &reg_data.word0) ||
James Smart05580562011-05-24 11:40:48 -04006870 (bf_get(lpfc_sliport_status_err, &reg_data) &&
6871 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
James Smart2fcee4b2010-12-15 17:57:46 -05006872 phba->work_status[0] =
6873 readl(phba->sli4_hba.u.if_type2.
6874 ERR1regaddr);
6875 phba->work_status[1] =
6876 readl(phba->sli4_hba.u.if_type2.
6877 ERR2regaddr);
6878 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05006879 "2888 Unrecoverable port error "
6880 "following POST: port status reg "
6881 "0x%x, port_smphr reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05006882 "error 1=0x%x, error 2=0x%x\n",
6883 reg_data.word0,
6884 portsmphr_reg.word0,
6885 phba->work_status[0],
6886 phba->work_status[1]);
6887 port_error = -ENODEV;
6888 }
6889 break;
6890 case LPFC_SLI_INTF_IF_TYPE_1:
6891 default:
6892 break;
6893 }
James Smart28baac72010-02-12 14:42:03 -05006894 }
James Smartda0436e2009-05-22 14:51:39 -04006895 return port_error;
6896}
6897
6898/**
6899 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
6900 * @phba: pointer to lpfc hba data structure.
James Smart2fcee4b2010-12-15 17:57:46 -05006901 * @if_type: The SLI4 interface type getting configured.
James Smartda0436e2009-05-22 14:51:39 -04006902 *
6903 * This routine is invoked to set up SLI4 BAR0 PCI config space register
6904 * memory map.
6905 **/
6906static void
James Smart2fcee4b2010-12-15 17:57:46 -05006907lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04006908{
James Smart2fcee4b2010-12-15 17:57:46 -05006909 switch (if_type) {
6910 case LPFC_SLI_INTF_IF_TYPE_0:
6911 phba->sli4_hba.u.if_type0.UERRLOregaddr =
6912 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
6913 phba->sli4_hba.u.if_type0.UERRHIregaddr =
6914 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
6915 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
6916 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
6917 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
6918 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
6919 phba->sli4_hba.SLIINTFregaddr =
6920 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
6921 break;
6922 case LPFC_SLI_INTF_IF_TYPE_2:
6923 phba->sli4_hba.u.if_type2.ERR1regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006924 phba->sli4_hba.conf_regs_memmap_p +
6925 LPFC_CTL_PORT_ER1_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006926 phba->sli4_hba.u.if_type2.ERR2regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006927 phba->sli4_hba.conf_regs_memmap_p +
6928 LPFC_CTL_PORT_ER2_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006929 phba->sli4_hba.u.if_type2.CTRLregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006930 phba->sli4_hba.conf_regs_memmap_p +
6931 LPFC_CTL_PORT_CTL_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006932 phba->sli4_hba.u.if_type2.STATUSregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006933 phba->sli4_hba.conf_regs_memmap_p +
6934 LPFC_CTL_PORT_STA_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006935 phba->sli4_hba.SLIINTFregaddr =
6936 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
6937 phba->sli4_hba.PSMPHRregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006938 phba->sli4_hba.conf_regs_memmap_p +
6939 LPFC_CTL_PORT_SEM_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006940 phba->sli4_hba.RQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05006941 phba->sli4_hba.conf_regs_memmap_p +
6942 LPFC_ULP0_RQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05006943 phba->sli4_hba.WQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05006944 phba->sli4_hba.conf_regs_memmap_p +
6945 LPFC_ULP0_WQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05006946 phba->sli4_hba.EQCQDBregaddr =
6947 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
6948 phba->sli4_hba.MQDBregaddr =
6949 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
6950 phba->sli4_hba.BMBXregaddr =
6951 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
6952 break;
6953 case LPFC_SLI_INTF_IF_TYPE_1:
6954 default:
6955 dev_printk(KERN_ERR, &phba->pcidev->dev,
6956 "FATAL - unsupported SLI4 interface type - %d\n",
6957 if_type);
6958 break;
6959 }
James Smartda0436e2009-05-22 14:51:39 -04006960}
6961
6962/**
6963 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
6964 * @phba: pointer to lpfc hba data structure.
6965 *
6966 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
6967 * memory map.
6968 **/
6969static void
6970lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
6971{
James Smart2fcee4b2010-12-15 17:57:46 -05006972 phba->sli4_hba.PSMPHRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
6973 LPFC_SLIPORT_IF0_SMPHR;
James Smartda0436e2009-05-22 14:51:39 -04006974 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006975 LPFC_HST_ISR0;
James Smartda0436e2009-05-22 14:51:39 -04006976 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006977 LPFC_HST_IMR0;
James Smartda0436e2009-05-22 14:51:39 -04006978 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006979 LPFC_HST_ISCR0;
James Smartda0436e2009-05-22 14:51:39 -04006980}
6981
6982/**
6983 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
6984 * @phba: pointer to lpfc hba data structure.
6985 * @vf: virtual function number
6986 *
6987 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
6988 * based on the given viftual function number, @vf.
6989 *
6990 * Return 0 if successful, otherwise -ENODEV.
6991 **/
6992static int
6993lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
6994{
6995 if (vf > LPFC_VIR_FUNC_MAX)
6996 return -ENODEV;
6997
6998 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05006999 vf * LPFC_VFR_PAGE_SIZE +
7000 LPFC_ULP0_RQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04007001 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05007002 vf * LPFC_VFR_PAGE_SIZE +
7003 LPFC_ULP0_WQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04007004 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7005 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
7006 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7007 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
7008 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7009 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
7010 return 0;
7011}
7012
7013/**
7014 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
7015 * @phba: pointer to lpfc hba data structure.
7016 *
7017 * This routine is invoked to create the bootstrap mailbox
7018 * region consistent with the SLI-4 interface spec. This
7019 * routine allocates all memory necessary to communicate
7020 * mailbox commands to the port and sets up all alignment
7021 * needs. No locks are expected to be held when calling
7022 * this routine.
7023 *
7024 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007025 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04007026 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04007027 **/
7028static int
7029lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
7030{
7031 uint32_t bmbx_size;
7032 struct lpfc_dmabuf *dmabuf;
7033 struct dma_address *dma_address;
7034 uint32_t pa_addr;
7035 uint64_t phys_addr;
7036
7037 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
7038 if (!dmabuf)
7039 return -ENOMEM;
7040
7041 /*
7042 * The bootstrap mailbox region is comprised of 2 parts
7043 * plus an alignment restriction of 16 bytes.
7044 */
7045 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
Joe Perches1aee3832014-09-03 12:56:12 -04007046 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, bmbx_size,
7047 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04007048 if (!dmabuf->virt) {
7049 kfree(dmabuf);
7050 return -ENOMEM;
7051 }
James Smartda0436e2009-05-22 14:51:39 -04007052
7053 /*
7054 * Initialize the bootstrap mailbox pointers now so that the register
7055 * operations are simple later. The mailbox dma address is required
7056 * to be 16-byte aligned. Also align the virtual memory as each
7057 * maibox is copied into the bmbx mailbox region before issuing the
7058 * command to the port.
7059 */
7060 phba->sli4_hba.bmbx.dmabuf = dmabuf;
7061 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
7062
7063 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
7064 LPFC_ALIGN_16_BYTE);
7065 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
7066 LPFC_ALIGN_16_BYTE);
7067
7068 /*
7069 * Set the high and low physical addresses now. The SLI4 alignment
7070 * requirement is 16 bytes and the mailbox is posted to the port
7071 * as two 30-bit addresses. The other data is a bit marking whether
7072 * the 30-bit address is the high or low address.
7073 * Upcast bmbx aphys to 64bits so shift instruction compiles
7074 * clean on 32 bit machines.
7075 */
7076 dma_address = &phba->sli4_hba.bmbx.dma_address;
7077 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
7078 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
7079 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
7080 LPFC_BMBX_BIT1_ADDR_HI);
7081
7082 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
7083 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
7084 LPFC_BMBX_BIT1_ADDR_LO);
7085 return 0;
7086}
7087
7088/**
7089 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
7090 * @phba: pointer to lpfc hba data structure.
7091 *
7092 * This routine is invoked to teardown the bootstrap mailbox
7093 * region and release all host resources. This routine requires
7094 * the caller to ensure all mailbox commands recovered, no
7095 * additional mailbox comands are sent, and interrupts are disabled
7096 * before calling this routine.
7097 *
7098 **/
7099static void
7100lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
7101{
7102 dma_free_coherent(&phba->pcidev->dev,
7103 phba->sli4_hba.bmbx.bmbx_size,
7104 phba->sli4_hba.bmbx.dmabuf->virt,
7105 phba->sli4_hba.bmbx.dmabuf->phys);
7106
7107 kfree(phba->sli4_hba.bmbx.dmabuf);
7108 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
7109}
7110
7111/**
7112 * lpfc_sli4_read_config - Get the config parameters.
7113 * @phba: pointer to lpfc hba data structure.
7114 *
7115 * This routine is invoked to read the configuration parameters from the HBA.
7116 * The configuration parameters are used to set the base and maximum values
7117 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
7118 * allocation for the port.
7119 *
7120 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007121 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007122 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007123 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007124 **/
James Smartff78d8f2011-12-13 13:21:35 -05007125int
James Smartda0436e2009-05-22 14:51:39 -04007126lpfc_sli4_read_config(struct lpfc_hba *phba)
7127{
7128 LPFC_MBOXQ_t *pmb;
7129 struct lpfc_mbx_read_config *rd_config;
James Smart912e3ac2011-05-24 11:42:11 -04007130 union lpfc_sli4_cfg_shdr *shdr;
7131 uint32_t shdr_status, shdr_add_status;
7132 struct lpfc_mbx_get_func_cfg *get_func_cfg;
7133 struct lpfc_rsrc_desc_fcfcoe *desc;
James Smart8aa134a2012-08-14 14:25:29 -04007134 char *pdesc_0;
James Smartc6918162016-10-13 15:06:16 -07007135 uint16_t forced_link_speed;
7136 uint32_t if_type;
James Smart8aa134a2012-08-14 14:25:29 -04007137 int length, i, rc = 0, rc2;
James Smartda0436e2009-05-22 14:51:39 -04007138
7139 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7140 if (!pmb) {
7141 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7142 "2011 Unable to allocate memory for issuing "
7143 "SLI_CONFIG_SPECIAL mailbox command\n");
7144 return -ENOMEM;
7145 }
7146
7147 lpfc_read_config(phba, pmb);
7148
7149 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
7150 if (rc != MBX_SUCCESS) {
7151 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7152 "2012 Mailbox failed , mbxCmd x%x "
7153 "READ_CONFIG, mbxStatus x%x\n",
7154 bf_get(lpfc_mqe_command, &pmb->u.mqe),
7155 bf_get(lpfc_mqe_status, &pmb->u.mqe));
7156 rc = -EIO;
7157 } else {
7158 rd_config = &pmb->u.mqe.un.rd_config;
James Smartff78d8f2011-12-13 13:21:35 -05007159 if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
7160 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
7161 phba->sli4_hba.lnk_info.lnk_tp =
7162 bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
7163 phba->sli4_hba.lnk_info.lnk_no =
7164 bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
7165 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7166 "3081 lnk_type:%d, lnk_numb:%d\n",
7167 phba->sli4_hba.lnk_info.lnk_tp,
7168 phba->sli4_hba.lnk_info.lnk_no);
7169 } else
7170 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
7171 "3082 Mailbox (x%x) returned ldv:x0\n",
7172 bf_get(lpfc_mqe_command, &pmb->u.mqe));
James Smart6d368e52011-05-24 11:44:12 -04007173 phba->sli4_hba.extents_in_use =
7174 bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04007175 phba->sli4_hba.max_cfg_param.max_xri =
7176 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
7177 phba->sli4_hba.max_cfg_param.xri_base =
7178 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
7179 phba->sli4_hba.max_cfg_param.max_vpi =
7180 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
7181 phba->sli4_hba.max_cfg_param.vpi_base =
7182 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
7183 phba->sli4_hba.max_cfg_param.max_rpi =
7184 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
7185 phba->sli4_hba.max_cfg_param.rpi_base =
7186 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
7187 phba->sli4_hba.max_cfg_param.max_vfi =
7188 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
7189 phba->sli4_hba.max_cfg_param.vfi_base =
7190 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
7191 phba->sli4_hba.max_cfg_param.max_fcfi =
7192 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04007193 phba->sli4_hba.max_cfg_param.max_eq =
7194 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
7195 phba->sli4_hba.max_cfg_param.max_rq =
7196 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
7197 phba->sli4_hba.max_cfg_param.max_wq =
7198 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
7199 phba->sli4_hba.max_cfg_param.max_cq =
7200 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
7201 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
7202 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
7203 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
7204 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
James Smart5ffc2662009-11-18 15:39:44 -05007205 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
7206 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04007207 phba->max_vports = phba->max_vpi;
7208 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart6d368e52011-05-24 11:44:12 -04007209 "2003 cfg params Extents? %d "
7210 "XRI(B:%d M:%d), "
James Smartda0436e2009-05-22 14:51:39 -04007211 "VPI(B:%d M:%d) "
7212 "VFI(B:%d M:%d) "
7213 "RPI(B:%d M:%d) "
James Smart2ea259e2017-02-12 13:52:27 -08007214 "FCFI:%d EQ:%d CQ:%d WQ:%d RQ:%d\n",
James Smart6d368e52011-05-24 11:44:12 -04007215 phba->sli4_hba.extents_in_use,
James Smartda0436e2009-05-22 14:51:39 -04007216 phba->sli4_hba.max_cfg_param.xri_base,
7217 phba->sli4_hba.max_cfg_param.max_xri,
7218 phba->sli4_hba.max_cfg_param.vpi_base,
7219 phba->sli4_hba.max_cfg_param.max_vpi,
7220 phba->sli4_hba.max_cfg_param.vfi_base,
7221 phba->sli4_hba.max_cfg_param.max_vfi,
7222 phba->sli4_hba.max_cfg_param.rpi_base,
7223 phba->sli4_hba.max_cfg_param.max_rpi,
James Smart2ea259e2017-02-12 13:52:27 -08007224 phba->sli4_hba.max_cfg_param.max_fcfi,
7225 phba->sli4_hba.max_cfg_param.max_eq,
7226 phba->sli4_hba.max_cfg_param.max_cq,
7227 phba->sli4_hba.max_cfg_param.max_wq,
7228 phba->sli4_hba.max_cfg_param.max_rq);
7229
James Smartda0436e2009-05-22 14:51:39 -04007230 }
James Smart912e3ac2011-05-24 11:42:11 -04007231
7232 if (rc)
7233 goto read_cfg_out;
James Smartda0436e2009-05-22 14:51:39 -04007234
James Smartc6918162016-10-13 15:06:16 -07007235 /* Update link speed if forced link speed is supported */
7236 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7237 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
7238 forced_link_speed =
7239 bf_get(lpfc_mbx_rd_conf_link_speed, rd_config);
7240 if (forced_link_speed) {
7241 phba->hba_flag |= HBA_FORCED_LINK_SPEED;
7242
7243 switch (forced_link_speed) {
7244 case LINK_SPEED_1G:
7245 phba->cfg_link_speed =
7246 LPFC_USER_LINK_SPEED_1G;
7247 break;
7248 case LINK_SPEED_2G:
7249 phba->cfg_link_speed =
7250 LPFC_USER_LINK_SPEED_2G;
7251 break;
7252 case LINK_SPEED_4G:
7253 phba->cfg_link_speed =
7254 LPFC_USER_LINK_SPEED_4G;
7255 break;
7256 case LINK_SPEED_8G:
7257 phba->cfg_link_speed =
7258 LPFC_USER_LINK_SPEED_8G;
7259 break;
7260 case LINK_SPEED_10G:
7261 phba->cfg_link_speed =
7262 LPFC_USER_LINK_SPEED_10G;
7263 break;
7264 case LINK_SPEED_16G:
7265 phba->cfg_link_speed =
7266 LPFC_USER_LINK_SPEED_16G;
7267 break;
7268 case LINK_SPEED_32G:
7269 phba->cfg_link_speed =
7270 LPFC_USER_LINK_SPEED_32G;
7271 break;
7272 case 0xffff:
7273 phba->cfg_link_speed =
7274 LPFC_USER_LINK_SPEED_AUTO;
7275 break;
7276 default:
7277 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7278 "0047 Unrecognized link "
7279 "speed : %d\n",
7280 forced_link_speed);
7281 phba->cfg_link_speed =
7282 LPFC_USER_LINK_SPEED_AUTO;
7283 }
7284 }
7285 }
7286
James Smartda0436e2009-05-22 14:51:39 -04007287 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -04007288 length = phba->sli4_hba.max_cfg_param.max_xri -
7289 lpfc_sli4_get_els_iocb_cnt(phba);
7290 if (phba->cfg_hba_queue_depth > length) {
7291 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7292 "3361 HBA queue depth changed from %d to %d\n",
7293 phba->cfg_hba_queue_depth, length);
7294 phba->cfg_hba_queue_depth = length;
7295 }
James Smart912e3ac2011-05-24 11:42:11 -04007296
7297 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
7298 LPFC_SLI_INTF_IF_TYPE_2)
7299 goto read_cfg_out;
7300
7301 /* get the pf# and vf# for SLI4 if_type 2 port */
7302 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
7303 sizeof(struct lpfc_sli4_cfg_mhdr));
7304 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
7305 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
7306 length, LPFC_SLI4_MBX_EMBED);
7307
James Smart8aa134a2012-08-14 14:25:29 -04007308 rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart912e3ac2011-05-24 11:42:11 -04007309 shdr = (union lpfc_sli4_cfg_shdr *)
7310 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
7311 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7312 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart8aa134a2012-08-14 14:25:29 -04007313 if (rc2 || shdr_status || shdr_add_status) {
James Smart912e3ac2011-05-24 11:42:11 -04007314 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7315 "3026 Mailbox failed , mbxCmd x%x "
7316 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
7317 bf_get(lpfc_mqe_command, &pmb->u.mqe),
7318 bf_get(lpfc_mqe_status, &pmb->u.mqe));
James Smart912e3ac2011-05-24 11:42:11 -04007319 goto read_cfg_out;
7320 }
7321
7322 /* search for fc_fcoe resrouce descriptor */
7323 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
James Smart912e3ac2011-05-24 11:42:11 -04007324
James Smart8aa134a2012-08-14 14:25:29 -04007325 pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
7326 desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
7327 length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
7328 if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
7329 length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
7330 else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
7331 goto read_cfg_out;
7332
James Smart912e3ac2011-05-24 11:42:11 -04007333 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
James Smart8aa134a2012-08-14 14:25:29 -04007334 desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
James Smart912e3ac2011-05-24 11:42:11 -04007335 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
James Smart8aa134a2012-08-14 14:25:29 -04007336 bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
James Smart912e3ac2011-05-24 11:42:11 -04007337 phba->sli4_hba.iov.pf_number =
7338 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
7339 phba->sli4_hba.iov.vf_number =
7340 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
7341 break;
7342 }
7343 }
7344
7345 if (i < LPFC_RSRC_DESC_MAX_NUM)
7346 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7347 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
7348 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
7349 phba->sli4_hba.iov.vf_number);
James Smart8aa134a2012-08-14 14:25:29 -04007350 else
James Smart912e3ac2011-05-24 11:42:11 -04007351 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7352 "3028 GET_FUNCTION_CONFIG: failed to find "
7353 "Resrouce Descriptor:x%x\n",
7354 LPFC_RSRC_DESC_TYPE_FCFCOE);
James Smart912e3ac2011-05-24 11:42:11 -04007355
7356read_cfg_out:
7357 mempool_free(pmb, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04007358 return rc;
7359}
7360
7361/**
James Smart2fcee4b2010-12-15 17:57:46 -05007362 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
James Smartda0436e2009-05-22 14:51:39 -04007363 * @phba: pointer to lpfc hba data structure.
7364 *
James Smart2fcee4b2010-12-15 17:57:46 -05007365 * This routine is invoked to setup the port-side endian order when
7366 * the port if_type is 0. This routine has no function for other
7367 * if_types.
James Smartda0436e2009-05-22 14:51:39 -04007368 *
7369 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007370 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007371 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007372 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007373 **/
7374static int
7375lpfc_setup_endian_order(struct lpfc_hba *phba)
7376{
7377 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05007378 uint32_t if_type, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04007379 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
7380 HOST_ENDIAN_HIGH_WORD1};
7381
James Smart2fcee4b2010-12-15 17:57:46 -05007382 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7383 switch (if_type) {
7384 case LPFC_SLI_INTF_IF_TYPE_0:
7385 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
7386 GFP_KERNEL);
7387 if (!mboxq) {
7388 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7389 "0492 Unable to allocate memory for "
7390 "issuing SLI_CONFIG_SPECIAL mailbox "
7391 "command\n");
7392 return -ENOMEM;
7393 }
James Smartda0436e2009-05-22 14:51:39 -04007394
James Smart2fcee4b2010-12-15 17:57:46 -05007395 /*
7396 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
7397 * two words to contain special data values and no other data.
7398 */
7399 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
7400 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
7401 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7402 if (rc != MBX_SUCCESS) {
7403 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7404 "0493 SLI_CONFIG_SPECIAL mailbox "
7405 "failed with status x%x\n",
7406 rc);
7407 rc = -EIO;
7408 }
7409 mempool_free(mboxq, phba->mbox_mem_pool);
7410 break;
7411 case LPFC_SLI_INTF_IF_TYPE_2:
7412 case LPFC_SLI_INTF_IF_TYPE_1:
7413 default:
7414 break;
James Smartda0436e2009-05-22 14:51:39 -04007415 }
James Smartda0436e2009-05-22 14:51:39 -04007416 return rc;
7417}
7418
7419/**
James Smart895427b2017-02-12 13:52:30 -08007420 * lpfc_sli4_queue_verify - Verify and update EQ counts
James Smartda0436e2009-05-22 14:51:39 -04007421 * @phba: pointer to lpfc hba data structure.
7422 *
James Smart895427b2017-02-12 13:52:30 -08007423 * This routine is invoked to check the user settable queue counts for EQs.
7424 * After this routine is called the counts will be set to valid values that
James Smart5350d872011-10-10 21:33:49 -04007425 * adhere to the constraints of the system's interrupt vectors and the port's
7426 * queue resources.
James Smartda0436e2009-05-22 14:51:39 -04007427 *
7428 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007429 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007430 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04007431 **/
7432static int
James Smart5350d872011-10-10 21:33:49 -04007433lpfc_sli4_queue_verify(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04007434{
James Smart895427b2017-02-12 13:52:30 -08007435 int io_channel;
James Smart1ba981f2014-02-20 09:56:45 -05007436 int fof_vectors = phba->cfg_fof ? 1 : 0;
James Smartda0436e2009-05-22 14:51:39 -04007437
7438 /*
James Smart67d12732012-08-03 12:36:13 -04007439 * Sanity check for configured queue parameters against the run-time
James Smartda0436e2009-05-22 14:51:39 -04007440 * device parameters
7441 */
7442
James Smart67d12732012-08-03 12:36:13 -04007443 /* Sanity check on HBA EQ parameters */
James Smart895427b2017-02-12 13:52:30 -08007444 io_channel = phba->io_channel_irqs;
James Smartda0436e2009-05-22 14:51:39 -04007445
James Smart895427b2017-02-12 13:52:30 -08007446 if (phba->sli4_hba.num_online_cpu < io_channel) {
James Smart82c3e9b2012-09-29 11:29:50 -04007447 lpfc_printf_log(phba,
7448 KERN_ERR, LOG_INIT,
James Smart90695ee2012-08-14 14:25:36 -04007449 "3188 Reducing IO channels to match number of "
James Smart7bb03bb2013-04-17 20:19:16 -04007450 "online CPUs: from %d to %d\n",
James Smart895427b2017-02-12 13:52:30 -08007451 io_channel, phba->sli4_hba.num_online_cpu);
7452 io_channel = phba->sli4_hba.num_online_cpu;
James Smart90695ee2012-08-14 14:25:36 -04007453 }
7454
James Smart895427b2017-02-12 13:52:30 -08007455 if (io_channel + fof_vectors > phba->sli4_hba.max_cfg_param.max_eq) {
James Smart82c3e9b2012-09-29 11:29:50 -04007456 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7457 "2575 Reducing IO channels to match number of "
7458 "available EQs: from %d to %d\n",
James Smart895427b2017-02-12 13:52:30 -08007459 io_channel,
James Smart82c3e9b2012-09-29 11:29:50 -04007460 phba->sli4_hba.max_cfg_param.max_eq);
James Smart895427b2017-02-12 13:52:30 -08007461 io_channel = phba->sli4_hba.max_cfg_param.max_eq - fof_vectors;
James Smartda0436e2009-05-22 14:51:39 -04007462 }
James Smart67d12732012-08-03 12:36:13 -04007463
James Smart895427b2017-02-12 13:52:30 -08007464 /* The actual number of FCP / NVME event queues adopted */
7465 if (io_channel != phba->io_channel_irqs)
7466 phba->io_channel_irqs = io_channel;
7467 if (phba->cfg_fcp_io_channel > io_channel)
7468 phba->cfg_fcp_io_channel = io_channel;
7469 if (phba->cfg_nvme_io_channel > io_channel)
7470 phba->cfg_nvme_io_channel = io_channel;
7471
7472 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7473 "2574 IRQs: %d, IO Channels: fcp %d nvme %d\n",
7474 phba->io_channel_irqs, phba->cfg_fcp_io_channel,
7475 phba->cfg_nvme_io_channel);
James Smartda0436e2009-05-22 14:51:39 -04007476
James Smartda0436e2009-05-22 14:51:39 -04007477 /* Get EQ depth from module parameter, fake the default for now */
7478 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
7479 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
7480
James Smart5350d872011-10-10 21:33:49 -04007481 /* Get CQ depth from module parameter, fake the default for now */
7482 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
7483 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
James Smart5350d872011-10-10 21:33:49 -04007484 return 0;
James Smart895427b2017-02-12 13:52:30 -08007485}
7486
7487static int
7488lpfc_alloc_nvme_wq_cq(struct lpfc_hba *phba, int wqidx)
7489{
7490 struct lpfc_queue *qdesc;
7491 int cnt;
7492
7493 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7494 phba->sli4_hba.cq_ecount);
7495 if (!qdesc) {
7496 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7497 "0508 Failed allocate fast-path NVME CQ (%d)\n",
7498 wqidx);
7499 return 1;
7500 }
7501 phba->sli4_hba.nvme_cq[wqidx] = qdesc;
7502
7503 cnt = LPFC_NVME_WQSIZE;
7504 qdesc = lpfc_sli4_queue_alloc(phba, LPFC_WQE128_SIZE, cnt);
7505 if (!qdesc) {
7506 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7507 "0509 Failed allocate fast-path NVME WQ (%d)\n",
7508 wqidx);
7509 return 1;
7510 }
7511 phba->sli4_hba.nvme_wq[wqidx] = qdesc;
7512 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
7513 return 0;
7514}
7515
7516static int
7517lpfc_alloc_fcp_wq_cq(struct lpfc_hba *phba, int wqidx)
7518{
7519 struct lpfc_queue *qdesc;
7520 uint32_t wqesize;
7521
7522 /* Create Fast Path FCP CQs */
7523 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7524 phba->sli4_hba.cq_ecount);
7525 if (!qdesc) {
7526 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7527 "0499 Failed allocate fast-path FCP CQ (%d)\n", wqidx);
7528 return 1;
7529 }
7530 phba->sli4_hba.fcp_cq[wqidx] = qdesc;
7531
7532 /* Create Fast Path FCP WQs */
7533 wqesize = (phba->fcp_embed_io) ?
7534 LPFC_WQE128_SIZE : phba->sli4_hba.wq_esize;
7535 qdesc = lpfc_sli4_queue_alloc(phba, wqesize, phba->sli4_hba.wq_ecount);
7536 if (!qdesc) {
7537 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7538 "0503 Failed allocate fast-path FCP WQ (%d)\n",
7539 wqidx);
7540 return 1;
7541 }
7542 phba->sli4_hba.fcp_wq[wqidx] = qdesc;
7543 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
7544 return 0;
James Smart5350d872011-10-10 21:33:49 -04007545}
7546
7547/**
7548 * lpfc_sli4_queue_create - Create all the SLI4 queues
7549 * @phba: pointer to lpfc hba data structure.
7550 *
7551 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
7552 * operation. For each SLI4 queue type, the parameters such as queue entry
7553 * count (queue depth) shall be taken from the module parameter. For now,
7554 * we just use some constant number as place holder.
7555 *
7556 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07007557 * 0 - successful
James Smart5350d872011-10-10 21:33:49 -04007558 * -ENOMEM - No availble memory
7559 * -EIO - The mailbox failed to complete successfully.
7560 **/
7561int
7562lpfc_sli4_queue_create(struct lpfc_hba *phba)
7563{
7564 struct lpfc_queue *qdesc;
James Smart895427b2017-02-12 13:52:30 -08007565 int idx, io_channel;
James Smart5350d872011-10-10 21:33:49 -04007566
7567 /*
James Smart67d12732012-08-03 12:36:13 -04007568 * Create HBA Record arrays.
James Smart895427b2017-02-12 13:52:30 -08007569 * Both NVME and FCP will share that same vectors / EQs
James Smart5350d872011-10-10 21:33:49 -04007570 */
James Smart895427b2017-02-12 13:52:30 -08007571 io_channel = phba->io_channel_irqs;
7572 if (!io_channel)
James Smart67d12732012-08-03 12:36:13 -04007573 return -ERANGE;
James Smart5350d872011-10-10 21:33:49 -04007574
James Smart67d12732012-08-03 12:36:13 -04007575 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
7576 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
7577 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
7578 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
7579 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
7580 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
James Smart895427b2017-02-12 13:52:30 -08007581 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
7582 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
7583 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
7584 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
James Smart67d12732012-08-03 12:36:13 -04007585
James Smart895427b2017-02-12 13:52:30 -08007586 phba->sli4_hba.hba_eq = kcalloc(io_channel,
7587 sizeof(struct lpfc_queue *),
7588 GFP_KERNEL);
James Smart67d12732012-08-03 12:36:13 -04007589 if (!phba->sli4_hba.hba_eq) {
James Smartda0436e2009-05-22 14:51:39 -04007590 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04007591 "2576 Failed allocate memory for "
7592 "fast-path EQ record array\n");
James Smartda0436e2009-05-22 14:51:39 -04007593 goto out_error;
7594 }
James Smart67d12732012-08-03 12:36:13 -04007595
James Smart895427b2017-02-12 13:52:30 -08007596 if (phba->cfg_fcp_io_channel) {
7597 phba->sli4_hba.fcp_cq = kcalloc(phba->cfg_fcp_io_channel,
7598 sizeof(struct lpfc_queue *),
7599 GFP_KERNEL);
7600 if (!phba->sli4_hba.fcp_cq) {
7601 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7602 "2577 Failed allocate memory for "
7603 "fast-path CQ record array\n");
7604 goto out_error;
7605 }
7606 phba->sli4_hba.fcp_wq = kcalloc(phba->cfg_fcp_io_channel,
7607 sizeof(struct lpfc_queue *),
7608 GFP_KERNEL);
7609 if (!phba->sli4_hba.fcp_wq) {
7610 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7611 "2578 Failed allocate memory for "
7612 "fast-path FCP WQ record array\n");
7613 goto out_error;
7614 }
7615 /*
7616 * Since the first EQ can have multiple CQs associated with it,
7617 * this array is used to quickly see if we have a FCP fast-path
7618 * CQ match.
7619 */
7620 phba->sli4_hba.fcp_cq_map = kcalloc(phba->cfg_fcp_io_channel,
7621 sizeof(uint16_t),
7622 GFP_KERNEL);
7623 if (!phba->sli4_hba.fcp_cq_map) {
7624 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7625 "2545 Failed allocate memory for "
7626 "fast-path CQ map\n");
7627 goto out_error;
7628 }
James Smart67d12732012-08-03 12:36:13 -04007629 }
7630
James Smart895427b2017-02-12 13:52:30 -08007631 if (phba->cfg_nvme_io_channel) {
7632 phba->sli4_hba.nvme_cq = kcalloc(phba->cfg_nvme_io_channel,
7633 sizeof(struct lpfc_queue *),
7634 GFP_KERNEL);
7635 if (!phba->sli4_hba.nvme_cq) {
7636 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7637 "6077 Failed allocate memory for "
7638 "fast-path CQ record array\n");
7639 goto out_error;
7640 }
7641
7642
7643 phba->sli4_hba.nvme_wq = kcalloc(phba->cfg_nvme_io_channel,
7644 sizeof(struct lpfc_queue *),
7645 GFP_KERNEL);
7646 if (!phba->sli4_hba.nvme_wq) {
7647 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7648 "2581 Failed allocate memory for "
7649 "fast-path NVME WQ record array\n");
7650 goto out_error;
7651 }
7652
7653 /*
7654 * Since the first EQ can have multiple CQs associated with it,
7655 * this array is used to quickly see if we have a NVME fast-path
7656 * CQ match.
7657 */
7658 phba->sli4_hba.nvme_cq_map = kcalloc(phba->cfg_nvme_io_channel,
7659 sizeof(uint16_t),
7660 GFP_KERNEL);
7661 if (!phba->sli4_hba.nvme_cq_map) {
7662 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7663 "6078 Failed allocate memory for "
7664 "fast-path CQ map\n");
7665 goto out_error;
7666 }
James Smart67d12732012-08-03 12:36:13 -04007667 }
James Smartda0436e2009-05-22 14:51:39 -04007668
James Smart895427b2017-02-12 13:52:30 -08007669 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
James Smart67d12732012-08-03 12:36:13 -04007670
James Smart895427b2017-02-12 13:52:30 -08007671 /* Create HBA Event Queues (EQs) */
7672 for (idx = 0; idx < io_channel; idx++) {
James Smart67d12732012-08-03 12:36:13 -04007673 /* Create EQs */
James Smartda0436e2009-05-22 14:51:39 -04007674 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
7675 phba->sli4_hba.eq_ecount);
7676 if (!qdesc) {
7677 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04007678 "0497 Failed allocate EQ (%d)\n", idx);
7679 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007680 }
James Smart67d12732012-08-03 12:36:13 -04007681 phba->sli4_hba.hba_eq[idx] = qdesc;
James Smartda0436e2009-05-22 14:51:39 -04007682 }
7683
James Smart895427b2017-02-12 13:52:30 -08007684 /* FCP and NVME io channels are not required to be balanced */
7685
7686 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++)
7687 if (lpfc_alloc_fcp_wq_cq(phba, idx))
7688 goto out_error;
7689
7690 for (idx = 0; idx < phba->cfg_nvme_io_channel; idx++)
7691 if (lpfc_alloc_nvme_wq_cq(phba, idx))
7692 goto out_error;
James Smart67d12732012-08-03 12:36:13 -04007693
James Smartda0436e2009-05-22 14:51:39 -04007694 /*
James Smart67d12732012-08-03 12:36:13 -04007695 * Create Slow Path Completion Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04007696 */
7697
James Smartda0436e2009-05-22 14:51:39 -04007698 /* Create slow-path Mailbox Command Complete Queue */
7699 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7700 phba->sli4_hba.cq_ecount);
7701 if (!qdesc) {
7702 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7703 "0500 Failed allocate slow-path mailbox CQ\n");
James Smart67d12732012-08-03 12:36:13 -04007704 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007705 }
7706 phba->sli4_hba.mbx_cq = qdesc;
7707
7708 /* Create slow-path ELS Complete Queue */
7709 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7710 phba->sli4_hba.cq_ecount);
7711 if (!qdesc) {
7712 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7713 "0501 Failed allocate slow-path ELS CQ\n");
James Smart67d12732012-08-03 12:36:13 -04007714 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007715 }
7716 phba->sli4_hba.els_cq = qdesc;
7717
James Smartda0436e2009-05-22 14:51:39 -04007718
James Smart5350d872011-10-10 21:33:49 -04007719 /*
James Smart67d12732012-08-03 12:36:13 -04007720 * Create Slow Path Work Queues (WQs)
James Smart5350d872011-10-10 21:33:49 -04007721 */
James Smartda0436e2009-05-22 14:51:39 -04007722
7723 /* Create Mailbox Command Queue */
James Smartda0436e2009-05-22 14:51:39 -04007724
7725 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
7726 phba->sli4_hba.mq_ecount);
7727 if (!qdesc) {
7728 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7729 "0505 Failed allocate slow-path MQ\n");
James Smart67d12732012-08-03 12:36:13 -04007730 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007731 }
7732 phba->sli4_hba.mbx_wq = qdesc;
7733
7734 /*
James Smart67d12732012-08-03 12:36:13 -04007735 * Create ELS Work Queues
James Smartda0436e2009-05-22 14:51:39 -04007736 */
James Smartda0436e2009-05-22 14:51:39 -04007737
7738 /* Create slow-path ELS Work Queue */
7739 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
7740 phba->sli4_hba.wq_ecount);
7741 if (!qdesc) {
7742 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7743 "0504 Failed allocate slow-path ELS WQ\n");
James Smart67d12732012-08-03 12:36:13 -04007744 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007745 }
7746 phba->sli4_hba.els_wq = qdesc;
James Smart895427b2017-02-12 13:52:30 -08007747 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
7748
7749 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
7750 /* Create NVME LS Complete Queue */
7751 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7752 phba->sli4_hba.cq_ecount);
7753 if (!qdesc) {
7754 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7755 "6079 Failed allocate NVME LS CQ\n");
7756 goto out_error;
7757 }
7758 phba->sli4_hba.nvmels_cq = qdesc;
7759
7760 /* Create NVME LS Work Queue */
7761 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
7762 phba->sli4_hba.wq_ecount);
7763 if (!qdesc) {
7764 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7765 "6080 Failed allocate NVME LS WQ\n");
7766 goto out_error;
7767 }
7768 phba->sli4_hba.nvmels_wq = qdesc;
7769 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
7770 }
James Smartda0436e2009-05-22 14:51:39 -04007771
James Smartda0436e2009-05-22 14:51:39 -04007772 /*
7773 * Create Receive Queue (RQ)
7774 */
James Smartda0436e2009-05-22 14:51:39 -04007775
7776 /* Create Receive Queue for header */
7777 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
7778 phba->sli4_hba.rq_ecount);
7779 if (!qdesc) {
7780 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7781 "0506 Failed allocate receive HRQ\n");
James Smart67d12732012-08-03 12:36:13 -04007782 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007783 }
7784 phba->sli4_hba.hdr_rq = qdesc;
7785
7786 /* Create Receive Queue for data */
7787 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
7788 phba->sli4_hba.rq_ecount);
7789 if (!qdesc) {
7790 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7791 "0507 Failed allocate receive DRQ\n");
James Smart67d12732012-08-03 12:36:13 -04007792 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007793 }
7794 phba->sli4_hba.dat_rq = qdesc;
7795
James Smart1ba981f2014-02-20 09:56:45 -05007796 /* Create the Queues needed for Flash Optimized Fabric operations */
7797 if (phba->cfg_fof)
7798 lpfc_fof_queue_create(phba);
James Smartda0436e2009-05-22 14:51:39 -04007799 return 0;
7800
James Smartda0436e2009-05-22 14:51:39 -04007801out_error:
James Smart67d12732012-08-03 12:36:13 -04007802 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007803 return -ENOMEM;
7804}
7805
James Smart895427b2017-02-12 13:52:30 -08007806static inline void
7807__lpfc_sli4_release_queue(struct lpfc_queue **qp)
7808{
7809 if (*qp != NULL) {
7810 lpfc_sli4_queue_free(*qp);
7811 *qp = NULL;
7812 }
7813}
7814
7815static inline void
7816lpfc_sli4_release_queues(struct lpfc_queue ***qs, int max)
7817{
7818 int idx;
7819
7820 if (*qs == NULL)
7821 return;
7822
7823 for (idx = 0; idx < max; idx++)
7824 __lpfc_sli4_release_queue(&(*qs)[idx]);
7825
7826 kfree(*qs);
7827 *qs = NULL;
7828}
7829
7830static inline void
7831lpfc_sli4_release_queue_map(uint16_t **qmap)
7832{
7833 if (*qmap != NULL) {
7834 kfree(*qmap);
7835 *qmap = NULL;
7836 }
7837}
7838
James Smartda0436e2009-05-22 14:51:39 -04007839/**
7840 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
7841 * @phba: pointer to lpfc hba data structure.
7842 *
7843 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
7844 * operation.
7845 *
7846 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007847 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007848 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007849 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007850 **/
James Smart5350d872011-10-10 21:33:49 -04007851void
James Smartda0436e2009-05-22 14:51:39 -04007852lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
7853{
James Smart1ba981f2014-02-20 09:56:45 -05007854 if (phba->cfg_fof)
7855 lpfc_fof_queue_destroy(phba);
7856
James Smart895427b2017-02-12 13:52:30 -08007857 /* Release HBA eqs */
7858 lpfc_sli4_release_queues(&phba->sli4_hba.hba_eq, phba->io_channel_irqs);
James Smart67d12732012-08-03 12:36:13 -04007859
James Smart895427b2017-02-12 13:52:30 -08007860 /* Release FCP cqs */
7861 lpfc_sli4_release_queues(&phba->sli4_hba.fcp_cq,
7862 phba->cfg_fcp_io_channel);
James Smart67d12732012-08-03 12:36:13 -04007863
James Smart895427b2017-02-12 13:52:30 -08007864 /* Release FCP wqs */
7865 lpfc_sli4_release_queues(&phba->sli4_hba.fcp_wq,
7866 phba->cfg_fcp_io_channel);
James Smart67d12732012-08-03 12:36:13 -04007867
7868 /* Release FCP CQ mapping array */
James Smart895427b2017-02-12 13:52:30 -08007869 lpfc_sli4_release_queue_map(&phba->sli4_hba.fcp_cq_map);
7870
7871 /* Release NVME cqs */
7872 lpfc_sli4_release_queues(&phba->sli4_hba.nvme_cq,
7873 phba->cfg_nvme_io_channel);
7874
7875 /* Release NVME wqs */
7876 lpfc_sli4_release_queues(&phba->sli4_hba.nvme_wq,
7877 phba->cfg_nvme_io_channel);
7878
7879 /* Release NVME CQ mapping array */
7880 lpfc_sli4_release_queue_map(&phba->sli4_hba.nvme_cq_map);
James Smartda0436e2009-05-22 14:51:39 -04007881
7882 /* Release mailbox command work queue */
James Smart895427b2017-02-12 13:52:30 -08007883 __lpfc_sli4_release_queue(&phba->sli4_hba.mbx_wq);
James Smartda0436e2009-05-22 14:51:39 -04007884
7885 /* Release ELS work queue */
James Smart895427b2017-02-12 13:52:30 -08007886 __lpfc_sli4_release_queue(&phba->sli4_hba.els_wq);
7887
7888 /* Release ELS work queue */
7889 __lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_wq);
James Smartda0436e2009-05-22 14:51:39 -04007890
7891 /* Release unsolicited receive queue */
James Smart895427b2017-02-12 13:52:30 -08007892 __lpfc_sli4_release_queue(&phba->sli4_hba.hdr_rq);
7893 __lpfc_sli4_release_queue(&phba->sli4_hba.dat_rq);
James Smartda0436e2009-05-22 14:51:39 -04007894
James Smartda0436e2009-05-22 14:51:39 -04007895 /* Release ELS complete queue */
James Smart895427b2017-02-12 13:52:30 -08007896 __lpfc_sli4_release_queue(&phba->sli4_hba.els_cq);
7897
7898 /* Release NVME LS complete queue */
7899 __lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_cq);
James Smartda0436e2009-05-22 14:51:39 -04007900
7901 /* Release mailbox command complete queue */
James Smart895427b2017-02-12 13:52:30 -08007902 __lpfc_sli4_release_queue(&phba->sli4_hba.mbx_cq);
7903
7904 /* Everything on this list has been freed */
7905 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
7906}
7907
7908int
7909lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
7910 struct lpfc_queue *drq, int count)
7911{
7912 int rc, i;
7913 struct lpfc_rqe hrqe;
7914 struct lpfc_rqe drqe;
7915 struct lpfc_rqb *rqbp;
7916 struct rqb_dmabuf *rqb_buffer;
7917 LIST_HEAD(rqb_buf_list);
7918
7919 rqbp = hrq->rqbp;
7920 for (i = 0; i < count; i++) {
7921 rqb_buffer = (rqbp->rqb_alloc_buffer)(phba);
7922 if (!rqb_buffer)
7923 break;
7924 rqb_buffer->hrq = hrq;
7925 rqb_buffer->drq = drq;
7926 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
7927 }
7928 while (!list_empty(&rqb_buf_list)) {
7929 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
7930 hbuf.list);
7931
7932 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
7933 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
7934 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
7935 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
7936 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
7937 if (rc < 0) {
7938 (rqbp->rqb_free_buffer)(phba, rqb_buffer);
7939 } else {
7940 list_add_tail(&rqb_buffer->hbuf.list,
7941 &rqbp->rqb_buffer_list);
7942 rqbp->buffer_count++;
7943 }
7944 }
7945 return 1;
7946}
7947
7948int
7949lpfc_free_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *rq)
7950{
7951 struct lpfc_rqb *rqbp;
7952 struct lpfc_dmabuf *h_buf;
7953 struct rqb_dmabuf *rqb_buffer;
7954
7955 rqbp = rq->rqbp;
7956 while (!list_empty(&rqbp->rqb_buffer_list)) {
7957 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
7958 struct lpfc_dmabuf, list);
7959
7960 rqb_buffer = container_of(h_buf, struct rqb_dmabuf, hbuf);
7961 (rqbp->rqb_free_buffer)(phba, rqb_buffer);
7962 rqbp->buffer_count--;
7963 }
7964 return 1;
7965}
7966
7967static int
7968lpfc_create_wq_cq(struct lpfc_hba *phba, struct lpfc_queue *eq,
7969 struct lpfc_queue *cq, struct lpfc_queue *wq, uint16_t *cq_map,
7970 int qidx, uint32_t qtype)
7971{
7972 struct lpfc_sli_ring *pring;
7973 int rc;
7974
7975 if (!eq || !cq || !wq) {
7976 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7977 "6085 Fast-path %s (%d) not allocated\n",
7978 ((eq) ? ((cq) ? "WQ" : "CQ") : "EQ"), qidx);
7979 return -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007980 }
James Smartda0436e2009-05-22 14:51:39 -04007981
James Smart895427b2017-02-12 13:52:30 -08007982 /* create the Cq first */
7983 rc = lpfc_cq_create(phba, cq, eq,
7984 (qtype == LPFC_MBOX) ? LPFC_MCQ : LPFC_WCQ, qtype);
7985 if (rc) {
7986 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7987 "6086 Failed setup of CQ (%d), rc = 0x%x\n",
7988 qidx, (uint32_t)rc);
7989 return rc;
7990 }
7991
7992 if (qtype != LPFC_MBOX) {
7993 /* Setup nvme_cq_map for fast lookup */
7994 if (cq_map)
7995 *cq_map = cq->queue_id;
7996
7997 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7998 "6087 CQ setup: cq[%d]-id=%d, parent eq[%d]-id=%d\n",
7999 qidx, cq->queue_id, qidx, eq->queue_id);
8000
8001 /* create the wq */
8002 rc = lpfc_wq_create(phba, wq, cq, qtype);
8003 if (rc) {
8004 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8005 "6123 Fail setup fastpath WQ (%d), rc = 0x%x\n",
8006 qidx, (uint32_t)rc);
8007 /* no need to tear down cq - caller will do so */
8008 return rc;
8009 }
8010
8011 /* Bind this CQ/WQ to the NVME ring */
8012 pring = wq->pring;
8013 pring->sli.sli4.wqp = (void *)wq;
8014 cq->pring = pring;
8015
8016 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8017 "2593 WQ setup: wq[%d]-id=%d assoc=%d, cq[%d]-id=%d\n",
8018 qidx, wq->queue_id, wq->assoc_qid, qidx, cq->queue_id);
8019 } else {
8020 rc = lpfc_mq_create(phba, wq, cq, LPFC_MBOX);
8021 if (rc) {
8022 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8023 "0539 Failed setup of slow-path MQ: "
8024 "rc = 0x%x\n", rc);
8025 /* no need to tear down cq - caller will do so */
8026 return rc;
8027 }
8028
8029 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8030 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
8031 phba->sli4_hba.mbx_wq->queue_id,
8032 phba->sli4_hba.mbx_cq->queue_id);
8033 }
8034
8035 return 0;
James Smartda0436e2009-05-22 14:51:39 -04008036}
8037
8038/**
8039 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
8040 * @phba: pointer to lpfc hba data structure.
8041 *
8042 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
8043 * operation.
8044 *
8045 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008046 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008047 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04008048 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04008049 **/
8050int
8051lpfc_sli4_queue_setup(struct lpfc_hba *phba)
8052{
James Smart962bc512013-01-03 15:44:00 -05008053 uint32_t shdr_status, shdr_add_status;
8054 union lpfc_sli4_cfg_shdr *shdr;
8055 LPFC_MBOXQ_t *mboxq;
James Smart895427b2017-02-12 13:52:30 -08008056 int qidx;
8057 uint32_t length, io_channel;
8058 int rc = -ENOMEM;
James Smart962bc512013-01-03 15:44:00 -05008059
8060 /* Check for dual-ULP support */
8061 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8062 if (!mboxq) {
8063 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8064 "3249 Unable to allocate memory for "
8065 "QUERY_FW_CFG mailbox command\n");
8066 return -ENOMEM;
8067 }
8068 length = (sizeof(struct lpfc_mbx_query_fw_config) -
8069 sizeof(struct lpfc_sli4_cfg_mhdr));
8070 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
8071 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
8072 length, LPFC_SLI4_MBX_EMBED);
8073
8074 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8075
8076 shdr = (union lpfc_sli4_cfg_shdr *)
8077 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
8078 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
8079 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
8080 if (shdr_status || shdr_add_status || rc) {
8081 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8082 "3250 QUERY_FW_CFG mailbox failed with status "
8083 "x%x add_status x%x, mbx status x%x\n",
8084 shdr_status, shdr_add_status, rc);
8085 if (rc != MBX_TIMEOUT)
8086 mempool_free(mboxq, phba->mbox_mem_pool);
8087 rc = -ENXIO;
8088 goto out_error;
8089 }
8090
8091 phba->sli4_hba.fw_func_mode =
8092 mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
8093 phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
8094 phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
James Smart8b017a32015-05-21 13:55:18 -04008095 phba->sli4_hba.physical_port =
8096 mboxq->u.mqe.un.query_fw_cfg.rsp.physical_port;
James Smart962bc512013-01-03 15:44:00 -05008097 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8098 "3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
8099 "ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
8100 phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
8101
8102 if (rc != MBX_TIMEOUT)
8103 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04008104
8105 /*
James Smart67d12732012-08-03 12:36:13 -04008106 * Set up HBA Event Queues (EQs)
James Smartda0436e2009-05-22 14:51:39 -04008107 */
James Smart895427b2017-02-12 13:52:30 -08008108 io_channel = phba->io_channel_irqs;
James Smartda0436e2009-05-22 14:51:39 -04008109
James Smart67d12732012-08-03 12:36:13 -04008110 /* Set up HBA event queue */
James Smart895427b2017-02-12 13:52:30 -08008111 if (io_channel && !phba->sli4_hba.hba_eq) {
James Smart2e90f4b2011-12-13 13:22:37 -05008112 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8113 "3147 Fast-path EQs not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05008114 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04008115 goto out_error;
James Smart2e90f4b2011-12-13 13:22:37 -05008116 }
James Smart895427b2017-02-12 13:52:30 -08008117 for (qidx = 0; qidx < io_channel; qidx++) {
8118 if (!phba->sli4_hba.hba_eq[qidx]) {
James Smartda0436e2009-05-22 14:51:39 -04008119 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8120 "0522 Fast-path EQ (%d) not "
James Smart895427b2017-02-12 13:52:30 -08008121 "allocated\n", qidx);
James Smart1b511972011-12-13 13:23:09 -05008122 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08008123 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008124 }
James Smart895427b2017-02-12 13:52:30 -08008125 rc = lpfc_eq_create(phba, phba->sli4_hba.hba_eq[qidx],
8126 phba->cfg_fcp_imax);
James Smartda0436e2009-05-22 14:51:39 -04008127 if (rc) {
8128 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8129 "0523 Failed setup of fast-path EQ "
James Smart895427b2017-02-12 13:52:30 -08008130 "(%d), rc = 0x%x\n", qidx,
James Smarta2fc4aef2014-09-03 12:57:55 -04008131 (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -08008132 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008133 }
8134 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008135 "2584 HBA EQ setup: queue[%d]-id=%d\n",
8136 qidx, phba->sli4_hba.hba_eq[qidx]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04008137 }
8138
James Smart895427b2017-02-12 13:52:30 -08008139 if (phba->cfg_nvme_io_channel) {
8140 if (!phba->sli4_hba.nvme_cq || !phba->sli4_hba.nvme_wq) {
James Smart67d12732012-08-03 12:36:13 -04008141 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008142 "6084 Fast-path NVME %s array not allocated\n",
8143 (phba->sli4_hba.nvme_cq) ? "CQ" : "WQ");
James Smart67d12732012-08-03 12:36:13 -04008144 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08008145 goto out_destroy;
James Smart67d12732012-08-03 12:36:13 -04008146 }
8147
James Smart895427b2017-02-12 13:52:30 -08008148 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++) {
8149 rc = lpfc_create_wq_cq(phba,
8150 phba->sli4_hba.hba_eq[
8151 qidx % io_channel],
8152 phba->sli4_hba.nvme_cq[qidx],
8153 phba->sli4_hba.nvme_wq[qidx],
8154 &phba->sli4_hba.nvme_cq_map[qidx],
8155 qidx, LPFC_NVME);
8156 if (rc) {
8157 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8158 "6123 Failed to setup fastpath "
8159 "NVME WQ/CQ (%d), rc = 0x%x\n",
8160 qidx, (uint32_t)rc);
8161 goto out_destroy;
8162 }
8163 }
James Smart67d12732012-08-03 12:36:13 -04008164 }
8165
James Smart895427b2017-02-12 13:52:30 -08008166 if (phba->cfg_fcp_io_channel) {
8167 /* Set up fast-path FCP Response Complete Queue */
8168 if (!phba->sli4_hba.fcp_cq || !phba->sli4_hba.fcp_wq) {
James Smart67d12732012-08-03 12:36:13 -04008169 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008170 "3148 Fast-path FCP %s array not allocated\n",
8171 phba->sli4_hba.fcp_cq ? "WQ" : "CQ");
James Smart67d12732012-08-03 12:36:13 -04008172 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08008173 goto out_destroy;
James Smart67d12732012-08-03 12:36:13 -04008174 }
8175
James Smart895427b2017-02-12 13:52:30 -08008176 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++) {
8177 rc = lpfc_create_wq_cq(phba,
8178 phba->sli4_hba.hba_eq[
8179 qidx % io_channel],
8180 phba->sli4_hba.fcp_cq[qidx],
8181 phba->sli4_hba.fcp_wq[qidx],
8182 &phba->sli4_hba.fcp_cq_map[qidx],
8183 qidx, LPFC_FCP);
8184 if (rc) {
8185 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8186 "0535 Failed to setup fastpath "
8187 "FCP WQ/CQ (%d), rc = 0x%x\n",
8188 qidx, (uint32_t)rc);
8189 goto out_destroy;
8190 }
8191 }
James Smart67d12732012-08-03 12:36:13 -04008192 }
James Smartda0436e2009-05-22 14:51:39 -04008193
8194 /*
James Smart895427b2017-02-12 13:52:30 -08008195 * Set up Slow Path Complete Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04008196 */
8197
James Smart895427b2017-02-12 13:52:30 -08008198 /* Set up slow-path MBOX CQ/MQ */
8199
8200 if (!phba->sli4_hba.mbx_cq || !phba->sli4_hba.mbx_wq) {
James Smartda0436e2009-05-22 14:51:39 -04008201 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008202 "0528 %s not allocated\n",
8203 phba->sli4_hba.mbx_cq ?
8204 "Mailbox WQ" : "Mailbox CQ");
James Smart1b511972011-12-13 13:23:09 -05008205 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08008206 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008207 }
James Smart895427b2017-02-12 13:52:30 -08008208
8209 rc = lpfc_create_wq_cq(phba, phba->sli4_hba.hba_eq[0],
8210 phba->sli4_hba.mbx_cq,
8211 phba->sli4_hba.mbx_wq,
8212 NULL, 0, LPFC_MBOX);
James Smartda0436e2009-05-22 14:51:39 -04008213 if (rc) {
8214 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008215 "0529 Failed setup of mailbox WQ/CQ: rc = 0x%x\n",
8216 (uint32_t)rc);
8217 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008218 }
James Smartda0436e2009-05-22 14:51:39 -04008219
James Smart895427b2017-02-12 13:52:30 -08008220 /* Set up slow-path ELS WQ/CQ */
8221 if (!phba->sli4_hba.els_cq || !phba->sli4_hba.els_wq) {
James Smartda0436e2009-05-22 14:51:39 -04008222 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008223 "0530 ELS %s not allocated\n",
8224 phba->sli4_hba.els_cq ? "WQ" : "CQ");
James Smart1b511972011-12-13 13:23:09 -05008225 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08008226 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008227 }
James Smart895427b2017-02-12 13:52:30 -08008228 rc = lpfc_create_wq_cq(phba, phba->sli4_hba.hba_eq[0],
8229 phba->sli4_hba.els_cq,
8230 phba->sli4_hba.els_wq,
8231 NULL, 0, LPFC_ELS);
James Smartda0436e2009-05-22 14:51:39 -04008232 if (rc) {
8233 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart895427b2017-02-12 13:52:30 -08008234 "0529 Failed setup of ELS WQ/CQ: rc = 0x%x\n",
8235 (uint32_t)rc);
8236 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008237 }
8238 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8239 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
8240 phba->sli4_hba.els_wq->queue_id,
8241 phba->sli4_hba.els_cq->queue_id);
8242
James Smart895427b2017-02-12 13:52:30 -08008243 if (phba->cfg_nvme_io_channel) {
8244 /* Set up NVME LS Complete Queue */
8245 if (!phba->sli4_hba.nvmels_cq || !phba->sli4_hba.nvmels_wq) {
8246 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8247 "6091 LS %s not allocated\n",
8248 phba->sli4_hba.nvmels_cq ? "WQ" : "CQ");
8249 rc = -ENOMEM;
8250 goto out_destroy;
8251 }
8252 rc = lpfc_create_wq_cq(phba, phba->sli4_hba.hba_eq[0],
8253 phba->sli4_hba.nvmels_cq,
8254 phba->sli4_hba.nvmels_wq,
8255 NULL, 0, LPFC_NVME_LS);
8256 if (rc) {
8257 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8258 "0529 Failed setup of NVVME LS WQ/CQ: "
8259 "rc = 0x%x\n", (uint32_t)rc);
8260 goto out_destroy;
8261 }
8262
8263 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8264 "6096 ELS WQ setup: wq-id=%d, "
8265 "parent cq-id=%d\n",
8266 phba->sli4_hba.nvmels_wq->queue_id,
8267 phba->sli4_hba.nvmels_cq->queue_id);
8268 }
8269
James Smartda0436e2009-05-22 14:51:39 -04008270 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
8271 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8272 "0540 Receive Queue not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05008273 rc = -ENOMEM;
James Smart895427b2017-02-12 13:52:30 -08008274 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008275 }
James Smart73d91e52011-10-10 21:32:10 -04008276
8277 lpfc_rq_adjust_repost(phba, phba->sli4_hba.hdr_rq, LPFC_ELS_HBQ);
8278 lpfc_rq_adjust_repost(phba, phba->sli4_hba.dat_rq, LPFC_ELS_HBQ);
8279
James Smartda0436e2009-05-22 14:51:39 -04008280 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04008281 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04008282 if (rc) {
8283 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8284 "0541 Failed setup of Receive Queue: "
James Smarta2fc4aef2014-09-03 12:57:55 -04008285 "rc = 0x%x\n", (uint32_t)rc);
James Smart895427b2017-02-12 13:52:30 -08008286 goto out_destroy;
James Smartda0436e2009-05-22 14:51:39 -04008287 }
James Smart73d91e52011-10-10 21:32:10 -04008288
James Smartda0436e2009-05-22 14:51:39 -04008289 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8290 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
8291 "parent cq-id=%d\n",
8292 phba->sli4_hba.hdr_rq->queue_id,
8293 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04008294 phba->sli4_hba.els_cq->queue_id);
James Smart1ba981f2014-02-20 09:56:45 -05008295
8296 if (phba->cfg_fof) {
8297 rc = lpfc_fof_queue_setup(phba);
8298 if (rc) {
8299 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8300 "0549 Failed setup of FOF Queues: "
8301 "rc = 0x%x\n", rc);
James Smart895427b2017-02-12 13:52:30 -08008302 goto out_destroy;
James Smart1ba981f2014-02-20 09:56:45 -05008303 }
8304 }
James Smart2c9c5a02015-04-07 15:07:15 -04008305
8306 /*
8307 * Configure EQ delay multipier for interrupt coalescing using
8308 * MODIFY_EQ_DELAY for all EQs created, LPFC_MAX_EQ_DELAY at a time.
8309 */
James Smart895427b2017-02-12 13:52:30 -08008310 for (qidx = 0; qidx < io_channel; qidx += LPFC_MAX_EQ_DELAY)
8311 lpfc_modify_hba_eq_delay(phba, qidx);
James Smartda0436e2009-05-22 14:51:39 -04008312 return 0;
8313
James Smart895427b2017-02-12 13:52:30 -08008314out_destroy:
8315 lpfc_sli4_queue_unset(phba);
James Smartda0436e2009-05-22 14:51:39 -04008316out_error:
8317 return rc;
8318}
8319
8320/**
8321 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
8322 * @phba: pointer to lpfc hba data structure.
8323 *
8324 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
8325 * operation.
8326 *
8327 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008328 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008329 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04008330 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04008331 **/
8332void
8333lpfc_sli4_queue_unset(struct lpfc_hba *phba)
8334{
James Smart895427b2017-02-12 13:52:30 -08008335 int qidx;
James Smartda0436e2009-05-22 14:51:39 -04008336
James Smart1ba981f2014-02-20 09:56:45 -05008337 /* Unset the queues created for Flash Optimized Fabric operations */
8338 if (phba->cfg_fof)
8339 lpfc_fof_queue_destroy(phba);
James Smart895427b2017-02-12 13:52:30 -08008340
James Smartda0436e2009-05-22 14:51:39 -04008341 /* Unset mailbox command work queue */
James Smart895427b2017-02-12 13:52:30 -08008342 if (phba->sli4_hba.mbx_wq)
8343 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
8344
8345 /* Unset NVME LS work queue */
8346 if (phba->sli4_hba.nvmels_wq)
8347 lpfc_wq_destroy(phba, phba->sli4_hba.nvmels_wq);
8348
James Smartda0436e2009-05-22 14:51:39 -04008349 /* Unset ELS work queue */
James Smart895427b2017-02-12 13:52:30 -08008350 if (phba->sli4_hba.els_cq)
8351 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
8352
James Smartda0436e2009-05-22 14:51:39 -04008353 /* Unset unsolicited receive queue */
James Smart895427b2017-02-12 13:52:30 -08008354 if (phba->sli4_hba.hdr_rq)
8355 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq,
8356 phba->sli4_hba.dat_rq);
8357
James Smartda0436e2009-05-22 14:51:39 -04008358 /* Unset FCP work queue */
James Smart895427b2017-02-12 13:52:30 -08008359 if (phba->sli4_hba.fcp_wq)
8360 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
8361 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[qidx]);
8362
8363 /* Unset NVME work queue */
8364 if (phba->sli4_hba.nvme_wq) {
8365 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
8366 lpfc_wq_destroy(phba, phba->sli4_hba.nvme_wq[qidx]);
James Smart67d12732012-08-03 12:36:13 -04008367 }
James Smart895427b2017-02-12 13:52:30 -08008368
James Smartda0436e2009-05-22 14:51:39 -04008369 /* Unset mailbox command complete queue */
James Smart895427b2017-02-12 13:52:30 -08008370 if (phba->sli4_hba.mbx_cq)
8371 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
8372
James Smartda0436e2009-05-22 14:51:39 -04008373 /* Unset ELS complete queue */
James Smart895427b2017-02-12 13:52:30 -08008374 if (phba->sli4_hba.els_cq)
8375 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
8376
8377 /* Unset NVME LS complete queue */
8378 if (phba->sli4_hba.nvmels_cq)
8379 lpfc_cq_destroy(phba, phba->sli4_hba.nvmels_cq);
8380
8381 /* Unset NVME response complete queue */
8382 if (phba->sli4_hba.nvme_cq)
8383 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
8384 lpfc_cq_destroy(phba, phba->sli4_hba.nvme_cq[qidx]);
8385
James Smartda0436e2009-05-22 14:51:39 -04008386 /* Unset FCP response complete queue */
James Smart895427b2017-02-12 13:52:30 -08008387 if (phba->sli4_hba.fcp_cq)
8388 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
8389 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[qidx]);
8390
James Smartda0436e2009-05-22 14:51:39 -04008391 /* Unset fast-path event queue */
James Smart895427b2017-02-12 13:52:30 -08008392 if (phba->sli4_hba.hba_eq)
8393 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
8394 lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[qidx]);
James Smartda0436e2009-05-22 14:51:39 -04008395}
8396
8397/**
8398 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
8399 * @phba: pointer to lpfc hba data structure.
8400 *
8401 * This routine is invoked to allocate and set up a pool of completion queue
8402 * events. The body of the completion queue event is a completion queue entry
8403 * CQE. For now, this pool is used for the interrupt service routine to queue
8404 * the following HBA completion queue events for the worker thread to process:
8405 * - Mailbox asynchronous events
8406 * - Receive queue completion unsolicited events
8407 * Later, this can be used for all the slow-path events.
8408 *
8409 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008410 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008411 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04008412 **/
8413static int
8414lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
8415{
8416 struct lpfc_cq_event *cq_event;
8417 int i;
8418
8419 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
8420 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
8421 if (!cq_event)
8422 goto out_pool_create_fail;
8423 list_add_tail(&cq_event->list,
8424 &phba->sli4_hba.sp_cqe_event_pool);
8425 }
8426 return 0;
8427
8428out_pool_create_fail:
8429 lpfc_sli4_cq_event_pool_destroy(phba);
8430 return -ENOMEM;
8431}
8432
8433/**
8434 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
8435 * @phba: pointer to lpfc hba data structure.
8436 *
8437 * This routine is invoked to free the pool of completion queue events at
8438 * driver unload time. Note that, it is the responsibility of the driver
8439 * cleanup routine to free all the outstanding completion-queue events
8440 * allocated from this pool back into the pool before invoking this routine
8441 * to destroy the pool.
8442 **/
8443static void
8444lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
8445{
8446 struct lpfc_cq_event *cq_event, *next_cq_event;
8447
8448 list_for_each_entry_safe(cq_event, next_cq_event,
8449 &phba->sli4_hba.sp_cqe_event_pool, list) {
8450 list_del(&cq_event->list);
8451 kfree(cq_event);
8452 }
8453}
8454
8455/**
8456 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
8457 * @phba: pointer to lpfc hba data structure.
8458 *
8459 * This routine is the lock free version of the API invoked to allocate a
8460 * completion-queue event from the free pool.
8461 *
8462 * Return: Pointer to the newly allocated completion-queue event if successful
8463 * NULL otherwise.
8464 **/
8465struct lpfc_cq_event *
8466__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
8467{
8468 struct lpfc_cq_event *cq_event = NULL;
8469
8470 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
8471 struct lpfc_cq_event, list);
8472 return cq_event;
8473}
8474
8475/**
8476 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
8477 * @phba: pointer to lpfc hba data structure.
8478 *
8479 * This routine is the lock version of the API invoked to allocate a
8480 * completion-queue event from the free pool.
8481 *
8482 * Return: Pointer to the newly allocated completion-queue event if successful
8483 * NULL otherwise.
8484 **/
8485struct lpfc_cq_event *
8486lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
8487{
8488 struct lpfc_cq_event *cq_event;
8489 unsigned long iflags;
8490
8491 spin_lock_irqsave(&phba->hbalock, iflags);
8492 cq_event = __lpfc_sli4_cq_event_alloc(phba);
8493 spin_unlock_irqrestore(&phba->hbalock, iflags);
8494 return cq_event;
8495}
8496
8497/**
8498 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
8499 * @phba: pointer to lpfc hba data structure.
8500 * @cq_event: pointer to the completion queue event to be freed.
8501 *
8502 * This routine is the lock free version of the API invoked to release a
8503 * completion-queue event back into the free pool.
8504 **/
8505void
8506__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
8507 struct lpfc_cq_event *cq_event)
8508{
8509 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
8510}
8511
8512/**
8513 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
8514 * @phba: pointer to lpfc hba data structure.
8515 * @cq_event: pointer to the completion queue event to be freed.
8516 *
8517 * This routine is the lock version of the API invoked to release a
8518 * completion-queue event back into the free pool.
8519 **/
8520void
8521lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
8522 struct lpfc_cq_event *cq_event)
8523{
8524 unsigned long iflags;
8525 spin_lock_irqsave(&phba->hbalock, iflags);
8526 __lpfc_sli4_cq_event_release(phba, cq_event);
8527 spin_unlock_irqrestore(&phba->hbalock, iflags);
8528}
8529
8530/**
8531 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
8532 * @phba: pointer to lpfc hba data structure.
8533 *
8534 * This routine is to free all the pending completion-queue events to the
8535 * back into the free pool for device reset.
8536 **/
8537static void
8538lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
8539{
8540 LIST_HEAD(cqelist);
8541 struct lpfc_cq_event *cqe;
8542 unsigned long iflags;
8543
8544 /* Retrieve all the pending WCQEs from pending WCQE lists */
8545 spin_lock_irqsave(&phba->hbalock, iflags);
8546 /* Pending FCP XRI abort events */
8547 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
8548 &cqelist);
8549 /* Pending ELS XRI abort events */
8550 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
8551 &cqelist);
8552 /* Pending asynnc events */
8553 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
8554 &cqelist);
8555 spin_unlock_irqrestore(&phba->hbalock, iflags);
8556
8557 while (!list_empty(&cqelist)) {
8558 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
8559 lpfc_sli4_cq_event_release(phba, cqe);
8560 }
8561}
8562
8563/**
8564 * lpfc_pci_function_reset - Reset pci function.
8565 * @phba: pointer to lpfc hba data structure.
8566 *
8567 * This routine is invoked to request a PCI function reset. It will destroys
8568 * all resources assigned to the PCI function which originates this request.
8569 *
8570 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008571 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008572 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04008573 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04008574 **/
8575int
8576lpfc_pci_function_reset(struct lpfc_hba *phba)
8577{
8578 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05008579 uint32_t rc = 0, if_type;
James Smartda0436e2009-05-22 14:51:39 -04008580 uint32_t shdr_status, shdr_add_status;
James Smart2f6fa2c2014-09-03 12:57:08 -04008581 uint32_t rdy_chk;
8582 uint32_t port_reset = 0;
James Smartda0436e2009-05-22 14:51:39 -04008583 union lpfc_sli4_cfg_shdr *shdr;
James Smart2fcee4b2010-12-15 17:57:46 -05008584 struct lpfc_register reg_data;
James Smart2b81f942012-03-01 22:37:18 -05008585 uint16_t devid;
James Smartda0436e2009-05-22 14:51:39 -04008586
James Smart2fcee4b2010-12-15 17:57:46 -05008587 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
8588 switch (if_type) {
8589 case LPFC_SLI_INTF_IF_TYPE_0:
8590 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
8591 GFP_KERNEL);
8592 if (!mboxq) {
8593 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8594 "0494 Unable to allocate memory for "
8595 "issuing SLI_FUNCTION_RESET mailbox "
8596 "command\n");
8597 return -ENOMEM;
8598 }
8599
8600 /* Setup PCI function reset mailbox-ioctl command */
8601 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
8602 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
8603 LPFC_SLI4_MBX_EMBED);
8604 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8605 shdr = (union lpfc_sli4_cfg_shdr *)
8606 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
8607 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
8608 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
8609 &shdr->response);
8610 if (rc != MBX_TIMEOUT)
8611 mempool_free(mboxq, phba->mbox_mem_pool);
8612 if (shdr_status || shdr_add_status || rc) {
8613 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8614 "0495 SLI_FUNCTION_RESET mailbox "
8615 "failed with status x%x add_status x%x,"
8616 " mbx status x%x\n",
8617 shdr_status, shdr_add_status, rc);
8618 rc = -ENXIO;
8619 }
8620 break;
8621 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart2f6fa2c2014-09-03 12:57:08 -04008622wait:
8623 /*
8624 * Poll the Port Status Register and wait for RDY for
8625 * up to 30 seconds. If the port doesn't respond, treat
8626 * it as an error.
8627 */
James Smart77d093f2015-04-07 15:07:08 -04008628 for (rdy_chk = 0; rdy_chk < 1500; rdy_chk++) {
James Smart2f6fa2c2014-09-03 12:57:08 -04008629 if (lpfc_readl(phba->sli4_hba.u.if_type2.
8630 STATUSregaddr, &reg_data.word0)) {
8631 rc = -ENODEV;
8632 goto out;
8633 }
8634 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
8635 break;
8636 msleep(20);
8637 }
8638
8639 if (!bf_get(lpfc_sliport_status_rdy, &reg_data)) {
8640 phba->work_status[0] = readl(
8641 phba->sli4_hba.u.if_type2.ERR1regaddr);
8642 phba->work_status[1] = readl(
8643 phba->sli4_hba.u.if_type2.ERR2regaddr);
8644 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8645 "2890 Port not ready, port status reg "
8646 "0x%x error 1=0x%x, error 2=0x%x\n",
8647 reg_data.word0,
8648 phba->work_status[0],
8649 phba->work_status[1]);
8650 rc = -ENODEV;
8651 goto out;
8652 }
8653
8654 if (!port_reset) {
8655 /*
8656 * Reset the port now
8657 */
James Smart2fcee4b2010-12-15 17:57:46 -05008658 reg_data.word0 = 0;
8659 bf_set(lpfc_sliport_ctrl_end, &reg_data,
8660 LPFC_SLIPORT_LITTLE_ENDIAN);
8661 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
8662 LPFC_SLIPORT_INIT_PORT);
8663 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
8664 CTRLregaddr);
James Smart8fcb8ac2012-03-01 22:35:58 -05008665 /* flush */
James Smart2b81f942012-03-01 22:37:18 -05008666 pci_read_config_word(phba->pcidev,
8667 PCI_DEVICE_ID, &devid);
James Smart2fcee4b2010-12-15 17:57:46 -05008668
James Smart2f6fa2c2014-09-03 12:57:08 -04008669 port_reset = 1;
8670 msleep(20);
8671 goto wait;
8672 } else if (bf_get(lpfc_sliport_status_rn, &reg_data)) {
8673 rc = -ENODEV;
8674 goto out;
James Smart2fcee4b2010-12-15 17:57:46 -05008675 }
8676 break;
James Smart2f6fa2c2014-09-03 12:57:08 -04008677
James Smart2fcee4b2010-12-15 17:57:46 -05008678 case LPFC_SLI_INTF_IF_TYPE_1:
8679 default:
8680 break;
James Smartda0436e2009-05-22 14:51:39 -04008681 }
8682
James Smart73d91e52011-10-10 21:32:10 -04008683out:
James Smart2fcee4b2010-12-15 17:57:46 -05008684 /* Catch the not-ready port failure after a port reset. */
James Smart2f6fa2c2014-09-03 12:57:08 -04008685 if (rc) {
James Smart229adb02013-04-17 20:16:51 -04008686 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8687 "3317 HBA not functional: IP Reset Failed "
James Smart2f6fa2c2014-09-03 12:57:08 -04008688 "try: echo fw_reset > board_mode\n");
James Smart2fcee4b2010-12-15 17:57:46 -05008689 rc = -ENODEV;
James Smart229adb02013-04-17 20:16:51 -04008690 }
James Smart2fcee4b2010-12-15 17:57:46 -05008691
James Smartda0436e2009-05-22 14:51:39 -04008692 return rc;
8693}
8694
8695/**
James Smartda0436e2009-05-22 14:51:39 -04008696 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
8697 * @phba: pointer to lpfc hba data structure.
8698 *
8699 * This routine is invoked to set up the PCI device memory space for device
8700 * with SLI-4 interface spec.
8701 *
8702 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008703 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008704 * other values - error
8705 **/
8706static int
8707lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
8708{
8709 struct pci_dev *pdev;
8710 unsigned long bar0map_len, bar1map_len, bar2map_len;
8711 int error = -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -05008712 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04008713
8714 /* Obtain PCI device reference */
8715 if (!phba->pcidev)
8716 return error;
8717 else
8718 pdev = phba->pcidev;
8719
8720 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05008721 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
8722 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
8723 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
8724 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smartda0436e2009-05-22 14:51:39 -04008725 return error;
Michael Reed8e685972009-09-18 12:02:05 -05008726 }
8727 }
James Smartda0436e2009-05-22 14:51:39 -04008728
James Smart2fcee4b2010-12-15 17:57:46 -05008729 /*
8730 * The BARs and register set definitions and offset locations are
8731 * dependent on the if_type.
8732 */
8733 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
8734 &phba->sli4_hba.sli_intf.word0)) {
8735 return error;
8736 }
8737
8738 /* There is no SLI3 failback for SLI4 devices. */
8739 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
8740 LPFC_SLI_INTF_VALID) {
8741 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8742 "2894 SLI_INTF reg contents invalid "
8743 "sli_intf reg 0x%x\n",
8744 phba->sli4_hba.sli_intf.word0);
8745 return error;
8746 }
8747
8748 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
8749 /*
8750 * Get the bus address of SLI4 device Bar regions and the
8751 * number of bytes required by each mapping. The mapping of the
8752 * particular PCI BARs regions is dependent on the type of
8753 * SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -04008754 */
James Smartf5ca6f22013-09-06 12:21:09 -04008755 if (pci_resource_start(pdev, PCI_64BIT_BAR0)) {
8756 phba->pci_bar0_map = pci_resource_start(pdev, PCI_64BIT_BAR0);
8757 bar0map_len = pci_resource_len(pdev, PCI_64BIT_BAR0);
James Smart2fcee4b2010-12-15 17:57:46 -05008758
8759 /*
8760 * Map SLI4 PCI Config Space Register base to a kernel virtual
8761 * addr
8762 */
8763 phba->sli4_hba.conf_regs_memmap_p =
8764 ioremap(phba->pci_bar0_map, bar0map_len);
8765 if (!phba->sli4_hba.conf_regs_memmap_p) {
8766 dev_printk(KERN_ERR, &pdev->dev,
8767 "ioremap failed for SLI4 PCI config "
8768 "registers.\n");
8769 goto out;
8770 }
James Smartf5ca6f22013-09-06 12:21:09 -04008771 phba->pci_bar0_memmap_p = phba->sli4_hba.conf_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05008772 /* Set up BAR0 PCI config space register memory map */
8773 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smart1dfb5a42010-02-12 14:40:50 -05008774 } else {
8775 phba->pci_bar0_map = pci_resource_start(pdev, 1);
8776 bar0map_len = pci_resource_len(pdev, 1);
James Smart2fcee4b2010-12-15 17:57:46 -05008777 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8778 dev_printk(KERN_ERR, &pdev->dev,
8779 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
8780 goto out;
8781 }
8782 phba->sli4_hba.conf_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04008783 ioremap(phba->pci_bar0_map, bar0map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05008784 if (!phba->sli4_hba.conf_regs_memmap_p) {
8785 dev_printk(KERN_ERR, &pdev->dev,
8786 "ioremap failed for SLI4 PCI config "
8787 "registers.\n");
8788 goto out;
8789 }
8790 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smartda0436e2009-05-22 14:51:39 -04008791 }
8792
James Smartc31098c2011-04-16 11:03:33 -04008793 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
James Smartf5ca6f22013-09-06 12:21:09 -04008794 (pci_resource_start(pdev, PCI_64BIT_BAR2))) {
James Smart2fcee4b2010-12-15 17:57:46 -05008795 /*
8796 * Map SLI4 if type 0 HBA Control Register base to a kernel
8797 * virtual address and setup the registers.
8798 */
James Smartf5ca6f22013-09-06 12:21:09 -04008799 phba->pci_bar1_map = pci_resource_start(pdev, PCI_64BIT_BAR2);
8800 bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
James Smart2fcee4b2010-12-15 17:57:46 -05008801 phba->sli4_hba.ctrl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04008802 ioremap(phba->pci_bar1_map, bar1map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05008803 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
8804 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04008805 "ioremap failed for SLI4 HBA control registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05008806 goto out_iounmap_conf;
8807 }
James Smartf5ca6f22013-09-06 12:21:09 -04008808 phba->pci_bar2_memmap_p = phba->sli4_hba.ctrl_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05008809 lpfc_sli4_bar1_register_memmap(phba);
James Smartda0436e2009-05-22 14:51:39 -04008810 }
8811
James Smartc31098c2011-04-16 11:03:33 -04008812 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
James Smartf5ca6f22013-09-06 12:21:09 -04008813 (pci_resource_start(pdev, PCI_64BIT_BAR4))) {
James Smart2fcee4b2010-12-15 17:57:46 -05008814 /*
8815 * Map SLI4 if type 0 HBA Doorbell Register base to a kernel
8816 * virtual address and setup the registers.
8817 */
James Smartf5ca6f22013-09-06 12:21:09 -04008818 phba->pci_bar2_map = pci_resource_start(pdev, PCI_64BIT_BAR4);
8819 bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
James Smart2fcee4b2010-12-15 17:57:46 -05008820 phba->sli4_hba.drbl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04008821 ioremap(phba->pci_bar2_map, bar2map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05008822 if (!phba->sli4_hba.drbl_regs_memmap_p) {
8823 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04008824 "ioremap failed for SLI4 HBA doorbell registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05008825 goto out_iounmap_ctrl;
8826 }
James Smartf5ca6f22013-09-06 12:21:09 -04008827 phba->pci_bar4_memmap_p = phba->sli4_hba.drbl_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05008828 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
8829 if (error)
8830 goto out_iounmap_all;
James Smartda0436e2009-05-22 14:51:39 -04008831 }
8832
James Smartda0436e2009-05-22 14:51:39 -04008833 return 0;
8834
8835out_iounmap_all:
8836 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
8837out_iounmap_ctrl:
8838 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
8839out_iounmap_conf:
8840 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8841out:
8842 return error;
8843}
8844
8845/**
8846 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
8847 * @phba: pointer to lpfc hba data structure.
8848 *
8849 * This routine is invoked to unset the PCI device memory space for device
8850 * with SLI-4 interface spec.
8851 **/
8852static void
8853lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
8854{
James Smart2e90f4b2011-12-13 13:22:37 -05008855 uint32_t if_type;
8856 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smartda0436e2009-05-22 14:51:39 -04008857
James Smart2e90f4b2011-12-13 13:22:37 -05008858 switch (if_type) {
8859 case LPFC_SLI_INTF_IF_TYPE_0:
8860 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
8861 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
8862 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8863 break;
8864 case LPFC_SLI_INTF_IF_TYPE_2:
8865 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8866 break;
8867 case LPFC_SLI_INTF_IF_TYPE_1:
8868 default:
8869 dev_printk(KERN_ERR, &phba->pcidev->dev,
8870 "FATAL - unsupported SLI4 interface type - %d\n",
8871 if_type);
8872 break;
8873 }
James Smartda0436e2009-05-22 14:51:39 -04008874}
8875
8876/**
James Smart3772a992009-05-22 14:50:54 -04008877 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
8878 * @phba: pointer to lpfc hba data structure.
8879 *
8880 * This routine is invoked to enable the MSI-X interrupt vectors to device
Christoph Hellwig45ffac12017-02-12 13:52:26 -08008881 * with SLI-3 interface specs.
James Smart3772a992009-05-22 14:50:54 -04008882 *
8883 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008884 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008885 * other values - error
8886 **/
8887static int
8888lpfc_sli_enable_msix(struct lpfc_hba *phba)
8889{
Christoph Hellwig45ffac12017-02-12 13:52:26 -08008890 int rc;
James Smart3772a992009-05-22 14:50:54 -04008891 LPFC_MBOXQ_t *pmb;
8892
8893 /* Set up MSI-X multi-message vectors */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08008894 rc = pci_alloc_irq_vectors(phba->pcidev,
8895 LPFC_MSIX_VECTORS, LPFC_MSIX_VECTORS, PCI_IRQ_MSIX);
8896 if (rc < 0) {
James Smart3772a992009-05-22 14:50:54 -04008897 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8898 "0420 PCI enable MSI-X failed (%d)\n", rc);
Alexander Gordeev029165a2014-07-16 20:05:15 +02008899 goto vec_fail_out;
James Smart3772a992009-05-22 14:50:54 -04008900 }
Christoph Hellwig45ffac12017-02-12 13:52:26 -08008901
James Smart3772a992009-05-22 14:50:54 -04008902 /*
8903 * Assign MSI-X vectors to interrupt handlers
8904 */
8905
8906 /* vector-0 is associated to slow-path handler */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08008907 rc = request_irq(pci_irq_vector(phba->pcidev, 0),
James Smarted243d32015-05-21 13:55:25 -04008908 &lpfc_sli_sp_intr_handler, 0,
James Smart3772a992009-05-22 14:50:54 -04008909 LPFC_SP_DRIVER_HANDLER_NAME, phba);
8910 if (rc) {
8911 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8912 "0421 MSI-X slow-path request_irq failed "
8913 "(%d)\n", rc);
8914 goto msi_fail_out;
8915 }
8916
8917 /* vector-1 is associated to fast-path handler */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08008918 rc = request_irq(pci_irq_vector(phba->pcidev, 1),
James Smarted243d32015-05-21 13:55:25 -04008919 &lpfc_sli_fp_intr_handler, 0,
James Smart3772a992009-05-22 14:50:54 -04008920 LPFC_FP_DRIVER_HANDLER_NAME, phba);
8921
8922 if (rc) {
8923 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8924 "0429 MSI-X fast-path request_irq failed "
8925 "(%d)\n", rc);
8926 goto irq_fail_out;
8927 }
8928
8929 /*
8930 * Configure HBA MSI-X attention conditions to messages
8931 */
8932 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8933
8934 if (!pmb) {
8935 rc = -ENOMEM;
8936 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8937 "0474 Unable to allocate memory for issuing "
8938 "MBOX_CONFIG_MSI command\n");
8939 goto mem_fail_out;
8940 }
8941 rc = lpfc_config_msi(phba, pmb);
8942 if (rc)
8943 goto mbx_fail_out;
8944 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
8945 if (rc != MBX_SUCCESS) {
8946 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
8947 "0351 Config MSI mailbox command failed, "
8948 "mbxCmd x%x, mbxStatus x%x\n",
8949 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
8950 goto mbx_fail_out;
8951 }
8952
8953 /* Free memory allocated for mailbox command */
8954 mempool_free(pmb, phba->mbox_mem_pool);
8955 return rc;
8956
8957mbx_fail_out:
8958 /* Free memory allocated for mailbox command */
8959 mempool_free(pmb, phba->mbox_mem_pool);
8960
8961mem_fail_out:
8962 /* free the irq already requested */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08008963 free_irq(pci_irq_vector(phba->pcidev, 1), phba);
James Smart3772a992009-05-22 14:50:54 -04008964
8965irq_fail_out:
8966 /* free the irq already requested */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08008967 free_irq(pci_irq_vector(phba->pcidev, 0), phba);
James Smart3772a992009-05-22 14:50:54 -04008968
8969msi_fail_out:
8970 /* Unconfigure MSI-X capability structure */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08008971 pci_free_irq_vectors(phba->pcidev);
Alexander Gordeev029165a2014-07-16 20:05:15 +02008972
8973vec_fail_out:
James Smart3772a992009-05-22 14:50:54 -04008974 return rc;
8975}
8976
8977/**
James Smart3772a992009-05-22 14:50:54 -04008978 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
8979 * @phba: pointer to lpfc hba data structure.
8980 *
8981 * This routine is invoked to enable the MSI interrupt mode to device with
8982 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
8983 * enable the MSI vector. The device driver is responsible for calling the
8984 * request_irq() to register MSI vector with a interrupt the handler, which
8985 * is done in this function.
8986 *
8987 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008988 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008989 * other values - error
8990 */
8991static int
8992lpfc_sli_enable_msi(struct lpfc_hba *phba)
8993{
8994 int rc;
8995
8996 rc = pci_enable_msi(phba->pcidev);
8997 if (!rc)
8998 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8999 "0462 PCI enable MSI mode success.\n");
9000 else {
9001 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9002 "0471 PCI enable MSI mode failed (%d)\n", rc);
9003 return rc;
9004 }
9005
9006 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
James Smarted243d32015-05-21 13:55:25 -04009007 0, LPFC_DRIVER_NAME, phba);
James Smart3772a992009-05-22 14:50:54 -04009008 if (rc) {
9009 pci_disable_msi(phba->pcidev);
9010 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9011 "0478 MSI request_irq failed (%d)\n", rc);
9012 }
9013 return rc;
9014}
9015
9016/**
James Smart3772a992009-05-22 14:50:54 -04009017 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
9018 * @phba: pointer to lpfc hba data structure.
9019 *
9020 * This routine is invoked to enable device interrupt and associate driver's
9021 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
9022 * spec. Depends on the interrupt mode configured to the driver, the driver
9023 * will try to fallback from the configured interrupt mode to an interrupt
9024 * mode which is supported by the platform, kernel, and device in the order
9025 * of:
9026 * MSI-X -> MSI -> IRQ.
9027 *
9028 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009029 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04009030 * other values - error
9031 **/
9032static uint32_t
9033lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
9034{
9035 uint32_t intr_mode = LPFC_INTR_ERROR;
9036 int retval;
9037
9038 if (cfg_mode == 2) {
9039 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
9040 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
9041 if (!retval) {
9042 /* Now, try to enable MSI-X interrupt mode */
9043 retval = lpfc_sli_enable_msix(phba);
9044 if (!retval) {
9045 /* Indicate initialization to MSI-X mode */
9046 phba->intr_type = MSIX;
9047 intr_mode = 2;
9048 }
9049 }
9050 }
9051
9052 /* Fallback to MSI if MSI-X initialization failed */
9053 if (cfg_mode >= 1 && phba->intr_type == NONE) {
9054 retval = lpfc_sli_enable_msi(phba);
9055 if (!retval) {
9056 /* Indicate initialization to MSI mode */
9057 phba->intr_type = MSI;
9058 intr_mode = 1;
9059 }
9060 }
9061
9062 /* Fallback to INTx if both MSI-X/MSI initalization failed */
9063 if (phba->intr_type == NONE) {
9064 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
9065 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
9066 if (!retval) {
9067 /* Indicate initialization to INTx mode */
9068 phba->intr_type = INTx;
9069 intr_mode = 0;
9070 }
9071 }
9072 return intr_mode;
9073}
9074
9075/**
9076 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
9077 * @phba: pointer to lpfc hba data structure.
9078 *
9079 * This routine is invoked to disable device interrupt and disassociate the
9080 * driver's interrupt handler(s) from interrupt vector(s) to device with
9081 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
9082 * release the interrupt vector(s) for the message signaled interrupt.
9083 **/
9084static void
9085lpfc_sli_disable_intr(struct lpfc_hba *phba)
9086{
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009087 int nr_irqs, i;
9088
James Smart3772a992009-05-22 14:50:54 -04009089 if (phba->intr_type == MSIX)
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009090 nr_irqs = LPFC_MSIX_VECTORS;
9091 else
9092 nr_irqs = 1;
9093
9094 for (i = 0; i < nr_irqs; i++)
9095 free_irq(pci_irq_vector(phba->pcidev, i), phba);
9096 pci_free_irq_vectors(phba->pcidev);
James Smart3772a992009-05-22 14:50:54 -04009097
9098 /* Reset interrupt management states */
9099 phba->intr_type = NONE;
9100 phba->sli.slistat.sli_intr = 0;
James Smart3772a992009-05-22 14:50:54 -04009101}
9102
9103/**
James Smart895427b2017-02-12 13:52:30 -08009104 * lpfc_cpu_affinity_check - Check vector CPU affinity mappings
James Smart7bb03bb2013-04-17 20:19:16 -04009105 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -08009106 * @vectors: number of msix vectors allocated.
James Smart7bb03bb2013-04-17 20:19:16 -04009107 *
James Smart895427b2017-02-12 13:52:30 -08009108 * The routine will figure out the CPU affinity assignment for every
9109 * MSI-X vector allocated for the HBA. The hba_eq_hdl will be updated
9110 * with a pointer to the CPU mask that defines ALL the CPUs this vector
9111 * can be associated with. If the vector can be unquely associated with
9112 * a single CPU, that CPU will be recorded in hba_eq_hdl[index].cpu.
9113 * In addition, the CPU to IO channel mapping will be calculated
9114 * and the phba->sli4_hba.cpu_map array will reflect this.
James Smart7bb03bb2013-04-17 20:19:16 -04009115 */
James Smart895427b2017-02-12 13:52:30 -08009116static void
9117lpfc_cpu_affinity_check(struct lpfc_hba *phba, int vectors)
James Smart7bb03bb2013-04-17 20:19:16 -04009118{
9119 struct lpfc_vector_map_info *cpup;
James Smart895427b2017-02-12 13:52:30 -08009120 int index = 0;
9121 int vec = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04009122 int cpu;
James Smart7bb03bb2013-04-17 20:19:16 -04009123#ifdef CONFIG_X86
9124 struct cpuinfo_x86 *cpuinfo;
9125#endif
James Smart7bb03bb2013-04-17 20:19:16 -04009126
9127 /* Init cpu_map array */
9128 memset(phba->sli4_hba.cpu_map, 0xff,
9129 (sizeof(struct lpfc_vector_map_info) *
James Smart895427b2017-02-12 13:52:30 -08009130 phba->sli4_hba.num_present_cpu));
James Smart7bb03bb2013-04-17 20:19:16 -04009131
9132 /* Update CPU map with physical id and core id of each CPU */
9133 cpup = phba->sli4_hba.cpu_map;
9134 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
9135#ifdef CONFIG_X86
9136 cpuinfo = &cpu_data(cpu);
9137 cpup->phys_id = cpuinfo->phys_proc_id;
9138 cpup->core_id = cpuinfo->cpu_core_id;
9139#else
9140 /* No distinction between CPUs for other platforms */
9141 cpup->phys_id = 0;
9142 cpup->core_id = 0;
9143#endif
James Smart895427b2017-02-12 13:52:30 -08009144 cpup->channel_id = index; /* For now round robin */
9145 cpup->irq = pci_irq_vector(phba->pcidev, vec);
9146 vec++;
9147 if (vec >= vectors)
9148 vec = 0;
9149 index++;
9150 if (index >= phba->cfg_fcp_io_channel)
9151 index = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04009152 cpup++;
9153 }
James Smart7bb03bb2013-04-17 20:19:16 -04009154}
9155
9156
9157/**
James Smartda0436e2009-05-22 14:51:39 -04009158 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
9159 * @phba: pointer to lpfc hba data structure.
9160 *
9161 * This routine is invoked to enable the MSI-X interrupt vectors to device
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009162 * with SLI-4 interface spec.
James Smartda0436e2009-05-22 14:51:39 -04009163 *
9164 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009165 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04009166 * other values - error
9167 **/
9168static int
9169lpfc_sli4_enable_msix(struct lpfc_hba *phba)
9170{
James Smart75baf692010-06-08 18:31:21 -04009171 int vectors, rc, index;
James Smartda0436e2009-05-22 14:51:39 -04009172
9173 /* Set up MSI-X multi-message vectors */
James Smart895427b2017-02-12 13:52:30 -08009174 vectors = phba->io_channel_irqs;
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009175 if (phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05009176 vectors++;
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009177
James Smart895427b2017-02-12 13:52:30 -08009178 rc = pci_alloc_irq_vectors(phba->pcidev, 2, vectors,
9179 PCI_IRQ_MSIX | PCI_IRQ_AFFINITY);
Alexander Gordeev4f871e12014-09-03 12:56:29 -04009180 if (rc < 0) {
James Smartda0436e2009-05-22 14:51:39 -04009181 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9182 "0484 PCI enable MSI-X failed (%d)\n", rc);
Alexander Gordeev029165a2014-07-16 20:05:15 +02009183 goto vec_fail_out;
James Smartda0436e2009-05-22 14:51:39 -04009184 }
Alexander Gordeev4f871e12014-09-03 12:56:29 -04009185 vectors = rc;
James Smart75baf692010-06-08 18:31:21 -04009186
James Smart7bb03bb2013-04-17 20:19:16 -04009187 /* Assign MSI-X vectors to interrupt handlers */
James Smart67d12732012-08-03 12:36:13 -04009188 for (index = 0; index < vectors; index++) {
James Smart4305f182012-08-03 12:36:33 -04009189 memset(&phba->sli4_hba.handler_name[index], 0, 16);
James Smarta2fc4aef2014-09-03 12:57:55 -04009190 snprintf((char *)&phba->sli4_hba.handler_name[index],
9191 LPFC_SLI4_HANDLER_NAME_SZ,
James Smart4305f182012-08-03 12:36:33 -04009192 LPFC_DRIVER_HANDLER_NAME"%d", index);
James Smartda0436e2009-05-22 14:51:39 -04009193
James Smart895427b2017-02-12 13:52:30 -08009194 phba->sli4_hba.hba_eq_hdl[index].idx = index;
9195 phba->sli4_hba.hba_eq_hdl[index].phba = phba;
9196 atomic_set(&phba->sli4_hba.hba_eq_hdl[index].hba_eq_in_use, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009197 if (phba->cfg_fof && (index == (vectors - 1)))
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009198 rc = request_irq(pci_irq_vector(phba->pcidev, index),
James Smarted243d32015-05-21 13:55:25 -04009199 &lpfc_sli4_fof_intr_handler, 0,
James Smart1ba981f2014-02-20 09:56:45 -05009200 (char *)&phba->sli4_hba.handler_name[index],
James Smart895427b2017-02-12 13:52:30 -08009201 &phba->sli4_hba.hba_eq_hdl[index]);
James Smart1ba981f2014-02-20 09:56:45 -05009202 else
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009203 rc = request_irq(pci_irq_vector(phba->pcidev, index),
James Smarted243d32015-05-21 13:55:25 -04009204 &lpfc_sli4_hba_intr_handler, 0,
James Smart4305f182012-08-03 12:36:33 -04009205 (char *)&phba->sli4_hba.handler_name[index],
James Smart895427b2017-02-12 13:52:30 -08009206 &phba->sli4_hba.hba_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -04009207 if (rc) {
9208 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9209 "0486 MSI-X fast-path (%d) "
9210 "request_irq failed (%d)\n", index, rc);
9211 goto cfg_fail_out;
9212 }
9213 }
9214
James Smart1ba981f2014-02-20 09:56:45 -05009215 if (phba->cfg_fof)
9216 vectors--;
9217
James Smart895427b2017-02-12 13:52:30 -08009218 if (vectors != phba->io_channel_irqs) {
James Smart82c3e9b2012-09-29 11:29:50 -04009219 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9220 "3238 Reducing IO channels to match number of "
9221 "MSI-X vectors, requested %d got %d\n",
James Smart895427b2017-02-12 13:52:30 -08009222 phba->io_channel_irqs, vectors);
9223 if (phba->cfg_fcp_io_channel > vectors)
9224 phba->cfg_fcp_io_channel = vectors;
9225 if (phba->cfg_nvme_io_channel > vectors)
9226 phba->cfg_nvme_io_channel = vectors;
9227 if (phba->cfg_fcp_io_channel > phba->cfg_nvme_io_channel)
9228 phba->io_channel_irqs = phba->cfg_fcp_io_channel;
9229 else
9230 phba->io_channel_irqs = phba->cfg_nvme_io_channel;
James Smart82c3e9b2012-09-29 11:29:50 -04009231 }
James Smart895427b2017-02-12 13:52:30 -08009232 lpfc_cpu_affinity_check(phba, vectors);
James Smart7bb03bb2013-04-17 20:19:16 -04009233
James Smartda0436e2009-05-22 14:51:39 -04009234 return rc;
9235
9236cfg_fail_out:
9237 /* free the irq already requested */
James Smart895427b2017-02-12 13:52:30 -08009238 for (--index; index >= 0; index--)
9239 free_irq(pci_irq_vector(phba->pcidev, index),
9240 &phba->sli4_hba.hba_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -04009241
James Smartda0436e2009-05-22 14:51:39 -04009242 /* Unconfigure MSI-X capability structure */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009243 pci_free_irq_vectors(phba->pcidev);
Alexander Gordeev029165a2014-07-16 20:05:15 +02009244
9245vec_fail_out:
James Smartda0436e2009-05-22 14:51:39 -04009246 return rc;
9247}
9248
9249/**
James Smartda0436e2009-05-22 14:51:39 -04009250 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
9251 * @phba: pointer to lpfc hba data structure.
9252 *
9253 * This routine is invoked to enable the MSI interrupt mode to device with
9254 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
9255 * to enable the MSI vector. The device driver is responsible for calling
9256 * the request_irq() to register MSI vector with a interrupt the handler,
9257 * which is done in this function.
9258 *
9259 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009260 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04009261 * other values - error
9262 **/
9263static int
9264lpfc_sli4_enable_msi(struct lpfc_hba *phba)
9265{
9266 int rc, index;
9267
9268 rc = pci_enable_msi(phba->pcidev);
9269 if (!rc)
9270 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9271 "0487 PCI enable MSI mode success.\n");
9272 else {
9273 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9274 "0488 PCI enable MSI mode failed (%d)\n", rc);
9275 return rc;
9276 }
9277
9278 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
James Smarted243d32015-05-21 13:55:25 -04009279 0, LPFC_DRIVER_NAME, phba);
James Smartda0436e2009-05-22 14:51:39 -04009280 if (rc) {
9281 pci_disable_msi(phba->pcidev);
9282 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9283 "0490 MSI request_irq failed (%d)\n", rc);
James Smart75baf692010-06-08 18:31:21 -04009284 return rc;
James Smartda0436e2009-05-22 14:51:39 -04009285 }
9286
James Smart895427b2017-02-12 13:52:30 -08009287 for (index = 0; index < phba->io_channel_irqs; index++) {
9288 phba->sli4_hba.hba_eq_hdl[index].idx = index;
9289 phba->sli4_hba.hba_eq_hdl[index].phba = phba;
James Smartda0436e2009-05-22 14:51:39 -04009290 }
9291
James Smart1ba981f2014-02-20 09:56:45 -05009292 if (phba->cfg_fof) {
James Smart895427b2017-02-12 13:52:30 -08009293 phba->sli4_hba.hba_eq_hdl[index].idx = index;
9294 phba->sli4_hba.hba_eq_hdl[index].phba = phba;
James Smart1ba981f2014-02-20 09:56:45 -05009295 }
James Smart75baf692010-06-08 18:31:21 -04009296 return 0;
James Smartda0436e2009-05-22 14:51:39 -04009297}
9298
9299/**
James Smartda0436e2009-05-22 14:51:39 -04009300 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
9301 * @phba: pointer to lpfc hba data structure.
9302 *
9303 * This routine is invoked to enable device interrupt and associate driver's
9304 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
9305 * interface spec. Depends on the interrupt mode configured to the driver,
9306 * the driver will try to fallback from the configured interrupt mode to an
9307 * interrupt mode which is supported by the platform, kernel, and device in
9308 * the order of:
9309 * MSI-X -> MSI -> IRQ.
9310 *
9311 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009312 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04009313 * other values - error
9314 **/
9315static uint32_t
9316lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
9317{
9318 uint32_t intr_mode = LPFC_INTR_ERROR;
James Smart895427b2017-02-12 13:52:30 -08009319 int retval, idx;
James Smartda0436e2009-05-22 14:51:39 -04009320
9321 if (cfg_mode == 2) {
9322 /* Preparation before conf_msi mbox cmd */
9323 retval = 0;
9324 if (!retval) {
9325 /* Now, try to enable MSI-X interrupt mode */
9326 retval = lpfc_sli4_enable_msix(phba);
9327 if (!retval) {
9328 /* Indicate initialization to MSI-X mode */
9329 phba->intr_type = MSIX;
9330 intr_mode = 2;
9331 }
9332 }
9333 }
9334
9335 /* Fallback to MSI if MSI-X initialization failed */
9336 if (cfg_mode >= 1 && phba->intr_type == NONE) {
9337 retval = lpfc_sli4_enable_msi(phba);
9338 if (!retval) {
9339 /* Indicate initialization to MSI mode */
9340 phba->intr_type = MSI;
9341 intr_mode = 1;
9342 }
9343 }
9344
9345 /* Fallback to INTx if both MSI-X/MSI initalization failed */
9346 if (phba->intr_type == NONE) {
9347 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
9348 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
9349 if (!retval) {
James Smart895427b2017-02-12 13:52:30 -08009350 struct lpfc_hba_eq_hdl *eqhdl;
9351
James Smartda0436e2009-05-22 14:51:39 -04009352 /* Indicate initialization to INTx mode */
9353 phba->intr_type = INTx;
9354 intr_mode = 0;
James Smart895427b2017-02-12 13:52:30 -08009355
9356 for (idx = 0; idx < phba->io_channel_irqs; idx++) {
9357 eqhdl = &phba->sli4_hba.hba_eq_hdl[idx];
9358 eqhdl->idx = idx;
9359 eqhdl->phba = phba;
9360 atomic_set(&eqhdl->hba_eq_in_use, 1);
James Smartda0436e2009-05-22 14:51:39 -04009361 }
James Smart1ba981f2014-02-20 09:56:45 -05009362 if (phba->cfg_fof) {
James Smart895427b2017-02-12 13:52:30 -08009363 eqhdl = &phba->sli4_hba.hba_eq_hdl[idx];
9364 eqhdl->idx = idx;
9365 eqhdl->phba = phba;
9366 atomic_set(&eqhdl->hba_eq_in_use, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009367 }
James Smartda0436e2009-05-22 14:51:39 -04009368 }
9369 }
9370 return intr_mode;
9371}
9372
9373/**
9374 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
9375 * @phba: pointer to lpfc hba data structure.
9376 *
9377 * This routine is invoked to disable device interrupt and disassociate
9378 * the driver's interrupt handler(s) from interrupt vector(s) to device
9379 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
9380 * will release the interrupt vector(s) for the message signaled interrupt.
9381 **/
9382static void
9383lpfc_sli4_disable_intr(struct lpfc_hba *phba)
9384{
9385 /* Disable the currently initialized interrupt mode */
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009386 if (phba->intr_type == MSIX) {
9387 int index;
9388
9389 /* Free up MSI-X multi-message vectors */
James Smart895427b2017-02-12 13:52:30 -08009390 for (index = 0; index < phba->io_channel_irqs; index++)
9391 free_irq(pci_irq_vector(phba->pcidev, index),
9392 &phba->sli4_hba.hba_eq_hdl[index]);
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009393
9394 if (phba->cfg_fof)
James Smart895427b2017-02-12 13:52:30 -08009395 free_irq(pci_irq_vector(phba->pcidev, index),
9396 &phba->sli4_hba.hba_eq_hdl[index]);
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009397 } else {
James Smartda0436e2009-05-22 14:51:39 -04009398 free_irq(phba->pcidev->irq, phba);
Christoph Hellwig45ffac12017-02-12 13:52:26 -08009399 }
9400
9401 pci_free_irq_vectors(phba->pcidev);
James Smartda0436e2009-05-22 14:51:39 -04009402
9403 /* Reset interrupt management states */
9404 phba->intr_type = NONE;
9405 phba->sli.slistat.sli_intr = 0;
James Smartda0436e2009-05-22 14:51:39 -04009406}
9407
9408/**
James Smart3772a992009-05-22 14:50:54 -04009409 * lpfc_unset_hba - Unset SLI3 hba device initialization
9410 * @phba: pointer to lpfc hba data structure.
9411 *
9412 * This routine is invoked to unset the HBA device initialization steps to
9413 * a device with SLI-3 interface spec.
9414 **/
9415static void
9416lpfc_unset_hba(struct lpfc_hba *phba)
9417{
9418 struct lpfc_vport *vport = phba->pport;
9419 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
9420
9421 spin_lock_irq(shost->host_lock);
9422 vport->load_flag |= FC_UNLOADING;
9423 spin_unlock_irq(shost->host_lock);
9424
James Smart72859902012-01-18 16:25:38 -05009425 kfree(phba->vpi_bmask);
9426 kfree(phba->vpi_ids);
9427
James Smart3772a992009-05-22 14:50:54 -04009428 lpfc_stop_hba_timers(phba);
9429
9430 phba->pport->work_port_events = 0;
9431
9432 lpfc_sli_hba_down(phba);
9433
9434 lpfc_sli_brdrestart(phba);
9435
9436 lpfc_sli_disable_intr(phba);
9437
9438 return;
9439}
9440
9441/**
James Smart5af5eee2010-10-22 11:06:38 -04009442 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
9443 * @phba: Pointer to HBA context object.
9444 *
9445 * This function is called in the SLI4 code path to wait for completion
9446 * of device's XRIs exchange busy. It will check the XRI exchange busy
9447 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
9448 * that, it will check the XRI exchange busy on outstanding FCP and ELS
9449 * I/Os every 30 seconds, log error message, and wait forever. Only when
9450 * all XRI exchange busy complete, the driver unload shall proceed with
9451 * invoking the function reset ioctl mailbox command to the CNA and the
9452 * the rest of the driver unload resource release.
9453 **/
9454static void
9455lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
9456{
9457 int wait_time = 0;
James Smart895427b2017-02-12 13:52:30 -08009458 int nvme_xri_cmpl = 1;
9459 int fcp_xri_cmpl = 1;
James Smart5af5eee2010-10-22 11:06:38 -04009460 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
9461
James Smart895427b2017-02-12 13:52:30 -08009462 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP)
9463 fcp_xri_cmpl =
9464 list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
9465 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
9466 nvme_xri_cmpl =
9467 list_empty(&phba->sli4_hba.lpfc_abts_nvme_buf_list);
9468
9469 while (!fcp_xri_cmpl || !els_xri_cmpl || !nvme_xri_cmpl) {
James Smart5af5eee2010-10-22 11:06:38 -04009470 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
James Smart895427b2017-02-12 13:52:30 -08009471 if (!nvme_xri_cmpl)
9472 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9473 "6100 NVME XRI exchange busy "
9474 "wait time: %d seconds.\n",
9475 wait_time/1000);
James Smart5af5eee2010-10-22 11:06:38 -04009476 if (!fcp_xri_cmpl)
9477 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9478 "2877 FCP XRI exchange busy "
9479 "wait time: %d seconds.\n",
9480 wait_time/1000);
9481 if (!els_xri_cmpl)
9482 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9483 "2878 ELS XRI exchange busy "
9484 "wait time: %d seconds.\n",
9485 wait_time/1000);
9486 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
9487 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
9488 } else {
9489 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
9490 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
9491 }
James Smart895427b2017-02-12 13:52:30 -08009492 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
9493 nvme_xri_cmpl = list_empty(
9494 &phba->sli4_hba.lpfc_abts_nvme_buf_list);
9495
9496 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP)
9497 fcp_xri_cmpl = list_empty(
9498 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
9499
James Smart5af5eee2010-10-22 11:06:38 -04009500 els_xri_cmpl =
9501 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
9502 }
9503}
9504
9505/**
James Smartda0436e2009-05-22 14:51:39 -04009506 * lpfc_sli4_hba_unset - Unset the fcoe hba
9507 * @phba: Pointer to HBA context object.
9508 *
9509 * This function is called in the SLI4 code path to reset the HBA's FCoE
9510 * function. The caller is not required to hold any lock. This routine
9511 * issues PCI function reset mailbox command to reset the FCoE function.
9512 * At the end of the function, it calls lpfc_hba_down_post function to
9513 * free any pending commands.
9514 **/
9515static void
9516lpfc_sli4_hba_unset(struct lpfc_hba *phba)
9517{
9518 int wait_cnt = 0;
9519 LPFC_MBOXQ_t *mboxq;
James Smart912e3ac2011-05-24 11:42:11 -04009520 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -04009521
9522 lpfc_stop_hba_timers(phba);
9523 phba->sli4_hba.intr_enable = 0;
9524
9525 /*
9526 * Gracefully wait out the potential current outstanding asynchronous
9527 * mailbox command.
9528 */
9529
9530 /* First, block any pending async mailbox command from posted */
9531 spin_lock_irq(&phba->hbalock);
9532 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
9533 spin_unlock_irq(&phba->hbalock);
9534 /* Now, trying to wait it out if we can */
9535 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9536 msleep(10);
9537 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
9538 break;
9539 }
9540 /* Forcefully release the outstanding mailbox command if timed out */
9541 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9542 spin_lock_irq(&phba->hbalock);
9543 mboxq = phba->sli.mbox_active;
9544 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
9545 __lpfc_mbox_cmpl_put(phba, mboxq);
9546 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9547 phba->sli.mbox_active = NULL;
9548 spin_unlock_irq(&phba->hbalock);
9549 }
9550
James Smart5af5eee2010-10-22 11:06:38 -04009551 /* Abort all iocbs associated with the hba */
9552 lpfc_sli_hba_iocb_abort(phba);
9553
9554 /* Wait for completion of device XRI exchange busy */
9555 lpfc_sli4_xri_exchange_busy_wait(phba);
9556
James Smartda0436e2009-05-22 14:51:39 -04009557 /* Disable PCI subsystem interrupt */
9558 lpfc_sli4_disable_intr(phba);
9559
James Smart912e3ac2011-05-24 11:42:11 -04009560 /* Disable SR-IOV if enabled */
9561 if (phba->cfg_sriov_nr_virtfn)
9562 pci_disable_sriov(pdev);
9563
James Smartda0436e2009-05-22 14:51:39 -04009564 /* Stop kthread signal shall trigger work_done one more time */
9565 kthread_stop(phba->worker_thread);
9566
James Smart3677a3a2010-09-29 11:19:14 -04009567 /* Reset SLI4 HBA FCoE function */
9568 lpfc_pci_function_reset(phba);
James Smart5350d872011-10-10 21:33:49 -04009569 lpfc_sli4_queue_destroy(phba);
James Smart3677a3a2010-09-29 11:19:14 -04009570
James Smartda0436e2009-05-22 14:51:39 -04009571 /* Stop the SLI4 device port */
9572 phba->pport->work_port_events = 0;
9573}
9574
James Smart28baac72010-02-12 14:42:03 -05009575 /**
9576 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
9577 * @phba: Pointer to HBA context object.
9578 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
9579 *
9580 * This function is called in the SLI4 code path to read the port's
9581 * sli4 capabilities.
9582 *
9583 * This function may be be called from any context that can block-wait
9584 * for the completion. The expectation is that this routine is called
9585 * typically from probe_one or from the online routine.
9586 **/
9587int
9588lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9589{
9590 int rc;
9591 struct lpfc_mqe *mqe;
9592 struct lpfc_pc_sli4_params *sli4_params;
9593 uint32_t mbox_tmo;
9594
9595 rc = 0;
9596 mqe = &mboxq->u.mqe;
9597
9598 /* Read the port's SLI4 Parameters port capabilities */
James Smartfedd3b72011-02-16 12:39:24 -05009599 lpfc_pc_sli4_params(mboxq);
James Smart28baac72010-02-12 14:42:03 -05009600 if (!phba->sli4_hba.intr_enable)
9601 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9602 else {
James Smarta183a152011-10-10 21:32:43 -04009603 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart28baac72010-02-12 14:42:03 -05009604 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
9605 }
9606
9607 if (unlikely(rc))
9608 return 1;
9609
9610 sli4_params = &phba->sli4_hba.pc_sli4_params;
9611 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
9612 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
9613 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
9614 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
9615 &mqe->un.sli4_params);
9616 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
9617 &mqe->un.sli4_params);
9618 sli4_params->proto_types = mqe->un.sli4_params.word3;
9619 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
9620 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
9621 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
9622 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
9623 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
9624 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
9625 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
9626 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
9627 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
9628 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
9629 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
9630 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
9631 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
9632 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
9633 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
9634 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
9635 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
9636 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
9637 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
9638 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
James Smart05580562011-05-24 11:40:48 -04009639
9640 /* Make sure that sge_supp_len can be handled by the driver */
9641 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
9642 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
9643
James Smart28baac72010-02-12 14:42:03 -05009644 return rc;
9645}
9646
James Smartda0436e2009-05-22 14:51:39 -04009647/**
James Smartfedd3b72011-02-16 12:39:24 -05009648 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
9649 * @phba: Pointer to HBA context object.
9650 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
9651 *
9652 * This function is called in the SLI4 code path to read the port's
9653 * sli4 capabilities.
9654 *
9655 * This function may be be called from any context that can block-wait
9656 * for the completion. The expectation is that this routine is called
9657 * typically from probe_one or from the online routine.
9658 **/
9659int
9660lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9661{
9662 int rc;
9663 struct lpfc_mqe *mqe = &mboxq->u.mqe;
9664 struct lpfc_pc_sli4_params *sli4_params;
James Smarta183a152011-10-10 21:32:43 -04009665 uint32_t mbox_tmo;
James Smartfedd3b72011-02-16 12:39:24 -05009666 int length;
9667 struct lpfc_sli4_parameters *mbx_sli4_parameters;
9668
James Smart6d368e52011-05-24 11:44:12 -04009669 /*
9670 * By default, the driver assumes the SLI4 port requires RPI
9671 * header postings. The SLI4_PARAM response will correct this
9672 * assumption.
9673 */
9674 phba->sli4_hba.rpi_hdrs_in_use = 1;
9675
James Smartfedd3b72011-02-16 12:39:24 -05009676 /* Read the port's SLI4 Config Parameters */
9677 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
9678 sizeof(struct lpfc_sli4_cfg_mhdr));
9679 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
9680 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
9681 length, LPFC_SLI4_MBX_EMBED);
9682 if (!phba->sli4_hba.intr_enable)
9683 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -04009684 else {
9685 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
9686 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
9687 }
James Smartfedd3b72011-02-16 12:39:24 -05009688 if (unlikely(rc))
9689 return rc;
9690 sli4_params = &phba->sli4_hba.pc_sli4_params;
9691 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
9692 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
9693 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
9694 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
9695 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
9696 mbx_sli4_parameters);
9697 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
9698 mbx_sli4_parameters);
9699 if (bf_get(cfg_phwq, mbx_sli4_parameters))
9700 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
9701 else
9702 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
9703 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
9704 sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
James Smart1ba981f2014-02-20 09:56:45 -05009705 sli4_params->oas_supported = bf_get(cfg_oas, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -05009706 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
9707 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
9708 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
9709 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
James Smart0c651872013-07-15 18:33:23 -04009710 sli4_params->wqsize = bf_get(cfg_wqsize, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -05009711 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
9712 mbx_sli4_parameters);
James Smart895427b2017-02-12 13:52:30 -08009713 sli4_params->wqpcnt = bf_get(cfg_wqpcnt, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -05009714 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
9715 mbx_sli4_parameters);
James Smart6d368e52011-05-24 11:44:12 -04009716 phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
9717 phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
James Smart895427b2017-02-12 13:52:30 -08009718 phba->nvme_support = (bf_get(cfg_nvme, mbx_sli4_parameters) &&
9719 bf_get(cfg_xib, mbx_sli4_parameters));
9720
9721 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP) ||
9722 !phba->nvme_support) {
9723 phba->nvme_support = 0;
9724 phba->nvmet_support = 0;
9725 phba->cfg_nvme_io_channel = 0;
9726 phba->io_channel_irqs = phba->cfg_fcp_io_channel;
9727 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_NVME,
9728 "6101 Disabling NVME support: "
9729 "Not supported by firmware: %d %d\n",
9730 bf_get(cfg_nvme, mbx_sli4_parameters),
9731 bf_get(cfg_xib, mbx_sli4_parameters));
9732
9733 /* If firmware doesn't support NVME, just use SCSI support */
9734 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
9735 return -ENODEV;
9736 phba->cfg_enable_fc4_type = LPFC_ENABLE_FCP;
9737 }
James Smart05580562011-05-24 11:40:48 -04009738
9739 /* Make sure that sge_supp_len can be handled by the driver */
9740 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
9741 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
9742
James Smartb5c53952016-03-31 14:12:30 -07009743 /*
9744 * Issue IOs with CDB embedded in WQE to minimized the number
9745 * of DMAs the firmware has to do. Setting this to 1 also forces
9746 * the driver to use 128 bytes WQEs for FCP IOs.
9747 */
9748 if (bf_get(cfg_ext_embed_cb, mbx_sli4_parameters))
9749 phba->fcp_embed_io = 1;
9750 else
9751 phba->fcp_embed_io = 0;
James Smart7bdedb32016-07-06 12:36:00 -07009752
9753 /*
9754 * Check if the SLI port supports MDS Diagnostics
9755 */
9756 if (bf_get(cfg_mds_diags, mbx_sli4_parameters))
9757 phba->mds_diags_support = 1;
9758 else
9759 phba->mds_diags_support = 0;
James Smartfedd3b72011-02-16 12:39:24 -05009760 return 0;
9761}
9762
9763/**
James Smart3772a992009-05-22 14:50:54 -04009764 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
9765 * @pdev: pointer to PCI device
9766 * @pid: pointer to PCI device identifier
9767 *
9768 * This routine is to be called to attach a device with SLI-3 interface spec
9769 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
9770 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
9771 * information of the device and driver to see if the driver state that it can
9772 * support this kind of device. If the match is successful, the driver core
9773 * invokes this routine. If this routine determines it can claim the HBA, it
9774 * does all the initialization that it needs to do to handle the HBA properly.
9775 *
9776 * Return code
9777 * 0 - driver can claim the device
9778 * negative value - driver can not claim the device
9779 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009780static int
James Smart3772a992009-05-22 14:50:54 -04009781lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
9782{
9783 struct lpfc_hba *phba;
9784 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04009785 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -04009786 int error;
9787 uint32_t cfg_mode, intr_mode;
9788
9789 /* Allocate memory for HBA structure */
9790 phba = lpfc_hba_alloc(pdev);
9791 if (!phba)
9792 return -ENOMEM;
9793
9794 /* Perform generic PCI device enabling operation */
9795 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -04009796 if (error)
James Smart3772a992009-05-22 14:50:54 -04009797 goto out_free_phba;
James Smart3772a992009-05-22 14:50:54 -04009798
9799 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
9800 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
9801 if (error)
9802 goto out_disable_pci_dev;
9803
9804 /* Set up SLI-3 specific device PCI memory space */
9805 error = lpfc_sli_pci_mem_setup(phba);
9806 if (error) {
9807 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9808 "1402 Failed to set up pci memory space.\n");
9809 goto out_disable_pci_dev;
9810 }
9811
James Smart3772a992009-05-22 14:50:54 -04009812 /* Set up SLI-3 specific device driver resources */
9813 error = lpfc_sli_driver_resource_setup(phba);
9814 if (error) {
9815 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9816 "1404 Failed to set up driver resource.\n");
9817 goto out_unset_pci_mem_s3;
9818 }
9819
9820 /* Initialize and populate the iocb list per host */
9821 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
9822 if (error) {
9823 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9824 "1405 Failed to initialize iocb list.\n");
9825 goto out_unset_driver_resource_s3;
9826 }
9827
9828 /* Set up common device driver resources */
9829 error = lpfc_setup_driver_resource_phase2(phba);
9830 if (error) {
9831 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9832 "1406 Failed to set up driver resource.\n");
9833 goto out_free_iocb_list;
9834 }
9835
James Smart079b5c92011-08-21 21:48:49 -04009836 /* Get the default values for Model Name and Description */
9837 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
9838
James Smart3772a992009-05-22 14:50:54 -04009839 /* Create SCSI host to the physical port */
9840 error = lpfc_create_shost(phba);
9841 if (error) {
9842 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9843 "1407 Failed to create scsi host.\n");
9844 goto out_unset_driver_resource;
9845 }
9846
9847 /* Configure sysfs attributes */
9848 vport = phba->pport;
9849 error = lpfc_alloc_sysfs_attr(vport);
9850 if (error) {
9851 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9852 "1476 Failed to allocate sysfs attr\n");
9853 goto out_destroy_shost;
9854 }
9855
James Smart6669f9b2009-10-02 15:16:45 -04009856 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -04009857 /* Now, trying to enable interrupt and bring up the device */
9858 cfg_mode = phba->cfg_use_msi;
9859 while (true) {
9860 /* Put device to a known state before enabling interrupt */
9861 lpfc_stop_port(phba);
9862 /* Configure and enable interrupt */
9863 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
9864 if (intr_mode == LPFC_INTR_ERROR) {
9865 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9866 "0431 Failed to enable interrupt.\n");
9867 error = -ENODEV;
9868 goto out_free_sysfs_attr;
9869 }
9870 /* SLI-3 HBA setup */
9871 if (lpfc_sli_hba_setup(phba)) {
9872 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9873 "1477 Failed to set up hba\n");
9874 error = -ENODEV;
9875 goto out_remove_device;
9876 }
9877
9878 /* Wait 50ms for the interrupts of previous mailbox commands */
9879 msleep(50);
9880 /* Check active interrupts on message signaled interrupts */
9881 if (intr_mode == 0 ||
9882 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
9883 /* Log the current active interrupt mode */
9884 phba->intr_mode = intr_mode;
9885 lpfc_log_intr_mode(phba, intr_mode);
9886 break;
9887 } else {
9888 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9889 "0447 Configure interrupt mode (%d) "
9890 "failed active interrupt test.\n",
9891 intr_mode);
9892 /* Disable the current interrupt mode */
9893 lpfc_sli_disable_intr(phba);
9894 /* Try next level of interrupt mode */
9895 cfg_mode = --intr_mode;
9896 }
9897 }
9898
9899 /* Perform post initialization setup */
9900 lpfc_post_init_setup(phba);
9901
9902 /* Check if there are static vports to be created. */
9903 lpfc_create_static_vport(phba);
9904
9905 return 0;
9906
9907out_remove_device:
9908 lpfc_unset_hba(phba);
9909out_free_sysfs_attr:
9910 lpfc_free_sysfs_attr(vport);
9911out_destroy_shost:
9912 lpfc_destroy_shost(phba);
9913out_unset_driver_resource:
9914 lpfc_unset_driver_resource_phase2(phba);
9915out_free_iocb_list:
9916 lpfc_free_iocb_list(phba);
9917out_unset_driver_resource_s3:
9918 lpfc_sli_driver_resource_unset(phba);
9919out_unset_pci_mem_s3:
9920 lpfc_sli_pci_mem_unset(phba);
9921out_disable_pci_dev:
9922 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04009923 if (shost)
9924 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -04009925out_free_phba:
9926 lpfc_hba_free(phba);
9927 return error;
9928}
9929
9930/**
9931 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -04009932 * @pdev: pointer to PCI device
9933 *
James Smart3772a992009-05-22 14:50:54 -04009934 * This routine is to be called to disattach a device with SLI-3 interface
9935 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
9936 * removed from PCI bus, it performs all the necessary cleanup for the HBA
9937 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -04009938 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009939static void
James Smart3772a992009-05-22 14:50:54 -04009940lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -05009941{
James Smart2e0fef82007-06-17 19:56:36 -05009942 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9943 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -05009944 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -05009945 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -05009946 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05009947
James Smart549e55c2007-08-02 11:09:51 -04009948 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04009949 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04009950 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009951
James Smart858c9f62007-06-17 19:56:39 -05009952 lpfc_free_sysfs_attr(vport);
9953
James Smarteada2722008-12-04 22:39:13 -05009954 /* Release all the vports against this physical port */
9955 vports = lpfc_create_vport_work_array(phba);
9956 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -04009957 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
9958 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
9959 continue;
James Smarteada2722008-12-04 22:39:13 -05009960 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -04009961 }
James Smarteada2722008-12-04 22:39:13 -05009962 lpfc_destroy_vport_work_array(phba, vports);
9963
9964 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -05009965 fc_remove_host(shost);
9966 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04009967 lpfc_cleanup(vport);
9968
James Smart2e0fef82007-06-17 19:56:36 -05009969 /*
9970 * Bring down the SLI Layer. This step disable all interrupts,
9971 * clears the rings, discards all mailbox commands, and resets
9972 * the HBA.
9973 */
James Smarta257bf92009-04-06 18:48:10 -04009974
Justin P. Mattock48e34d02010-12-30 15:07:58 -08009975 /* HBA interrupt will be disabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -05009976 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -04009977 /* Stop kthread signal shall trigger work_done one more time */
9978 kthread_stop(phba->worker_thread);
9979 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -05009980 lpfc_sli_brdrestart(phba);
9981
James Smart72859902012-01-18 16:25:38 -05009982 kfree(phba->vpi_bmask);
9983 kfree(phba->vpi_ids);
9984
James Smart3772a992009-05-22 14:50:54 -04009985 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05009986 spin_lock_irq(&phba->hbalock);
9987 list_del_init(&vport->listentry);
9988 spin_unlock_irq(&phba->hbalock);
9989
James Smart858c9f62007-06-17 19:56:39 -05009990 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05009991
James Smart912e3ac2011-05-24 11:42:11 -04009992 /* Disable SR-IOV if enabled */
9993 if (phba->cfg_sriov_nr_virtfn)
9994 pci_disable_sriov(pdev);
9995
James Smart5b75da22008-12-04 22:39:35 -05009996 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009997 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -05009998
James Smart858c9f62007-06-17 19:56:39 -05009999 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -050010000
10001 /*
10002 * Call scsi_free before mem_free since scsi bufs are released to their
10003 * corresponding pools here.
10004 */
10005 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -040010006 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -050010007
James Smart34b02dc2008-08-24 21:49:55 -040010008 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
10009 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -050010010
James Smart2e0fef82007-06-17 19:56:36 -050010011 /* Free resources associated with SLI2 interface */
10012 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -040010013 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -050010014
10015 /* unmap adapter SLIM and Control Registers */
10016 iounmap(phba->ctrl_regs_memmap_p);
10017 iounmap(phba->slim_memmap_p);
10018
James Smart3772a992009-05-22 14:50:54 -040010019 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -050010020
Johannes Thumshirne0c04832016-06-07 09:44:03 +020010021 pci_release_mem_regions(pdev);
James Smart2e0fef82007-06-17 19:56:36 -050010022 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -050010023}
10024
Linas Vepstas8d63f372007-02-14 14:28:36 -060010025/**
James Smart3772a992009-05-22 14:50:54 -040010026 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -050010027 * @pdev: pointer to PCI device
10028 * @msg: power management message
10029 *
James Smart3772a992009-05-22 14:50:54 -040010030 * This routine is to be called from the kernel's PCI subsystem to support
10031 * system Power Management (PM) to device with SLI-3 interface spec. When
10032 * PM invokes this method, it quiesces the device by stopping the driver's
10033 * worker thread for the device, turning off device's interrupt and DMA,
10034 * and bring the device offline. Note that as the driver implements the
10035 * minimum PM requirements to a power-aware driver's PM support for the
10036 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
10037 * to the suspend() method call will be treated as SUSPEND and the driver will
10038 * fully reinitialize its device during resume() method call, the driver will
10039 * set device to PCI_D3hot state in PCI config space instead of setting it
10040 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -050010041 *
10042 * Return code
James Smart3772a992009-05-22 14:50:54 -040010043 * 0 - driver suspended the device
10044 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -050010045 **/
10046static int
James Smart3772a992009-05-22 14:50:54 -040010047lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -050010048{
10049 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10050 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10051
10052 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10053 "0473 PCI device Power Management suspend.\n");
10054
10055 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040010056 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart3a55b532008-12-04 22:38:54 -050010057 lpfc_offline(phba);
10058 kthread_stop(phba->worker_thread);
10059
10060 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -040010061 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -050010062
10063 /* Save device state to PCI config space */
10064 pci_save_state(pdev);
10065 pci_set_power_state(pdev, PCI_D3hot);
10066
10067 return 0;
10068}
10069
10070/**
James Smart3772a992009-05-22 14:50:54 -040010071 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -050010072 * @pdev: pointer to PCI device
10073 *
James Smart3772a992009-05-22 14:50:54 -040010074 * This routine is to be called from the kernel's PCI subsystem to support
10075 * system Power Management (PM) to device with SLI-3 interface spec. When PM
10076 * invokes this method, it restores the device's PCI config space state and
10077 * fully reinitializes the device and brings it online. Note that as the
10078 * driver implements the minimum PM requirements to a power-aware driver's
10079 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
10080 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
10081 * driver will fully reinitialize its device during resume() method call,
10082 * the device will be set to PCI_D0 directly in PCI config space before
10083 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -050010084 *
10085 * Return code
James Smart3772a992009-05-22 14:50:54 -040010086 * 0 - driver suspended the device
10087 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -050010088 **/
10089static int
James Smart3772a992009-05-22 14:50:54 -040010090lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -050010091{
10092 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10093 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -050010094 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -050010095 int error;
10096
10097 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10098 "0452 PCI device Power Management resume.\n");
10099
10100 /* Restore device state from PCI config space */
10101 pci_set_power_state(pdev, PCI_D0);
10102 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -040010103
James Smart1dfb5a42010-02-12 14:40:50 -050010104 /*
10105 * As the new kernel behavior of pci_restore_state() API call clears
10106 * device saved_state flag, need to save the restored state again.
10107 */
10108 pci_save_state(pdev);
10109
James Smart3a55b532008-12-04 22:38:54 -050010110 if (pdev->is_busmaster)
10111 pci_set_master(pdev);
10112
10113 /* Startup the kernel thread for this host adapter. */
10114 phba->worker_thread = kthread_run(lpfc_do_work, phba,
10115 "lpfc_worker_%d", phba->brd_no);
10116 if (IS_ERR(phba->worker_thread)) {
10117 error = PTR_ERR(phba->worker_thread);
10118 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10119 "0434 PM resume failed to start worker "
10120 "thread: error=x%x.\n", error);
10121 return error;
10122 }
10123
James Smart5b75da22008-12-04 22:39:35 -050010124 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -040010125 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -050010126 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -050010127 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -050010128 "0430 PM resume Failed to enable interrupt\n");
10129 return -EIO;
10130 } else
10131 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -050010132
10133 /* Restart HBA and bring it online */
10134 lpfc_sli_brdrestart(phba);
10135 lpfc_online(phba);
10136
James Smart5b75da22008-12-04 22:39:35 -050010137 /* Log the current active interrupt mode */
10138 lpfc_log_intr_mode(phba, phba->intr_mode);
10139
James Smart3a55b532008-12-04 22:38:54 -050010140 return 0;
10141}
10142
10143/**
James Smart891478a2009-11-18 15:40:23 -050010144 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
10145 * @phba: pointer to lpfc hba data structure.
10146 *
10147 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -040010148 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -050010149 **/
10150static void
10151lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
10152{
10153 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10154 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -040010155
10156 /*
10157 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
10158 * and let the SCSI mid-layer to retry them to recover.
10159 */
James Smartdb55fba2014-04-04 13:52:02 -040010160 lpfc_sli_abort_fcp_rings(phba);
James Smart891478a2009-11-18 15:40:23 -050010161}
10162
10163/**
James Smart0d878412009-10-02 15:16:56 -040010164 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
10165 * @phba: pointer to lpfc hba data structure.
10166 *
10167 * This routine is called to prepare the SLI3 device for PCI slot reset. It
10168 * disables the device interrupt and pci device, and aborts the internal FCP
10169 * pending I/Os.
10170 **/
10171static void
10172lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
10173{
James Smart0d878412009-10-02 15:16:56 -040010174 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -050010175 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -040010176
James Smart75baf692010-06-08 18:31:21 -040010177 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040010178 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040010179
James Smarte2af0d22010-03-15 11:25:32 -040010180 /* Block all SCSI devices' I/Os on the host */
10181 lpfc_scsi_dev_block(phba);
10182
James Smartea714f32013-04-17 20:18:47 -040010183 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
10184 lpfc_sli_flush_fcp_rings(phba);
10185
James Smarte2af0d22010-03-15 11:25:32 -040010186 /* stop all timers */
10187 lpfc_stop_hba_timers(phba);
10188
James Smart0d878412009-10-02 15:16:56 -040010189 /* Disable interrupt and pci device */
10190 lpfc_sli_disable_intr(phba);
10191 pci_disable_device(phba->pcidev);
James Smart0d878412009-10-02 15:16:56 -040010192}
10193
10194/**
10195 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
10196 * @phba: pointer to lpfc hba data structure.
10197 *
10198 * This routine is called to prepare the SLI3 device for PCI slot permanently
10199 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
10200 * pending I/Os.
10201 **/
10202static void
James Smart75baf692010-06-08 18:31:21 -040010203lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
James Smart0d878412009-10-02 15:16:56 -040010204{
10205 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -050010206 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -040010207 /* Block all SCSI devices' I/Os on the host */
10208 lpfc_scsi_dev_block(phba);
10209
10210 /* stop all timers */
10211 lpfc_stop_hba_timers(phba);
10212
James Smart0d878412009-10-02 15:16:56 -040010213 /* Clean up all driver's outstanding SCSI I/Os */
10214 lpfc_sli_flush_fcp_rings(phba);
10215}
10216
10217/**
James Smart3772a992009-05-22 14:50:54 -040010218 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -040010219 * @pdev: pointer to PCI device.
10220 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -060010221 *
James Smart3772a992009-05-22 14:50:54 -040010222 * This routine is called from the PCI subsystem for I/O error handling to
10223 * device with SLI-3 interface spec. This function is called by the PCI
10224 * subsystem after a PCI bus error affecting this device has been detected.
10225 * When this function is invoked, it will need to stop all the I/Os and
10226 * interrupt(s) to the device. Once that is done, it will return
10227 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
10228 * as desired.
James Smarte59058c2008-08-24 21:49:00 -040010229 *
10230 * Return codes
James Smart0d878412009-10-02 15:16:56 -040010231 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -040010232 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10233 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -040010234 **/
James Smart3772a992009-05-22 14:50:54 -040010235static pci_ers_result_t
10236lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -060010237{
James Smart51ef4c22007-08-02 11:10:31 -040010238 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10239 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060010240
James Smart0d878412009-10-02 15:16:56 -040010241 switch (state) {
10242 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -050010243 /* Non-fatal error, prepare for recovery */
10244 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -040010245 return PCI_ERS_RESULT_CAN_RECOVER;
10246 case pci_channel_io_frozen:
10247 /* Fatal error, prepare for slot reset */
10248 lpfc_sli_prep_dev_for_reset(phba);
10249 return PCI_ERS_RESULT_NEED_RESET;
10250 case pci_channel_io_perm_failure:
10251 /* Permanent failure, prepare for device down */
James Smart75baf692010-06-08 18:31:21 -040010252 lpfc_sli_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -060010253 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -040010254 default:
10255 /* Unknown state, prepare and request slot reset */
10256 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10257 "0472 Unknown PCI error state: x%x\n", state);
10258 lpfc_sli_prep_dev_for_reset(phba);
10259 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -040010260 }
Linas Vepstas8d63f372007-02-14 14:28:36 -060010261}
10262
10263/**
James Smart3772a992009-05-22 14:50:54 -040010264 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -040010265 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -060010266 *
James Smart3772a992009-05-22 14:50:54 -040010267 * This routine is called from the PCI subsystem for error handling to
10268 * device with SLI-3 interface spec. This is called after PCI bus has been
10269 * reset to restart the PCI card from scratch, as if from a cold-boot.
10270 * During the PCI subsystem error recovery, after driver returns
10271 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
10272 * recovery and then call this routine before calling the .resume method
10273 * to recover the device. This function will initialize the HBA device,
10274 * enable the interrupt, but it will just put the HBA to offline state
10275 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -040010276 *
10277 * Return codes
James Smart3772a992009-05-22 14:50:54 -040010278 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10279 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -060010280 */
James Smart3772a992009-05-22 14:50:54 -040010281static pci_ers_result_t
10282lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -060010283{
James Smart51ef4c22007-08-02 11:10:31 -040010284 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10285 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060010286 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -050010287 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -060010288
10289 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +110010290 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -060010291 printk(KERN_ERR "lpfc: Cannot re-enable "
10292 "PCI device after reset.\n");
10293 return PCI_ERS_RESULT_DISCONNECT;
10294 }
10295
James Smart97207482008-12-04 22:39:19 -050010296 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050010297
10298 /*
10299 * As the new kernel behavior of pci_restore_state() API call clears
10300 * device saved_state flag, need to save the restored state again.
10301 */
10302 pci_save_state(pdev);
10303
James Smart97207482008-12-04 22:39:19 -050010304 if (pdev->is_busmaster)
10305 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -060010306
James Smart92d7f7b2007-06-17 19:56:38 -050010307 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010308 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -050010309 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -060010310
James Smart5b75da22008-12-04 22:39:35 -050010311 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -040010312 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -050010313 if (intr_mode == LPFC_INTR_ERROR) {
10314 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10315 "0427 Cannot re-enable interrupt after "
10316 "slot reset.\n");
10317 return PCI_ERS_RESULT_DISCONNECT;
10318 } else
10319 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -060010320
James Smart75baf692010-06-08 18:31:21 -040010321 /* Take device offline, it will perform cleanup */
James Smart618a5232012-06-12 13:54:36 -040010322 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
Linas Vepstas8d63f372007-02-14 14:28:36 -060010323 lpfc_offline(phba);
10324 lpfc_sli_brdrestart(phba);
10325
James Smart5b75da22008-12-04 22:39:35 -050010326 /* Log the current active interrupt mode */
10327 lpfc_log_intr_mode(phba, phba->intr_mode);
10328
Linas Vepstas8d63f372007-02-14 14:28:36 -060010329 return PCI_ERS_RESULT_RECOVERED;
10330}
10331
10332/**
James Smart3772a992009-05-22 14:50:54 -040010333 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -040010334 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -060010335 *
James Smart3772a992009-05-22 14:50:54 -040010336 * This routine is called from the PCI subsystem for error handling to device
10337 * with SLI-3 interface spec. It is called when kernel error recovery tells
10338 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
10339 * error recovery. After this call, traffic can start to flow from this device
10340 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -060010341 */
James Smart3772a992009-05-22 14:50:54 -040010342static void
10343lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -060010344{
James Smart51ef4c22007-08-02 11:10:31 -040010345 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10346 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060010347
James Smarte2af0d22010-03-15 11:25:32 -040010348 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -040010349 lpfc_online(phba);
James Smart0d878412009-10-02 15:16:56 -040010350
10351 /* Clean up Advanced Error Reporting (AER) if needed */
10352 if (phba->hba_flag & HBA_AER_ENABLED)
10353 pci_cleanup_aer_uncorrect_error_status(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -060010354}
10355
James Smart3772a992009-05-22 14:50:54 -040010356/**
James Smartda0436e2009-05-22 14:51:39 -040010357 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
10358 * @phba: pointer to lpfc hba data structure.
10359 *
10360 * returns the number of ELS/CT IOCBs to reserve
10361 **/
10362int
10363lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
10364{
10365 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
10366
James Smartf1126682009-06-10 17:22:44 -040010367 if (phba->sli_rev == LPFC_SLI_REV4) {
10368 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -040010369 return 10;
James Smartf1126682009-06-10 17:22:44 -040010370 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -040010371 return 25;
James Smartf1126682009-06-10 17:22:44 -040010372 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -040010373 return 50;
James Smartf1126682009-06-10 17:22:44 -040010374 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -040010375 return 100;
James Smart8a9d2e82012-05-09 21:16:12 -040010376 else if (max_xri <= 1536)
James Smart6a9c52c2009-10-02 15:16:51 -040010377 return 150;
James Smart8a9d2e82012-05-09 21:16:12 -040010378 else if (max_xri <= 2048)
10379 return 200;
10380 else
10381 return 250;
James Smartf1126682009-06-10 17:22:44 -040010382 } else
10383 return 0;
James Smartda0436e2009-05-22 14:51:39 -040010384}
10385
10386/**
James Smart895427b2017-02-12 13:52:30 -080010387 * lpfc_sli4_get_iocb_cnt - Calculate the # of total IOCBs to reserve
10388 * @phba: pointer to lpfc hba data structure.
10389 *
10390 * returns the number of ELS/CT
10391 **/
10392int
10393lpfc_sli4_get_iocb_cnt(struct lpfc_hba *phba)
10394{
10395 int max_xri = lpfc_sli4_get_els_iocb_cnt(phba);
10396
10397 return max_xri;
10398}
10399
10400
10401/**
James Smart52d52442011-05-24 11:42:45 -040010402 * lpfc_write_firmware - attempt to write a firmware image to the port
James Smart52d52442011-05-24 11:42:45 -040010403 * @fw: pointer to firmware image returned from request_firmware.
James Smartce396282012-09-29 11:30:56 -040010404 * @phba: pointer to lpfc hba data structure.
James Smart52d52442011-05-24 11:42:45 -040010405 *
James Smart52d52442011-05-24 11:42:45 -040010406 **/
James Smartce396282012-09-29 11:30:56 -040010407static void
10408lpfc_write_firmware(const struct firmware *fw, void *context)
James Smart52d52442011-05-24 11:42:45 -040010409{
James Smartce396282012-09-29 11:30:56 -040010410 struct lpfc_hba *phba = (struct lpfc_hba *)context;
James Smart6b5151f2012-01-18 16:24:06 -050010411 char fwrev[FW_REV_STR_SIZE];
James Smartce396282012-09-29 11:30:56 -040010412 struct lpfc_grp_hdr *image;
James Smart52d52442011-05-24 11:42:45 -040010413 struct list_head dma_buffer_list;
10414 int i, rc = 0;
10415 struct lpfc_dmabuf *dmabuf, *next;
10416 uint32_t offset = 0, temp_offset = 0;
James Smart6b6ef5d2016-10-13 15:06:17 -070010417 uint32_t magic_number, ftype, fid, fsize;
James Smart52d52442011-05-24 11:42:45 -040010418
James Smartc71ab862012-10-31 14:44:33 -040010419 /* It can be null in no-wait mode, sanity check */
James Smartce396282012-09-29 11:30:56 -040010420 if (!fw) {
10421 rc = -ENXIO;
10422 goto out;
10423 }
10424 image = (struct lpfc_grp_hdr *)fw->data;
10425
James Smart6b6ef5d2016-10-13 15:06:17 -070010426 magic_number = be32_to_cpu(image->magic_number);
10427 ftype = bf_get_be32(lpfc_grp_hdr_file_type, image);
10428 fid = bf_get_be32(lpfc_grp_hdr_id, image),
10429 fsize = be32_to_cpu(image->size);
10430
James Smart52d52442011-05-24 11:42:45 -040010431 INIT_LIST_HEAD(&dma_buffer_list);
James Smart6b6ef5d2016-10-13 15:06:17 -070010432 if ((magic_number != LPFC_GROUP_OJECT_MAGIC_G5 &&
10433 magic_number != LPFC_GROUP_OJECT_MAGIC_G6) ||
10434 ftype != LPFC_FILE_TYPE_GROUP || fsize != fw->size) {
James Smart52d52442011-05-24 11:42:45 -040010435 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10436 "3022 Invalid FW image found. "
Arnd Bergmannefe583c2016-10-17 14:35:46 +020010437 "Magic:%x Type:%x ID:%x Size %d %zd\n",
James Smart6b6ef5d2016-10-13 15:06:17 -070010438 magic_number, ftype, fid, fsize, fw->size);
James Smartce396282012-09-29 11:30:56 -040010439 rc = -EINVAL;
10440 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010441 }
10442 lpfc_decode_firmware_rev(phba, fwrev, 1);
James Smart88a2cfb2011-07-22 18:36:33 -040010443 if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
James Smart52d52442011-05-24 11:42:45 -040010444 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartce396282012-09-29 11:30:56 -040010445 "3023 Updating Firmware, Current Version:%s "
James Smart52d52442011-05-24 11:42:45 -040010446 "New Version:%s\n",
James Smart88a2cfb2011-07-22 18:36:33 -040010447 fwrev, image->revision);
James Smart52d52442011-05-24 11:42:45 -040010448 for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
10449 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
10450 GFP_KERNEL);
10451 if (!dmabuf) {
10452 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040010453 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010454 }
10455 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
10456 SLI4_PAGE_SIZE,
10457 &dmabuf->phys,
10458 GFP_KERNEL);
10459 if (!dmabuf->virt) {
10460 kfree(dmabuf);
10461 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040010462 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010463 }
10464 list_add_tail(&dmabuf->list, &dma_buffer_list);
10465 }
10466 while (offset < fw->size) {
10467 temp_offset = offset;
10468 list_for_each_entry(dmabuf, &dma_buffer_list, list) {
James Smart079b5c92011-08-21 21:48:49 -040010469 if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
James Smart52d52442011-05-24 11:42:45 -040010470 memcpy(dmabuf->virt,
10471 fw->data + temp_offset,
James Smart079b5c92011-08-21 21:48:49 -040010472 fw->size - temp_offset);
10473 temp_offset = fw->size;
James Smart52d52442011-05-24 11:42:45 -040010474 break;
10475 }
James Smart52d52442011-05-24 11:42:45 -040010476 memcpy(dmabuf->virt, fw->data + temp_offset,
10477 SLI4_PAGE_SIZE);
James Smart88a2cfb2011-07-22 18:36:33 -040010478 temp_offset += SLI4_PAGE_SIZE;
James Smart52d52442011-05-24 11:42:45 -040010479 }
10480 rc = lpfc_wr_object(phba, &dma_buffer_list,
10481 (fw->size - offset), &offset);
James Smartce396282012-09-29 11:30:56 -040010482 if (rc)
10483 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010484 }
10485 rc = offset;
10486 }
James Smartce396282012-09-29 11:30:56 -040010487
10488release_out:
James Smart52d52442011-05-24 11:42:45 -040010489 list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
10490 list_del(&dmabuf->list);
10491 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
10492 dmabuf->virt, dmabuf->phys);
10493 kfree(dmabuf);
10494 }
James Smartce396282012-09-29 11:30:56 -040010495 release_firmware(fw);
10496out:
10497 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartc71ab862012-10-31 14:44:33 -040010498 "3024 Firmware update done: %d.\n", rc);
James Smartce396282012-09-29 11:30:56 -040010499 return;
James Smart52d52442011-05-24 11:42:45 -040010500}
10501
10502/**
James Smartc71ab862012-10-31 14:44:33 -040010503 * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
10504 * @phba: pointer to lpfc hba data structure.
10505 *
10506 * This routine is called to perform Linux generic firmware upgrade on device
10507 * that supports such feature.
10508 **/
10509int
10510lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
10511{
10512 uint8_t file_name[ELX_MODEL_NAME_SIZE];
10513 int ret;
10514 const struct firmware *fw;
10515
10516 /* Only supported on SLI4 interface type 2 for now */
10517 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
10518 LPFC_SLI_INTF_IF_TYPE_2)
10519 return -EPERM;
10520
10521 snprintf(file_name, ELX_MODEL_NAME_SIZE, "%s.grp", phba->ModelName);
10522
10523 if (fw_upgrade == INT_FW_UPGRADE) {
10524 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
10525 file_name, &phba->pcidev->dev,
10526 GFP_KERNEL, (void *)phba,
10527 lpfc_write_firmware);
10528 } else if (fw_upgrade == RUN_FW_UPGRADE) {
10529 ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
10530 if (!ret)
10531 lpfc_write_firmware(fw, (void *)phba);
10532 } else {
10533 ret = -EINVAL;
10534 }
10535
10536 return ret;
10537}
10538
10539/**
James Smartda0436e2009-05-22 14:51:39 -040010540 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
10541 * @pdev: pointer to PCI device
10542 * @pid: pointer to PCI device identifier
10543 *
10544 * This routine is called from the kernel's PCI subsystem to device with
10545 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
10546 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
10547 * information of the device and driver to see if the driver state that it
10548 * can support this kind of device. If the match is successful, the driver
10549 * core invokes this routine. If this routine determines it can claim the HBA,
10550 * it does all the initialization that it needs to do to handle the HBA
10551 * properly.
10552 *
10553 * Return code
10554 * 0 - driver can claim the device
10555 * negative value - driver can not claim the device
10556 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010557static int
James Smartda0436e2009-05-22 14:51:39 -040010558lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
10559{
10560 struct lpfc_hba *phba;
10561 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -040010562 struct Scsi_Host *shost = NULL;
Sebastian Herbsztdb6f1c22015-08-31 16:48:14 -040010563 int error;
James Smartda0436e2009-05-22 14:51:39 -040010564 uint32_t cfg_mode, intr_mode;
James Smartda0436e2009-05-22 14:51:39 -040010565
10566 /* Allocate memory for HBA structure */
10567 phba = lpfc_hba_alloc(pdev);
10568 if (!phba)
10569 return -ENOMEM;
10570
10571 /* Perform generic PCI device enabling operation */
10572 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -040010573 if (error)
James Smartda0436e2009-05-22 14:51:39 -040010574 goto out_free_phba;
James Smartda0436e2009-05-22 14:51:39 -040010575
10576 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
10577 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
10578 if (error)
10579 goto out_disable_pci_dev;
10580
10581 /* Set up SLI-4 specific device PCI memory space */
10582 error = lpfc_sli4_pci_mem_setup(phba);
10583 if (error) {
10584 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10585 "1410 Failed to set up pci memory space.\n");
10586 goto out_disable_pci_dev;
10587 }
10588
James Smartda0436e2009-05-22 14:51:39 -040010589 /* Set up SLI-4 Specific device driver resources */
10590 error = lpfc_sli4_driver_resource_setup(phba);
10591 if (error) {
10592 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10593 "1412 Failed to set up driver resource.\n");
10594 goto out_unset_pci_mem_s4;
10595 }
10596
10597 /* Initialize and populate the iocb list per host */
James Smart2a9bf3d2010-06-07 15:24:45 -040010598
10599 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10600 "2821 initialize iocb list %d.\n",
10601 phba->cfg_iocb_cnt*1024);
10602 error = lpfc_init_iocb_list(phba, phba->cfg_iocb_cnt*1024);
10603
James Smartda0436e2009-05-22 14:51:39 -040010604 if (error) {
10605 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10606 "1413 Failed to initialize iocb list.\n");
10607 goto out_unset_driver_resource_s4;
10608 }
10609
James Smart19ca7602010-11-20 23:11:55 -050010610 INIT_LIST_HEAD(&phba->active_rrq_list);
James Smart7d791df2011-07-22 18:37:52 -040010611 INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
James Smart19ca7602010-11-20 23:11:55 -050010612
James Smartda0436e2009-05-22 14:51:39 -040010613 /* Set up common device driver resources */
10614 error = lpfc_setup_driver_resource_phase2(phba);
10615 if (error) {
10616 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10617 "1414 Failed to set up driver resource.\n");
10618 goto out_free_iocb_list;
10619 }
10620
James Smart079b5c92011-08-21 21:48:49 -040010621 /* Get the default values for Model Name and Description */
10622 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
10623
James Smartda0436e2009-05-22 14:51:39 -040010624 /* Create SCSI host to the physical port */
10625 error = lpfc_create_shost(phba);
10626 if (error) {
10627 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10628 "1415 Failed to create scsi host.\n");
10629 goto out_unset_driver_resource;
10630 }
10631
10632 /* Configure sysfs attributes */
10633 vport = phba->pport;
10634 error = lpfc_alloc_sysfs_attr(vport);
10635 if (error) {
10636 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10637 "1416 Failed to allocate sysfs attr\n");
10638 goto out_destroy_shost;
10639 }
10640
James Smart6669f9b2009-10-02 15:16:45 -040010641 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -040010642 /* Now, trying to enable interrupt and bring up the device */
10643 cfg_mode = phba->cfg_use_msi;
James Smartda0436e2009-05-22 14:51:39 -040010644
James Smart7b15db32013-01-03 15:43:29 -050010645 /* Put device to a known state before enabling interrupt */
10646 lpfc_stop_port(phba);
James Smart895427b2017-02-12 13:52:30 -080010647
James Smart7b15db32013-01-03 15:43:29 -050010648 /* Configure and enable interrupt */
10649 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
10650 if (intr_mode == LPFC_INTR_ERROR) {
10651 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10652 "0426 Failed to enable interrupt.\n");
10653 error = -ENODEV;
10654 goto out_free_sysfs_attr;
James Smartda0436e2009-05-22 14:51:39 -040010655 }
James Smart7b15db32013-01-03 15:43:29 -050010656 /* Default to single EQ for non-MSI-X */
James Smart895427b2017-02-12 13:52:30 -080010657 if (phba->intr_type != MSIX) {
10658 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP)
10659 phba->cfg_fcp_io_channel = 1;
10660 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
10661 phba->cfg_nvme_io_channel = 1;
10662 phba->io_channel_irqs = 1;
10663 }
10664
10665
James Smart7b15db32013-01-03 15:43:29 -050010666 /* Set up SLI-4 HBA */
10667 if (lpfc_sli4_hba_setup(phba)) {
10668 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10669 "1421 Failed to set up hba\n");
10670 error = -ENODEV;
10671 goto out_disable_intr;
10672 }
10673
10674 /* Log the current active interrupt mode */
10675 phba->intr_mode = intr_mode;
10676 lpfc_log_intr_mode(phba, intr_mode);
James Smartda0436e2009-05-22 14:51:39 -040010677
10678 /* Perform post initialization setup */
10679 lpfc_post_init_setup(phba);
10680
James Smart895427b2017-02-12 13:52:30 -080010681 /* todo: init: register port with nvme */
10682
James Smartc71ab862012-10-31 14:44:33 -040010683 /* check for firmware upgrade or downgrade */
10684 if (phba->cfg_request_firmware_upgrade)
Sebastian Herbsztdb6f1c22015-08-31 16:48:14 -040010685 lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
James Smart52d52442011-05-24 11:42:45 -040010686
James Smart1c6834a2009-07-19 10:01:26 -040010687 /* Check if there are static vports to be created. */
10688 lpfc_create_static_vport(phba);
James Smartda0436e2009-05-22 14:51:39 -040010689 return 0;
10690
10691out_disable_intr:
10692 lpfc_sli4_disable_intr(phba);
10693out_free_sysfs_attr:
10694 lpfc_free_sysfs_attr(vport);
10695out_destroy_shost:
10696 lpfc_destroy_shost(phba);
10697out_unset_driver_resource:
10698 lpfc_unset_driver_resource_phase2(phba);
10699out_free_iocb_list:
10700 lpfc_free_iocb_list(phba);
10701out_unset_driver_resource_s4:
10702 lpfc_sli4_driver_resource_unset(phba);
10703out_unset_pci_mem_s4:
10704 lpfc_sli4_pci_mem_unset(phba);
10705out_disable_pci_dev:
10706 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040010707 if (shost)
10708 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -040010709out_free_phba:
10710 lpfc_hba_free(phba);
10711 return error;
10712}
10713
10714/**
10715 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
10716 * @pdev: pointer to PCI device
10717 *
10718 * This routine is called from the kernel's PCI subsystem to device with
10719 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
10720 * removed from PCI bus, it performs all the necessary cleanup for the HBA
10721 * device to be removed from the PCI subsystem properly.
10722 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010723static void
James Smartda0436e2009-05-22 14:51:39 -040010724lpfc_pci_remove_one_s4(struct pci_dev *pdev)
10725{
10726 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10727 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
10728 struct lpfc_vport **vports;
10729 struct lpfc_hba *phba = vport->phba;
10730 int i;
10731
10732 /* Mark the device unloading flag */
10733 spin_lock_irq(&phba->hbalock);
10734 vport->load_flag |= FC_UNLOADING;
10735 spin_unlock_irq(&phba->hbalock);
10736
10737 /* Free the HBA sysfs attributes */
10738 lpfc_free_sysfs_attr(vport);
10739
10740 /* Release all the vports against this physical port */
10741 vports = lpfc_create_vport_work_array(phba);
10742 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040010743 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
10744 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
10745 continue;
James Smartda0436e2009-05-22 14:51:39 -040010746 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040010747 }
James Smartda0436e2009-05-22 14:51:39 -040010748 lpfc_destroy_vport_work_array(phba, vports);
10749
10750 /* Remove FC host and then SCSI host with the physical port */
10751 fc_remove_host(shost);
10752 scsi_remove_host(shost);
10753
James Smart895427b2017-02-12 13:52:30 -080010754 /* Perform ndlp cleanup on the physical port. The nvme localport
10755 * is destroyed after to ensure all rports are io-disabled.
10756 */
James Smartda0436e2009-05-22 14:51:39 -040010757 lpfc_cleanup(vport);
James Smart895427b2017-02-12 13:52:30 -080010758 /* todo: init: unregister port with nvme */
James Smartda0436e2009-05-22 14:51:39 -040010759
10760 /*
10761 * Bring down the SLI Layer. This step disables all interrupts,
10762 * clears the rings, discards all mailbox commands, and resets
10763 * the HBA FCoE function.
10764 */
10765 lpfc_debugfs_terminate(vport);
10766 lpfc_sli4_hba_unset(phba);
10767
10768 spin_lock_irq(&phba->hbalock);
10769 list_del_init(&vport->listentry);
10770 spin_unlock_irq(&phba->hbalock);
10771
James Smart3677a3a2010-09-29 11:19:14 -040010772 /* Perform scsi free before driver resource_unset since scsi
James Smartda0436e2009-05-22 14:51:39 -040010773 * buffers are released to their corresponding pools here.
10774 */
10775 lpfc_scsi_free(phba);
James Smart895427b2017-02-12 13:52:30 -080010776 lpfc_nvme_free(phba);
James Smart67d12732012-08-03 12:36:13 -040010777
James Smartda0436e2009-05-22 14:51:39 -040010778 lpfc_sli4_driver_resource_unset(phba);
10779
10780 /* Unmap adapter Control and Doorbell registers */
10781 lpfc_sli4_pci_mem_unset(phba);
10782
10783 /* Release PCI resources and disable device's PCI function */
10784 scsi_host_put(shost);
10785 lpfc_disable_pci_dev(phba);
10786
10787 /* Finally, free the driver's device data structure */
10788 lpfc_hba_free(phba);
10789
10790 return;
10791}
10792
10793/**
10794 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
10795 * @pdev: pointer to PCI device
10796 * @msg: power management message
10797 *
10798 * This routine is called from the kernel's PCI subsystem to support system
10799 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
10800 * this method, it quiesces the device by stopping the driver's worker
10801 * thread for the device, turning off device's interrupt and DMA, and bring
10802 * the device offline. Note that as the driver implements the minimum PM
10803 * requirements to a power-aware driver's PM support for suspend/resume -- all
10804 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
10805 * method call will be treated as SUSPEND and the driver will fully
10806 * reinitialize its device during resume() method call, the driver will set
10807 * device to PCI_D3hot state in PCI config space instead of setting it
10808 * according to the @msg provided by the PM.
10809 *
10810 * Return code
10811 * 0 - driver suspended the device
10812 * Error otherwise
10813 **/
10814static int
10815lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
10816{
10817 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10818 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10819
10820 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart75baf692010-06-08 18:31:21 -040010821 "2843 PCI device Power Management suspend.\n");
James Smartda0436e2009-05-22 14:51:39 -040010822
10823 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040010824 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smartda0436e2009-05-22 14:51:39 -040010825 lpfc_offline(phba);
10826 kthread_stop(phba->worker_thread);
10827
10828 /* Disable interrupt from device */
10829 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040010830 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -040010831
10832 /* Save device state to PCI config space */
10833 pci_save_state(pdev);
10834 pci_set_power_state(pdev, PCI_D3hot);
10835
10836 return 0;
10837}
10838
10839/**
10840 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
10841 * @pdev: pointer to PCI device
10842 *
10843 * This routine is called from the kernel's PCI subsystem to support system
10844 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
10845 * this method, it restores the device's PCI config space state and fully
10846 * reinitializes the device and brings it online. Note that as the driver
10847 * implements the minimum PM requirements to a power-aware driver's PM for
10848 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
10849 * to the suspend() method call will be treated as SUSPEND and the driver
10850 * will fully reinitialize its device during resume() method call, the device
10851 * will be set to PCI_D0 directly in PCI config space before restoring the
10852 * state.
10853 *
10854 * Return code
10855 * 0 - driver suspended the device
10856 * Error otherwise
10857 **/
10858static int
10859lpfc_pci_resume_one_s4(struct pci_dev *pdev)
10860{
10861 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10862 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10863 uint32_t intr_mode;
10864 int error;
10865
10866 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10867 "0292 PCI device Power Management resume.\n");
10868
10869 /* Restore device state from PCI config space */
10870 pci_set_power_state(pdev, PCI_D0);
10871 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050010872
10873 /*
10874 * As the new kernel behavior of pci_restore_state() API call clears
10875 * device saved_state flag, need to save the restored state again.
10876 */
10877 pci_save_state(pdev);
10878
James Smartda0436e2009-05-22 14:51:39 -040010879 if (pdev->is_busmaster)
10880 pci_set_master(pdev);
10881
10882 /* Startup the kernel thread for this host adapter. */
10883 phba->worker_thread = kthread_run(lpfc_do_work, phba,
10884 "lpfc_worker_%d", phba->brd_no);
10885 if (IS_ERR(phba->worker_thread)) {
10886 error = PTR_ERR(phba->worker_thread);
10887 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10888 "0293 PM resume failed to start worker "
10889 "thread: error=x%x.\n", error);
10890 return error;
10891 }
10892
10893 /* Configure and enable interrupt */
10894 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
10895 if (intr_mode == LPFC_INTR_ERROR) {
10896 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10897 "0294 PM resume Failed to enable interrupt\n");
10898 return -EIO;
10899 } else
10900 phba->intr_mode = intr_mode;
10901
10902 /* Restart HBA and bring it online */
10903 lpfc_sli_brdrestart(phba);
10904 lpfc_online(phba);
10905
10906 /* Log the current active interrupt mode */
10907 lpfc_log_intr_mode(phba, phba->intr_mode);
10908
10909 return 0;
10910}
10911
10912/**
James Smart75baf692010-06-08 18:31:21 -040010913 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
10914 * @phba: pointer to lpfc hba data structure.
10915 *
10916 * This routine is called to prepare the SLI4 device for PCI slot recover. It
10917 * aborts all the outstanding SCSI I/Os to the pci device.
10918 **/
10919static void
10920lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
10921{
James Smart75baf692010-06-08 18:31:21 -040010922 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10923 "2828 PCI channel I/O abort preparing for recovery\n");
10924 /*
10925 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
10926 * and let the SCSI mid-layer to retry them to recover.
10927 */
James Smartdb55fba2014-04-04 13:52:02 -040010928 lpfc_sli_abort_fcp_rings(phba);
James Smart75baf692010-06-08 18:31:21 -040010929}
10930
10931/**
10932 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
10933 * @phba: pointer to lpfc hba data structure.
10934 *
10935 * This routine is called to prepare the SLI4 device for PCI slot reset. It
10936 * disables the device interrupt and pci device, and aborts the internal FCP
10937 * pending I/Os.
10938 **/
10939static void
10940lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
10941{
10942 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10943 "2826 PCI channel disable preparing for reset\n");
10944
10945 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040010946 lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
James Smart75baf692010-06-08 18:31:21 -040010947
10948 /* Block all SCSI devices' I/Os on the host */
10949 lpfc_scsi_dev_block(phba);
10950
James Smartea714f32013-04-17 20:18:47 -040010951 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
10952 lpfc_sli_flush_fcp_rings(phba);
10953
James Smart75baf692010-06-08 18:31:21 -040010954 /* stop all timers */
10955 lpfc_stop_hba_timers(phba);
10956
10957 /* Disable interrupt and pci device */
10958 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040010959 lpfc_sli4_queue_destroy(phba);
James Smart75baf692010-06-08 18:31:21 -040010960 pci_disable_device(phba->pcidev);
James Smart75baf692010-06-08 18:31:21 -040010961}
10962
10963/**
10964 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
10965 * @phba: pointer to lpfc hba data structure.
10966 *
10967 * This routine is called to prepare the SLI4 device for PCI slot permanently
10968 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
10969 * pending I/Os.
10970 **/
10971static void
10972lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
10973{
10974 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10975 "2827 PCI channel permanent disable for failure\n");
10976
10977 /* Block all SCSI devices' I/Os on the host */
10978 lpfc_scsi_dev_block(phba);
10979
10980 /* stop all timers */
10981 lpfc_stop_hba_timers(phba);
10982
10983 /* Clean up all driver's outstanding SCSI I/Os */
10984 lpfc_sli_flush_fcp_rings(phba);
10985}
10986
10987/**
James Smartda0436e2009-05-22 14:51:39 -040010988 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
10989 * @pdev: pointer to PCI device.
10990 * @state: the current PCI connection state.
10991 *
10992 * This routine is called from the PCI subsystem for error handling to device
10993 * with SLI-4 interface spec. This function is called by the PCI subsystem
10994 * after a PCI bus error affecting this device has been detected. When this
10995 * function is invoked, it will need to stop all the I/Os and interrupt(s)
10996 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
10997 * for the PCI subsystem to perform proper recovery as desired.
10998 *
10999 * Return codes
11000 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
11001 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11002 **/
11003static pci_ers_result_t
11004lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
11005{
James Smart75baf692010-06-08 18:31:21 -040011006 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11007 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11008
11009 switch (state) {
11010 case pci_channel_io_normal:
11011 /* Non-fatal error, prepare for recovery */
11012 lpfc_sli4_prep_dev_for_recover(phba);
11013 return PCI_ERS_RESULT_CAN_RECOVER;
11014 case pci_channel_io_frozen:
11015 /* Fatal error, prepare for slot reset */
11016 lpfc_sli4_prep_dev_for_reset(phba);
11017 return PCI_ERS_RESULT_NEED_RESET;
11018 case pci_channel_io_perm_failure:
11019 /* Permanent failure, prepare for device down */
11020 lpfc_sli4_prep_dev_for_perm_failure(phba);
11021 return PCI_ERS_RESULT_DISCONNECT;
11022 default:
11023 /* Unknown state, prepare and request slot reset */
11024 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11025 "2825 Unknown PCI error state: x%x\n", state);
11026 lpfc_sli4_prep_dev_for_reset(phba);
11027 return PCI_ERS_RESULT_NEED_RESET;
11028 }
James Smartda0436e2009-05-22 14:51:39 -040011029}
11030
11031/**
11032 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
11033 * @pdev: pointer to PCI device.
11034 *
11035 * This routine is called from the PCI subsystem for error handling to device
11036 * with SLI-4 interface spec. It is called after PCI bus has been reset to
11037 * restart the PCI card from scratch, as if from a cold-boot. During the
11038 * PCI subsystem error recovery, after the driver returns
11039 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
11040 * recovery and then call this routine before calling the .resume method to
11041 * recover the device. This function will initialize the HBA device, enable
11042 * the interrupt, but it will just put the HBA to offline state without
11043 * passing any I/O traffic.
11044 *
11045 * Return codes
11046 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
11047 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11048 */
11049static pci_ers_result_t
11050lpfc_io_slot_reset_s4(struct pci_dev *pdev)
11051{
James Smart75baf692010-06-08 18:31:21 -040011052 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11053 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11054 struct lpfc_sli *psli = &phba->sli;
11055 uint32_t intr_mode;
11056
11057 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
11058 if (pci_enable_device_mem(pdev)) {
11059 printk(KERN_ERR "lpfc: Cannot re-enable "
11060 "PCI device after reset.\n");
11061 return PCI_ERS_RESULT_DISCONNECT;
11062 }
11063
11064 pci_restore_state(pdev);
James Smart0a96e972011-07-22 18:37:28 -040011065
11066 /*
11067 * As the new kernel behavior of pci_restore_state() API call clears
11068 * device saved_state flag, need to save the restored state again.
11069 */
11070 pci_save_state(pdev);
11071
James Smart75baf692010-06-08 18:31:21 -040011072 if (pdev->is_busmaster)
11073 pci_set_master(pdev);
11074
11075 spin_lock_irq(&phba->hbalock);
11076 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
11077 spin_unlock_irq(&phba->hbalock);
11078
11079 /* Configure and enable interrupt */
11080 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
11081 if (intr_mode == LPFC_INTR_ERROR) {
11082 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11083 "2824 Cannot re-enable interrupt after "
11084 "slot reset.\n");
11085 return PCI_ERS_RESULT_DISCONNECT;
11086 } else
11087 phba->intr_mode = intr_mode;
11088
11089 /* Log the current active interrupt mode */
11090 lpfc_log_intr_mode(phba, phba->intr_mode);
11091
James Smartda0436e2009-05-22 14:51:39 -040011092 return PCI_ERS_RESULT_RECOVERED;
11093}
11094
11095/**
11096 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
11097 * @pdev: pointer to PCI device
11098 *
11099 * This routine is called from the PCI subsystem for error handling to device
11100 * with SLI-4 interface spec. It is called when kernel error recovery tells
11101 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
11102 * error recovery. After this call, traffic can start to flow from this device
11103 * again.
11104 **/
11105static void
11106lpfc_io_resume_s4(struct pci_dev *pdev)
11107{
James Smart75baf692010-06-08 18:31:21 -040011108 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11109 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11110
11111 /*
11112 * In case of slot reset, as function reset is performed through
11113 * mailbox command which needs DMA to be enabled, this operation
11114 * has to be moved to the io resume phase. Taking device offline
11115 * will perform the necessary cleanup.
11116 */
11117 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
11118 /* Perform device reset */
James Smart618a5232012-06-12 13:54:36 -040011119 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040011120 lpfc_offline(phba);
11121 lpfc_sli_brdrestart(phba);
11122 /* Bring the device back online */
11123 lpfc_online(phba);
11124 }
11125
11126 /* Clean up Advanced Error Reporting (AER) if needed */
11127 if (phba->hba_flag & HBA_AER_ENABLED)
11128 pci_cleanup_aer_uncorrect_error_status(pdev);
James Smartda0436e2009-05-22 14:51:39 -040011129}
11130
11131/**
James Smart3772a992009-05-22 14:50:54 -040011132 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
11133 * @pdev: pointer to PCI device
11134 * @pid: pointer to PCI device identifier
11135 *
11136 * This routine is to be registered to the kernel's PCI subsystem. When an
11137 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
11138 * at PCI device-specific information of the device and driver to see if the
11139 * driver state that it can support this kind of device. If the match is
11140 * successful, the driver core invokes this routine. This routine dispatches
11141 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
11142 * do all the initialization that it needs to do to handle the HBA device
11143 * properly.
11144 *
11145 * Return code
11146 * 0 - driver can claim the device
11147 * negative value - driver can not claim the device
11148 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011149static int
James Smart3772a992009-05-22 14:50:54 -040011150lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
11151{
11152 int rc;
James Smart8fa38512009-07-19 10:01:03 -040011153 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -040011154
James Smart28baac72010-02-12 14:42:03 -050011155 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -040011156 return -ENODEV;
11157
James Smart8fa38512009-07-19 10:01:03 -040011158 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -050011159 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -040011160 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040011161 else
James Smart3772a992009-05-22 14:50:54 -040011162 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040011163
James Smart3772a992009-05-22 14:50:54 -040011164 return rc;
11165}
11166
11167/**
11168 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
11169 * @pdev: pointer to PCI device
11170 *
11171 * This routine is to be registered to the kernel's PCI subsystem. When an
11172 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
11173 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
11174 * remove routine, which will perform all the necessary cleanup for the
11175 * device to be removed from the PCI subsystem properly.
11176 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011177static void
James Smart3772a992009-05-22 14:50:54 -040011178lpfc_pci_remove_one(struct pci_dev *pdev)
11179{
11180 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11181 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11182
11183 switch (phba->pci_dev_grp) {
11184 case LPFC_PCI_DEV_LP:
11185 lpfc_pci_remove_one_s3(pdev);
11186 break;
James Smartda0436e2009-05-22 14:51:39 -040011187 case LPFC_PCI_DEV_OC:
11188 lpfc_pci_remove_one_s4(pdev);
11189 break;
James Smart3772a992009-05-22 14:50:54 -040011190 default:
11191 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11192 "1424 Invalid PCI device group: 0x%x\n",
11193 phba->pci_dev_grp);
11194 break;
11195 }
11196 return;
11197}
11198
11199/**
11200 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
11201 * @pdev: pointer to PCI device
11202 * @msg: power management message
11203 *
11204 * This routine is to be registered to the kernel's PCI subsystem to support
11205 * system Power Management (PM). When PM invokes this method, it dispatches
11206 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
11207 * suspend the device.
11208 *
11209 * Return code
11210 * 0 - driver suspended the device
11211 * Error otherwise
11212 **/
11213static int
11214lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
11215{
11216 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11217 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11218 int rc = -ENODEV;
11219
11220 switch (phba->pci_dev_grp) {
11221 case LPFC_PCI_DEV_LP:
11222 rc = lpfc_pci_suspend_one_s3(pdev, msg);
11223 break;
James Smartda0436e2009-05-22 14:51:39 -040011224 case LPFC_PCI_DEV_OC:
11225 rc = lpfc_pci_suspend_one_s4(pdev, msg);
11226 break;
James Smart3772a992009-05-22 14:50:54 -040011227 default:
11228 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11229 "1425 Invalid PCI device group: 0x%x\n",
11230 phba->pci_dev_grp);
11231 break;
11232 }
11233 return rc;
11234}
11235
11236/**
11237 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
11238 * @pdev: pointer to PCI device
11239 *
11240 * This routine is to be registered to the kernel's PCI subsystem to support
11241 * system Power Management (PM). When PM invokes this method, it dispatches
11242 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
11243 * resume the device.
11244 *
11245 * Return code
11246 * 0 - driver suspended the device
11247 * Error otherwise
11248 **/
11249static int
11250lpfc_pci_resume_one(struct pci_dev *pdev)
11251{
11252 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11253 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11254 int rc = -ENODEV;
11255
11256 switch (phba->pci_dev_grp) {
11257 case LPFC_PCI_DEV_LP:
11258 rc = lpfc_pci_resume_one_s3(pdev);
11259 break;
James Smartda0436e2009-05-22 14:51:39 -040011260 case LPFC_PCI_DEV_OC:
11261 rc = lpfc_pci_resume_one_s4(pdev);
11262 break;
James Smart3772a992009-05-22 14:50:54 -040011263 default:
11264 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11265 "1426 Invalid PCI device group: 0x%x\n",
11266 phba->pci_dev_grp);
11267 break;
11268 }
11269 return rc;
11270}
11271
11272/**
11273 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
11274 * @pdev: pointer to PCI device.
11275 * @state: the current PCI connection state.
11276 *
11277 * This routine is registered to the PCI subsystem for error handling. This
11278 * function is called by the PCI subsystem after a PCI bus error affecting
11279 * this device has been detected. When this routine is invoked, it dispatches
11280 * the action to the proper SLI-3 or SLI-4 device error detected handling
11281 * routine, which will perform the proper error detected operation.
11282 *
11283 * Return codes
11284 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
11285 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11286 **/
11287static pci_ers_result_t
11288lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
11289{
11290 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11291 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11292 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
11293
11294 switch (phba->pci_dev_grp) {
11295 case LPFC_PCI_DEV_LP:
11296 rc = lpfc_io_error_detected_s3(pdev, state);
11297 break;
James Smartda0436e2009-05-22 14:51:39 -040011298 case LPFC_PCI_DEV_OC:
11299 rc = lpfc_io_error_detected_s4(pdev, state);
11300 break;
James Smart3772a992009-05-22 14:50:54 -040011301 default:
11302 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11303 "1427 Invalid PCI device group: 0x%x\n",
11304 phba->pci_dev_grp);
11305 break;
11306 }
11307 return rc;
11308}
11309
11310/**
11311 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
11312 * @pdev: pointer to PCI device.
11313 *
11314 * This routine is registered to the PCI subsystem for error handling. This
11315 * function is called after PCI bus has been reset to restart the PCI card
11316 * from scratch, as if from a cold-boot. When this routine is invoked, it
11317 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
11318 * routine, which will perform the proper device reset.
11319 *
11320 * Return codes
11321 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
11322 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11323 **/
11324static pci_ers_result_t
11325lpfc_io_slot_reset(struct pci_dev *pdev)
11326{
11327 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11328 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11329 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
11330
11331 switch (phba->pci_dev_grp) {
11332 case LPFC_PCI_DEV_LP:
11333 rc = lpfc_io_slot_reset_s3(pdev);
11334 break;
James Smartda0436e2009-05-22 14:51:39 -040011335 case LPFC_PCI_DEV_OC:
11336 rc = lpfc_io_slot_reset_s4(pdev);
11337 break;
James Smart3772a992009-05-22 14:50:54 -040011338 default:
11339 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11340 "1428 Invalid PCI device group: 0x%x\n",
11341 phba->pci_dev_grp);
11342 break;
11343 }
11344 return rc;
11345}
11346
11347/**
11348 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
11349 * @pdev: pointer to PCI device
11350 *
11351 * This routine is registered to the PCI subsystem for error handling. It
11352 * is called when kernel error recovery tells the lpfc driver that it is
11353 * OK to resume normal PCI operation after PCI bus error recovery. When
11354 * this routine is invoked, it dispatches the action to the proper SLI-3
11355 * or SLI-4 device io_resume routine, which will resume the device operation.
11356 **/
11357static void
11358lpfc_io_resume(struct pci_dev *pdev)
11359{
11360 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11361 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11362
11363 switch (phba->pci_dev_grp) {
11364 case LPFC_PCI_DEV_LP:
11365 lpfc_io_resume_s3(pdev);
11366 break;
James Smartda0436e2009-05-22 14:51:39 -040011367 case LPFC_PCI_DEV_OC:
11368 lpfc_io_resume_s4(pdev);
11369 break;
James Smart3772a992009-05-22 14:50:54 -040011370 default:
11371 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11372 "1429 Invalid PCI device group: 0x%x\n",
11373 phba->pci_dev_grp);
11374 break;
11375 }
11376 return;
11377}
11378
James Smart1ba981f2014-02-20 09:56:45 -050011379/**
11380 * lpfc_sli4_oas_verify - Verify OAS is supported by this adapter
11381 * @phba: pointer to lpfc hba data structure.
11382 *
11383 * This routine checks to see if OAS is supported for this adapter. If
11384 * supported, the configure Flash Optimized Fabric flag is set. Otherwise,
11385 * the enable oas flag is cleared and the pool created for OAS device data
11386 * is destroyed.
11387 *
11388 **/
11389void
11390lpfc_sli4_oas_verify(struct lpfc_hba *phba)
11391{
11392
11393 if (!phba->cfg_EnableXLane)
11394 return;
11395
11396 if (phba->sli4_hba.pc_sli4_params.oas_supported) {
11397 phba->cfg_fof = 1;
11398 } else {
James Smartf38fa0b2014-04-04 13:52:21 -040011399 phba->cfg_fof = 0;
James Smart1ba981f2014-02-20 09:56:45 -050011400 if (phba->device_data_mem_pool)
11401 mempool_destroy(phba->device_data_mem_pool);
11402 phba->device_data_mem_pool = NULL;
11403 }
11404
11405 return;
11406}
11407
11408/**
11409 * lpfc_fof_queue_setup - Set up all the fof queues
11410 * @phba: pointer to lpfc hba data structure.
11411 *
11412 * This routine is invoked to set up all the fof queues for the FC HBA
11413 * operation.
11414 *
11415 * Return codes
11416 * 0 - successful
11417 * -ENOMEM - No available memory
11418 **/
11419int
11420lpfc_fof_queue_setup(struct lpfc_hba *phba)
11421{
James Smart895427b2017-02-12 13:52:30 -080011422 struct lpfc_sli_ring *pring;
James Smart1ba981f2014-02-20 09:56:45 -050011423 int rc;
11424
11425 rc = lpfc_eq_create(phba, phba->sli4_hba.fof_eq, LPFC_MAX_IMAX);
11426 if (rc)
11427 return -ENOMEM;
11428
James Smartf38fa0b2014-04-04 13:52:21 -040011429 if (phba->cfg_fof) {
James Smart1ba981f2014-02-20 09:56:45 -050011430
11431 rc = lpfc_cq_create(phba, phba->sli4_hba.oas_cq,
11432 phba->sli4_hba.fof_eq, LPFC_WCQ, LPFC_FCP);
11433 if (rc)
11434 goto out_oas_cq;
11435
11436 rc = lpfc_wq_create(phba, phba->sli4_hba.oas_wq,
11437 phba->sli4_hba.oas_cq, LPFC_FCP);
11438 if (rc)
11439 goto out_oas_wq;
11440
James Smart895427b2017-02-12 13:52:30 -080011441 /* Bind this CQ/WQ to the NVME ring */
11442 pring = phba->sli4_hba.oas_wq->pring;
11443 pring->sli.sli4.wqp =
11444 (void *)phba->sli4_hba.oas_wq;
11445 phba->sli4_hba.oas_cq->pring = pring;
James Smart1ba981f2014-02-20 09:56:45 -050011446 }
11447
11448 return 0;
11449
11450out_oas_wq:
James Smartf38fa0b2014-04-04 13:52:21 -040011451 lpfc_cq_destroy(phba, phba->sli4_hba.oas_cq);
James Smart1ba981f2014-02-20 09:56:45 -050011452out_oas_cq:
11453 lpfc_eq_destroy(phba, phba->sli4_hba.fof_eq);
11454 return rc;
11455
11456}
11457
11458/**
11459 * lpfc_fof_queue_create - Create all the fof queues
11460 * @phba: pointer to lpfc hba data structure.
11461 *
11462 * This routine is invoked to allocate all the fof queues for the FC HBA
11463 * operation. For each SLI4 queue type, the parameters such as queue entry
11464 * count (queue depth) shall be taken from the module parameter. For now,
11465 * we just use some constant number as place holder.
11466 *
11467 * Return codes
11468 * 0 - successful
11469 * -ENOMEM - No availble memory
11470 * -EIO - The mailbox failed to complete successfully.
11471 **/
11472int
11473lpfc_fof_queue_create(struct lpfc_hba *phba)
11474{
11475 struct lpfc_queue *qdesc;
11476
11477 /* Create FOF EQ */
11478 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
11479 phba->sli4_hba.eq_ecount);
11480 if (!qdesc)
11481 goto out_error;
11482
11483 phba->sli4_hba.fof_eq = qdesc;
11484
James Smartf38fa0b2014-04-04 13:52:21 -040011485 if (phba->cfg_fof) {
James Smart1ba981f2014-02-20 09:56:45 -050011486
11487 /* Create OAS CQ */
11488 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
11489 phba->sli4_hba.cq_ecount);
11490 if (!qdesc)
11491 goto out_error;
11492
11493 phba->sli4_hba.oas_cq = qdesc;
11494
11495 /* Create OAS WQ */
11496 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
11497 phba->sli4_hba.wq_ecount);
11498 if (!qdesc)
11499 goto out_error;
11500
11501 phba->sli4_hba.oas_wq = qdesc;
James Smart895427b2017-02-12 13:52:30 -080011502 list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
James Smart1ba981f2014-02-20 09:56:45 -050011503
11504 }
11505 return 0;
11506
11507out_error:
11508 lpfc_fof_queue_destroy(phba);
11509 return -ENOMEM;
11510}
11511
11512/**
11513 * lpfc_fof_queue_destroy - Destroy all the fof queues
11514 * @phba: pointer to lpfc hba data structure.
11515 *
11516 * This routine is invoked to release all the SLI4 queues with the FC HBA
11517 * operation.
11518 *
11519 * Return codes
11520 * 0 - successful
11521 **/
11522int
11523lpfc_fof_queue_destroy(struct lpfc_hba *phba)
11524{
11525 /* Release FOF Event queue */
11526 if (phba->sli4_hba.fof_eq != NULL) {
11527 lpfc_sli4_queue_free(phba->sli4_hba.fof_eq);
11528 phba->sli4_hba.fof_eq = NULL;
11529 }
11530
11531 /* Release OAS Completion queue */
11532 if (phba->sli4_hba.oas_cq != NULL) {
11533 lpfc_sli4_queue_free(phba->sli4_hba.oas_cq);
11534 phba->sli4_hba.oas_cq = NULL;
11535 }
11536
11537 /* Release OAS Work queue */
11538 if (phba->sli4_hba.oas_wq != NULL) {
11539 lpfc_sli4_queue_free(phba->sli4_hba.oas_wq);
11540 phba->sli4_hba.oas_wq = NULL;
11541 }
11542 return 0;
11543}
11544
dea31012005-04-17 16:05:31 -050011545MODULE_DEVICE_TABLE(pci, lpfc_id_table);
11546
Stephen Hemmingera55b2d22012-09-07 09:33:16 -070011547static const struct pci_error_handlers lpfc_err_handler = {
Linas Vepstas8d63f372007-02-14 14:28:36 -060011548 .error_detected = lpfc_io_error_detected,
11549 .slot_reset = lpfc_io_slot_reset,
11550 .resume = lpfc_io_resume,
11551};
11552
dea31012005-04-17 16:05:31 -050011553static struct pci_driver lpfc_driver = {
11554 .name = LPFC_DRIVER_NAME,
11555 .id_table = lpfc_id_table,
11556 .probe = lpfc_pci_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011557 .remove = lpfc_pci_remove_one,
James Smart3a55b532008-12-04 22:38:54 -050011558 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -040011559 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -050011560 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -050011561};
11562
James Smart3ef6d242012-01-18 16:23:48 -050011563static const struct file_operations lpfc_mgmt_fop = {
Al Viro858feac2013-04-14 22:39:37 -040011564 .owner = THIS_MODULE,
James Smart3ef6d242012-01-18 16:23:48 -050011565};
11566
11567static struct miscdevice lpfc_mgmt_dev = {
11568 .minor = MISC_DYNAMIC_MINOR,
11569 .name = "lpfcmgmt",
11570 .fops = &lpfc_mgmt_fop,
11571};
11572
James Smarte59058c2008-08-24 21:49:00 -040011573/**
James Smart3621a712009-04-06 18:47:14 -040011574 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -040011575 *
11576 * This routine is to be invoked when the lpfc module is loaded into the
11577 * kernel. The special kernel macro module_init() is used to indicate the
11578 * role of this routine to the kernel as lpfc module entry point.
11579 *
11580 * Return codes
11581 * 0 - successful
11582 * -ENOMEM - FC attach transport failed
11583 * all others - failed
11584 */
dea31012005-04-17 16:05:31 -050011585static int __init
11586lpfc_init(void)
11587{
11588 int error = 0;
11589
11590 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040011591 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -050011592
James Smart3ef6d242012-01-18 16:23:48 -050011593 error = misc_register(&lpfc_mgmt_dev);
11594 if (error)
11595 printk(KERN_ERR "Could not register lpfcmgmt device, "
11596 "misc_register returned with status %d", error);
11597
James Smart458c0832016-07-06 12:36:07 -070011598 lpfc_transport_functions.vport_create = lpfc_vport_create;
11599 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
dea31012005-04-17 16:05:31 -050011600 lpfc_transport_template =
11601 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -040011602 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -050011603 return -ENOMEM;
James Smart458c0832016-07-06 12:36:07 -070011604 lpfc_vport_transport_template =
11605 fc_attach_transport(&lpfc_vport_transport_functions);
11606 if (lpfc_vport_transport_template == NULL) {
11607 fc_release_transport(lpfc_transport_template);
11608 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -040011609 }
James Smart7bb03bb2013-04-17 20:19:16 -040011610
11611 /* Initialize in case vector mapping is needed */
James Smartb246de12013-05-31 17:03:07 -040011612 lpfc_used_cpu = NULL;
James Smart2ea259e2017-02-12 13:52:27 -080011613 lpfc_present_cpu = num_present_cpus();
James Smart7bb03bb2013-04-17 20:19:16 -040011614
dea31012005-04-17 16:05:31 -050011615 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -050011616 if (error) {
dea31012005-04-17 16:05:31 -050011617 fc_release_transport(lpfc_transport_template);
James Smart458c0832016-07-06 12:36:07 -070011618 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -050011619 }
dea31012005-04-17 16:05:31 -050011620
11621 return error;
11622}
11623
James Smarte59058c2008-08-24 21:49:00 -040011624/**
James Smart3621a712009-04-06 18:47:14 -040011625 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -040011626 *
11627 * This routine is invoked when the lpfc module is removed from the kernel.
11628 * The special kernel macro module_exit() is used to indicate the role of
11629 * this routine to the kernel as lpfc module exit point.
11630 */
dea31012005-04-17 16:05:31 -050011631static void __exit
11632lpfc_exit(void)
11633{
James Smart3ef6d242012-01-18 16:23:48 -050011634 misc_deregister(&lpfc_mgmt_dev);
dea31012005-04-17 16:05:31 -050011635 pci_unregister_driver(&lpfc_driver);
11636 fc_release_transport(lpfc_transport_template);
James Smart458c0832016-07-06 12:36:07 -070011637 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -050011638 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040011639 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
11640 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050011641 (1L << _dump_buf_data_order), _dump_buf_data);
11642 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
11643 }
11644
11645 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -040011646 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
11647 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050011648 (1L << _dump_buf_dif_order), _dump_buf_dif);
11649 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
11650 }
James Smartb246de12013-05-31 17:03:07 -040011651 kfree(lpfc_used_cpu);
Johannes Thumshirn79739672015-08-31 16:48:11 -040011652 idr_destroy(&lpfc_hba_index);
dea31012005-04-17 16:05:31 -050011653}
11654
11655module_init(lpfc_init);
11656module_exit(lpfc_exit);
11657MODULE_LICENSE("GPL");
11658MODULE_DESCRIPTION(LPFC_MODULE_DESC);
11659MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
11660MODULE_VERSION("0:" LPFC_DRIVER_VERSION);