blob: f4b3548f101a686e44faf35b24f9778184cc9ded [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>
dea31012005-04-17 16:05:31 -050037
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040038#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050039#include <scsi/scsi_device.h>
40#include <scsi/scsi_host.h>
41#include <scsi/scsi_transport_fc.h>
42
James Smartda0436e2009-05-22 14:51:39 -040043#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050044#include "lpfc_hw.h"
45#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040046#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040047#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050048#include "lpfc_disc.h"
49#include "lpfc_scsi.h"
50#include "lpfc.h"
51#include "lpfc_logmsg.h"
52#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050053#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050054#include "lpfc_version.h"
James Smart12f44452016-07-06 12:36:03 -070055#include "lpfc_ids.h"
dea31012005-04-17 16:05:31 -050056
James Smart81301a92008-12-04 22:39:46 -050057char *_dump_buf_data;
58unsigned long _dump_buf_data_order;
59char *_dump_buf_dif;
60unsigned long _dump_buf_dif_order;
61spinlock_t _dump_buf_lock;
62
James Smart7bb03bb2013-04-17 20:19:16 -040063/* Used when mapping IRQ vectors in a driver centric manner */
James Smartb246de12013-05-31 17:03:07 -040064uint16_t *lpfc_used_cpu;
65uint32_t lpfc_present_cpu;
James Smart7bb03bb2013-04-17 20:19:16 -040066
dea31012005-04-17 16:05:31 -050067static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
68static int lpfc_post_rcv_buf(struct lpfc_hba *);
James Smart5350d872011-10-10 21:33:49 -040069static int lpfc_sli4_queue_verify(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040070static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
71static int lpfc_setup_endian_order(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040072static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
James Smart8a9d2e82012-05-09 21:16:12 -040073static void lpfc_free_els_sgl_list(struct lpfc_hba *);
74static void lpfc_init_sgl_list(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040075static int lpfc_init_active_sgl_array(struct lpfc_hba *);
76static void lpfc_free_active_sgl(struct lpfc_hba *);
77static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
78static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
79static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
80static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
81static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
James Smart618a5232012-06-12 13:54:36 -040082static void lpfc_sli4_disable_intr(struct lpfc_hba *);
83static uint32_t lpfc_sli4_enable_intr(struct lpfc_hba *, uint32_t);
James Smart1ba981f2014-02-20 09:56:45 -050084static void lpfc_sli4_oas_verify(struct lpfc_hba *phba);
dea31012005-04-17 16:05:31 -050085
86static struct scsi_transport_template *lpfc_transport_template = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050087static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea31012005-04-17 16:05:31 -050088static DEFINE_IDR(lpfc_hba_index);
89
James Smarte59058c2008-08-24 21:49:00 -040090/**
James Smart3621a712009-04-06 18:47:14 -040091 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
James Smarte59058c2008-08-24 21:49:00 -040092 * @phba: pointer to lpfc hba data structure.
93 *
94 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
95 * mailbox command. It retrieves the revision information from the HBA and
96 * collects the Vital Product Data (VPD) about the HBA for preparing the
97 * configuration of the HBA.
98 *
99 * Return codes:
100 * 0 - success.
101 * -ERESTART - requests the SLI layer to reset the HBA and try again.
102 * Any other value - indicates an error.
103 **/
dea31012005-04-17 16:05:31 -0500104int
James Smart2e0fef82007-06-17 19:56:36 -0500105lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500106{
107 lpfc_vpd_t *vp = &phba->vpd;
108 int i = 0, rc;
109 LPFC_MBOXQ_t *pmb;
110 MAILBOX_t *mb;
111 char *lpfc_vpd_data = NULL;
112 uint16_t offset = 0;
113 static char licensed[56] =
114 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -0400115 static int init_key = 1;
dea31012005-04-17 16:05:31 -0500116
117 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
118 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500119 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500120 return -ENOMEM;
121 }
122
James Smart04c68492009-05-22 14:52:52 -0400123 mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -0500124 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500125
126 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -0400127 if (init_key) {
128 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -0500129
James Smart65a29c12006-07-06 15:50:50 -0400130 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
131 *ptext = cpu_to_be32(*ptext);
132 init_key = 0;
133 }
dea31012005-04-17 16:05:31 -0500134
135 lpfc_read_nv(phba, pmb);
136 memset((char*)mb->un.varRDnvp.rsvd3, 0,
137 sizeof (mb->un.varRDnvp.rsvd3));
138 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
139 sizeof (licensed));
140
141 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
142
143 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500144 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smarte8b62012007-08-02 11:10:09 -0400145 "0324 Config Port initialization "
dea31012005-04-17 16:05:31 -0500146 "error, mbxCmd x%x READ_NVPARM, "
147 "mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500148 mb->mbxCommand, mb->mbxStatus);
149 mempool_free(pmb, phba->mbox_mem_pool);
150 return -ERESTART;
151 }
152 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
James Smart2e0fef82007-06-17 19:56:36 -0500153 sizeof(phba->wwnn));
154 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
155 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500156 }
157
James Smart92d7f7b2007-06-17 19:56:38 -0500158 phba->sli3_options = 0x0;
159
dea31012005-04-17 16:05:31 -0500160 /* Setup and issue mailbox READ REV command */
161 lpfc_read_rev(phba, pmb);
162 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
163 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500164 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400165 "0439 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500166 "READ_REV, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500167 mb->mbxCommand, mb->mbxStatus);
168 mempool_free( pmb, phba->mbox_mem_pool);
169 return -ERESTART;
170 }
171
James Smart92d7f7b2007-06-17 19:56:38 -0500172
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500173 /*
174 * The value of rr must be 1 since the driver set the cv field to 1.
175 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500176 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500177 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500178 vp->rev.rBit = 0;
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500179 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400180 "0440 Adapter failed to init, READ_REV has "
181 "missing revision information.\n");
dea31012005-04-17 16:05:31 -0500182 mempool_free(pmb, phba->mbox_mem_pool);
183 return -ERESTART;
dea31012005-04-17 16:05:31 -0500184 }
185
James Smart495a7142008-06-14 22:52:59 -0400186 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
187 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -0500188 return -EINVAL;
James Smart495a7142008-06-14 22:52:59 -0400189 }
James Smarted957682007-06-17 19:56:37 -0500190
dea31012005-04-17 16:05:31 -0500191 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500192 vp->rev.rBit = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500193 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500194 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
195 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
196 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
197 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500198 vp->rev.biuRev = mb->un.varRdRev.biuRev;
199 vp->rev.smRev = mb->un.varRdRev.smRev;
200 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
201 vp->rev.endecRev = mb->un.varRdRev.endecRev;
202 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
203 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
204 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
205 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
206 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
207 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
208
James Smart92d7f7b2007-06-17 19:56:38 -0500209 /* If the sli feature level is less then 9, we must
210 * tear down all RPIs and VPIs on link down if NPIV
211 * is enabled.
212 */
213 if (vp->rev.feaLevelHigh < 9)
214 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
215
dea31012005-04-17 16:05:31 -0500216 if (lpfc_is_LC_HBA(phba->pcidev->device))
217 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
218 sizeof (phba->RandomData));
219
dea31012005-04-17 16:05:31 -0500220 /* Get adapter VPD information */
dea31012005-04-17 16:05:31 -0500221 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
222 if (!lpfc_vpd_data)
James Smartd7c255b2008-08-24 21:50:00 -0400223 goto out_free_mbox;
dea31012005-04-17 16:05:31 -0500224 do {
James Smarta0c87cb2009-07-19 10:01:10 -0400225 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
dea31012005-04-17 16:05:31 -0500226 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
227
228 if (rc != MBX_SUCCESS) {
229 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400230 "0441 VPD not present on adapter, "
dea31012005-04-17 16:05:31 -0500231 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500232 mb->mbxCommand, mb->mbxStatus);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500233 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500234 }
James Smart04c68492009-05-22 14:52:52 -0400235 /* dump mem may return a zero when finished or we got a
236 * mailbox error, either way we are done.
237 */
238 if (mb->un.varDmp.word_cnt == 0)
239 break;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500240 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
241 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
James Smartd7c255b2008-08-24 21:50:00 -0400242 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
243 lpfc_vpd_data + offset,
James Smart92d7f7b2007-06-17 19:56:38 -0500244 mb->un.varDmp.word_cnt);
dea31012005-04-17 16:05:31 -0500245 offset += mb->un.varDmp.word_cnt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500246 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
247 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500248
249 kfree(lpfc_vpd_data);
dea31012005-04-17 16:05:31 -0500250out_free_mbox:
251 mempool_free(pmb, phba->mbox_mem_pool);
252 return 0;
253}
254
James Smarte59058c2008-08-24 21:49:00 -0400255/**
James Smart3621a712009-04-06 18:47:14 -0400256 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
James Smarte59058c2008-08-24 21:49:00 -0400257 * @phba: pointer to lpfc hba data structure.
258 * @pmboxq: pointer to the driver internal queue element for mailbox command.
259 *
260 * This is the completion handler for driver's configuring asynchronous event
261 * mailbox command to the device. If the mailbox command returns successfully,
262 * it will set internal async event support flag to 1; otherwise, it will
263 * set internal async event support flag to 0.
264 **/
James Smart57127f12007-10-27 13:37:05 -0400265static void
266lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
267{
James Smart04c68492009-05-22 14:52:52 -0400268 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
James Smart57127f12007-10-27 13:37:05 -0400269 phba->temp_sensor_support = 1;
270 else
271 phba->temp_sensor_support = 0;
272 mempool_free(pmboxq, phba->mbox_mem_pool);
273 return;
274}
275
James Smarte59058c2008-08-24 21:49:00 -0400276/**
James Smart3621a712009-04-06 18:47:14 -0400277 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
James Smart97207482008-12-04 22:39:19 -0500278 * @phba: pointer to lpfc hba data structure.
279 * @pmboxq: pointer to the driver internal queue element for mailbox command.
280 *
281 * This is the completion handler for dump mailbox command for getting
282 * wake up parameters. When this command complete, the response contain
283 * Option rom version of the HBA. This function translate the version number
284 * into a human readable string and store it in OptionROMVersion.
285 **/
286static void
287lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
288{
289 struct prog_id *prg;
290 uint32_t prog_id_word;
291 char dist = ' ';
292 /* character array used for decoding dist type. */
293 char dist_char[] = "nabx";
294
James Smart04c68492009-05-22 14:52:52 -0400295 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
James Smart9f1e1b52008-12-04 22:39:40 -0500296 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500297 return;
James Smart9f1e1b52008-12-04 22:39:40 -0500298 }
James Smart97207482008-12-04 22:39:19 -0500299
300 prg = (struct prog_id *) &prog_id_word;
301
302 /* word 7 contain option rom version */
James Smart04c68492009-05-22 14:52:52 -0400303 prog_id_word = pmboxq->u.mb.un.varWords[7];
James Smart97207482008-12-04 22:39:19 -0500304
305 /* Decode the Option rom version word to a readable string */
306 if (prg->dist < 4)
307 dist = dist_char[prg->dist];
308
309 if ((prg->dist == 3) && (prg->num == 0))
James Smarta2fc4aef2014-09-03 12:57:55 -0400310 snprintf(phba->OptionROMVersion, 32, "%d.%d%d",
James Smart97207482008-12-04 22:39:19 -0500311 prg->ver, prg->rev, prg->lev);
312 else
James Smarta2fc4aef2014-09-03 12:57:55 -0400313 snprintf(phba->OptionROMVersion, 32, "%d.%d%d%c%d",
James Smart97207482008-12-04 22:39:19 -0500314 prg->ver, prg->rev, prg->lev,
315 dist, prg->num);
James Smart9f1e1b52008-12-04 22:39:40 -0500316 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500317 return;
318}
319
320/**
James Smart05580562011-05-24 11:40:48 -0400321 * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
322 * cfg_soft_wwnn, cfg_soft_wwpn
323 * @vport: pointer to lpfc vport data structure.
324 *
325 *
326 * Return codes
327 * None.
328 **/
329void
330lpfc_update_vport_wwn(struct lpfc_vport *vport)
331{
332 /* If the soft name exists then update it using the service params */
333 if (vport->phba->cfg_soft_wwnn)
334 u64_to_wwn(vport->phba->cfg_soft_wwnn,
335 vport->fc_sparam.nodeName.u.wwn);
336 if (vport->phba->cfg_soft_wwpn)
337 u64_to_wwn(vport->phba->cfg_soft_wwpn,
338 vport->fc_sparam.portName.u.wwn);
339
340 /*
341 * If the name is empty or there exists a soft name
342 * then copy the service params name, otherwise use the fc name
343 */
344 if (vport->fc_nodename.u.wwn[0] == 0 || vport->phba->cfg_soft_wwnn)
345 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
346 sizeof(struct lpfc_name));
347 else
348 memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
349 sizeof(struct lpfc_name));
350
351 if (vport->fc_portname.u.wwn[0] == 0 || vport->phba->cfg_soft_wwpn)
352 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
353 sizeof(struct lpfc_name));
354 else
355 memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
356 sizeof(struct lpfc_name));
357}
358
359/**
James Smart3621a712009-04-06 18:47:14 -0400360 * lpfc_config_port_post - Perform lpfc initialization after config port
James Smarte59058c2008-08-24 21:49:00 -0400361 * @phba: pointer to lpfc hba data structure.
362 *
363 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
364 * command call. It performs all internal resource and state setups on the
365 * port: post IOCB buffers, enable appropriate host interrupt attentions,
366 * ELS ring timers, etc.
367 *
368 * Return codes
369 * 0 - success.
370 * Any other value - error.
371 **/
dea31012005-04-17 16:05:31 -0500372int
James Smart2e0fef82007-06-17 19:56:36 -0500373lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500374{
James Smart2e0fef82007-06-17 19:56:36 -0500375 struct lpfc_vport *vport = phba->pport;
James Smarta257bf92009-04-06 18:48:10 -0400376 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -0500377 LPFC_MBOXQ_t *pmb;
378 MAILBOX_t *mb;
379 struct lpfc_dmabuf *mp;
380 struct lpfc_sli *psli = &phba->sli;
381 uint32_t status, timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500382 int i, j;
383 int rc;
dea31012005-04-17 16:05:31 -0500384
James Smart7af67052007-10-27 13:38:11 -0400385 spin_lock_irq(&phba->hbalock);
386 /*
387 * If the Config port completed correctly the HBA is not
388 * over heated any more.
389 */
390 if (phba->over_temp_state == HBA_OVER_TEMP)
391 phba->over_temp_state = HBA_NORMAL_TEMP;
392 spin_unlock_irq(&phba->hbalock);
393
dea31012005-04-17 16:05:31 -0500394 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
395 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500396 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500397 return -ENOMEM;
398 }
James Smart04c68492009-05-22 14:52:52 -0400399 mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500400
dea31012005-04-17 16:05:31 -0500401 /* Get login parameters for NID. */
James Smart9f1177a2010-02-26 14:12:57 -0500402 rc = lpfc_read_sparam(phba, pmb, 0);
403 if (rc) {
404 mempool_free(pmb, phba->mbox_mem_pool);
405 return -ENOMEM;
406 }
407
James Smarted957682007-06-17 19:56:37 -0500408 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500409 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500410 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400411 "0448 Adapter failed init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500412 "READ_SPARM mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500413 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500414 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500415 mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart9f1177a2010-02-26 14:12:57 -0500416 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500417 lpfc_mbuf_free(phba, mp->virt, mp->phys);
418 kfree(mp);
419 return -EIO;
420 }
421
422 mp = (struct lpfc_dmabuf *) pmb->context1;
423
James Smart2e0fef82007-06-17 19:56:36 -0500424 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500425 lpfc_mbuf_free(phba, mp->virt, mp->phys);
426 kfree(mp);
427 pmb->context1 = NULL;
James Smart05580562011-05-24 11:40:48 -0400428 lpfc_update_vport_wwn(vport);
James Smarta257bf92009-04-06 18:48:10 -0400429
430 /* Update the fc_host data structures with new wwn. */
431 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
432 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart21e9a0a2009-05-22 14:53:21 -0400433 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smarta257bf92009-04-06 18:48:10 -0400434
dea31012005-04-17 16:05:31 -0500435 /* If no serial number in VPD data, use low 6 bytes of WWNN */
436 /* This should be consolidated into parse_vpd ? - mr */
437 if (phba->SerialNumber[0] == 0) {
438 uint8_t *outptr;
439
James Smart2e0fef82007-06-17 19:56:36 -0500440 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500441 for (i = 0; i < 12; i++) {
442 status = *outptr++;
443 j = ((status & 0xf0) >> 4);
444 if (j <= 9)
445 phba->SerialNumber[i] =
446 (char)((uint8_t) 0x30 + (uint8_t) j);
447 else
448 phba->SerialNumber[i] =
449 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
450 i++;
451 j = (status & 0xf);
452 if (j <= 9)
453 phba->SerialNumber[i] =
454 (char)((uint8_t) 0x30 + (uint8_t) j);
455 else
456 phba->SerialNumber[i] =
457 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
458 }
459 }
460
dea31012005-04-17 16:05:31 -0500461 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500462 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500463 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500464 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400465 "0453 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500466 "READ_CONFIG, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500467 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500468 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500469 mempool_free( pmb, phba->mbox_mem_pool);
470 return -EIO;
471 }
472
James Smarta0c87cb2009-07-19 10:01:10 -0400473 /* Check if the port is disabled */
474 lpfc_sli_read_link_ste(phba);
475
dea31012005-04-17 16:05:31 -0500476 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -0400477 i = (mb->un.varRdConfig.max_xri + 1);
478 if (phba->cfg_hba_queue_depth > i) {
479 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
480 "3359 HBA queue depth changed from %d to %d\n",
481 phba->cfg_hba_queue_depth, i);
482 phba->cfg_hba_queue_depth = i;
483 }
484
485 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
486 i = (mb->un.varRdConfig.max_xri >> 3);
487 if (phba->pport->cfg_lun_queue_depth > i) {
488 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
489 "3360 LUN queue depth changed from %d to %d\n",
490 phba->pport->cfg_lun_queue_depth, i);
491 phba->pport->cfg_lun_queue_depth = i;
492 }
dea31012005-04-17 16:05:31 -0500493
494 phba->lmt = mb->un.varRdConfig.lmt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500495
496 /* Get the default values for Model Name and Description */
497 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
498
James Smart2e0fef82007-06-17 19:56:36 -0500499 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500500
James Smart0b727fe2007-10-27 13:37:25 -0400501 /* Only process IOCBs on ELS ring till hba_state is READY */
James Smart7e56aa22012-08-03 12:35:34 -0400502 if (psli->ring[psli->extra_ring].sli.sli3.cmdringaddr)
James Smarta4bc3372006-12-02 13:34:16 -0500503 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
James Smart7e56aa22012-08-03 12:35:34 -0400504 if (psli->ring[psli->fcp_ring].sli.sli3.cmdringaddr)
dea31012005-04-17 16:05:31 -0500505 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
James Smart7e56aa22012-08-03 12:35:34 -0400506 if (psli->ring[psli->next_ring].sli.sli3.cmdringaddr)
dea31012005-04-17 16:05:31 -0500507 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
508
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 Smartbcece5f2014-04-04 13:51:34 -0400895 pring = &psli->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;
928 struct lpfc_sli_ring *pring;
929 LIST_HEAD(completions);
930 int i;
931
James Smartbcece5f2014-04-04 13:51:34 -0400932 for (i = 0; i < psli->num_rings; i++) {
933 pring = &psli->ring[i];
934 if (phba->sli_rev >= LPFC_SLI_REV4)
935 spin_lock_irq(&pring->ring_lock);
936 else
937 spin_lock_irq(&phba->hbalock);
938 /* At this point in time the HBA is either reset or DOA. Either
939 * way, nothing should be on txcmplq as it will NEVER complete.
940 */
941 list_splice_init(&pring->txcmplq, &completions);
James Smartdb55fba2014-04-04 13:52:02 -0400942 pring->txcmplq_cnt = 0;
James Smartbcece5f2014-04-04 13:51:34 -0400943
944 if (phba->sli_rev >= LPFC_SLI_REV4)
945 spin_unlock_irq(&pring->ring_lock);
946 else
947 spin_unlock_irq(&phba->hbalock);
948
949 /* Cancel all the IOCBs from the completions list */
950 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
951 IOERR_SLI_ABORTED);
952 lpfc_sli_abort_iocb_ring(phba, pring);
953 }
954}
955
956/**
James Smart3772a992009-05-22 14:50:54 -0400957 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smartbcece5f2014-04-04 13:51:34 -0400958 int i;
James Smarte59058c2008-08-24 21:49:00 -0400959 * @phba: pointer to lpfc HBA data structure.
960 *
961 * This routine will do uninitialization after the HBA is reset when bring
962 * down the SLI Layer.
963 *
964 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200965 * 0 - success.
James Smarte59058c2008-08-24 21:49:00 -0400966 * Any other value - error.
967 **/
James Smart3772a992009-05-22 14:50:54 -0400968static int
969lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -0500970{
James Smartbcece5f2014-04-04 13:51:34 -0400971 lpfc_hba_free_post_buf(phba);
972 lpfc_hba_clean_txcmplq(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500973 return 0;
974}
James Smart5af5eee2010-10-22 11:06:38 -0400975
James Smartda0436e2009-05-22 14:51:39 -0400976/**
977 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
978 * @phba: pointer to lpfc HBA data structure.
979 *
980 * This routine will do uninitialization after the HBA is reset when bring
981 * down the SLI Layer.
982 *
983 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200984 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400985 * Any other value - error.
986 **/
987static int
988lpfc_hba_down_post_s4(struct lpfc_hba *phba)
989{
990 struct lpfc_scsi_buf *psb, *psb_next;
991 LIST_HEAD(aborts);
James Smartda0436e2009-05-22 14:51:39 -0400992 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500993 struct lpfc_sglq *sglq_entry = NULL;
James Smartdafe8ce2014-09-03 12:56:40 -0400994 struct lpfc_sli *psli = &phba->sli;
995 struct lpfc_sli_ring *pring;
James Smart0f65ff62010-02-26 14:14:23 -0500996
James Smartbcece5f2014-04-04 13:51:34 -0400997 lpfc_hba_free_post_buf(phba);
998 lpfc_hba_clean_txcmplq(phba);
James Smartdafe8ce2014-09-03 12:56:40 -0400999 pring = &psli->ring[LPFC_ELS_RING];
James Smartbcece5f2014-04-04 13:51:34 -04001000
James Smartda0436e2009-05-22 14:51:39 -04001001 /* At this point in time the HBA is either reset or DOA. Either
1002 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
1003 * on the lpfc_sgl_list so that it can either be freed if the
1004 * driver is unloading or reposted if the driver is restarting
1005 * the port.
1006 */
1007 spin_lock_irq(&phba->hbalock); /* required for lpfc_sgl_list and */
1008 /* scsl_buf_list */
1009 /* abts_sgl_list_lock required because worker thread uses this
1010 * list.
1011 */
1012 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -05001013 list_for_each_entry(sglq_entry,
1014 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
1015 sglq_entry->state = SGL_FREED;
1016
James Smartdafe8ce2014-09-03 12:56:40 -04001017 spin_lock(&pring->ring_lock);
James Smartda0436e2009-05-22 14:51:39 -04001018 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
1019 &phba->sli4_hba.lpfc_sgl_list);
James Smartdafe8ce2014-09-03 12:56:40 -04001020 spin_unlock(&pring->ring_lock);
James Smartda0436e2009-05-22 14:51:39 -04001021 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
1022 /* abts_scsi_buf_list_lock required because worker thread uses this
1023 * list.
1024 */
1025 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
1026 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
1027 &aborts);
1028 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
1029 spin_unlock_irq(&phba->hbalock);
1030
1031 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
1032 psb->pCmd = NULL;
1033 psb->status = IOSTAT_SUCCESS;
1034 }
James Smarta40fc5f2013-04-17 20:17:40 -04001035 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
1036 list_splice(&aborts, &phba->lpfc_scsi_buf_list_put);
1037 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
James Smart68e814f2014-05-21 08:04:59 -04001038
1039 lpfc_sli4_free_sp_events(phba);
James Smartda0436e2009-05-22 14:51:39 -04001040 return 0;
1041}
1042
1043/**
1044 * lpfc_hba_down_post - Wrapper func for hba down post routine
1045 * @phba: pointer to lpfc HBA data structure.
1046 *
1047 * This routine wraps the actual SLI3 or SLI4 routine for performing
1048 * uninitialization after the HBA is reset when bring down the SLI Layer.
1049 *
1050 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001051 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001052 * Any other value - error.
1053 **/
1054int
1055lpfc_hba_down_post(struct lpfc_hba *phba)
1056{
1057 return (*phba->lpfc_hba_down_post)(phba);
1058}
Jamie Wellnitz41415862006-02-28 19:25:27 -05001059
James Smarte59058c2008-08-24 21:49:00 -04001060/**
James Smart3621a712009-04-06 18:47:14 -04001061 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001062 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1063 *
1064 * This is the HBA-timer timeout handler registered to the lpfc driver. When
1065 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
1066 * work-port-events bitmap and the worker thread is notified. This timeout
1067 * event will be used by the worker thread to invoke the actual timeout
1068 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
1069 * be performed in the timeout handler and the HBA timeout event bit shall
1070 * be cleared by the worker thread after it has taken the event bitmap out.
1071 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001072static void
James Smart858c9f62007-06-17 19:56:39 -05001073lpfc_hb_timeout(unsigned long ptr)
1074{
1075 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -04001076 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -05001077 unsigned long iflag;
1078
1079 phba = (struct lpfc_hba *)ptr;
James Smart93996272008-08-24 21:50:30 -04001080
1081 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -05001082 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04001083 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
1084 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -05001085 phba->pport->work_port_events |= WORKER_HB_TMO;
1086 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
1087
James Smart93996272008-08-24 21:50:30 -04001088 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -04001089 if (!tmo_posted)
1090 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -05001091 return;
1092}
1093
James Smarte59058c2008-08-24 21:49:00 -04001094/**
James Smart19ca7602010-11-20 23:11:55 -05001095 * lpfc_rrq_timeout - The RRQ-timer timeout handler
1096 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1097 *
1098 * This is the RRQ-timer timeout handler registered to the lpfc driver. When
1099 * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
1100 * work-port-events bitmap and the worker thread is notified. This timeout
1101 * event will be used by the worker thread to invoke the actual timeout
1102 * handler routine, lpfc_rrq_handler. Any periodical operations will
1103 * be performed in the timeout handler and the RRQ timeout event bit shall
1104 * be cleared by the worker thread after it has taken the event bitmap out.
1105 **/
1106static void
1107lpfc_rrq_timeout(unsigned long ptr)
1108{
1109 struct lpfc_hba *phba;
James Smart19ca7602010-11-20 23:11:55 -05001110 unsigned long iflag;
1111
1112 phba = (struct lpfc_hba *)ptr;
1113 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001114 if (!(phba->pport->load_flag & FC_UNLOADING))
1115 phba->hba_flag |= HBA_RRQ_ACTIVE;
1116 else
1117 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart19ca7602010-11-20 23:11:55 -05001118 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001119
1120 if (!(phba->pport->load_flag & FC_UNLOADING))
1121 lpfc_worker_wake_up(phba);
James Smart19ca7602010-11-20 23:11:55 -05001122}
1123
1124/**
James Smart3621a712009-04-06 18:47:14 -04001125 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -04001126 * @phba: pointer to lpfc hba data structure.
1127 * @pmboxq: pointer to the driver internal queue element for mailbox command.
1128 *
1129 * This is the callback function to the lpfc heart-beat mailbox command.
1130 * If configured, the lpfc driver issues the heart-beat mailbox command to
1131 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
1132 * heart-beat mailbox command is issued, the driver shall set up heart-beat
1133 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
1134 * heart-beat outstanding state. Once the mailbox command comes back and
1135 * no error conditions detected, the heart-beat mailbox command timer is
1136 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1137 * state is cleared for the next heart-beat. If the timer expired with the
1138 * heart-beat outstanding state set, the driver will put the HBA offline.
1139 **/
James Smart858c9f62007-06-17 19:56:39 -05001140static void
1141lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1142{
1143 unsigned long drvr_flag;
1144
1145 spin_lock_irqsave(&phba->hbalock, drvr_flag);
1146 phba->hb_outstanding = 0;
1147 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1148
James Smart93996272008-08-24 21:50:30 -04001149 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -05001150 mempool_free(pmboxq, phba->mbox_mem_pool);
1151 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1152 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -04001153 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -05001154 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001155 jiffies +
1156 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001157 return;
1158}
1159
James Smarte59058c2008-08-24 21:49:00 -04001160/**
James Smart3621a712009-04-06 18:47:14 -04001161 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001162 * @phba: pointer to lpfc hba data structure.
1163 *
1164 * This is the actual HBA-timer timeout handler to be invoked by the worker
1165 * thread whenever the HBA timer fired and HBA-timeout event posted. This
1166 * handler performs any periodic operations needed for the device. If such
1167 * periodic event has already been attended to either in the interrupt handler
1168 * or by processing slow-ring or fast-ring events within the HBA-timer
1169 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1170 * the timer for the next timeout period. If lpfc heart-beat mailbox command
1171 * is configured and there is no heart-beat mailbox command outstanding, a
1172 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1173 * has been a heart-beat mailbox command outstanding, the HBA shall be put
1174 * to offline.
1175 **/
James Smart858c9f62007-06-17 19:56:39 -05001176void
1177lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1178{
James Smart45ed1192009-10-02 15:17:02 -04001179 struct lpfc_vport **vports;
James Smart858c9f62007-06-17 19:56:39 -05001180 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -05001181 struct lpfc_dmabuf *buf_ptr;
James Smart45ed1192009-10-02 15:17:02 -04001182 int retval, i;
James Smart858c9f62007-06-17 19:56:39 -05001183 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -05001184 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -05001185
James Smart45ed1192009-10-02 15:17:02 -04001186 vports = lpfc_create_vport_work_array(phba);
1187 if (vports != NULL)
James Smart4258e982015-12-16 18:11:58 -05001188 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart45ed1192009-10-02 15:17:02 -04001189 lpfc_rcv_seq_check_edtov(vports[i]);
James Smart4258e982015-12-16 18:11:58 -05001190 lpfc_fdmi_num_disc_check(vports[i]);
1191 }
James Smart45ed1192009-10-02 15:17:02 -04001192 lpfc_destroy_vport_work_array(phba, vports);
1193
James Smart858c9f62007-06-17 19:56:39 -05001194 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -04001195 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -05001196 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1197 return;
1198
1199 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -05001200
James Smart256ec0d2013-04-17 20:14:58 -04001201 if (time_after(phba->last_completion_time +
1202 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL),
1203 jiffies)) {
James Smart858c9f62007-06-17 19:56:39 -05001204 spin_unlock_irq(&phba->pport->work_port_lock);
1205 if (!phba->hb_outstanding)
1206 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001207 jiffies +
1208 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001209 else
1210 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001211 jiffies +
1212 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001213 return;
1214 }
1215 spin_unlock_irq(&phba->pport->work_port_lock);
1216
James Smart0ff10d42008-01-11 01:52:36 -05001217 if (phba->elsbuf_cnt &&
1218 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1219 spin_lock_irq(&phba->hbalock);
1220 list_splice_init(&phba->elsbuf, &completions);
1221 phba->elsbuf_cnt = 0;
1222 phba->elsbuf_prev_cnt = 0;
1223 spin_unlock_irq(&phba->hbalock);
1224
1225 while (!list_empty(&completions)) {
1226 list_remove_head(&completions, buf_ptr,
1227 struct lpfc_dmabuf, list);
1228 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1229 kfree(buf_ptr);
1230 }
1231 }
1232 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1233
James Smart858c9f62007-06-17 19:56:39 -05001234 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -05001235 if (phba->cfg_enable_hba_heartbeat) {
1236 if (!phba->hb_outstanding) {
James Smartbc739052010-08-04 16:11:18 -04001237 if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1238 (list_empty(&psli->mboxq))) {
1239 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1240 GFP_KERNEL);
1241 if (!pmboxq) {
1242 mod_timer(&phba->hb_tmofunc,
1243 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001244 msecs_to_jiffies(1000 *
1245 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001246 return;
1247 }
James Smart13815c82008-01-11 01:52:48 -05001248
James Smartbc739052010-08-04 16:11:18 -04001249 lpfc_heart_beat(phba, pmboxq);
1250 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1251 pmboxq->vport = phba->pport;
1252 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1253 MBX_NOWAIT);
James Smart13815c82008-01-11 01:52:48 -05001254
James Smartbc739052010-08-04 16:11:18 -04001255 if (retval != MBX_BUSY &&
1256 retval != MBX_SUCCESS) {
1257 mempool_free(pmboxq,
1258 phba->mbox_mem_pool);
1259 mod_timer(&phba->hb_tmofunc,
1260 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001261 msecs_to_jiffies(1000 *
1262 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001263 return;
1264 }
1265 phba->skipped_hb = 0;
1266 phba->hb_outstanding = 1;
1267 } else if (time_before_eq(phba->last_completion_time,
1268 phba->skipped_hb)) {
1269 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1270 "2857 Last completion time not "
1271 " updated in %d ms\n",
1272 jiffies_to_msecs(jiffies
1273 - phba->last_completion_time));
1274 } else
1275 phba->skipped_hb = jiffies;
1276
James Smart858c9f62007-06-17 19:56:39 -05001277 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001278 jiffies +
1279 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001280 return;
James Smart13815c82008-01-11 01:52:48 -05001281 } else {
1282 /*
1283 * If heart beat timeout called with hb_outstanding set
James Smartdcf2a4e2010-09-29 11:18:53 -04001284 * we need to give the hb mailbox cmd a chance to
1285 * complete or TMO.
James Smart13815c82008-01-11 01:52:48 -05001286 */
James Smartdcf2a4e2010-09-29 11:18:53 -04001287 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1288 "0459 Adapter heartbeat still out"
1289 "standing:last compl time was %d ms.\n",
1290 jiffies_to_msecs(jiffies
1291 - phba->last_completion_time));
1292 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001293 jiffies +
1294 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001295 }
James Smart4258e982015-12-16 18:11:58 -05001296 } else {
1297 mod_timer(&phba->hb_tmofunc,
1298 jiffies +
1299 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001300 }
1301}
1302
James Smarte59058c2008-08-24 21:49:00 -04001303/**
James Smart3621a712009-04-06 18:47:14 -04001304 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -04001305 * @phba: pointer to lpfc hba data structure.
1306 *
1307 * This routine is called to bring the HBA offline when HBA hardware error
1308 * other than Port Error 6 has been detected.
1309 **/
James Smart09372822008-01-11 01:52:54 -05001310static void
1311lpfc_offline_eratt(struct lpfc_hba *phba)
1312{
1313 struct lpfc_sli *psli = &phba->sli;
1314
1315 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001316 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -05001317 spin_unlock_irq(&phba->hbalock);
James Smart618a5232012-06-12 13:54:36 -04001318 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart09372822008-01-11 01:52:54 -05001319
1320 lpfc_offline(phba);
1321 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001322 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001323 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001324 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001325 lpfc_hba_down_post(phba);
1326 lpfc_sli_brdready(phba, HS_MBRDY);
1327 lpfc_unblock_mgmt_io(phba);
1328 phba->link_state = LPFC_HBA_ERROR;
1329 return;
1330}
1331
James Smarte59058c2008-08-24 21:49:00 -04001332/**
James Smartda0436e2009-05-22 14:51:39 -04001333 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1334 * @phba: pointer to lpfc hba data structure.
1335 *
1336 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1337 * other than Port Error 6 has been detected.
1338 **/
James Smarta88dbb62013-04-17 20:18:39 -04001339void
James Smartda0436e2009-05-22 14:51:39 -04001340lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1341{
James Smart946727d2015-04-07 15:07:09 -04001342 spin_lock_irq(&phba->hbalock);
1343 phba->link_state = LPFC_HBA_ERROR;
1344 spin_unlock_irq(&phba->hbalock);
1345
James Smart618a5232012-06-12 13:54:36 -04001346 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smartda0436e2009-05-22 14:51:39 -04001347 lpfc_offline(phba);
James Smartda0436e2009-05-22 14:51:39 -04001348 lpfc_hba_down_post(phba);
James Smartda0436e2009-05-22 14:51:39 -04001349 lpfc_unblock_mgmt_io(phba);
James Smartda0436e2009-05-22 14:51:39 -04001350}
1351
1352/**
James Smarta257bf92009-04-06 18:48:10 -04001353 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1354 * @phba: pointer to lpfc hba data structure.
1355 *
1356 * This routine is invoked to handle the deferred HBA hardware error
1357 * conditions. This type of error is indicated by HBA by setting ER1
1358 * and another ER bit in the host status register. The driver will
1359 * wait until the ER1 bit clears before handling the error condition.
1360 **/
1361static void
1362lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1363{
1364 uint32_t old_host_status = phba->work_hs;
James Smarta257bf92009-04-06 18:48:10 -04001365 struct lpfc_sli *psli = &phba->sli;
1366
James Smartf4b4c682009-05-22 14:53:12 -04001367 /* If the pci channel is offline, ignore possible errors,
1368 * since we cannot communicate with the pci card anyway.
1369 */
1370 if (pci_channel_offline(phba->pcidev)) {
1371 spin_lock_irq(&phba->hbalock);
1372 phba->hba_flag &= ~DEFER_ERATT;
1373 spin_unlock_irq(&phba->hbalock);
1374 return;
1375 }
1376
James Smarta257bf92009-04-06 18:48:10 -04001377 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1378 "0479 Deferred Adapter Hardware Error "
1379 "Data: x%x x%x x%x\n",
1380 phba->work_hs,
1381 phba->work_status[0], phba->work_status[1]);
1382
1383 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001384 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001385 spin_unlock_irq(&phba->hbalock);
1386
1387
1388 /*
1389 * Firmware stops when it triggred erratt. That could cause the I/Os
1390 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1391 * SCSI layer retry it after re-establishing link.
1392 */
James Smartdb55fba2014-04-04 13:52:02 -04001393 lpfc_sli_abort_fcp_rings(phba);
James Smarta257bf92009-04-06 18:48:10 -04001394
1395 /*
1396 * There was a firmware error. Take the hba offline and then
1397 * attempt to restart it.
1398 */
James Smart618a5232012-06-12 13:54:36 -04001399 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smarta257bf92009-04-06 18:48:10 -04001400 lpfc_offline(phba);
1401
1402 /* Wait for the ER1 bit to clear.*/
1403 while (phba->work_hs & HS_FFER1) {
1404 msleep(100);
James Smart9940b972011-03-11 16:06:12 -05001405 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1406 phba->work_hs = UNPLUG_ERR ;
1407 break;
1408 }
James Smarta257bf92009-04-06 18:48:10 -04001409 /* If driver is unloading let the worker thread continue */
1410 if (phba->pport->load_flag & FC_UNLOADING) {
1411 phba->work_hs = 0;
1412 break;
1413 }
1414 }
1415
1416 /*
1417 * This is to ptrotect against a race condition in which
1418 * first write to the host attention register clear the
1419 * host status register.
1420 */
1421 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1422 phba->work_hs = old_host_status & ~HS_FFER1;
1423
James Smart3772a992009-05-22 14:50:54 -04001424 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001425 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001426 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001427 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1428 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1429}
1430
James Smart3772a992009-05-22 14:50:54 -04001431static void
1432lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1433{
1434 struct lpfc_board_event_header board_event;
1435 struct Scsi_Host *shost;
1436
1437 board_event.event_type = FC_REG_BOARD_EVENT;
1438 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1439 shost = lpfc_shost_from_vport(phba->pport);
1440 fc_host_post_vendor_event(shost, fc_get_event_number(),
1441 sizeof(board_event),
1442 (char *) &board_event,
1443 LPFC_NL_VENDOR_ID);
1444}
1445
James Smarta257bf92009-04-06 18:48:10 -04001446/**
James Smart3772a992009-05-22 14:50:54 -04001447 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001448 * @phba: pointer to lpfc hba data structure.
1449 *
1450 * This routine is invoked to handle the following HBA hardware error
1451 * conditions:
1452 * 1 - HBA error attention interrupt
1453 * 2 - DMA ring index out of range
1454 * 3 - Mailbox command came back as unknown
1455 **/
James Smart3772a992009-05-22 14:50:54 -04001456static void
1457lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001458{
James Smart2e0fef82007-06-17 19:56:36 -05001459 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001460 struct lpfc_sli *psli = &phba->sli;
James Smartd2873e42006-08-18 17:46:43 -04001461 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001462 unsigned long temperature;
1463 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001464 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001465
Linas Vepstas8d63f372007-02-14 14:28:36 -06001466 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001467 * since we cannot communicate with the pci card anyway.
1468 */
1469 if (pci_channel_offline(phba->pcidev)) {
1470 spin_lock_irq(&phba->hbalock);
1471 phba->hba_flag &= ~DEFER_ERATT;
1472 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001473 return;
James Smart3772a992009-05-22 14:50:54 -04001474 }
1475
James Smart13815c82008-01-11 01:52:48 -05001476 /* If resets are disabled then leave the HBA alone and return */
1477 if (!phba->cfg_enable_hba_reset)
1478 return;
dea31012005-04-17 16:05:31 -05001479
James Smartea2151b2008-09-07 11:52:10 -04001480 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001481 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001482
James Smarta257bf92009-04-06 18:48:10 -04001483 if (phba->hba_flag & DEFER_ERATT)
1484 lpfc_handle_deferred_eratt(phba);
1485
James Smartdcf2a4e2010-09-29 11:18:53 -04001486 if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1487 if (phba->work_hs & HS_FFER6)
1488 /* Re-establishing Link */
1489 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1490 "1301 Re-establishing Link "
1491 "Data: x%x x%x x%x\n",
1492 phba->work_hs, phba->work_status[0],
1493 phba->work_status[1]);
1494 if (phba->work_hs & HS_FFER8)
1495 /* Device Zeroization */
1496 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1497 "2861 Host Authentication device "
1498 "zeroization Data:x%x x%x x%x\n",
1499 phba->work_hs, phba->work_status[0],
1500 phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001501
James Smart92d7f7b2007-06-17 19:56:38 -05001502 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001503 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001504 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001505
1506 /*
1507 * Firmware stops when it triggled erratt with HS_FFER6.
1508 * That could cause the I/Os dropped by the firmware.
1509 * Error iocb (I/O) on txcmplq and let the SCSI layer
1510 * retry it after re-establishing link.
1511 */
James Smartdb55fba2014-04-04 13:52:02 -04001512 lpfc_sli_abort_fcp_rings(phba);
dea31012005-04-17 16:05:31 -05001513
dea31012005-04-17 16:05:31 -05001514 /*
1515 * There was a firmware error. Take the hba offline and then
1516 * attempt to restart it.
1517 */
James Smart618a5232012-06-12 13:54:36 -04001518 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
dea31012005-04-17 16:05:31 -05001519 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001520 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001521 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001522 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001523 return;
1524 }
James Smart46fa3112007-04-25 09:51:45 -04001525 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001526 } else if (phba->work_hs & HS_CRIT_TEMP) {
1527 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1528 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1529 temp_event_data.event_code = LPFC_CRIT_TEMP;
1530 temp_event_data.data = (uint32_t)temperature;
1531
1532 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001533 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001534 "(%ld), taking this port offline "
1535 "Data: x%x x%x x%x\n",
1536 temperature, phba->work_hs,
1537 phba->work_status[0], phba->work_status[1]);
1538
1539 shost = lpfc_shost_from_vport(phba->pport);
1540 fc_host_post_vendor_event(shost, fc_get_event_number(),
1541 sizeof(temp_event_data),
1542 (char *) &temp_event_data,
1543 SCSI_NL_VID_TYPE_PCI
1544 | PCI_VENDOR_ID_EMULEX);
1545
James Smart7af67052007-10-27 13:38:11 -04001546 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001547 phba->over_temp_state = HBA_OVER_TEMP;
1548 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001549 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001550
dea31012005-04-17 16:05:31 -05001551 } else {
1552 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001553 * failure is a value other than FFER6. Do not call the offline
1554 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001555 */
1556 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001557 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001558 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001559 phba->work_hs,
dea31012005-04-17 16:05:31 -05001560 phba->work_status[0], phba->work_status[1]);
1561
James Smartd2873e42006-08-18 17:46:43 -04001562 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001563 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001564 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001565 sizeof(event_data), (char *) &event_data,
1566 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1567
James Smart09372822008-01-11 01:52:54 -05001568 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001569 }
James Smart93996272008-08-24 21:50:30 -04001570 return;
dea31012005-04-17 16:05:31 -05001571}
1572
James Smarte59058c2008-08-24 21:49:00 -04001573/**
James Smart618a5232012-06-12 13:54:36 -04001574 * lpfc_sli4_port_sta_fn_reset - The SLI4 function reset due to port status reg
1575 * @phba: pointer to lpfc hba data structure.
1576 * @mbx_action: flag for mailbox shutdown action.
1577 *
1578 * This routine is invoked to perform an SLI4 port PCI function reset in
1579 * response to port status register polling attention. It waits for port
1580 * status register (ERR, RDY, RN) bits before proceeding with function reset.
1581 * During this process, interrupt vectors are freed and later requested
1582 * for handling possible port resource change.
1583 **/
1584static int
James Smarte10b2022014-02-20 09:57:43 -05001585lpfc_sli4_port_sta_fn_reset(struct lpfc_hba *phba, int mbx_action,
1586 bool en_rn_msg)
James Smart618a5232012-06-12 13:54:36 -04001587{
1588 int rc;
1589 uint32_t intr_mode;
1590
James Smart65791f12016-07-06 12:35:56 -07001591 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
1592 LPFC_SLI_INTF_IF_TYPE_2) {
1593 /*
1594 * On error status condition, driver need to wait for port
1595 * ready before performing reset.
1596 */
1597 rc = lpfc_sli4_pdev_status_reg_wait(phba);
James Smart0e916ee2016-07-06 12:36:06 -07001598 if (rc)
James Smart65791f12016-07-06 12:35:56 -07001599 return rc;
James Smart618a5232012-06-12 13:54:36 -04001600 }
James Smart0e916ee2016-07-06 12:36:06 -07001601
James Smart65791f12016-07-06 12:35:56 -07001602 /* need reset: attempt for port recovery */
1603 if (en_rn_msg)
1604 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1605 "2887 Reset Needed: Attempting Port "
1606 "Recovery...\n");
1607 lpfc_offline_prep(phba, mbx_action);
1608 lpfc_offline(phba);
1609 /* release interrupt for possible resource change */
1610 lpfc_sli4_disable_intr(phba);
1611 lpfc_sli_brdrestart(phba);
1612 /* request and enable interrupt */
1613 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
1614 if (intr_mode == LPFC_INTR_ERROR) {
1615 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1616 "3175 Failed to enable interrupt\n");
1617 return -EIO;
1618 }
1619 phba->intr_mode = intr_mode;
1620 rc = lpfc_online(phba);
1621 if (rc == 0)
1622 lpfc_unblock_mgmt_io(phba);
1623
James Smart618a5232012-06-12 13:54:36 -04001624 return rc;
1625}
1626
1627/**
James Smartda0436e2009-05-22 14:51:39 -04001628 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1629 * @phba: pointer to lpfc hba data structure.
1630 *
1631 * This routine is invoked to handle the SLI4 HBA hardware error attention
1632 * conditions.
1633 **/
1634static void
1635lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1636{
1637 struct lpfc_vport *vport = phba->pport;
1638 uint32_t event_data;
1639 struct Scsi_Host *shost;
James Smart2fcee4b2010-12-15 17:57:46 -05001640 uint32_t if_type;
James Smart2e90f4b2011-12-13 13:22:37 -05001641 struct lpfc_register portstat_reg = {0};
1642 uint32_t reg_err1, reg_err2;
1643 uint32_t uerrlo_reg, uemasklo_reg;
James Smart65791f12016-07-06 12:35:56 -07001644 uint32_t smphr_port_status = 0, pci_rd_rc1, pci_rd_rc2;
James Smarte10b2022014-02-20 09:57:43 -05001645 bool en_rn_msg = true;
James Smart946727d2015-04-07 15:07:09 -04001646 struct temp_event temp_event_data;
James Smart65791f12016-07-06 12:35:56 -07001647 struct lpfc_register portsmphr_reg;
1648 int rc, i;
James Smartda0436e2009-05-22 14:51:39 -04001649
1650 /* If the pci channel is offline, ignore possible errors, since
1651 * we cannot communicate with the pci card anyway.
1652 */
1653 if (pci_channel_offline(phba->pcidev))
1654 return;
James Smartda0436e2009-05-22 14:51:39 -04001655
James Smart65791f12016-07-06 12:35:56 -07001656 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
James Smart2fcee4b2010-12-15 17:57:46 -05001657 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1658 switch (if_type) {
1659 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart2e90f4b2011-12-13 13:22:37 -05001660 pci_rd_rc1 = lpfc_readl(
1661 phba->sli4_hba.u.if_type0.UERRLOregaddr,
1662 &uerrlo_reg);
1663 pci_rd_rc2 = lpfc_readl(
1664 phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
1665 &uemasklo_reg);
1666 /* consider PCI bus read error as pci_channel_offline */
1667 if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
1668 return;
James Smart65791f12016-07-06 12:35:56 -07001669 if (!(phba->hba_flag & HBA_RECOVERABLE_UE)) {
1670 lpfc_sli4_offline_eratt(phba);
1671 return;
1672 }
1673 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1674 "7623 Checking UE recoverable");
1675
1676 for (i = 0; i < phba->sli4_hba.ue_to_sr / 1000; i++) {
1677 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1678 &portsmphr_reg.word0))
1679 continue;
1680
1681 smphr_port_status = bf_get(lpfc_port_smphr_port_status,
1682 &portsmphr_reg);
1683 if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1684 LPFC_PORT_SEM_UE_RECOVERABLE)
1685 break;
1686 /*Sleep for 1Sec, before checking SEMAPHORE */
1687 msleep(1000);
1688 }
1689
1690 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1691 "4827 smphr_port_status x%x : Waited %dSec",
1692 smphr_port_status, i);
1693
1694 /* Recoverable UE, reset the HBA device */
1695 if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1696 LPFC_PORT_SEM_UE_RECOVERABLE) {
1697 for (i = 0; i < 20; i++) {
1698 msleep(1000);
1699 if (!lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1700 &portsmphr_reg.word0) &&
1701 (LPFC_POST_STAGE_PORT_READY ==
1702 bf_get(lpfc_port_smphr_port_status,
1703 &portsmphr_reg))) {
1704 rc = lpfc_sli4_port_sta_fn_reset(phba,
1705 LPFC_MBX_NO_WAIT, en_rn_msg);
1706 if (rc == 0)
1707 return;
1708 lpfc_printf_log(phba,
1709 KERN_ERR, LOG_INIT,
1710 "4215 Failed to recover UE");
1711 break;
1712 }
1713 }
1714 }
1715 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1716 "7624 Firmware not ready: Failing UE recovery,"
1717 " waited %dSec", i);
James Smart2fcee4b2010-12-15 17:57:46 -05001718 lpfc_sli4_offline_eratt(phba);
1719 break;
James Smart946727d2015-04-07 15:07:09 -04001720
James Smart2fcee4b2010-12-15 17:57:46 -05001721 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart2e90f4b2011-12-13 13:22:37 -05001722 pci_rd_rc1 = lpfc_readl(
1723 phba->sli4_hba.u.if_type2.STATUSregaddr,
1724 &portstat_reg.word0);
1725 /* consider PCI bus read error as pci_channel_offline */
James Smart6b5151f2012-01-18 16:24:06 -05001726 if (pci_rd_rc1 == -EIO) {
1727 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1728 "3151 PCI bus read access failure: x%x\n",
1729 readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
James Smart2e90f4b2011-12-13 13:22:37 -05001730 return;
James Smart6b5151f2012-01-18 16:24:06 -05001731 }
James Smart2e90f4b2011-12-13 13:22:37 -05001732 reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
1733 reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
James Smart2fcee4b2010-12-15 17:57:46 -05001734 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
James Smart2fcee4b2010-12-15 17:57:46 -05001735 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1736 "2889 Port Overtemperature event, "
James Smart946727d2015-04-07 15:07:09 -04001737 "taking port offline Data: x%x x%x\n",
1738 reg_err1, reg_err2);
1739
James Smart310429e2016-07-06 12:35:54 -07001740 phba->sfp_alarm |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
James Smart946727d2015-04-07 15:07:09 -04001741 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1742 temp_event_data.event_code = LPFC_CRIT_TEMP;
1743 temp_event_data.data = 0xFFFFFFFF;
1744
1745 shost = lpfc_shost_from_vport(phba->pport);
1746 fc_host_post_vendor_event(shost, fc_get_event_number(),
1747 sizeof(temp_event_data),
1748 (char *)&temp_event_data,
1749 SCSI_NL_VID_TYPE_PCI
1750 | PCI_VENDOR_ID_EMULEX);
1751
James Smart2fcee4b2010-12-15 17:57:46 -05001752 spin_lock_irq(&phba->hbalock);
1753 phba->over_temp_state = HBA_OVER_TEMP;
1754 spin_unlock_irq(&phba->hbalock);
1755 lpfc_sli4_offline_eratt(phba);
James Smart946727d2015-04-07 15:07:09 -04001756 return;
James Smart2fcee4b2010-12-15 17:57:46 -05001757 }
James Smart2e90f4b2011-12-13 13:22:37 -05001758 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smarte10b2022014-02-20 09:57:43 -05001759 reg_err2 == SLIPORT_ERR2_REG_FW_RESTART) {
James Smart2e90f4b2011-12-13 13:22:37 -05001760 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte10b2022014-02-20 09:57:43 -05001761 "3143 Port Down: Firmware Update "
1762 "Detected\n");
1763 en_rn_msg = false;
1764 } else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smart2e90f4b2011-12-13 13:22:37 -05001765 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1766 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1767 "3144 Port Down: Debug Dump\n");
1768 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1769 reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
1770 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1771 "3145 Port Down: Provisioning\n");
James Smart618a5232012-06-12 13:54:36 -04001772
James Smart946727d2015-04-07 15:07:09 -04001773 /* If resets are disabled then leave the HBA alone and return */
1774 if (!phba->cfg_enable_hba_reset)
1775 return;
1776
James Smart618a5232012-06-12 13:54:36 -04001777 /* Check port status register for function reset */
James Smarte10b2022014-02-20 09:57:43 -05001778 rc = lpfc_sli4_port_sta_fn_reset(phba, LPFC_MBX_NO_WAIT,
1779 en_rn_msg);
James Smart618a5232012-06-12 13:54:36 -04001780 if (rc == 0) {
1781 /* don't report event on forced debug dump */
1782 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1783 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1784 return;
1785 else
1786 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001787 }
James Smart618a5232012-06-12 13:54:36 -04001788 /* fall through for not able to recover */
James Smart6b5151f2012-01-18 16:24:06 -05001789 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1790 "3152 Unrecoverable error, bring the port "
1791 "offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001792 lpfc_sli4_offline_eratt(phba);
1793 break;
1794 case LPFC_SLI_INTF_IF_TYPE_1:
1795 default:
1796 break;
1797 }
James Smart2e90f4b2011-12-13 13:22:37 -05001798 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1799 "3123 Report dump event to upper layer\n");
1800 /* Send an internal error event to mgmt application */
1801 lpfc_board_errevt_to_mgmt(phba);
1802
1803 event_data = FC_REG_DUMP_EVENT;
1804 shost = lpfc_shost_from_vport(vport);
1805 fc_host_post_vendor_event(shost, fc_get_event_number(),
1806 sizeof(event_data), (char *) &event_data,
1807 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
James Smartda0436e2009-05-22 14:51:39 -04001808}
1809
1810/**
1811 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1812 * @phba: pointer to lpfc HBA data structure.
1813 *
1814 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1815 * routine from the API jump table function pointer from the lpfc_hba struct.
1816 *
1817 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001818 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001819 * Any other value - error.
1820 **/
1821void
1822lpfc_handle_eratt(struct lpfc_hba *phba)
1823{
1824 (*phba->lpfc_handle_eratt)(phba);
1825}
1826
1827/**
James Smart3621a712009-04-06 18:47:14 -04001828 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04001829 * @phba: pointer to lpfc hba data structure.
1830 *
1831 * This routine is invoked from the worker thread to handle a HBA host
1832 * attention link event.
1833 **/
dea31012005-04-17 16:05:31 -05001834void
James Smart2e0fef82007-06-17 19:56:36 -05001835lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001836{
James Smart2e0fef82007-06-17 19:56:36 -05001837 struct lpfc_vport *vport = phba->pport;
1838 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001839 LPFC_MBOXQ_t *pmb;
1840 volatile uint32_t control;
1841 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05001842 int rc = 0;
dea31012005-04-17 16:05:31 -05001843
1844 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001845 if (!pmb) {
1846 rc = 1;
dea31012005-04-17 16:05:31 -05001847 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05001848 }
dea31012005-04-17 16:05:31 -05001849
1850 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001851 if (!mp) {
1852 rc = 2;
dea31012005-04-17 16:05:31 -05001853 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05001854 }
dea31012005-04-17 16:05:31 -05001855
1856 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05001857 if (!mp->virt) {
1858 rc = 3;
dea31012005-04-17 16:05:31 -05001859 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05001860 }
dea31012005-04-17 16:05:31 -05001861
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05001862 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04001863 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05001864
1865 psli->slistat.link_event++;
James Smart76a95d72010-11-20 23:11:48 -05001866 lpfc_read_topology(phba, pmb, mp);
1867 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smart2e0fef82007-06-17 19:56:36 -05001868 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04001869 /* Block ELS IOCBs until we have processed this mbox command */
1870 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04001871 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05001872 if (rc == MBX_NOT_FINISHED) {
1873 rc = 4;
James Smart14691152006-12-02 13:34:28 -05001874 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05001875 }
dea31012005-04-17 16:05:31 -05001876
1877 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05001878 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001879 writel(HA_LATT, phba->HAregaddr);
1880 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001881 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001882
1883 return;
1884
James Smart14691152006-12-02 13:34:28 -05001885lpfc_handle_latt_free_mbuf:
James Smart0d2b6b82008-06-14 22:52:47 -04001886 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05001887 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001888lpfc_handle_latt_free_mp:
1889 kfree(mp);
1890lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04001891 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001892lpfc_handle_latt_err_exit:
1893 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001894 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001895 psli->sli_flag |= LPFC_PROCESS_LA;
1896 control = readl(phba->HCregaddr);
1897 control |= HC_LAINT_ENA;
1898 writel(control, phba->HCregaddr);
1899 readl(phba->HCregaddr); /* flush */
1900
1901 /* Clear Link Attention in HA REG */
1902 writel(HA_LATT, phba->HAregaddr);
1903 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001904 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001905 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001906 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001907
James Smart09372822008-01-11 01:52:54 -05001908 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1909 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05001910
1911 return;
1912}
1913
James Smarte59058c2008-08-24 21:49:00 -04001914/**
James Smart3621a712009-04-06 18:47:14 -04001915 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04001916 * @phba: pointer to lpfc hba data structure.
1917 * @vpd: pointer to the vital product data.
1918 * @len: length of the vital product data in bytes.
1919 *
1920 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1921 * an array of characters. In this routine, the ModelName, ProgramType, and
1922 * ModelDesc, etc. fields of the phba data structure will be populated.
1923 *
1924 * Return codes
1925 * 0 - pointer to the VPD passed in is NULL
1926 * 1 - success
1927 **/
James Smart3772a992009-05-22 14:50:54 -04001928int
James Smart2e0fef82007-06-17 19:56:36 -05001929lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05001930{
1931 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05001932 int Length;
dea31012005-04-17 16:05:31 -05001933 int i, j;
1934 int finished = 0;
1935 int index = 0;
1936
1937 if (!vpd)
1938 return 0;
1939
1940 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05001941 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001942 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05001943 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1944 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001945 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05001946 switch (vpd[index]) {
1947 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001948 case 0x91:
dea31012005-04-17 16:05:31 -05001949 index += 1;
1950 lenlo = vpd[index];
1951 index += 1;
1952 lenhi = vpd[index];
1953 index += 1;
1954 i = ((((unsigned short)lenhi) << 8) + lenlo);
1955 index += i;
1956 break;
1957 case 0x90:
1958 index += 1;
1959 lenlo = vpd[index];
1960 index += 1;
1961 lenhi = vpd[index];
1962 index += 1;
1963 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001964 if (Length > len - index)
1965 Length = len - index;
dea31012005-04-17 16:05:31 -05001966 while (Length > 0) {
1967 /* Look for Serial Number */
1968 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1969 index += 2;
1970 i = vpd[index];
1971 index += 1;
1972 j = 0;
1973 Length -= (3+i);
1974 while(i--) {
1975 phba->SerialNumber[j++] = vpd[index++];
1976 if (j == 31)
1977 break;
1978 }
1979 phba->SerialNumber[j] = 0;
1980 continue;
1981 }
1982 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1983 phba->vpd_flag |= VPD_MODEL_DESC;
1984 index += 2;
1985 i = vpd[index];
1986 index += 1;
1987 j = 0;
1988 Length -= (3+i);
1989 while(i--) {
1990 phba->ModelDesc[j++] = vpd[index++];
1991 if (j == 255)
1992 break;
1993 }
1994 phba->ModelDesc[j] = 0;
1995 continue;
1996 }
1997 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1998 phba->vpd_flag |= VPD_MODEL_NAME;
1999 index += 2;
2000 i = vpd[index];
2001 index += 1;
2002 j = 0;
2003 Length -= (3+i);
2004 while(i--) {
2005 phba->ModelName[j++] = vpd[index++];
2006 if (j == 79)
2007 break;
2008 }
2009 phba->ModelName[j] = 0;
2010 continue;
2011 }
2012 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
2013 phba->vpd_flag |= VPD_PROGRAM_TYPE;
2014 index += 2;
2015 i = vpd[index];
2016 index += 1;
2017 j = 0;
2018 Length -= (3+i);
2019 while(i--) {
2020 phba->ProgramType[j++] = vpd[index++];
2021 if (j == 255)
2022 break;
2023 }
2024 phba->ProgramType[j] = 0;
2025 continue;
2026 }
2027 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
2028 phba->vpd_flag |= VPD_PORT;
2029 index += 2;
2030 i = vpd[index];
2031 index += 1;
2032 j = 0;
2033 Length -= (3+i);
2034 while(i--) {
James Smartcd1c8302011-10-10 21:33:25 -04002035 if ((phba->sli_rev == LPFC_SLI_REV4) &&
2036 (phba->sli4_hba.pport_name_sta ==
2037 LPFC_SLI4_PPNAME_GET)) {
2038 j++;
2039 index++;
2040 } else
2041 phba->Port[j++] = vpd[index++];
2042 if (j == 19)
2043 break;
dea31012005-04-17 16:05:31 -05002044 }
James Smartcd1c8302011-10-10 21:33:25 -04002045 if ((phba->sli_rev != LPFC_SLI_REV4) ||
2046 (phba->sli4_hba.pport_name_sta ==
2047 LPFC_SLI4_PPNAME_NON))
2048 phba->Port[j] = 0;
dea31012005-04-17 16:05:31 -05002049 continue;
2050 }
2051 else {
2052 index += 2;
2053 i = vpd[index];
2054 index += 1;
2055 index += i;
2056 Length -= (3 + i);
2057 }
2058 }
2059 finished = 0;
2060 break;
2061 case 0x78:
2062 finished = 1;
2063 break;
2064 default:
2065 index ++;
2066 break;
2067 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002068 }
dea31012005-04-17 16:05:31 -05002069
2070 return(1);
2071}
2072
James Smarte59058c2008-08-24 21:49:00 -04002073/**
James Smart3621a712009-04-06 18:47:14 -04002074 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04002075 * @phba: pointer to lpfc hba data structure.
2076 * @mdp: pointer to the data structure to hold the derived model name.
2077 * @descp: pointer to the data structure to hold the derived description.
2078 *
2079 * This routine retrieves HBA's description based on its registered PCI device
2080 * ID. The @descp passed into this function points to an array of 256 chars. It
2081 * shall be returned with the model name, maximum speed, and the host bus type.
2082 * The @mdp passed into this function points to an array of 80 chars. When the
2083 * function returns, the @mdp will be filled with the model name.
2084 **/
dea31012005-04-17 16:05:31 -05002085static void
James Smart2e0fef82007-06-17 19:56:36 -05002086lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05002087{
2088 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05002089 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002090 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04002091 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04002092 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002093 struct {
James Smarta747c9c2009-11-18 15:41:10 -05002094 char *name;
2095 char *bus;
2096 char *function;
2097 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002098
2099 if (mdp && mdp[0] != '\0'
2100 && descp && descp[0] != '\0')
2101 return;
2102
James Smartd38dd522015-08-31 16:48:17 -04002103 if (phba->lmt & LMT_32Gb)
2104 max_speed = 32;
2105 else if (phba->lmt & LMT_16Gb)
James Smartc0c11512011-05-24 11:41:34 -04002106 max_speed = 16;
2107 else if (phba->lmt & LMT_10Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002108 max_speed = 10;
2109 else if (phba->lmt & LMT_8Gb)
2110 max_speed = 8;
2111 else if (phba->lmt & LMT_4Gb)
2112 max_speed = 4;
2113 else if (phba->lmt & LMT_2Gb)
2114 max_speed = 2;
James Smart4169d862012-09-29 11:32:09 -04002115 else if (phba->lmt & LMT_1Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002116 max_speed = 1;
James Smart4169d862012-09-29 11:32:09 -04002117 else
2118 max_speed = 0;
dea31012005-04-17 16:05:31 -05002119
2120 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05002121
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002122 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002123 case PCI_DEVICE_ID_FIREFLY:
James Smart12222f42014-05-21 08:05:19 -04002124 m = (typeof(m)){"LP6000", "PCI",
2125 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002126 break;
dea31012005-04-17 16:05:31 -05002127 case PCI_DEVICE_ID_SUPERFLY:
2128 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smart12222f42014-05-21 08:05:19 -04002129 m = (typeof(m)){"LP7000", "PCI", ""};
dea31012005-04-17 16:05:31 -05002130 else
James Smart12222f42014-05-21 08:05:19 -04002131 m = (typeof(m)){"LP7000E", "PCI", ""};
2132 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
dea31012005-04-17 16:05:31 -05002133 break;
2134 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002135 m = (typeof(m)){"LP8000", "PCI",
James Smart12222f42014-05-21 08:05:19 -04002136 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002137 break;
2138 case PCI_DEVICE_ID_CENTAUR:
2139 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smart12222f42014-05-21 08:05:19 -04002140 m = (typeof(m)){"LP9002", "PCI", ""};
dea31012005-04-17 16:05:31 -05002141 else
James Smart12222f42014-05-21 08:05:19 -04002142 m = (typeof(m)){"LP9000", "PCI", ""};
2143 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
dea31012005-04-17 16:05:31 -05002144 break;
2145 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002146 m = (typeof(m)){"LP952", "PCI",
James Smart12222f42014-05-21 08:05:19 -04002147 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002148 break;
2149 case PCI_DEVICE_ID_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05002150 m = (typeof(m)){"LP9802", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002151 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002152 break;
2153 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05002154 m = (typeof(m)){"LP10000", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002155 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002156 break;
2157 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05002158 m = (typeof(m)){"LPX1000", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002159 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002160 break;
2161 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002162 m = (typeof(m)){"LP982", "PCI-X",
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_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002166 m = (typeof(m)){"LP1050", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002167 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002168 break;
2169 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05002170 m = (typeof(m)){"LP11000", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002171 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002172 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002173 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002174 m = (typeof(m)){"LP11000-SP", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002175 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002176 break;
2177 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002178 m = (typeof(m)){"LP11002-SP", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002179 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002180 break;
2181 case PCI_DEVICE_ID_NEPTUNE:
James Smart12222f42014-05-21 08:05:19 -04002182 m = (typeof(m)){"LPe1000", "PCIe",
2183 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002184 break;
2185 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smart12222f42014-05-21 08:05:19 -04002186 m = (typeof(m)){"LPe1000-SP", "PCIe",
2187 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002188 break;
2189 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smart12222f42014-05-21 08:05:19 -04002190 m = (typeof(m)){"LPe1002-SP", "PCIe",
2191 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002192 break;
dea31012005-04-17 16:05:31 -05002193 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05002194 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002195 break;
2196 case PCI_DEVICE_ID_BSMB:
James Smart12222f42014-05-21 08:05:19 -04002197 m = (typeof(m)){"LP111", "PCI-X2",
2198 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002199 break;
2200 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05002201 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002202 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002203 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002204 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002205 break;
2206 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002207 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04002208 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002209 break;
dea31012005-04-17 16:05:31 -05002210 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05002211 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002212 break;
2213 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05002214 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002215 break;
2216 case PCI_DEVICE_ID_LP101:
James Smart12222f42014-05-21 08:05:19 -04002217 m = (typeof(m)){"LP101", "PCI-X",
2218 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002219 break;
2220 case PCI_DEVICE_ID_LP10000S:
James Smart12222f42014-05-21 08:05:19 -04002221 m = (typeof(m)){"LP10000-S", "PCI",
2222 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002223 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002224 case PCI_DEVICE_ID_LP11000S:
James Smart12222f42014-05-21 08:05:19 -04002225 m = (typeof(m)){"LP11000-S", "PCI-X2",
2226 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05002227 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002228 case PCI_DEVICE_ID_LPE11000S:
James Smart12222f42014-05-21 08:05:19 -04002229 m = (typeof(m)){"LPe11000-S", "PCIe",
2230 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002231 break;
James Smartb87eab32007-04-25 09:53:28 -04002232 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05002233 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002234 break;
2235 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05002236 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002237 break;
2238 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05002239 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002240 break;
2241 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002242 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002243 break;
2244 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002245 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002246 break;
2247 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05002248 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002249 break;
James Smart84774a42008-08-24 21:50:06 -04002250 case PCI_DEVICE_ID_HORNET:
James Smart12222f42014-05-21 08:05:19 -04002251 m = (typeof(m)){"LP21000", "PCIe",
2252 "Obsolete, Unsupported FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002253 GE = 1;
2254 break;
2255 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05002256 m = (typeof(m)){"LPev12000", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002257 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002258 break;
2259 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05002260 m = (typeof(m)){"LPev12000", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002261 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002262 break;
2263 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05002264 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002265 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002266 break;
James Smartda0436e2009-05-22 14:51:39 -04002267 case PCI_DEVICE_ID_TIGERSHARK:
2268 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002269 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04002270 break;
James Smarta747c9c2009-11-18 15:41:10 -05002271 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04002272 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002273 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
2274 break;
2275 case PCI_DEVICE_ID_FALCON:
2276 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
2277 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04002278 break;
James Smart98fc5dd2010-06-07 15:24:29 -04002279 case PCI_DEVICE_ID_BALIUS:
2280 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
James Smart12222f42014-05-21 08:05:19 -04002281 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart98fc5dd2010-06-07 15:24:29 -04002282 break;
James Smart085c6472010-11-20 23:11:37 -05002283 case PCI_DEVICE_ID_LANCER_FC:
James Smartc0c11512011-05-24 11:41:34 -04002284 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
James Smart085c6472010-11-20 23:11:37 -05002285 break;
James Smart12222f42014-05-21 08:05:19 -04002286 case PCI_DEVICE_ID_LANCER_FC_VF:
2287 m = (typeof(m)){"LPe16000", "PCIe",
2288 "Obsolete, Unsupported Fibre Channel Adapter"};
2289 break;
James Smart085c6472010-11-20 23:11:37 -05002290 case PCI_DEVICE_ID_LANCER_FCOE:
2291 oneConnect = 1;
James Smart079b5c92011-08-21 21:48:49 -04002292 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
James Smart085c6472010-11-20 23:11:37 -05002293 break;
James Smart12222f42014-05-21 08:05:19 -04002294 case PCI_DEVICE_ID_LANCER_FCOE_VF:
2295 oneConnect = 1;
2296 m = (typeof(m)){"OCe15100", "PCIe",
2297 "Obsolete, Unsupported FCoE"};
2298 break;
James Smartd38dd522015-08-31 16:48:17 -04002299 case PCI_DEVICE_ID_LANCER_G6_FC:
2300 m = (typeof(m)){"LPe32000", "PCIe", "Fibre Channel Adapter"};
2301 break;
James Smartf8cafd32012-08-03 12:42:51 -04002302 case PCI_DEVICE_ID_SKYHAWK:
2303 case PCI_DEVICE_ID_SKYHAWK_VF:
2304 oneConnect = 1;
2305 m = (typeof(m)){"OCe14000", "PCIe", "FCoE"};
2306 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002307 default:
James Smarta747c9c2009-11-18 15:41:10 -05002308 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002309 break;
dea31012005-04-17 16:05:31 -05002310 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002311
2312 if (mdp && mdp[0] == '\0')
2313 snprintf(mdp, 79,"%s", m.name);
James Smartc0c11512011-05-24 11:41:34 -04002314 /*
2315 * oneConnect hba requires special processing, they are all initiators
James Smartda0436e2009-05-22 14:51:39 -04002316 * and we put the port number on the end
2317 */
2318 if (descp && descp[0] == '\0') {
2319 if (oneConnect)
2320 snprintf(descp, 255,
James Smart4169d862012-09-29 11:32:09 -04002321 "Emulex OneConnect %s, %s Initiator %s",
James Smarta747c9c2009-11-18 15:41:10 -05002322 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04002323 phba->Port);
James Smart4169d862012-09-29 11:32:09 -04002324 else if (max_speed == 0)
2325 snprintf(descp, 255,
Sebastian Herbszt290237d2015-08-31 16:48:08 -04002326 "Emulex %s %s %s",
James Smart4169d862012-09-29 11:32:09 -04002327 m.name, m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002328 else
2329 snprintf(descp, 255,
2330 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05002331 m.name, max_speed, (GE) ? "GE" : "Gb",
2332 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002333 }
dea31012005-04-17 16:05:31 -05002334}
2335
James Smarte59058c2008-08-24 21:49:00 -04002336/**
James Smart3621a712009-04-06 18:47:14 -04002337 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04002338 * @phba: pointer to lpfc hba data structure.
2339 * @pring: pointer to a IOCB ring.
2340 * @cnt: the number of IOCBs to be posted to the IOCB ring.
2341 *
2342 * This routine posts a given number of IOCBs with the associated DMA buffer
2343 * descriptors specified by the cnt argument to the given IOCB ring.
2344 *
2345 * Return codes
2346 * The number of IOCBs NOT able to be posted to the IOCB ring.
2347 **/
dea31012005-04-17 16:05:31 -05002348int
James Smart495a7142008-06-14 22:52:59 -04002349lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05002350{
2351 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002352 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05002353 struct lpfc_dmabuf *mp1, *mp2;
2354
2355 cnt += pring->missbufcnt;
2356
2357 /* While there are buffers to post */
2358 while (cnt > 0) {
2359 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002360 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002361 if (iocb == NULL) {
2362 pring->missbufcnt = cnt;
2363 return cnt;
2364 }
dea31012005-04-17 16:05:31 -05002365 icmd = &iocb->iocb;
2366
2367 /* 2 buffers can be posted per command */
2368 /* Allocate buffer to post */
2369 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2370 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04002371 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2372 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002373 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002374 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002375 pring->missbufcnt = cnt;
2376 return cnt;
2377 }
2378
2379 INIT_LIST_HEAD(&mp1->list);
2380 /* Allocate buffer to post */
2381 if (cnt > 1) {
2382 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2383 if (mp2)
2384 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2385 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04002386 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002387 kfree(mp2);
dea31012005-04-17 16:05:31 -05002388 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2389 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002390 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002391 pring->missbufcnt = cnt;
2392 return cnt;
2393 }
2394
2395 INIT_LIST_HEAD(&mp2->list);
2396 } else {
2397 mp2 = NULL;
2398 }
2399
2400 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2401 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2402 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2403 icmd->ulpBdeCount = 1;
2404 cnt--;
2405 if (mp2) {
2406 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2407 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2408 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2409 cnt--;
2410 icmd->ulpBdeCount = 2;
2411 }
2412
2413 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2414 icmd->ulpLe = 1;
2415
James Smart3772a992009-05-22 14:50:54 -04002416 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2417 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05002418 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2419 kfree(mp1);
2420 cnt++;
2421 if (mp2) {
2422 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2423 kfree(mp2);
2424 cnt++;
2425 }
James Bottomley604a3e32005-10-29 10:28:33 -05002426 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002427 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05002428 return cnt;
2429 }
dea31012005-04-17 16:05:31 -05002430 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05002431 if (mp2)
dea31012005-04-17 16:05:31 -05002432 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05002433 }
2434 pring->missbufcnt = 0;
2435 return 0;
2436}
2437
James Smarte59058c2008-08-24 21:49:00 -04002438/**
James Smart3621a712009-04-06 18:47:14 -04002439 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04002440 * @phba: pointer to lpfc hba data structure.
2441 *
2442 * This routine posts initial receive IOCB buffers to the ELS ring. The
2443 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
2444 * set to 64 IOCBs.
2445 *
2446 * Return codes
2447 * 0 - success (currently always success)
2448 **/
dea31012005-04-17 16:05:31 -05002449static int
James Smart2e0fef82007-06-17 19:56:36 -05002450lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002451{
2452 struct lpfc_sli *psli = &phba->sli;
2453
2454 /* Ring 0, ELS / CT buffers */
James Smart495a7142008-06-14 22:52:59 -04002455 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05002456 /* Ring 2 - FCP no buffers needed */
2457
2458 return 0;
2459}
2460
2461#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2462
James Smarte59058c2008-08-24 21:49:00 -04002463/**
James Smart3621a712009-04-06 18:47:14 -04002464 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04002465 * @HashResultPointer: pointer to an array as hash table.
2466 *
2467 * This routine sets up the initial values to the array of hash table entries
2468 * for the LC HBAs.
2469 **/
dea31012005-04-17 16:05:31 -05002470static void
2471lpfc_sha_init(uint32_t * HashResultPointer)
2472{
2473 HashResultPointer[0] = 0x67452301;
2474 HashResultPointer[1] = 0xEFCDAB89;
2475 HashResultPointer[2] = 0x98BADCFE;
2476 HashResultPointer[3] = 0x10325476;
2477 HashResultPointer[4] = 0xC3D2E1F0;
2478}
2479
James Smarte59058c2008-08-24 21:49:00 -04002480/**
James Smart3621a712009-04-06 18:47:14 -04002481 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04002482 * @HashResultPointer: pointer to an initial/result hash table.
2483 * @HashWorkingPointer: pointer to an working hash table.
2484 *
2485 * This routine iterates an initial hash table pointed by @HashResultPointer
2486 * with the values from the working hash table pointeed by @HashWorkingPointer.
2487 * The results are putting back to the initial hash table, returned through
2488 * the @HashResultPointer as the result hash table.
2489 **/
dea31012005-04-17 16:05:31 -05002490static void
2491lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2492{
2493 int t;
2494 uint32_t TEMP;
2495 uint32_t A, B, C, D, E;
2496 t = 16;
2497 do {
2498 HashWorkingPointer[t] =
2499 S(1,
2500 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2501 8] ^
2502 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2503 } while (++t <= 79);
2504 t = 0;
2505 A = HashResultPointer[0];
2506 B = HashResultPointer[1];
2507 C = HashResultPointer[2];
2508 D = HashResultPointer[3];
2509 E = HashResultPointer[4];
2510
2511 do {
2512 if (t < 20) {
2513 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2514 } else if (t < 40) {
2515 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2516 } else if (t < 60) {
2517 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2518 } else {
2519 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2520 }
2521 TEMP += S(5, A) + E + HashWorkingPointer[t];
2522 E = D;
2523 D = C;
2524 C = S(30, B);
2525 B = A;
2526 A = TEMP;
2527 } while (++t <= 79);
2528
2529 HashResultPointer[0] += A;
2530 HashResultPointer[1] += B;
2531 HashResultPointer[2] += C;
2532 HashResultPointer[3] += D;
2533 HashResultPointer[4] += E;
2534
2535}
2536
James Smarte59058c2008-08-24 21:49:00 -04002537/**
James Smart3621a712009-04-06 18:47:14 -04002538 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04002539 * @RandomChallenge: pointer to the entry of host challenge random number array.
2540 * @HashWorking: pointer to the entry of the working hash array.
2541 *
2542 * This routine calculates the working hash array referred by @HashWorking
2543 * from the challenge random numbers associated with the host, referred by
2544 * @RandomChallenge. The result is put into the entry of the working hash
2545 * array and returned by reference through @HashWorking.
2546 **/
dea31012005-04-17 16:05:31 -05002547static void
2548lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2549{
2550 *HashWorking = (*RandomChallenge ^ *HashWorking);
2551}
2552
James Smarte59058c2008-08-24 21:49:00 -04002553/**
James Smart3621a712009-04-06 18:47:14 -04002554 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04002555 * @phba: pointer to lpfc hba data structure.
2556 * @hbainit: pointer to an array of unsigned 32-bit integers.
2557 *
2558 * This routine performs the special handling for LC HBA initialization.
2559 **/
dea31012005-04-17 16:05:31 -05002560void
2561lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2562{
2563 int t;
2564 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05002565 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05002566
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002567 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002568 if (!HashWorking)
2569 return;
2570
dea31012005-04-17 16:05:31 -05002571 HashWorking[0] = HashWorking[78] = *pwwnn++;
2572 HashWorking[1] = HashWorking[79] = *pwwnn;
2573
2574 for (t = 0; t < 7; t++)
2575 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2576
2577 lpfc_sha_init(hbainit);
2578 lpfc_sha_iterate(hbainit, HashWorking);
2579 kfree(HashWorking);
2580}
2581
James Smarte59058c2008-08-24 21:49:00 -04002582/**
James Smart3621a712009-04-06 18:47:14 -04002583 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04002584 * @vport: pointer to a virtual N_Port data structure.
2585 *
2586 * This routine performs the necessary cleanups before deleting the @vport.
2587 * It invokes the discovery state machine to perform necessary state
2588 * transitions and to release the ndlps associated with the @vport. Note,
2589 * the physical port is treated as @vport 0.
2590 **/
James Smart87af33f2007-10-27 13:37:43 -04002591void
James Smart2e0fef82007-06-17 19:56:36 -05002592lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002593{
James Smart87af33f2007-10-27 13:37:43 -04002594 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002595 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04002596 int i = 0;
dea31012005-04-17 16:05:31 -05002597
James Smart87af33f2007-10-27 13:37:43 -04002598 if (phba->link_state > LPFC_LINK_DOWN)
2599 lpfc_port_link_failure(vport);
2600
2601 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002602 if (!NLP_CHK_NODE_ACT(ndlp)) {
2603 ndlp = lpfc_enable_node(vport, ndlp,
2604 NLP_STE_UNUSED_NODE);
2605 if (!ndlp)
2606 continue;
2607 spin_lock_irq(&phba->ndlp_lock);
2608 NLP_SET_FREE_REQ(ndlp);
2609 spin_unlock_irq(&phba->ndlp_lock);
2610 /* Trigger the release of the ndlp memory */
2611 lpfc_nlp_put(ndlp);
2612 continue;
2613 }
2614 spin_lock_irq(&phba->ndlp_lock);
2615 if (NLP_CHK_FREE_REQ(ndlp)) {
2616 /* The ndlp should not be in memory free mode already */
2617 spin_unlock_irq(&phba->ndlp_lock);
2618 continue;
2619 } else
2620 /* Indicate request for freeing ndlp memory */
2621 NLP_SET_FREE_REQ(ndlp);
2622 spin_unlock_irq(&phba->ndlp_lock);
2623
James Smart58da1ff2008-04-07 10:15:56 -04002624 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2625 ndlp->nlp_DID == Fabric_DID) {
2626 /* Just free up ndlp with Fabric_DID for vports */
2627 lpfc_nlp_put(ndlp);
2628 continue;
2629 }
2630
James Smarteff4a012012-01-18 16:25:25 -05002631 /* take care of nodes in unused state before the state
2632 * machine taking action.
2633 */
2634 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
2635 lpfc_nlp_put(ndlp);
2636 continue;
2637 }
2638
James Smart87af33f2007-10-27 13:37:43 -04002639 if (ndlp->nlp_type & NLP_FABRIC)
2640 lpfc_disc_state_machine(vport, ndlp, NULL,
2641 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002642
James Smart87af33f2007-10-27 13:37:43 -04002643 lpfc_disc_state_machine(vport, ndlp, NULL,
2644 NLP_EVT_DEVICE_RM);
2645 }
2646
James Smarta8adb832007-10-27 13:37:53 -04002647 /* At this point, ALL ndlp's should be gone
2648 * because of the previous NLP_EVT_DEVICE_RM.
2649 * Lets wait for this to happen, if needed.
2650 */
James Smart87af33f2007-10-27 13:37:43 -04002651 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002652 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002653 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002654 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002655 list_for_each_entry_safe(ndlp, next_ndlp,
2656 &vport->fc_nodes, nlp_listp) {
2657 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2658 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002659 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002660 "usgmap:x%x refcnt:%d\n",
2661 ndlp->nlp_DID, (void *)ndlp,
2662 ndlp->nlp_usg_map,
2663 atomic_read(
2664 &ndlp->kref.refcount));
2665 }
James Smarta8adb832007-10-27 13:37:53 -04002666 break;
James Smart87af33f2007-10-27 13:37:43 -04002667 }
James Smarta8adb832007-10-27 13:37:53 -04002668
2669 /* Wait for any activity on ndlps to settle */
2670 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002671 }
James Smart1151e3e2011-02-16 12:39:35 -05002672 lpfc_cleanup_vports_rrqs(vport, NULL);
dea31012005-04-17 16:05:31 -05002673}
2674
James Smarte59058c2008-08-24 21:49:00 -04002675/**
James Smart3621a712009-04-06 18:47:14 -04002676 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002677 * @vport: pointer to a virtual N_Port data structure.
2678 *
2679 * This routine stops all the timers associated with a @vport. This function
2680 * is invoked before disabling or deleting a @vport. Note that the physical
2681 * port is treated as @vport 0.
2682 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002683void
2684lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002685{
James Smart92d7f7b2007-06-17 19:56:38 -05002686 del_timer_sync(&vport->els_tmofunc);
James Smart92494142011-02-16 12:39:44 -05002687 del_timer_sync(&vport->delayed_disc_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002688 lpfc_can_disctmo(vport);
2689 return;
dea31012005-04-17 16:05:31 -05002690}
2691
James Smarte59058c2008-08-24 21:49:00 -04002692/**
James Smartecfd03c2010-02-12 14:41:27 -05002693 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2694 * @phba: pointer to lpfc hba data structure.
2695 *
2696 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2697 * caller of this routine should already hold the host lock.
2698 **/
2699void
2700__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2701{
James Smart5ac6b302010-10-22 11:05:36 -04002702 /* Clear pending FCF rediscovery wait flag */
2703 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2704
James Smartecfd03c2010-02-12 14:41:27 -05002705 /* Now, try to stop the timer */
2706 del_timer(&phba->fcf.redisc_wait);
2707}
2708
2709/**
2710 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2711 * @phba: pointer to lpfc hba data structure.
2712 *
2713 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2714 * checks whether the FCF rediscovery wait timer is pending with the host
2715 * lock held before proceeding with disabling the timer and clearing the
2716 * wait timer pendig flag.
2717 **/
2718void
2719lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2720{
2721 spin_lock_irq(&phba->hbalock);
2722 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2723 /* FCF rediscovery timer already fired or stopped */
2724 spin_unlock_irq(&phba->hbalock);
2725 return;
2726 }
2727 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart5ac6b302010-10-22 11:05:36 -04002728 /* Clear failover in progress flags */
2729 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
James Smartecfd03c2010-02-12 14:41:27 -05002730 spin_unlock_irq(&phba->hbalock);
2731}
2732
2733/**
James Smart3772a992009-05-22 14:50:54 -04002734 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002735 * @phba: pointer to lpfc hba data structure.
2736 *
2737 * This routine stops all the timers associated with a HBA. This function is
2738 * invoked before either putting a HBA offline or unloading the driver.
2739 **/
James Smart3772a992009-05-22 14:50:54 -04002740void
2741lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002742{
James Smart51ef4c22007-08-02 11:10:31 -04002743 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05002744 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002745 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002746 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002747 del_timer_sync(&phba->hb_tmofunc);
James Smart1151e3e2011-02-16 12:39:35 -05002748 if (phba->sli_rev == LPFC_SLI_REV4) {
2749 del_timer_sync(&phba->rrq_tmr);
2750 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2751 }
James Smart3772a992009-05-22 14:50:54 -04002752 phba->hb_outstanding = 0;
2753
2754 switch (phba->pci_dev_grp) {
2755 case LPFC_PCI_DEV_LP:
2756 /* Stop any LightPulse device specific driver timers */
2757 del_timer_sync(&phba->fcp_poll_timer);
2758 break;
2759 case LPFC_PCI_DEV_OC:
2760 /* Stop any OneConnect device sepcific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05002761 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04002762 break;
2763 default:
2764 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2765 "0297 Invalid device group (x%x)\n",
2766 phba->pci_dev_grp);
2767 break;
2768 }
James Smart2e0fef82007-06-17 19:56:36 -05002769 return;
dea31012005-04-17 16:05:31 -05002770}
2771
James Smarte59058c2008-08-24 21:49:00 -04002772/**
James Smart3621a712009-04-06 18:47:14 -04002773 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002774 * @phba: pointer to lpfc hba data structure.
2775 *
2776 * This routine marks a HBA's management interface as blocked. Once the HBA's
2777 * management interface is marked as blocked, all the user space access to
2778 * the HBA, whether they are from sysfs interface or libdfc interface will
2779 * all be blocked. The HBA is set to block the management interface when the
2780 * driver prepares the HBA interface for online or offline.
2781 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002782static void
James Smart618a5232012-06-12 13:54:36 -04002783lpfc_block_mgmt_io(struct lpfc_hba *phba, int mbx_action)
Adrian Bunka6ababd2007-11-05 18:07:33 +01002784{
2785 unsigned long iflag;
James Smart6e7288d2010-06-07 15:23:35 -04002786 uint8_t actcmd = MBX_HEARTBEAT;
2787 unsigned long timeout;
2788
Adrian Bunka6ababd2007-11-05 18:07:33 +01002789 spin_lock_irqsave(&phba->hbalock, iflag);
2790 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
James Smart618a5232012-06-12 13:54:36 -04002791 spin_unlock_irqrestore(&phba->hbalock, iflag);
2792 if (mbx_action == LPFC_MBX_NO_WAIT)
2793 return;
2794 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
2795 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002796 if (phba->sli.mbox_active) {
James Smart6e7288d2010-06-07 15:23:35 -04002797 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
James Smarta183a152011-10-10 21:32:43 -04002798 /* Determine how long we might wait for the active mailbox
2799 * command to be gracefully completed by firmware.
2800 */
2801 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
2802 phba->sli.mbox_active) * 1000) + jiffies;
2803 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002804 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002805
James Smart6e7288d2010-06-07 15:23:35 -04002806 /* Wait for the outstnading mailbox command to complete */
2807 while (phba->sli.mbox_active) {
2808 /* Check active mailbox complete status every 2ms */
2809 msleep(2);
2810 if (time_after(jiffies, timeout)) {
2811 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2812 "2813 Mgmt IO is Blocked %x "
2813 "- mbox cmd %x still active\n",
2814 phba->sli.sli_flag, actcmd);
2815 break;
2816 }
2817 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002818}
2819
James Smarte59058c2008-08-24 21:49:00 -04002820/**
James Smart6b5151f2012-01-18 16:24:06 -05002821 * lpfc_sli4_node_prep - Assign RPIs for active nodes.
2822 * @phba: pointer to lpfc hba data structure.
2823 *
2824 * Allocate RPIs for all active remote nodes. This is needed whenever
2825 * an SLI4 adapter is reset and the driver is not unloading. Its purpose
2826 * is to fixup the temporary rpi assignments.
2827 **/
2828void
2829lpfc_sli4_node_prep(struct lpfc_hba *phba)
2830{
2831 struct lpfc_nodelist *ndlp, *next_ndlp;
2832 struct lpfc_vport **vports;
2833 int i;
2834
2835 if (phba->sli_rev != LPFC_SLI_REV4)
2836 return;
2837
2838 vports = lpfc_create_vport_work_array(phba);
2839 if (vports != NULL) {
2840 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2841 if (vports[i]->load_flag & FC_UNLOADING)
2842 continue;
2843
2844 list_for_each_entry_safe(ndlp, next_ndlp,
2845 &vports[i]->fc_nodes,
2846 nlp_listp) {
James Smartbe6bb942015-04-07 15:07:22 -04002847 if (NLP_CHK_NODE_ACT(ndlp)) {
James Smart6b5151f2012-01-18 16:24:06 -05002848 ndlp->nlp_rpi =
2849 lpfc_sli4_alloc_rpi(phba);
James Smartbe6bb942015-04-07 15:07:22 -04002850 lpfc_printf_vlog(ndlp->vport, KERN_INFO,
2851 LOG_NODE,
2852 "0009 rpi:%x DID:%x "
2853 "flg:%x map:%x %p\n",
2854 ndlp->nlp_rpi,
2855 ndlp->nlp_DID,
2856 ndlp->nlp_flag,
2857 ndlp->nlp_usg_map,
2858 ndlp);
2859 }
James Smart6b5151f2012-01-18 16:24:06 -05002860 }
2861 }
2862 }
2863 lpfc_destroy_vport_work_array(phba, vports);
2864}
2865
2866/**
James Smart3621a712009-04-06 18:47:14 -04002867 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04002868 * @phba: pointer to lpfc hba data structure.
2869 *
2870 * This routine initializes the HBA and brings a HBA online. During this
2871 * process, the management interface is blocked to prevent user space access
2872 * to the HBA interfering with the driver initialization.
2873 *
2874 * Return codes
2875 * 0 - successful
2876 * 1 - failed
2877 **/
dea31012005-04-17 16:05:31 -05002878int
James Smart2e0fef82007-06-17 19:56:36 -05002879lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002880{
Julia Lawall372bd282008-12-16 16:15:08 +01002881 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04002882 struct lpfc_vport **vports;
2883 int i;
James Smart16a3a202013-04-17 20:14:38 -04002884 bool vpis_cleared = false;
James Smart2e0fef82007-06-17 19:56:36 -05002885
dea31012005-04-17 16:05:31 -05002886 if (!phba)
2887 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01002888 vport = phba->pport;
dea31012005-04-17 16:05:31 -05002889
James Smart2e0fef82007-06-17 19:56:36 -05002890 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05002891 return 0;
2892
James Smarted957682007-06-17 19:56:37 -05002893 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002894 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05002895
James Smart618a5232012-06-12 13:54:36 -04002896 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05002897
James Smart46fa3112007-04-25 09:51:45 -04002898 if (!lpfc_sli_queue_setup(phba)) {
2899 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002900 return 1;
James Smart46fa3112007-04-25 09:51:45 -04002901 }
2902
James Smartda0436e2009-05-22 14:51:39 -04002903 if (phba->sli_rev == LPFC_SLI_REV4) {
2904 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2905 lpfc_unblock_mgmt_io(phba);
2906 return 1;
2907 }
James Smart16a3a202013-04-17 20:14:38 -04002908 spin_lock_irq(&phba->hbalock);
2909 if (!phba->sli4_hba.max_cfg_param.vpi_used)
2910 vpis_cleared = true;
2911 spin_unlock_irq(&phba->hbalock);
James Smartda0436e2009-05-22 14:51:39 -04002912 } else {
2913 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2914 lpfc_unblock_mgmt_io(phba);
2915 return 1;
2916 }
James Smart46fa3112007-04-25 09:51:45 -04002917 }
dea31012005-04-17 16:05:31 -05002918
James Smart549e55c2007-08-02 11:09:51 -04002919 vports = lpfc_create_vport_work_array(phba);
Arnd Bergmannaeb66412016-03-14 15:29:44 +01002920 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04002921 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002922 struct Scsi_Host *shost;
2923 shost = lpfc_shost_from_vport(vports[i]);
2924 spin_lock_irq(shost->host_lock);
2925 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2926 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2927 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart16a3a202013-04-17 20:14:38 -04002928 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart1c6834a2009-07-19 10:01:26 -04002929 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart16a3a202013-04-17 20:14:38 -04002930 if ((vpis_cleared) &&
2931 (vports[i]->port_type !=
2932 LPFC_PHYSICAL_PORT))
2933 vports[i]->vpi = 0;
2934 }
James Smart549e55c2007-08-02 11:09:51 -04002935 spin_unlock_irq(shost->host_lock);
2936 }
Arnd Bergmannaeb66412016-03-14 15:29:44 +01002937 }
2938 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002939
James Smart46fa3112007-04-25 09:51:45 -04002940 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002941 return 0;
2942}
2943
James Smarte59058c2008-08-24 21:49:00 -04002944/**
James Smart3621a712009-04-06 18:47:14 -04002945 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04002946 * @phba: pointer to lpfc hba data structure.
2947 *
2948 * This routine marks a HBA's management interface as not blocked. Once the
2949 * HBA's management interface is marked as not blocked, all the user space
2950 * access to the HBA, whether they are from sysfs interface or libdfc
2951 * interface will be allowed. The HBA is set to block the management interface
2952 * when the driver prepares the HBA interface for online or offline and then
2953 * set to unblock the management interface afterwards.
2954 **/
James Smart46fa3112007-04-25 09:51:45 -04002955void
James Smart46fa3112007-04-25 09:51:45 -04002956lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2957{
2958 unsigned long iflag;
2959
James Smart2e0fef82007-06-17 19:56:36 -05002960 spin_lock_irqsave(&phba->hbalock, iflag);
2961 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2962 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04002963}
2964
James Smarte59058c2008-08-24 21:49:00 -04002965/**
James Smart3621a712009-04-06 18:47:14 -04002966 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04002967 * @phba: pointer to lpfc hba data structure.
2968 *
2969 * This routine is invoked to prepare a HBA to be brought offline. It performs
2970 * unregistration login to all the nodes on all vports and flushes the mailbox
2971 * queue to make it ready to be brought offline.
2972 **/
James Smart46fa3112007-04-25 09:51:45 -04002973void
James Smart618a5232012-06-12 13:54:36 -04002974lpfc_offline_prep(struct lpfc_hba *phba, int mbx_action)
James Smart46fa3112007-04-25 09:51:45 -04002975{
James Smart2e0fef82007-06-17 19:56:36 -05002976 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04002977 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04002978 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05002979 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04002980 int i;
dea31012005-04-17 16:05:31 -05002981
James Smart2e0fef82007-06-17 19:56:36 -05002982 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002983 return;
dea31012005-04-17 16:05:31 -05002984
James Smart618a5232012-06-12 13:54:36 -04002985 lpfc_block_mgmt_io(phba, mbx_action);
dea31012005-04-17 16:05:31 -05002986
2987 lpfc_linkdown(phba);
2988
James Smart87af33f2007-10-27 13:37:43 -04002989 /* Issue an unreg_login to all nodes on all vports */
2990 vports = lpfc_create_vport_work_array(phba);
2991 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04002992 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04002993 if (vports[i]->load_flag & FC_UNLOADING)
2994 continue;
James Smart72100cc2010-02-12 14:43:01 -05002995 shost = lpfc_shost_from_vport(vports[i]);
2996 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002997 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002998 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2999 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05003000 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05003001
James Smart87af33f2007-10-27 13:37:43 -04003002 shost = lpfc_shost_from_vport(vports[i]);
3003 list_for_each_entry_safe(ndlp, next_ndlp,
3004 &vports[i]->fc_nodes,
3005 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05003006 if (!NLP_CHK_NODE_ACT(ndlp))
3007 continue;
James Smart87af33f2007-10-27 13:37:43 -04003008 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
3009 continue;
3010 if (ndlp->nlp_type & NLP_FABRIC) {
3011 lpfc_disc_state_machine(vports[i], ndlp,
3012 NULL, NLP_EVT_DEVICE_RECOVERY);
3013 lpfc_disc_state_machine(vports[i], ndlp,
3014 NULL, NLP_EVT_DEVICE_RM);
3015 }
3016 spin_lock_irq(shost->host_lock);
3017 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart401ee0c2012-03-01 22:35:34 -05003018 spin_unlock_irq(shost->host_lock);
James Smart6b5151f2012-01-18 16:24:06 -05003019 /*
3020 * Whenever an SLI4 port goes offline, free the
James Smart401ee0c2012-03-01 22:35:34 -05003021 * RPI. Get a new RPI when the adapter port
3022 * comes back online.
James Smart6b5151f2012-01-18 16:24:06 -05003023 */
James Smartbe6bb942015-04-07 15:07:22 -04003024 if (phba->sli_rev == LPFC_SLI_REV4) {
3025 lpfc_printf_vlog(ndlp->vport,
3026 KERN_INFO, LOG_NODE,
3027 "0011 lpfc_offline: "
3028 "ndlp:x%p did %x "
3029 "usgmap:x%x rpi:%x\n",
3030 ndlp, ndlp->nlp_DID,
3031 ndlp->nlp_usg_map,
3032 ndlp->nlp_rpi);
3033
James Smart6b5151f2012-01-18 16:24:06 -05003034 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smartbe6bb942015-04-07 15:07:22 -04003035 }
James Smart87af33f2007-10-27 13:37:43 -04003036 lpfc_unreg_rpi(vports[i], ndlp);
3037 }
3038 }
3039 }
James Smart09372822008-01-11 01:52:54 -05003040 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05003041
James Smart618a5232012-06-12 13:54:36 -04003042 lpfc_sli_mbox_sys_shutdown(phba, mbx_action);
James Smart46fa3112007-04-25 09:51:45 -04003043}
3044
James Smarte59058c2008-08-24 21:49:00 -04003045/**
James Smart3621a712009-04-06 18:47:14 -04003046 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04003047 * @phba: pointer to lpfc hba data structure.
3048 *
3049 * This routine actually brings a HBA offline. It stops all the timers
3050 * associated with the HBA, brings down the SLI layer, and eventually
3051 * marks the HBA as in offline state for the upper layer protocol.
3052 **/
James Smart46fa3112007-04-25 09:51:45 -04003053void
James Smart2e0fef82007-06-17 19:56:36 -05003054lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04003055{
James Smart549e55c2007-08-02 11:09:51 -04003056 struct Scsi_Host *shost;
3057 struct lpfc_vport **vports;
3058 int i;
James Smart46fa3112007-04-25 09:51:45 -04003059
James Smart549e55c2007-08-02 11:09:51 -04003060 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04003061 return;
James Smart688a8862006-07-06 15:49:56 -04003062
James Smartda0436e2009-05-22 14:51:39 -04003063 /* stop port and all timers associated with this hba */
3064 lpfc_stop_port(phba);
James Smart51ef4c22007-08-02 11:10:31 -04003065 vports = lpfc_create_vport_work_array(phba);
3066 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04003067 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04003068 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05003069 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05003070 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003071 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05003072 /* Bring down the SLI Layer and cleanup. The HBA is offline
3073 now. */
3074 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003075 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04003076 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003077 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04003078 vports = lpfc_create_vport_work_array(phba);
3079 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04003080 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04003081 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04003082 spin_lock_irq(shost->host_lock);
3083 vports[i]->work_port_events = 0;
3084 vports[i]->fc_flag |= FC_OFFLINE_MODE;
3085 spin_unlock_irq(shost->host_lock);
3086 }
James Smart09372822008-01-11 01:52:54 -05003087 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05003088}
3089
James Smarte59058c2008-08-24 21:49:00 -04003090/**
James Smart3621a712009-04-06 18:47:14 -04003091 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04003092 * @phba: pointer to lpfc hba data structure.
3093 *
3094 * This routine is to free all the SCSI buffers and IOCBs from the driver
3095 * list back to kernel. It is called from lpfc_pci_remove_one to free
3096 * the internal resources before the device is removed from the system.
James Smarte59058c2008-08-24 21:49:00 -04003097 **/
James Smart8a9d2e82012-05-09 21:16:12 -04003098static void
James Smart2e0fef82007-06-17 19:56:36 -05003099lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003100{
3101 struct lpfc_scsi_buf *sb, *sb_next;
3102 struct lpfc_iocbq *io, *io_next;
3103
James Smart2e0fef82007-06-17 19:56:36 -05003104 spin_lock_irq(&phba->hbalock);
James Smarta40fc5f2013-04-17 20:17:40 -04003105
dea31012005-04-17 16:05:31 -05003106 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smarta40fc5f2013-04-17 20:17:40 -04003107
3108 spin_lock(&phba->scsi_buf_list_put_lock);
3109 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_put,
3110 list) {
dea31012005-04-17 16:05:31 -05003111 list_del(&sb->list);
3112 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05003113 sb->dma_handle);
dea31012005-04-17 16:05:31 -05003114 kfree(sb);
3115 phba->total_scsi_bufs--;
3116 }
James Smarta40fc5f2013-04-17 20:17:40 -04003117 spin_unlock(&phba->scsi_buf_list_put_lock);
3118
3119 spin_lock(&phba->scsi_buf_list_get_lock);
3120 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_get,
3121 list) {
3122 list_del(&sb->list);
3123 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
3124 sb->dma_handle);
3125 kfree(sb);
3126 phba->total_scsi_bufs--;
3127 }
3128 spin_unlock(&phba->scsi_buf_list_get_lock);
dea31012005-04-17 16:05:31 -05003129
3130 /* Release all the lpfc_iocbq entries maintained by this host. */
3131 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
3132 list_del(&io->list);
3133 kfree(io);
3134 phba->total_iocbq_bufs--;
3135 }
James Smart6d368e52011-05-24 11:44:12 -04003136
James Smart2e0fef82007-06-17 19:56:36 -05003137 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003138}
3139
3140/**
3141 * lpfc_sli4_xri_sgl_update - update xri-sgl sizing and mapping
3142 * @phba: pointer to lpfc hba data structure.
3143 *
3144 * This routine first calculates the sizes of the current els and allocated
3145 * scsi sgl lists, and then goes through all sgls to updates the physical
3146 * XRIs assigned due to port function reset. During port initialization, the
3147 * current els and allocated scsi sgl lists are 0s.
3148 *
3149 * Return codes
3150 * 0 - successful (for now, it always returns 0)
3151 **/
3152int
3153lpfc_sli4_xri_sgl_update(struct lpfc_hba *phba)
3154{
3155 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
3156 struct lpfc_scsi_buf *psb = NULL, *psb_next = NULL;
3157 uint16_t i, lxri, xri_cnt, els_xri_cnt, scsi_xri_cnt;
3158 LIST_HEAD(els_sgl_list);
3159 LIST_HEAD(scsi_sgl_list);
3160 int rc;
James Smartdafe8ce2014-09-03 12:56:40 -04003161 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart8a9d2e82012-05-09 21:16:12 -04003162
3163 /*
3164 * update on pci function's els xri-sgl list
3165 */
3166 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3167 if (els_xri_cnt > phba->sli4_hba.els_xri_cnt) {
3168 /* els xri-sgl expanded */
3169 xri_cnt = els_xri_cnt - phba->sli4_hba.els_xri_cnt;
3170 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3171 "3157 ELS xri-sgl count increased from "
3172 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3173 els_xri_cnt);
3174 /* allocate the additional els sgls */
3175 for (i = 0; i < xri_cnt; i++) {
3176 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3177 GFP_KERNEL);
3178 if (sglq_entry == NULL) {
3179 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3180 "2562 Failure to allocate an "
3181 "ELS sgl entry:%d\n", i);
3182 rc = -ENOMEM;
3183 goto out_free_mem;
3184 }
3185 sglq_entry->buff_type = GEN_BUFF_TYPE;
3186 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0,
3187 &sglq_entry->phys);
3188 if (sglq_entry->virt == NULL) {
3189 kfree(sglq_entry);
3190 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3191 "2563 Failure to allocate an "
3192 "ELS mbuf:%d\n", i);
3193 rc = -ENOMEM;
3194 goto out_free_mem;
3195 }
3196 sglq_entry->sgl = sglq_entry->virt;
3197 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
3198 sglq_entry->state = SGL_FREED;
3199 list_add_tail(&sglq_entry->list, &els_sgl_list);
3200 }
James Smart38c20672013-03-01 16:37:44 -05003201 spin_lock_irq(&phba->hbalock);
James Smartdafe8ce2014-09-03 12:56:40 -04003202 spin_lock(&pring->ring_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003203 list_splice_init(&els_sgl_list, &phba->sli4_hba.lpfc_sgl_list);
James Smartdafe8ce2014-09-03 12:56:40 -04003204 spin_unlock(&pring->ring_lock);
James Smart38c20672013-03-01 16:37:44 -05003205 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003206 } else if (els_xri_cnt < phba->sli4_hba.els_xri_cnt) {
3207 /* els xri-sgl shrinked */
3208 xri_cnt = phba->sli4_hba.els_xri_cnt - els_xri_cnt;
3209 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3210 "3158 ELS xri-sgl count decreased from "
3211 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3212 els_xri_cnt);
3213 spin_lock_irq(&phba->hbalock);
James Smartdafe8ce2014-09-03 12:56:40 -04003214 spin_lock(&pring->ring_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003215 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &els_sgl_list);
James Smartdafe8ce2014-09-03 12:56:40 -04003216 spin_unlock(&pring->ring_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003217 spin_unlock_irq(&phba->hbalock);
3218 /* release extra els sgls from list */
3219 for (i = 0; i < xri_cnt; i++) {
3220 list_remove_head(&els_sgl_list,
3221 sglq_entry, struct lpfc_sglq, list);
3222 if (sglq_entry) {
3223 lpfc_mbuf_free(phba, sglq_entry->virt,
3224 sglq_entry->phys);
3225 kfree(sglq_entry);
3226 }
3227 }
3228 spin_lock_irq(&phba->hbalock);
James Smartdafe8ce2014-09-03 12:56:40 -04003229 spin_lock(&pring->ring_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003230 list_splice_init(&els_sgl_list, &phba->sli4_hba.lpfc_sgl_list);
James Smartdafe8ce2014-09-03 12:56:40 -04003231 spin_unlock(&pring->ring_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003232 spin_unlock_irq(&phba->hbalock);
3233 } else
3234 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3235 "3163 ELS xri-sgl count unchanged: %d\n",
3236 els_xri_cnt);
3237 phba->sli4_hba.els_xri_cnt = els_xri_cnt;
3238
3239 /* update xris to els sgls on the list */
3240 sglq_entry = NULL;
3241 sglq_entry_next = NULL;
3242 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
3243 &phba->sli4_hba.lpfc_sgl_list, list) {
3244 lxri = lpfc_sli4_next_xritag(phba);
3245 if (lxri == NO_XRI) {
3246 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3247 "2400 Failed to allocate xri for "
3248 "ELS sgl\n");
3249 rc = -ENOMEM;
3250 goto out_free_mem;
3251 }
3252 sglq_entry->sli4_lxritag = lxri;
3253 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3254 }
3255
3256 /*
3257 * update on pci function's allocated scsi xri-sgl list
3258 */
3259 phba->total_scsi_bufs = 0;
3260
3261 /* maximum number of xris available for scsi buffers */
3262 phba->sli4_hba.scsi_xri_max = phba->sli4_hba.max_cfg_param.max_xri -
3263 els_xri_cnt;
3264
3265 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3266 "2401 Current allocated SCSI xri-sgl count:%d, "
3267 "maximum SCSI xri count:%d\n",
3268 phba->sli4_hba.scsi_xri_cnt,
3269 phba->sli4_hba.scsi_xri_max);
3270
James Smarta40fc5f2013-04-17 20:17:40 -04003271 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart164cecd2013-09-06 12:22:38 -04003272 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003273 list_splice_init(&phba->lpfc_scsi_buf_list_get, &scsi_sgl_list);
3274 list_splice(&phba->lpfc_scsi_buf_list_put, &scsi_sgl_list);
James Smart164cecd2013-09-06 12:22:38 -04003275 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003276 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003277
3278 if (phba->sli4_hba.scsi_xri_cnt > phba->sli4_hba.scsi_xri_max) {
3279 /* max scsi xri shrinked below the allocated scsi buffers */
3280 scsi_xri_cnt = phba->sli4_hba.scsi_xri_cnt -
3281 phba->sli4_hba.scsi_xri_max;
3282 /* release the extra allocated scsi buffers */
3283 for (i = 0; i < scsi_xri_cnt; i++) {
3284 list_remove_head(&scsi_sgl_list, psb,
3285 struct lpfc_scsi_buf, list);
James Smarta2fc4aef2014-09-03 12:57:55 -04003286 if (psb) {
3287 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
3288 psb->data, psb->dma_handle);
3289 kfree(psb);
3290 }
James Smart8a9d2e82012-05-09 21:16:12 -04003291 }
James Smarta40fc5f2013-04-17 20:17:40 -04003292 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003293 phba->sli4_hba.scsi_xri_cnt -= scsi_xri_cnt;
James Smarta40fc5f2013-04-17 20:17:40 -04003294 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003295 }
3296
3297 /* update xris associated to remaining allocated scsi buffers */
3298 psb = NULL;
3299 psb_next = NULL;
3300 list_for_each_entry_safe(psb, psb_next, &scsi_sgl_list, list) {
3301 lxri = lpfc_sli4_next_xritag(phba);
3302 if (lxri == NO_XRI) {
3303 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3304 "2560 Failed to allocate xri for "
3305 "scsi buffer\n");
3306 rc = -ENOMEM;
3307 goto out_free_mem;
3308 }
3309 psb->cur_iocbq.sli4_lxritag = lxri;
3310 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3311 }
James Smarta40fc5f2013-04-17 20:17:40 -04003312 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart164cecd2013-09-06 12:22:38 -04003313 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003314 list_splice_init(&scsi_sgl_list, &phba->lpfc_scsi_buf_list_get);
3315 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
James Smart164cecd2013-09-06 12:22:38 -04003316 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003317 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003318
dea31012005-04-17 16:05:31 -05003319 return 0;
James Smart8a9d2e82012-05-09 21:16:12 -04003320
3321out_free_mem:
3322 lpfc_free_els_sgl_list(phba);
3323 lpfc_scsi_free(phba);
3324 return rc;
dea31012005-04-17 16:05:31 -05003325}
3326
James Smarte59058c2008-08-24 21:49:00 -04003327/**
James Smart3621a712009-04-06 18:47:14 -04003328 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04003329 * @phba: pointer to lpfc hba data structure.
3330 * @instance: a unique integer ID to this FC port.
3331 * @dev: pointer to the device data structure.
3332 *
3333 * This routine creates a FC port for the upper layer protocol. The FC port
3334 * can be created on top of either a physical port or a virtual port provided
3335 * by the HBA. This routine also allocates a SCSI host data structure (shost)
3336 * and associates the FC port created before adding the shost into the SCSI
3337 * layer.
3338 *
3339 * Return codes
3340 * @vport - pointer to the virtual N_Port data structure.
3341 * NULL - port create failed.
3342 **/
James Smart2e0fef82007-06-17 19:56:36 -05003343struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04003344lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04003345{
James Smart2e0fef82007-06-17 19:56:36 -05003346 struct lpfc_vport *vport;
3347 struct Scsi_Host *shost;
3348 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04003349
James Smartea4142f2015-04-07 15:07:13 -04003350 if (dev != &phba->pcidev->dev) {
James Smart3de2a652007-08-02 11:09:59 -04003351 shost = scsi_host_alloc(&lpfc_vport_template,
3352 sizeof(struct lpfc_vport));
James Smartea4142f2015-04-07 15:07:13 -04003353 } else {
3354 if (phba->sli_rev == LPFC_SLI_REV4)
3355 shost = scsi_host_alloc(&lpfc_template,
James Smart3de2a652007-08-02 11:09:59 -04003356 sizeof(struct lpfc_vport));
James Smartea4142f2015-04-07 15:07:13 -04003357 else
3358 shost = scsi_host_alloc(&lpfc_template_s3,
3359 sizeof(struct lpfc_vport));
3360 }
James Smart2e0fef82007-06-17 19:56:36 -05003361 if (!shost)
3362 goto out;
James Smart47a86172007-04-25 09:53:22 -04003363
James Smart2e0fef82007-06-17 19:56:36 -05003364 vport = (struct lpfc_vport *) shost->hostdata;
3365 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05003366 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05003367 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05003368 vport->fc_rscn_flush = 0;
James Smart47a86172007-04-25 09:53:22 -04003369
James Smart3de2a652007-08-02 11:09:59 -04003370 lpfc_get_vport_cfgparam(vport);
James Smart2e0fef82007-06-17 19:56:36 -05003371 shost->unique_id = instance;
3372 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04003373 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05003374 shost->this_id = -1;
3375 shost->max_cmd_len = 16;
James Smart8b0dff12015-05-22 10:42:38 -04003376 shost->nr_hw_queues = phba->cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04003377 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart28baac72010-02-12 14:42:03 -05003378 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04003379 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smartda0436e2009-05-22 14:51:39 -04003380 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
3381 }
James Smart81301a92008-12-04 22:39:46 -05003382
James Smart47a86172007-04-25 09:53:22 -04003383 /*
James Smart2e0fef82007-06-17 19:56:36 -05003384 * Set initial can_queue value since 0 is no longer supported and
3385 * scsi_add_host will fail. This will be adjusted later based on the
3386 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04003387 */
James Smart2e0fef82007-06-17 19:56:36 -05003388 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04003389 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05003390 shost->transportt = lpfc_vport_transport_template;
3391 vport->port_type = LPFC_NPIV_PORT;
3392 } else {
3393 shost->transportt = lpfc_transport_template;
3394 vport->port_type = LPFC_PHYSICAL_PORT;
3395 }
James Smart47a86172007-04-25 09:53:22 -04003396
James Smart2e0fef82007-06-17 19:56:36 -05003397 /* Initialize all internally managed lists. */
3398 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04003399 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05003400 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04003401
James Smart2e0fef82007-06-17 19:56:36 -05003402 init_timer(&vport->fc_disctmo);
3403 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05003404 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04003405
James Smart2e0fef82007-06-17 19:56:36 -05003406 init_timer(&vport->els_tmofunc);
3407 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05003408 vport->els_tmofunc.data = (unsigned long)vport;
James Smart92494142011-02-16 12:39:44 -05003409
3410 init_timer(&vport->delayed_disc_tmo);
3411 vport->delayed_disc_tmo.function = lpfc_delayed_disc_tmo;
3412 vport->delayed_disc_tmo.data = (unsigned long)vport;
3413
James Bottomleyd139b9b2009-11-05 13:33:12 -06003414 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05003415 if (error)
3416 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04003417
James Smart549e55c2007-08-02 11:09:51 -04003418 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003419 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04003420 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003421 return vport;
James Smart47a86172007-04-25 09:53:22 -04003422
James Smart2e0fef82007-06-17 19:56:36 -05003423out_put_shost:
3424 scsi_host_put(shost);
3425out:
3426 return NULL;
James Smart47a86172007-04-25 09:53:22 -04003427}
3428
James Smarte59058c2008-08-24 21:49:00 -04003429/**
James Smart3621a712009-04-06 18:47:14 -04003430 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04003431 * @vport: pointer to an lpfc virtual N_Port data structure.
3432 *
3433 * This routine destroys a FC port from the upper layer protocol. All the
3434 * resources associated with the port are released.
3435 **/
James Smart2e0fef82007-06-17 19:56:36 -05003436void
3437destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04003438{
James Smart92d7f7b2007-06-17 19:56:38 -05003439 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3440 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003441
James Smart858c9f62007-06-17 19:56:39 -05003442 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05003443 fc_remove_host(shost);
3444 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04003445
James Smart92d7f7b2007-06-17 19:56:38 -05003446 spin_lock_irq(&phba->hbalock);
3447 list_del_init(&vport->listentry);
3448 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04003449
James Smart92d7f7b2007-06-17 19:56:38 -05003450 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04003451 return;
James Smart47a86172007-04-25 09:53:22 -04003452}
3453
James Smarte59058c2008-08-24 21:49:00 -04003454/**
James Smart3621a712009-04-06 18:47:14 -04003455 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04003456 *
3457 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
3458 * uses the kernel idr facility to perform the task.
3459 *
3460 * Return codes:
3461 * instance - a unique integer ID allocated as the new instance.
3462 * -1 - lpfc get instance failed.
3463 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003464int
3465lpfc_get_instance(void)
3466{
Tejun Heoab516032013-02-27 17:04:44 -08003467 int ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003468
Tejun Heoab516032013-02-27 17:04:44 -08003469 ret = idr_alloc(&lpfc_hba_index, NULL, 0, 0, GFP_KERNEL);
3470 return ret < 0 ? -1 : ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003471}
3472
James Smarte59058c2008-08-24 21:49:00 -04003473/**
James Smart3621a712009-04-06 18:47:14 -04003474 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04003475 * @shost: pointer to SCSI host data structure.
3476 * @time: elapsed time of the scan in jiffies.
3477 *
3478 * This routine is called by the SCSI layer with a SCSI host to determine
3479 * whether the scan host is finished.
3480 *
3481 * Note: there is no scan_start function as adapter initialization will have
3482 * asynchronously kicked off the link initialization.
3483 *
3484 * Return codes
3485 * 0 - SCSI host scan is not over yet.
3486 * 1 - SCSI host scan is over.
3487 **/
James Smart47a86172007-04-25 09:53:22 -04003488int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
3489{
James Smart2e0fef82007-06-17 19:56:36 -05003490 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3491 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05003492 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04003493
James Smart858c9f62007-06-17 19:56:39 -05003494 spin_lock_irq(shost->host_lock);
3495
James Smart51ef4c22007-08-02 11:10:31 -04003496 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05003497 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003498 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05003499 }
James Smart256ec0d2013-04-17 20:14:58 -04003500 if (time >= msecs_to_jiffies(30 * 1000)) {
James Smart2e0fef82007-06-17 19:56:36 -05003501 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003502 "0461 Scanning longer than 30 "
3503 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003504 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003505 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003506 }
James Smart256ec0d2013-04-17 20:14:58 -04003507 if (time >= msecs_to_jiffies(15 * 1000) &&
3508 phba->link_state <= LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -05003509 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003510 "0465 Link down longer than 15 "
3511 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003512 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05003513 goto finished;
James Smart47a86172007-04-25 09:53:22 -04003514 }
3515
James Smart2e0fef82007-06-17 19:56:36 -05003516 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05003517 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003518 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05003519 goto finished;
James Smart256ec0d2013-04-17 20:14:58 -04003520 if (vport->fc_map_cnt == 0 && time < msecs_to_jiffies(2 * 1000))
James Smart858c9f62007-06-17 19:56:39 -05003521 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003522 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05003523 goto finished;
3524
3525 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003526
3527finished:
James Smart858c9f62007-06-17 19:56:39 -05003528 spin_unlock_irq(shost->host_lock);
3529 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05003530}
3531
James Smarte59058c2008-08-24 21:49:00 -04003532/**
James Smart3621a712009-04-06 18:47:14 -04003533 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04003534 * @shost: pointer to SCSI host data structure.
3535 *
3536 * This routine initializes a given SCSI host attributes on a FC port. The
3537 * SCSI host can be either on top of a physical port or a virtual port.
3538 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003539void lpfc_host_attrib_init(struct Scsi_Host *shost)
3540{
3541 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3542 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003543 /*
James Smart2e0fef82007-06-17 19:56:36 -05003544 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04003545 */
3546
James Smart2e0fef82007-06-17 19:56:36 -05003547 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
3548 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04003549 fc_host_supported_classes(shost) = FC_COS_CLASS3;
3550
3551 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003552 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003553 fc_host_supported_fc4s(shost)[2] = 1;
3554 fc_host_supported_fc4s(shost)[7] = 1;
3555
James Smart92d7f7b2007-06-17 19:56:38 -05003556 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
3557 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04003558
3559 fc_host_supported_speeds(shost) = 0;
James Smartd38dd522015-08-31 16:48:17 -04003560 if (phba->lmt & LMT_32Gb)
3561 fc_host_supported_speeds(shost) |= FC_PORTSPEED_32GBIT;
James Smart88a2cfb2011-07-22 18:36:33 -04003562 if (phba->lmt & LMT_16Gb)
3563 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
James Smart47a86172007-04-25 09:53:22 -04003564 if (phba->lmt & LMT_10Gb)
3565 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04003566 if (phba->lmt & LMT_8Gb)
3567 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04003568 if (phba->lmt & LMT_4Gb)
3569 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
3570 if (phba->lmt & LMT_2Gb)
3571 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
3572 if (phba->lmt & LMT_1Gb)
3573 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
3574
3575 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05003576 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
3577 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04003578
Mike Christie0af5d702010-09-15 16:52:31 -05003579 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
3580
James Smart47a86172007-04-25 09:53:22 -04003581 /* This value is also unchanging */
3582 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003583 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003584 fc_host_active_fc4s(shost)[2] = 1;
3585 fc_host_active_fc4s(shost)[7] = 1;
3586
James Smart92d7f7b2007-06-17 19:56:38 -05003587 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04003588 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04003589 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04003590 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04003591}
dea31012005-04-17 16:05:31 -05003592
James Smarte59058c2008-08-24 21:49:00 -04003593/**
James Smartda0436e2009-05-22 14:51:39 -04003594 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04003595 * @phba: pointer to lpfc hba data structure.
3596 *
James Smartda0436e2009-05-22 14:51:39 -04003597 * This routine is invoked to stop an SLI3 device port, it stops the device
3598 * from generating interrupts and stops the device driver's timers for the
3599 * device.
James Smarte59058c2008-08-24 21:49:00 -04003600 **/
James Smartdb2378e2008-02-08 18:49:51 -05003601static void
James Smartda0436e2009-05-22 14:51:39 -04003602lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05003603{
James Smartda0436e2009-05-22 14:51:39 -04003604 /* Clear all interrupt enable conditions */
3605 writel(0, phba->HCregaddr);
3606 readl(phba->HCregaddr); /* flush */
3607 /* Clear all pending interrupts */
3608 writel(0xffffffff, phba->HAregaddr);
3609 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04003610
James Smartda0436e2009-05-22 14:51:39 -04003611 /* Reset some HBA SLI setup states */
3612 lpfc_stop_hba_timers(phba);
3613 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05003614}
3615
James Smarte59058c2008-08-24 21:49:00 -04003616/**
James Smartda0436e2009-05-22 14:51:39 -04003617 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05003618 * @phba: pointer to lpfc hba data structure.
3619 *
James Smartda0436e2009-05-22 14:51:39 -04003620 * This routine is invoked to stop an SLI4 device port, it stops the device
3621 * from generating interrupts and stops the device driver's timers for the
3622 * device.
3623 **/
3624static void
3625lpfc_stop_port_s4(struct lpfc_hba *phba)
3626{
3627 /* Reset some HBA SLI4 setup states */
3628 lpfc_stop_hba_timers(phba);
3629 phba->pport->work_port_events = 0;
3630 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04003631}
3632
3633/**
3634 * lpfc_stop_port - Wrapper function for stopping hba port
3635 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05003636 *
James Smartda0436e2009-05-22 14:51:39 -04003637 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
3638 * the API jump table function pointer from the lpfc_hba struct.
3639 **/
3640void
3641lpfc_stop_port(struct lpfc_hba *phba)
3642{
3643 phba->lpfc_stop_port(phba);
3644}
3645
3646/**
James Smartecfd03c2010-02-12 14:41:27 -05003647 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
3648 * @phba: Pointer to hba for which this call is being executed.
3649 *
3650 * This routine starts the timer waiting for the FCF rediscovery to complete.
3651 **/
3652void
3653lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
3654{
3655 unsigned long fcf_redisc_wait_tmo =
3656 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
3657 /* Start fcf rediscovery wait period timer */
3658 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
3659 spin_lock_irq(&phba->hbalock);
3660 /* Allow action to new fcf asynchronous event */
3661 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
3662 /* Mark the FCF rediscovery pending state */
3663 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
3664 spin_unlock_irq(&phba->hbalock);
3665}
3666
3667/**
3668 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
3669 * @ptr: Map to lpfc_hba data structure pointer.
3670 *
3671 * This routine is invoked when waiting for FCF table rediscover has been
3672 * timed out. If new FCF record(s) has (have) been discovered during the
3673 * wait period, a new FCF event shall be added to the FCOE async event
3674 * list, and then worker thread shall be waked up for processing from the
3675 * worker thread context.
3676 **/
Rashika Kheriae399b222014-09-03 12:55:28 -04003677static void
James Smartecfd03c2010-02-12 14:41:27 -05003678lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
3679{
3680 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
3681
3682 /* Don't send FCF rediscovery event if timer cancelled */
3683 spin_lock_irq(&phba->hbalock);
3684 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
3685 spin_unlock_irq(&phba->hbalock);
3686 return;
3687 }
3688 /* Clear FCF rediscovery timer pending flag */
3689 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
3690 /* FCF rediscovery event to worker thread */
3691 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
3692 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003693 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04003694 "2776 FCF rediscover quiescent timer expired\n");
James Smartecfd03c2010-02-12 14:41:27 -05003695 /* wake up worker thread */
3696 lpfc_worker_wake_up(phba);
3697}
3698
3699/**
James Smartda0436e2009-05-22 14:51:39 -04003700 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
3701 * @phba: pointer to lpfc hba data structure.
3702 * @acqe_link: pointer to the async link completion queue entry.
3703 *
3704 * This routine is to parse the SLI4 link-attention link fault code and
3705 * translate it into the base driver's read link attention mailbox command
3706 * status.
3707 *
3708 * Return: Link-attention status in terms of base driver's coding.
3709 **/
3710static uint16_t
3711lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
3712 struct lpfc_acqe_link *acqe_link)
3713{
3714 uint16_t latt_fault;
3715
3716 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
3717 case LPFC_ASYNC_LINK_FAULT_NONE:
3718 case LPFC_ASYNC_LINK_FAULT_LOCAL:
3719 case LPFC_ASYNC_LINK_FAULT_REMOTE:
3720 latt_fault = 0;
3721 break;
3722 default:
3723 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3724 "0398 Invalid link fault code: x%x\n",
3725 bf_get(lpfc_acqe_link_fault, acqe_link));
3726 latt_fault = MBXERR_ERROR;
3727 break;
3728 }
3729 return latt_fault;
3730}
3731
3732/**
3733 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
3734 * @phba: pointer to lpfc hba data structure.
3735 * @acqe_link: pointer to the async link completion queue entry.
3736 *
3737 * This routine is to parse the SLI4 link attention type and translate it
3738 * into the base driver's link attention type coding.
3739 *
3740 * Return: Link attention type in terms of base driver's coding.
3741 **/
3742static uint8_t
3743lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
3744 struct lpfc_acqe_link *acqe_link)
3745{
3746 uint8_t att_type;
3747
3748 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
3749 case LPFC_ASYNC_LINK_STATUS_DOWN:
3750 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
James Smart76a95d72010-11-20 23:11:48 -05003751 att_type = LPFC_ATT_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04003752 break;
3753 case LPFC_ASYNC_LINK_STATUS_UP:
3754 /* Ignore physical link up events - wait for logical link up */
James Smart76a95d72010-11-20 23:11:48 -05003755 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003756 break;
3757 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
James Smart76a95d72010-11-20 23:11:48 -05003758 att_type = LPFC_ATT_LINK_UP;
James Smartda0436e2009-05-22 14:51:39 -04003759 break;
3760 default:
3761 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3762 "0399 Invalid link attention type: x%x\n",
3763 bf_get(lpfc_acqe_link_status, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003764 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003765 break;
3766 }
3767 return att_type;
3768}
3769
3770/**
James Smart8b68cd52012-09-29 11:32:37 -04003771 * lpfc_sli_port_speed_get - Get sli3 link speed code to link speed
3772 * @phba: pointer to lpfc hba data structure.
3773 *
3774 * This routine is to get an SLI3 FC port's link speed in Mbps.
3775 *
3776 * Return: link speed in terms of Mbps.
3777 **/
3778uint32_t
3779lpfc_sli_port_speed_get(struct lpfc_hba *phba)
3780{
3781 uint32_t link_speed;
3782
3783 if (!lpfc_is_link_up(phba))
3784 return 0;
3785
James Smarta085e872015-12-16 18:12:02 -05003786 if (phba->sli_rev <= LPFC_SLI_REV3) {
3787 switch (phba->fc_linkspeed) {
3788 case LPFC_LINK_SPEED_1GHZ:
3789 link_speed = 1000;
3790 break;
3791 case LPFC_LINK_SPEED_2GHZ:
3792 link_speed = 2000;
3793 break;
3794 case LPFC_LINK_SPEED_4GHZ:
3795 link_speed = 4000;
3796 break;
3797 case LPFC_LINK_SPEED_8GHZ:
3798 link_speed = 8000;
3799 break;
3800 case LPFC_LINK_SPEED_10GHZ:
3801 link_speed = 10000;
3802 break;
3803 case LPFC_LINK_SPEED_16GHZ:
3804 link_speed = 16000;
3805 break;
3806 default:
3807 link_speed = 0;
3808 }
3809 } else {
3810 if (phba->sli4_hba.link_state.logical_speed)
3811 link_speed =
3812 phba->sli4_hba.link_state.logical_speed;
3813 else
3814 link_speed = phba->sli4_hba.link_state.speed;
James Smart8b68cd52012-09-29 11:32:37 -04003815 }
3816 return link_speed;
3817}
3818
3819/**
3820 * lpfc_sli4_port_speed_parse - Parse async evt link speed code to link speed
3821 * @phba: pointer to lpfc hba data structure.
3822 * @evt_code: asynchronous event code.
3823 * @speed_code: asynchronous event link speed code.
3824 *
3825 * This routine is to parse the giving SLI4 async event link speed code into
3826 * value of Mbps for the link speed.
3827 *
3828 * Return: link speed in terms of Mbps.
3829 **/
3830static uint32_t
3831lpfc_sli4_port_speed_parse(struct lpfc_hba *phba, uint32_t evt_code,
3832 uint8_t speed_code)
3833{
3834 uint32_t port_speed;
3835
3836 switch (evt_code) {
3837 case LPFC_TRAILER_CODE_LINK:
3838 switch (speed_code) {
James Smart26d830e2015-04-07 15:07:17 -04003839 case LPFC_ASYNC_LINK_SPEED_ZERO:
James Smart8b68cd52012-09-29 11:32:37 -04003840 port_speed = 0;
3841 break;
James Smart26d830e2015-04-07 15:07:17 -04003842 case LPFC_ASYNC_LINK_SPEED_10MBPS:
James Smart8b68cd52012-09-29 11:32:37 -04003843 port_speed = 10;
3844 break;
James Smart26d830e2015-04-07 15:07:17 -04003845 case LPFC_ASYNC_LINK_SPEED_100MBPS:
James Smart8b68cd52012-09-29 11:32:37 -04003846 port_speed = 100;
3847 break;
James Smart26d830e2015-04-07 15:07:17 -04003848 case LPFC_ASYNC_LINK_SPEED_1GBPS:
James Smart8b68cd52012-09-29 11:32:37 -04003849 port_speed = 1000;
3850 break;
James Smart26d830e2015-04-07 15:07:17 -04003851 case LPFC_ASYNC_LINK_SPEED_10GBPS:
James Smart8b68cd52012-09-29 11:32:37 -04003852 port_speed = 10000;
3853 break;
James Smart26d830e2015-04-07 15:07:17 -04003854 case LPFC_ASYNC_LINK_SPEED_20GBPS:
3855 port_speed = 20000;
3856 break;
3857 case LPFC_ASYNC_LINK_SPEED_25GBPS:
3858 port_speed = 25000;
3859 break;
3860 case LPFC_ASYNC_LINK_SPEED_40GBPS:
3861 port_speed = 40000;
3862 break;
James Smart8b68cd52012-09-29 11:32:37 -04003863 default:
3864 port_speed = 0;
3865 }
3866 break;
3867 case LPFC_TRAILER_CODE_FC:
3868 switch (speed_code) {
James Smart26d830e2015-04-07 15:07:17 -04003869 case LPFC_FC_LA_SPEED_UNKNOWN:
James Smart8b68cd52012-09-29 11:32:37 -04003870 port_speed = 0;
3871 break;
James Smart26d830e2015-04-07 15:07:17 -04003872 case LPFC_FC_LA_SPEED_1G:
James Smart8b68cd52012-09-29 11:32:37 -04003873 port_speed = 1000;
3874 break;
James Smart26d830e2015-04-07 15:07:17 -04003875 case LPFC_FC_LA_SPEED_2G:
James Smart8b68cd52012-09-29 11:32:37 -04003876 port_speed = 2000;
3877 break;
James Smart26d830e2015-04-07 15:07:17 -04003878 case LPFC_FC_LA_SPEED_4G:
James Smart8b68cd52012-09-29 11:32:37 -04003879 port_speed = 4000;
3880 break;
James Smart26d830e2015-04-07 15:07:17 -04003881 case LPFC_FC_LA_SPEED_8G:
James Smart8b68cd52012-09-29 11:32:37 -04003882 port_speed = 8000;
3883 break;
James Smart26d830e2015-04-07 15:07:17 -04003884 case LPFC_FC_LA_SPEED_10G:
James Smart8b68cd52012-09-29 11:32:37 -04003885 port_speed = 10000;
3886 break;
James Smart26d830e2015-04-07 15:07:17 -04003887 case LPFC_FC_LA_SPEED_16G:
James Smart8b68cd52012-09-29 11:32:37 -04003888 port_speed = 16000;
3889 break;
James Smartd38dd522015-08-31 16:48:17 -04003890 case LPFC_FC_LA_SPEED_32G:
3891 port_speed = 32000;
3892 break;
James Smart8b68cd52012-09-29 11:32:37 -04003893 default:
3894 port_speed = 0;
3895 }
3896 break;
3897 default:
3898 port_speed = 0;
3899 }
3900 return port_speed;
3901}
3902
3903/**
James Smart70f3c072010-12-15 17:57:33 -05003904 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
James Smartda0436e2009-05-22 14:51:39 -04003905 * @phba: pointer to lpfc hba data structure.
3906 * @acqe_link: pointer to the async link completion queue entry.
3907 *
James Smart70f3c072010-12-15 17:57:33 -05003908 * This routine is to handle the SLI4 asynchronous FCoE link event.
James Smartda0436e2009-05-22 14:51:39 -04003909 **/
3910static void
3911lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3912 struct lpfc_acqe_link *acqe_link)
3913{
3914 struct lpfc_dmabuf *mp;
3915 LPFC_MBOXQ_t *pmb;
3916 MAILBOX_t *mb;
James Smart76a95d72010-11-20 23:11:48 -05003917 struct lpfc_mbx_read_top *la;
James Smartda0436e2009-05-22 14:51:39 -04003918 uint8_t att_type;
James Smart76a95d72010-11-20 23:11:48 -05003919 int rc;
James Smartda0436e2009-05-22 14:51:39 -04003920
3921 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05003922 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
James Smartda0436e2009-05-22 14:51:39 -04003923 return;
James Smart32b97932009-07-19 10:01:21 -04003924 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003925 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3926 if (!pmb) {
3927 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3928 "0395 The mboxq allocation failed\n");
3929 return;
3930 }
3931 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3932 if (!mp) {
3933 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3934 "0396 The lpfc_dmabuf allocation failed\n");
3935 goto out_free_pmb;
3936 }
3937 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3938 if (!mp->virt) {
3939 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3940 "0397 The mbuf allocation failed\n");
3941 goto out_free_dmabuf;
3942 }
3943
3944 /* Cleanup any outstanding ELS commands */
3945 lpfc_els_flush_all_cmd(phba);
3946
3947 /* Block ELS IOCBs until we have done process link event */
3948 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3949
3950 /* Update link event statistics */
3951 phba->sli.slistat.link_event++;
3952
James Smart76a95d72010-11-20 23:11:48 -05003953 /* Create lpfc_handle_latt mailbox command from link ACQE */
3954 lpfc_read_topology(phba, pmb, mp);
3955 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smartda0436e2009-05-22 14:51:39 -04003956 pmb->vport = phba->pport;
3957
James Smartda0436e2009-05-22 14:51:39 -04003958 /* Keep the link status for extra SLI4 state machine reference */
3959 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04003960 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_LINK,
3961 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smartda0436e2009-05-22 14:51:39 -04003962 phba->sli4_hba.link_state.duplex =
3963 bf_get(lpfc_acqe_link_duplex, acqe_link);
3964 phba->sli4_hba.link_state.status =
3965 bf_get(lpfc_acqe_link_status, acqe_link);
James Smart70f3c072010-12-15 17:57:33 -05003966 phba->sli4_hba.link_state.type =
3967 bf_get(lpfc_acqe_link_type, acqe_link);
3968 phba->sli4_hba.link_state.number =
3969 bf_get(lpfc_acqe_link_number, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04003970 phba->sli4_hba.link_state.fault =
3971 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05003972 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04003973 bf_get(lpfc_acqe_logical_link_speed, acqe_link) * 10;
3974
James Smart70f3c072010-12-15 17:57:33 -05003975 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartc31098c2011-04-16 11:03:33 -04003976 "2900 Async FC/FCoE Link event - Speed:%dGBit "
3977 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
3978 "Logical speed:%dMbps Fault:%d\n",
James Smart70f3c072010-12-15 17:57:33 -05003979 phba->sli4_hba.link_state.speed,
3980 phba->sli4_hba.link_state.topology,
3981 phba->sli4_hba.link_state.status,
3982 phba->sli4_hba.link_state.type,
3983 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04003984 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05003985 phba->sli4_hba.link_state.fault);
James Smart76a95d72010-11-20 23:11:48 -05003986 /*
3987 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
3988 * topology info. Note: Optional for non FC-AL ports.
3989 */
3990 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3991 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3992 if (rc == MBX_NOT_FINISHED)
3993 goto out_free_dmabuf;
3994 return;
3995 }
3996 /*
3997 * For FCoE Mode: fill in all the topology information we need and call
3998 * the READ_TOPOLOGY completion routine to continue without actually
3999 * sending the READ_TOPOLOGY mailbox command to the port.
4000 */
4001 /* Parse and translate status field */
4002 mb = &pmb->u.mb;
4003 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
4004
4005 /* Parse and translate link attention fields */
4006 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
4007 la->eventTag = acqe_link->event_tag;
4008 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
4009 bf_set(lpfc_mbx_read_top_link_spd, la,
James Smarta085e872015-12-16 18:12:02 -05004010 (bf_get(lpfc_acqe_link_speed, acqe_link)));
James Smart76a95d72010-11-20 23:11:48 -05004011
4012 /* Fake the the following irrelvant fields */
4013 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
4014 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
4015 bf_set(lpfc_mbx_read_top_il, la, 0);
4016 bf_set(lpfc_mbx_read_top_pb, la, 0);
4017 bf_set(lpfc_mbx_read_top_fa, la, 0);
4018 bf_set(lpfc_mbx_read_top_mm, la, 0);
James Smartda0436e2009-05-22 14:51:39 -04004019
4020 /* Invoke the lpfc_handle_latt mailbox command callback function */
James Smart76a95d72010-11-20 23:11:48 -05004021 lpfc_mbx_cmpl_read_topology(phba, pmb);
James Smartda0436e2009-05-22 14:51:39 -04004022
4023 return;
4024
4025out_free_dmabuf:
4026 kfree(mp);
4027out_free_pmb:
4028 mempool_free(pmb, phba->mbox_mem_pool);
4029}
4030
4031/**
James Smart70f3c072010-12-15 17:57:33 -05004032 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
4033 * @phba: pointer to lpfc hba data structure.
4034 * @acqe_fc: pointer to the async fc completion queue entry.
4035 *
4036 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
4037 * that the event was received and then issue a read_topology mailbox command so
4038 * that the rest of the driver will treat it the same as SLI3.
4039 **/
4040static void
4041lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
4042{
4043 struct lpfc_dmabuf *mp;
4044 LPFC_MBOXQ_t *pmb;
James Smart7bdedb32016-07-06 12:36:00 -07004045 MAILBOX_t *mb;
4046 struct lpfc_mbx_read_top *la;
James Smart70f3c072010-12-15 17:57:33 -05004047 int rc;
4048
4049 if (bf_get(lpfc_trailer_type, acqe_fc) !=
4050 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
4051 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4052 "2895 Non FC link Event detected.(%d)\n",
4053 bf_get(lpfc_trailer_type, acqe_fc));
4054 return;
4055 }
4056 /* Keep the link status for extra SLI4 state machine reference */
4057 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04004058 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
4059 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
James Smart70f3c072010-12-15 17:57:33 -05004060 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
4061 phba->sli4_hba.link_state.topology =
4062 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
4063 phba->sli4_hba.link_state.status =
4064 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
4065 phba->sli4_hba.link_state.type =
4066 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
4067 phba->sli4_hba.link_state.number =
4068 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
4069 phba->sli4_hba.link_state.fault =
4070 bf_get(lpfc_acqe_link_fault, acqe_fc);
4071 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04004072 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
James Smart70f3c072010-12-15 17:57:33 -05004073 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4074 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
4075 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
4076 "%dMbps Fault:%d\n",
4077 phba->sli4_hba.link_state.speed,
4078 phba->sli4_hba.link_state.topology,
4079 phba->sli4_hba.link_state.status,
4080 phba->sli4_hba.link_state.type,
4081 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04004082 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05004083 phba->sli4_hba.link_state.fault);
4084 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4085 if (!pmb) {
4086 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4087 "2897 The mboxq allocation failed\n");
4088 return;
4089 }
4090 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4091 if (!mp) {
4092 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4093 "2898 The lpfc_dmabuf allocation failed\n");
4094 goto out_free_pmb;
4095 }
4096 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
4097 if (!mp->virt) {
4098 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4099 "2899 The mbuf allocation failed\n");
4100 goto out_free_dmabuf;
4101 }
4102
4103 /* Cleanup any outstanding ELS commands */
4104 lpfc_els_flush_all_cmd(phba);
4105
4106 /* Block ELS IOCBs until we have done process link event */
4107 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
4108
4109 /* Update link event statistics */
4110 phba->sli.slistat.link_event++;
4111
4112 /* Create lpfc_handle_latt mailbox command from link ACQE */
4113 lpfc_read_topology(phba, pmb, mp);
4114 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
4115 pmb->vport = phba->pport;
4116
James Smart7bdedb32016-07-06 12:36:00 -07004117 if (phba->sli4_hba.link_state.status != LPFC_FC_LA_TYPE_LINK_UP) {
4118 /* Parse and translate status field */
4119 mb = &pmb->u.mb;
4120 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba,
4121 (void *)acqe_fc);
4122
4123 /* Parse and translate link attention fields */
4124 la = (struct lpfc_mbx_read_top *)&pmb->u.mb.un.varReadTop;
4125 la->eventTag = acqe_fc->event_tag;
4126 bf_set(lpfc_mbx_read_top_att_type, la,
4127 LPFC_FC_LA_TYPE_LINK_DOWN);
4128
4129 /* Invoke the mailbox command callback function */
4130 lpfc_mbx_cmpl_read_topology(phba, pmb);
4131
4132 return;
4133 }
4134
James Smart70f3c072010-12-15 17:57:33 -05004135 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4136 if (rc == MBX_NOT_FINISHED)
4137 goto out_free_dmabuf;
4138 return;
4139
4140out_free_dmabuf:
4141 kfree(mp);
4142out_free_pmb:
4143 mempool_free(pmb, phba->mbox_mem_pool);
4144}
4145
4146/**
4147 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
4148 * @phba: pointer to lpfc hba data structure.
4149 * @acqe_fc: pointer to the async SLI completion queue entry.
4150 *
4151 * This routine is to handle the SLI4 asynchronous SLI events.
4152 **/
4153static void
4154lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
4155{
James Smart4b8bae02012-06-12 13:55:07 -04004156 char port_name;
James Smart8c1312e2012-10-31 14:45:09 -04004157 char message[128];
James Smart4b8bae02012-06-12 13:55:07 -04004158 uint8_t status;
James Smart946727d2015-04-07 15:07:09 -04004159 uint8_t evt_type;
James Smart448193b2015-12-16 18:12:05 -05004160 uint8_t operational = 0;
James Smart946727d2015-04-07 15:07:09 -04004161 struct temp_event temp_event_data;
James Smart4b8bae02012-06-12 13:55:07 -04004162 struct lpfc_acqe_misconfigured_event *misconfigured;
James Smart946727d2015-04-07 15:07:09 -04004163 struct Scsi_Host *shost;
James Smart4b8bae02012-06-12 13:55:07 -04004164
James Smart946727d2015-04-07 15:07:09 -04004165 evt_type = bf_get(lpfc_trailer_type, acqe_sli);
4166
James Smart448193b2015-12-16 18:12:05 -05004167 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4168 "2901 Async SLI event - Event Data1:x%08x Event Data2:"
4169 "x%08x SLI Event Type:%d\n",
4170 acqe_sli->event_data1, acqe_sli->event_data2,
4171 evt_type);
James Smart4b8bae02012-06-12 13:55:07 -04004172
4173 port_name = phba->Port[0];
4174 if (port_name == 0x00)
4175 port_name = '?'; /* get port name is empty */
4176
James Smart946727d2015-04-07 15:07:09 -04004177 switch (evt_type) {
4178 case LPFC_SLI_EVENT_TYPE_OVER_TEMP:
4179 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
4180 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
4181 temp_event_data.data = (uint32_t)acqe_sli->event_data1;
4182
4183 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4184 "3190 Over Temperature:%d Celsius- Port Name %c\n",
4185 acqe_sli->event_data1, port_name);
4186
James Smart310429e2016-07-06 12:35:54 -07004187 phba->sfp_warning |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
James Smart946727d2015-04-07 15:07:09 -04004188 shost = lpfc_shost_from_vport(phba->pport);
4189 fc_host_post_vendor_event(shost, fc_get_event_number(),
4190 sizeof(temp_event_data),
4191 (char *)&temp_event_data,
4192 SCSI_NL_VID_TYPE_PCI
4193 | PCI_VENDOR_ID_EMULEX);
4194 break;
4195 case LPFC_SLI_EVENT_TYPE_NORM_TEMP:
4196 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
4197 temp_event_data.event_code = LPFC_NORMAL_TEMP;
4198 temp_event_data.data = (uint32_t)acqe_sli->event_data1;
4199
4200 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4201 "3191 Normal Temperature:%d Celsius - Port Name %c\n",
4202 acqe_sli->event_data1, port_name);
4203
4204 shost = lpfc_shost_from_vport(phba->pport);
4205 fc_host_post_vendor_event(shost, fc_get_event_number(),
4206 sizeof(temp_event_data),
4207 (char *)&temp_event_data,
4208 SCSI_NL_VID_TYPE_PCI
4209 | PCI_VENDOR_ID_EMULEX);
4210 break;
4211 case LPFC_SLI_EVENT_TYPE_MISCONFIGURED:
4212 misconfigured = (struct lpfc_acqe_misconfigured_event *)
James Smart4b8bae02012-06-12 13:55:07 -04004213 &acqe_sli->event_data1;
4214
James Smart946727d2015-04-07 15:07:09 -04004215 /* fetch the status for this port */
4216 switch (phba->sli4_hba.lnk_info.lnk_no) {
4217 case LPFC_LINK_NUMBER_0:
James Smart448193b2015-12-16 18:12:05 -05004218 status = bf_get(lpfc_sli_misconfigured_port0_state,
4219 &misconfigured->theEvent);
4220 operational = bf_get(lpfc_sli_misconfigured_port0_op,
James Smart4b8bae02012-06-12 13:55:07 -04004221 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04004222 break;
4223 case LPFC_LINK_NUMBER_1:
James Smart448193b2015-12-16 18:12:05 -05004224 status = bf_get(lpfc_sli_misconfigured_port1_state,
4225 &misconfigured->theEvent);
4226 operational = bf_get(lpfc_sli_misconfigured_port1_op,
James Smart4b8bae02012-06-12 13:55:07 -04004227 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04004228 break;
4229 case LPFC_LINK_NUMBER_2:
James Smart448193b2015-12-16 18:12:05 -05004230 status = bf_get(lpfc_sli_misconfigured_port2_state,
4231 &misconfigured->theEvent);
4232 operational = bf_get(lpfc_sli_misconfigured_port2_op,
James Smart4b8bae02012-06-12 13:55:07 -04004233 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04004234 break;
4235 case LPFC_LINK_NUMBER_3:
James Smart448193b2015-12-16 18:12:05 -05004236 status = bf_get(lpfc_sli_misconfigured_port3_state,
4237 &misconfigured->theEvent);
4238 operational = bf_get(lpfc_sli_misconfigured_port3_op,
James Smart4b8bae02012-06-12 13:55:07 -04004239 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04004240 break;
4241 default:
James Smart448193b2015-12-16 18:12:05 -05004242 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4243 "3296 "
4244 "LPFC_SLI_EVENT_TYPE_MISCONFIGURED "
4245 "event: Invalid link %d",
4246 phba->sli4_hba.lnk_info.lnk_no);
4247 return;
James Smart946727d2015-04-07 15:07:09 -04004248 }
James Smart4b8bae02012-06-12 13:55:07 -04004249
James Smart448193b2015-12-16 18:12:05 -05004250 /* Skip if optic state unchanged */
4251 if (phba->sli4_hba.lnk_info.optic_state == status)
4252 return;
4253
James Smart946727d2015-04-07 15:07:09 -04004254 switch (status) {
4255 case LPFC_SLI_EVENT_STATUS_VALID:
James Smart448193b2015-12-16 18:12:05 -05004256 sprintf(message, "Physical Link is functional");
4257 break;
James Smart946727d2015-04-07 15:07:09 -04004258 case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
4259 sprintf(message, "Optics faulted/incorrectly "
4260 "installed/not installed - Reseat optics, "
4261 "if issue not resolved, replace.");
4262 break;
4263 case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
4264 sprintf(message,
4265 "Optics of two types installed - Remove one "
4266 "optic or install matching pair of optics.");
4267 break;
4268 case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
4269 sprintf(message, "Incompatible optics - Replace with "
James Smart292098b2012-09-29 11:31:41 -04004270 "compatible optics for card to function.");
James Smart946727d2015-04-07 15:07:09 -04004271 break;
James Smart448193b2015-12-16 18:12:05 -05004272 case LPFC_SLI_EVENT_STATUS_UNQUALIFIED:
4273 sprintf(message, "Unqualified optics - Replace with "
4274 "Avago optics for Warranty and Technical "
4275 "Support - Link is%s operational",
4276 (operational) ? "" : " not");
4277 break;
4278 case LPFC_SLI_EVENT_STATUS_UNCERTIFIED:
4279 sprintf(message, "Uncertified optics - Replace with "
4280 "Avago-certified optics to enable link "
4281 "operation - Link is%s operational",
4282 (operational) ? "" : " not");
4283 break;
James Smart946727d2015-04-07 15:07:09 -04004284 default:
4285 /* firmware is reporting a status we don't know about */
4286 sprintf(message, "Unknown event status x%02x", status);
4287 break;
4288 }
James Smart448193b2015-12-16 18:12:05 -05004289 phba->sli4_hba.lnk_info.optic_state = status;
James Smart946727d2015-04-07 15:07:09 -04004290 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart448193b2015-12-16 18:12:05 -05004291 "3176 Port Name %c %s\n", port_name, message);
James Smart946727d2015-04-07 15:07:09 -04004292 break;
4293 case LPFC_SLI_EVENT_TYPE_REMOTE_DPORT:
4294 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4295 "3192 Remote DPort Test Initiated - "
4296 "Event Data1:x%08x Event Data2: x%08x\n",
4297 acqe_sli->event_data1, acqe_sli->event_data2);
James Smart4b8bae02012-06-12 13:55:07 -04004298 break;
4299 default:
James Smart946727d2015-04-07 15:07:09 -04004300 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4301 "3193 Async SLI event - Event Data1:x%08x Event Data2:"
4302 "x%08x SLI Event Type:%d\n",
4303 acqe_sli->event_data1, acqe_sli->event_data2,
4304 evt_type);
James Smart4b8bae02012-06-12 13:55:07 -04004305 break;
4306 }
James Smart70f3c072010-12-15 17:57:33 -05004307}
4308
4309/**
James Smartfc2b9892010-02-26 14:15:29 -05004310 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
4311 * @vport: pointer to vport data structure.
4312 *
4313 * This routine is to perform Clear Virtual Link (CVL) on a vport in
4314 * response to a CVL event.
4315 *
4316 * Return the pointer to the ndlp with the vport if successful, otherwise
4317 * return NULL.
4318 **/
4319static struct lpfc_nodelist *
4320lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
4321{
4322 struct lpfc_nodelist *ndlp;
4323 struct Scsi_Host *shost;
4324 struct lpfc_hba *phba;
4325
4326 if (!vport)
4327 return NULL;
James Smartfc2b9892010-02-26 14:15:29 -05004328 phba = vport->phba;
4329 if (!phba)
4330 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04004331 ndlp = lpfc_findnode_did(vport, Fabric_DID);
4332 if (!ndlp) {
4333 /* Cannot find existing Fabric ndlp, so allocate a new one */
4334 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
4335 if (!ndlp)
4336 return 0;
4337 lpfc_nlp_init(vport, ndlp, Fabric_DID);
4338 /* Set the node type */
4339 ndlp->nlp_type |= NLP_FABRIC;
4340 /* Put ndlp onto node list */
4341 lpfc_enqueue_node(vport, ndlp);
4342 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
4343 /* re-setup ndlp without removing from node list */
4344 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
4345 if (!ndlp)
4346 return 0;
4347 }
James Smart63e801c2010-11-20 23:14:19 -05004348 if ((phba->pport->port_state < LPFC_FLOGI) &&
4349 (phba->pport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05004350 return NULL;
4351 /* If virtual link is not yet instantiated ignore CVL */
James Smart63e801c2010-11-20 23:14:19 -05004352 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
4353 && (vport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05004354 return NULL;
4355 shost = lpfc_shost_from_vport(vport);
4356 if (!shost)
4357 return NULL;
4358 lpfc_linkdown_port(vport);
4359 lpfc_cleanup_pending_mbox(vport);
4360 spin_lock_irq(shost->host_lock);
4361 vport->fc_flag |= FC_VPORT_CVL_RCVD;
4362 spin_unlock_irq(shost->host_lock);
4363
4364 return ndlp;
4365}
4366
4367/**
4368 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
4369 * @vport: pointer to lpfc hba data structure.
4370 *
4371 * This routine is to perform Clear Virtual Link (CVL) on all vports in
4372 * response to a FCF dead event.
4373 **/
4374static void
4375lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
4376{
4377 struct lpfc_vport **vports;
4378 int i;
4379
4380 vports = lpfc_create_vport_work_array(phba);
4381 if (vports)
4382 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
4383 lpfc_sli4_perform_vport_cvl(vports[i]);
4384 lpfc_destroy_vport_work_array(phba, vports);
4385}
4386
4387/**
James Smart76a95d72010-11-20 23:11:48 -05004388 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
James Smartda0436e2009-05-22 14:51:39 -04004389 * @phba: pointer to lpfc hba data structure.
4390 * @acqe_link: pointer to the async fcoe completion queue entry.
4391 *
4392 * This routine is to handle the SLI4 asynchronous fcoe event.
4393 **/
4394static void
James Smart76a95d72010-11-20 23:11:48 -05004395lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
James Smart70f3c072010-12-15 17:57:33 -05004396 struct lpfc_acqe_fip *acqe_fip)
James Smartda0436e2009-05-22 14:51:39 -04004397{
James Smart70f3c072010-12-15 17:57:33 -05004398 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004399 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04004400 struct lpfc_vport *vport;
4401 struct lpfc_nodelist *ndlp;
4402 struct Scsi_Host *shost;
James Smart695a8142010-01-26 23:08:03 -05004403 int active_vlink_present;
4404 struct lpfc_vport **vports;
4405 int i;
James Smartda0436e2009-05-22 14:51:39 -04004406
James Smart70f3c072010-12-15 17:57:33 -05004407 phba->fc_eventTag = acqe_fip->event_tag;
4408 phba->fcoe_eventtag = acqe_fip->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004409 switch (event_type) {
James Smart70f3c072010-12-15 17:57:33 -05004410 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
4411 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
4412 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
James Smart999d8132010-03-15 11:24:56 -04004413 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4414 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004415 "2546 New FCF event, evt_tag:x%x, "
4416 "index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004417 acqe_fip->event_tag,
4418 acqe_fip->index);
James Smart999d8132010-03-15 11:24:56 -04004419 else
4420 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
4421 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004422 "2788 FCF param modified event, "
4423 "evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004424 acqe_fip->event_tag,
4425 acqe_fip->index);
James Smart38b92ef2010-08-04 16:11:39 -04004426 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004427 /*
4428 * During period of FCF discovery, read the FCF
4429 * table record indexed by the event to update
James Smarta93ff372010-10-22 11:06:08 -04004430 * FCF roundrobin failover eligible FCF bmask.
James Smart0c9ab6f2010-02-26 14:15:57 -05004431 */
4432 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4433 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004434 "2779 Read FCF (x%x) for updating "
4435 "roundrobin FCF failover bmask\n",
James Smart70f3c072010-12-15 17:57:33 -05004436 acqe_fip->index);
4437 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05004438 }
James Smart38b92ef2010-08-04 16:11:39 -04004439
4440 /* If the FCF discovery is in progress, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04004441 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04004442 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart38b92ef2010-08-04 16:11:39 -04004443 spin_unlock_irq(&phba->hbalock);
4444 break;
4445 }
4446 /* If fast FCF failover rescan event is pending, do nothing */
4447 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
4448 spin_unlock_irq(&phba->hbalock);
4449 break;
4450 }
4451
James Smartc2b97122013-05-31 17:05:36 -04004452 /* If the FCF has been in discovered state, do nothing. */
4453 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
James Smart3804dc82010-07-14 15:31:37 -04004454 spin_unlock_irq(&phba->hbalock);
4455 break;
4456 }
4457 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04004458
James Smart0c9ab6f2010-02-26 14:15:57 -05004459 /* Otherwise, scan the entire FCF table and re-discover SAN */
4460 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004461 "2770 Start FCF table scan per async FCF "
4462 "event, evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004463 acqe_fip->event_tag, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05004464 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
4465 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04004466 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05004467 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4468 "2547 Issue FCF scan read FCF mailbox "
James Smarta93ff372010-10-22 11:06:08 -04004469 "command failed (x%x)\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004470 break;
4471
James Smart70f3c072010-12-15 17:57:33 -05004472 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
James Smartda0436e2009-05-22 14:51:39 -04004473 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04004474 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004475 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
4476 acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04004477 break;
4478
James Smart70f3c072010-12-15 17:57:33 -05004479 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
James Smart80c17842012-03-01 22:35:45 -05004480 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05004481 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004482 "2549 FCF (x%x) disconnected from network, "
James Smart70f3c072010-12-15 17:57:33 -05004483 "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart38b92ef2010-08-04 16:11:39 -04004484 /*
4485 * If we are in the middle of FCF failover process, clear
4486 * the corresponding FCF bit in the roundrobin bitmap.
James Smartda0436e2009-05-22 14:51:39 -04004487 */
James Smartfc2b9892010-02-26 14:15:29 -05004488 spin_lock_irq(&phba->hbalock);
James Smarta1cadfe2016-07-06 12:36:02 -07004489 if ((phba->fcf.fcf_flag & FCF_DISCOVERY) &&
4490 (phba->fcf.current_rec.fcf_indx != acqe_fip->index)) {
James Smartfc2b9892010-02-26 14:15:29 -05004491 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004492 /* Update FLOGI FCF failover eligible FCF bmask */
James Smart70f3c072010-12-15 17:57:33 -05004493 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004494 break;
4495 }
James Smart38b92ef2010-08-04 16:11:39 -04004496 spin_unlock_irq(&phba->hbalock);
4497
4498 /* If the event is not for currently used fcf do nothing */
James Smart70f3c072010-12-15 17:57:33 -05004499 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
James Smart38b92ef2010-08-04 16:11:39 -04004500 break;
4501
4502 /*
4503 * Otherwise, request the port to rediscover the entire FCF
4504 * table for a fast recovery from case that the current FCF
4505 * is no longer valid as we are not in the middle of FCF
4506 * failover process already.
4507 */
James Smartc2b97122013-05-31 17:05:36 -04004508 spin_lock_irq(&phba->hbalock);
4509 /* Mark the fast failover process in progress */
4510 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
4511 spin_unlock_irq(&phba->hbalock);
4512
4513 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
4514 "2771 Start FCF fast failover process due to "
4515 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
4516 "\n", acqe_fip->event_tag, acqe_fip->index);
4517 rc = lpfc_sli4_redisc_fcf_table(phba);
4518 if (rc) {
4519 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4520 LOG_DISCOVERY,
4521 "2772 Issue FCF rediscover mabilbox "
4522 "command failed, fail through to FCF "
4523 "dead event\n");
4524 spin_lock_irq(&phba->hbalock);
4525 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
4526 spin_unlock_irq(&phba->hbalock);
4527 /*
4528 * Last resort will fail over by treating this
4529 * as a link down to FCF registration.
4530 */
4531 lpfc_sli4_fcf_dead_failthrough(phba);
4532 } else {
4533 /* Reset FCF roundrobin bmask for new discovery */
4534 lpfc_sli4_clear_fcf_rr_bmask(phba);
4535 /*
4536 * Handling fast FCF failover to a DEAD FCF event is
4537 * considered equalivant to receiving CVL to all vports.
4538 */
4539 lpfc_sli4_perform_all_vport_cvl(phba);
4540 }
James Smartda0436e2009-05-22 14:51:39 -04004541 break;
James Smart70f3c072010-12-15 17:57:33 -05004542 case LPFC_FIP_EVENT_TYPE_CVL:
James Smart80c17842012-03-01 22:35:45 -05004543 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05004544 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6669f9b2009-10-02 15:16:45 -04004545 "2718 Clear Virtual Link Received for VPI 0x%x"
James Smart70f3c072010-12-15 17:57:33 -05004546 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart6d368e52011-05-24 11:44:12 -04004547
James Smart6669f9b2009-10-02 15:16:45 -04004548 vport = lpfc_find_vport_by_vpid(phba,
James Smart5248a742011-07-22 18:37:06 -04004549 acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004550 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04004551 if (!ndlp)
4552 break;
James Smart695a8142010-01-26 23:08:03 -05004553 active_vlink_present = 0;
4554
4555 vports = lpfc_create_vport_work_array(phba);
4556 if (vports) {
4557 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
4558 i++) {
4559 if ((!(vports[i]->fc_flag &
4560 FC_VPORT_CVL_RCVD)) &&
4561 (vports[i]->port_state > LPFC_FDISC)) {
4562 active_vlink_present = 1;
4563 break;
4564 }
4565 }
4566 lpfc_destroy_vport_work_array(phba, vports);
4567 }
4568
James Smartcc823552015-05-21 13:55:26 -04004569 /*
4570 * Don't re-instantiate if vport is marked for deletion.
4571 * If we are here first then vport_delete is going to wait
4572 * for discovery to complete.
4573 */
4574 if (!(vport->load_flag & FC_UNLOADING) &&
4575 active_vlink_present) {
James Smart695a8142010-01-26 23:08:03 -05004576 /*
4577 * If there are other active VLinks present,
4578 * re-instantiate the Vlink using FDISC.
4579 */
James Smart256ec0d2013-04-17 20:14:58 -04004580 mod_timer(&ndlp->nlp_delayfunc,
4581 jiffies + msecs_to_jiffies(1000));
James Smartfc2b9892010-02-26 14:15:29 -05004582 shost = lpfc_shost_from_vport(vport);
James Smart6669f9b2009-10-02 15:16:45 -04004583 spin_lock_irq(shost->host_lock);
4584 ndlp->nlp_flag |= NLP_DELAY_TMO;
4585 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05004586 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
4587 vport->port_state = LPFC_FDISC;
4588 } else {
James Smartecfd03c2010-02-12 14:41:27 -05004589 /*
4590 * Otherwise, we request port to rediscover
4591 * the entire FCF table for a fast recovery
4592 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05004593 * is no longer valid if we are not already
4594 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05004595 */
James Smartfc2b9892010-02-26 14:15:29 -05004596 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004597 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05004598 spin_unlock_irq(&phba->hbalock);
4599 break;
4600 }
4601 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05004602 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004603 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004604 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4605 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004606 "2773 Start FCF failover per CVL, "
James Smart70f3c072010-12-15 17:57:33 -05004607 "evt_tag:x%x\n", acqe_fip->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05004608 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05004609 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004610 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4611 LOG_DISCOVERY,
4612 "2774 Issue FCF rediscover "
4613 "mabilbox command failed, "
4614 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05004615 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004616 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004617 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05004618 /*
4619 * Last resort will be re-try on the
4620 * the current registered FCF entry.
4621 */
4622 lpfc_retry_pport_discovery(phba);
James Smart38b92ef2010-08-04 16:11:39 -04004623 } else
4624 /*
4625 * Reset FCF roundrobin bmask for new
4626 * discovery.
4627 */
James Smart7d791df2011-07-22 18:37:52 -04004628 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart6669f9b2009-10-02 15:16:45 -04004629 }
4630 break;
James Smartda0436e2009-05-22 14:51:39 -04004631 default:
4632 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4633 "0288 Unknown FCoE event type 0x%x event tag "
James Smart70f3c072010-12-15 17:57:33 -05004634 "0x%x\n", event_type, acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04004635 break;
4636 }
4637}
4638
4639/**
4640 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
4641 * @phba: pointer to lpfc hba data structure.
4642 * @acqe_link: pointer to the async dcbx completion queue entry.
4643 *
4644 * This routine is to handle the SLI4 asynchronous dcbx event.
4645 **/
4646static void
4647lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
4648 struct lpfc_acqe_dcbx *acqe_dcbx)
4649{
James Smart4d9ab992009-10-02 15:16:39 -04004650 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004651 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4652 "0290 The SLI4 DCBX asynchronous event is not "
4653 "handled yet\n");
4654}
4655
4656/**
James Smartb19a0612010-04-06 14:48:51 -04004657 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
4658 * @phba: pointer to lpfc hba data structure.
4659 * @acqe_link: pointer to the async grp5 completion queue entry.
4660 *
4661 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
4662 * is an asynchronous notified of a logical link speed change. The Port
4663 * reports the logical link speed in units of 10Mbps.
4664 **/
4665static void
4666lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
4667 struct lpfc_acqe_grp5 *acqe_grp5)
4668{
4669 uint16_t prev_ll_spd;
4670
4671 phba->fc_eventTag = acqe_grp5->event_tag;
4672 phba->fcoe_eventtag = acqe_grp5->event_tag;
4673 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
4674 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04004675 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5)) * 10;
James Smartb19a0612010-04-06 14:48:51 -04004676 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4677 "2789 GRP5 Async Event: Updating logical link speed "
James Smart8b68cd52012-09-29 11:32:37 -04004678 "from %dMbps to %dMbps\n", prev_ll_spd,
4679 phba->sli4_hba.link_state.logical_speed);
James Smartb19a0612010-04-06 14:48:51 -04004680}
4681
4682/**
James Smartda0436e2009-05-22 14:51:39 -04004683 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
4684 * @phba: pointer to lpfc hba data structure.
4685 *
4686 * This routine is invoked by the worker thread to process all the pending
4687 * SLI4 asynchronous events.
4688 **/
4689void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
4690{
4691 struct lpfc_cq_event *cq_event;
4692
4693 /* First, declare the async event has been handled */
4694 spin_lock_irq(&phba->hbalock);
4695 phba->hba_flag &= ~ASYNC_EVENT;
4696 spin_unlock_irq(&phba->hbalock);
4697 /* Now, handle all the async events */
4698 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
4699 /* Get the first event from the head of the event queue */
4700 spin_lock_irq(&phba->hbalock);
4701 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
4702 cq_event, struct lpfc_cq_event, list);
4703 spin_unlock_irq(&phba->hbalock);
4704 /* Process the asynchronous event */
4705 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
4706 case LPFC_TRAILER_CODE_LINK:
4707 lpfc_sli4_async_link_evt(phba,
4708 &cq_event->cqe.acqe_link);
4709 break;
4710 case LPFC_TRAILER_CODE_FCOE:
James Smart70f3c072010-12-15 17:57:33 -05004711 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004712 break;
4713 case LPFC_TRAILER_CODE_DCBX:
4714 lpfc_sli4_async_dcbx_evt(phba,
4715 &cq_event->cqe.acqe_dcbx);
4716 break;
James Smartb19a0612010-04-06 14:48:51 -04004717 case LPFC_TRAILER_CODE_GRP5:
4718 lpfc_sli4_async_grp5_evt(phba,
4719 &cq_event->cqe.acqe_grp5);
4720 break;
James Smart70f3c072010-12-15 17:57:33 -05004721 case LPFC_TRAILER_CODE_FC:
4722 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
4723 break;
4724 case LPFC_TRAILER_CODE_SLI:
4725 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
4726 break;
James Smartda0436e2009-05-22 14:51:39 -04004727 default:
4728 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4729 "1804 Invalid asynchrous event code: "
4730 "x%x\n", bf_get(lpfc_trailer_code,
4731 &cq_event->cqe.mcqe_cmpl));
4732 break;
4733 }
4734 /* Free the completion event processed to the free pool */
4735 lpfc_sli4_cq_event_release(phba, cq_event);
4736 }
4737}
4738
4739/**
James Smartecfd03c2010-02-12 14:41:27 -05004740 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
4741 * @phba: pointer to lpfc hba data structure.
4742 *
4743 * This routine is invoked by the worker thread to process FCF table
4744 * rediscovery pending completion event.
4745 **/
4746void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
4747{
4748 int rc;
4749
4750 spin_lock_irq(&phba->hbalock);
4751 /* Clear FCF rediscovery timeout event */
4752 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
4753 /* Clear driver fast failover FCF record flag */
4754 phba->fcf.failover_rec.flag = 0;
4755 /* Set state for FCF fast failover */
4756 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
4757 spin_unlock_irq(&phba->hbalock);
4758
4759 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05004760 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004761 "2777 Start post-quiescent FCF table scan\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05004762 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05004763 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05004764 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4765 "2747 Issue FCF scan read FCF mailbox "
4766 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05004767}
4768
4769/**
James Smartda0436e2009-05-22 14:51:39 -04004770 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
4771 * @phba: pointer to lpfc hba data structure.
4772 * @dev_grp: The HBA PCI-Device group number.
4773 *
4774 * This routine is invoked to set up the per HBA PCI-Device group function
4775 * API jump table entries.
4776 *
4777 * Return: 0 if success, otherwise -ENODEV
4778 **/
4779int
4780lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05004781{
4782 int rc;
4783
James Smartda0436e2009-05-22 14:51:39 -04004784 /* Set up lpfc PCI-device group */
4785 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05004786
James Smartda0436e2009-05-22 14:51:39 -04004787 /* The LPFC_PCI_DEV_OC uses SLI4 */
4788 if (dev_grp == LPFC_PCI_DEV_OC)
4789 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05004790
James Smartda0436e2009-05-22 14:51:39 -04004791 /* Set up device INIT API function jump table */
4792 rc = lpfc_init_api_table_setup(phba, dev_grp);
4793 if (rc)
4794 return -ENODEV;
4795 /* Set up SCSI API function jump table */
4796 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
4797 if (rc)
4798 return -ENODEV;
4799 /* Set up SLI API function jump table */
4800 rc = lpfc_sli_api_table_setup(phba, dev_grp);
4801 if (rc)
4802 return -ENODEV;
4803 /* Set up MBOX API function jump table */
4804 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
4805 if (rc)
4806 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05004807
James Smartda0436e2009-05-22 14:51:39 -04004808 return 0;
James Smart5b75da22008-12-04 22:39:35 -05004809}
4810
4811/**
James Smart3621a712009-04-06 18:47:14 -04004812 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05004813 * @phba: pointer to lpfc hba data structure.
4814 * @intr_mode: active interrupt mode adopted.
4815 *
4816 * This routine it invoked to log the currently used active interrupt mode
4817 * to the device.
James Smart3772a992009-05-22 14:50:54 -04004818 **/
4819static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05004820{
4821 switch (intr_mode) {
4822 case 0:
4823 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4824 "0470 Enable INTx interrupt mode.\n");
4825 break;
4826 case 1:
4827 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4828 "0481 Enabled MSI interrupt mode.\n");
4829 break;
4830 case 2:
4831 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4832 "0480 Enabled MSI-X interrupt mode.\n");
4833 break;
4834 default:
4835 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4836 "0482 Illegal interrupt mode.\n");
4837 break;
4838 }
4839 return;
4840}
4841
James Smart5b75da22008-12-04 22:39:35 -05004842/**
James Smart3772a992009-05-22 14:50:54 -04004843 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05004844 * @phba: pointer to lpfc hba data structure.
4845 *
James Smart3772a992009-05-22 14:50:54 -04004846 * This routine is invoked to enable the PCI device that is common to all
4847 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05004848 *
4849 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004850 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004851 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05004852 **/
James Smart3772a992009-05-22 14:50:54 -04004853static int
4854lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05004855{
James Smart3772a992009-05-22 14:50:54 -04004856 struct pci_dev *pdev;
James Smart079b5c92011-08-21 21:48:49 -04004857 int bars = 0;
James Smart5b75da22008-12-04 22:39:35 -05004858
James Smart3772a992009-05-22 14:50:54 -04004859 /* Obtain PCI device reference */
4860 if (!phba->pcidev)
4861 goto out_error;
4862 else
4863 pdev = phba->pcidev;
4864 /* Select PCI BARs */
4865 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4866 /* Enable PCI device */
4867 if (pci_enable_device_mem(pdev))
4868 goto out_error;
4869 /* Request PCI resource for the device */
4870 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
4871 goto out_disable_device;
4872 /* Set up device as PCI master and save state for EEH */
4873 pci_set_master(pdev);
4874 pci_try_set_mwi(pdev);
4875 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05004876
James Smart05580562011-05-24 11:40:48 -04004877 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
Jon Mason453193e2013-09-11 15:38:13 -07004878 if (pci_is_pcie(pdev))
James Smart05580562011-05-24 11:40:48 -04004879 pdev->needs_freset = 1;
4880
James Smart3772a992009-05-22 14:50:54 -04004881 return 0;
James Smart5b75da22008-12-04 22:39:35 -05004882
James Smart3772a992009-05-22 14:50:54 -04004883out_disable_device:
4884 pci_disable_device(pdev);
4885out_error:
James Smart079b5c92011-08-21 21:48:49 -04004886 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4887 "1401 Failed to enable pci device, bars:x%x\n", bars);
James Smart3772a992009-05-22 14:50:54 -04004888 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05004889}
4890
4891/**
James Smart3772a992009-05-22 14:50:54 -04004892 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05004893 * @phba: pointer to lpfc hba data structure.
4894 *
James Smart3772a992009-05-22 14:50:54 -04004895 * This routine is invoked to disable the PCI device that is common to all
4896 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05004897 **/
4898static void
James Smart3772a992009-05-22 14:50:54 -04004899lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05004900{
James Smart3772a992009-05-22 14:50:54 -04004901 struct pci_dev *pdev;
4902 int bars;
James Smart5b75da22008-12-04 22:39:35 -05004903
James Smart3772a992009-05-22 14:50:54 -04004904 /* Obtain PCI device reference */
4905 if (!phba->pcidev)
4906 return;
4907 else
4908 pdev = phba->pcidev;
4909 /* Select PCI BARs */
4910 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4911 /* Release PCI resource and disable PCI device */
4912 pci_release_selected_regions(pdev, bars);
4913 pci_disable_device(pdev);
James Smart5b75da22008-12-04 22:39:35 -05004914
4915 return;
4916}
4917
4918/**
James Smart3772a992009-05-22 14:50:54 -04004919 * lpfc_reset_hba - Reset a hba
4920 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04004921 *
James Smart3772a992009-05-22 14:50:54 -04004922 * This routine is invoked to reset a hba device. It brings the HBA
4923 * offline, performs a board restart, and then brings the board back
4924 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
4925 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04004926 **/
James Smart3772a992009-05-22 14:50:54 -04004927void
4928lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004929{
James Smart3772a992009-05-22 14:50:54 -04004930 /* If resets are disabled then set error state and return. */
4931 if (!phba->cfg_enable_hba_reset) {
4932 phba->link_state = LPFC_HBA_ERROR;
4933 return;
4934 }
James Smartee620212014-04-04 13:51:53 -04004935 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
4936 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
4937 else
4938 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart3772a992009-05-22 14:50:54 -04004939 lpfc_offline(phba);
4940 lpfc_sli_brdrestart(phba);
4941 lpfc_online(phba);
4942 lpfc_unblock_mgmt_io(phba);
4943}
dea31012005-04-17 16:05:31 -05004944
James Smart3772a992009-05-22 14:50:54 -04004945/**
James Smart0a96e972011-07-22 18:37:28 -04004946 * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
4947 * @phba: pointer to lpfc hba data structure.
4948 *
4949 * This function enables the PCI SR-IOV virtual functions to a physical
4950 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4951 * enable the number of virtual functions to the physical function. As
4952 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4953 * API call does not considered as an error condition for most of the device.
4954 **/
4955uint16_t
4956lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
4957{
4958 struct pci_dev *pdev = phba->pcidev;
4959 uint16_t nr_virtfn;
4960 int pos;
4961
James Smart0a96e972011-07-22 18:37:28 -04004962 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
4963 if (pos == 0)
4964 return 0;
4965
4966 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
4967 return nr_virtfn;
4968}
4969
4970/**
James Smart912e3ac2011-05-24 11:42:11 -04004971 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
4972 * @phba: pointer to lpfc hba data structure.
4973 * @nr_vfn: number of virtual functions to be enabled.
4974 *
4975 * This function enables the PCI SR-IOV virtual functions to a physical
4976 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4977 * enable the number of virtual functions to the physical function. As
4978 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4979 * API call does not considered as an error condition for most of the device.
4980 **/
4981int
4982lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
4983{
4984 struct pci_dev *pdev = phba->pcidev;
James Smart0a96e972011-07-22 18:37:28 -04004985 uint16_t max_nr_vfn;
James Smart912e3ac2011-05-24 11:42:11 -04004986 int rc;
4987
James Smart0a96e972011-07-22 18:37:28 -04004988 max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
4989 if (nr_vfn > max_nr_vfn) {
4990 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4991 "3057 Requested vfs (%d) greater than "
4992 "supported vfs (%d)", nr_vfn, max_nr_vfn);
4993 return -EINVAL;
4994 }
4995
James Smart912e3ac2011-05-24 11:42:11 -04004996 rc = pci_enable_sriov(pdev, nr_vfn);
4997 if (rc) {
4998 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4999 "2806 Failed to enable sriov on this device "
5000 "with vfn number nr_vf:%d, rc:%d\n",
5001 nr_vfn, rc);
5002 } else
5003 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5004 "2807 Successful enable sriov on this device "
5005 "with vfn number nr_vf:%d\n", nr_vfn);
5006 return rc;
5007}
5008
5009/**
James Smart3772a992009-05-22 14:50:54 -04005010 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
5011 * @phba: pointer to lpfc hba data structure.
5012 *
5013 * This routine is invoked to set up the driver internal resources specific to
5014 * support the SLI-3 HBA device it attached to.
5015 *
5016 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005017 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005018 * other values - error
5019 **/
5020static int
5021lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
5022{
5023 struct lpfc_sli *psli;
James Smart912e3ac2011-05-24 11:42:11 -04005024 int rc;
dea31012005-04-17 16:05:31 -05005025
James Smart3772a992009-05-22 14:50:54 -04005026 /*
5027 * Initialize timers used by driver
5028 */
dea31012005-04-17 16:05:31 -05005029
James Smart3772a992009-05-22 14:50:54 -04005030 /* Heartbeat timer */
5031 init_timer(&phba->hb_tmofunc);
5032 phba->hb_tmofunc.function = lpfc_hb_timeout;
5033 phba->hb_tmofunc.data = (unsigned long)phba;
5034
5035 psli = &phba->sli;
5036 /* MBOX heartbeat timer */
5037 init_timer(&psli->mbox_tmo);
5038 psli->mbox_tmo.function = lpfc_mbox_timeout;
5039 psli->mbox_tmo.data = (unsigned long) phba;
5040 /* FCP polling mode timer */
5041 init_timer(&phba->fcp_poll_timer);
5042 phba->fcp_poll_timer.function = lpfc_poll_timeout;
5043 phba->fcp_poll_timer.data = (unsigned long) phba;
5044 /* Fabric block timer */
5045 init_timer(&phba->fabric_block_timer);
5046 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
5047 phba->fabric_block_timer.data = (unsigned long) phba;
5048 /* EA polling mode timer */
5049 init_timer(&phba->eratt_poll);
5050 phba->eratt_poll.function = lpfc_poll_eratt;
5051 phba->eratt_poll.data = (unsigned long) phba;
5052
5053 /* Host attention work mask setup */
5054 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
5055 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
5056
5057 /* Get all the module params for configuring this host */
5058 lpfc_get_cfgparam(phba);
James Smart49198b32010-04-06 15:04:33 -04005059 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
5060 phba->menlo_flag |= HBA_MENLO_SUPPORT;
5061 /* check for menlo minimum sg count */
5062 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
5063 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
5064 }
5065
James Smart2a76a282012-08-03 12:35:54 -04005066 if (!phba->sli.ring)
Firo Yangc6cb9b42015-08-31 16:48:09 -04005067 phba->sli.ring = kzalloc(LPFC_SLI3_MAX_RING *
James Smart2a76a282012-08-03 12:35:54 -04005068 sizeof(struct lpfc_sli_ring), GFP_KERNEL);
5069 if (!phba->sli.ring)
5070 return -ENOMEM;
5071
James Smart3772a992009-05-22 14:50:54 -04005072 /*
James Smart96f70772013-04-17 20:16:15 -04005073 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smart3772a992009-05-22 14:50:54 -04005074 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smart3772a992009-05-22 14:50:54 -04005075 */
James Smart3772a992009-05-22 14:50:54 -04005076
James Smart96f70772013-04-17 20:16:15 -04005077 /* Initialize the host templates the configured values. */
James Smart3772a992009-05-22 14:50:54 -04005078 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
Bodo Stroesser5f406fa2015-08-31 16:48:10 -04005079 lpfc_template_s3.sg_tablesize = phba->cfg_sg_seg_cnt;
James Smart3772a992009-05-22 14:50:54 -04005080
James Smart96f70772013-04-17 20:16:15 -04005081 /* There are going to be 2 reserved BDEs: 1 FCP cmnd + 1 FCP rsp */
5082 if (phba->cfg_enable_bg) {
5083 /*
5084 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
5085 * the FCP rsp, and a BDE for each. Sice we have no control
5086 * over how many protection data segments the SCSI Layer
5087 * will hand us (ie: there could be one for every block
5088 * in the IO), we just allocate enough BDEs to accomidate
5089 * our max amount and we need to limit lpfc_sg_seg_cnt to
5090 * minimize the risk of running out.
5091 */
5092 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5093 sizeof(struct fcp_rsp) +
5094 (LPFC_MAX_SG_SEG_CNT * sizeof(struct ulp_bde64));
5095
5096 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SEG_CNT_DIF)
5097 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT_DIF;
5098
5099 /* Total BDEs in BPL for scsi_sg_list and scsi_sg_prot_list */
5100 phba->cfg_total_seg_cnt = LPFC_MAX_SG_SEG_CNT;
5101 } else {
5102 /*
5103 * The scsi_buf for a regular I/O will hold the FCP cmnd,
5104 * the FCP rsp, a BDE for each, and a BDE for up to
5105 * cfg_sg_seg_cnt data segments.
5106 */
5107 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5108 sizeof(struct fcp_rsp) +
5109 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
5110
5111 /* Total BDEs in BPL for scsi_sg_list */
5112 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
5113 }
5114
5115 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
5116 "9088 sg_tablesize:%d dmabuf_size:%d total_bde:%d\n",
5117 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
5118 phba->cfg_total_seg_cnt);
5119
James Smart3772a992009-05-22 14:50:54 -04005120 phba->max_vpi = LPFC_MAX_VPI;
5121 /* This will be set to correct value after config_port mbox */
5122 phba->max_vports = 0;
5123
5124 /*
5125 * Initialize the SLI Layer to run with lpfc HBAs.
5126 */
5127 lpfc_sli_setup(phba);
5128 lpfc_sli_queue_setup(phba);
5129
5130 /* Allocate device driver memory */
5131 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
5132 return -ENOMEM;
5133
James Smart912e3ac2011-05-24 11:42:11 -04005134 /*
5135 * Enable sr-iov virtual functions if supported and configured
5136 * through the module parameter.
5137 */
5138 if (phba->cfg_sriov_nr_virtfn > 0) {
5139 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
5140 phba->cfg_sriov_nr_virtfn);
5141 if (rc) {
5142 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5143 "2808 Requested number of SR-IOV "
5144 "virtual functions (%d) is not "
5145 "supported\n",
5146 phba->cfg_sriov_nr_virtfn);
5147 phba->cfg_sriov_nr_virtfn = 0;
5148 }
5149 }
5150
James Smart3772a992009-05-22 14:50:54 -04005151 return 0;
5152}
5153
5154/**
5155 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
5156 * @phba: pointer to lpfc hba data structure.
5157 *
5158 * This routine is invoked to unset the driver internal resources set up
5159 * specific for supporting the SLI-3 HBA device it attached to.
5160 **/
5161static void
5162lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
5163{
5164 /* Free device driver memory allocated */
5165 lpfc_mem_free_all(phba);
5166
5167 return;
5168}
5169
5170/**
James Smartda0436e2009-05-22 14:51:39 -04005171 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
5172 * @phba: pointer to lpfc hba data structure.
5173 *
5174 * This routine is invoked to set up the driver internal resources specific to
5175 * support the SLI-4 HBA device it attached to.
5176 *
5177 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005178 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005179 * other values - error
5180 **/
5181static int
5182lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
5183{
James Smart7bb03bb2013-04-17 20:19:16 -04005184 struct lpfc_vector_map_info *cpup;
James Smartda0436e2009-05-22 14:51:39 -04005185 struct lpfc_sli *psli;
James Smart28baac72010-02-12 14:42:03 -05005186 LPFC_MBOXQ_t *mboxq;
James Smart96f70772013-04-17 20:16:15 -04005187 int rc, i, hbq_count, max_buf_size;
James Smart28baac72010-02-12 14:42:03 -05005188 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
5189 struct lpfc_mqe *mqe;
James Smart09294d42013-04-17 20:16:05 -04005190 int longs;
James Smart1ba981f2014-02-20 09:56:45 -05005191 int fof_vectors = 0;
James Smartda0436e2009-05-22 14:51:39 -04005192
James Smart716d3bc2013-09-06 12:18:28 -04005193 /* Get all the module params for configuring this host */
5194 lpfc_get_cfgparam(phba);
5195
James Smartda0436e2009-05-22 14:51:39 -04005196 /* Before proceed, wait for POST done and device ready */
5197 rc = lpfc_sli4_post_status_check(phba);
5198 if (rc)
5199 return -ENODEV;
5200
5201 /*
5202 * Initialize timers used by driver
5203 */
5204
5205 /* Heartbeat timer */
5206 init_timer(&phba->hb_tmofunc);
5207 phba->hb_tmofunc.function = lpfc_hb_timeout;
5208 phba->hb_tmofunc.data = (unsigned long)phba;
James Smart19ca7602010-11-20 23:11:55 -05005209 init_timer(&phba->rrq_tmr);
5210 phba->rrq_tmr.function = lpfc_rrq_timeout;
5211 phba->rrq_tmr.data = (unsigned long)phba;
James Smartda0436e2009-05-22 14:51:39 -04005212
5213 psli = &phba->sli;
5214 /* MBOX heartbeat timer */
5215 init_timer(&psli->mbox_tmo);
5216 psli->mbox_tmo.function = lpfc_mbox_timeout;
5217 psli->mbox_tmo.data = (unsigned long) phba;
5218 /* Fabric block timer */
5219 init_timer(&phba->fabric_block_timer);
5220 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
5221 phba->fabric_block_timer.data = (unsigned long) phba;
5222 /* EA polling mode timer */
5223 init_timer(&phba->eratt_poll);
5224 phba->eratt_poll.function = lpfc_poll_eratt;
5225 phba->eratt_poll.data = (unsigned long) phba;
James Smartecfd03c2010-02-12 14:41:27 -05005226 /* FCF rediscover timer */
5227 init_timer(&phba->fcf.redisc_wait);
5228 phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
5229 phba->fcf.redisc_wait.data = (unsigned long)phba;
5230
James Smartda0436e2009-05-22 14:51:39 -04005231 /*
James Smart7ad20aa2011-05-24 11:44:28 -04005232 * Control structure for handling external multi-buffer mailbox
5233 * command pass-through.
5234 */
5235 memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
5236 sizeof(struct lpfc_mbox_ext_buf_ctx));
5237 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
5238
James Smartda0436e2009-05-22 14:51:39 -04005239 phba->max_vpi = LPFC_MAX_VPI;
James Smart67d12732012-08-03 12:36:13 -04005240
James Smartda0436e2009-05-22 14:51:39 -04005241 /* This will be set to correct value after the read_config mbox */
5242 phba->max_vports = 0;
5243
5244 /* Program the default value of vlan_id and fc_map */
5245 phba->valid_vlan = 0;
5246 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5247 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5248 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5249
5250 /*
James Smart2a76a282012-08-03 12:35:54 -04005251 * For SLI4, instead of using ring 0 (LPFC_FCP_RING) for FCP commands
5252 * we will associate a new ring, for each FCP fastpath EQ/CQ/WQ tuple.
5253 */
5254 if (!phba->sli.ring)
5255 phba->sli.ring = kzalloc(
James Smart67d12732012-08-03 12:36:13 -04005256 (LPFC_SLI3_MAX_RING + phba->cfg_fcp_io_channel) *
James Smart2a76a282012-08-03 12:35:54 -04005257 sizeof(struct lpfc_sli_ring), GFP_KERNEL);
5258 if (!phba->sli.ring)
5259 return -ENOMEM;
James Smart085c6472010-11-20 23:11:37 -05005260
James Smart09294d42013-04-17 20:16:05 -04005261 /*
5262 * It doesn't matter what family our adapter is in, we are
5263 * limited to 2 Pages, 512 SGEs, for our SGL.
5264 * There are going to be 2 reserved SGEs: 1 FCP cmnd + 1 FCP rsp
5265 */
5266 max_buf_size = (2 * SLI4_PAGE_SIZE);
5267 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SGL_SEG_CNT - 2)
5268 phba->cfg_sg_seg_cnt = LPFC_MAX_SGL_SEG_CNT - 2;
James Smart09294d42013-04-17 20:16:05 -04005269
James Smartda0436e2009-05-22 14:51:39 -04005270 /*
James Smart96f70772013-04-17 20:16:15 -04005271 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smartda0436e2009-05-22 14:51:39 -04005272 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smart28baac72010-02-12 14:42:03 -05005273 */
James Smart085c6472010-11-20 23:11:37 -05005274
James Smart96f70772013-04-17 20:16:15 -04005275 if (phba->cfg_enable_bg) {
5276 /*
5277 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
5278 * the FCP rsp, and a SGE for each. Sice we have no control
5279 * over how many protection data segments the SCSI Layer
5280 * will hand us (ie: there could be one for every block
5281 * in the IO), we just allocate enough SGEs to accomidate
5282 * our max amount and we need to limit lpfc_sg_seg_cnt to
5283 * minimize the risk of running out.
5284 */
5285 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5286 sizeof(struct fcp_rsp) + max_buf_size;
5287
5288 /* Total SGEs for scsi_sg_list and scsi_sg_prot_list */
5289 phba->cfg_total_seg_cnt = LPFC_MAX_SGL_SEG_CNT;
5290
5291 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SLI4_SEG_CNT_DIF)
5292 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SLI4_SEG_CNT_DIF;
5293 } else {
5294 /*
5295 * The scsi_buf for a regular I/O will hold the FCP cmnd,
5296 * the FCP rsp, a SGE for each, and a SGE for up to
5297 * cfg_sg_seg_cnt data segments.
5298 */
5299 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5300 sizeof(struct fcp_rsp) +
5301 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct sli4_sge));
5302
5303 /* Total SGEs for scsi_sg_list */
5304 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
5305 /*
5306 * NOTE: if (phba->cfg_sg_seg_cnt + 2) <= 256 we only need
5307 * to post 1 page for the SGL.
5308 */
James Smart085c6472010-11-20 23:11:37 -05005309 }
James Smartacd68592012-01-18 16:25:09 -05005310
James Smart96f70772013-04-17 20:16:15 -04005311 /* Initialize the host templates with the updated values. */
5312 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5313 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5314
5315 if (phba->cfg_sg_dma_buf_size <= LPFC_MIN_SG_SLI4_BUF_SZ)
5316 phba->cfg_sg_dma_buf_size = LPFC_MIN_SG_SLI4_BUF_SZ;
5317 else
5318 phba->cfg_sg_dma_buf_size =
5319 SLI4_PAGE_ALIGN(phba->cfg_sg_dma_buf_size);
5320
5321 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
5322 "9087 sg_tablesize:%d dmabuf_size:%d total_sge:%d\n",
5323 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
5324 phba->cfg_total_seg_cnt);
James Smartda0436e2009-05-22 14:51:39 -04005325
5326 /* Initialize buffer queue management fields */
5327 hbq_count = lpfc_sli_hbq_count();
5328 for (i = 0; i < hbq_count; ++i)
5329 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
5330 INIT_LIST_HEAD(&phba->rb_pend_list);
5331 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
5332 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
5333
5334 /*
5335 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
5336 */
5337 /* Initialize the Abort scsi buffer list used by driver */
5338 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
5339 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
5340 /* This abort list used by worker thread */
5341 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
5342
5343 /*
James Smart6d368e52011-05-24 11:44:12 -04005344 * Initialize driver internal slow-path work queues
James Smartda0436e2009-05-22 14:51:39 -04005345 */
5346
5347 /* Driver internel slow-path CQ Event pool */
5348 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
5349 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04005350 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04005351 /* Asynchronous event CQ Event work queue list */
5352 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
5353 /* Fast-path XRI aborted CQ Event work queue list */
5354 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
5355 /* Slow-path XRI aborted CQ Event work queue list */
5356 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
5357 /* Receive queue CQ Event work queue list */
5358 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
5359
James Smart6d368e52011-05-24 11:44:12 -04005360 /* Initialize extent block lists. */
5361 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
5362 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
5363 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
5364 INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
5365
James Smart448193b2015-12-16 18:12:05 -05005366 /* initialize optic_state to 0xFF */
5367 phba->sli4_hba.lnk_info.optic_state = 0xff;
5368
James Smartda0436e2009-05-22 14:51:39 -04005369 /* Initialize the driver internal SLI layer lists. */
5370 lpfc_sli_setup(phba);
5371 lpfc_sli_queue_setup(phba);
5372
5373 /* Allocate device driver memory */
5374 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
5375 if (rc)
5376 return -ENOMEM;
5377
James Smart2fcee4b2010-12-15 17:57:46 -05005378 /* IF Type 2 ports get initialized now. */
5379 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
5380 LPFC_SLI_INTF_IF_TYPE_2) {
5381 rc = lpfc_pci_function_reset(phba);
5382 if (unlikely(rc))
5383 return -ENODEV;
James Smart946727d2015-04-07 15:07:09 -04005384 phba->temp_sensor_support = 1;
James Smart2fcee4b2010-12-15 17:57:46 -05005385 }
5386
James Smartda0436e2009-05-22 14:51:39 -04005387 /* Create the bootstrap mailbox command */
5388 rc = lpfc_create_bootstrap_mbox(phba);
5389 if (unlikely(rc))
5390 goto out_free_mem;
5391
5392 /* Set up the host's endian order with the device. */
5393 rc = lpfc_setup_endian_order(phba);
5394 if (unlikely(rc))
5395 goto out_free_bsmbx;
5396
5397 /* Set up the hba's configuration parameters. */
5398 rc = lpfc_sli4_read_config(phba);
5399 if (unlikely(rc))
5400 goto out_free_bsmbx;
James Smartcff261f2013-12-17 20:29:47 -05005401 rc = lpfc_mem_alloc_active_rrq_pool_s4(phba);
5402 if (unlikely(rc))
5403 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04005404
James Smart2fcee4b2010-12-15 17:57:46 -05005405 /* IF Type 0 ports get initialized now. */
5406 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
5407 LPFC_SLI_INTF_IF_TYPE_0) {
5408 rc = lpfc_pci_function_reset(phba);
5409 if (unlikely(rc))
5410 goto out_free_bsmbx;
5411 }
James Smartda0436e2009-05-22 14:51:39 -04005412
James Smartcb5172e2010-03-15 11:25:07 -04005413 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
5414 GFP_KERNEL);
5415 if (!mboxq) {
5416 rc = -ENOMEM;
5417 goto out_free_bsmbx;
5418 }
5419
James Smartfedd3b72011-02-16 12:39:24 -05005420 /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
James Smartcb5172e2010-03-15 11:25:07 -04005421 lpfc_supported_pages(mboxq);
5422 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smartfedd3b72011-02-16 12:39:24 -05005423 if (!rc) {
5424 mqe = &mboxq->u.mqe;
5425 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
5426 LPFC_MAX_SUPPORTED_PAGES);
5427 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
5428 switch (pn_page[i]) {
5429 case LPFC_SLI4_PARAMETERS:
5430 phba->sli4_hba.pc_sli4_params.supported = 1;
5431 break;
5432 default:
5433 break;
5434 }
5435 }
5436 /* Read the port's SLI4 Parameters capabilities if supported. */
5437 if (phba->sli4_hba.pc_sli4_params.supported)
5438 rc = lpfc_pc_sli4_params_get(phba, mboxq);
5439 if (rc) {
5440 mempool_free(mboxq, phba->mbox_mem_pool);
5441 rc = -EIO;
5442 goto out_free_bsmbx;
James Smartcb5172e2010-03-15 11:25:07 -04005443 }
5444 }
James Smart65791f12016-07-06 12:35:56 -07005445
James Smartfedd3b72011-02-16 12:39:24 -05005446 /*
5447 * Get sli4 parameters that override parameters from Port capabilities.
James Smart6d368e52011-05-24 11:44:12 -04005448 * If this call fails, it isn't critical unless the SLI4 parameters come
5449 * back in conflict.
James Smartfedd3b72011-02-16 12:39:24 -05005450 */
James Smart6d368e52011-05-24 11:44:12 -04005451 rc = lpfc_get_sli4_parameters(phba, mboxq);
5452 if (rc) {
5453 if (phba->sli4_hba.extents_in_use &&
5454 phba->sli4_hba.rpi_hdrs_in_use) {
5455 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5456 "2999 Unsupported SLI4 Parameters "
5457 "Extents and RPI headers enabled.\n");
5458 goto out_free_bsmbx;
5459 }
5460 }
James Smartcb5172e2010-03-15 11:25:07 -04005461 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1ba981f2014-02-20 09:56:45 -05005462
5463 /* Verify OAS is supported */
5464 lpfc_sli4_oas_verify(phba);
5465 if (phba->cfg_fof)
5466 fof_vectors = 1;
5467
James Smart5350d872011-10-10 21:33:49 -04005468 /* Verify all the SLI4 queues */
5469 rc = lpfc_sli4_queue_verify(phba);
James Smartda0436e2009-05-22 14:51:39 -04005470 if (rc)
5471 goto out_free_bsmbx;
5472
5473 /* Create driver internal CQE event pool */
5474 rc = lpfc_sli4_cq_event_pool_create(phba);
5475 if (rc)
James Smart5350d872011-10-10 21:33:49 -04005476 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04005477
James Smart8a9d2e82012-05-09 21:16:12 -04005478 /* Initialize sgl lists per host */
5479 lpfc_init_sgl_list(phba);
5480
5481 /* Allocate and initialize active sgl array */
James Smartda0436e2009-05-22 14:51:39 -04005482 rc = lpfc_init_active_sgl_array(phba);
5483 if (rc) {
5484 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5485 "1430 Failed to initialize sgl list.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04005486 goto out_destroy_cq_event_pool;
James Smartda0436e2009-05-22 14:51:39 -04005487 }
James Smartda0436e2009-05-22 14:51:39 -04005488 rc = lpfc_sli4_init_rpi_hdrs(phba);
5489 if (rc) {
5490 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5491 "1432 Failed to initialize rpi headers.\n");
5492 goto out_free_active_sgl;
5493 }
5494
James Smarta93ff372010-10-22 11:06:08 -04005495 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
James Smart0c9ab6f2010-02-26 14:15:57 -05005496 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
5497 phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
5498 GFP_KERNEL);
5499 if (!phba->fcf.fcf_rr_bmask) {
5500 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5501 "2759 Failed allocate memory for FCF round "
5502 "robin failover bmask\n");
James Smart05580562011-05-24 11:40:48 -04005503 rc = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -05005504 goto out_remove_rpi_hdrs;
5505 }
5506
James Smart67d12732012-08-03 12:36:13 -04005507 phba->sli4_hba.fcp_eq_hdl =
5508 kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
James Smart1ba981f2014-02-20 09:56:45 -05005509 (fof_vectors + phba->cfg_fcp_io_channel)),
5510 GFP_KERNEL);
James Smart67d12732012-08-03 12:36:13 -04005511 if (!phba->sli4_hba.fcp_eq_hdl) {
5512 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5513 "2572 Failed allocate memory for "
5514 "fast-path per-EQ handle array\n");
5515 rc = -ENOMEM;
5516 goto out_free_fcf_rr_bmask;
James Smartda0436e2009-05-22 14:51:39 -04005517 }
5518
5519 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
James Smart1ba981f2014-02-20 09:56:45 -05005520 (fof_vectors +
5521 phba->cfg_fcp_io_channel)), GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005522 if (!phba->sli4_hba.msix_entries) {
5523 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5524 "2573 Failed allocate memory for msi-x "
5525 "interrupt vector entries\n");
James Smart05580562011-05-24 11:40:48 -04005526 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04005527 goto out_free_fcp_eq_hdl;
5528 }
5529
James Smart7bb03bb2013-04-17 20:19:16 -04005530 phba->sli4_hba.cpu_map = kzalloc((sizeof(struct lpfc_vector_map_info) *
5531 phba->sli4_hba.num_present_cpu),
5532 GFP_KERNEL);
5533 if (!phba->sli4_hba.cpu_map) {
5534 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5535 "3327 Failed allocate memory for msi-x "
5536 "interrupt vector mapping\n");
5537 rc = -ENOMEM;
5538 goto out_free_msix;
5539 }
James Smartb246de12013-05-31 17:03:07 -04005540 if (lpfc_used_cpu == NULL) {
5541 lpfc_used_cpu = kzalloc((sizeof(uint16_t) * lpfc_present_cpu),
5542 GFP_KERNEL);
5543 if (!lpfc_used_cpu) {
5544 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5545 "3335 Failed allocate memory for msi-x "
5546 "interrupt vector mapping\n");
5547 kfree(phba->sli4_hba.cpu_map);
5548 rc = -ENOMEM;
5549 goto out_free_msix;
5550 }
5551 for (i = 0; i < lpfc_present_cpu; i++)
5552 lpfc_used_cpu[i] = LPFC_VECTOR_MAP_EMPTY;
5553 }
5554
James Smart7bb03bb2013-04-17 20:19:16 -04005555 /* Initialize io channels for round robin */
5556 cpup = phba->sli4_hba.cpu_map;
5557 rc = 0;
5558 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
5559 cpup->channel_id = rc;
5560 rc++;
5561 if (rc >= phba->cfg_fcp_io_channel)
5562 rc = 0;
5563 }
5564
James Smart912e3ac2011-05-24 11:42:11 -04005565 /*
5566 * Enable sr-iov virtual functions if supported and configured
5567 * through the module parameter.
5568 */
5569 if (phba->cfg_sriov_nr_virtfn > 0) {
5570 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
5571 phba->cfg_sriov_nr_virtfn);
5572 if (rc) {
5573 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5574 "3020 Requested number of SR-IOV "
5575 "virtual functions (%d) is not "
5576 "supported\n",
5577 phba->cfg_sriov_nr_virtfn);
5578 phba->cfg_sriov_nr_virtfn = 0;
5579 }
5580 }
5581
James Smart5248a742011-07-22 18:37:06 -04005582 return 0;
James Smartda0436e2009-05-22 14:51:39 -04005583
James Smart7bb03bb2013-04-17 20:19:16 -04005584out_free_msix:
5585 kfree(phba->sli4_hba.msix_entries);
James Smartda0436e2009-05-22 14:51:39 -04005586out_free_fcp_eq_hdl:
5587 kfree(phba->sli4_hba.fcp_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05005588out_free_fcf_rr_bmask:
5589 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04005590out_remove_rpi_hdrs:
5591 lpfc_sli4_remove_rpi_hdrs(phba);
5592out_free_active_sgl:
5593 lpfc_free_active_sgl(phba);
James Smartda0436e2009-05-22 14:51:39 -04005594out_destroy_cq_event_pool:
5595 lpfc_sli4_cq_event_pool_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04005596out_free_bsmbx:
5597 lpfc_destroy_bootstrap_mbox(phba);
5598out_free_mem:
5599 lpfc_mem_free(phba);
5600 return rc;
5601}
5602
5603/**
5604 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
5605 * @phba: pointer to lpfc hba data structure.
5606 *
5607 * This routine is invoked to unset the driver internal resources set up
5608 * specific for supporting the SLI-4 HBA device it attached to.
5609 **/
5610static void
5611lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
5612{
5613 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
5614
James Smart7bb03bb2013-04-17 20:19:16 -04005615 /* Free memory allocated for msi-x interrupt vector to CPU mapping */
5616 kfree(phba->sli4_hba.cpu_map);
5617 phba->sli4_hba.num_present_cpu = 0;
5618 phba->sli4_hba.num_online_cpu = 0;
James Smart76fd07a2014-02-20 09:57:18 -05005619 phba->sli4_hba.curr_disp_cpu = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04005620
James Smartda0436e2009-05-22 14:51:39 -04005621 /* Free memory allocated for msi-x interrupt vector entries */
5622 kfree(phba->sli4_hba.msix_entries);
5623
5624 /* Free memory allocated for fast-path work queue handles */
5625 kfree(phba->sli4_hba.fcp_eq_hdl);
5626
5627 /* Free the allocated rpi headers. */
5628 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04005629 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04005630
James Smart0c9ab6f2010-02-26 14:15:57 -05005631 /* Free eligible FCF index bmask */
5632 kfree(phba->fcf.fcf_rr_bmask);
5633
James Smartda0436e2009-05-22 14:51:39 -04005634 /* Free the ELS sgl list */
5635 lpfc_free_active_sgl(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04005636 lpfc_free_els_sgl_list(phba);
James Smartda0436e2009-05-22 14:51:39 -04005637
James Smartda0436e2009-05-22 14:51:39 -04005638 /* Free the completion queue EQ event pool */
5639 lpfc_sli4_cq_event_release_all(phba);
5640 lpfc_sli4_cq_event_pool_destroy(phba);
5641
James Smart6d368e52011-05-24 11:44:12 -04005642 /* Release resource identifiers. */
5643 lpfc_sli4_dealloc_resource_identifiers(phba);
5644
James Smartda0436e2009-05-22 14:51:39 -04005645 /* Free the bsmbx region. */
5646 lpfc_destroy_bootstrap_mbox(phba);
5647
5648 /* Free the SLI Layer memory with SLI4 HBAs */
5649 lpfc_mem_free_all(phba);
5650
5651 /* Free the current connect table */
5652 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04005653 &phba->fcf_conn_rec_list, list) {
5654 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04005655 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04005656 }
James Smartda0436e2009-05-22 14:51:39 -04005657
5658 return;
5659}
5660
5661/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005662 * lpfc_init_api_table_setup - Set up init api function jump table
James Smart3772a992009-05-22 14:50:54 -04005663 * @phba: The hba struct for which this call is being executed.
5664 * @dev_grp: The HBA PCI-Device group number.
5665 *
5666 * This routine sets up the device INIT interface API function jump table
5667 * in @phba struct.
5668 *
5669 * Returns: 0 - success, -ENODEV - failure.
5670 **/
5671int
5672lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5673{
James Smart84d1b002010-02-12 14:42:33 -05005674 phba->lpfc_hba_init_link = lpfc_hba_init_link;
5675 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart7f860592011-03-11 16:05:52 -05005676 phba->lpfc_selective_reset = lpfc_selective_reset;
James Smart3772a992009-05-22 14:50:54 -04005677 switch (dev_grp) {
5678 case LPFC_PCI_DEV_LP:
5679 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
5680 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
5681 phba->lpfc_stop_port = lpfc_stop_port_s3;
5682 break;
James Smartda0436e2009-05-22 14:51:39 -04005683 case LPFC_PCI_DEV_OC:
5684 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
5685 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
5686 phba->lpfc_stop_port = lpfc_stop_port_s4;
5687 break;
James Smart3772a992009-05-22 14:50:54 -04005688 default:
5689 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5690 "1431 Invalid HBA PCI-device group: 0x%x\n",
5691 dev_grp);
5692 return -ENODEV;
5693 break;
5694 }
5695 return 0;
5696}
5697
5698/**
5699 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
5700 * @phba: pointer to lpfc hba data structure.
5701 *
5702 * This routine is invoked to set up the driver internal resources before the
5703 * device specific resource setup to support the HBA device it attached to.
5704 *
5705 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005706 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005707 * other values - error
5708 **/
5709static int
5710lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
5711{
5712 /*
5713 * Driver resources common to all SLI revisions
5714 */
James Smartea2151b2008-09-07 11:52:10 -04005715 atomic_set(&phba->fast_event_count, 0);
James Smart2e0fef82007-06-17 19:56:36 -05005716 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005717
James Smarte47c9092008-02-08 18:49:26 -05005718 /* Initialize ndlp management spinlock */
5719 spin_lock_init(&phba->ndlp_lock);
5720
James Smart2e0fef82007-06-17 19:56:36 -05005721 INIT_LIST_HEAD(&phba->port_list);
James Smart3772a992009-05-22 14:50:54 -04005722 INIT_LIST_HEAD(&phba->work_list);
James Smart84774a42008-08-24 21:50:06 -04005723 init_waitqueue_head(&phba->wait_4_mlo_m_q);
dea31012005-04-17 16:05:31 -05005724
James Smart3772a992009-05-22 14:50:54 -04005725 /* Initialize the wait queue head for the kernel thread */
5726 init_waitqueue_head(&phba->work_waitq);
James Smart858c9f62007-06-17 19:56:39 -05005727
James Smart3772a992009-05-22 14:50:54 -04005728 /* Initialize the scsi buffer list used by driver for scsi IO */
James Smarta40fc5f2013-04-17 20:17:40 -04005729 spin_lock_init(&phba->scsi_buf_list_get_lock);
5730 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_get);
5731 spin_lock_init(&phba->scsi_buf_list_put_lock);
5732 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
dea31012005-04-17 16:05:31 -05005733
James Smart3772a992009-05-22 14:50:54 -04005734 /* Initialize the fabric iocb list */
5735 INIT_LIST_HEAD(&phba->fabric_iocb_list);
dea31012005-04-17 16:05:31 -05005736
James Smart3772a992009-05-22 14:50:54 -04005737 /* Initialize list to save ELS buffers */
5738 INIT_LIST_HEAD(&phba->elsbuf);
dea31012005-04-17 16:05:31 -05005739
James Smart3772a992009-05-22 14:50:54 -04005740 /* Initialize FCF connection rec list */
5741 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
dea31012005-04-17 16:05:31 -05005742
James Smart1ba981f2014-02-20 09:56:45 -05005743 /* Initialize OAS configuration list */
5744 spin_lock_init(&phba->devicelock);
5745 INIT_LIST_HEAD(&phba->luns);
5746
James Smart3772a992009-05-22 14:50:54 -04005747 return 0;
5748}
dea31012005-04-17 16:05:31 -05005749
James Smart3772a992009-05-22 14:50:54 -04005750/**
5751 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
5752 * @phba: pointer to lpfc hba data structure.
5753 *
5754 * This routine is invoked to set up the driver internal resources after the
5755 * device specific resource setup to support the HBA device it attached to.
5756 *
5757 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005758 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005759 * other values - error
5760 **/
5761static int
5762lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
5763{
5764 int error;
5765
5766 /* Startup the kernel thread for this host adapter. */
5767 phba->worker_thread = kthread_run(lpfc_do_work, phba,
5768 "lpfc_worker_%d", phba->brd_no);
5769 if (IS_ERR(phba->worker_thread)) {
5770 error = PTR_ERR(phba->worker_thread);
5771 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005772 }
5773
James Smart3772a992009-05-22 14:50:54 -04005774 return 0;
5775}
5776
5777/**
5778 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
5779 * @phba: pointer to lpfc hba data structure.
5780 *
5781 * This routine is invoked to unset the driver internal resources set up after
5782 * the device specific resource setup for supporting the HBA device it
5783 * attached to.
5784 **/
5785static void
5786lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
5787{
5788 /* Stop kernel worker thread */
5789 kthread_stop(phba->worker_thread);
5790}
5791
5792/**
5793 * lpfc_free_iocb_list - Free iocb list.
5794 * @phba: pointer to lpfc hba data structure.
5795 *
5796 * This routine is invoked to free the driver's IOCB list and memory.
5797 **/
5798static void
5799lpfc_free_iocb_list(struct lpfc_hba *phba)
5800{
5801 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
5802
5803 spin_lock_irq(&phba->hbalock);
5804 list_for_each_entry_safe(iocbq_entry, iocbq_next,
5805 &phba->lpfc_iocb_list, list) {
5806 list_del(&iocbq_entry->list);
5807 kfree(iocbq_entry);
5808 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005809 }
James Smart3772a992009-05-22 14:50:54 -04005810 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005811
James Smart3772a992009-05-22 14:50:54 -04005812 return;
5813}
dea31012005-04-17 16:05:31 -05005814
James Smart3772a992009-05-22 14:50:54 -04005815/**
5816 * lpfc_init_iocb_list - Allocate and initialize iocb list.
5817 * @phba: pointer to lpfc hba data structure.
5818 *
5819 * This routine is invoked to allocate and initizlize the driver's IOCB
5820 * list and set up the IOCB tag array accordingly.
5821 *
5822 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005823 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005824 * other values - error
5825 **/
5826static int
5827lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
5828{
5829 struct lpfc_iocbq *iocbq_entry = NULL;
5830 uint16_t iotag;
5831 int i;
dea31012005-04-17 16:05:31 -05005832
5833 /* Initialize and populate the iocb list per host. */
5834 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04005835 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07005836 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05005837 if (iocbq_entry == NULL) {
5838 printk(KERN_ERR "%s: only allocated %d iocbs of "
5839 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07005840 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05005841 goto out_free_iocbq;
5842 }
5843
James Bottomley604a3e32005-10-29 10:28:33 -05005844 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
5845 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04005846 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05005847 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04005848 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05005849 goto out_free_iocbq;
5850 }
James Smart6d368e52011-05-24 11:44:12 -04005851 iocbq_entry->sli4_lxritag = NO_XRI;
James Smart3772a992009-05-22 14:50:54 -04005852 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05005853
5854 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005855 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
5856 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05005857 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005858 }
5859
James Smart3772a992009-05-22 14:50:54 -04005860 return 0;
5861
5862out_free_iocbq:
5863 lpfc_free_iocb_list(phba);
5864
5865 return -ENOMEM;
5866}
5867
5868/**
James Smart8a9d2e82012-05-09 21:16:12 -04005869 * lpfc_free_sgl_list - Free a given sgl list.
James Smartda0436e2009-05-22 14:51:39 -04005870 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -04005871 * @sglq_list: pointer to the head of sgl list.
James Smartda0436e2009-05-22 14:51:39 -04005872 *
James Smart8a9d2e82012-05-09 21:16:12 -04005873 * This routine is invoked to free a give sgl list and memory.
James Smartda0436e2009-05-22 14:51:39 -04005874 **/
James Smart8a9d2e82012-05-09 21:16:12 -04005875void
5876lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
James Smartda0436e2009-05-22 14:51:39 -04005877{
5878 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart8a9d2e82012-05-09 21:16:12 -04005879
5880 list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
5881 list_del(&sglq_entry->list);
5882 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
5883 kfree(sglq_entry);
5884 }
5885}
5886
5887/**
5888 * lpfc_free_els_sgl_list - Free els sgl list.
5889 * @phba: pointer to lpfc hba data structure.
5890 *
5891 * This routine is invoked to free the driver's els sgl list and memory.
5892 **/
5893static void
5894lpfc_free_els_sgl_list(struct lpfc_hba *phba)
5895{
James Smartda0436e2009-05-22 14:51:39 -04005896 LIST_HEAD(sglq_list);
James Smartdafe8ce2014-09-03 12:56:40 -04005897 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smartda0436e2009-05-22 14:51:39 -04005898
James Smart8a9d2e82012-05-09 21:16:12 -04005899 /* Retrieve all els sgls from driver list */
James Smartda0436e2009-05-22 14:51:39 -04005900 spin_lock_irq(&phba->hbalock);
James Smartdafe8ce2014-09-03 12:56:40 -04005901 spin_lock(&pring->ring_lock);
James Smartda0436e2009-05-22 14:51:39 -04005902 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
James Smartdafe8ce2014-09-03 12:56:40 -04005903 spin_unlock(&pring->ring_lock);
James Smartda0436e2009-05-22 14:51:39 -04005904 spin_unlock_irq(&phba->hbalock);
5905
James Smart8a9d2e82012-05-09 21:16:12 -04005906 /* Now free the sgl list */
5907 lpfc_free_sgl_list(phba, &sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04005908}
5909
5910/**
5911 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
5912 * @phba: pointer to lpfc hba data structure.
5913 *
5914 * This routine is invoked to allocate the driver's active sgl memory.
5915 * This array will hold the sglq_entry's for active IOs.
5916 **/
5917static int
5918lpfc_init_active_sgl_array(struct lpfc_hba *phba)
5919{
5920 int size;
5921 size = sizeof(struct lpfc_sglq *);
5922 size *= phba->sli4_hba.max_cfg_param.max_xri;
5923
5924 phba->sli4_hba.lpfc_sglq_active_list =
5925 kzalloc(size, GFP_KERNEL);
5926 if (!phba->sli4_hba.lpfc_sglq_active_list)
5927 return -ENOMEM;
5928 return 0;
5929}
5930
5931/**
5932 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
5933 * @phba: pointer to lpfc hba data structure.
5934 *
5935 * This routine is invoked to walk through the array of active sglq entries
5936 * and free all of the resources.
5937 * This is just a place holder for now.
5938 **/
5939static void
5940lpfc_free_active_sgl(struct lpfc_hba *phba)
5941{
5942 kfree(phba->sli4_hba.lpfc_sglq_active_list);
5943}
5944
5945/**
5946 * lpfc_init_sgl_list - Allocate and initialize sgl list.
5947 * @phba: pointer to lpfc hba data structure.
5948 *
5949 * This routine is invoked to allocate and initizlize the driver's sgl
5950 * list and set up the sgl xritag tag array accordingly.
5951 *
James Smartda0436e2009-05-22 14:51:39 -04005952 **/
James Smart8a9d2e82012-05-09 21:16:12 -04005953static void
James Smartda0436e2009-05-22 14:51:39 -04005954lpfc_init_sgl_list(struct lpfc_hba *phba)
5955{
James Smartda0436e2009-05-22 14:51:39 -04005956 /* Initialize and populate the sglq list per host/VF. */
5957 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
5958 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
5959
James Smart8a9d2e82012-05-09 21:16:12 -04005960 /* els xri-sgl book keeping */
5961 phba->sli4_hba.els_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04005962
James Smart8a9d2e82012-05-09 21:16:12 -04005963 /* scsi xri-buffer book keeping */
James Smartda0436e2009-05-22 14:51:39 -04005964 phba->sli4_hba.scsi_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04005965}
5966
5967/**
5968 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
5969 * @phba: pointer to lpfc hba data structure.
5970 *
5971 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -04005972 * port for those SLI4 ports that do not support extents. This routine
James Smartda0436e2009-05-22 14:51:39 -04005973 * posts a PAGE_SIZE memory region to the port to hold up to
James Smart88a2cfb2011-07-22 18:36:33 -04005974 * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
5975 * and should be called only when interrupts are disabled.
James Smartda0436e2009-05-22 14:51:39 -04005976 *
5977 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005978 * 0 - successful
James Smart88a2cfb2011-07-22 18:36:33 -04005979 * -ERROR - otherwise.
James Smartda0436e2009-05-22 14:51:39 -04005980 **/
5981int
5982lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
5983{
5984 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04005985 struct lpfc_rpi_hdr *rpi_hdr;
5986
5987 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
James Smartff78d8f2011-12-13 13:21:35 -05005988 if (!phba->sli4_hba.rpi_hdrs_in_use)
James Smart6d368e52011-05-24 11:44:12 -04005989 return rc;
James Smart6d368e52011-05-24 11:44:12 -04005990 if (phba->sli4_hba.extents_in_use)
5991 return -EIO;
James Smartda0436e2009-05-22 14:51:39 -04005992
5993 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
5994 if (!rpi_hdr) {
5995 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5996 "0391 Error during rpi post operation\n");
5997 lpfc_sli4_remove_rpis(phba);
5998 rc = -ENODEV;
5999 }
6000
6001 return rc;
6002}
6003
6004/**
6005 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
6006 * @phba: pointer to lpfc hba data structure.
6007 *
6008 * This routine is invoked to allocate a single 4KB memory region to
6009 * support rpis and stores them in the phba. This single region
6010 * provides support for up to 64 rpis. The region is used globally
6011 * by the device.
6012 *
6013 * Returns:
6014 * A valid rpi hdr on success.
6015 * A NULL pointer on any failure.
6016 **/
6017struct lpfc_rpi_hdr *
6018lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
6019{
6020 uint16_t rpi_limit, curr_rpi_range;
6021 struct lpfc_dmabuf *dmabuf;
6022 struct lpfc_rpi_hdr *rpi_hdr;
James Smart9589b0622011-04-16 11:03:17 -04006023 uint32_t rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04006024
James Smart6d368e52011-05-24 11:44:12 -04006025 /*
6026 * If the SLI4 port supports extents, posting the rpi header isn't
6027 * required. Set the expected maximum count and let the actual value
6028 * get set when extents are fully allocated.
6029 */
6030 if (!phba->sli4_hba.rpi_hdrs_in_use)
6031 return NULL;
6032 if (phba->sli4_hba.extents_in_use)
6033 return NULL;
6034
6035 /* The limit on the logical index is just the max_rpi count. */
James Smartda0436e2009-05-22 14:51:39 -04006036 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
James Smart6d368e52011-05-24 11:44:12 -04006037 phba->sli4_hba.max_cfg_param.max_rpi - 1;
James Smartda0436e2009-05-22 14:51:39 -04006038
6039 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04006040 /*
6041 * Establish the starting RPI in this header block. The starting
6042 * rpi is normalized to a zero base because the physical rpi is
6043 * port based.
6044 */
James Smart97f2ecf2012-03-01 22:35:23 -05006045 curr_rpi_range = phba->sli4_hba.next_rpi;
James Smartda0436e2009-05-22 14:51:39 -04006046 spin_unlock_irq(&phba->hbalock);
6047
6048 /*
6049 * The port has a limited number of rpis. The increment here
6050 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
6051 * and to allow the full max_rpi range per port.
6052 */
6053 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
James Smart9589b0622011-04-16 11:03:17 -04006054 rpi_count = rpi_limit - curr_rpi_range;
6055 else
6056 rpi_count = LPFC_RPI_HDR_COUNT;
James Smartda0436e2009-05-22 14:51:39 -04006057
James Smart6d368e52011-05-24 11:44:12 -04006058 if (!rpi_count)
6059 return NULL;
James Smartda0436e2009-05-22 14:51:39 -04006060 /*
6061 * First allocate the protocol header region for the port. The
6062 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
6063 */
6064 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
6065 if (!dmabuf)
6066 return NULL;
6067
Joe Perches1aee3832014-09-03 12:56:12 -04006068 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
6069 LPFC_HDR_TEMPLATE_SIZE,
6070 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04006071 if (!dmabuf->virt) {
6072 rpi_hdr = NULL;
6073 goto err_free_dmabuf;
6074 }
6075
James Smartda0436e2009-05-22 14:51:39 -04006076 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
6077 rpi_hdr = NULL;
6078 goto err_free_coherent;
6079 }
6080
6081 /* Save the rpi header data for cleanup later. */
6082 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
6083 if (!rpi_hdr)
6084 goto err_free_coherent;
6085
6086 rpi_hdr->dmabuf = dmabuf;
6087 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
6088 rpi_hdr->page_count = 1;
6089 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04006090
6091 /* The rpi_hdr stores the logical index only. */
6092 rpi_hdr->start_rpi = curr_rpi_range;
James Smartda0436e2009-05-22 14:51:39 -04006093 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
6094
6095 /*
James Smart6d368e52011-05-24 11:44:12 -04006096 * The next_rpi stores the next logical module-64 rpi value used
6097 * to post physical rpis in subsequent rpi postings.
James Smartda0436e2009-05-22 14:51:39 -04006098 */
James Smart9589b0622011-04-16 11:03:17 -04006099 phba->sli4_hba.next_rpi += rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04006100 spin_unlock_irq(&phba->hbalock);
6101 return rpi_hdr;
6102
6103 err_free_coherent:
6104 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
6105 dmabuf->virt, dmabuf->phys);
6106 err_free_dmabuf:
6107 kfree(dmabuf);
6108 return NULL;
6109}
6110
6111/**
6112 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
6113 * @phba: pointer to lpfc hba data structure.
6114 *
6115 * This routine is invoked to remove all memory resources allocated
James Smart6d368e52011-05-24 11:44:12 -04006116 * to support rpis for SLI4 ports not supporting extents. This routine
6117 * presumes the caller has released all rpis consumed by fabric or port
6118 * logins and is prepared to have the header pages removed.
James Smartda0436e2009-05-22 14:51:39 -04006119 **/
6120void
6121lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
6122{
6123 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
6124
James Smart6d368e52011-05-24 11:44:12 -04006125 if (!phba->sli4_hba.rpi_hdrs_in_use)
6126 goto exit;
6127
James Smartda0436e2009-05-22 14:51:39 -04006128 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
6129 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
6130 list_del(&rpi_hdr->list);
6131 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
6132 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
6133 kfree(rpi_hdr->dmabuf);
6134 kfree(rpi_hdr);
6135 }
James Smart6d368e52011-05-24 11:44:12 -04006136 exit:
6137 /* There are no rpis available to the port now. */
6138 phba->sli4_hba.next_rpi = 0;
James Smartda0436e2009-05-22 14:51:39 -04006139}
6140
6141/**
James Smart3772a992009-05-22 14:50:54 -04006142 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
6143 * @pdev: pointer to pci device data structure.
6144 *
6145 * This routine is invoked to allocate the driver hba data structure for an
6146 * HBA device. If the allocation is successful, the phba reference to the
6147 * PCI device data structure is set.
6148 *
6149 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006150 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04006151 * NULL - error
6152 **/
6153static struct lpfc_hba *
6154lpfc_hba_alloc(struct pci_dev *pdev)
6155{
6156 struct lpfc_hba *phba;
6157
6158 /* Allocate memory for HBA structure */
6159 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
6160 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02006161 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04006162 return NULL;
6163 }
6164
6165 /* Set reference to PCI device in HBA structure */
6166 phba->pcidev = pdev;
6167
6168 /* Assign an unused board number */
6169 phba->brd_no = lpfc_get_instance();
6170 if (phba->brd_no < 0) {
6171 kfree(phba);
6172 return NULL;
6173 }
James Smart65791f12016-07-06 12:35:56 -07006174 phba->eratt_poll_interval = LPFC_ERATT_POLL_INTERVAL;
James Smart3772a992009-05-22 14:50:54 -04006175
James Smart4fede782010-01-26 23:08:55 -05006176 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04006177 INIT_LIST_HEAD(&phba->ct_ev_waiters);
6178
James Smart3772a992009-05-22 14:50:54 -04006179 return phba;
6180}
6181
6182/**
6183 * lpfc_hba_free - Free driver hba data structure with a device.
6184 * @phba: pointer to lpfc hba data structure.
6185 *
6186 * This routine is invoked to free the driver hba data structure with an
6187 * HBA device.
6188 **/
6189static void
6190lpfc_hba_free(struct lpfc_hba *phba)
6191{
6192 /* Release the driver assigned board number */
6193 idr_remove(&lpfc_hba_index, phba->brd_no);
6194
James Smart2a76a282012-08-03 12:35:54 -04006195 /* Free memory allocated with sli rings */
6196 kfree(phba->sli.ring);
6197 phba->sli.ring = NULL;
6198
James Smart3772a992009-05-22 14:50:54 -04006199 kfree(phba);
6200 return;
6201}
6202
6203/**
6204 * lpfc_create_shost - Create hba physical port with associated scsi host.
6205 * @phba: pointer to lpfc hba data structure.
6206 *
6207 * This routine is invoked to create HBA physical port and associate a SCSI
6208 * host with it.
6209 *
6210 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006211 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006212 * other values - error
6213 **/
6214static int
6215lpfc_create_shost(struct lpfc_hba *phba)
6216{
6217 struct lpfc_vport *vport;
6218 struct Scsi_Host *shost;
6219
6220 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05006221 phba->fc_edtov = FF_DEF_EDTOV;
6222 phba->fc_ratov = FF_DEF_RATOV;
6223 phba->fc_altov = FF_DEF_ALTOV;
6224 phba->fc_arbtov = FF_DEF_ARBTOV;
6225
James Smartd7c47992010-06-08 18:31:54 -04006226 atomic_set(&phba->sdev_cnt, 0);
James Smart3de2a652007-08-02 11:09:59 -04006227 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05006228 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04006229 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05006230
6231 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05006232 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05006233 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04006234 /* Put reference to SCSI host to driver's device private data */
6235 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05006236
James Smart4258e982015-12-16 18:11:58 -05006237 /*
6238 * At this point we are fully registered with PSA. In addition,
6239 * any initial discovery should be completed.
6240 */
6241 vport->load_flag |= FC_ALLOW_FDMI;
James Smart8663cbb2016-03-31 14:12:33 -07006242 if (phba->cfg_enable_SmartSAN ||
6243 (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
James Smart4258e982015-12-16 18:11:58 -05006244
6245 /* Setup appropriate attribute masks */
6246 vport->fdmi_hba_mask = LPFC_FDMI2_HBA_ATTR;
James Smart8663cbb2016-03-31 14:12:33 -07006247 if (phba->cfg_enable_SmartSAN)
James Smart4258e982015-12-16 18:11:58 -05006248 vport->fdmi_port_mask = LPFC_FDMI2_SMART_ATTR;
6249 else
6250 vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
6251 }
James Smart3772a992009-05-22 14:50:54 -04006252 return 0;
6253}
dea31012005-04-17 16:05:31 -05006254
James Smart3772a992009-05-22 14:50:54 -04006255/**
6256 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
6257 * @phba: pointer to lpfc hba data structure.
6258 *
6259 * This routine is invoked to destroy HBA physical port and the associated
6260 * SCSI host.
6261 **/
6262static void
6263lpfc_destroy_shost(struct lpfc_hba *phba)
6264{
6265 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04006266
James Smart3772a992009-05-22 14:50:54 -04006267 /* Destroy physical port that associated with the SCSI host */
6268 destroy_port(vport);
6269
6270 return;
6271}
6272
6273/**
6274 * lpfc_setup_bg - Setup Block guard structures and debug areas.
6275 * @phba: pointer to lpfc hba data structure.
6276 * @shost: the shost to be used to detect Block guard settings.
6277 *
6278 * This routine sets up the local Block guard protocol settings for @shost.
6279 * This routine also allocates memory for debugging bg buffers.
6280 **/
6281static void
6282lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
6283{
James Smartbbeb79b2012-06-12 13:54:27 -04006284 uint32_t old_mask;
6285 uint32_t old_guard;
6286
James Smart3772a992009-05-22 14:50:54 -04006287 int pagecnt = 10;
6288 if (lpfc_prot_mask && lpfc_prot_guard) {
6289 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6290 "1478 Registering BlockGuard with the "
6291 "SCSI layer\n");
James Smartbbeb79b2012-06-12 13:54:27 -04006292
6293 old_mask = lpfc_prot_mask;
6294 old_guard = lpfc_prot_guard;
6295
6296 /* Only allow supported values */
6297 lpfc_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
6298 SHOST_DIX_TYPE0_PROTECTION |
6299 SHOST_DIX_TYPE1_PROTECTION);
6300 lpfc_prot_guard &= (SHOST_DIX_GUARD_IP | SHOST_DIX_GUARD_CRC);
6301
6302 /* DIF Type 1 protection for profiles AST1/C1 is end to end */
6303 if (lpfc_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
6304 lpfc_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
6305
6306 if (lpfc_prot_mask && lpfc_prot_guard) {
6307 if ((old_mask != lpfc_prot_mask) ||
6308 (old_guard != lpfc_prot_guard))
6309 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6310 "1475 Registering BlockGuard with the "
6311 "SCSI layer: mask %d guard %d\n",
6312 lpfc_prot_mask, lpfc_prot_guard);
6313
6314 scsi_host_set_prot(shost, lpfc_prot_mask);
6315 scsi_host_set_guard(shost, lpfc_prot_guard);
6316 } else
6317 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6318 "1479 Not Registering BlockGuard with the SCSI "
6319 "layer, Bad protection parameters: %d %d\n",
6320 old_mask, old_guard);
James Smart98c9ea52007-10-27 13:37:33 -04006321 }
James Smartbbeb79b2012-06-12 13:54:27 -04006322
James Smart81301a92008-12-04 22:39:46 -05006323 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05006324 while (pagecnt) {
6325 spin_lock_init(&_dump_buf_lock);
6326 _dump_buf_data =
6327 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
6328 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04006329 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6330 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04006331 "_dump_buf_data at 0x%p\n",
6332 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05006333 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04006334 memset(_dump_buf_data, 0,
6335 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05006336 break;
James Smart3772a992009-05-22 14:50:54 -04006337 } else
James Smart81301a92008-12-04 22:39:46 -05006338 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05006339 }
James Smart81301a92008-12-04 22:39:46 -05006340 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04006341 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6342 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04006343 "memory for hexdump\n");
6344 } else
James Smart6a9c52c2009-10-02 15:16:51 -04006345 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6346 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05006347 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05006348 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05006349 while (pagecnt) {
6350 _dump_buf_dif =
6351 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
6352 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04006353 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6354 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04006355 "_dump_buf_dif at 0x%p\n",
6356 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05006357 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04006358 memset(_dump_buf_dif, 0,
6359 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05006360 break;
James Smart3772a992009-05-22 14:50:54 -04006361 } else
James Smart81301a92008-12-04 22:39:46 -05006362 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05006363 }
James Smart81301a92008-12-04 22:39:46 -05006364 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04006365 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6366 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04006367 "memory for hexdump\n");
6368 } else
James Smart6a9c52c2009-10-02 15:16:51 -04006369 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6370 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04006371 _dump_buf_dif);
6372}
6373
6374/**
6375 * lpfc_post_init_setup - Perform necessary device post initialization setup.
6376 * @phba: pointer to lpfc hba data structure.
6377 *
6378 * This routine is invoked to perform all the necessary post initialization
6379 * setup for the device.
6380 **/
6381static void
6382lpfc_post_init_setup(struct lpfc_hba *phba)
6383{
6384 struct Scsi_Host *shost;
6385 struct lpfc_adapter_event_header adapter_event;
6386
6387 /* Get the default values for Model Name and Description */
6388 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
6389
6390 /*
6391 * hba setup may have changed the hba_queue_depth so we need to
6392 * adjust the value of can_queue.
6393 */
6394 shost = pci_get_drvdata(phba->pcidev);
6395 shost->can_queue = phba->cfg_hba_queue_depth - 10;
6396 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
6397 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05006398
6399 lpfc_host_attrib_init(shost);
6400
James Smart2e0fef82007-06-17 19:56:36 -05006401 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
6402 spin_lock_irq(shost->host_lock);
6403 lpfc_poll_start_timer(phba);
6404 spin_unlock_irq(shost->host_lock);
6405 }
James Smart8f6d98d2006-08-01 07:34:00 -04006406
James Smart93996272008-08-24 21:50:30 -04006407 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6408 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04006409 /* Send board arrival event to upper layer */
6410 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
6411 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
6412 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04006413 sizeof(adapter_event),
6414 (char *) &adapter_event,
6415 LPFC_NL_VENDOR_ID);
6416 return;
6417}
6418
6419/**
6420 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
6421 * @phba: pointer to lpfc hba data structure.
6422 *
6423 * This routine is invoked to set up the PCI device memory space for device
6424 * with SLI-3 interface spec.
6425 *
6426 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006427 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006428 * other values - error
6429 **/
6430static int
6431lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
6432{
6433 struct pci_dev *pdev;
6434 unsigned long bar0map_len, bar2map_len;
6435 int i, hbq_count;
6436 void *ptr;
6437 int error = -ENODEV;
6438
6439 /* Obtain PCI device reference */
6440 if (!phba->pcidev)
6441 return error;
6442 else
6443 pdev = phba->pcidev;
6444
6445 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05006446 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
6447 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
6448 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
6449 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smart3772a992009-05-22 14:50:54 -04006450 return error;
Michael Reed8e685972009-09-18 12:02:05 -05006451 }
6452 }
James Smart3772a992009-05-22 14:50:54 -04006453
6454 /* Get the bus address of Bar0 and Bar2 and the number of bytes
6455 * required by each mapping.
6456 */
6457 phba->pci_bar0_map = pci_resource_start(pdev, 0);
6458 bar0map_len = pci_resource_len(pdev, 0);
6459
6460 phba->pci_bar2_map = pci_resource_start(pdev, 2);
6461 bar2map_len = pci_resource_len(pdev, 2);
6462
6463 /* Map HBA SLIM to a kernel virtual address. */
6464 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
6465 if (!phba->slim_memmap_p) {
6466 dev_printk(KERN_ERR, &pdev->dev,
6467 "ioremap failed for SLIM memory.\n");
6468 goto out;
6469 }
6470
6471 /* Map HBA Control Registers to a kernel virtual address. */
6472 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
6473 if (!phba->ctrl_regs_memmap_p) {
6474 dev_printk(KERN_ERR, &pdev->dev,
6475 "ioremap failed for HBA control registers.\n");
6476 goto out_iounmap_slim;
6477 }
6478
6479 /* Allocate memory for SLI-2 structures */
Joe Perches1aee3832014-09-03 12:56:12 -04006480 phba->slim2p.virt = dma_zalloc_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6481 &phba->slim2p.phys, GFP_KERNEL);
James Smart3772a992009-05-22 14:50:54 -04006482 if (!phba->slim2p.virt)
6483 goto out_iounmap;
6484
James Smart3772a992009-05-22 14:50:54 -04006485 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04006486 phba->mbox_ext = (phba->slim2p.virt +
6487 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04006488 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
6489 phba->IOCBs = (phba->slim2p.virt +
6490 offsetof(struct lpfc_sli2_slim, IOCBs));
6491
6492 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
6493 lpfc_sli_hbq_size(),
6494 &phba->hbqslimp.phys,
6495 GFP_KERNEL);
6496 if (!phba->hbqslimp.virt)
6497 goto out_free_slim;
6498
6499 hbq_count = lpfc_sli_hbq_count();
6500 ptr = phba->hbqslimp.virt;
6501 for (i = 0; i < hbq_count; ++i) {
6502 phba->hbqs[i].hbq_virt = ptr;
6503 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
6504 ptr += (lpfc_hbq_defs[i]->entry_count *
6505 sizeof(struct lpfc_hbq_entry));
6506 }
6507 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
6508 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
6509
6510 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
6511
6512 INIT_LIST_HEAD(&phba->rb_pend_list);
6513
6514 phba->MBslimaddr = phba->slim_memmap_p;
6515 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
6516 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
6517 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
6518 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04006519
dea31012005-04-17 16:05:31 -05006520 return 0;
6521
dea31012005-04-17 16:05:31 -05006522out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04006523 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6524 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05006525out_iounmap:
6526 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05006527out_iounmap_slim:
dea31012005-04-17 16:05:31 -05006528 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05006529out:
6530 return error;
6531}
6532
James Smarte59058c2008-08-24 21:49:00 -04006533/**
James Smart3772a992009-05-22 14:50:54 -04006534 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
6535 * @phba: pointer to lpfc hba data structure.
6536 *
6537 * This routine is invoked to unset the PCI device memory space for device
6538 * with SLI-3 interface spec.
6539 **/
6540static void
6541lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
6542{
6543 struct pci_dev *pdev;
6544
6545 /* Obtain PCI device reference */
6546 if (!phba->pcidev)
6547 return;
6548 else
6549 pdev = phba->pcidev;
6550
6551 /* Free coherent DMA memory allocated */
6552 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
6553 phba->hbqslimp.virt, phba->hbqslimp.phys);
6554 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6555 phba->slim2p.virt, phba->slim2p.phys);
6556
6557 /* I/O memory unmap */
6558 iounmap(phba->ctrl_regs_memmap_p);
6559 iounmap(phba->slim_memmap_p);
6560
6561 return;
6562}
6563
6564/**
James Smartda0436e2009-05-22 14:51:39 -04006565 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
6566 * @phba: pointer to lpfc hba data structure.
6567 *
6568 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
6569 * done and check status.
6570 *
6571 * Return 0 if successful, otherwise -ENODEV.
6572 **/
6573int
6574lpfc_sli4_post_status_check(struct lpfc_hba *phba)
6575{
James Smart2fcee4b2010-12-15 17:57:46 -05006576 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
6577 struct lpfc_register reg_data;
6578 int i, port_error = 0;
6579 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04006580
James Smart9940b972011-03-11 16:06:12 -05006581 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
6582 memset(&reg_data, 0, sizeof(reg_data));
James Smart2fcee4b2010-12-15 17:57:46 -05006583 if (!phba->sli4_hba.PSMPHRregaddr)
James Smartda0436e2009-05-22 14:51:39 -04006584 return -ENODEV;
6585
James Smartda0436e2009-05-22 14:51:39 -04006586 /* Wait up to 30 seconds for the SLI Port POST done and ready */
6587 for (i = 0; i < 3000; i++) {
James Smart9940b972011-03-11 16:06:12 -05006588 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
6589 &portsmphr_reg.word0) ||
6590 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
James Smart2fcee4b2010-12-15 17:57:46 -05006591 /* Port has a fatal POST error, break out */
James Smartda0436e2009-05-22 14:51:39 -04006592 port_error = -ENODEV;
6593 break;
6594 }
James Smart2fcee4b2010-12-15 17:57:46 -05006595 if (LPFC_POST_STAGE_PORT_READY ==
6596 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
James Smartda0436e2009-05-22 14:51:39 -04006597 break;
James Smartda0436e2009-05-22 14:51:39 -04006598 msleep(10);
6599 }
6600
James Smart2fcee4b2010-12-15 17:57:46 -05006601 /*
6602 * If there was a port error during POST, then don't proceed with
6603 * other register reads as the data may not be valid. Just exit.
6604 */
6605 if (port_error) {
James Smartda0436e2009-05-22 14:51:39 -04006606 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05006607 "1408 Port Failed POST - portsmphr=0x%x, "
6608 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
6609 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
6610 portsmphr_reg.word0,
6611 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
6612 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
6613 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
6614 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
6615 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
6616 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
6617 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
6618 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
6619 } else {
James Smart28baac72010-02-12 14:42:03 -05006620 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05006621 "2534 Device Info: SLIFamily=0x%x, "
6622 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
6623 "SLIHint_2=0x%x, FT=0x%x\n",
James Smart28baac72010-02-12 14:42:03 -05006624 bf_get(lpfc_sli_intf_sli_family,
6625 &phba->sli4_hba.sli_intf),
6626 bf_get(lpfc_sli_intf_slirev,
6627 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05006628 bf_get(lpfc_sli_intf_if_type,
James Smart28baac72010-02-12 14:42:03 -05006629 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05006630 bf_get(lpfc_sli_intf_sli_hint1,
6631 &phba->sli4_hba.sli_intf),
6632 bf_get(lpfc_sli_intf_sli_hint2,
6633 &phba->sli4_hba.sli_intf),
6634 bf_get(lpfc_sli_intf_func_type,
James Smart28baac72010-02-12 14:42:03 -05006635 &phba->sli4_hba.sli_intf));
James Smart2fcee4b2010-12-15 17:57:46 -05006636 /*
6637 * Check for other Port errors during the initialization
6638 * process. Fail the load if the port did not come up
6639 * correctly.
6640 */
6641 if_type = bf_get(lpfc_sli_intf_if_type,
6642 &phba->sli4_hba.sli_intf);
6643 switch (if_type) {
6644 case LPFC_SLI_INTF_IF_TYPE_0:
6645 phba->sli4_hba.ue_mask_lo =
6646 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
6647 phba->sli4_hba.ue_mask_hi =
6648 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
6649 uerrlo_reg.word0 =
6650 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
6651 uerrhi_reg.word0 =
6652 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
6653 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
6654 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
6655 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6656 "1422 Unrecoverable Error "
6657 "Detected during POST "
6658 "uerr_lo_reg=0x%x, "
6659 "uerr_hi_reg=0x%x, "
6660 "ue_mask_lo_reg=0x%x, "
6661 "ue_mask_hi_reg=0x%x\n",
6662 uerrlo_reg.word0,
6663 uerrhi_reg.word0,
6664 phba->sli4_hba.ue_mask_lo,
6665 phba->sli4_hba.ue_mask_hi);
6666 port_error = -ENODEV;
6667 }
6668 break;
6669 case LPFC_SLI_INTF_IF_TYPE_2:
6670 /* Final checks. The port status should be clean. */
James Smart9940b972011-03-11 16:06:12 -05006671 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
6672 &reg_data.word0) ||
James Smart05580562011-05-24 11:40:48 -04006673 (bf_get(lpfc_sliport_status_err, &reg_data) &&
6674 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
James Smart2fcee4b2010-12-15 17:57:46 -05006675 phba->work_status[0] =
6676 readl(phba->sli4_hba.u.if_type2.
6677 ERR1regaddr);
6678 phba->work_status[1] =
6679 readl(phba->sli4_hba.u.if_type2.
6680 ERR2regaddr);
6681 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05006682 "2888 Unrecoverable port error "
6683 "following POST: port status reg "
6684 "0x%x, port_smphr reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05006685 "error 1=0x%x, error 2=0x%x\n",
6686 reg_data.word0,
6687 portsmphr_reg.word0,
6688 phba->work_status[0],
6689 phba->work_status[1]);
6690 port_error = -ENODEV;
6691 }
6692 break;
6693 case LPFC_SLI_INTF_IF_TYPE_1:
6694 default:
6695 break;
6696 }
James Smart28baac72010-02-12 14:42:03 -05006697 }
James Smartda0436e2009-05-22 14:51:39 -04006698 return port_error;
6699}
6700
6701/**
6702 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
6703 * @phba: pointer to lpfc hba data structure.
James Smart2fcee4b2010-12-15 17:57:46 -05006704 * @if_type: The SLI4 interface type getting configured.
James Smartda0436e2009-05-22 14:51:39 -04006705 *
6706 * This routine is invoked to set up SLI4 BAR0 PCI config space register
6707 * memory map.
6708 **/
6709static void
James Smart2fcee4b2010-12-15 17:57:46 -05006710lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04006711{
James Smart2fcee4b2010-12-15 17:57:46 -05006712 switch (if_type) {
6713 case LPFC_SLI_INTF_IF_TYPE_0:
6714 phba->sli4_hba.u.if_type0.UERRLOregaddr =
6715 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
6716 phba->sli4_hba.u.if_type0.UERRHIregaddr =
6717 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
6718 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
6719 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
6720 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
6721 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
6722 phba->sli4_hba.SLIINTFregaddr =
6723 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
6724 break;
6725 case LPFC_SLI_INTF_IF_TYPE_2:
6726 phba->sli4_hba.u.if_type2.ERR1regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006727 phba->sli4_hba.conf_regs_memmap_p +
6728 LPFC_CTL_PORT_ER1_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006729 phba->sli4_hba.u.if_type2.ERR2regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006730 phba->sli4_hba.conf_regs_memmap_p +
6731 LPFC_CTL_PORT_ER2_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006732 phba->sli4_hba.u.if_type2.CTRLregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006733 phba->sli4_hba.conf_regs_memmap_p +
6734 LPFC_CTL_PORT_CTL_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006735 phba->sli4_hba.u.if_type2.STATUSregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006736 phba->sli4_hba.conf_regs_memmap_p +
6737 LPFC_CTL_PORT_STA_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006738 phba->sli4_hba.SLIINTFregaddr =
6739 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
6740 phba->sli4_hba.PSMPHRregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006741 phba->sli4_hba.conf_regs_memmap_p +
6742 LPFC_CTL_PORT_SEM_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006743 phba->sli4_hba.RQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05006744 phba->sli4_hba.conf_regs_memmap_p +
6745 LPFC_ULP0_RQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05006746 phba->sli4_hba.WQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05006747 phba->sli4_hba.conf_regs_memmap_p +
6748 LPFC_ULP0_WQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05006749 phba->sli4_hba.EQCQDBregaddr =
6750 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
6751 phba->sli4_hba.MQDBregaddr =
6752 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
6753 phba->sli4_hba.BMBXregaddr =
6754 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
6755 break;
6756 case LPFC_SLI_INTF_IF_TYPE_1:
6757 default:
6758 dev_printk(KERN_ERR, &phba->pcidev->dev,
6759 "FATAL - unsupported SLI4 interface type - %d\n",
6760 if_type);
6761 break;
6762 }
James Smartda0436e2009-05-22 14:51:39 -04006763}
6764
6765/**
6766 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
6767 * @phba: pointer to lpfc hba data structure.
6768 *
6769 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
6770 * memory map.
6771 **/
6772static void
6773lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
6774{
James Smart2fcee4b2010-12-15 17:57:46 -05006775 phba->sli4_hba.PSMPHRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
6776 LPFC_SLIPORT_IF0_SMPHR;
James Smartda0436e2009-05-22 14:51:39 -04006777 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006778 LPFC_HST_ISR0;
James Smartda0436e2009-05-22 14:51:39 -04006779 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006780 LPFC_HST_IMR0;
James Smartda0436e2009-05-22 14:51:39 -04006781 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006782 LPFC_HST_ISCR0;
James Smartda0436e2009-05-22 14:51:39 -04006783}
6784
6785/**
6786 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
6787 * @phba: pointer to lpfc hba data structure.
6788 * @vf: virtual function number
6789 *
6790 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
6791 * based on the given viftual function number, @vf.
6792 *
6793 * Return 0 if successful, otherwise -ENODEV.
6794 **/
6795static int
6796lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
6797{
6798 if (vf > LPFC_VIR_FUNC_MAX)
6799 return -ENODEV;
6800
6801 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05006802 vf * LPFC_VFR_PAGE_SIZE +
6803 LPFC_ULP0_RQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04006804 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05006805 vf * LPFC_VFR_PAGE_SIZE +
6806 LPFC_ULP0_WQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04006807 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6808 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
6809 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6810 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
6811 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6812 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
6813 return 0;
6814}
6815
6816/**
6817 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
6818 * @phba: pointer to lpfc hba data structure.
6819 *
6820 * This routine is invoked to create the bootstrap mailbox
6821 * region consistent with the SLI-4 interface spec. This
6822 * routine allocates all memory necessary to communicate
6823 * mailbox commands to the port and sets up all alignment
6824 * needs. No locks are expected to be held when calling
6825 * this routine.
6826 *
6827 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006828 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04006829 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04006830 **/
6831static int
6832lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
6833{
6834 uint32_t bmbx_size;
6835 struct lpfc_dmabuf *dmabuf;
6836 struct dma_address *dma_address;
6837 uint32_t pa_addr;
6838 uint64_t phys_addr;
6839
6840 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
6841 if (!dmabuf)
6842 return -ENOMEM;
6843
6844 /*
6845 * The bootstrap mailbox region is comprised of 2 parts
6846 * plus an alignment restriction of 16 bytes.
6847 */
6848 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
Joe Perches1aee3832014-09-03 12:56:12 -04006849 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, bmbx_size,
6850 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04006851 if (!dmabuf->virt) {
6852 kfree(dmabuf);
6853 return -ENOMEM;
6854 }
James Smartda0436e2009-05-22 14:51:39 -04006855
6856 /*
6857 * Initialize the bootstrap mailbox pointers now so that the register
6858 * operations are simple later. The mailbox dma address is required
6859 * to be 16-byte aligned. Also align the virtual memory as each
6860 * maibox is copied into the bmbx mailbox region before issuing the
6861 * command to the port.
6862 */
6863 phba->sli4_hba.bmbx.dmabuf = dmabuf;
6864 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
6865
6866 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
6867 LPFC_ALIGN_16_BYTE);
6868 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
6869 LPFC_ALIGN_16_BYTE);
6870
6871 /*
6872 * Set the high and low physical addresses now. The SLI4 alignment
6873 * requirement is 16 bytes and the mailbox is posted to the port
6874 * as two 30-bit addresses. The other data is a bit marking whether
6875 * the 30-bit address is the high or low address.
6876 * Upcast bmbx aphys to 64bits so shift instruction compiles
6877 * clean on 32 bit machines.
6878 */
6879 dma_address = &phba->sli4_hba.bmbx.dma_address;
6880 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
6881 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
6882 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
6883 LPFC_BMBX_BIT1_ADDR_HI);
6884
6885 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
6886 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
6887 LPFC_BMBX_BIT1_ADDR_LO);
6888 return 0;
6889}
6890
6891/**
6892 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
6893 * @phba: pointer to lpfc hba data structure.
6894 *
6895 * This routine is invoked to teardown the bootstrap mailbox
6896 * region and release all host resources. This routine requires
6897 * the caller to ensure all mailbox commands recovered, no
6898 * additional mailbox comands are sent, and interrupts are disabled
6899 * before calling this routine.
6900 *
6901 **/
6902static void
6903lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
6904{
6905 dma_free_coherent(&phba->pcidev->dev,
6906 phba->sli4_hba.bmbx.bmbx_size,
6907 phba->sli4_hba.bmbx.dmabuf->virt,
6908 phba->sli4_hba.bmbx.dmabuf->phys);
6909
6910 kfree(phba->sli4_hba.bmbx.dmabuf);
6911 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
6912}
6913
6914/**
6915 * lpfc_sli4_read_config - Get the config parameters.
6916 * @phba: pointer to lpfc hba data structure.
6917 *
6918 * This routine is invoked to read the configuration parameters from the HBA.
6919 * The configuration parameters are used to set the base and maximum values
6920 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
6921 * allocation for the port.
6922 *
6923 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006924 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006925 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006926 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006927 **/
James Smartff78d8f2011-12-13 13:21:35 -05006928int
James Smartda0436e2009-05-22 14:51:39 -04006929lpfc_sli4_read_config(struct lpfc_hba *phba)
6930{
6931 LPFC_MBOXQ_t *pmb;
6932 struct lpfc_mbx_read_config *rd_config;
James Smart912e3ac2011-05-24 11:42:11 -04006933 union lpfc_sli4_cfg_shdr *shdr;
6934 uint32_t shdr_status, shdr_add_status;
6935 struct lpfc_mbx_get_func_cfg *get_func_cfg;
6936 struct lpfc_rsrc_desc_fcfcoe *desc;
James Smart8aa134a2012-08-14 14:25:29 -04006937 char *pdesc_0;
James Smart8aa134a2012-08-14 14:25:29 -04006938 int length, i, rc = 0, rc2;
James Smartda0436e2009-05-22 14:51:39 -04006939
6940 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6941 if (!pmb) {
6942 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6943 "2011 Unable to allocate memory for issuing "
6944 "SLI_CONFIG_SPECIAL mailbox command\n");
6945 return -ENOMEM;
6946 }
6947
6948 lpfc_read_config(phba, pmb);
6949
6950 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6951 if (rc != MBX_SUCCESS) {
6952 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6953 "2012 Mailbox failed , mbxCmd x%x "
6954 "READ_CONFIG, mbxStatus x%x\n",
6955 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6956 bf_get(lpfc_mqe_status, &pmb->u.mqe));
6957 rc = -EIO;
6958 } else {
6959 rd_config = &pmb->u.mqe.un.rd_config;
James Smartff78d8f2011-12-13 13:21:35 -05006960 if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
6961 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
6962 phba->sli4_hba.lnk_info.lnk_tp =
6963 bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
6964 phba->sli4_hba.lnk_info.lnk_no =
6965 bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
6966 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6967 "3081 lnk_type:%d, lnk_numb:%d\n",
6968 phba->sli4_hba.lnk_info.lnk_tp,
6969 phba->sli4_hba.lnk_info.lnk_no);
6970 } else
6971 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6972 "3082 Mailbox (x%x) returned ldv:x0\n",
6973 bf_get(lpfc_mqe_command, &pmb->u.mqe));
James Smart6d368e52011-05-24 11:44:12 -04006974 phba->sli4_hba.extents_in_use =
6975 bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04006976 phba->sli4_hba.max_cfg_param.max_xri =
6977 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
6978 phba->sli4_hba.max_cfg_param.xri_base =
6979 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
6980 phba->sli4_hba.max_cfg_param.max_vpi =
6981 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
6982 phba->sli4_hba.max_cfg_param.vpi_base =
6983 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
6984 phba->sli4_hba.max_cfg_param.max_rpi =
6985 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
6986 phba->sli4_hba.max_cfg_param.rpi_base =
6987 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
6988 phba->sli4_hba.max_cfg_param.max_vfi =
6989 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
6990 phba->sli4_hba.max_cfg_param.vfi_base =
6991 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
6992 phba->sli4_hba.max_cfg_param.max_fcfi =
6993 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04006994 phba->sli4_hba.max_cfg_param.max_eq =
6995 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
6996 phba->sli4_hba.max_cfg_param.max_rq =
6997 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
6998 phba->sli4_hba.max_cfg_param.max_wq =
6999 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
7000 phba->sli4_hba.max_cfg_param.max_cq =
7001 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
7002 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
7003 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
7004 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
7005 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
James Smart5ffc2662009-11-18 15:39:44 -05007006 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
7007 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04007008 phba->max_vports = phba->max_vpi;
7009 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart6d368e52011-05-24 11:44:12 -04007010 "2003 cfg params Extents? %d "
7011 "XRI(B:%d M:%d), "
James Smartda0436e2009-05-22 14:51:39 -04007012 "VPI(B:%d M:%d) "
7013 "VFI(B:%d M:%d) "
7014 "RPI(B:%d M:%d) "
James Smart6d368e52011-05-24 11:44:12 -04007015 "FCFI(Count:%d)\n",
7016 phba->sli4_hba.extents_in_use,
James Smartda0436e2009-05-22 14:51:39 -04007017 phba->sli4_hba.max_cfg_param.xri_base,
7018 phba->sli4_hba.max_cfg_param.max_xri,
7019 phba->sli4_hba.max_cfg_param.vpi_base,
7020 phba->sli4_hba.max_cfg_param.max_vpi,
7021 phba->sli4_hba.max_cfg_param.vfi_base,
7022 phba->sli4_hba.max_cfg_param.max_vfi,
7023 phba->sli4_hba.max_cfg_param.rpi_base,
7024 phba->sli4_hba.max_cfg_param.max_rpi,
James Smartda0436e2009-05-22 14:51:39 -04007025 phba->sli4_hba.max_cfg_param.max_fcfi);
7026 }
James Smart912e3ac2011-05-24 11:42:11 -04007027
7028 if (rc)
7029 goto read_cfg_out;
James Smartda0436e2009-05-22 14:51:39 -04007030
7031 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -04007032 length = phba->sli4_hba.max_cfg_param.max_xri -
7033 lpfc_sli4_get_els_iocb_cnt(phba);
7034 if (phba->cfg_hba_queue_depth > length) {
7035 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7036 "3361 HBA queue depth changed from %d to %d\n",
7037 phba->cfg_hba_queue_depth, length);
7038 phba->cfg_hba_queue_depth = length;
7039 }
James Smart912e3ac2011-05-24 11:42:11 -04007040
7041 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
7042 LPFC_SLI_INTF_IF_TYPE_2)
7043 goto read_cfg_out;
7044
7045 /* get the pf# and vf# for SLI4 if_type 2 port */
7046 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
7047 sizeof(struct lpfc_sli4_cfg_mhdr));
7048 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
7049 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
7050 length, LPFC_SLI4_MBX_EMBED);
7051
James Smart8aa134a2012-08-14 14:25:29 -04007052 rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart912e3ac2011-05-24 11:42:11 -04007053 shdr = (union lpfc_sli4_cfg_shdr *)
7054 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
7055 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7056 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart8aa134a2012-08-14 14:25:29 -04007057 if (rc2 || shdr_status || shdr_add_status) {
James Smart912e3ac2011-05-24 11:42:11 -04007058 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7059 "3026 Mailbox failed , mbxCmd x%x "
7060 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
7061 bf_get(lpfc_mqe_command, &pmb->u.mqe),
7062 bf_get(lpfc_mqe_status, &pmb->u.mqe));
James Smart912e3ac2011-05-24 11:42:11 -04007063 goto read_cfg_out;
7064 }
7065
7066 /* search for fc_fcoe resrouce descriptor */
7067 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
James Smart912e3ac2011-05-24 11:42:11 -04007068
James Smart8aa134a2012-08-14 14:25:29 -04007069 pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
7070 desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
7071 length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
7072 if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
7073 length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
7074 else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
7075 goto read_cfg_out;
7076
James Smart912e3ac2011-05-24 11:42:11 -04007077 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
James Smart8aa134a2012-08-14 14:25:29 -04007078 desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
James Smart912e3ac2011-05-24 11:42:11 -04007079 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
James Smart8aa134a2012-08-14 14:25:29 -04007080 bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
James Smart912e3ac2011-05-24 11:42:11 -04007081 phba->sli4_hba.iov.pf_number =
7082 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
7083 phba->sli4_hba.iov.vf_number =
7084 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
7085 break;
7086 }
7087 }
7088
7089 if (i < LPFC_RSRC_DESC_MAX_NUM)
7090 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7091 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
7092 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
7093 phba->sli4_hba.iov.vf_number);
James Smart8aa134a2012-08-14 14:25:29 -04007094 else
James Smart912e3ac2011-05-24 11:42:11 -04007095 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7096 "3028 GET_FUNCTION_CONFIG: failed to find "
7097 "Resrouce Descriptor:x%x\n",
7098 LPFC_RSRC_DESC_TYPE_FCFCOE);
James Smart912e3ac2011-05-24 11:42:11 -04007099
7100read_cfg_out:
7101 mempool_free(pmb, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04007102 return rc;
7103}
7104
7105/**
James Smart2fcee4b2010-12-15 17:57:46 -05007106 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
James Smartda0436e2009-05-22 14:51:39 -04007107 * @phba: pointer to lpfc hba data structure.
7108 *
James Smart2fcee4b2010-12-15 17:57:46 -05007109 * This routine is invoked to setup the port-side endian order when
7110 * the port if_type is 0. This routine has no function for other
7111 * if_types.
James Smartda0436e2009-05-22 14:51:39 -04007112 *
7113 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007114 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007115 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007116 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007117 **/
7118static int
7119lpfc_setup_endian_order(struct lpfc_hba *phba)
7120{
7121 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05007122 uint32_t if_type, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04007123 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
7124 HOST_ENDIAN_HIGH_WORD1};
7125
James Smart2fcee4b2010-12-15 17:57:46 -05007126 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7127 switch (if_type) {
7128 case LPFC_SLI_INTF_IF_TYPE_0:
7129 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
7130 GFP_KERNEL);
7131 if (!mboxq) {
7132 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7133 "0492 Unable to allocate memory for "
7134 "issuing SLI_CONFIG_SPECIAL mailbox "
7135 "command\n");
7136 return -ENOMEM;
7137 }
James Smartda0436e2009-05-22 14:51:39 -04007138
James Smart2fcee4b2010-12-15 17:57:46 -05007139 /*
7140 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
7141 * two words to contain special data values and no other data.
7142 */
7143 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
7144 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
7145 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7146 if (rc != MBX_SUCCESS) {
7147 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7148 "0493 SLI_CONFIG_SPECIAL mailbox "
7149 "failed with status x%x\n",
7150 rc);
7151 rc = -EIO;
7152 }
7153 mempool_free(mboxq, phba->mbox_mem_pool);
7154 break;
7155 case LPFC_SLI_INTF_IF_TYPE_2:
7156 case LPFC_SLI_INTF_IF_TYPE_1:
7157 default:
7158 break;
James Smartda0436e2009-05-22 14:51:39 -04007159 }
James Smartda0436e2009-05-22 14:51:39 -04007160 return rc;
7161}
7162
7163/**
James Smart5350d872011-10-10 21:33:49 -04007164 * lpfc_sli4_queue_verify - Verify and update EQ and CQ counts
James Smartda0436e2009-05-22 14:51:39 -04007165 * @phba: pointer to lpfc hba data structure.
7166 *
James Smart5350d872011-10-10 21:33:49 -04007167 * This routine is invoked to check the user settable queue counts for EQs and
7168 * CQs. after this routine is called the counts will be set to valid values that
7169 * adhere to the constraints of the system's interrupt vectors and the port's
7170 * queue resources.
James Smartda0436e2009-05-22 14:51:39 -04007171 *
7172 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007173 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007174 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04007175 **/
7176static int
James Smart5350d872011-10-10 21:33:49 -04007177lpfc_sli4_queue_verify(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04007178{
James Smart67d12732012-08-03 12:36:13 -04007179 int cfg_fcp_io_channel;
James Smart90695ee2012-08-14 14:25:36 -04007180 uint32_t cpu;
7181 uint32_t i = 0;
James Smart1ba981f2014-02-20 09:56:45 -05007182 int fof_vectors = phba->cfg_fof ? 1 : 0;
James Smartda0436e2009-05-22 14:51:39 -04007183
7184 /*
James Smart67d12732012-08-03 12:36:13 -04007185 * Sanity check for configured queue parameters against the run-time
James Smartda0436e2009-05-22 14:51:39 -04007186 * device parameters
7187 */
7188
James Smart67d12732012-08-03 12:36:13 -04007189 /* Sanity check on HBA EQ parameters */
7190 cfg_fcp_io_channel = phba->cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04007191
James Smart7bb03bb2013-04-17 20:19:16 -04007192 /* It doesn't make sense to have more io channels then online CPUs */
7193 for_each_present_cpu(cpu) {
7194 if (cpu_online(cpu))
7195 i++;
James Smart90695ee2012-08-14 14:25:36 -04007196 }
James Smart7bb03bb2013-04-17 20:19:16 -04007197 phba->sli4_hba.num_online_cpu = i;
James Smartb246de12013-05-31 17:03:07 -04007198 phba->sli4_hba.num_present_cpu = lpfc_present_cpu;
James Smart76fd07a2014-02-20 09:57:18 -05007199 phba->sli4_hba.curr_disp_cpu = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04007200
James Smart90695ee2012-08-14 14:25:36 -04007201 if (i < cfg_fcp_io_channel) {
James Smart82c3e9b2012-09-29 11:29:50 -04007202 lpfc_printf_log(phba,
7203 KERN_ERR, LOG_INIT,
James Smart90695ee2012-08-14 14:25:36 -04007204 "3188 Reducing IO channels to match number of "
James Smart7bb03bb2013-04-17 20:19:16 -04007205 "online CPUs: from %d to %d\n",
7206 cfg_fcp_io_channel, i);
James Smart90695ee2012-08-14 14:25:36 -04007207 cfg_fcp_io_channel = i;
7208 }
7209
James Smart1ba981f2014-02-20 09:56:45 -05007210 if (cfg_fcp_io_channel + fof_vectors >
James Smart67d12732012-08-03 12:36:13 -04007211 phba->sli4_hba.max_cfg_param.max_eq) {
James Smart82c3e9b2012-09-29 11:29:50 -04007212 if (phba->sli4_hba.max_cfg_param.max_eq <
7213 LPFC_FCP_IO_CHAN_MIN) {
James Smartda0436e2009-05-22 14:51:39 -04007214 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7215 "2574 Not enough EQs (%d) from the "
7216 "pci function for supporting FCP "
7217 "EQs (%d)\n",
7218 phba->sli4_hba.max_cfg_param.max_eq,
James Smart67d12732012-08-03 12:36:13 -04007219 phba->cfg_fcp_io_channel);
James Smartda0436e2009-05-22 14:51:39 -04007220 goto out_error;
7221 }
James Smart82c3e9b2012-09-29 11:29:50 -04007222 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7223 "2575 Reducing IO channels to match number of "
7224 "available EQs: from %d to %d\n",
7225 cfg_fcp_io_channel,
7226 phba->sli4_hba.max_cfg_param.max_eq);
James Smart1ba981f2014-02-20 09:56:45 -05007227 cfg_fcp_io_channel = phba->sli4_hba.max_cfg_param.max_eq -
7228 fof_vectors;
James Smartda0436e2009-05-22 14:51:39 -04007229 }
James Smart67d12732012-08-03 12:36:13 -04007230
James Smartda0436e2009-05-22 14:51:39 -04007231 /* The actual number of FCP event queues adopted */
James Smart67d12732012-08-03 12:36:13 -04007232 phba->cfg_fcp_io_channel = cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04007233
James Smartda0436e2009-05-22 14:51:39 -04007234 /* Get EQ depth from module parameter, fake the default for now */
7235 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
7236 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
7237
James Smart5350d872011-10-10 21:33:49 -04007238 /* Get CQ depth from module parameter, fake the default for now */
7239 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
7240 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
7241
7242 return 0;
7243out_error:
7244 return -ENOMEM;
7245}
7246
7247/**
7248 * lpfc_sli4_queue_create - Create all the SLI4 queues
7249 * @phba: pointer to lpfc hba data structure.
7250 *
7251 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
7252 * operation. For each SLI4 queue type, the parameters such as queue entry
7253 * count (queue depth) shall be taken from the module parameter. For now,
7254 * we just use some constant number as place holder.
7255 *
7256 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07007257 * 0 - successful
James Smart5350d872011-10-10 21:33:49 -04007258 * -ENOMEM - No availble memory
7259 * -EIO - The mailbox failed to complete successfully.
7260 **/
7261int
7262lpfc_sli4_queue_create(struct lpfc_hba *phba)
7263{
7264 struct lpfc_queue *qdesc;
James Smart67d12732012-08-03 12:36:13 -04007265 int idx;
James Smart5350d872011-10-10 21:33:49 -04007266
7267 /*
James Smart67d12732012-08-03 12:36:13 -04007268 * Create HBA Record arrays.
James Smart5350d872011-10-10 21:33:49 -04007269 */
James Smart67d12732012-08-03 12:36:13 -04007270 if (!phba->cfg_fcp_io_channel)
7271 return -ERANGE;
James Smart5350d872011-10-10 21:33:49 -04007272
James Smart67d12732012-08-03 12:36:13 -04007273 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
7274 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
7275 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
7276 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
7277 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
7278 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
7279
7280 phba->sli4_hba.hba_eq = kzalloc((sizeof(struct lpfc_queue *) *
7281 phba->cfg_fcp_io_channel), GFP_KERNEL);
7282 if (!phba->sli4_hba.hba_eq) {
James Smartda0436e2009-05-22 14:51:39 -04007283 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04007284 "2576 Failed allocate memory for "
7285 "fast-path EQ record array\n");
James Smartda0436e2009-05-22 14:51:39 -04007286 goto out_error;
7287 }
James Smart67d12732012-08-03 12:36:13 -04007288
7289 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
7290 phba->cfg_fcp_io_channel), GFP_KERNEL);
7291 if (!phba->sli4_hba.fcp_cq) {
7292 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7293 "2577 Failed allocate memory for fast-path "
7294 "CQ record array\n");
7295 goto out_error;
7296 }
7297
7298 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
7299 phba->cfg_fcp_io_channel), GFP_KERNEL);
7300 if (!phba->sli4_hba.fcp_wq) {
7301 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7302 "2578 Failed allocate memory for fast-path "
7303 "WQ record array\n");
7304 goto out_error;
7305 }
James Smartda0436e2009-05-22 14:51:39 -04007306
James Smart5350d872011-10-10 21:33:49 -04007307 /*
James Smart67d12732012-08-03 12:36:13 -04007308 * Since the first EQ can have multiple CQs associated with it,
7309 * this array is used to quickly see if we have a FCP fast-path
7310 * CQ match.
James Smart5350d872011-10-10 21:33:49 -04007311 */
James Smart67d12732012-08-03 12:36:13 -04007312 phba->sli4_hba.fcp_cq_map = kzalloc((sizeof(uint16_t) *
7313 phba->cfg_fcp_io_channel), GFP_KERNEL);
7314 if (!phba->sli4_hba.fcp_cq_map) {
7315 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7316 "2545 Failed allocate memory for fast-path "
7317 "CQ map\n");
7318 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007319 }
James Smart67d12732012-08-03 12:36:13 -04007320
7321 /*
7322 * Create HBA Event Queues (EQs). The cfg_fcp_io_channel specifies
7323 * how many EQs to create.
7324 */
7325 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7326
7327 /* Create EQs */
James Smartda0436e2009-05-22 14:51:39 -04007328 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
7329 phba->sli4_hba.eq_ecount);
7330 if (!qdesc) {
7331 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04007332 "0497 Failed allocate EQ (%d)\n", idx);
7333 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007334 }
James Smart67d12732012-08-03 12:36:13 -04007335 phba->sli4_hba.hba_eq[idx] = qdesc;
7336
7337 /* Create Fast Path FCP CQs */
7338 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7339 phba->sli4_hba.cq_ecount);
7340 if (!qdesc) {
7341 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7342 "0499 Failed allocate fast-path FCP "
7343 "CQ (%d)\n", idx);
7344 goto out_error;
7345 }
7346 phba->sli4_hba.fcp_cq[idx] = qdesc;
7347
7348 /* Create Fast Path FCP WQs */
James Smartb5c53952016-03-31 14:12:30 -07007349 if (phba->fcp_embed_io) {
7350 qdesc = lpfc_sli4_queue_alloc(phba,
7351 LPFC_WQE128_SIZE,
7352 LPFC_WQE128_DEF_COUNT);
7353 } else {
7354 qdesc = lpfc_sli4_queue_alloc(phba,
7355 phba->sli4_hba.wq_esize,
7356 phba->sli4_hba.wq_ecount);
7357 }
James Smart67d12732012-08-03 12:36:13 -04007358 if (!qdesc) {
7359 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7360 "0503 Failed allocate fast-path FCP "
7361 "WQ (%d)\n", idx);
7362 goto out_error;
7363 }
7364 phba->sli4_hba.fcp_wq[idx] = qdesc;
James Smartda0436e2009-05-22 14:51:39 -04007365 }
7366
James Smart67d12732012-08-03 12:36:13 -04007367
James Smartda0436e2009-05-22 14:51:39 -04007368 /*
James Smart67d12732012-08-03 12:36:13 -04007369 * Create Slow Path Completion Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04007370 */
7371
James Smartda0436e2009-05-22 14:51:39 -04007372 /* Create slow-path Mailbox Command Complete Queue */
7373 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7374 phba->sli4_hba.cq_ecount);
7375 if (!qdesc) {
7376 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7377 "0500 Failed allocate slow-path mailbox CQ\n");
James Smart67d12732012-08-03 12:36:13 -04007378 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007379 }
7380 phba->sli4_hba.mbx_cq = qdesc;
7381
7382 /* Create slow-path ELS Complete Queue */
7383 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7384 phba->sli4_hba.cq_ecount);
7385 if (!qdesc) {
7386 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7387 "0501 Failed allocate slow-path ELS CQ\n");
James Smart67d12732012-08-03 12:36:13 -04007388 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007389 }
7390 phba->sli4_hba.els_cq = qdesc;
7391
James Smartda0436e2009-05-22 14:51:39 -04007392
James Smart5350d872011-10-10 21:33:49 -04007393 /*
James Smart67d12732012-08-03 12:36:13 -04007394 * Create Slow Path Work Queues (WQs)
James Smart5350d872011-10-10 21:33:49 -04007395 */
James Smartda0436e2009-05-22 14:51:39 -04007396
7397 /* Create Mailbox Command Queue */
James Smartda0436e2009-05-22 14:51:39 -04007398
7399 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
7400 phba->sli4_hba.mq_ecount);
7401 if (!qdesc) {
7402 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7403 "0505 Failed allocate slow-path MQ\n");
James Smart67d12732012-08-03 12:36:13 -04007404 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007405 }
7406 phba->sli4_hba.mbx_wq = qdesc;
7407
7408 /*
James Smart67d12732012-08-03 12:36:13 -04007409 * Create ELS Work Queues
James Smartda0436e2009-05-22 14:51:39 -04007410 */
James Smartda0436e2009-05-22 14:51:39 -04007411
7412 /* Create slow-path ELS Work Queue */
7413 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
7414 phba->sli4_hba.wq_ecount);
7415 if (!qdesc) {
7416 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7417 "0504 Failed allocate slow-path ELS WQ\n");
James Smart67d12732012-08-03 12:36:13 -04007418 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007419 }
7420 phba->sli4_hba.els_wq = qdesc;
7421
James Smartda0436e2009-05-22 14:51:39 -04007422 /*
7423 * Create Receive Queue (RQ)
7424 */
James Smartda0436e2009-05-22 14:51:39 -04007425
7426 /* Create Receive Queue for header */
7427 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
7428 phba->sli4_hba.rq_ecount);
7429 if (!qdesc) {
7430 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7431 "0506 Failed allocate receive HRQ\n");
James Smart67d12732012-08-03 12:36:13 -04007432 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007433 }
7434 phba->sli4_hba.hdr_rq = qdesc;
7435
7436 /* Create Receive Queue for data */
7437 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
7438 phba->sli4_hba.rq_ecount);
7439 if (!qdesc) {
7440 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7441 "0507 Failed allocate receive DRQ\n");
James Smart67d12732012-08-03 12:36:13 -04007442 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007443 }
7444 phba->sli4_hba.dat_rq = qdesc;
7445
James Smart1ba981f2014-02-20 09:56:45 -05007446 /* Create the Queues needed for Flash Optimized Fabric operations */
7447 if (phba->cfg_fof)
7448 lpfc_fof_queue_create(phba);
James Smartda0436e2009-05-22 14:51:39 -04007449 return 0;
7450
James Smartda0436e2009-05-22 14:51:39 -04007451out_error:
James Smart67d12732012-08-03 12:36:13 -04007452 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007453 return -ENOMEM;
7454}
7455
7456/**
7457 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
7458 * @phba: pointer to lpfc hba data structure.
7459 *
7460 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
7461 * operation.
7462 *
7463 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007464 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007465 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007466 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007467 **/
James Smart5350d872011-10-10 21:33:49 -04007468void
James Smartda0436e2009-05-22 14:51:39 -04007469lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
7470{
James Smart67d12732012-08-03 12:36:13 -04007471 int idx;
7472
James Smart1ba981f2014-02-20 09:56:45 -05007473 if (phba->cfg_fof)
7474 lpfc_fof_queue_destroy(phba);
7475
James Smart67d12732012-08-03 12:36:13 -04007476 if (phba->sli4_hba.hba_eq != NULL) {
7477 /* Release HBA event queue */
7478 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7479 if (phba->sli4_hba.hba_eq[idx] != NULL) {
7480 lpfc_sli4_queue_free(
7481 phba->sli4_hba.hba_eq[idx]);
7482 phba->sli4_hba.hba_eq[idx] = NULL;
7483 }
7484 }
7485 kfree(phba->sli4_hba.hba_eq);
7486 phba->sli4_hba.hba_eq = NULL;
7487 }
7488
7489 if (phba->sli4_hba.fcp_cq != NULL) {
7490 /* Release FCP completion queue */
7491 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7492 if (phba->sli4_hba.fcp_cq[idx] != NULL) {
7493 lpfc_sli4_queue_free(
7494 phba->sli4_hba.fcp_cq[idx]);
7495 phba->sli4_hba.fcp_cq[idx] = NULL;
7496 }
7497 }
7498 kfree(phba->sli4_hba.fcp_cq);
7499 phba->sli4_hba.fcp_cq = NULL;
7500 }
7501
7502 if (phba->sli4_hba.fcp_wq != NULL) {
7503 /* Release FCP work queue */
7504 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7505 if (phba->sli4_hba.fcp_wq[idx] != NULL) {
7506 lpfc_sli4_queue_free(
7507 phba->sli4_hba.fcp_wq[idx]);
7508 phba->sli4_hba.fcp_wq[idx] = NULL;
7509 }
7510 }
7511 kfree(phba->sli4_hba.fcp_wq);
7512 phba->sli4_hba.fcp_wq = NULL;
7513 }
7514
7515 /* Release FCP CQ mapping array */
7516 if (phba->sli4_hba.fcp_cq_map != NULL) {
7517 kfree(phba->sli4_hba.fcp_cq_map);
7518 phba->sli4_hba.fcp_cq_map = NULL;
7519 }
James Smartda0436e2009-05-22 14:51:39 -04007520
7521 /* Release mailbox command work queue */
James Smart67d12732012-08-03 12:36:13 -04007522 if (phba->sli4_hba.mbx_wq != NULL) {
7523 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
7524 phba->sli4_hba.mbx_wq = NULL;
7525 }
James Smartda0436e2009-05-22 14:51:39 -04007526
7527 /* Release ELS work queue */
James Smart67d12732012-08-03 12:36:13 -04007528 if (phba->sli4_hba.els_wq != NULL) {
7529 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
7530 phba->sli4_hba.els_wq = NULL;
7531 }
James Smartda0436e2009-05-22 14:51:39 -04007532
7533 /* Release unsolicited receive queue */
James Smart67d12732012-08-03 12:36:13 -04007534 if (phba->sli4_hba.hdr_rq != NULL) {
7535 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
7536 phba->sli4_hba.hdr_rq = NULL;
7537 }
7538 if (phba->sli4_hba.dat_rq != NULL) {
7539 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
7540 phba->sli4_hba.dat_rq = NULL;
7541 }
James Smartda0436e2009-05-22 14:51:39 -04007542
James Smartda0436e2009-05-22 14:51:39 -04007543 /* Release ELS complete queue */
James Smart67d12732012-08-03 12:36:13 -04007544 if (phba->sli4_hba.els_cq != NULL) {
7545 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
7546 phba->sli4_hba.els_cq = NULL;
7547 }
James Smartda0436e2009-05-22 14:51:39 -04007548
7549 /* Release mailbox command complete queue */
James Smart67d12732012-08-03 12:36:13 -04007550 if (phba->sli4_hba.mbx_cq != NULL) {
7551 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
7552 phba->sli4_hba.mbx_cq = NULL;
7553 }
James Smartda0436e2009-05-22 14:51:39 -04007554
7555 return;
7556}
7557
7558/**
7559 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
7560 * @phba: pointer to lpfc hba data structure.
7561 *
7562 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
7563 * operation.
7564 *
7565 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007566 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007567 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007568 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007569 **/
7570int
7571lpfc_sli4_queue_setup(struct lpfc_hba *phba)
7572{
James Smart2a76a282012-08-03 12:35:54 -04007573 struct lpfc_sli *psli = &phba->sli;
7574 struct lpfc_sli_ring *pring;
James Smartda0436e2009-05-22 14:51:39 -04007575 int rc = -ENOMEM;
7576 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
7577 int fcp_cq_index = 0;
James Smart962bc512013-01-03 15:44:00 -05007578 uint32_t shdr_status, shdr_add_status;
7579 union lpfc_sli4_cfg_shdr *shdr;
7580 LPFC_MBOXQ_t *mboxq;
7581 uint32_t length;
7582
7583 /* Check for dual-ULP support */
7584 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7585 if (!mboxq) {
7586 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7587 "3249 Unable to allocate memory for "
7588 "QUERY_FW_CFG mailbox command\n");
7589 return -ENOMEM;
7590 }
7591 length = (sizeof(struct lpfc_mbx_query_fw_config) -
7592 sizeof(struct lpfc_sli4_cfg_mhdr));
7593 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7594 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
7595 length, LPFC_SLI4_MBX_EMBED);
7596
7597 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7598
7599 shdr = (union lpfc_sli4_cfg_shdr *)
7600 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7601 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7602 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7603 if (shdr_status || shdr_add_status || rc) {
7604 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7605 "3250 QUERY_FW_CFG mailbox failed with status "
7606 "x%x add_status x%x, mbx status x%x\n",
7607 shdr_status, shdr_add_status, rc);
7608 if (rc != MBX_TIMEOUT)
7609 mempool_free(mboxq, phba->mbox_mem_pool);
7610 rc = -ENXIO;
7611 goto out_error;
7612 }
7613
7614 phba->sli4_hba.fw_func_mode =
7615 mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
7616 phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
7617 phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
James Smart8b017a32015-05-21 13:55:18 -04007618 phba->sli4_hba.physical_port =
7619 mboxq->u.mqe.un.query_fw_cfg.rsp.physical_port;
James Smart962bc512013-01-03 15:44:00 -05007620 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7621 "3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
7622 "ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
7623 phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
7624
7625 if (rc != MBX_TIMEOUT)
7626 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04007627
7628 /*
James Smart67d12732012-08-03 12:36:13 -04007629 * Set up HBA Event Queues (EQs)
James Smartda0436e2009-05-22 14:51:39 -04007630 */
7631
James Smart67d12732012-08-03 12:36:13 -04007632 /* Set up HBA event queue */
7633 if (phba->cfg_fcp_io_channel && !phba->sli4_hba.hba_eq) {
James Smart2e90f4b2011-12-13 13:22:37 -05007634 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7635 "3147 Fast-path EQs not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007636 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007637 goto out_error;
James Smart2e90f4b2011-12-13 13:22:37 -05007638 }
James Smart67d12732012-08-03 12:36:13 -04007639 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
7640 if (!phba->sli4_hba.hba_eq[fcp_eqidx]) {
James Smartda0436e2009-05-22 14:51:39 -04007641 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7642 "0522 Fast-path EQ (%d) not "
7643 "allocated\n", fcp_eqidx);
James Smart1b511972011-12-13 13:23:09 -05007644 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007645 goto out_destroy_hba_eq;
James Smartda0436e2009-05-22 14:51:39 -04007646 }
James Smart67d12732012-08-03 12:36:13 -04007647 rc = lpfc_eq_create(phba, phba->sli4_hba.hba_eq[fcp_eqidx],
James Smartbf8dae82012-08-03 12:36:24 -04007648 (phba->cfg_fcp_imax / phba->cfg_fcp_io_channel));
James Smartda0436e2009-05-22 14:51:39 -04007649 if (rc) {
7650 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7651 "0523 Failed setup of fast-path EQ "
James Smarta2fc4aef2014-09-03 12:57:55 -04007652 "(%d), rc = 0x%x\n", fcp_eqidx,
7653 (uint32_t)rc);
James Smart67d12732012-08-03 12:36:13 -04007654 goto out_destroy_hba_eq;
James Smartda0436e2009-05-22 14:51:39 -04007655 }
7656 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04007657 "2584 HBA EQ setup: "
James Smartda0436e2009-05-22 14:51:39 -04007658 "queue[%d]-id=%d\n", fcp_eqidx,
James Smart67d12732012-08-03 12:36:13 -04007659 phba->sli4_hba.hba_eq[fcp_eqidx]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007660 }
7661
James Smart67d12732012-08-03 12:36:13 -04007662 /* Set up fast-path FCP Response Complete Queue */
7663 if (!phba->sli4_hba.fcp_cq) {
7664 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7665 "3148 Fast-path FCP CQ array not "
7666 "allocated\n");
7667 rc = -ENOMEM;
7668 goto out_destroy_hba_eq;
7669 }
7670
7671 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_io_channel; fcp_cqidx++) {
7672 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
7673 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7674 "0526 Fast-path FCP CQ (%d) not "
7675 "allocated\n", fcp_cqidx);
7676 rc = -ENOMEM;
7677 goto out_destroy_fcp_cq;
7678 }
7679 rc = lpfc_cq_create(phba, phba->sli4_hba.fcp_cq[fcp_cqidx],
7680 phba->sli4_hba.hba_eq[fcp_cqidx], LPFC_WCQ, LPFC_FCP);
7681 if (rc) {
7682 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7683 "0527 Failed setup of fast-path FCP "
James Smarta2fc4aef2014-09-03 12:57:55 -04007684 "CQ (%d), rc = 0x%x\n", fcp_cqidx,
7685 (uint32_t)rc);
James Smart67d12732012-08-03 12:36:13 -04007686 goto out_destroy_fcp_cq;
7687 }
7688
7689 /* Setup fcp_cq_map for fast lookup */
7690 phba->sli4_hba.fcp_cq_map[fcp_cqidx] =
7691 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id;
7692
7693 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7694 "2588 FCP CQ setup: cq[%d]-id=%d, "
7695 "parent seq[%d]-id=%d\n",
7696 fcp_cqidx,
7697 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
7698 fcp_cqidx,
7699 phba->sli4_hba.hba_eq[fcp_cqidx]->queue_id);
7700 }
7701
7702 /* Set up fast-path FCP Work Queue */
7703 if (!phba->sli4_hba.fcp_wq) {
7704 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7705 "3149 Fast-path FCP WQ array not "
7706 "allocated\n");
7707 rc = -ENOMEM;
7708 goto out_destroy_fcp_cq;
7709 }
7710
7711 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_io_channel; fcp_wqidx++) {
7712 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
7713 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7714 "0534 Fast-path FCP WQ (%d) not "
7715 "allocated\n", fcp_wqidx);
7716 rc = -ENOMEM;
7717 goto out_destroy_fcp_wq;
7718 }
7719 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
7720 phba->sli4_hba.fcp_cq[fcp_wqidx],
7721 LPFC_FCP);
7722 if (rc) {
7723 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7724 "0535 Failed setup of fast-path FCP "
James Smarta2fc4aef2014-09-03 12:57:55 -04007725 "WQ (%d), rc = 0x%x\n", fcp_wqidx,
7726 (uint32_t)rc);
James Smart67d12732012-08-03 12:36:13 -04007727 goto out_destroy_fcp_wq;
7728 }
7729
7730 /* Bind this WQ to the next FCP ring */
7731 pring = &psli->ring[MAX_SLI3_CONFIGURED_RINGS + fcp_wqidx];
7732 pring->sli.sli4.wqp = (void *)phba->sli4_hba.fcp_wq[fcp_wqidx];
7733 phba->sli4_hba.fcp_cq[fcp_wqidx]->pring = pring;
7734
7735 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7736 "2591 FCP WQ setup: wq[%d]-id=%d, "
7737 "parent cq[%d]-id=%d\n",
7738 fcp_wqidx,
7739 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
7740 fcp_cq_index,
7741 phba->sli4_hba.fcp_cq[fcp_wqidx]->queue_id);
7742 }
James Smartda0436e2009-05-22 14:51:39 -04007743 /*
7744 * Set up Complete Queues (CQs)
7745 */
7746
7747 /* Set up slow-path MBOX Complete Queue as the first CQ */
7748 if (!phba->sli4_hba.mbx_cq) {
7749 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7750 "0528 Mailbox CQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007751 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007752 goto out_destroy_fcp_wq;
James Smartda0436e2009-05-22 14:51:39 -04007753 }
James Smart67d12732012-08-03 12:36:13 -04007754 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq,
7755 phba->sli4_hba.hba_eq[0], LPFC_MCQ, LPFC_MBOX);
James Smartda0436e2009-05-22 14:51:39 -04007756 if (rc) {
7757 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7758 "0529 Failed setup of slow-path mailbox CQ: "
James Smarta2fc4aef2014-09-03 12:57:55 -04007759 "rc = 0x%x\n", (uint32_t)rc);
James Smart67d12732012-08-03 12:36:13 -04007760 goto out_destroy_fcp_wq;
James Smartda0436e2009-05-22 14:51:39 -04007761 }
7762 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7763 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
7764 phba->sli4_hba.mbx_cq->queue_id,
James Smart67d12732012-08-03 12:36:13 -04007765 phba->sli4_hba.hba_eq[0]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007766
7767 /* Set up slow-path ELS Complete Queue */
7768 if (!phba->sli4_hba.els_cq) {
7769 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7770 "0530 ELS CQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007771 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04007772 goto out_destroy_mbx_cq;
7773 }
James Smart67d12732012-08-03 12:36:13 -04007774 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq,
7775 phba->sli4_hba.hba_eq[0], LPFC_WCQ, LPFC_ELS);
James Smartda0436e2009-05-22 14:51:39 -04007776 if (rc) {
7777 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7778 "0531 Failed setup of slow-path ELS CQ: "
James Smarta2fc4aef2014-09-03 12:57:55 -04007779 "rc = 0x%x\n", (uint32_t)rc);
James Smartda0436e2009-05-22 14:51:39 -04007780 goto out_destroy_mbx_cq;
7781 }
7782 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7783 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
7784 phba->sli4_hba.els_cq->queue_id,
James Smart67d12732012-08-03 12:36:13 -04007785 phba->sli4_hba.hba_eq[0]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007786
7787 /*
7788 * Set up all the Work Queues (WQs)
7789 */
7790
7791 /* Set up Mailbox Command Queue */
7792 if (!phba->sli4_hba.mbx_wq) {
7793 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7794 "0538 Slow-path MQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007795 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007796 goto out_destroy_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04007797 }
7798 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
7799 phba->sli4_hba.mbx_cq, LPFC_MBOX);
7800 if (rc) {
7801 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7802 "0539 Failed setup of slow-path MQ: "
7803 "rc = 0x%x\n", rc);
James Smart67d12732012-08-03 12:36:13 -04007804 goto out_destroy_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04007805 }
7806 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7807 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
7808 phba->sli4_hba.mbx_wq->queue_id,
7809 phba->sli4_hba.mbx_cq->queue_id);
7810
7811 /* Set up slow-path ELS Work Queue */
7812 if (!phba->sli4_hba.els_wq) {
7813 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7814 "0536 Slow-path ELS WQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007815 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04007816 goto out_destroy_mbx_wq;
7817 }
7818 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
7819 phba->sli4_hba.els_cq, LPFC_ELS);
7820 if (rc) {
7821 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7822 "0537 Failed setup of slow-path ELS WQ: "
James Smarta2fc4aef2014-09-03 12:57:55 -04007823 "rc = 0x%x\n", (uint32_t)rc);
James Smartda0436e2009-05-22 14:51:39 -04007824 goto out_destroy_mbx_wq;
7825 }
James Smart2a76a282012-08-03 12:35:54 -04007826
7827 /* Bind this WQ to the ELS ring */
7828 pring = &psli->ring[LPFC_ELS_RING];
7829 pring->sli.sli4.wqp = (void *)phba->sli4_hba.els_wq;
7830 phba->sli4_hba.els_cq->pring = pring;
7831
James Smartda0436e2009-05-22 14:51:39 -04007832 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7833 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
7834 phba->sli4_hba.els_wq->queue_id,
7835 phba->sli4_hba.els_cq->queue_id);
7836
James Smartda0436e2009-05-22 14:51:39 -04007837 /*
7838 * Create Receive Queue (RQ)
7839 */
7840 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
7841 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7842 "0540 Receive Queue not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007843 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007844 goto out_destroy_els_wq;
James Smartda0436e2009-05-22 14:51:39 -04007845 }
James Smart73d91e52011-10-10 21:32:10 -04007846
7847 lpfc_rq_adjust_repost(phba, phba->sli4_hba.hdr_rq, LPFC_ELS_HBQ);
7848 lpfc_rq_adjust_repost(phba, phba->sli4_hba.dat_rq, LPFC_ELS_HBQ);
7849
James Smartda0436e2009-05-22 14:51:39 -04007850 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04007851 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04007852 if (rc) {
7853 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7854 "0541 Failed setup of Receive Queue: "
James Smarta2fc4aef2014-09-03 12:57:55 -04007855 "rc = 0x%x\n", (uint32_t)rc);
James Smartda0436e2009-05-22 14:51:39 -04007856 goto out_destroy_fcp_wq;
7857 }
James Smart73d91e52011-10-10 21:32:10 -04007858
James Smartda0436e2009-05-22 14:51:39 -04007859 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7860 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
7861 "parent cq-id=%d\n",
7862 phba->sli4_hba.hdr_rq->queue_id,
7863 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04007864 phba->sli4_hba.els_cq->queue_id);
James Smart1ba981f2014-02-20 09:56:45 -05007865
7866 if (phba->cfg_fof) {
7867 rc = lpfc_fof_queue_setup(phba);
7868 if (rc) {
7869 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7870 "0549 Failed setup of FOF Queues: "
7871 "rc = 0x%x\n", rc);
7872 goto out_destroy_els_rq;
7873 }
7874 }
James Smart2c9c5a02015-04-07 15:07:15 -04007875
7876 /*
7877 * Configure EQ delay multipier for interrupt coalescing using
7878 * MODIFY_EQ_DELAY for all EQs created, LPFC_MAX_EQ_DELAY at a time.
7879 */
7880 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel;
7881 fcp_eqidx += LPFC_MAX_EQ_DELAY)
7882 lpfc_modify_fcp_eq_delay(phba, fcp_eqidx);
James Smartda0436e2009-05-22 14:51:39 -04007883 return 0;
7884
James Smart1ba981f2014-02-20 09:56:45 -05007885out_destroy_els_rq:
7886 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
James Smart2e90f4b2011-12-13 13:22:37 -05007887out_destroy_els_wq:
James Smartda0436e2009-05-22 14:51:39 -04007888 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7889out_destroy_mbx_wq:
7890 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
James Smart2e90f4b2011-12-13 13:22:37 -05007891out_destroy_els_cq:
James Smartda0436e2009-05-22 14:51:39 -04007892 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
7893out_destroy_mbx_cq:
7894 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
James Smart67d12732012-08-03 12:36:13 -04007895out_destroy_fcp_wq:
7896 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
7897 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
7898out_destroy_fcp_cq:
7899 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
7900 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
7901out_destroy_hba_eq:
James Smartda0436e2009-05-22 14:51:39 -04007902 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
James Smart67d12732012-08-03 12:36:13 -04007903 lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[fcp_eqidx]);
James Smartda0436e2009-05-22 14:51:39 -04007904out_error:
7905 return rc;
7906}
7907
7908/**
7909 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
7910 * @phba: pointer to lpfc hba data structure.
7911 *
7912 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
7913 * operation.
7914 *
7915 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007916 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007917 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007918 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007919 **/
7920void
7921lpfc_sli4_queue_unset(struct lpfc_hba *phba)
7922{
7923 int fcp_qidx;
7924
James Smart1ba981f2014-02-20 09:56:45 -05007925 /* Unset the queues created for Flash Optimized Fabric operations */
7926 if (phba->cfg_fof)
7927 lpfc_fof_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007928 /* Unset mailbox command work queue */
7929 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
7930 /* Unset ELS work queue */
7931 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7932 /* Unset unsolicited receive queue */
7933 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
7934 /* Unset FCP work queue */
James Smart67d12732012-08-03 12:36:13 -04007935 if (phba->sli4_hba.fcp_wq) {
7936 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
7937 fcp_qidx++)
7938 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
7939 }
James Smartda0436e2009-05-22 14:51:39 -04007940 /* Unset mailbox command complete queue */
7941 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
7942 /* Unset ELS complete queue */
7943 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
James Smartda0436e2009-05-22 14:51:39 -04007944 /* Unset FCP response complete queue */
James Smart2e90f4b2011-12-13 13:22:37 -05007945 if (phba->sli4_hba.fcp_cq) {
James Smart67d12732012-08-03 12:36:13 -04007946 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
7947 fcp_qidx++)
James Smart2e90f4b2011-12-13 13:22:37 -05007948 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05007949 }
James Smartda0436e2009-05-22 14:51:39 -04007950 /* Unset fast-path event queue */
James Smart67d12732012-08-03 12:36:13 -04007951 if (phba->sli4_hba.hba_eq) {
7952 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
James Smart2e90f4b2011-12-13 13:22:37 -05007953 fcp_qidx++)
James Smart67d12732012-08-03 12:36:13 -04007954 lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[fcp_qidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05007955 }
James Smartda0436e2009-05-22 14:51:39 -04007956}
7957
7958/**
7959 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
7960 * @phba: pointer to lpfc hba data structure.
7961 *
7962 * This routine is invoked to allocate and set up a pool of completion queue
7963 * events. The body of the completion queue event is a completion queue entry
7964 * CQE. For now, this pool is used for the interrupt service routine to queue
7965 * the following HBA completion queue events for the worker thread to process:
7966 * - Mailbox asynchronous events
7967 * - Receive queue completion unsolicited events
7968 * Later, this can be used for all the slow-path events.
7969 *
7970 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007971 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007972 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04007973 **/
7974static int
7975lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
7976{
7977 struct lpfc_cq_event *cq_event;
7978 int i;
7979
7980 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
7981 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
7982 if (!cq_event)
7983 goto out_pool_create_fail;
7984 list_add_tail(&cq_event->list,
7985 &phba->sli4_hba.sp_cqe_event_pool);
7986 }
7987 return 0;
7988
7989out_pool_create_fail:
7990 lpfc_sli4_cq_event_pool_destroy(phba);
7991 return -ENOMEM;
7992}
7993
7994/**
7995 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
7996 * @phba: pointer to lpfc hba data structure.
7997 *
7998 * This routine is invoked to free the pool of completion queue events at
7999 * driver unload time. Note that, it is the responsibility of the driver
8000 * cleanup routine to free all the outstanding completion-queue events
8001 * allocated from this pool back into the pool before invoking this routine
8002 * to destroy the pool.
8003 **/
8004static void
8005lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
8006{
8007 struct lpfc_cq_event *cq_event, *next_cq_event;
8008
8009 list_for_each_entry_safe(cq_event, next_cq_event,
8010 &phba->sli4_hba.sp_cqe_event_pool, list) {
8011 list_del(&cq_event->list);
8012 kfree(cq_event);
8013 }
8014}
8015
8016/**
8017 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
8018 * @phba: pointer to lpfc hba data structure.
8019 *
8020 * This routine is the lock free version of the API invoked to allocate a
8021 * completion-queue event from the free pool.
8022 *
8023 * Return: Pointer to the newly allocated completion-queue event if successful
8024 * NULL otherwise.
8025 **/
8026struct lpfc_cq_event *
8027__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
8028{
8029 struct lpfc_cq_event *cq_event = NULL;
8030
8031 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
8032 struct lpfc_cq_event, list);
8033 return cq_event;
8034}
8035
8036/**
8037 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
8038 * @phba: pointer to lpfc hba data structure.
8039 *
8040 * This routine is the lock version of the API invoked to allocate a
8041 * completion-queue event from the free pool.
8042 *
8043 * Return: Pointer to the newly allocated completion-queue event if successful
8044 * NULL otherwise.
8045 **/
8046struct lpfc_cq_event *
8047lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
8048{
8049 struct lpfc_cq_event *cq_event;
8050 unsigned long iflags;
8051
8052 spin_lock_irqsave(&phba->hbalock, iflags);
8053 cq_event = __lpfc_sli4_cq_event_alloc(phba);
8054 spin_unlock_irqrestore(&phba->hbalock, iflags);
8055 return cq_event;
8056}
8057
8058/**
8059 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
8060 * @phba: pointer to lpfc hba data structure.
8061 * @cq_event: pointer to the completion queue event to be freed.
8062 *
8063 * This routine is the lock free version of the API invoked to release a
8064 * completion-queue event back into the free pool.
8065 **/
8066void
8067__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
8068 struct lpfc_cq_event *cq_event)
8069{
8070 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
8071}
8072
8073/**
8074 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
8075 * @phba: pointer to lpfc hba data structure.
8076 * @cq_event: pointer to the completion queue event to be freed.
8077 *
8078 * This routine is the lock version of the API invoked to release a
8079 * completion-queue event back into the free pool.
8080 **/
8081void
8082lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
8083 struct lpfc_cq_event *cq_event)
8084{
8085 unsigned long iflags;
8086 spin_lock_irqsave(&phba->hbalock, iflags);
8087 __lpfc_sli4_cq_event_release(phba, cq_event);
8088 spin_unlock_irqrestore(&phba->hbalock, iflags);
8089}
8090
8091/**
8092 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
8093 * @phba: pointer to lpfc hba data structure.
8094 *
8095 * This routine is to free all the pending completion-queue events to the
8096 * back into the free pool for device reset.
8097 **/
8098static void
8099lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
8100{
8101 LIST_HEAD(cqelist);
8102 struct lpfc_cq_event *cqe;
8103 unsigned long iflags;
8104
8105 /* Retrieve all the pending WCQEs from pending WCQE lists */
8106 spin_lock_irqsave(&phba->hbalock, iflags);
8107 /* Pending FCP XRI abort events */
8108 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
8109 &cqelist);
8110 /* Pending ELS XRI abort events */
8111 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
8112 &cqelist);
8113 /* Pending asynnc events */
8114 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
8115 &cqelist);
8116 spin_unlock_irqrestore(&phba->hbalock, iflags);
8117
8118 while (!list_empty(&cqelist)) {
8119 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
8120 lpfc_sli4_cq_event_release(phba, cqe);
8121 }
8122}
8123
8124/**
8125 * lpfc_pci_function_reset - Reset pci function.
8126 * @phba: pointer to lpfc hba data structure.
8127 *
8128 * This routine is invoked to request a PCI function reset. It will destroys
8129 * all resources assigned to the PCI function which originates this request.
8130 *
8131 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008132 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008133 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04008134 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04008135 **/
8136int
8137lpfc_pci_function_reset(struct lpfc_hba *phba)
8138{
8139 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05008140 uint32_t rc = 0, if_type;
James Smartda0436e2009-05-22 14:51:39 -04008141 uint32_t shdr_status, shdr_add_status;
James Smart2f6fa2c2014-09-03 12:57:08 -04008142 uint32_t rdy_chk;
8143 uint32_t port_reset = 0;
James Smartda0436e2009-05-22 14:51:39 -04008144 union lpfc_sli4_cfg_shdr *shdr;
James Smart2fcee4b2010-12-15 17:57:46 -05008145 struct lpfc_register reg_data;
James Smart2b81f942012-03-01 22:37:18 -05008146 uint16_t devid;
James Smartda0436e2009-05-22 14:51:39 -04008147
James Smart2fcee4b2010-12-15 17:57:46 -05008148 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
8149 switch (if_type) {
8150 case LPFC_SLI_INTF_IF_TYPE_0:
8151 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
8152 GFP_KERNEL);
8153 if (!mboxq) {
8154 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8155 "0494 Unable to allocate memory for "
8156 "issuing SLI_FUNCTION_RESET mailbox "
8157 "command\n");
8158 return -ENOMEM;
8159 }
8160
8161 /* Setup PCI function reset mailbox-ioctl command */
8162 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
8163 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
8164 LPFC_SLI4_MBX_EMBED);
8165 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8166 shdr = (union lpfc_sli4_cfg_shdr *)
8167 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
8168 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
8169 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
8170 &shdr->response);
8171 if (rc != MBX_TIMEOUT)
8172 mempool_free(mboxq, phba->mbox_mem_pool);
8173 if (shdr_status || shdr_add_status || rc) {
8174 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8175 "0495 SLI_FUNCTION_RESET mailbox "
8176 "failed with status x%x add_status x%x,"
8177 " mbx status x%x\n",
8178 shdr_status, shdr_add_status, rc);
8179 rc = -ENXIO;
8180 }
8181 break;
8182 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart2f6fa2c2014-09-03 12:57:08 -04008183wait:
8184 /*
8185 * Poll the Port Status Register and wait for RDY for
8186 * up to 30 seconds. If the port doesn't respond, treat
8187 * it as an error.
8188 */
James Smart77d093f2015-04-07 15:07:08 -04008189 for (rdy_chk = 0; rdy_chk < 1500; rdy_chk++) {
James Smart2f6fa2c2014-09-03 12:57:08 -04008190 if (lpfc_readl(phba->sli4_hba.u.if_type2.
8191 STATUSregaddr, &reg_data.word0)) {
8192 rc = -ENODEV;
8193 goto out;
8194 }
8195 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
8196 break;
8197 msleep(20);
8198 }
8199
8200 if (!bf_get(lpfc_sliport_status_rdy, &reg_data)) {
8201 phba->work_status[0] = readl(
8202 phba->sli4_hba.u.if_type2.ERR1regaddr);
8203 phba->work_status[1] = readl(
8204 phba->sli4_hba.u.if_type2.ERR2regaddr);
8205 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8206 "2890 Port not ready, port status reg "
8207 "0x%x error 1=0x%x, error 2=0x%x\n",
8208 reg_data.word0,
8209 phba->work_status[0],
8210 phba->work_status[1]);
8211 rc = -ENODEV;
8212 goto out;
8213 }
8214
8215 if (!port_reset) {
8216 /*
8217 * Reset the port now
8218 */
James Smart2fcee4b2010-12-15 17:57:46 -05008219 reg_data.word0 = 0;
8220 bf_set(lpfc_sliport_ctrl_end, &reg_data,
8221 LPFC_SLIPORT_LITTLE_ENDIAN);
8222 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
8223 LPFC_SLIPORT_INIT_PORT);
8224 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
8225 CTRLregaddr);
James Smart8fcb8ac2012-03-01 22:35:58 -05008226 /* flush */
James Smart2b81f942012-03-01 22:37:18 -05008227 pci_read_config_word(phba->pcidev,
8228 PCI_DEVICE_ID, &devid);
James Smart2fcee4b2010-12-15 17:57:46 -05008229
James Smart2f6fa2c2014-09-03 12:57:08 -04008230 port_reset = 1;
8231 msleep(20);
8232 goto wait;
8233 } else if (bf_get(lpfc_sliport_status_rn, &reg_data)) {
8234 rc = -ENODEV;
8235 goto out;
James Smart2fcee4b2010-12-15 17:57:46 -05008236 }
8237 break;
James Smart2f6fa2c2014-09-03 12:57:08 -04008238
James Smart2fcee4b2010-12-15 17:57:46 -05008239 case LPFC_SLI_INTF_IF_TYPE_1:
8240 default:
8241 break;
James Smartda0436e2009-05-22 14:51:39 -04008242 }
8243
James Smart73d91e52011-10-10 21:32:10 -04008244out:
James Smart2fcee4b2010-12-15 17:57:46 -05008245 /* Catch the not-ready port failure after a port reset. */
James Smart2f6fa2c2014-09-03 12:57:08 -04008246 if (rc) {
James Smart229adb02013-04-17 20:16:51 -04008247 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8248 "3317 HBA not functional: IP Reset Failed "
James Smart2f6fa2c2014-09-03 12:57:08 -04008249 "try: echo fw_reset > board_mode\n");
James Smart2fcee4b2010-12-15 17:57:46 -05008250 rc = -ENODEV;
James Smart229adb02013-04-17 20:16:51 -04008251 }
James Smart2fcee4b2010-12-15 17:57:46 -05008252
James Smartda0436e2009-05-22 14:51:39 -04008253 return rc;
8254}
8255
8256/**
James Smartda0436e2009-05-22 14:51:39 -04008257 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
8258 * @phba: pointer to lpfc hba data structure.
8259 *
8260 * This routine is invoked to set up the PCI device memory space for device
8261 * with SLI-4 interface spec.
8262 *
8263 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008264 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008265 * other values - error
8266 **/
8267static int
8268lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
8269{
8270 struct pci_dev *pdev;
8271 unsigned long bar0map_len, bar1map_len, bar2map_len;
8272 int error = -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -05008273 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04008274
8275 /* Obtain PCI device reference */
8276 if (!phba->pcidev)
8277 return error;
8278 else
8279 pdev = phba->pcidev;
8280
8281 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05008282 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
8283 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
8284 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
8285 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smartda0436e2009-05-22 14:51:39 -04008286 return error;
Michael Reed8e685972009-09-18 12:02:05 -05008287 }
8288 }
James Smartda0436e2009-05-22 14:51:39 -04008289
James Smart2fcee4b2010-12-15 17:57:46 -05008290 /*
8291 * The BARs and register set definitions and offset locations are
8292 * dependent on the if_type.
8293 */
8294 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
8295 &phba->sli4_hba.sli_intf.word0)) {
8296 return error;
8297 }
8298
8299 /* There is no SLI3 failback for SLI4 devices. */
8300 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
8301 LPFC_SLI_INTF_VALID) {
8302 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8303 "2894 SLI_INTF reg contents invalid "
8304 "sli_intf reg 0x%x\n",
8305 phba->sli4_hba.sli_intf.word0);
8306 return error;
8307 }
8308
8309 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
8310 /*
8311 * Get the bus address of SLI4 device Bar regions and the
8312 * number of bytes required by each mapping. The mapping of the
8313 * particular PCI BARs regions is dependent on the type of
8314 * SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -04008315 */
James Smartf5ca6f22013-09-06 12:21:09 -04008316 if (pci_resource_start(pdev, PCI_64BIT_BAR0)) {
8317 phba->pci_bar0_map = pci_resource_start(pdev, PCI_64BIT_BAR0);
8318 bar0map_len = pci_resource_len(pdev, PCI_64BIT_BAR0);
James Smart2fcee4b2010-12-15 17:57:46 -05008319
8320 /*
8321 * Map SLI4 PCI Config Space Register base to a kernel virtual
8322 * addr
8323 */
8324 phba->sli4_hba.conf_regs_memmap_p =
8325 ioremap(phba->pci_bar0_map, bar0map_len);
8326 if (!phba->sli4_hba.conf_regs_memmap_p) {
8327 dev_printk(KERN_ERR, &pdev->dev,
8328 "ioremap failed for SLI4 PCI config "
8329 "registers.\n");
8330 goto out;
8331 }
James Smartf5ca6f22013-09-06 12:21:09 -04008332 phba->pci_bar0_memmap_p = phba->sli4_hba.conf_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05008333 /* Set up BAR0 PCI config space register memory map */
8334 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smart1dfb5a42010-02-12 14:40:50 -05008335 } else {
8336 phba->pci_bar0_map = pci_resource_start(pdev, 1);
8337 bar0map_len = pci_resource_len(pdev, 1);
James Smart2fcee4b2010-12-15 17:57:46 -05008338 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8339 dev_printk(KERN_ERR, &pdev->dev,
8340 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
8341 goto out;
8342 }
8343 phba->sli4_hba.conf_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04008344 ioremap(phba->pci_bar0_map, bar0map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05008345 if (!phba->sli4_hba.conf_regs_memmap_p) {
8346 dev_printk(KERN_ERR, &pdev->dev,
8347 "ioremap failed for SLI4 PCI config "
8348 "registers.\n");
8349 goto out;
8350 }
8351 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smartda0436e2009-05-22 14:51:39 -04008352 }
8353
James Smartc31098c2011-04-16 11:03:33 -04008354 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
James Smartf5ca6f22013-09-06 12:21:09 -04008355 (pci_resource_start(pdev, PCI_64BIT_BAR2))) {
James Smart2fcee4b2010-12-15 17:57:46 -05008356 /*
8357 * Map SLI4 if type 0 HBA Control Register base to a kernel
8358 * virtual address and setup the registers.
8359 */
James Smartf5ca6f22013-09-06 12:21:09 -04008360 phba->pci_bar1_map = pci_resource_start(pdev, PCI_64BIT_BAR2);
8361 bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
James Smart2fcee4b2010-12-15 17:57:46 -05008362 phba->sli4_hba.ctrl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04008363 ioremap(phba->pci_bar1_map, bar1map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05008364 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
8365 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04008366 "ioremap failed for SLI4 HBA control registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05008367 goto out_iounmap_conf;
8368 }
James Smartf5ca6f22013-09-06 12:21:09 -04008369 phba->pci_bar2_memmap_p = phba->sli4_hba.ctrl_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05008370 lpfc_sli4_bar1_register_memmap(phba);
James Smartda0436e2009-05-22 14:51:39 -04008371 }
8372
James Smartc31098c2011-04-16 11:03:33 -04008373 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
James Smartf5ca6f22013-09-06 12:21:09 -04008374 (pci_resource_start(pdev, PCI_64BIT_BAR4))) {
James Smart2fcee4b2010-12-15 17:57:46 -05008375 /*
8376 * Map SLI4 if type 0 HBA Doorbell Register base to a kernel
8377 * virtual address and setup the registers.
8378 */
James Smartf5ca6f22013-09-06 12:21:09 -04008379 phba->pci_bar2_map = pci_resource_start(pdev, PCI_64BIT_BAR4);
8380 bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
James Smart2fcee4b2010-12-15 17:57:46 -05008381 phba->sli4_hba.drbl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04008382 ioremap(phba->pci_bar2_map, bar2map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05008383 if (!phba->sli4_hba.drbl_regs_memmap_p) {
8384 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04008385 "ioremap failed for SLI4 HBA doorbell registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05008386 goto out_iounmap_ctrl;
8387 }
James Smartf5ca6f22013-09-06 12:21:09 -04008388 phba->pci_bar4_memmap_p = phba->sli4_hba.drbl_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05008389 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
8390 if (error)
8391 goto out_iounmap_all;
James Smartda0436e2009-05-22 14:51:39 -04008392 }
8393
James Smartda0436e2009-05-22 14:51:39 -04008394 return 0;
8395
8396out_iounmap_all:
8397 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
8398out_iounmap_ctrl:
8399 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
8400out_iounmap_conf:
8401 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8402out:
8403 return error;
8404}
8405
8406/**
8407 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
8408 * @phba: pointer to lpfc hba data structure.
8409 *
8410 * This routine is invoked to unset the PCI device memory space for device
8411 * with SLI-4 interface spec.
8412 **/
8413static void
8414lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
8415{
James Smart2e90f4b2011-12-13 13:22:37 -05008416 uint32_t if_type;
8417 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smartda0436e2009-05-22 14:51:39 -04008418
James Smart2e90f4b2011-12-13 13:22:37 -05008419 switch (if_type) {
8420 case LPFC_SLI_INTF_IF_TYPE_0:
8421 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
8422 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
8423 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8424 break;
8425 case LPFC_SLI_INTF_IF_TYPE_2:
8426 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8427 break;
8428 case LPFC_SLI_INTF_IF_TYPE_1:
8429 default:
8430 dev_printk(KERN_ERR, &phba->pcidev->dev,
8431 "FATAL - unsupported SLI4 interface type - %d\n",
8432 if_type);
8433 break;
8434 }
James Smartda0436e2009-05-22 14:51:39 -04008435}
8436
8437/**
James Smart3772a992009-05-22 14:50:54 -04008438 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
8439 * @phba: pointer to lpfc hba data structure.
8440 *
8441 * This routine is invoked to enable the MSI-X interrupt vectors to device
Alexander Gordeev4f871e12014-09-03 12:56:29 -04008442 * with SLI-3 interface specs. The kernel function pci_enable_msix_exact()
8443 * is called to enable the MSI-X vectors. Note that pci_enable_msix_exact(),
8444 * once invoked, enables either all or nothing, depending on the current
James Smart3772a992009-05-22 14:50:54 -04008445 * availability of PCI vector resources. The device driver is responsible
8446 * for calling the individual request_irq() to register each MSI-X vector
8447 * with a interrupt handler, which is done in this function. Note that
8448 * later when device is unloading, the driver should always call free_irq()
8449 * on all MSI-X vectors it has done request_irq() on before calling
8450 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
8451 * will be left with MSI-X enabled and leaks its vectors.
8452 *
8453 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008454 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008455 * other values - error
8456 **/
8457static int
8458lpfc_sli_enable_msix(struct lpfc_hba *phba)
8459{
8460 int rc, i;
8461 LPFC_MBOXQ_t *pmb;
8462
8463 /* Set up MSI-X multi-message vectors */
8464 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8465 phba->msix_entries[i].entry = i;
8466
8467 /* Configure MSI-X capability structure */
Alexander Gordeev4f871e12014-09-03 12:56:29 -04008468 rc = pci_enable_msix_exact(phba->pcidev, phba->msix_entries,
8469 LPFC_MSIX_VECTORS);
James Smart3772a992009-05-22 14:50:54 -04008470 if (rc) {
8471 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8472 "0420 PCI enable MSI-X failed (%d)\n", rc);
Alexander Gordeev029165a2014-07-16 20:05:15 +02008473 goto vec_fail_out;
James Smart3772a992009-05-22 14:50:54 -04008474 }
8475 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8476 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8477 "0477 MSI-X entry[%d]: vector=x%x "
8478 "message=%d\n", i,
8479 phba->msix_entries[i].vector,
8480 phba->msix_entries[i].entry);
8481 /*
8482 * Assign MSI-X vectors to interrupt handlers
8483 */
8484
8485 /* vector-0 is associated to slow-path handler */
8486 rc = request_irq(phba->msix_entries[0].vector,
James Smarted243d32015-05-21 13:55:25 -04008487 &lpfc_sli_sp_intr_handler, 0,
James Smart3772a992009-05-22 14:50:54 -04008488 LPFC_SP_DRIVER_HANDLER_NAME, phba);
8489 if (rc) {
8490 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8491 "0421 MSI-X slow-path request_irq failed "
8492 "(%d)\n", rc);
8493 goto msi_fail_out;
8494 }
8495
8496 /* vector-1 is associated to fast-path handler */
8497 rc = request_irq(phba->msix_entries[1].vector,
James Smarted243d32015-05-21 13:55:25 -04008498 &lpfc_sli_fp_intr_handler, 0,
James Smart3772a992009-05-22 14:50:54 -04008499 LPFC_FP_DRIVER_HANDLER_NAME, phba);
8500
8501 if (rc) {
8502 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8503 "0429 MSI-X fast-path request_irq failed "
8504 "(%d)\n", rc);
8505 goto irq_fail_out;
8506 }
8507
8508 /*
8509 * Configure HBA MSI-X attention conditions to messages
8510 */
8511 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8512
8513 if (!pmb) {
8514 rc = -ENOMEM;
8515 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8516 "0474 Unable to allocate memory for issuing "
8517 "MBOX_CONFIG_MSI command\n");
8518 goto mem_fail_out;
8519 }
8520 rc = lpfc_config_msi(phba, pmb);
8521 if (rc)
8522 goto mbx_fail_out;
8523 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
8524 if (rc != MBX_SUCCESS) {
8525 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
8526 "0351 Config MSI mailbox command failed, "
8527 "mbxCmd x%x, mbxStatus x%x\n",
8528 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
8529 goto mbx_fail_out;
8530 }
8531
8532 /* Free memory allocated for mailbox command */
8533 mempool_free(pmb, phba->mbox_mem_pool);
8534 return rc;
8535
8536mbx_fail_out:
8537 /* Free memory allocated for mailbox command */
8538 mempool_free(pmb, phba->mbox_mem_pool);
8539
8540mem_fail_out:
8541 /* free the irq already requested */
8542 free_irq(phba->msix_entries[1].vector, phba);
8543
8544irq_fail_out:
8545 /* free the irq already requested */
8546 free_irq(phba->msix_entries[0].vector, phba);
8547
8548msi_fail_out:
8549 /* Unconfigure MSI-X capability structure */
8550 pci_disable_msix(phba->pcidev);
Alexander Gordeev029165a2014-07-16 20:05:15 +02008551
8552vec_fail_out:
James Smart3772a992009-05-22 14:50:54 -04008553 return rc;
8554}
8555
8556/**
8557 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
8558 * @phba: pointer to lpfc hba data structure.
8559 *
8560 * This routine is invoked to release the MSI-X vectors and then disable the
8561 * MSI-X interrupt mode to device with SLI-3 interface spec.
8562 **/
8563static void
8564lpfc_sli_disable_msix(struct lpfc_hba *phba)
8565{
8566 int i;
8567
8568 /* Free up MSI-X multi-message vectors */
8569 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8570 free_irq(phba->msix_entries[i].vector, phba);
8571 /* Disable MSI-X */
8572 pci_disable_msix(phba->pcidev);
8573
8574 return;
8575}
8576
8577/**
8578 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
8579 * @phba: pointer to lpfc hba data structure.
8580 *
8581 * This routine is invoked to enable the MSI interrupt mode to device with
8582 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
8583 * enable the MSI vector. The device driver is responsible for calling the
8584 * request_irq() to register MSI vector with a interrupt the handler, which
8585 * is done in this function.
8586 *
8587 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008588 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008589 * other values - error
8590 */
8591static int
8592lpfc_sli_enable_msi(struct lpfc_hba *phba)
8593{
8594 int rc;
8595
8596 rc = pci_enable_msi(phba->pcidev);
8597 if (!rc)
8598 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8599 "0462 PCI enable MSI mode success.\n");
8600 else {
8601 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8602 "0471 PCI enable MSI mode failed (%d)\n", rc);
8603 return rc;
8604 }
8605
8606 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
James Smarted243d32015-05-21 13:55:25 -04008607 0, LPFC_DRIVER_NAME, phba);
James Smart3772a992009-05-22 14:50:54 -04008608 if (rc) {
8609 pci_disable_msi(phba->pcidev);
8610 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8611 "0478 MSI request_irq failed (%d)\n", rc);
8612 }
8613 return rc;
8614}
8615
8616/**
8617 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
8618 * @phba: pointer to lpfc hba data structure.
8619 *
8620 * This routine is invoked to disable the MSI interrupt mode to device with
8621 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
8622 * done request_irq() on before calling pci_disable_msi(). Failure to do so
8623 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
8624 * its vector.
8625 */
8626static void
8627lpfc_sli_disable_msi(struct lpfc_hba *phba)
8628{
8629 free_irq(phba->pcidev->irq, phba);
8630 pci_disable_msi(phba->pcidev);
8631 return;
8632}
8633
8634/**
8635 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
8636 * @phba: pointer to lpfc hba data structure.
8637 *
8638 * This routine is invoked to enable device interrupt and associate driver's
8639 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
8640 * spec. Depends on the interrupt mode configured to the driver, the driver
8641 * will try to fallback from the configured interrupt mode to an interrupt
8642 * mode which is supported by the platform, kernel, and device in the order
8643 * of:
8644 * MSI-X -> MSI -> IRQ.
8645 *
8646 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008647 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008648 * other values - error
8649 **/
8650static uint32_t
8651lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
8652{
8653 uint32_t intr_mode = LPFC_INTR_ERROR;
8654 int retval;
8655
8656 if (cfg_mode == 2) {
8657 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
8658 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
8659 if (!retval) {
8660 /* Now, try to enable MSI-X interrupt mode */
8661 retval = lpfc_sli_enable_msix(phba);
8662 if (!retval) {
8663 /* Indicate initialization to MSI-X mode */
8664 phba->intr_type = MSIX;
8665 intr_mode = 2;
8666 }
8667 }
8668 }
8669
8670 /* Fallback to MSI if MSI-X initialization failed */
8671 if (cfg_mode >= 1 && phba->intr_type == NONE) {
8672 retval = lpfc_sli_enable_msi(phba);
8673 if (!retval) {
8674 /* Indicate initialization to MSI mode */
8675 phba->intr_type = MSI;
8676 intr_mode = 1;
8677 }
8678 }
8679
8680 /* Fallback to INTx if both MSI-X/MSI initalization failed */
8681 if (phba->intr_type == NONE) {
8682 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
8683 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8684 if (!retval) {
8685 /* Indicate initialization to INTx mode */
8686 phba->intr_type = INTx;
8687 intr_mode = 0;
8688 }
8689 }
8690 return intr_mode;
8691}
8692
8693/**
8694 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
8695 * @phba: pointer to lpfc hba data structure.
8696 *
8697 * This routine is invoked to disable device interrupt and disassociate the
8698 * driver's interrupt handler(s) from interrupt vector(s) to device with
8699 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
8700 * release the interrupt vector(s) for the message signaled interrupt.
8701 **/
8702static void
8703lpfc_sli_disable_intr(struct lpfc_hba *phba)
8704{
8705 /* Disable the currently initialized interrupt mode */
8706 if (phba->intr_type == MSIX)
8707 lpfc_sli_disable_msix(phba);
8708 else if (phba->intr_type == MSI)
8709 lpfc_sli_disable_msi(phba);
8710 else if (phba->intr_type == INTx)
8711 free_irq(phba->pcidev->irq, phba);
8712
8713 /* Reset interrupt management states */
8714 phba->intr_type = NONE;
8715 phba->sli.slistat.sli_intr = 0;
8716
8717 return;
8718}
8719
8720/**
James Smart7bb03bb2013-04-17 20:19:16 -04008721 * lpfc_find_next_cpu - Find next available CPU that matches the phys_id
8722 * @phba: pointer to lpfc hba data structure.
8723 *
8724 * Find next available CPU to use for IRQ to CPU affinity.
8725 */
8726static int
8727lpfc_find_next_cpu(struct lpfc_hba *phba, uint32_t phys_id)
8728{
8729 struct lpfc_vector_map_info *cpup;
8730 int cpu;
8731
8732 cpup = phba->sli4_hba.cpu_map;
8733 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8734 /* CPU must be online */
8735 if (cpu_online(cpu)) {
8736 if ((cpup->irq == LPFC_VECTOR_MAP_EMPTY) &&
8737 (lpfc_used_cpu[cpu] == LPFC_VECTOR_MAP_EMPTY) &&
8738 (cpup->phys_id == phys_id)) {
8739 return cpu;
8740 }
8741 }
8742 cpup++;
8743 }
8744
8745 /*
8746 * If we get here, we have used ALL CPUs for the specific
8747 * phys_id. Now we need to clear out lpfc_used_cpu and start
8748 * reusing CPUs.
8749 */
8750
8751 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8752 if (lpfc_used_cpu[cpu] == phys_id)
8753 lpfc_used_cpu[cpu] = LPFC_VECTOR_MAP_EMPTY;
8754 }
8755
8756 cpup = phba->sli4_hba.cpu_map;
8757 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8758 /* CPU must be online */
8759 if (cpu_online(cpu)) {
8760 if ((cpup->irq == LPFC_VECTOR_MAP_EMPTY) &&
8761 (cpup->phys_id == phys_id)) {
8762 return cpu;
8763 }
8764 }
8765 cpup++;
8766 }
8767 return LPFC_VECTOR_MAP_EMPTY;
8768}
8769
8770/**
8771 * lpfc_sli4_set_affinity - Set affinity for HBA IRQ vectors
8772 * @phba: pointer to lpfc hba data structure.
8773 * @vectors: number of HBA vectors
8774 *
8775 * Affinitize MSIX IRQ vectors to CPUs. Try to equally spread vector
8776 * affinization across multple physical CPUs (numa nodes).
8777 * In addition, this routine will assign an IO channel for each CPU
8778 * to use when issuing I/Os.
8779 */
8780static int
8781lpfc_sli4_set_affinity(struct lpfc_hba *phba, int vectors)
8782{
8783 int i, idx, saved_chann, used_chann, cpu, phys_id;
James Smartec2087a2013-09-06 12:20:36 -04008784 int max_phys_id, min_phys_id;
8785 int num_io_channel, first_cpu, chan;
James Smart7bb03bb2013-04-17 20:19:16 -04008786 struct lpfc_vector_map_info *cpup;
8787#ifdef CONFIG_X86
8788 struct cpuinfo_x86 *cpuinfo;
8789#endif
James Smart7bb03bb2013-04-17 20:19:16 -04008790 uint8_t chann[LPFC_FCP_IO_CHAN_MAX+1];
8791
8792 /* If there is no mapping, just return */
8793 if (!phba->cfg_fcp_cpu_map)
8794 return 1;
8795
8796 /* Init cpu_map array */
8797 memset(phba->sli4_hba.cpu_map, 0xff,
8798 (sizeof(struct lpfc_vector_map_info) *
8799 phba->sli4_hba.num_present_cpu));
8800
8801 max_phys_id = 0;
James Smartec2087a2013-09-06 12:20:36 -04008802 min_phys_id = 0xff;
James Smart7bb03bb2013-04-17 20:19:16 -04008803 phys_id = 0;
8804 num_io_channel = 0;
8805 first_cpu = LPFC_VECTOR_MAP_EMPTY;
8806
8807 /* Update CPU map with physical id and core id of each CPU */
8808 cpup = phba->sli4_hba.cpu_map;
8809 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8810#ifdef CONFIG_X86
8811 cpuinfo = &cpu_data(cpu);
8812 cpup->phys_id = cpuinfo->phys_proc_id;
8813 cpup->core_id = cpuinfo->cpu_core_id;
8814#else
8815 /* No distinction between CPUs for other platforms */
8816 cpup->phys_id = 0;
8817 cpup->core_id = 0;
8818#endif
8819
8820 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8821 "3328 CPU physid %d coreid %d\n",
8822 cpup->phys_id, cpup->core_id);
8823
8824 if (cpup->phys_id > max_phys_id)
8825 max_phys_id = cpup->phys_id;
James Smartec2087a2013-09-06 12:20:36 -04008826 if (cpup->phys_id < min_phys_id)
8827 min_phys_id = cpup->phys_id;
James Smart7bb03bb2013-04-17 20:19:16 -04008828 cpup++;
8829 }
8830
James Smartec2087a2013-09-06 12:20:36 -04008831 phys_id = min_phys_id;
James Smart7bb03bb2013-04-17 20:19:16 -04008832 /* Now associate the HBA vectors with specific CPUs */
8833 for (idx = 0; idx < vectors; idx++) {
8834 cpup = phba->sli4_hba.cpu_map;
8835 cpu = lpfc_find_next_cpu(phba, phys_id);
8836 if (cpu == LPFC_VECTOR_MAP_EMPTY) {
8837
8838 /* Try for all phys_id's */
8839 for (i = 1; i < max_phys_id; i++) {
8840 phys_id++;
8841 if (phys_id > max_phys_id)
James Smartec2087a2013-09-06 12:20:36 -04008842 phys_id = min_phys_id;
James Smart7bb03bb2013-04-17 20:19:16 -04008843 cpu = lpfc_find_next_cpu(phba, phys_id);
8844 if (cpu == LPFC_VECTOR_MAP_EMPTY)
8845 continue;
8846 goto found;
8847 }
8848
James Smartec2087a2013-09-06 12:20:36 -04008849 /* Use round robin for scheduling */
8850 phba->cfg_fcp_io_sched = LPFC_FCP_SCHED_ROUND_ROBIN;
8851 chan = 0;
8852 cpup = phba->sli4_hba.cpu_map;
8853 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
8854 cpup->channel_id = chan;
8855 cpup++;
8856 chan++;
8857 if (chan >= phba->cfg_fcp_io_channel)
8858 chan = 0;
8859 }
8860
James Smart7bb03bb2013-04-17 20:19:16 -04008861 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8862 "3329 Cannot set affinity:"
8863 "Error mapping vector %d (%d)\n",
8864 idx, vectors);
8865 return 0;
8866 }
8867found:
8868 cpup += cpu;
8869 if (phba->cfg_fcp_cpu_map == LPFC_DRIVER_CPU_MAP)
8870 lpfc_used_cpu[cpu] = phys_id;
8871
8872 /* Associate vector with selected CPU */
8873 cpup->irq = phba->sli4_hba.msix_entries[idx].vector;
8874
8875 /* Associate IO channel with selected CPU */
8876 cpup->channel_id = idx;
8877 num_io_channel++;
8878
8879 if (first_cpu == LPFC_VECTOR_MAP_EMPTY)
8880 first_cpu = cpu;
8881
8882 /* Now affinitize to the selected CPU */
James Smart7bb03bb2013-04-17 20:19:16 -04008883 i = irq_set_affinity_hint(phba->sli4_hba.msix_entries[idx].
Ian Mitchellc0365c02015-08-31 16:48:12 -04008884 vector, get_cpu_mask(cpu));
James Smart7bb03bb2013-04-17 20:19:16 -04008885
8886 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8887 "3330 Set Affinity: CPU %d channel %d "
8888 "irq %d (%x)\n",
8889 cpu, cpup->channel_id,
8890 phba->sli4_hba.msix_entries[idx].vector, i);
8891
8892 /* Spread vector mapping across multple physical CPU nodes */
8893 phys_id++;
8894 if (phys_id > max_phys_id)
James Smartec2087a2013-09-06 12:20:36 -04008895 phys_id = min_phys_id;
James Smart7bb03bb2013-04-17 20:19:16 -04008896 }
8897
8898 /*
8899 * Finally fill in the IO channel for any remaining CPUs.
8900 * At this point, all IO channels have been assigned to a specific
8901 * MSIx vector, mapped to a specific CPU.
8902 * Base the remaining IO channel assigned, to IO channels already
8903 * assigned to other CPUs on the same phys_id.
8904 */
James Smartec2087a2013-09-06 12:20:36 -04008905 for (i = min_phys_id; i <= max_phys_id; i++) {
James Smart7bb03bb2013-04-17 20:19:16 -04008906 /*
8907 * If there are no io channels already mapped to
8908 * this phys_id, just round robin thru the io_channels.
8909 * Setup chann[] for round robin.
8910 */
8911 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++)
8912 chann[idx] = idx;
8913
8914 saved_chann = 0;
8915 used_chann = 0;
8916
8917 /*
8918 * First build a list of IO channels already assigned
8919 * to this phys_id before reassigning the same IO
8920 * channels to the remaining CPUs.
8921 */
8922 cpup = phba->sli4_hba.cpu_map;
8923 cpu = first_cpu;
8924 cpup += cpu;
8925 for (idx = 0; idx < phba->sli4_hba.num_present_cpu;
8926 idx++) {
8927 if (cpup->phys_id == i) {
8928 /*
8929 * Save any IO channels that are
8930 * already mapped to this phys_id.
8931 */
8932 if (cpup->irq != LPFC_VECTOR_MAP_EMPTY) {
James Smart6690e0d2015-12-16 18:11:56 -05008933 if (saved_chann <=
8934 LPFC_FCP_IO_CHAN_MAX) {
8935 chann[saved_chann] =
8936 cpup->channel_id;
8937 saved_chann++;
8938 }
James Smart7bb03bb2013-04-17 20:19:16 -04008939 goto out;
8940 }
8941
8942 /* See if we are using round-robin */
8943 if (saved_chann == 0)
8944 saved_chann =
8945 phba->cfg_fcp_io_channel;
8946
8947 /* Associate next IO channel with CPU */
8948 cpup->channel_id = chann[used_chann];
8949 num_io_channel++;
8950 used_chann++;
8951 if (used_chann == saved_chann)
8952 used_chann = 0;
8953
8954 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8955 "3331 Set IO_CHANN "
8956 "CPU %d channel %d\n",
8957 idx, cpup->channel_id);
8958 }
8959out:
8960 cpu++;
8961 if (cpu >= phba->sli4_hba.num_present_cpu) {
8962 cpup = phba->sli4_hba.cpu_map;
8963 cpu = 0;
8964 } else {
8965 cpup++;
8966 }
8967 }
8968 }
8969
8970 if (phba->sli4_hba.num_online_cpu != phba->sli4_hba.num_present_cpu) {
8971 cpup = phba->sli4_hba.cpu_map;
8972 for (idx = 0; idx < phba->sli4_hba.num_present_cpu; idx++) {
8973 if (cpup->channel_id == LPFC_VECTOR_MAP_EMPTY) {
8974 cpup->channel_id = 0;
8975 num_io_channel++;
8976
8977 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8978 "3332 Assign IO_CHANN "
8979 "CPU %d channel %d\n",
8980 idx, cpup->channel_id);
8981 }
8982 cpup++;
8983 }
8984 }
8985
8986 /* Sanity check */
8987 if (num_io_channel != phba->sli4_hba.num_present_cpu)
8988 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8989 "3333 Set affinity mismatch:"
James Smartec2087a2013-09-06 12:20:36 -04008990 "%d chann != %d cpus: %d vectors\n",
James Smart7bb03bb2013-04-17 20:19:16 -04008991 num_io_channel, phba->sli4_hba.num_present_cpu,
8992 vectors);
8993
James Smartec2087a2013-09-06 12:20:36 -04008994 /* Enable using cpu affinity for scheduling */
James Smart7bb03bb2013-04-17 20:19:16 -04008995 phba->cfg_fcp_io_sched = LPFC_FCP_SCHED_BY_CPU;
8996 return 1;
8997}
8998
8999
9000/**
James Smartda0436e2009-05-22 14:51:39 -04009001 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
9002 * @phba: pointer to lpfc hba data structure.
9003 *
9004 * This routine is invoked to enable the MSI-X interrupt vectors to device
Alexander Gordeev4f871e12014-09-03 12:56:29 -04009005 * with SLI-4 interface spec. The kernel function pci_enable_msix_range()
9006 * is called to enable the MSI-X vectors. The device driver is responsible
9007 * for calling the individual request_irq() to register each MSI-X vector
9008 * with a interrupt handler, which is done in this function. Note that
9009 * later when device is unloading, the driver should always call free_irq()
9010 * on all MSI-X vectors it has done request_irq() on before calling
9011 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
9012 * will be left with MSI-X enabled and leaks its vectors.
James Smartda0436e2009-05-22 14:51:39 -04009013 *
9014 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009015 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04009016 * other values - error
9017 **/
9018static int
9019lpfc_sli4_enable_msix(struct lpfc_hba *phba)
9020{
James Smart75baf692010-06-08 18:31:21 -04009021 int vectors, rc, index;
James Smartda0436e2009-05-22 14:51:39 -04009022
9023 /* Set up MSI-X multi-message vectors */
James Smart82c3e9b2012-09-29 11:29:50 -04009024 for (index = 0; index < phba->cfg_fcp_io_channel; index++)
James Smartda0436e2009-05-22 14:51:39 -04009025 phba->sli4_hba.msix_entries[index].entry = index;
9026
9027 /* Configure MSI-X capability structure */
James Smart82c3e9b2012-09-29 11:29:50 -04009028 vectors = phba->cfg_fcp_io_channel;
James Smart1ba981f2014-02-20 09:56:45 -05009029 if (phba->cfg_fof) {
9030 phba->sli4_hba.msix_entries[index].entry = index;
9031 vectors++;
9032 }
Alexander Gordeev4f871e12014-09-03 12:56:29 -04009033 rc = pci_enable_msix_range(phba->pcidev, phba->sli4_hba.msix_entries,
9034 2, vectors);
9035 if (rc < 0) {
James Smartda0436e2009-05-22 14:51:39 -04009036 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9037 "0484 PCI enable MSI-X failed (%d)\n", rc);
Alexander Gordeev029165a2014-07-16 20:05:15 +02009038 goto vec_fail_out;
James Smartda0436e2009-05-22 14:51:39 -04009039 }
Alexander Gordeev4f871e12014-09-03 12:56:29 -04009040 vectors = rc;
James Smart75baf692010-06-08 18:31:21 -04009041
James Smartda0436e2009-05-22 14:51:39 -04009042 /* Log MSI-X vector assignment */
James Smart75baf692010-06-08 18:31:21 -04009043 for (index = 0; index < vectors; index++)
James Smartda0436e2009-05-22 14:51:39 -04009044 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9045 "0489 MSI-X entry[%d]: vector=x%x "
9046 "message=%d\n", index,
9047 phba->sli4_hba.msix_entries[index].vector,
9048 phba->sli4_hba.msix_entries[index].entry);
James Smart67d12732012-08-03 12:36:13 -04009049
James Smart7bb03bb2013-04-17 20:19:16 -04009050 /* Assign MSI-X vectors to interrupt handlers */
James Smart67d12732012-08-03 12:36:13 -04009051 for (index = 0; index < vectors; index++) {
James Smart4305f182012-08-03 12:36:33 -04009052 memset(&phba->sli4_hba.handler_name[index], 0, 16);
James Smarta2fc4aef2014-09-03 12:57:55 -04009053 snprintf((char *)&phba->sli4_hba.handler_name[index],
9054 LPFC_SLI4_HANDLER_NAME_SZ,
James Smart4305f182012-08-03 12:36:33 -04009055 LPFC_DRIVER_HANDLER_NAME"%d", index);
James Smartda0436e2009-05-22 14:51:39 -04009056
James Smart67d12732012-08-03 12:36:13 -04009057 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
9058 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
James Smartba20c852012-08-03 12:36:52 -04009059 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].fcp_eq_in_use, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009060 if (phba->cfg_fof && (index == (vectors - 1)))
9061 rc = request_irq(
9062 phba->sli4_hba.msix_entries[index].vector,
James Smarted243d32015-05-21 13:55:25 -04009063 &lpfc_sli4_fof_intr_handler, 0,
James Smart1ba981f2014-02-20 09:56:45 -05009064 (char *)&phba->sli4_hba.handler_name[index],
9065 &phba->sli4_hba.fcp_eq_hdl[index]);
9066 else
9067 rc = request_irq(
9068 phba->sli4_hba.msix_entries[index].vector,
James Smarted243d32015-05-21 13:55:25 -04009069 &lpfc_sli4_hba_intr_handler, 0,
James Smart4305f182012-08-03 12:36:33 -04009070 (char *)&phba->sli4_hba.handler_name[index],
James Smart67d12732012-08-03 12:36:13 -04009071 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -04009072 if (rc) {
9073 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9074 "0486 MSI-X fast-path (%d) "
9075 "request_irq failed (%d)\n", index, rc);
9076 goto cfg_fail_out;
9077 }
9078 }
9079
James Smart1ba981f2014-02-20 09:56:45 -05009080 if (phba->cfg_fof)
9081 vectors--;
9082
James Smart82c3e9b2012-09-29 11:29:50 -04009083 if (vectors != phba->cfg_fcp_io_channel) {
9084 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9085 "3238 Reducing IO channels to match number of "
9086 "MSI-X vectors, requested %d got %d\n",
9087 phba->cfg_fcp_io_channel, vectors);
9088 phba->cfg_fcp_io_channel = vectors;
9089 }
James Smart7bb03bb2013-04-17 20:19:16 -04009090
James Smart8b0dff12015-05-22 10:42:38 -04009091 if (!shost_use_blk_mq(lpfc_shost_from_vport(phba->pport)))
9092 lpfc_sli4_set_affinity(phba, vectors);
James Smartda0436e2009-05-22 14:51:39 -04009093 return rc;
9094
9095cfg_fail_out:
9096 /* free the irq already requested */
James Smartacbd8612013-09-06 12:21:19 -04009097 for (--index; index >= 0; index--) {
9098 irq_set_affinity_hint(phba->sli4_hba.msix_entries[index].
9099 vector, NULL);
James Smart67d12732012-08-03 12:36:13 -04009100 free_irq(phba->sli4_hba.msix_entries[index].vector,
9101 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smartacbd8612013-09-06 12:21:19 -04009102 }
James Smartda0436e2009-05-22 14:51:39 -04009103
James Smartda0436e2009-05-22 14:51:39 -04009104 /* Unconfigure MSI-X capability structure */
9105 pci_disable_msix(phba->pcidev);
Alexander Gordeev029165a2014-07-16 20:05:15 +02009106
9107vec_fail_out:
James Smartda0436e2009-05-22 14:51:39 -04009108 return rc;
9109}
9110
9111/**
9112 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
9113 * @phba: pointer to lpfc hba data structure.
9114 *
9115 * This routine is invoked to release the MSI-X vectors and then disable the
9116 * MSI-X interrupt mode to device with SLI-4 interface spec.
9117 **/
9118static void
9119lpfc_sli4_disable_msix(struct lpfc_hba *phba)
9120{
9121 int index;
9122
9123 /* Free up MSI-X multi-message vectors */
James Smartacbd8612013-09-06 12:21:19 -04009124 for (index = 0; index < phba->cfg_fcp_io_channel; index++) {
9125 irq_set_affinity_hint(phba->sli4_hba.msix_entries[index].
9126 vector, NULL);
James Smartda0436e2009-05-22 14:51:39 -04009127 free_irq(phba->sli4_hba.msix_entries[index].vector,
James Smart67d12732012-08-03 12:36:13 -04009128 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smartacbd8612013-09-06 12:21:19 -04009129 }
James Smart1ba981f2014-02-20 09:56:45 -05009130 if (phba->cfg_fof) {
9131 free_irq(phba->sli4_hba.msix_entries[index].vector,
9132 &phba->sli4_hba.fcp_eq_hdl[index]);
9133 }
James Smartda0436e2009-05-22 14:51:39 -04009134 /* Disable MSI-X */
9135 pci_disable_msix(phba->pcidev);
9136
9137 return;
9138}
9139
9140/**
9141 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
9142 * @phba: pointer to lpfc hba data structure.
9143 *
9144 * This routine is invoked to enable the MSI interrupt mode to device with
9145 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
9146 * to enable the MSI vector. The device driver is responsible for calling
9147 * the request_irq() to register MSI vector with a interrupt the handler,
9148 * which is done in this function.
9149 *
9150 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009151 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04009152 * other values - error
9153 **/
9154static int
9155lpfc_sli4_enable_msi(struct lpfc_hba *phba)
9156{
9157 int rc, index;
9158
9159 rc = pci_enable_msi(phba->pcidev);
9160 if (!rc)
9161 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9162 "0487 PCI enable MSI mode success.\n");
9163 else {
9164 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9165 "0488 PCI enable MSI mode failed (%d)\n", rc);
9166 return rc;
9167 }
9168
9169 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
James Smarted243d32015-05-21 13:55:25 -04009170 0, LPFC_DRIVER_NAME, phba);
James Smartda0436e2009-05-22 14:51:39 -04009171 if (rc) {
9172 pci_disable_msi(phba->pcidev);
9173 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9174 "0490 MSI request_irq failed (%d)\n", rc);
James Smart75baf692010-06-08 18:31:21 -04009175 return rc;
James Smartda0436e2009-05-22 14:51:39 -04009176 }
9177
James Smart67d12732012-08-03 12:36:13 -04009178 for (index = 0; index < phba->cfg_fcp_io_channel; index++) {
James Smartda0436e2009-05-22 14:51:39 -04009179 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
9180 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
9181 }
9182
James Smart1ba981f2014-02-20 09:56:45 -05009183 if (phba->cfg_fof) {
9184 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
9185 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
9186 }
James Smart75baf692010-06-08 18:31:21 -04009187 return 0;
James Smartda0436e2009-05-22 14:51:39 -04009188}
9189
9190/**
9191 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
9192 * @phba: pointer to lpfc hba data structure.
9193 *
9194 * This routine is invoked to disable the MSI interrupt mode to device with
9195 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
9196 * done request_irq() on before calling pci_disable_msi(). Failure to do so
9197 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
9198 * its vector.
9199 **/
9200static void
9201lpfc_sli4_disable_msi(struct lpfc_hba *phba)
9202{
9203 free_irq(phba->pcidev->irq, phba);
9204 pci_disable_msi(phba->pcidev);
9205 return;
9206}
9207
9208/**
9209 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
9210 * @phba: pointer to lpfc hba data structure.
9211 *
9212 * This routine is invoked to enable device interrupt and associate driver's
9213 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
9214 * interface spec. Depends on the interrupt mode configured to the driver,
9215 * the driver will try to fallback from the configured interrupt mode to an
9216 * interrupt mode which is supported by the platform, kernel, and device in
9217 * the order of:
9218 * MSI-X -> MSI -> IRQ.
9219 *
9220 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009221 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04009222 * other values - error
9223 **/
9224static uint32_t
9225lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
9226{
9227 uint32_t intr_mode = LPFC_INTR_ERROR;
9228 int retval, index;
9229
9230 if (cfg_mode == 2) {
9231 /* Preparation before conf_msi mbox cmd */
9232 retval = 0;
9233 if (!retval) {
9234 /* Now, try to enable MSI-X interrupt mode */
9235 retval = lpfc_sli4_enable_msix(phba);
9236 if (!retval) {
9237 /* Indicate initialization to MSI-X mode */
9238 phba->intr_type = MSIX;
9239 intr_mode = 2;
9240 }
9241 }
9242 }
9243
9244 /* Fallback to MSI if MSI-X initialization failed */
9245 if (cfg_mode >= 1 && phba->intr_type == NONE) {
9246 retval = lpfc_sli4_enable_msi(phba);
9247 if (!retval) {
9248 /* Indicate initialization to MSI mode */
9249 phba->intr_type = MSI;
9250 intr_mode = 1;
9251 }
9252 }
9253
9254 /* Fallback to INTx if both MSI-X/MSI initalization failed */
9255 if (phba->intr_type == NONE) {
9256 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
9257 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
9258 if (!retval) {
9259 /* Indicate initialization to INTx mode */
9260 phba->intr_type = INTx;
9261 intr_mode = 0;
James Smart67d12732012-08-03 12:36:13 -04009262 for (index = 0; index < phba->cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04009263 index++) {
9264 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
9265 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
James Smartba20c852012-08-03 12:36:52 -04009266 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].
9267 fcp_eq_in_use, 1);
James Smartda0436e2009-05-22 14:51:39 -04009268 }
James Smart1ba981f2014-02-20 09:56:45 -05009269 if (phba->cfg_fof) {
9270 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
9271 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
9272 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].
9273 fcp_eq_in_use, 1);
9274 }
James Smartda0436e2009-05-22 14:51:39 -04009275 }
9276 }
9277 return intr_mode;
9278}
9279
9280/**
9281 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
9282 * @phba: pointer to lpfc hba data structure.
9283 *
9284 * This routine is invoked to disable device interrupt and disassociate
9285 * the driver's interrupt handler(s) from interrupt vector(s) to device
9286 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
9287 * will release the interrupt vector(s) for the message signaled interrupt.
9288 **/
9289static void
9290lpfc_sli4_disable_intr(struct lpfc_hba *phba)
9291{
9292 /* Disable the currently initialized interrupt mode */
9293 if (phba->intr_type == MSIX)
9294 lpfc_sli4_disable_msix(phba);
9295 else if (phba->intr_type == MSI)
9296 lpfc_sli4_disable_msi(phba);
9297 else if (phba->intr_type == INTx)
9298 free_irq(phba->pcidev->irq, phba);
9299
9300 /* Reset interrupt management states */
9301 phba->intr_type = NONE;
9302 phba->sli.slistat.sli_intr = 0;
9303
9304 return;
9305}
9306
9307/**
James Smart3772a992009-05-22 14:50:54 -04009308 * lpfc_unset_hba - Unset SLI3 hba device initialization
9309 * @phba: pointer to lpfc hba data structure.
9310 *
9311 * This routine is invoked to unset the HBA device initialization steps to
9312 * a device with SLI-3 interface spec.
9313 **/
9314static void
9315lpfc_unset_hba(struct lpfc_hba *phba)
9316{
9317 struct lpfc_vport *vport = phba->pport;
9318 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
9319
9320 spin_lock_irq(shost->host_lock);
9321 vport->load_flag |= FC_UNLOADING;
9322 spin_unlock_irq(shost->host_lock);
9323
James Smart72859902012-01-18 16:25:38 -05009324 kfree(phba->vpi_bmask);
9325 kfree(phba->vpi_ids);
9326
James Smart3772a992009-05-22 14:50:54 -04009327 lpfc_stop_hba_timers(phba);
9328
9329 phba->pport->work_port_events = 0;
9330
9331 lpfc_sli_hba_down(phba);
9332
9333 lpfc_sli_brdrestart(phba);
9334
9335 lpfc_sli_disable_intr(phba);
9336
9337 return;
9338}
9339
9340/**
James Smart5af5eee2010-10-22 11:06:38 -04009341 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
9342 * @phba: Pointer to HBA context object.
9343 *
9344 * This function is called in the SLI4 code path to wait for completion
9345 * of device's XRIs exchange busy. It will check the XRI exchange busy
9346 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
9347 * that, it will check the XRI exchange busy on outstanding FCP and ELS
9348 * I/Os every 30 seconds, log error message, and wait forever. Only when
9349 * all XRI exchange busy complete, the driver unload shall proceed with
9350 * invoking the function reset ioctl mailbox command to the CNA and the
9351 * the rest of the driver unload resource release.
9352 **/
9353static void
9354lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
9355{
9356 int wait_time = 0;
9357 int fcp_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
9358 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
9359
9360 while (!fcp_xri_cmpl || !els_xri_cmpl) {
9361 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
9362 if (!fcp_xri_cmpl)
9363 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9364 "2877 FCP XRI exchange busy "
9365 "wait time: %d seconds.\n",
9366 wait_time/1000);
9367 if (!els_xri_cmpl)
9368 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9369 "2878 ELS XRI exchange busy "
9370 "wait time: %d seconds.\n",
9371 wait_time/1000);
9372 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
9373 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
9374 } else {
9375 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
9376 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
9377 }
9378 fcp_xri_cmpl =
9379 list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
9380 els_xri_cmpl =
9381 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
9382 }
9383}
9384
9385/**
James Smartda0436e2009-05-22 14:51:39 -04009386 * lpfc_sli4_hba_unset - Unset the fcoe hba
9387 * @phba: Pointer to HBA context object.
9388 *
9389 * This function is called in the SLI4 code path to reset the HBA's FCoE
9390 * function. The caller is not required to hold any lock. This routine
9391 * issues PCI function reset mailbox command to reset the FCoE function.
9392 * At the end of the function, it calls lpfc_hba_down_post function to
9393 * free any pending commands.
9394 **/
9395static void
9396lpfc_sli4_hba_unset(struct lpfc_hba *phba)
9397{
9398 int wait_cnt = 0;
9399 LPFC_MBOXQ_t *mboxq;
James Smart912e3ac2011-05-24 11:42:11 -04009400 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -04009401
9402 lpfc_stop_hba_timers(phba);
9403 phba->sli4_hba.intr_enable = 0;
9404
9405 /*
9406 * Gracefully wait out the potential current outstanding asynchronous
9407 * mailbox command.
9408 */
9409
9410 /* First, block any pending async mailbox command from posted */
9411 spin_lock_irq(&phba->hbalock);
9412 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
9413 spin_unlock_irq(&phba->hbalock);
9414 /* Now, trying to wait it out if we can */
9415 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9416 msleep(10);
9417 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
9418 break;
9419 }
9420 /* Forcefully release the outstanding mailbox command if timed out */
9421 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9422 spin_lock_irq(&phba->hbalock);
9423 mboxq = phba->sli.mbox_active;
9424 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
9425 __lpfc_mbox_cmpl_put(phba, mboxq);
9426 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9427 phba->sli.mbox_active = NULL;
9428 spin_unlock_irq(&phba->hbalock);
9429 }
9430
James Smart5af5eee2010-10-22 11:06:38 -04009431 /* Abort all iocbs associated with the hba */
9432 lpfc_sli_hba_iocb_abort(phba);
9433
9434 /* Wait for completion of device XRI exchange busy */
9435 lpfc_sli4_xri_exchange_busy_wait(phba);
9436
James Smartda0436e2009-05-22 14:51:39 -04009437 /* Disable PCI subsystem interrupt */
9438 lpfc_sli4_disable_intr(phba);
9439
James Smart912e3ac2011-05-24 11:42:11 -04009440 /* Disable SR-IOV if enabled */
9441 if (phba->cfg_sriov_nr_virtfn)
9442 pci_disable_sriov(pdev);
9443
James Smartda0436e2009-05-22 14:51:39 -04009444 /* Stop kthread signal shall trigger work_done one more time */
9445 kthread_stop(phba->worker_thread);
9446
James Smart3677a3a2010-09-29 11:19:14 -04009447 /* Reset SLI4 HBA FCoE function */
9448 lpfc_pci_function_reset(phba);
James Smart5350d872011-10-10 21:33:49 -04009449 lpfc_sli4_queue_destroy(phba);
James Smart3677a3a2010-09-29 11:19:14 -04009450
James Smartda0436e2009-05-22 14:51:39 -04009451 /* Stop the SLI4 device port */
9452 phba->pport->work_port_events = 0;
9453}
9454
James Smart28baac72010-02-12 14:42:03 -05009455 /**
9456 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
9457 * @phba: Pointer to HBA context object.
9458 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
9459 *
9460 * This function is called in the SLI4 code path to read the port's
9461 * sli4 capabilities.
9462 *
9463 * This function may be be called from any context that can block-wait
9464 * for the completion. The expectation is that this routine is called
9465 * typically from probe_one or from the online routine.
9466 **/
9467int
9468lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9469{
9470 int rc;
9471 struct lpfc_mqe *mqe;
9472 struct lpfc_pc_sli4_params *sli4_params;
9473 uint32_t mbox_tmo;
9474
9475 rc = 0;
9476 mqe = &mboxq->u.mqe;
9477
9478 /* Read the port's SLI4 Parameters port capabilities */
James Smartfedd3b72011-02-16 12:39:24 -05009479 lpfc_pc_sli4_params(mboxq);
James Smart28baac72010-02-12 14:42:03 -05009480 if (!phba->sli4_hba.intr_enable)
9481 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9482 else {
James Smarta183a152011-10-10 21:32:43 -04009483 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart28baac72010-02-12 14:42:03 -05009484 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
9485 }
9486
9487 if (unlikely(rc))
9488 return 1;
9489
9490 sli4_params = &phba->sli4_hba.pc_sli4_params;
9491 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
9492 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
9493 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
9494 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
9495 &mqe->un.sli4_params);
9496 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
9497 &mqe->un.sli4_params);
9498 sli4_params->proto_types = mqe->un.sli4_params.word3;
9499 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
9500 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
9501 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
9502 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
9503 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
9504 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
9505 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
9506 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
9507 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
9508 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
9509 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
9510 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
9511 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
9512 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
9513 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
9514 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
9515 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
9516 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
9517 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
9518 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
James Smart05580562011-05-24 11:40:48 -04009519
9520 /* Make sure that sge_supp_len can be handled by the driver */
9521 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
9522 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
9523
James Smart28baac72010-02-12 14:42:03 -05009524 return rc;
9525}
9526
James Smartda0436e2009-05-22 14:51:39 -04009527/**
James Smartfedd3b72011-02-16 12:39:24 -05009528 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
9529 * @phba: Pointer to HBA context object.
9530 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
9531 *
9532 * This function is called in the SLI4 code path to read the port's
9533 * sli4 capabilities.
9534 *
9535 * This function may be be called from any context that can block-wait
9536 * for the completion. The expectation is that this routine is called
9537 * typically from probe_one or from the online routine.
9538 **/
9539int
9540lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9541{
9542 int rc;
9543 struct lpfc_mqe *mqe = &mboxq->u.mqe;
9544 struct lpfc_pc_sli4_params *sli4_params;
James Smarta183a152011-10-10 21:32:43 -04009545 uint32_t mbox_tmo;
James Smartfedd3b72011-02-16 12:39:24 -05009546 int length;
9547 struct lpfc_sli4_parameters *mbx_sli4_parameters;
9548
James Smart6d368e52011-05-24 11:44:12 -04009549 /*
9550 * By default, the driver assumes the SLI4 port requires RPI
9551 * header postings. The SLI4_PARAM response will correct this
9552 * assumption.
9553 */
9554 phba->sli4_hba.rpi_hdrs_in_use = 1;
9555
James Smartfedd3b72011-02-16 12:39:24 -05009556 /* Read the port's SLI4 Config Parameters */
9557 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
9558 sizeof(struct lpfc_sli4_cfg_mhdr));
9559 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
9560 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
9561 length, LPFC_SLI4_MBX_EMBED);
9562 if (!phba->sli4_hba.intr_enable)
9563 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -04009564 else {
9565 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
9566 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
9567 }
James Smartfedd3b72011-02-16 12:39:24 -05009568 if (unlikely(rc))
9569 return rc;
9570 sli4_params = &phba->sli4_hba.pc_sli4_params;
9571 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
9572 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
9573 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
9574 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
9575 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
9576 mbx_sli4_parameters);
9577 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
9578 mbx_sli4_parameters);
9579 if (bf_get(cfg_phwq, mbx_sli4_parameters))
9580 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
9581 else
9582 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
9583 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
9584 sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
James Smart1ba981f2014-02-20 09:56:45 -05009585 sli4_params->oas_supported = bf_get(cfg_oas, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -05009586 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
9587 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
9588 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
9589 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
James Smart0c651872013-07-15 18:33:23 -04009590 sli4_params->wqsize = bf_get(cfg_wqsize, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -05009591 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
9592 mbx_sli4_parameters);
9593 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
9594 mbx_sli4_parameters);
James Smart6d368e52011-05-24 11:44:12 -04009595 phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
9596 phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
James Smart05580562011-05-24 11:40:48 -04009597
9598 /* Make sure that sge_supp_len can be handled by the driver */
9599 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
9600 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
9601
James Smartb5c53952016-03-31 14:12:30 -07009602 /*
9603 * Issue IOs with CDB embedded in WQE to minimized the number
9604 * of DMAs the firmware has to do. Setting this to 1 also forces
9605 * the driver to use 128 bytes WQEs for FCP IOs.
9606 */
9607 if (bf_get(cfg_ext_embed_cb, mbx_sli4_parameters))
9608 phba->fcp_embed_io = 1;
9609 else
9610 phba->fcp_embed_io = 0;
James Smart7bdedb32016-07-06 12:36:00 -07009611
9612 /*
9613 * Check if the SLI port supports MDS Diagnostics
9614 */
9615 if (bf_get(cfg_mds_diags, mbx_sli4_parameters))
9616 phba->mds_diags_support = 1;
9617 else
9618 phba->mds_diags_support = 0;
James Smartfedd3b72011-02-16 12:39:24 -05009619 return 0;
9620}
9621
9622/**
James Smart3772a992009-05-22 14:50:54 -04009623 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
9624 * @pdev: pointer to PCI device
9625 * @pid: pointer to PCI device identifier
9626 *
9627 * This routine is to be called to attach a device with SLI-3 interface spec
9628 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
9629 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
9630 * information of the device and driver to see if the driver state that it can
9631 * support this kind of device. If the match is successful, the driver core
9632 * invokes this routine. If this routine determines it can claim the HBA, it
9633 * does all the initialization that it needs to do to handle the HBA properly.
9634 *
9635 * Return code
9636 * 0 - driver can claim the device
9637 * negative value - driver can not claim the device
9638 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009639static int
James Smart3772a992009-05-22 14:50:54 -04009640lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
9641{
9642 struct lpfc_hba *phba;
9643 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04009644 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -04009645 int error;
9646 uint32_t cfg_mode, intr_mode;
9647
9648 /* Allocate memory for HBA structure */
9649 phba = lpfc_hba_alloc(pdev);
9650 if (!phba)
9651 return -ENOMEM;
9652
9653 /* Perform generic PCI device enabling operation */
9654 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -04009655 if (error)
James Smart3772a992009-05-22 14:50:54 -04009656 goto out_free_phba;
James Smart3772a992009-05-22 14:50:54 -04009657
9658 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
9659 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
9660 if (error)
9661 goto out_disable_pci_dev;
9662
9663 /* Set up SLI-3 specific device PCI memory space */
9664 error = lpfc_sli_pci_mem_setup(phba);
9665 if (error) {
9666 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9667 "1402 Failed to set up pci memory space.\n");
9668 goto out_disable_pci_dev;
9669 }
9670
9671 /* Set up phase-1 common device driver resources */
9672 error = lpfc_setup_driver_resource_phase1(phba);
9673 if (error) {
9674 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9675 "1403 Failed to set up driver resource.\n");
9676 goto out_unset_pci_mem_s3;
9677 }
9678
9679 /* Set up SLI-3 specific device driver resources */
9680 error = lpfc_sli_driver_resource_setup(phba);
9681 if (error) {
9682 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9683 "1404 Failed to set up driver resource.\n");
9684 goto out_unset_pci_mem_s3;
9685 }
9686
9687 /* Initialize and populate the iocb list per host */
9688 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
9689 if (error) {
9690 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9691 "1405 Failed to initialize iocb list.\n");
9692 goto out_unset_driver_resource_s3;
9693 }
9694
9695 /* Set up common device driver resources */
9696 error = lpfc_setup_driver_resource_phase2(phba);
9697 if (error) {
9698 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9699 "1406 Failed to set up driver resource.\n");
9700 goto out_free_iocb_list;
9701 }
9702
James Smart079b5c92011-08-21 21:48:49 -04009703 /* Get the default values for Model Name and Description */
9704 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
9705
James Smart3772a992009-05-22 14:50:54 -04009706 /* Create SCSI host to the physical port */
9707 error = lpfc_create_shost(phba);
9708 if (error) {
9709 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9710 "1407 Failed to create scsi host.\n");
9711 goto out_unset_driver_resource;
9712 }
9713
9714 /* Configure sysfs attributes */
9715 vport = phba->pport;
9716 error = lpfc_alloc_sysfs_attr(vport);
9717 if (error) {
9718 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9719 "1476 Failed to allocate sysfs attr\n");
9720 goto out_destroy_shost;
9721 }
9722
James Smart6669f9b2009-10-02 15:16:45 -04009723 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -04009724 /* Now, trying to enable interrupt and bring up the device */
9725 cfg_mode = phba->cfg_use_msi;
9726 while (true) {
9727 /* Put device to a known state before enabling interrupt */
9728 lpfc_stop_port(phba);
9729 /* Configure and enable interrupt */
9730 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
9731 if (intr_mode == LPFC_INTR_ERROR) {
9732 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9733 "0431 Failed to enable interrupt.\n");
9734 error = -ENODEV;
9735 goto out_free_sysfs_attr;
9736 }
9737 /* SLI-3 HBA setup */
9738 if (lpfc_sli_hba_setup(phba)) {
9739 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9740 "1477 Failed to set up hba\n");
9741 error = -ENODEV;
9742 goto out_remove_device;
9743 }
9744
9745 /* Wait 50ms for the interrupts of previous mailbox commands */
9746 msleep(50);
9747 /* Check active interrupts on message signaled interrupts */
9748 if (intr_mode == 0 ||
9749 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
9750 /* Log the current active interrupt mode */
9751 phba->intr_mode = intr_mode;
9752 lpfc_log_intr_mode(phba, intr_mode);
9753 break;
9754 } else {
9755 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9756 "0447 Configure interrupt mode (%d) "
9757 "failed active interrupt test.\n",
9758 intr_mode);
9759 /* Disable the current interrupt mode */
9760 lpfc_sli_disable_intr(phba);
9761 /* Try next level of interrupt mode */
9762 cfg_mode = --intr_mode;
9763 }
9764 }
9765
9766 /* Perform post initialization setup */
9767 lpfc_post_init_setup(phba);
9768
9769 /* Check if there are static vports to be created. */
9770 lpfc_create_static_vport(phba);
9771
9772 return 0;
9773
9774out_remove_device:
9775 lpfc_unset_hba(phba);
9776out_free_sysfs_attr:
9777 lpfc_free_sysfs_attr(vport);
9778out_destroy_shost:
9779 lpfc_destroy_shost(phba);
9780out_unset_driver_resource:
9781 lpfc_unset_driver_resource_phase2(phba);
9782out_free_iocb_list:
9783 lpfc_free_iocb_list(phba);
9784out_unset_driver_resource_s3:
9785 lpfc_sli_driver_resource_unset(phba);
9786out_unset_pci_mem_s3:
9787 lpfc_sli_pci_mem_unset(phba);
9788out_disable_pci_dev:
9789 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04009790 if (shost)
9791 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -04009792out_free_phba:
9793 lpfc_hba_free(phba);
9794 return error;
9795}
9796
9797/**
9798 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -04009799 * @pdev: pointer to PCI device
9800 *
James Smart3772a992009-05-22 14:50:54 -04009801 * This routine is to be called to disattach a device with SLI-3 interface
9802 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
9803 * removed from PCI bus, it performs all the necessary cleanup for the HBA
9804 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -04009805 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009806static void
James Smart3772a992009-05-22 14:50:54 -04009807lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -05009808{
James Smart2e0fef82007-06-17 19:56:36 -05009809 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9810 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -05009811 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -05009812 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -05009813 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05009814 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
9815
James Smart549e55c2007-08-02 11:09:51 -04009816 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04009817 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04009818 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009819
James Smart858c9f62007-06-17 19:56:39 -05009820 lpfc_free_sysfs_attr(vport);
9821
James Smarteada2722008-12-04 22:39:13 -05009822 /* Release all the vports against this physical port */
9823 vports = lpfc_create_vport_work_array(phba);
9824 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -04009825 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
9826 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
9827 continue;
James Smarteada2722008-12-04 22:39:13 -05009828 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -04009829 }
James Smarteada2722008-12-04 22:39:13 -05009830 lpfc_destroy_vport_work_array(phba, vports);
9831
9832 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -05009833 fc_remove_host(shost);
9834 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04009835 lpfc_cleanup(vport);
9836
James Smart2e0fef82007-06-17 19:56:36 -05009837 /*
9838 * Bring down the SLI Layer. This step disable all interrupts,
9839 * clears the rings, discards all mailbox commands, and resets
9840 * the HBA.
9841 */
James Smarta257bf92009-04-06 18:48:10 -04009842
Justin P. Mattock48e34d02010-12-30 15:07:58 -08009843 /* HBA interrupt will be disabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -05009844 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -04009845 /* Stop kthread signal shall trigger work_done one more time */
9846 kthread_stop(phba->worker_thread);
9847 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -05009848 lpfc_sli_brdrestart(phba);
9849
James Smart72859902012-01-18 16:25:38 -05009850 kfree(phba->vpi_bmask);
9851 kfree(phba->vpi_ids);
9852
James Smart3772a992009-05-22 14:50:54 -04009853 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05009854 spin_lock_irq(&phba->hbalock);
9855 list_del_init(&vport->listentry);
9856 spin_unlock_irq(&phba->hbalock);
9857
James Smart858c9f62007-06-17 19:56:39 -05009858 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05009859
James Smart912e3ac2011-05-24 11:42:11 -04009860 /* Disable SR-IOV if enabled */
9861 if (phba->cfg_sriov_nr_virtfn)
9862 pci_disable_sriov(pdev);
9863
James Smart5b75da22008-12-04 22:39:35 -05009864 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009865 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -05009866
James Smart858c9f62007-06-17 19:56:39 -05009867 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05009868
9869 /*
9870 * Call scsi_free before mem_free since scsi bufs are released to their
9871 * corresponding pools here.
9872 */
9873 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -04009874 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -05009875
James Smart34b02dc2008-08-24 21:49:55 -04009876 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
9877 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -05009878
James Smart2e0fef82007-06-17 19:56:36 -05009879 /* Free resources associated with SLI2 interface */
9880 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -04009881 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -05009882
9883 /* unmap adapter SLIM and Control Registers */
9884 iounmap(phba->ctrl_regs_memmap_p);
9885 iounmap(phba->slim_memmap_p);
9886
James Smart3772a992009-05-22 14:50:54 -04009887 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -05009888
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05009889 pci_release_selected_regions(pdev, bars);
James Smart2e0fef82007-06-17 19:56:36 -05009890 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05009891}
9892
Linas Vepstas8d63f372007-02-14 14:28:36 -06009893/**
James Smart3772a992009-05-22 14:50:54 -04009894 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05009895 * @pdev: pointer to PCI device
9896 * @msg: power management message
9897 *
James Smart3772a992009-05-22 14:50:54 -04009898 * This routine is to be called from the kernel's PCI subsystem to support
9899 * system Power Management (PM) to device with SLI-3 interface spec. When
9900 * PM invokes this method, it quiesces the device by stopping the driver's
9901 * worker thread for the device, turning off device's interrupt and DMA,
9902 * and bring the device offline. Note that as the driver implements the
9903 * minimum PM requirements to a power-aware driver's PM support for the
9904 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
9905 * to the suspend() method call will be treated as SUSPEND and the driver will
9906 * fully reinitialize its device during resume() method call, the driver will
9907 * set device to PCI_D3hot state in PCI config space instead of setting it
9908 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -05009909 *
9910 * Return code
James Smart3772a992009-05-22 14:50:54 -04009911 * 0 - driver suspended the device
9912 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05009913 **/
9914static int
James Smart3772a992009-05-22 14:50:54 -04009915lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -05009916{
9917 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9918 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9919
9920 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9921 "0473 PCI device Power Management suspend.\n");
9922
9923 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -04009924 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart3a55b532008-12-04 22:38:54 -05009925 lpfc_offline(phba);
9926 kthread_stop(phba->worker_thread);
9927
9928 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -04009929 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -05009930
9931 /* Save device state to PCI config space */
9932 pci_save_state(pdev);
9933 pci_set_power_state(pdev, PCI_D3hot);
9934
9935 return 0;
9936}
9937
9938/**
James Smart3772a992009-05-22 14:50:54 -04009939 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05009940 * @pdev: pointer to PCI device
9941 *
James Smart3772a992009-05-22 14:50:54 -04009942 * This routine is to be called from the kernel's PCI subsystem to support
9943 * system Power Management (PM) to device with SLI-3 interface spec. When PM
9944 * invokes this method, it restores the device's PCI config space state and
9945 * fully reinitializes the device and brings it online. Note that as the
9946 * driver implements the minimum PM requirements to a power-aware driver's
9947 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
9948 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
9949 * driver will fully reinitialize its device during resume() method call,
9950 * the device will be set to PCI_D0 directly in PCI config space before
9951 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -05009952 *
9953 * Return code
James Smart3772a992009-05-22 14:50:54 -04009954 * 0 - driver suspended the device
9955 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05009956 **/
9957static int
James Smart3772a992009-05-22 14:50:54 -04009958lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -05009959{
9960 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9961 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -05009962 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05009963 int error;
9964
9965 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9966 "0452 PCI device Power Management resume.\n");
9967
9968 /* Restore device state from PCI config space */
9969 pci_set_power_state(pdev, PCI_D0);
9970 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -04009971
James Smart1dfb5a42010-02-12 14:40:50 -05009972 /*
9973 * As the new kernel behavior of pci_restore_state() API call clears
9974 * device saved_state flag, need to save the restored state again.
9975 */
9976 pci_save_state(pdev);
9977
James Smart3a55b532008-12-04 22:38:54 -05009978 if (pdev->is_busmaster)
9979 pci_set_master(pdev);
9980
9981 /* Startup the kernel thread for this host adapter. */
9982 phba->worker_thread = kthread_run(lpfc_do_work, phba,
9983 "lpfc_worker_%d", phba->brd_no);
9984 if (IS_ERR(phba->worker_thread)) {
9985 error = PTR_ERR(phba->worker_thread);
9986 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9987 "0434 PM resume failed to start worker "
9988 "thread: error=x%x.\n", error);
9989 return error;
9990 }
9991
James Smart5b75da22008-12-04 22:39:35 -05009992 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009993 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05009994 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -05009995 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -05009996 "0430 PM resume Failed to enable interrupt\n");
9997 return -EIO;
9998 } else
9999 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -050010000
10001 /* Restart HBA and bring it online */
10002 lpfc_sli_brdrestart(phba);
10003 lpfc_online(phba);
10004
James Smart5b75da22008-12-04 22:39:35 -050010005 /* Log the current active interrupt mode */
10006 lpfc_log_intr_mode(phba, phba->intr_mode);
10007
James Smart3a55b532008-12-04 22:38:54 -050010008 return 0;
10009}
10010
10011/**
James Smart891478a2009-11-18 15:40:23 -050010012 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
10013 * @phba: pointer to lpfc hba data structure.
10014 *
10015 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -040010016 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -050010017 **/
10018static void
10019lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
10020{
10021 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10022 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -040010023
10024 /*
10025 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
10026 * and let the SCSI mid-layer to retry them to recover.
10027 */
James Smartdb55fba2014-04-04 13:52:02 -040010028 lpfc_sli_abort_fcp_rings(phba);
James Smart891478a2009-11-18 15:40:23 -050010029}
10030
10031/**
James Smart0d878412009-10-02 15:16:56 -040010032 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
10033 * @phba: pointer to lpfc hba data structure.
10034 *
10035 * This routine is called to prepare the SLI3 device for PCI slot reset. It
10036 * disables the device interrupt and pci device, and aborts the internal FCP
10037 * pending I/Os.
10038 **/
10039static void
10040lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
10041{
James Smart0d878412009-10-02 15:16:56 -040010042 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -050010043 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -040010044
James Smart75baf692010-06-08 18:31:21 -040010045 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040010046 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040010047
James Smarte2af0d22010-03-15 11:25:32 -040010048 /* Block all SCSI devices' I/Os on the host */
10049 lpfc_scsi_dev_block(phba);
10050
James Smartea714f32013-04-17 20:18:47 -040010051 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
10052 lpfc_sli_flush_fcp_rings(phba);
10053
James Smarte2af0d22010-03-15 11:25:32 -040010054 /* stop all timers */
10055 lpfc_stop_hba_timers(phba);
10056
James Smart0d878412009-10-02 15:16:56 -040010057 /* Disable interrupt and pci device */
10058 lpfc_sli_disable_intr(phba);
10059 pci_disable_device(phba->pcidev);
James Smart0d878412009-10-02 15:16:56 -040010060}
10061
10062/**
10063 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
10064 * @phba: pointer to lpfc hba data structure.
10065 *
10066 * This routine is called to prepare the SLI3 device for PCI slot permanently
10067 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
10068 * pending I/Os.
10069 **/
10070static void
James Smart75baf692010-06-08 18:31:21 -040010071lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
James Smart0d878412009-10-02 15:16:56 -040010072{
10073 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -050010074 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -040010075 /* Block all SCSI devices' I/Os on the host */
10076 lpfc_scsi_dev_block(phba);
10077
10078 /* stop all timers */
10079 lpfc_stop_hba_timers(phba);
10080
James Smart0d878412009-10-02 15:16:56 -040010081 /* Clean up all driver's outstanding SCSI I/Os */
10082 lpfc_sli_flush_fcp_rings(phba);
10083}
10084
10085/**
James Smart3772a992009-05-22 14:50:54 -040010086 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -040010087 * @pdev: pointer to PCI device.
10088 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -060010089 *
James Smart3772a992009-05-22 14:50:54 -040010090 * This routine is called from the PCI subsystem for I/O error handling to
10091 * device with SLI-3 interface spec. This function is called by the PCI
10092 * subsystem after a PCI bus error affecting this device has been detected.
10093 * When this function is invoked, it will need to stop all the I/Os and
10094 * interrupt(s) to the device. Once that is done, it will return
10095 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
10096 * as desired.
James Smarte59058c2008-08-24 21:49:00 -040010097 *
10098 * Return codes
James Smart0d878412009-10-02 15:16:56 -040010099 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -040010100 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10101 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -040010102 **/
James Smart3772a992009-05-22 14:50:54 -040010103static pci_ers_result_t
10104lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -060010105{
James Smart51ef4c22007-08-02 11:10:31 -040010106 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10107 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060010108
James Smart0d878412009-10-02 15:16:56 -040010109 switch (state) {
10110 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -050010111 /* Non-fatal error, prepare for recovery */
10112 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -040010113 return PCI_ERS_RESULT_CAN_RECOVER;
10114 case pci_channel_io_frozen:
10115 /* Fatal error, prepare for slot reset */
10116 lpfc_sli_prep_dev_for_reset(phba);
10117 return PCI_ERS_RESULT_NEED_RESET;
10118 case pci_channel_io_perm_failure:
10119 /* Permanent failure, prepare for device down */
James Smart75baf692010-06-08 18:31:21 -040010120 lpfc_sli_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -060010121 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -040010122 default:
10123 /* Unknown state, prepare and request slot reset */
10124 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10125 "0472 Unknown PCI error state: x%x\n", state);
10126 lpfc_sli_prep_dev_for_reset(phba);
10127 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -040010128 }
Linas Vepstas8d63f372007-02-14 14:28:36 -060010129}
10130
10131/**
James Smart3772a992009-05-22 14:50:54 -040010132 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -040010133 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -060010134 *
James Smart3772a992009-05-22 14:50:54 -040010135 * This routine is called from the PCI subsystem for error handling to
10136 * device with SLI-3 interface spec. This is called after PCI bus has been
10137 * reset to restart the PCI card from scratch, as if from a cold-boot.
10138 * During the PCI subsystem error recovery, after driver returns
10139 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
10140 * recovery and then call this routine before calling the .resume method
10141 * to recover the device. This function will initialize the HBA device,
10142 * enable the interrupt, but it will just put the HBA to offline state
10143 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -040010144 *
10145 * Return codes
James Smart3772a992009-05-22 14:50:54 -040010146 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10147 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -060010148 */
James Smart3772a992009-05-22 14:50:54 -040010149static pci_ers_result_t
10150lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -060010151{
James Smart51ef4c22007-08-02 11:10:31 -040010152 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10153 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060010154 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -050010155 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -060010156
10157 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +110010158 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -060010159 printk(KERN_ERR "lpfc: Cannot re-enable "
10160 "PCI device after reset.\n");
10161 return PCI_ERS_RESULT_DISCONNECT;
10162 }
10163
James Smart97207482008-12-04 22:39:19 -050010164 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050010165
10166 /*
10167 * As the new kernel behavior of pci_restore_state() API call clears
10168 * device saved_state flag, need to save the restored state again.
10169 */
10170 pci_save_state(pdev);
10171
James Smart97207482008-12-04 22:39:19 -050010172 if (pdev->is_busmaster)
10173 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -060010174
James Smart92d7f7b2007-06-17 19:56:38 -050010175 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010176 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -050010177 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -060010178
James Smart5b75da22008-12-04 22:39:35 -050010179 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -040010180 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -050010181 if (intr_mode == LPFC_INTR_ERROR) {
10182 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10183 "0427 Cannot re-enable interrupt after "
10184 "slot reset.\n");
10185 return PCI_ERS_RESULT_DISCONNECT;
10186 } else
10187 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -060010188
James Smart75baf692010-06-08 18:31:21 -040010189 /* Take device offline, it will perform cleanup */
James Smart618a5232012-06-12 13:54:36 -040010190 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
Linas Vepstas8d63f372007-02-14 14:28:36 -060010191 lpfc_offline(phba);
10192 lpfc_sli_brdrestart(phba);
10193
James Smart5b75da22008-12-04 22:39:35 -050010194 /* Log the current active interrupt mode */
10195 lpfc_log_intr_mode(phba, phba->intr_mode);
10196
Linas Vepstas8d63f372007-02-14 14:28:36 -060010197 return PCI_ERS_RESULT_RECOVERED;
10198}
10199
10200/**
James Smart3772a992009-05-22 14:50:54 -040010201 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -040010202 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -060010203 *
James Smart3772a992009-05-22 14:50:54 -040010204 * This routine is called from the PCI subsystem for error handling to device
10205 * with SLI-3 interface spec. It is called when kernel error recovery tells
10206 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
10207 * error recovery. After this call, traffic can start to flow from this device
10208 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -060010209 */
James Smart3772a992009-05-22 14:50:54 -040010210static void
10211lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -060010212{
James Smart51ef4c22007-08-02 11:10:31 -040010213 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10214 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060010215
James Smarte2af0d22010-03-15 11:25:32 -040010216 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -040010217 lpfc_online(phba);
James Smart0d878412009-10-02 15:16:56 -040010218
10219 /* Clean up Advanced Error Reporting (AER) if needed */
10220 if (phba->hba_flag & HBA_AER_ENABLED)
10221 pci_cleanup_aer_uncorrect_error_status(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -060010222}
10223
James Smart3772a992009-05-22 14:50:54 -040010224/**
James Smartda0436e2009-05-22 14:51:39 -040010225 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
10226 * @phba: pointer to lpfc hba data structure.
10227 *
10228 * returns the number of ELS/CT IOCBs to reserve
10229 **/
10230int
10231lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
10232{
10233 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
10234
James Smartf1126682009-06-10 17:22:44 -040010235 if (phba->sli_rev == LPFC_SLI_REV4) {
10236 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -040010237 return 10;
James Smartf1126682009-06-10 17:22:44 -040010238 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -040010239 return 25;
James Smartf1126682009-06-10 17:22:44 -040010240 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -040010241 return 50;
James Smartf1126682009-06-10 17:22:44 -040010242 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -040010243 return 100;
James Smart8a9d2e82012-05-09 21:16:12 -040010244 else if (max_xri <= 1536)
James Smart6a9c52c2009-10-02 15:16:51 -040010245 return 150;
James Smart8a9d2e82012-05-09 21:16:12 -040010246 else if (max_xri <= 2048)
10247 return 200;
10248 else
10249 return 250;
James Smartf1126682009-06-10 17:22:44 -040010250 } else
10251 return 0;
James Smartda0436e2009-05-22 14:51:39 -040010252}
10253
10254/**
James Smart52d52442011-05-24 11:42:45 -040010255 * lpfc_write_firmware - attempt to write a firmware image to the port
James Smart52d52442011-05-24 11:42:45 -040010256 * @fw: pointer to firmware image returned from request_firmware.
James Smartce396282012-09-29 11:30:56 -040010257 * @phba: pointer to lpfc hba data structure.
James Smart52d52442011-05-24 11:42:45 -040010258 *
James Smart52d52442011-05-24 11:42:45 -040010259 **/
James Smartce396282012-09-29 11:30:56 -040010260static void
10261lpfc_write_firmware(const struct firmware *fw, void *context)
James Smart52d52442011-05-24 11:42:45 -040010262{
James Smartce396282012-09-29 11:30:56 -040010263 struct lpfc_hba *phba = (struct lpfc_hba *)context;
James Smart6b5151f2012-01-18 16:24:06 -050010264 char fwrev[FW_REV_STR_SIZE];
James Smartce396282012-09-29 11:30:56 -040010265 struct lpfc_grp_hdr *image;
James Smart52d52442011-05-24 11:42:45 -040010266 struct list_head dma_buffer_list;
10267 int i, rc = 0;
10268 struct lpfc_dmabuf *dmabuf, *next;
10269 uint32_t offset = 0, temp_offset = 0;
10270
James Smartc71ab862012-10-31 14:44:33 -040010271 /* It can be null in no-wait mode, sanity check */
James Smartce396282012-09-29 11:30:56 -040010272 if (!fw) {
10273 rc = -ENXIO;
10274 goto out;
10275 }
10276 image = (struct lpfc_grp_hdr *)fw->data;
10277
James Smart52d52442011-05-24 11:42:45 -040010278 INIT_LIST_HEAD(&dma_buffer_list);
James Smart079b5c92011-08-21 21:48:49 -040010279 if ((be32_to_cpu(image->magic_number) != LPFC_GROUP_OJECT_MAGIC_NUM) ||
10280 (bf_get_be32(lpfc_grp_hdr_file_type, image) !=
10281 LPFC_FILE_TYPE_GROUP) ||
10282 (bf_get_be32(lpfc_grp_hdr_id, image) != LPFC_FILE_ID_GROUP) ||
10283 (be32_to_cpu(image->size) != fw->size)) {
James Smart52d52442011-05-24 11:42:45 -040010284 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10285 "3022 Invalid FW image found. "
James Smart079b5c92011-08-21 21:48:49 -040010286 "Magic:%x Type:%x ID:%x\n",
10287 be32_to_cpu(image->magic_number),
10288 bf_get_be32(lpfc_grp_hdr_file_type, image),
10289 bf_get_be32(lpfc_grp_hdr_id, image));
James Smartce396282012-09-29 11:30:56 -040010290 rc = -EINVAL;
10291 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010292 }
10293 lpfc_decode_firmware_rev(phba, fwrev, 1);
James Smart88a2cfb2011-07-22 18:36:33 -040010294 if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
James Smart52d52442011-05-24 11:42:45 -040010295 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartce396282012-09-29 11:30:56 -040010296 "3023 Updating Firmware, Current Version:%s "
James Smart52d52442011-05-24 11:42:45 -040010297 "New Version:%s\n",
James Smart88a2cfb2011-07-22 18:36:33 -040010298 fwrev, image->revision);
James Smart52d52442011-05-24 11:42:45 -040010299 for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
10300 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
10301 GFP_KERNEL);
10302 if (!dmabuf) {
10303 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040010304 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010305 }
10306 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
10307 SLI4_PAGE_SIZE,
10308 &dmabuf->phys,
10309 GFP_KERNEL);
10310 if (!dmabuf->virt) {
10311 kfree(dmabuf);
10312 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040010313 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010314 }
10315 list_add_tail(&dmabuf->list, &dma_buffer_list);
10316 }
10317 while (offset < fw->size) {
10318 temp_offset = offset;
10319 list_for_each_entry(dmabuf, &dma_buffer_list, list) {
James Smart079b5c92011-08-21 21:48:49 -040010320 if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
James Smart52d52442011-05-24 11:42:45 -040010321 memcpy(dmabuf->virt,
10322 fw->data + temp_offset,
James Smart079b5c92011-08-21 21:48:49 -040010323 fw->size - temp_offset);
10324 temp_offset = fw->size;
James Smart52d52442011-05-24 11:42:45 -040010325 break;
10326 }
James Smart52d52442011-05-24 11:42:45 -040010327 memcpy(dmabuf->virt, fw->data + temp_offset,
10328 SLI4_PAGE_SIZE);
James Smart88a2cfb2011-07-22 18:36:33 -040010329 temp_offset += SLI4_PAGE_SIZE;
James Smart52d52442011-05-24 11:42:45 -040010330 }
10331 rc = lpfc_wr_object(phba, &dma_buffer_list,
10332 (fw->size - offset), &offset);
James Smartce396282012-09-29 11:30:56 -040010333 if (rc)
10334 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010335 }
10336 rc = offset;
10337 }
James Smartce396282012-09-29 11:30:56 -040010338
10339release_out:
James Smart52d52442011-05-24 11:42:45 -040010340 list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
10341 list_del(&dmabuf->list);
10342 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
10343 dmabuf->virt, dmabuf->phys);
10344 kfree(dmabuf);
10345 }
James Smartce396282012-09-29 11:30:56 -040010346 release_firmware(fw);
10347out:
10348 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartc71ab862012-10-31 14:44:33 -040010349 "3024 Firmware update done: %d.\n", rc);
James Smartce396282012-09-29 11:30:56 -040010350 return;
James Smart52d52442011-05-24 11:42:45 -040010351}
10352
10353/**
James Smartc71ab862012-10-31 14:44:33 -040010354 * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
10355 * @phba: pointer to lpfc hba data structure.
10356 *
10357 * This routine is called to perform Linux generic firmware upgrade on device
10358 * that supports such feature.
10359 **/
10360int
10361lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
10362{
10363 uint8_t file_name[ELX_MODEL_NAME_SIZE];
10364 int ret;
10365 const struct firmware *fw;
10366
10367 /* Only supported on SLI4 interface type 2 for now */
10368 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
10369 LPFC_SLI_INTF_IF_TYPE_2)
10370 return -EPERM;
10371
10372 snprintf(file_name, ELX_MODEL_NAME_SIZE, "%s.grp", phba->ModelName);
10373
10374 if (fw_upgrade == INT_FW_UPGRADE) {
10375 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
10376 file_name, &phba->pcidev->dev,
10377 GFP_KERNEL, (void *)phba,
10378 lpfc_write_firmware);
10379 } else if (fw_upgrade == RUN_FW_UPGRADE) {
10380 ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
10381 if (!ret)
10382 lpfc_write_firmware(fw, (void *)phba);
10383 } else {
10384 ret = -EINVAL;
10385 }
10386
10387 return ret;
10388}
10389
10390/**
James Smartda0436e2009-05-22 14:51:39 -040010391 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
10392 * @pdev: pointer to PCI device
10393 * @pid: pointer to PCI device identifier
10394 *
10395 * This routine is called from the kernel's PCI subsystem to device with
10396 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
10397 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
10398 * information of the device and driver to see if the driver state that it
10399 * can support this kind of device. If the match is successful, the driver
10400 * core invokes this routine. If this routine determines it can claim the HBA,
10401 * it does all the initialization that it needs to do to handle the HBA
10402 * properly.
10403 *
10404 * Return code
10405 * 0 - driver can claim the device
10406 * negative value - driver can not claim the device
10407 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010408static int
James Smartda0436e2009-05-22 14:51:39 -040010409lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
10410{
10411 struct lpfc_hba *phba;
10412 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -040010413 struct Scsi_Host *shost = NULL;
Sebastian Herbsztdb6f1c22015-08-31 16:48:14 -040010414 int error;
James Smartda0436e2009-05-22 14:51:39 -040010415 uint32_t cfg_mode, intr_mode;
James Smart67d12732012-08-03 12:36:13 -040010416 int adjusted_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -040010417
10418 /* Allocate memory for HBA structure */
10419 phba = lpfc_hba_alloc(pdev);
10420 if (!phba)
10421 return -ENOMEM;
10422
10423 /* Perform generic PCI device enabling operation */
10424 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -040010425 if (error)
James Smartda0436e2009-05-22 14:51:39 -040010426 goto out_free_phba;
James Smartda0436e2009-05-22 14:51:39 -040010427
10428 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
10429 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
10430 if (error)
10431 goto out_disable_pci_dev;
10432
10433 /* Set up SLI-4 specific device PCI memory space */
10434 error = lpfc_sli4_pci_mem_setup(phba);
10435 if (error) {
10436 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10437 "1410 Failed to set up pci memory space.\n");
10438 goto out_disable_pci_dev;
10439 }
10440
10441 /* Set up phase-1 common device driver resources */
10442 error = lpfc_setup_driver_resource_phase1(phba);
10443 if (error) {
10444 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10445 "1411 Failed to set up driver resource.\n");
10446 goto out_unset_pci_mem_s4;
10447 }
10448
10449 /* Set up SLI-4 Specific device driver resources */
10450 error = lpfc_sli4_driver_resource_setup(phba);
10451 if (error) {
10452 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10453 "1412 Failed to set up driver resource.\n");
10454 goto out_unset_pci_mem_s4;
10455 }
10456
10457 /* Initialize and populate the iocb list per host */
James Smart2a9bf3d2010-06-07 15:24:45 -040010458
10459 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10460 "2821 initialize iocb list %d.\n",
10461 phba->cfg_iocb_cnt*1024);
10462 error = lpfc_init_iocb_list(phba, phba->cfg_iocb_cnt*1024);
10463
James Smartda0436e2009-05-22 14:51:39 -040010464 if (error) {
10465 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10466 "1413 Failed to initialize iocb list.\n");
10467 goto out_unset_driver_resource_s4;
10468 }
10469
James Smart19ca7602010-11-20 23:11:55 -050010470 INIT_LIST_HEAD(&phba->active_rrq_list);
James Smart7d791df2011-07-22 18:37:52 -040010471 INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
James Smart19ca7602010-11-20 23:11:55 -050010472
James Smartda0436e2009-05-22 14:51:39 -040010473 /* Set up common device driver resources */
10474 error = lpfc_setup_driver_resource_phase2(phba);
10475 if (error) {
10476 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10477 "1414 Failed to set up driver resource.\n");
10478 goto out_free_iocb_list;
10479 }
10480
James Smart079b5c92011-08-21 21:48:49 -040010481 /* Get the default values for Model Name and Description */
10482 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
10483
James Smartda0436e2009-05-22 14:51:39 -040010484 /* Create SCSI host to the physical port */
10485 error = lpfc_create_shost(phba);
10486 if (error) {
10487 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10488 "1415 Failed to create scsi host.\n");
10489 goto out_unset_driver_resource;
10490 }
10491
10492 /* Configure sysfs attributes */
10493 vport = phba->pport;
10494 error = lpfc_alloc_sysfs_attr(vport);
10495 if (error) {
10496 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10497 "1416 Failed to allocate sysfs attr\n");
10498 goto out_destroy_shost;
10499 }
10500
James Smart6669f9b2009-10-02 15:16:45 -040010501 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -040010502 /* Now, trying to enable interrupt and bring up the device */
10503 cfg_mode = phba->cfg_use_msi;
James Smartda0436e2009-05-22 14:51:39 -040010504
James Smart7b15db32013-01-03 15:43:29 -050010505 /* Put device to a known state before enabling interrupt */
10506 lpfc_stop_port(phba);
10507 /* Configure and enable interrupt */
10508 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
10509 if (intr_mode == LPFC_INTR_ERROR) {
10510 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10511 "0426 Failed to enable interrupt.\n");
10512 error = -ENODEV;
10513 goto out_free_sysfs_attr;
James Smartda0436e2009-05-22 14:51:39 -040010514 }
James Smart7b15db32013-01-03 15:43:29 -050010515 /* Default to single EQ for non-MSI-X */
10516 if (phba->intr_type != MSIX)
10517 adjusted_fcp_io_channel = 1;
10518 else
10519 adjusted_fcp_io_channel = phba->cfg_fcp_io_channel;
10520 phba->cfg_fcp_io_channel = adjusted_fcp_io_channel;
10521 /* Set up SLI-4 HBA */
10522 if (lpfc_sli4_hba_setup(phba)) {
10523 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10524 "1421 Failed to set up hba\n");
10525 error = -ENODEV;
10526 goto out_disable_intr;
10527 }
10528
10529 /* Log the current active interrupt mode */
10530 phba->intr_mode = intr_mode;
10531 lpfc_log_intr_mode(phba, intr_mode);
James Smartda0436e2009-05-22 14:51:39 -040010532
10533 /* Perform post initialization setup */
10534 lpfc_post_init_setup(phba);
10535
James Smartc71ab862012-10-31 14:44:33 -040010536 /* check for firmware upgrade or downgrade */
10537 if (phba->cfg_request_firmware_upgrade)
Sebastian Herbsztdb6f1c22015-08-31 16:48:14 -040010538 lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
James Smart52d52442011-05-24 11:42:45 -040010539
James Smart1c6834a2009-07-19 10:01:26 -040010540 /* Check if there are static vports to be created. */
10541 lpfc_create_static_vport(phba);
James Smartda0436e2009-05-22 14:51:39 -040010542 return 0;
10543
10544out_disable_intr:
10545 lpfc_sli4_disable_intr(phba);
10546out_free_sysfs_attr:
10547 lpfc_free_sysfs_attr(vport);
10548out_destroy_shost:
10549 lpfc_destroy_shost(phba);
10550out_unset_driver_resource:
10551 lpfc_unset_driver_resource_phase2(phba);
10552out_free_iocb_list:
10553 lpfc_free_iocb_list(phba);
10554out_unset_driver_resource_s4:
10555 lpfc_sli4_driver_resource_unset(phba);
10556out_unset_pci_mem_s4:
10557 lpfc_sli4_pci_mem_unset(phba);
10558out_disable_pci_dev:
10559 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040010560 if (shost)
10561 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -040010562out_free_phba:
10563 lpfc_hba_free(phba);
10564 return error;
10565}
10566
10567/**
10568 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
10569 * @pdev: pointer to PCI device
10570 *
10571 * This routine is called from the kernel's PCI subsystem to device with
10572 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
10573 * removed from PCI bus, it performs all the necessary cleanup for the HBA
10574 * device to be removed from the PCI subsystem properly.
10575 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010576static void
James Smartda0436e2009-05-22 14:51:39 -040010577lpfc_pci_remove_one_s4(struct pci_dev *pdev)
10578{
10579 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10580 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
10581 struct lpfc_vport **vports;
10582 struct lpfc_hba *phba = vport->phba;
10583 int i;
10584
10585 /* Mark the device unloading flag */
10586 spin_lock_irq(&phba->hbalock);
10587 vport->load_flag |= FC_UNLOADING;
10588 spin_unlock_irq(&phba->hbalock);
10589
10590 /* Free the HBA sysfs attributes */
10591 lpfc_free_sysfs_attr(vport);
10592
10593 /* Release all the vports against this physical port */
10594 vports = lpfc_create_vport_work_array(phba);
10595 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040010596 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
10597 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
10598 continue;
James Smartda0436e2009-05-22 14:51:39 -040010599 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040010600 }
James Smartda0436e2009-05-22 14:51:39 -040010601 lpfc_destroy_vport_work_array(phba, vports);
10602
10603 /* Remove FC host and then SCSI host with the physical port */
10604 fc_remove_host(shost);
10605 scsi_remove_host(shost);
10606
10607 /* Perform cleanup on the physical port */
10608 lpfc_cleanup(vport);
10609
10610 /*
10611 * Bring down the SLI Layer. This step disables all interrupts,
10612 * clears the rings, discards all mailbox commands, and resets
10613 * the HBA FCoE function.
10614 */
10615 lpfc_debugfs_terminate(vport);
10616 lpfc_sli4_hba_unset(phba);
10617
10618 spin_lock_irq(&phba->hbalock);
10619 list_del_init(&vport->listentry);
10620 spin_unlock_irq(&phba->hbalock);
10621
James Smart3677a3a2010-09-29 11:19:14 -040010622 /* Perform scsi free before driver resource_unset since scsi
James Smartda0436e2009-05-22 14:51:39 -040010623 * buffers are released to their corresponding pools here.
10624 */
10625 lpfc_scsi_free(phba);
James Smart67d12732012-08-03 12:36:13 -040010626
James Smartda0436e2009-05-22 14:51:39 -040010627 lpfc_sli4_driver_resource_unset(phba);
10628
10629 /* Unmap adapter Control and Doorbell registers */
10630 lpfc_sli4_pci_mem_unset(phba);
10631
10632 /* Release PCI resources and disable device's PCI function */
10633 scsi_host_put(shost);
10634 lpfc_disable_pci_dev(phba);
10635
10636 /* Finally, free the driver's device data structure */
10637 lpfc_hba_free(phba);
10638
10639 return;
10640}
10641
10642/**
10643 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
10644 * @pdev: pointer to PCI device
10645 * @msg: power management message
10646 *
10647 * This routine is called from the kernel's PCI subsystem to support system
10648 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
10649 * this method, it quiesces the device by stopping the driver's worker
10650 * thread for the device, turning off device's interrupt and DMA, and bring
10651 * the device offline. Note that as the driver implements the minimum PM
10652 * requirements to a power-aware driver's PM support for suspend/resume -- all
10653 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
10654 * method call will be treated as SUSPEND and the driver will fully
10655 * reinitialize its device during resume() method call, the driver will set
10656 * device to PCI_D3hot state in PCI config space instead of setting it
10657 * according to the @msg provided by the PM.
10658 *
10659 * Return code
10660 * 0 - driver suspended the device
10661 * Error otherwise
10662 **/
10663static int
10664lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
10665{
10666 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10667 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10668
10669 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart75baf692010-06-08 18:31:21 -040010670 "2843 PCI device Power Management suspend.\n");
James Smartda0436e2009-05-22 14:51:39 -040010671
10672 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040010673 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smartda0436e2009-05-22 14:51:39 -040010674 lpfc_offline(phba);
10675 kthread_stop(phba->worker_thread);
10676
10677 /* Disable interrupt from device */
10678 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040010679 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -040010680
10681 /* Save device state to PCI config space */
10682 pci_save_state(pdev);
10683 pci_set_power_state(pdev, PCI_D3hot);
10684
10685 return 0;
10686}
10687
10688/**
10689 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
10690 * @pdev: pointer to PCI device
10691 *
10692 * This routine is called from the kernel's PCI subsystem to support system
10693 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
10694 * this method, it restores the device's PCI config space state and fully
10695 * reinitializes the device and brings it online. Note that as the driver
10696 * implements the minimum PM requirements to a power-aware driver's PM for
10697 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
10698 * to the suspend() method call will be treated as SUSPEND and the driver
10699 * will fully reinitialize its device during resume() method call, the device
10700 * will be set to PCI_D0 directly in PCI config space before restoring the
10701 * state.
10702 *
10703 * Return code
10704 * 0 - driver suspended the device
10705 * Error otherwise
10706 **/
10707static int
10708lpfc_pci_resume_one_s4(struct pci_dev *pdev)
10709{
10710 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10711 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10712 uint32_t intr_mode;
10713 int error;
10714
10715 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10716 "0292 PCI device Power Management resume.\n");
10717
10718 /* Restore device state from PCI config space */
10719 pci_set_power_state(pdev, PCI_D0);
10720 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050010721
10722 /*
10723 * As the new kernel behavior of pci_restore_state() API call clears
10724 * device saved_state flag, need to save the restored state again.
10725 */
10726 pci_save_state(pdev);
10727
James Smartda0436e2009-05-22 14:51:39 -040010728 if (pdev->is_busmaster)
10729 pci_set_master(pdev);
10730
10731 /* Startup the kernel thread for this host adapter. */
10732 phba->worker_thread = kthread_run(lpfc_do_work, phba,
10733 "lpfc_worker_%d", phba->brd_no);
10734 if (IS_ERR(phba->worker_thread)) {
10735 error = PTR_ERR(phba->worker_thread);
10736 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10737 "0293 PM resume failed to start worker "
10738 "thread: error=x%x.\n", error);
10739 return error;
10740 }
10741
10742 /* Configure and enable interrupt */
10743 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
10744 if (intr_mode == LPFC_INTR_ERROR) {
10745 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10746 "0294 PM resume Failed to enable interrupt\n");
10747 return -EIO;
10748 } else
10749 phba->intr_mode = intr_mode;
10750
10751 /* Restart HBA and bring it online */
10752 lpfc_sli_brdrestart(phba);
10753 lpfc_online(phba);
10754
10755 /* Log the current active interrupt mode */
10756 lpfc_log_intr_mode(phba, phba->intr_mode);
10757
10758 return 0;
10759}
10760
10761/**
James Smart75baf692010-06-08 18:31:21 -040010762 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
10763 * @phba: pointer to lpfc hba data structure.
10764 *
10765 * This routine is called to prepare the SLI4 device for PCI slot recover. It
10766 * aborts all the outstanding SCSI I/Os to the pci device.
10767 **/
10768static void
10769lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
10770{
James Smart75baf692010-06-08 18:31:21 -040010771 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10772 "2828 PCI channel I/O abort preparing for recovery\n");
10773 /*
10774 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
10775 * and let the SCSI mid-layer to retry them to recover.
10776 */
James Smartdb55fba2014-04-04 13:52:02 -040010777 lpfc_sli_abort_fcp_rings(phba);
James Smart75baf692010-06-08 18:31:21 -040010778}
10779
10780/**
10781 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
10782 * @phba: pointer to lpfc hba data structure.
10783 *
10784 * This routine is called to prepare the SLI4 device for PCI slot reset. It
10785 * disables the device interrupt and pci device, and aborts the internal FCP
10786 * pending I/Os.
10787 **/
10788static void
10789lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
10790{
10791 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10792 "2826 PCI channel disable preparing for reset\n");
10793
10794 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040010795 lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
James Smart75baf692010-06-08 18:31:21 -040010796
10797 /* Block all SCSI devices' I/Os on the host */
10798 lpfc_scsi_dev_block(phba);
10799
James Smartea714f32013-04-17 20:18:47 -040010800 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
10801 lpfc_sli_flush_fcp_rings(phba);
10802
James Smart75baf692010-06-08 18:31:21 -040010803 /* stop all timers */
10804 lpfc_stop_hba_timers(phba);
10805
10806 /* Disable interrupt and pci device */
10807 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040010808 lpfc_sli4_queue_destroy(phba);
James Smart75baf692010-06-08 18:31:21 -040010809 pci_disable_device(phba->pcidev);
James Smart75baf692010-06-08 18:31:21 -040010810}
10811
10812/**
10813 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
10814 * @phba: pointer to lpfc hba data structure.
10815 *
10816 * This routine is called to prepare the SLI4 device for PCI slot permanently
10817 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
10818 * pending I/Os.
10819 **/
10820static void
10821lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
10822{
10823 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10824 "2827 PCI channel permanent disable for failure\n");
10825
10826 /* Block all SCSI devices' I/Os on the host */
10827 lpfc_scsi_dev_block(phba);
10828
10829 /* stop all timers */
10830 lpfc_stop_hba_timers(phba);
10831
10832 /* Clean up all driver's outstanding SCSI I/Os */
10833 lpfc_sli_flush_fcp_rings(phba);
10834}
10835
10836/**
James Smartda0436e2009-05-22 14:51:39 -040010837 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
10838 * @pdev: pointer to PCI device.
10839 * @state: the current PCI connection state.
10840 *
10841 * This routine is called from the PCI subsystem for error handling to device
10842 * with SLI-4 interface spec. This function is called by the PCI subsystem
10843 * after a PCI bus error affecting this device has been detected. When this
10844 * function is invoked, it will need to stop all the I/Os and interrupt(s)
10845 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
10846 * for the PCI subsystem to perform proper recovery as desired.
10847 *
10848 * Return codes
10849 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10850 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10851 **/
10852static pci_ers_result_t
10853lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
10854{
James Smart75baf692010-06-08 18:31:21 -040010855 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10856 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10857
10858 switch (state) {
10859 case pci_channel_io_normal:
10860 /* Non-fatal error, prepare for recovery */
10861 lpfc_sli4_prep_dev_for_recover(phba);
10862 return PCI_ERS_RESULT_CAN_RECOVER;
10863 case pci_channel_io_frozen:
10864 /* Fatal error, prepare for slot reset */
10865 lpfc_sli4_prep_dev_for_reset(phba);
10866 return PCI_ERS_RESULT_NEED_RESET;
10867 case pci_channel_io_perm_failure:
10868 /* Permanent failure, prepare for device down */
10869 lpfc_sli4_prep_dev_for_perm_failure(phba);
10870 return PCI_ERS_RESULT_DISCONNECT;
10871 default:
10872 /* Unknown state, prepare and request slot reset */
10873 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10874 "2825 Unknown PCI error state: x%x\n", state);
10875 lpfc_sli4_prep_dev_for_reset(phba);
10876 return PCI_ERS_RESULT_NEED_RESET;
10877 }
James Smartda0436e2009-05-22 14:51:39 -040010878}
10879
10880/**
10881 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
10882 * @pdev: pointer to PCI device.
10883 *
10884 * This routine is called from the PCI subsystem for error handling to device
10885 * with SLI-4 interface spec. It is called after PCI bus has been reset to
10886 * restart the PCI card from scratch, as if from a cold-boot. During the
10887 * PCI subsystem error recovery, after the driver returns
10888 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
10889 * recovery and then call this routine before calling the .resume method to
10890 * recover the device. This function will initialize the HBA device, enable
10891 * the interrupt, but it will just put the HBA to offline state without
10892 * passing any I/O traffic.
10893 *
10894 * Return codes
10895 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10896 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10897 */
10898static pci_ers_result_t
10899lpfc_io_slot_reset_s4(struct pci_dev *pdev)
10900{
James Smart75baf692010-06-08 18:31:21 -040010901 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10902 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10903 struct lpfc_sli *psli = &phba->sli;
10904 uint32_t intr_mode;
10905
10906 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
10907 if (pci_enable_device_mem(pdev)) {
10908 printk(KERN_ERR "lpfc: Cannot re-enable "
10909 "PCI device after reset.\n");
10910 return PCI_ERS_RESULT_DISCONNECT;
10911 }
10912
10913 pci_restore_state(pdev);
James Smart0a96e972011-07-22 18:37:28 -040010914
10915 /*
10916 * As the new kernel behavior of pci_restore_state() API call clears
10917 * device saved_state flag, need to save the restored state again.
10918 */
10919 pci_save_state(pdev);
10920
James Smart75baf692010-06-08 18:31:21 -040010921 if (pdev->is_busmaster)
10922 pci_set_master(pdev);
10923
10924 spin_lock_irq(&phba->hbalock);
10925 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
10926 spin_unlock_irq(&phba->hbalock);
10927
10928 /* Configure and enable interrupt */
10929 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
10930 if (intr_mode == LPFC_INTR_ERROR) {
10931 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10932 "2824 Cannot re-enable interrupt after "
10933 "slot reset.\n");
10934 return PCI_ERS_RESULT_DISCONNECT;
10935 } else
10936 phba->intr_mode = intr_mode;
10937
10938 /* Log the current active interrupt mode */
10939 lpfc_log_intr_mode(phba, phba->intr_mode);
10940
James Smartda0436e2009-05-22 14:51:39 -040010941 return PCI_ERS_RESULT_RECOVERED;
10942}
10943
10944/**
10945 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
10946 * @pdev: pointer to PCI device
10947 *
10948 * This routine is called from the PCI subsystem for error handling to device
10949 * with SLI-4 interface spec. It is called when kernel error recovery tells
10950 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
10951 * error recovery. After this call, traffic can start to flow from this device
10952 * again.
10953 **/
10954static void
10955lpfc_io_resume_s4(struct pci_dev *pdev)
10956{
James Smart75baf692010-06-08 18:31:21 -040010957 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10958 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10959
10960 /*
10961 * In case of slot reset, as function reset is performed through
10962 * mailbox command which needs DMA to be enabled, this operation
10963 * has to be moved to the io resume phase. Taking device offline
10964 * will perform the necessary cleanup.
10965 */
10966 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
10967 /* Perform device reset */
James Smart618a5232012-06-12 13:54:36 -040010968 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040010969 lpfc_offline(phba);
10970 lpfc_sli_brdrestart(phba);
10971 /* Bring the device back online */
10972 lpfc_online(phba);
10973 }
10974
10975 /* Clean up Advanced Error Reporting (AER) if needed */
10976 if (phba->hba_flag & HBA_AER_ENABLED)
10977 pci_cleanup_aer_uncorrect_error_status(pdev);
James Smartda0436e2009-05-22 14:51:39 -040010978}
10979
10980/**
James Smart3772a992009-05-22 14:50:54 -040010981 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
10982 * @pdev: pointer to PCI device
10983 * @pid: pointer to PCI device identifier
10984 *
10985 * This routine is to be registered to the kernel's PCI subsystem. When an
10986 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
10987 * at PCI device-specific information of the device and driver to see if the
10988 * driver state that it can support this kind of device. If the match is
10989 * successful, the driver core invokes this routine. This routine dispatches
10990 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
10991 * do all the initialization that it needs to do to handle the HBA device
10992 * properly.
10993 *
10994 * Return code
10995 * 0 - driver can claim the device
10996 * negative value - driver can not claim the device
10997 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010998static int
James Smart3772a992009-05-22 14:50:54 -040010999lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
11000{
11001 int rc;
James Smart8fa38512009-07-19 10:01:03 -040011002 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -040011003
James Smart28baac72010-02-12 14:42:03 -050011004 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -040011005 return -ENODEV;
11006
James Smart8fa38512009-07-19 10:01:03 -040011007 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -050011008 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -040011009 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040011010 else
James Smart3772a992009-05-22 14:50:54 -040011011 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040011012
James Smart3772a992009-05-22 14:50:54 -040011013 return rc;
11014}
11015
11016/**
11017 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
11018 * @pdev: pointer to PCI device
11019 *
11020 * This routine is to be registered to the kernel's PCI subsystem. When an
11021 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
11022 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
11023 * remove routine, which will perform all the necessary cleanup for the
11024 * device to be removed from the PCI subsystem properly.
11025 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011026static void
James Smart3772a992009-05-22 14:50:54 -040011027lpfc_pci_remove_one(struct pci_dev *pdev)
11028{
11029 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11030 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11031
11032 switch (phba->pci_dev_grp) {
11033 case LPFC_PCI_DEV_LP:
11034 lpfc_pci_remove_one_s3(pdev);
11035 break;
James Smartda0436e2009-05-22 14:51:39 -040011036 case LPFC_PCI_DEV_OC:
11037 lpfc_pci_remove_one_s4(pdev);
11038 break;
James Smart3772a992009-05-22 14:50:54 -040011039 default:
11040 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11041 "1424 Invalid PCI device group: 0x%x\n",
11042 phba->pci_dev_grp);
11043 break;
11044 }
11045 return;
11046}
11047
11048/**
11049 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
11050 * @pdev: pointer to PCI device
11051 * @msg: power management message
11052 *
11053 * This routine is to be registered to the kernel's PCI subsystem to support
11054 * system Power Management (PM). When PM invokes this method, it dispatches
11055 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
11056 * suspend the device.
11057 *
11058 * Return code
11059 * 0 - driver suspended the device
11060 * Error otherwise
11061 **/
11062static int
11063lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
11064{
11065 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11066 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11067 int rc = -ENODEV;
11068
11069 switch (phba->pci_dev_grp) {
11070 case LPFC_PCI_DEV_LP:
11071 rc = lpfc_pci_suspend_one_s3(pdev, msg);
11072 break;
James Smartda0436e2009-05-22 14:51:39 -040011073 case LPFC_PCI_DEV_OC:
11074 rc = lpfc_pci_suspend_one_s4(pdev, msg);
11075 break;
James Smart3772a992009-05-22 14:50:54 -040011076 default:
11077 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11078 "1425 Invalid PCI device group: 0x%x\n",
11079 phba->pci_dev_grp);
11080 break;
11081 }
11082 return rc;
11083}
11084
11085/**
11086 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
11087 * @pdev: pointer to PCI device
11088 *
11089 * This routine is to be registered to the kernel's PCI subsystem to support
11090 * system Power Management (PM). When PM invokes this method, it dispatches
11091 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
11092 * resume the device.
11093 *
11094 * Return code
11095 * 0 - driver suspended the device
11096 * Error otherwise
11097 **/
11098static int
11099lpfc_pci_resume_one(struct pci_dev *pdev)
11100{
11101 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11102 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11103 int rc = -ENODEV;
11104
11105 switch (phba->pci_dev_grp) {
11106 case LPFC_PCI_DEV_LP:
11107 rc = lpfc_pci_resume_one_s3(pdev);
11108 break;
James Smartda0436e2009-05-22 14:51:39 -040011109 case LPFC_PCI_DEV_OC:
11110 rc = lpfc_pci_resume_one_s4(pdev);
11111 break;
James Smart3772a992009-05-22 14:50:54 -040011112 default:
11113 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11114 "1426 Invalid PCI device group: 0x%x\n",
11115 phba->pci_dev_grp);
11116 break;
11117 }
11118 return rc;
11119}
11120
11121/**
11122 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
11123 * @pdev: pointer to PCI device.
11124 * @state: the current PCI connection state.
11125 *
11126 * This routine is registered to the PCI subsystem for error handling. This
11127 * function is called by the PCI subsystem after a PCI bus error affecting
11128 * this device has been detected. When this routine is invoked, it dispatches
11129 * the action to the proper SLI-3 or SLI-4 device error detected handling
11130 * routine, which will perform the proper error detected operation.
11131 *
11132 * Return codes
11133 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
11134 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11135 **/
11136static pci_ers_result_t
11137lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
11138{
11139 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11140 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11141 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
11142
11143 switch (phba->pci_dev_grp) {
11144 case LPFC_PCI_DEV_LP:
11145 rc = lpfc_io_error_detected_s3(pdev, state);
11146 break;
James Smartda0436e2009-05-22 14:51:39 -040011147 case LPFC_PCI_DEV_OC:
11148 rc = lpfc_io_error_detected_s4(pdev, state);
11149 break;
James Smart3772a992009-05-22 14:50:54 -040011150 default:
11151 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11152 "1427 Invalid PCI device group: 0x%x\n",
11153 phba->pci_dev_grp);
11154 break;
11155 }
11156 return rc;
11157}
11158
11159/**
11160 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
11161 * @pdev: pointer to PCI device.
11162 *
11163 * This routine is registered to the PCI subsystem for error handling. This
11164 * function is called after PCI bus has been reset to restart the PCI card
11165 * from scratch, as if from a cold-boot. When this routine is invoked, it
11166 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
11167 * routine, which will perform the proper device reset.
11168 *
11169 * Return codes
11170 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
11171 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11172 **/
11173static pci_ers_result_t
11174lpfc_io_slot_reset(struct pci_dev *pdev)
11175{
11176 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11177 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11178 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
11179
11180 switch (phba->pci_dev_grp) {
11181 case LPFC_PCI_DEV_LP:
11182 rc = lpfc_io_slot_reset_s3(pdev);
11183 break;
James Smartda0436e2009-05-22 14:51:39 -040011184 case LPFC_PCI_DEV_OC:
11185 rc = lpfc_io_slot_reset_s4(pdev);
11186 break;
James Smart3772a992009-05-22 14:50:54 -040011187 default:
11188 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11189 "1428 Invalid PCI device group: 0x%x\n",
11190 phba->pci_dev_grp);
11191 break;
11192 }
11193 return rc;
11194}
11195
11196/**
11197 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
11198 * @pdev: pointer to PCI device
11199 *
11200 * This routine is registered to the PCI subsystem for error handling. It
11201 * is called when kernel error recovery tells the lpfc driver that it is
11202 * OK to resume normal PCI operation after PCI bus error recovery. When
11203 * this routine is invoked, it dispatches the action to the proper SLI-3
11204 * or SLI-4 device io_resume routine, which will resume the device operation.
11205 **/
11206static void
11207lpfc_io_resume(struct pci_dev *pdev)
11208{
11209 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11210 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11211
11212 switch (phba->pci_dev_grp) {
11213 case LPFC_PCI_DEV_LP:
11214 lpfc_io_resume_s3(pdev);
11215 break;
James Smartda0436e2009-05-22 14:51:39 -040011216 case LPFC_PCI_DEV_OC:
11217 lpfc_io_resume_s4(pdev);
11218 break;
James Smart3772a992009-05-22 14:50:54 -040011219 default:
11220 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11221 "1429 Invalid PCI device group: 0x%x\n",
11222 phba->pci_dev_grp);
11223 break;
11224 }
11225 return;
11226}
11227
James Smart1ba981f2014-02-20 09:56:45 -050011228/**
11229 * lpfc_sli4_oas_verify - Verify OAS is supported by this adapter
11230 * @phba: pointer to lpfc hba data structure.
11231 *
11232 * This routine checks to see if OAS is supported for this adapter. If
11233 * supported, the configure Flash Optimized Fabric flag is set. Otherwise,
11234 * the enable oas flag is cleared and the pool created for OAS device data
11235 * is destroyed.
11236 *
11237 **/
11238void
11239lpfc_sli4_oas_verify(struct lpfc_hba *phba)
11240{
11241
11242 if (!phba->cfg_EnableXLane)
11243 return;
11244
11245 if (phba->sli4_hba.pc_sli4_params.oas_supported) {
11246 phba->cfg_fof = 1;
11247 } else {
James Smartf38fa0b2014-04-04 13:52:21 -040011248 phba->cfg_fof = 0;
James Smart1ba981f2014-02-20 09:56:45 -050011249 if (phba->device_data_mem_pool)
11250 mempool_destroy(phba->device_data_mem_pool);
11251 phba->device_data_mem_pool = NULL;
11252 }
11253
11254 return;
11255}
11256
11257/**
11258 * lpfc_fof_queue_setup - Set up all the fof queues
11259 * @phba: pointer to lpfc hba data structure.
11260 *
11261 * This routine is invoked to set up all the fof queues for the FC HBA
11262 * operation.
11263 *
11264 * Return codes
11265 * 0 - successful
11266 * -ENOMEM - No available memory
11267 **/
11268int
11269lpfc_fof_queue_setup(struct lpfc_hba *phba)
11270{
11271 struct lpfc_sli *psli = &phba->sli;
11272 int rc;
11273
11274 rc = lpfc_eq_create(phba, phba->sli4_hba.fof_eq, LPFC_MAX_IMAX);
11275 if (rc)
11276 return -ENOMEM;
11277
James Smartf38fa0b2014-04-04 13:52:21 -040011278 if (phba->cfg_fof) {
James Smart1ba981f2014-02-20 09:56:45 -050011279
11280 rc = lpfc_cq_create(phba, phba->sli4_hba.oas_cq,
11281 phba->sli4_hba.fof_eq, LPFC_WCQ, LPFC_FCP);
11282 if (rc)
11283 goto out_oas_cq;
11284
11285 rc = lpfc_wq_create(phba, phba->sli4_hba.oas_wq,
11286 phba->sli4_hba.oas_cq, LPFC_FCP);
11287 if (rc)
11288 goto out_oas_wq;
11289
11290 phba->sli4_hba.oas_cq->pring = &psli->ring[LPFC_FCP_OAS_RING];
11291 phba->sli4_hba.oas_ring = &psli->ring[LPFC_FCP_OAS_RING];
11292 }
11293
11294 return 0;
11295
11296out_oas_wq:
James Smartf38fa0b2014-04-04 13:52:21 -040011297 lpfc_cq_destroy(phba, phba->sli4_hba.oas_cq);
James Smart1ba981f2014-02-20 09:56:45 -050011298out_oas_cq:
11299 lpfc_eq_destroy(phba, phba->sli4_hba.fof_eq);
11300 return rc;
11301
11302}
11303
11304/**
11305 * lpfc_fof_queue_create - Create all the fof queues
11306 * @phba: pointer to lpfc hba data structure.
11307 *
11308 * This routine is invoked to allocate all the fof queues for the FC HBA
11309 * operation. For each SLI4 queue type, the parameters such as queue entry
11310 * count (queue depth) shall be taken from the module parameter. For now,
11311 * we just use some constant number as place holder.
11312 *
11313 * Return codes
11314 * 0 - successful
11315 * -ENOMEM - No availble memory
11316 * -EIO - The mailbox failed to complete successfully.
11317 **/
11318int
11319lpfc_fof_queue_create(struct lpfc_hba *phba)
11320{
11321 struct lpfc_queue *qdesc;
11322
11323 /* Create FOF EQ */
11324 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
11325 phba->sli4_hba.eq_ecount);
11326 if (!qdesc)
11327 goto out_error;
11328
11329 phba->sli4_hba.fof_eq = qdesc;
11330
James Smartf38fa0b2014-04-04 13:52:21 -040011331 if (phba->cfg_fof) {
James Smart1ba981f2014-02-20 09:56:45 -050011332
11333 /* Create OAS CQ */
11334 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
11335 phba->sli4_hba.cq_ecount);
11336 if (!qdesc)
11337 goto out_error;
11338
11339 phba->sli4_hba.oas_cq = qdesc;
11340
11341 /* Create OAS WQ */
11342 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
11343 phba->sli4_hba.wq_ecount);
11344 if (!qdesc)
11345 goto out_error;
11346
11347 phba->sli4_hba.oas_wq = qdesc;
11348
11349 }
11350 return 0;
11351
11352out_error:
11353 lpfc_fof_queue_destroy(phba);
11354 return -ENOMEM;
11355}
11356
11357/**
11358 * lpfc_fof_queue_destroy - Destroy all the fof queues
11359 * @phba: pointer to lpfc hba data structure.
11360 *
11361 * This routine is invoked to release all the SLI4 queues with the FC HBA
11362 * operation.
11363 *
11364 * Return codes
11365 * 0 - successful
11366 **/
11367int
11368lpfc_fof_queue_destroy(struct lpfc_hba *phba)
11369{
11370 /* Release FOF Event queue */
11371 if (phba->sli4_hba.fof_eq != NULL) {
11372 lpfc_sli4_queue_free(phba->sli4_hba.fof_eq);
11373 phba->sli4_hba.fof_eq = NULL;
11374 }
11375
11376 /* Release OAS Completion queue */
11377 if (phba->sli4_hba.oas_cq != NULL) {
11378 lpfc_sli4_queue_free(phba->sli4_hba.oas_cq);
11379 phba->sli4_hba.oas_cq = NULL;
11380 }
11381
11382 /* Release OAS Work queue */
11383 if (phba->sli4_hba.oas_wq != NULL) {
11384 lpfc_sli4_queue_free(phba->sli4_hba.oas_wq);
11385 phba->sli4_hba.oas_wq = NULL;
11386 }
11387 return 0;
11388}
11389
dea31012005-04-17 16:05:31 -050011390MODULE_DEVICE_TABLE(pci, lpfc_id_table);
11391
Stephen Hemmingera55b2d22012-09-07 09:33:16 -070011392static const struct pci_error_handlers lpfc_err_handler = {
Linas Vepstas8d63f372007-02-14 14:28:36 -060011393 .error_detected = lpfc_io_error_detected,
11394 .slot_reset = lpfc_io_slot_reset,
11395 .resume = lpfc_io_resume,
11396};
11397
dea31012005-04-17 16:05:31 -050011398static struct pci_driver lpfc_driver = {
11399 .name = LPFC_DRIVER_NAME,
11400 .id_table = lpfc_id_table,
11401 .probe = lpfc_pci_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011402 .remove = lpfc_pci_remove_one,
James Smart3a55b532008-12-04 22:38:54 -050011403 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -040011404 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -050011405 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -050011406};
11407
James Smart3ef6d242012-01-18 16:23:48 -050011408static const struct file_operations lpfc_mgmt_fop = {
Al Viro858feac2013-04-14 22:39:37 -040011409 .owner = THIS_MODULE,
James Smart3ef6d242012-01-18 16:23:48 -050011410};
11411
11412static struct miscdevice lpfc_mgmt_dev = {
11413 .minor = MISC_DYNAMIC_MINOR,
11414 .name = "lpfcmgmt",
11415 .fops = &lpfc_mgmt_fop,
11416};
11417
James Smarte59058c2008-08-24 21:49:00 -040011418/**
James Smart3621a712009-04-06 18:47:14 -040011419 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -040011420 *
11421 * This routine is to be invoked when the lpfc module is loaded into the
11422 * kernel. The special kernel macro module_init() is used to indicate the
11423 * role of this routine to the kernel as lpfc module entry point.
11424 *
11425 * Return codes
11426 * 0 - successful
11427 * -ENOMEM - FC attach transport failed
11428 * all others - failed
11429 */
dea31012005-04-17 16:05:31 -050011430static int __init
11431lpfc_init(void)
11432{
James Smart7bb03bb2013-04-17 20:19:16 -040011433 int cpu;
dea31012005-04-17 16:05:31 -050011434 int error = 0;
11435
11436 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040011437 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -050011438
James Smart3ef6d242012-01-18 16:23:48 -050011439 error = misc_register(&lpfc_mgmt_dev);
11440 if (error)
11441 printk(KERN_ERR "Could not register lpfcmgmt device, "
11442 "misc_register returned with status %d", error);
11443
James Smart7ee5d432007-10-27 13:37:17 -040011444 if (lpfc_enable_npiv) {
11445 lpfc_transport_functions.vport_create = lpfc_vport_create;
11446 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
11447 }
dea31012005-04-17 16:05:31 -050011448 lpfc_transport_template =
11449 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -040011450 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -050011451 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -040011452 if (lpfc_enable_npiv) {
James Smart7ee5d432007-10-27 13:37:17 -040011453 lpfc_vport_transport_template =
James Smart98c9ea52007-10-27 13:37:33 -040011454 fc_attach_transport(&lpfc_vport_transport_functions);
11455 if (lpfc_vport_transport_template == NULL) {
11456 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040011457 return -ENOMEM;
James Smart98c9ea52007-10-27 13:37:33 -040011458 }
James Smart7ee5d432007-10-27 13:37:17 -040011459 }
James Smart7bb03bb2013-04-17 20:19:16 -040011460
11461 /* Initialize in case vector mapping is needed */
James Smartb246de12013-05-31 17:03:07 -040011462 lpfc_used_cpu = NULL;
11463 lpfc_present_cpu = 0;
11464 for_each_present_cpu(cpu)
11465 lpfc_present_cpu++;
James Smart7bb03bb2013-04-17 20:19:16 -040011466
dea31012005-04-17 16:05:31 -050011467 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -050011468 if (error) {
dea31012005-04-17 16:05:31 -050011469 fc_release_transport(lpfc_transport_template);
James Smartd7c255b2008-08-24 21:50:00 -040011470 if (lpfc_enable_npiv)
11471 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -050011472 }
dea31012005-04-17 16:05:31 -050011473
11474 return error;
11475}
11476
James Smarte59058c2008-08-24 21:49:00 -040011477/**
James Smart3621a712009-04-06 18:47:14 -040011478 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -040011479 *
11480 * This routine is invoked when the lpfc module is removed from the kernel.
11481 * The special kernel macro module_exit() is used to indicate the role of
11482 * this routine to the kernel as lpfc module exit point.
11483 */
dea31012005-04-17 16:05:31 -050011484static void __exit
11485lpfc_exit(void)
11486{
James Smart3ef6d242012-01-18 16:23:48 -050011487 misc_deregister(&lpfc_mgmt_dev);
dea31012005-04-17 16:05:31 -050011488 pci_unregister_driver(&lpfc_driver);
11489 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040011490 if (lpfc_enable_npiv)
11491 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -050011492 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040011493 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
11494 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050011495 (1L << _dump_buf_data_order), _dump_buf_data);
11496 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
11497 }
11498
11499 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -040011500 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
11501 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050011502 (1L << _dump_buf_dif_order), _dump_buf_dif);
11503 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
11504 }
James Smartb246de12013-05-31 17:03:07 -040011505 kfree(lpfc_used_cpu);
Johannes Thumshirn79739672015-08-31 16:48:11 -040011506 idr_destroy(&lpfc_hba_index);
dea31012005-04-17 16:05:31 -050011507}
11508
11509module_init(lpfc_init);
11510module_exit(lpfc_exit);
11511MODULE_LICENSE("GPL");
11512MODULE_DESCRIPTION(LPFC_MODULE_DESC);
11513MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
11514MODULE_VERSION("0:" LPFC_DRIVER_VERSION);