blob: 8d940ed2b5042019c8bb90f9425bfff36b430801 [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"
55
James Smart81301a92008-12-04 22:39:46 -050056char *_dump_buf_data;
57unsigned long _dump_buf_data_order;
58char *_dump_buf_dif;
59unsigned long _dump_buf_dif_order;
60spinlock_t _dump_buf_lock;
61
James Smart7bb03bb2013-04-17 20:19:16 -040062/* Used when mapping IRQ vectors in a driver centric manner */
James Smartb246de12013-05-31 17:03:07 -040063uint16_t *lpfc_used_cpu;
64uint32_t lpfc_present_cpu;
James Smart7bb03bb2013-04-17 20:19:16 -040065
dea31012005-04-17 16:05:31 -050066static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
67static int lpfc_post_rcv_buf(struct lpfc_hba *);
James Smart5350d872011-10-10 21:33:49 -040068static int lpfc_sli4_queue_verify(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040069static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
70static int lpfc_setup_endian_order(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040071static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
James Smart8a9d2e82012-05-09 21:16:12 -040072static void lpfc_free_els_sgl_list(struct lpfc_hba *);
73static void lpfc_init_sgl_list(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040074static int lpfc_init_active_sgl_array(struct lpfc_hba *);
75static void lpfc_free_active_sgl(struct lpfc_hba *);
76static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
77static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
78static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
79static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
80static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
James Smart618a5232012-06-12 13:54:36 -040081static void lpfc_sli4_disable_intr(struct lpfc_hba *);
82static uint32_t lpfc_sli4_enable_intr(struct lpfc_hba *, uint32_t);
James Smart1ba981f2014-02-20 09:56:45 -050083static void lpfc_sli4_oas_verify(struct lpfc_hba *phba);
dea31012005-04-17 16:05:31 -050084
85static struct scsi_transport_template *lpfc_transport_template = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050086static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea31012005-04-17 16:05:31 -050087static DEFINE_IDR(lpfc_hba_index);
88
James Smarte59058c2008-08-24 21:49:00 -040089/**
James Smart3621a712009-04-06 18:47:14 -040090 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
James Smarte59058c2008-08-24 21:49:00 -040091 * @phba: pointer to lpfc hba data structure.
92 *
93 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
94 * mailbox command. It retrieves the revision information from the HBA and
95 * collects the Vital Product Data (VPD) about the HBA for preparing the
96 * configuration of the HBA.
97 *
98 * Return codes:
99 * 0 - success.
100 * -ERESTART - requests the SLI layer to reset the HBA and try again.
101 * Any other value - indicates an error.
102 **/
dea31012005-04-17 16:05:31 -0500103int
James Smart2e0fef82007-06-17 19:56:36 -0500104lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500105{
106 lpfc_vpd_t *vp = &phba->vpd;
107 int i = 0, rc;
108 LPFC_MBOXQ_t *pmb;
109 MAILBOX_t *mb;
110 char *lpfc_vpd_data = NULL;
111 uint16_t offset = 0;
112 static char licensed[56] =
113 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -0400114 static int init_key = 1;
dea31012005-04-17 16:05:31 -0500115
116 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
117 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500118 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500119 return -ENOMEM;
120 }
121
James Smart04c68492009-05-22 14:52:52 -0400122 mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -0500123 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500124
125 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -0400126 if (init_key) {
127 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -0500128
James Smart65a29c12006-07-06 15:50:50 -0400129 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
130 *ptext = cpu_to_be32(*ptext);
131 init_key = 0;
132 }
dea31012005-04-17 16:05:31 -0500133
134 lpfc_read_nv(phba, pmb);
135 memset((char*)mb->un.varRDnvp.rsvd3, 0,
136 sizeof (mb->un.varRDnvp.rsvd3));
137 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
138 sizeof (licensed));
139
140 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
141
142 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500143 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smarte8b62012007-08-02 11:10:09 -0400144 "0324 Config Port initialization "
dea31012005-04-17 16:05:31 -0500145 "error, mbxCmd x%x READ_NVPARM, "
146 "mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500147 mb->mbxCommand, mb->mbxStatus);
148 mempool_free(pmb, phba->mbox_mem_pool);
149 return -ERESTART;
150 }
151 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
James Smart2e0fef82007-06-17 19:56:36 -0500152 sizeof(phba->wwnn));
153 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
154 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500155 }
156
James Smart92d7f7b2007-06-17 19:56:38 -0500157 phba->sli3_options = 0x0;
158
dea31012005-04-17 16:05:31 -0500159 /* Setup and issue mailbox READ REV command */
160 lpfc_read_rev(phba, pmb);
161 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
162 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500163 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400164 "0439 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500165 "READ_REV, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500166 mb->mbxCommand, mb->mbxStatus);
167 mempool_free( pmb, phba->mbox_mem_pool);
168 return -ERESTART;
169 }
170
James Smart92d7f7b2007-06-17 19:56:38 -0500171
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500172 /*
173 * The value of rr must be 1 since the driver set the cv field to 1.
174 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500175 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500176 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500177 vp->rev.rBit = 0;
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500178 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400179 "0440 Adapter failed to init, READ_REV has "
180 "missing revision information.\n");
dea31012005-04-17 16:05:31 -0500181 mempool_free(pmb, phba->mbox_mem_pool);
182 return -ERESTART;
dea31012005-04-17 16:05:31 -0500183 }
184
James Smart495a7142008-06-14 22:52:59 -0400185 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
186 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -0500187 return -EINVAL;
James Smart495a7142008-06-14 22:52:59 -0400188 }
James Smarted957682007-06-17 19:56:37 -0500189
dea31012005-04-17 16:05:31 -0500190 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500191 vp->rev.rBit = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500192 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500193 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
194 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
195 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
196 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500197 vp->rev.biuRev = mb->un.varRdRev.biuRev;
198 vp->rev.smRev = mb->un.varRdRev.smRev;
199 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
200 vp->rev.endecRev = mb->un.varRdRev.endecRev;
201 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
202 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
203 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
204 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
205 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
206 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
207
James Smart92d7f7b2007-06-17 19:56:38 -0500208 /* If the sli feature level is less then 9, we must
209 * tear down all RPIs and VPIs on link down if NPIV
210 * is enabled.
211 */
212 if (vp->rev.feaLevelHigh < 9)
213 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
214
dea31012005-04-17 16:05:31 -0500215 if (lpfc_is_LC_HBA(phba->pcidev->device))
216 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
217 sizeof (phba->RandomData));
218
dea31012005-04-17 16:05:31 -0500219 /* Get adapter VPD information */
dea31012005-04-17 16:05:31 -0500220 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
221 if (!lpfc_vpd_data)
James Smartd7c255b2008-08-24 21:50:00 -0400222 goto out_free_mbox;
dea31012005-04-17 16:05:31 -0500223 do {
James Smarta0c87cb2009-07-19 10:01:10 -0400224 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
dea31012005-04-17 16:05:31 -0500225 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
226
227 if (rc != MBX_SUCCESS) {
228 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400229 "0441 VPD not present on adapter, "
dea31012005-04-17 16:05:31 -0500230 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500231 mb->mbxCommand, mb->mbxStatus);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500232 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500233 }
James Smart04c68492009-05-22 14:52:52 -0400234 /* dump mem may return a zero when finished or we got a
235 * mailbox error, either way we are done.
236 */
237 if (mb->un.varDmp.word_cnt == 0)
238 break;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500239 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
240 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
James Smartd7c255b2008-08-24 21:50:00 -0400241 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
242 lpfc_vpd_data + offset,
James Smart92d7f7b2007-06-17 19:56:38 -0500243 mb->un.varDmp.word_cnt);
dea31012005-04-17 16:05:31 -0500244 offset += mb->un.varDmp.word_cnt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500245 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
246 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500247
248 kfree(lpfc_vpd_data);
dea31012005-04-17 16:05:31 -0500249out_free_mbox:
250 mempool_free(pmb, phba->mbox_mem_pool);
251 return 0;
252}
253
James Smarte59058c2008-08-24 21:49:00 -0400254/**
James Smart3621a712009-04-06 18:47:14 -0400255 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
James Smarte59058c2008-08-24 21:49:00 -0400256 * @phba: pointer to lpfc hba data structure.
257 * @pmboxq: pointer to the driver internal queue element for mailbox command.
258 *
259 * This is the completion handler for driver's configuring asynchronous event
260 * mailbox command to the device. If the mailbox command returns successfully,
261 * it will set internal async event support flag to 1; otherwise, it will
262 * set internal async event support flag to 0.
263 **/
James Smart57127f12007-10-27 13:37:05 -0400264static void
265lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
266{
James Smart04c68492009-05-22 14:52:52 -0400267 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
James Smart57127f12007-10-27 13:37:05 -0400268 phba->temp_sensor_support = 1;
269 else
270 phba->temp_sensor_support = 0;
271 mempool_free(pmboxq, phba->mbox_mem_pool);
272 return;
273}
274
James Smarte59058c2008-08-24 21:49:00 -0400275/**
James Smart3621a712009-04-06 18:47:14 -0400276 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
James Smart97207482008-12-04 22:39:19 -0500277 * @phba: pointer to lpfc hba data structure.
278 * @pmboxq: pointer to the driver internal queue element for mailbox command.
279 *
280 * This is the completion handler for dump mailbox command for getting
281 * wake up parameters. When this command complete, the response contain
282 * Option rom version of the HBA. This function translate the version number
283 * into a human readable string and store it in OptionROMVersion.
284 **/
285static void
286lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
287{
288 struct prog_id *prg;
289 uint32_t prog_id_word;
290 char dist = ' ';
291 /* character array used for decoding dist type. */
292 char dist_char[] = "nabx";
293
James Smart04c68492009-05-22 14:52:52 -0400294 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
James Smart9f1e1b52008-12-04 22:39:40 -0500295 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500296 return;
James Smart9f1e1b52008-12-04 22:39:40 -0500297 }
James Smart97207482008-12-04 22:39:19 -0500298
299 prg = (struct prog_id *) &prog_id_word;
300
301 /* word 7 contain option rom version */
James Smart04c68492009-05-22 14:52:52 -0400302 prog_id_word = pmboxq->u.mb.un.varWords[7];
James Smart97207482008-12-04 22:39:19 -0500303
304 /* Decode the Option rom version word to a readable string */
305 if (prg->dist < 4)
306 dist = dist_char[prg->dist];
307
308 if ((prg->dist == 3) && (prg->num == 0))
James Smarta2fc4aef2014-09-03 12:57:55 -0400309 snprintf(phba->OptionROMVersion, 32, "%d.%d%d",
James Smart97207482008-12-04 22:39:19 -0500310 prg->ver, prg->rev, prg->lev);
311 else
James Smarta2fc4aef2014-09-03 12:57:55 -0400312 snprintf(phba->OptionROMVersion, 32, "%d.%d%d%c%d",
James Smart97207482008-12-04 22:39:19 -0500313 prg->ver, prg->rev, prg->lev,
314 dist, prg->num);
James Smart9f1e1b52008-12-04 22:39:40 -0500315 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500316 return;
317}
318
319/**
James Smart05580562011-05-24 11:40:48 -0400320 * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
321 * cfg_soft_wwnn, cfg_soft_wwpn
322 * @vport: pointer to lpfc vport data structure.
323 *
324 *
325 * Return codes
326 * None.
327 **/
328void
329lpfc_update_vport_wwn(struct lpfc_vport *vport)
330{
331 /* If the soft name exists then update it using the service params */
332 if (vport->phba->cfg_soft_wwnn)
333 u64_to_wwn(vport->phba->cfg_soft_wwnn,
334 vport->fc_sparam.nodeName.u.wwn);
335 if (vport->phba->cfg_soft_wwpn)
336 u64_to_wwn(vport->phba->cfg_soft_wwpn,
337 vport->fc_sparam.portName.u.wwn);
338
339 /*
340 * If the name is empty or there exists a soft name
341 * then copy the service params name, otherwise use the fc name
342 */
343 if (vport->fc_nodename.u.wwn[0] == 0 || vport->phba->cfg_soft_wwnn)
344 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
345 sizeof(struct lpfc_name));
346 else
347 memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
348 sizeof(struct lpfc_name));
349
350 if (vport->fc_portname.u.wwn[0] == 0 || vport->phba->cfg_soft_wwpn)
351 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
352 sizeof(struct lpfc_name));
353 else
354 memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
355 sizeof(struct lpfc_name));
356}
357
358/**
James Smart3621a712009-04-06 18:47:14 -0400359 * lpfc_config_port_post - Perform lpfc initialization after config port
James Smarte59058c2008-08-24 21:49:00 -0400360 * @phba: pointer to lpfc hba data structure.
361 *
362 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
363 * command call. It performs all internal resource and state setups on the
364 * port: post IOCB buffers, enable appropriate host interrupt attentions,
365 * ELS ring timers, etc.
366 *
367 * Return codes
368 * 0 - success.
369 * Any other value - error.
370 **/
dea31012005-04-17 16:05:31 -0500371int
James Smart2e0fef82007-06-17 19:56:36 -0500372lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500373{
James Smart2e0fef82007-06-17 19:56:36 -0500374 struct lpfc_vport *vport = phba->pport;
James Smarta257bf92009-04-06 18:48:10 -0400375 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -0500376 LPFC_MBOXQ_t *pmb;
377 MAILBOX_t *mb;
378 struct lpfc_dmabuf *mp;
379 struct lpfc_sli *psli = &phba->sli;
380 uint32_t status, timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500381 int i, j;
382 int rc;
dea31012005-04-17 16:05:31 -0500383
James Smart7af67052007-10-27 13:38:11 -0400384 spin_lock_irq(&phba->hbalock);
385 /*
386 * If the Config port completed correctly the HBA is not
387 * over heated any more.
388 */
389 if (phba->over_temp_state == HBA_OVER_TEMP)
390 phba->over_temp_state = HBA_NORMAL_TEMP;
391 spin_unlock_irq(&phba->hbalock);
392
dea31012005-04-17 16:05:31 -0500393 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
394 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500395 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500396 return -ENOMEM;
397 }
James Smart04c68492009-05-22 14:52:52 -0400398 mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500399
dea31012005-04-17 16:05:31 -0500400 /* Get login parameters for NID. */
James Smart9f1177a2010-02-26 14:12:57 -0500401 rc = lpfc_read_sparam(phba, pmb, 0);
402 if (rc) {
403 mempool_free(pmb, phba->mbox_mem_pool);
404 return -ENOMEM;
405 }
406
James Smarted957682007-06-17 19:56:37 -0500407 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500408 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500409 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400410 "0448 Adapter failed init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500411 "READ_SPARM mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500412 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500413 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500414 mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart9f1177a2010-02-26 14:12:57 -0500415 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500416 lpfc_mbuf_free(phba, mp->virt, mp->phys);
417 kfree(mp);
418 return -EIO;
419 }
420
421 mp = (struct lpfc_dmabuf *) pmb->context1;
422
James Smart2e0fef82007-06-17 19:56:36 -0500423 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500424 lpfc_mbuf_free(phba, mp->virt, mp->phys);
425 kfree(mp);
426 pmb->context1 = NULL;
James Smart05580562011-05-24 11:40:48 -0400427 lpfc_update_vport_wwn(vport);
James Smarta257bf92009-04-06 18:48:10 -0400428
429 /* Update the fc_host data structures with new wwn. */
430 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
431 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart21e9a0a2009-05-22 14:53:21 -0400432 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smarta257bf92009-04-06 18:48:10 -0400433
dea31012005-04-17 16:05:31 -0500434 /* If no serial number in VPD data, use low 6 bytes of WWNN */
435 /* This should be consolidated into parse_vpd ? - mr */
436 if (phba->SerialNumber[0] == 0) {
437 uint8_t *outptr;
438
James Smart2e0fef82007-06-17 19:56:36 -0500439 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500440 for (i = 0; i < 12; i++) {
441 status = *outptr++;
442 j = ((status & 0xf0) >> 4);
443 if (j <= 9)
444 phba->SerialNumber[i] =
445 (char)((uint8_t) 0x30 + (uint8_t) j);
446 else
447 phba->SerialNumber[i] =
448 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
449 i++;
450 j = (status & 0xf);
451 if (j <= 9)
452 phba->SerialNumber[i] =
453 (char)((uint8_t) 0x30 + (uint8_t) j);
454 else
455 phba->SerialNumber[i] =
456 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
457 }
458 }
459
dea31012005-04-17 16:05:31 -0500460 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500461 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500462 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500463 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400464 "0453 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500465 "READ_CONFIG, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500466 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500467 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500468 mempool_free( pmb, phba->mbox_mem_pool);
469 return -EIO;
470 }
471
James Smarta0c87cb2009-07-19 10:01:10 -0400472 /* Check if the port is disabled */
473 lpfc_sli_read_link_ste(phba);
474
dea31012005-04-17 16:05:31 -0500475 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -0400476 i = (mb->un.varRdConfig.max_xri + 1);
477 if (phba->cfg_hba_queue_depth > i) {
478 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
479 "3359 HBA queue depth changed from %d to %d\n",
480 phba->cfg_hba_queue_depth, i);
481 phba->cfg_hba_queue_depth = i;
482 }
483
484 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
485 i = (mb->un.varRdConfig.max_xri >> 3);
486 if (phba->pport->cfg_lun_queue_depth > i) {
487 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
488 "3360 LUN queue depth changed from %d to %d\n",
489 phba->pport->cfg_lun_queue_depth, i);
490 phba->pport->cfg_lun_queue_depth = i;
491 }
dea31012005-04-17 16:05:31 -0500492
493 phba->lmt = mb->un.varRdConfig.lmt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500494
495 /* Get the default values for Model Name and Description */
496 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
497
James Smart2e0fef82007-06-17 19:56:36 -0500498 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500499
James Smart0b727fe2007-10-27 13:37:25 -0400500 /* Only process IOCBs on ELS ring till hba_state is READY */
James Smart7e56aa22012-08-03 12:35:34 -0400501 if (psli->ring[psli->extra_ring].sli.sli3.cmdringaddr)
James Smarta4bc3372006-12-02 13:34:16 -0500502 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
James Smart7e56aa22012-08-03 12:35:34 -0400503 if (psli->ring[psli->fcp_ring].sli.sli3.cmdringaddr)
dea31012005-04-17 16:05:31 -0500504 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
James Smart7e56aa22012-08-03 12:35:34 -0400505 if (psli->ring[psli->next_ring].sli.sli3.cmdringaddr)
dea31012005-04-17 16:05:31 -0500506 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
507
508 /* Post receive buffers for desired rings */
James Smarted957682007-06-17 19:56:37 -0500509 if (phba->sli_rev != 3)
510 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500511
James Smart93996272008-08-24 21:50:30 -0400512 /*
513 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
514 */
515 if (phba->intr_type == MSIX) {
516 rc = lpfc_config_msi(phba, pmb);
517 if (rc) {
518 mempool_free(pmb, phba->mbox_mem_pool);
519 return -EIO;
520 }
521 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
522 if (rc != MBX_SUCCESS) {
523 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
524 "0352 Config MSI mailbox command "
525 "failed, mbxCmd x%x, mbxStatus x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400526 pmb->u.mb.mbxCommand,
527 pmb->u.mb.mbxStatus);
James Smart93996272008-08-24 21:50:30 -0400528 mempool_free(pmb, phba->mbox_mem_pool);
529 return -EIO;
530 }
531 }
532
James Smart04c68492009-05-22 14:52:52 -0400533 spin_lock_irq(&phba->hbalock);
James Smart93996272008-08-24 21:50:30 -0400534 /* Initialize ERATT handling flag */
535 phba->hba_flag &= ~HBA_ERATT_HANDLED;
536
dea31012005-04-17 16:05:31 -0500537 /* Enable appropriate host interrupts */
James Smart9940b972011-03-11 16:06:12 -0500538 if (lpfc_readl(phba->HCregaddr, &status)) {
539 spin_unlock_irq(&phba->hbalock);
540 return -EIO;
541 }
dea31012005-04-17 16:05:31 -0500542 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
543 if (psli->num_rings > 0)
544 status |= HC_R0INT_ENA;
545 if (psli->num_rings > 1)
546 status |= HC_R1INT_ENA;
547 if (psli->num_rings > 2)
548 status |= HC_R2INT_ENA;
549 if (psli->num_rings > 3)
550 status |= HC_R3INT_ENA;
551
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500552 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
553 (phba->cfg_poll & DISABLE_FCP_RING_INT))
James Smart93996272008-08-24 21:50:30 -0400554 status &= ~(HC_R0INT_ENA);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500555
dea31012005-04-17 16:05:31 -0500556 writel(status, phba->HCregaddr);
557 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500558 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500559
James Smart93996272008-08-24 21:50:30 -0400560 /* Set up ring-0 (ELS) timer */
561 timeout = phba->fc_ratov * 2;
James Smart256ec0d2013-04-17 20:14:58 -0400562 mod_timer(&vport->els_tmofunc,
563 jiffies + msecs_to_jiffies(1000 * timeout));
James Smart93996272008-08-24 21:50:30 -0400564 /* Set up heart beat (HB) timer */
James Smart256ec0d2013-04-17 20:14:58 -0400565 mod_timer(&phba->hb_tmofunc,
566 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -0500567 phba->hb_outstanding = 0;
568 phba->last_completion_time = jiffies;
James Smart93996272008-08-24 21:50:30 -0400569 /* Set up error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -0400570 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -0700571 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
dea31012005-04-17 16:05:31 -0500572
James Smarta0c87cb2009-07-19 10:01:10 -0400573 if (phba->hba_flag & LINK_DISABLED) {
574 lpfc_printf_log(phba,
575 KERN_ERR, LOG_INIT,
576 "2598 Adapter Link is disabled.\n");
577 lpfc_down_link(phba, pmb);
578 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
579 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
580 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
581 lpfc_printf_log(phba,
582 KERN_ERR, LOG_INIT,
583 "2599 Adapter failed to issue DOWN_LINK"
584 " mbox command rc 0x%x\n", rc);
585
586 mempool_free(pmb, phba->mbox_mem_pool);
587 return -EIO;
588 }
James Smarte40a02c2010-02-26 14:13:54 -0500589 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart026abb82011-12-13 13:20:45 -0500590 mempool_free(pmb, phba->mbox_mem_pool);
591 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
592 if (rc)
593 return rc;
dea31012005-04-17 16:05:31 -0500594 }
595 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400596 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500597 if (!pmb) {
598 phba->link_state = LPFC_HBA_ERROR;
599 return -ENOMEM;
600 }
601
James Smart57127f12007-10-27 13:37:05 -0400602 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
603 pmb->mbox_cmpl = lpfc_config_async_cmpl;
604 pmb->vport = phba->pport;
605 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500606
James Smart57127f12007-10-27 13:37:05 -0400607 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
608 lpfc_printf_log(phba,
609 KERN_ERR,
610 LOG_INIT,
611 "0456 Adapter failed to issue "
James Smarte4e74272009-07-19 10:01:38 -0400612 "ASYNCEVT_ENABLE mbox status x%x\n",
James Smart57127f12007-10-27 13:37:05 -0400613 rc);
614 mempool_free(pmb, phba->mbox_mem_pool);
615 }
James Smart97207482008-12-04 22:39:19 -0500616
617 /* Get Option rom version */
618 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500619 if (!pmb) {
620 phba->link_state = LPFC_HBA_ERROR;
621 return -ENOMEM;
622 }
623
James Smart97207482008-12-04 22:39:19 -0500624 lpfc_dump_wakeup_param(phba, pmb);
625 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
626 pmb->vport = phba->pport;
627 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
628
629 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
630 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
James Smarte4e74272009-07-19 10:01:38 -0400631 "to get Option ROM version status x%x\n", rc);
James Smart97207482008-12-04 22:39:19 -0500632 mempool_free(pmb, phba->mbox_mem_pool);
633 }
634
James Smartd7c255b2008-08-24 21:50:00 -0400635 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400636}
637
James Smarte59058c2008-08-24 21:49:00 -0400638/**
James Smart84d1b002010-02-12 14:42:33 -0500639 * lpfc_hba_init_link - Initialize the FC link
640 * @phba: pointer to lpfc hba data structure.
James Smart6e7288d2010-06-07 15:23:35 -0400641 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500642 *
643 * This routine will issue the INIT_LINK mailbox command call.
644 * It is available to other drivers through the lpfc_hba data
645 * structure for use as a delayed link up mechanism with the
646 * module parameter lpfc_suppress_link_up.
647 *
648 * Return code
649 * 0 - success
650 * Any other value - error
651 **/
Rashika Kheriae399b222014-09-03 12:55:28 -0400652static int
James Smart6e7288d2010-06-07 15:23:35 -0400653lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500654{
James Smart1b511972011-12-13 13:23:09 -0500655 return lpfc_hba_init_link_fc_topology(phba, phba->cfg_topology, flag);
656}
657
658/**
659 * lpfc_hba_init_link_fc_topology - Initialize FC link with desired topology
660 * @phba: pointer to lpfc hba data structure.
661 * @fc_topology: desired fc topology.
662 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
663 *
664 * This routine will issue the INIT_LINK mailbox command call.
665 * It is available to other drivers through the lpfc_hba data
666 * structure for use as a delayed link up mechanism with the
667 * module parameter lpfc_suppress_link_up.
668 *
669 * Return code
670 * 0 - success
671 * Any other value - error
672 **/
673int
674lpfc_hba_init_link_fc_topology(struct lpfc_hba *phba, uint32_t fc_topology,
675 uint32_t flag)
676{
James Smart84d1b002010-02-12 14:42:33 -0500677 struct lpfc_vport *vport = phba->pport;
678 LPFC_MBOXQ_t *pmb;
679 MAILBOX_t *mb;
680 int rc;
681
682 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
683 if (!pmb) {
684 phba->link_state = LPFC_HBA_ERROR;
685 return -ENOMEM;
686 }
687 mb = &pmb->u.mb;
688 pmb->vport = vport;
689
James Smart026abb82011-12-13 13:20:45 -0500690 if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_MAX) ||
691 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G) &&
692 !(phba->lmt & LMT_1Gb)) ||
693 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G) &&
694 !(phba->lmt & LMT_2Gb)) ||
695 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G) &&
696 !(phba->lmt & LMT_4Gb)) ||
697 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G) &&
698 !(phba->lmt & LMT_8Gb)) ||
699 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G) &&
700 !(phba->lmt & LMT_10Gb)) ||
701 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G) &&
James Smartd38dd522015-08-31 16:48:17 -0400702 !(phba->lmt & LMT_16Gb)) ||
703 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_32G) &&
704 !(phba->lmt & LMT_32Gb))) {
James Smart026abb82011-12-13 13:20:45 -0500705 /* Reset link speed to auto */
706 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
707 "1302 Invalid speed for this board:%d "
708 "Reset link speed to auto.\n",
709 phba->cfg_link_speed);
710 phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
711 }
James Smart1b511972011-12-13 13:23:09 -0500712 lpfc_init_link(phba, pmb, fc_topology, phba->cfg_link_speed);
James Smart84d1b002010-02-12 14:42:33 -0500713 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart1b511972011-12-13 13:23:09 -0500714 if (phba->sli_rev < LPFC_SLI_REV4)
715 lpfc_set_loopback_flag(phba);
James Smart6e7288d2010-06-07 15:23:35 -0400716 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart76a95d72010-11-20 23:11:48 -0500717 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
James Smart84d1b002010-02-12 14:42:33 -0500718 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
719 "0498 Adapter failed to init, mbxCmd x%x "
720 "INIT_LINK, mbxStatus x%x\n",
721 mb->mbxCommand, mb->mbxStatus);
James Smart76a95d72010-11-20 23:11:48 -0500722 if (phba->sli_rev <= LPFC_SLI_REV3) {
723 /* Clear all interrupt enable conditions */
724 writel(0, phba->HCregaddr);
725 readl(phba->HCregaddr); /* flush */
726 /* Clear all pending interrupts */
727 writel(0xffffffff, phba->HAregaddr);
728 readl(phba->HAregaddr); /* flush */
729 }
James Smart84d1b002010-02-12 14:42:33 -0500730 phba->link_state = LPFC_HBA_ERROR;
James Smart6e7288d2010-06-07 15:23:35 -0400731 if (rc != MBX_BUSY || flag == MBX_POLL)
James Smart84d1b002010-02-12 14:42:33 -0500732 mempool_free(pmb, phba->mbox_mem_pool);
733 return -EIO;
734 }
James Smarte40a02c2010-02-26 14:13:54 -0500735 phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
James Smart6e7288d2010-06-07 15:23:35 -0400736 if (flag == MBX_POLL)
737 mempool_free(pmb, phba->mbox_mem_pool);
James Smart84d1b002010-02-12 14:42:33 -0500738
739 return 0;
740}
741
742/**
743 * lpfc_hba_down_link - this routine downs the FC link
James Smart6e7288d2010-06-07 15:23:35 -0400744 * @phba: pointer to lpfc hba data structure.
745 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500746 *
747 * This routine will issue the DOWN_LINK mailbox command call.
748 * It is available to other drivers through the lpfc_hba data
749 * structure for use to stop the link.
750 *
751 * Return code
752 * 0 - success
753 * Any other value - error
754 **/
Rashika Kheriae399b222014-09-03 12:55:28 -0400755static int
James Smart6e7288d2010-06-07 15:23:35 -0400756lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500757{
758 LPFC_MBOXQ_t *pmb;
759 int rc;
760
761 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
762 if (!pmb) {
763 phba->link_state = LPFC_HBA_ERROR;
764 return -ENOMEM;
765 }
766
767 lpfc_printf_log(phba,
768 KERN_ERR, LOG_INIT,
769 "0491 Adapter Link is disabled.\n");
770 lpfc_down_link(phba, pmb);
771 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6e7288d2010-06-07 15:23:35 -0400772 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart84d1b002010-02-12 14:42:33 -0500773 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
774 lpfc_printf_log(phba,
775 KERN_ERR, LOG_INIT,
776 "2522 Adapter failed to issue DOWN_LINK"
777 " mbox command rc 0x%x\n", rc);
778
779 mempool_free(pmb, phba->mbox_mem_pool);
780 return -EIO;
781 }
James Smart6e7288d2010-06-07 15:23:35 -0400782 if (flag == MBX_POLL)
783 mempool_free(pmb, phba->mbox_mem_pool);
784
James Smart84d1b002010-02-12 14:42:33 -0500785 return 0;
786}
787
788/**
James Smart3621a712009-04-06 18:47:14 -0400789 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400790 * @phba: pointer to lpfc HBA data structure.
791 *
792 * This routine will do LPFC uninitialization before the HBA is reset when
793 * bringing down the SLI Layer.
794 *
795 * Return codes
796 * 0 - success.
797 * Any other value - error.
798 **/
dea31012005-04-17 16:05:31 -0500799int
James Smart2e0fef82007-06-17 19:56:36 -0500800lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500801{
James Smart1b32f6a2008-02-08 18:49:39 -0500802 struct lpfc_vport **vports;
803 int i;
James Smart3772a992009-05-22 14:50:54 -0400804
805 if (phba->sli_rev <= LPFC_SLI_REV3) {
806 /* Disable interrupts */
807 writel(0, phba->HCregaddr);
808 readl(phba->HCregaddr); /* flush */
809 }
dea31012005-04-17 16:05:31 -0500810
James Smart1b32f6a2008-02-08 18:49:39 -0500811 if (phba->pport->load_flag & FC_UNLOADING)
812 lpfc_cleanup_discovery_resources(phba->pport);
813 else {
814 vports = lpfc_create_vport_work_array(phba);
815 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400816 for (i = 0; i <= phba->max_vports &&
817 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500818 lpfc_cleanup_discovery_resources(vports[i]);
819 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500820 }
821 return 0;
dea31012005-04-17 16:05:31 -0500822}
823
James Smarte59058c2008-08-24 21:49:00 -0400824/**
James Smart68e814f2014-05-21 08:04:59 -0400825 * lpfc_sli4_free_sp_events - Cleanup sp_queue_events to free
826 * rspiocb which got deferred
827 *
828 * @phba: pointer to lpfc HBA data structure.
829 *
830 * This routine will cleanup completed slow path events after HBA is reset
831 * when bringing down the SLI Layer.
832 *
833 *
834 * Return codes
835 * void.
836 **/
837static void
838lpfc_sli4_free_sp_events(struct lpfc_hba *phba)
839{
840 struct lpfc_iocbq *rspiocbq;
841 struct hbq_dmabuf *dmabuf;
842 struct lpfc_cq_event *cq_event;
843
844 spin_lock_irq(&phba->hbalock);
845 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
846 spin_unlock_irq(&phba->hbalock);
847
848 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
849 /* Get the response iocb from the head of work queue */
850 spin_lock_irq(&phba->hbalock);
851 list_remove_head(&phba->sli4_hba.sp_queue_event,
852 cq_event, struct lpfc_cq_event, list);
853 spin_unlock_irq(&phba->hbalock);
854
855 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
856 case CQE_CODE_COMPL_WQE:
857 rspiocbq = container_of(cq_event, struct lpfc_iocbq,
858 cq_event);
859 lpfc_sli_release_iocbq(phba, rspiocbq);
860 break;
861 case CQE_CODE_RECEIVE:
862 case CQE_CODE_RECEIVE_V1:
863 dmabuf = container_of(cq_event, struct hbq_dmabuf,
864 cq_event);
865 lpfc_in_buf_free(phba, &dmabuf->dbuf);
866 }
867 }
868}
869
870/**
James Smartbcece5f2014-04-04 13:51:34 -0400871 * lpfc_hba_free_post_buf - Perform lpfc uninitialization after HBA reset
872 * @phba: pointer to lpfc HBA data structure.
873 *
874 * This routine will cleanup posted ELS buffers after the HBA is reset
875 * when bringing down the SLI Layer.
876 *
877 *
878 * Return codes
879 * void.
880 **/
881static void
882lpfc_hba_free_post_buf(struct lpfc_hba *phba)
883{
884 struct lpfc_sli *psli = &phba->sli;
885 struct lpfc_sli_ring *pring;
886 struct lpfc_dmabuf *mp, *next_mp;
James Smart07eab622014-04-04 13:51:44 -0400887 LIST_HEAD(buflist);
888 int count;
James Smartbcece5f2014-04-04 13:51:34 -0400889
890 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
891 lpfc_sli_hbqbuf_free_all(phba);
892 else {
893 /* Cleanup preposted buffers on the ELS ring */
James Smartbcece5f2014-04-04 13:51:34 -0400894 pring = &psli->ring[LPFC_ELS_RING];
James Smart07eab622014-04-04 13:51:44 -0400895 spin_lock_irq(&phba->hbalock);
896 list_splice_init(&pring->postbufq, &buflist);
897 spin_unlock_irq(&phba->hbalock);
898
899 count = 0;
900 list_for_each_entry_safe(mp, next_mp, &buflist, list) {
James Smartbcece5f2014-04-04 13:51:34 -0400901 list_del(&mp->list);
James Smart07eab622014-04-04 13:51:44 -0400902 count++;
James Smartbcece5f2014-04-04 13:51:34 -0400903 lpfc_mbuf_free(phba, mp->virt, mp->phys);
904 kfree(mp);
905 }
James Smart07eab622014-04-04 13:51:44 -0400906
907 spin_lock_irq(&phba->hbalock);
908 pring->postbufq_cnt -= count;
James Smartbcece5f2014-04-04 13:51:34 -0400909 spin_unlock_irq(&phba->hbalock);
910 }
911}
912
913/**
914 * lpfc_hba_clean_txcmplq - Perform lpfc uninitialization after HBA reset
915 * @phba: pointer to lpfc HBA data structure.
916 *
917 * This routine will cleanup the txcmplq after the HBA is reset when bringing
918 * down the SLI Layer.
919 *
920 * Return codes
921 * void
922 **/
923static void
924lpfc_hba_clean_txcmplq(struct lpfc_hba *phba)
925{
926 struct lpfc_sli *psli = &phba->sli;
927 struct lpfc_sli_ring *pring;
928 LIST_HEAD(completions);
929 int i;
930
James Smartbcece5f2014-04-04 13:51:34 -0400931 for (i = 0; i < psli->num_rings; i++) {
932 pring = &psli->ring[i];
933 if (phba->sli_rev >= LPFC_SLI_REV4)
934 spin_lock_irq(&pring->ring_lock);
935 else
936 spin_lock_irq(&phba->hbalock);
937 /* At this point in time the HBA is either reset or DOA. Either
938 * way, nothing should be on txcmplq as it will NEVER complete.
939 */
940 list_splice_init(&pring->txcmplq, &completions);
James Smartdb55fba2014-04-04 13:52:02 -0400941 pring->txcmplq_cnt = 0;
James Smartbcece5f2014-04-04 13:51:34 -0400942
943 if (phba->sli_rev >= LPFC_SLI_REV4)
944 spin_unlock_irq(&pring->ring_lock);
945 else
946 spin_unlock_irq(&phba->hbalock);
947
948 /* Cancel all the IOCBs from the completions list */
949 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
950 IOERR_SLI_ABORTED);
951 lpfc_sli_abort_iocb_ring(phba, pring);
952 }
953}
954
955/**
James Smart3772a992009-05-22 14:50:54 -0400956 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smartbcece5f2014-04-04 13:51:34 -0400957 int i;
James Smarte59058c2008-08-24 21:49:00 -0400958 * @phba: pointer to lpfc HBA data structure.
959 *
960 * This routine will do uninitialization after the HBA is reset when bring
961 * down the SLI Layer.
962 *
963 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200964 * 0 - success.
James Smarte59058c2008-08-24 21:49:00 -0400965 * Any other value - error.
966 **/
James Smart3772a992009-05-22 14:50:54 -0400967static int
968lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -0500969{
James Smartbcece5f2014-04-04 13:51:34 -0400970 lpfc_hba_free_post_buf(phba);
971 lpfc_hba_clean_txcmplq(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500972 return 0;
973}
James Smart5af5eee2010-10-22 11:06:38 -0400974
James Smartda0436e2009-05-22 14:51:39 -0400975/**
976 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
977 * @phba: pointer to lpfc HBA data structure.
978 *
979 * This routine will do uninitialization after the HBA is reset when bring
980 * down the SLI Layer.
981 *
982 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200983 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400984 * Any other value - error.
985 **/
986static int
987lpfc_hba_down_post_s4(struct lpfc_hba *phba)
988{
989 struct lpfc_scsi_buf *psb, *psb_next;
990 LIST_HEAD(aborts);
James Smartda0436e2009-05-22 14:51:39 -0400991 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500992 struct lpfc_sglq *sglq_entry = NULL;
James Smartdafe8ce2014-09-03 12:56:40 -0400993 struct lpfc_sli *psli = &phba->sli;
994 struct lpfc_sli_ring *pring;
James Smart0f65ff62010-02-26 14:14:23 -0500995
James Smartbcece5f2014-04-04 13:51:34 -0400996 lpfc_hba_free_post_buf(phba);
997 lpfc_hba_clean_txcmplq(phba);
James Smartdafe8ce2014-09-03 12:56:40 -0400998 pring = &psli->ring[LPFC_ELS_RING];
James Smartbcece5f2014-04-04 13:51:34 -0400999
James Smartda0436e2009-05-22 14:51:39 -04001000 /* At this point in time the HBA is either reset or DOA. Either
1001 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
1002 * on the lpfc_sgl_list so that it can either be freed if the
1003 * driver is unloading or reposted if the driver is restarting
1004 * the port.
1005 */
1006 spin_lock_irq(&phba->hbalock); /* required for lpfc_sgl_list and */
1007 /* scsl_buf_list */
1008 /* abts_sgl_list_lock required because worker thread uses this
1009 * list.
1010 */
1011 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -05001012 list_for_each_entry(sglq_entry,
1013 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
1014 sglq_entry->state = SGL_FREED;
1015
James Smartdafe8ce2014-09-03 12:56:40 -04001016 spin_lock(&pring->ring_lock);
James Smartda0436e2009-05-22 14:51:39 -04001017 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
1018 &phba->sli4_hba.lpfc_sgl_list);
James Smartdafe8ce2014-09-03 12:56:40 -04001019 spin_unlock(&pring->ring_lock);
James Smartda0436e2009-05-22 14:51:39 -04001020 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
1021 /* abts_scsi_buf_list_lock required because worker thread uses this
1022 * list.
1023 */
1024 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
1025 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
1026 &aborts);
1027 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
1028 spin_unlock_irq(&phba->hbalock);
1029
1030 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
1031 psb->pCmd = NULL;
1032 psb->status = IOSTAT_SUCCESS;
1033 }
James Smarta40fc5f2013-04-17 20:17:40 -04001034 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
1035 list_splice(&aborts, &phba->lpfc_scsi_buf_list_put);
1036 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
James Smart68e814f2014-05-21 08:04:59 -04001037
1038 lpfc_sli4_free_sp_events(phba);
James Smartda0436e2009-05-22 14:51:39 -04001039 return 0;
1040}
1041
1042/**
1043 * lpfc_hba_down_post - Wrapper func for hba down post routine
1044 * @phba: pointer to lpfc HBA data structure.
1045 *
1046 * This routine wraps the actual SLI3 or SLI4 routine for performing
1047 * uninitialization after the HBA is reset when bring down the SLI Layer.
1048 *
1049 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001050 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001051 * Any other value - error.
1052 **/
1053int
1054lpfc_hba_down_post(struct lpfc_hba *phba)
1055{
1056 return (*phba->lpfc_hba_down_post)(phba);
1057}
Jamie Wellnitz41415862006-02-28 19:25:27 -05001058
James Smarte59058c2008-08-24 21:49:00 -04001059/**
James Smart3621a712009-04-06 18:47:14 -04001060 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001061 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1062 *
1063 * This is the HBA-timer timeout handler registered to the lpfc driver. When
1064 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
1065 * work-port-events bitmap and the worker thread is notified. This timeout
1066 * event will be used by the worker thread to invoke the actual timeout
1067 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
1068 * be performed in the timeout handler and the HBA timeout event bit shall
1069 * be cleared by the worker thread after it has taken the event bitmap out.
1070 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001071static void
James Smart858c9f62007-06-17 19:56:39 -05001072lpfc_hb_timeout(unsigned long ptr)
1073{
1074 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -04001075 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -05001076 unsigned long iflag;
1077
1078 phba = (struct lpfc_hba *)ptr;
James Smart93996272008-08-24 21:50:30 -04001079
1080 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -05001081 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04001082 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
1083 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -05001084 phba->pport->work_port_events |= WORKER_HB_TMO;
1085 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
1086
James Smart93996272008-08-24 21:50:30 -04001087 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -04001088 if (!tmo_posted)
1089 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -05001090 return;
1091}
1092
James Smarte59058c2008-08-24 21:49:00 -04001093/**
James Smart19ca7602010-11-20 23:11:55 -05001094 * lpfc_rrq_timeout - The RRQ-timer timeout handler
1095 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1096 *
1097 * This is the RRQ-timer timeout handler registered to the lpfc driver. When
1098 * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
1099 * work-port-events bitmap and the worker thread is notified. This timeout
1100 * event will be used by the worker thread to invoke the actual timeout
1101 * handler routine, lpfc_rrq_handler. Any periodical operations will
1102 * be performed in the timeout handler and the RRQ timeout event bit shall
1103 * be cleared by the worker thread after it has taken the event bitmap out.
1104 **/
1105static void
1106lpfc_rrq_timeout(unsigned long ptr)
1107{
1108 struct lpfc_hba *phba;
James Smart19ca7602010-11-20 23:11:55 -05001109 unsigned long iflag;
1110
1111 phba = (struct lpfc_hba *)ptr;
1112 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001113 if (!(phba->pport->load_flag & FC_UNLOADING))
1114 phba->hba_flag |= HBA_RRQ_ACTIVE;
1115 else
1116 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart19ca7602010-11-20 23:11:55 -05001117 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001118
1119 if (!(phba->pport->load_flag & FC_UNLOADING))
1120 lpfc_worker_wake_up(phba);
James Smart19ca7602010-11-20 23:11:55 -05001121}
1122
1123/**
James Smart3621a712009-04-06 18:47:14 -04001124 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -04001125 * @phba: pointer to lpfc hba data structure.
1126 * @pmboxq: pointer to the driver internal queue element for mailbox command.
1127 *
1128 * This is the callback function to the lpfc heart-beat mailbox command.
1129 * If configured, the lpfc driver issues the heart-beat mailbox command to
1130 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
1131 * heart-beat mailbox command is issued, the driver shall set up heart-beat
1132 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
1133 * heart-beat outstanding state. Once the mailbox command comes back and
1134 * no error conditions detected, the heart-beat mailbox command timer is
1135 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1136 * state is cleared for the next heart-beat. If the timer expired with the
1137 * heart-beat outstanding state set, the driver will put the HBA offline.
1138 **/
James Smart858c9f62007-06-17 19:56:39 -05001139static void
1140lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1141{
1142 unsigned long drvr_flag;
1143
1144 spin_lock_irqsave(&phba->hbalock, drvr_flag);
1145 phba->hb_outstanding = 0;
1146 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1147
James Smart93996272008-08-24 21:50:30 -04001148 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -05001149 mempool_free(pmboxq, phba->mbox_mem_pool);
1150 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1151 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -04001152 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -05001153 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001154 jiffies +
1155 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001156 return;
1157}
1158
James Smarte59058c2008-08-24 21:49:00 -04001159/**
James Smart3621a712009-04-06 18:47:14 -04001160 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001161 * @phba: pointer to lpfc hba data structure.
1162 *
1163 * This is the actual HBA-timer timeout handler to be invoked by the worker
1164 * thread whenever the HBA timer fired and HBA-timeout event posted. This
1165 * handler performs any periodic operations needed for the device. If such
1166 * periodic event has already been attended to either in the interrupt handler
1167 * or by processing slow-ring or fast-ring events within the HBA-timer
1168 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1169 * the timer for the next timeout period. If lpfc heart-beat mailbox command
1170 * is configured and there is no heart-beat mailbox command outstanding, a
1171 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1172 * has been a heart-beat mailbox command outstanding, the HBA shall be put
1173 * to offline.
1174 **/
James Smart858c9f62007-06-17 19:56:39 -05001175void
1176lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1177{
James Smart45ed1192009-10-02 15:17:02 -04001178 struct lpfc_vport **vports;
James Smart858c9f62007-06-17 19:56:39 -05001179 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -05001180 struct lpfc_dmabuf *buf_ptr;
James Smart45ed1192009-10-02 15:17:02 -04001181 int retval, i;
James Smart858c9f62007-06-17 19:56:39 -05001182 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -05001183 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -05001184
James Smart45ed1192009-10-02 15:17:02 -04001185 vports = lpfc_create_vport_work_array(phba);
1186 if (vports != NULL)
James Smart4258e982015-12-16 18:11:58 -05001187 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart45ed1192009-10-02 15:17:02 -04001188 lpfc_rcv_seq_check_edtov(vports[i]);
James Smart4258e982015-12-16 18:11:58 -05001189 lpfc_fdmi_num_disc_check(vports[i]);
1190 }
James Smart45ed1192009-10-02 15:17:02 -04001191 lpfc_destroy_vport_work_array(phba, vports);
1192
James Smart858c9f62007-06-17 19:56:39 -05001193 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -04001194 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -05001195 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1196 return;
1197
1198 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -05001199
James Smart256ec0d2013-04-17 20:14:58 -04001200 if (time_after(phba->last_completion_time +
1201 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL),
1202 jiffies)) {
James Smart858c9f62007-06-17 19:56:39 -05001203 spin_unlock_irq(&phba->pport->work_port_lock);
1204 if (!phba->hb_outstanding)
1205 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001206 jiffies +
1207 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001208 else
1209 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001210 jiffies +
1211 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001212 return;
1213 }
1214 spin_unlock_irq(&phba->pport->work_port_lock);
1215
James Smart0ff10d42008-01-11 01:52:36 -05001216 if (phba->elsbuf_cnt &&
1217 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1218 spin_lock_irq(&phba->hbalock);
1219 list_splice_init(&phba->elsbuf, &completions);
1220 phba->elsbuf_cnt = 0;
1221 phba->elsbuf_prev_cnt = 0;
1222 spin_unlock_irq(&phba->hbalock);
1223
1224 while (!list_empty(&completions)) {
1225 list_remove_head(&completions, buf_ptr,
1226 struct lpfc_dmabuf, list);
1227 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1228 kfree(buf_ptr);
1229 }
1230 }
1231 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1232
James Smart858c9f62007-06-17 19:56:39 -05001233 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -05001234 if (phba->cfg_enable_hba_heartbeat) {
1235 if (!phba->hb_outstanding) {
James Smartbc739052010-08-04 16:11:18 -04001236 if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1237 (list_empty(&psli->mboxq))) {
1238 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1239 GFP_KERNEL);
1240 if (!pmboxq) {
1241 mod_timer(&phba->hb_tmofunc,
1242 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001243 msecs_to_jiffies(1000 *
1244 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001245 return;
1246 }
James Smart13815c82008-01-11 01:52:48 -05001247
James Smartbc739052010-08-04 16:11:18 -04001248 lpfc_heart_beat(phba, pmboxq);
1249 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1250 pmboxq->vport = phba->pport;
1251 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1252 MBX_NOWAIT);
James Smart13815c82008-01-11 01:52:48 -05001253
James Smartbc739052010-08-04 16:11:18 -04001254 if (retval != MBX_BUSY &&
1255 retval != MBX_SUCCESS) {
1256 mempool_free(pmboxq,
1257 phba->mbox_mem_pool);
1258 mod_timer(&phba->hb_tmofunc,
1259 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001260 msecs_to_jiffies(1000 *
1261 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001262 return;
1263 }
1264 phba->skipped_hb = 0;
1265 phba->hb_outstanding = 1;
1266 } else if (time_before_eq(phba->last_completion_time,
1267 phba->skipped_hb)) {
1268 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1269 "2857 Last completion time not "
1270 " updated in %d ms\n",
1271 jiffies_to_msecs(jiffies
1272 - phba->last_completion_time));
1273 } else
1274 phba->skipped_hb = jiffies;
1275
James Smart858c9f62007-06-17 19:56:39 -05001276 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001277 jiffies +
1278 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001279 return;
James Smart13815c82008-01-11 01:52:48 -05001280 } else {
1281 /*
1282 * If heart beat timeout called with hb_outstanding set
James Smartdcf2a4e2010-09-29 11:18:53 -04001283 * we need to give the hb mailbox cmd a chance to
1284 * complete or TMO.
James Smart13815c82008-01-11 01:52:48 -05001285 */
James Smartdcf2a4e2010-09-29 11:18:53 -04001286 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1287 "0459 Adapter heartbeat still out"
1288 "standing:last compl time was %d ms.\n",
1289 jiffies_to_msecs(jiffies
1290 - phba->last_completion_time));
1291 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001292 jiffies +
1293 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001294 }
James Smart4258e982015-12-16 18:11:58 -05001295 } else {
1296 mod_timer(&phba->hb_tmofunc,
1297 jiffies +
1298 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001299 }
1300}
1301
James Smarte59058c2008-08-24 21:49:00 -04001302/**
James Smart3621a712009-04-06 18:47:14 -04001303 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -04001304 * @phba: pointer to lpfc hba data structure.
1305 *
1306 * This routine is called to bring the HBA offline when HBA hardware error
1307 * other than Port Error 6 has been detected.
1308 **/
James Smart09372822008-01-11 01:52:54 -05001309static void
1310lpfc_offline_eratt(struct lpfc_hba *phba)
1311{
1312 struct lpfc_sli *psli = &phba->sli;
1313
1314 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001315 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -05001316 spin_unlock_irq(&phba->hbalock);
James Smart618a5232012-06-12 13:54:36 -04001317 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart09372822008-01-11 01:52:54 -05001318
1319 lpfc_offline(phba);
1320 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001321 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001322 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001323 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001324 lpfc_hba_down_post(phba);
1325 lpfc_sli_brdready(phba, HS_MBRDY);
1326 lpfc_unblock_mgmt_io(phba);
1327 phba->link_state = LPFC_HBA_ERROR;
1328 return;
1329}
1330
James Smarte59058c2008-08-24 21:49:00 -04001331/**
James Smartda0436e2009-05-22 14:51:39 -04001332 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1333 * @phba: pointer to lpfc hba data structure.
1334 *
1335 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1336 * other than Port Error 6 has been detected.
1337 **/
James Smarta88dbb62013-04-17 20:18:39 -04001338void
James Smartda0436e2009-05-22 14:51:39 -04001339lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1340{
James Smart946727d2015-04-07 15:07:09 -04001341 spin_lock_irq(&phba->hbalock);
1342 phba->link_state = LPFC_HBA_ERROR;
1343 spin_unlock_irq(&phba->hbalock);
1344
James Smart618a5232012-06-12 13:54:36 -04001345 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smartda0436e2009-05-22 14:51:39 -04001346 lpfc_offline(phba);
James Smartda0436e2009-05-22 14:51:39 -04001347 lpfc_hba_down_post(phba);
James Smartda0436e2009-05-22 14:51:39 -04001348 lpfc_unblock_mgmt_io(phba);
James Smartda0436e2009-05-22 14:51:39 -04001349}
1350
1351/**
James Smarta257bf92009-04-06 18:48:10 -04001352 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1353 * @phba: pointer to lpfc hba data structure.
1354 *
1355 * This routine is invoked to handle the deferred HBA hardware error
1356 * conditions. This type of error is indicated by HBA by setting ER1
1357 * and another ER bit in the host status register. The driver will
1358 * wait until the ER1 bit clears before handling the error condition.
1359 **/
1360static void
1361lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1362{
1363 uint32_t old_host_status = phba->work_hs;
James Smarta257bf92009-04-06 18:48:10 -04001364 struct lpfc_sli *psli = &phba->sli;
1365
James Smartf4b4c682009-05-22 14:53:12 -04001366 /* If the pci channel is offline, ignore possible errors,
1367 * since we cannot communicate with the pci card anyway.
1368 */
1369 if (pci_channel_offline(phba->pcidev)) {
1370 spin_lock_irq(&phba->hbalock);
1371 phba->hba_flag &= ~DEFER_ERATT;
1372 spin_unlock_irq(&phba->hbalock);
1373 return;
1374 }
1375
James Smarta257bf92009-04-06 18:48:10 -04001376 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1377 "0479 Deferred Adapter Hardware Error "
1378 "Data: x%x x%x x%x\n",
1379 phba->work_hs,
1380 phba->work_status[0], phba->work_status[1]);
1381
1382 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001383 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001384 spin_unlock_irq(&phba->hbalock);
1385
1386
1387 /*
1388 * Firmware stops when it triggred erratt. That could cause the I/Os
1389 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1390 * SCSI layer retry it after re-establishing link.
1391 */
James Smartdb55fba2014-04-04 13:52:02 -04001392 lpfc_sli_abort_fcp_rings(phba);
James Smarta257bf92009-04-06 18:48:10 -04001393
1394 /*
1395 * There was a firmware error. Take the hba offline and then
1396 * attempt to restart it.
1397 */
James Smart618a5232012-06-12 13:54:36 -04001398 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smarta257bf92009-04-06 18:48:10 -04001399 lpfc_offline(phba);
1400
1401 /* Wait for the ER1 bit to clear.*/
1402 while (phba->work_hs & HS_FFER1) {
1403 msleep(100);
James Smart9940b972011-03-11 16:06:12 -05001404 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1405 phba->work_hs = UNPLUG_ERR ;
1406 break;
1407 }
James Smarta257bf92009-04-06 18:48:10 -04001408 /* If driver is unloading let the worker thread continue */
1409 if (phba->pport->load_flag & FC_UNLOADING) {
1410 phba->work_hs = 0;
1411 break;
1412 }
1413 }
1414
1415 /*
1416 * This is to ptrotect against a race condition in which
1417 * first write to the host attention register clear the
1418 * host status register.
1419 */
1420 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1421 phba->work_hs = old_host_status & ~HS_FFER1;
1422
James Smart3772a992009-05-22 14:50:54 -04001423 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001424 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001425 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001426 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1427 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1428}
1429
James Smart3772a992009-05-22 14:50:54 -04001430static void
1431lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1432{
1433 struct lpfc_board_event_header board_event;
1434 struct Scsi_Host *shost;
1435
1436 board_event.event_type = FC_REG_BOARD_EVENT;
1437 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1438 shost = lpfc_shost_from_vport(phba->pport);
1439 fc_host_post_vendor_event(shost, fc_get_event_number(),
1440 sizeof(board_event),
1441 (char *) &board_event,
1442 LPFC_NL_VENDOR_ID);
1443}
1444
James Smarta257bf92009-04-06 18:48:10 -04001445/**
James Smart3772a992009-05-22 14:50:54 -04001446 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001447 * @phba: pointer to lpfc hba data structure.
1448 *
1449 * This routine is invoked to handle the following HBA hardware error
1450 * conditions:
1451 * 1 - HBA error attention interrupt
1452 * 2 - DMA ring index out of range
1453 * 3 - Mailbox command came back as unknown
1454 **/
James Smart3772a992009-05-22 14:50:54 -04001455static void
1456lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001457{
James Smart2e0fef82007-06-17 19:56:36 -05001458 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001459 struct lpfc_sli *psli = &phba->sli;
James Smartd2873e42006-08-18 17:46:43 -04001460 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001461 unsigned long temperature;
1462 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001463 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001464
Linas Vepstas8d63f372007-02-14 14:28:36 -06001465 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001466 * since we cannot communicate with the pci card anyway.
1467 */
1468 if (pci_channel_offline(phba->pcidev)) {
1469 spin_lock_irq(&phba->hbalock);
1470 phba->hba_flag &= ~DEFER_ERATT;
1471 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001472 return;
James Smart3772a992009-05-22 14:50:54 -04001473 }
1474
James Smart13815c82008-01-11 01:52:48 -05001475 /* If resets are disabled then leave the HBA alone and return */
1476 if (!phba->cfg_enable_hba_reset)
1477 return;
dea31012005-04-17 16:05:31 -05001478
James Smartea2151b2008-09-07 11:52:10 -04001479 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001480 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001481
James Smarta257bf92009-04-06 18:48:10 -04001482 if (phba->hba_flag & DEFER_ERATT)
1483 lpfc_handle_deferred_eratt(phba);
1484
James Smartdcf2a4e2010-09-29 11:18:53 -04001485 if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1486 if (phba->work_hs & HS_FFER6)
1487 /* Re-establishing Link */
1488 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1489 "1301 Re-establishing Link "
1490 "Data: x%x x%x x%x\n",
1491 phba->work_hs, phba->work_status[0],
1492 phba->work_status[1]);
1493 if (phba->work_hs & HS_FFER8)
1494 /* Device Zeroization */
1495 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1496 "2861 Host Authentication device "
1497 "zeroization Data:x%x x%x x%x\n",
1498 phba->work_hs, phba->work_status[0],
1499 phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001500
James Smart92d7f7b2007-06-17 19:56:38 -05001501 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001502 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001503 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001504
1505 /*
1506 * Firmware stops when it triggled erratt with HS_FFER6.
1507 * That could cause the I/Os dropped by the firmware.
1508 * Error iocb (I/O) on txcmplq and let the SCSI layer
1509 * retry it after re-establishing link.
1510 */
James Smartdb55fba2014-04-04 13:52:02 -04001511 lpfc_sli_abort_fcp_rings(phba);
dea31012005-04-17 16:05:31 -05001512
dea31012005-04-17 16:05:31 -05001513 /*
1514 * There was a firmware error. Take the hba offline and then
1515 * attempt to restart it.
1516 */
James Smart618a5232012-06-12 13:54:36 -04001517 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
dea31012005-04-17 16:05:31 -05001518 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001519 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001520 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001521 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001522 return;
1523 }
James Smart46fa3112007-04-25 09:51:45 -04001524 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001525 } else if (phba->work_hs & HS_CRIT_TEMP) {
1526 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1527 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1528 temp_event_data.event_code = LPFC_CRIT_TEMP;
1529 temp_event_data.data = (uint32_t)temperature;
1530
1531 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001532 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001533 "(%ld), taking this port offline "
1534 "Data: x%x x%x x%x\n",
1535 temperature, phba->work_hs,
1536 phba->work_status[0], phba->work_status[1]);
1537
1538 shost = lpfc_shost_from_vport(phba->pport);
1539 fc_host_post_vendor_event(shost, fc_get_event_number(),
1540 sizeof(temp_event_data),
1541 (char *) &temp_event_data,
1542 SCSI_NL_VID_TYPE_PCI
1543 | PCI_VENDOR_ID_EMULEX);
1544
James Smart7af67052007-10-27 13:38:11 -04001545 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001546 phba->over_temp_state = HBA_OVER_TEMP;
1547 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001548 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001549
dea31012005-04-17 16:05:31 -05001550 } else {
1551 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001552 * failure is a value other than FFER6. Do not call the offline
1553 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001554 */
1555 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001556 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001557 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001558 phba->work_hs,
dea31012005-04-17 16:05:31 -05001559 phba->work_status[0], phba->work_status[1]);
1560
James Smartd2873e42006-08-18 17:46:43 -04001561 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001562 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001563 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001564 sizeof(event_data), (char *) &event_data,
1565 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1566
James Smart09372822008-01-11 01:52:54 -05001567 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001568 }
James Smart93996272008-08-24 21:50:30 -04001569 return;
dea31012005-04-17 16:05:31 -05001570}
1571
James Smarte59058c2008-08-24 21:49:00 -04001572/**
James Smart618a5232012-06-12 13:54:36 -04001573 * lpfc_sli4_port_sta_fn_reset - The SLI4 function reset due to port status reg
1574 * @phba: pointer to lpfc hba data structure.
1575 * @mbx_action: flag for mailbox shutdown action.
1576 *
1577 * This routine is invoked to perform an SLI4 port PCI function reset in
1578 * response to port status register polling attention. It waits for port
1579 * status register (ERR, RDY, RN) bits before proceeding with function reset.
1580 * During this process, interrupt vectors are freed and later requested
1581 * for handling possible port resource change.
1582 **/
1583static int
James Smarte10b2022014-02-20 09:57:43 -05001584lpfc_sli4_port_sta_fn_reset(struct lpfc_hba *phba, int mbx_action,
1585 bool en_rn_msg)
James Smart618a5232012-06-12 13:54:36 -04001586{
1587 int rc;
1588 uint32_t intr_mode;
1589
James Smart65791f12016-07-06 12:35:56 -07001590 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
1591 LPFC_SLI_INTF_IF_TYPE_2) {
1592 /*
1593 * On error status condition, driver need to wait for port
1594 * ready before performing reset.
1595 */
1596 rc = lpfc_sli4_pdev_status_reg_wait(phba);
1597 if (!rc)
1598 return rc;
James Smart618a5232012-06-12 13:54:36 -04001599 }
James Smart65791f12016-07-06 12:35:56 -07001600 /* need reset: attempt for port recovery */
1601 if (en_rn_msg)
1602 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1603 "2887 Reset Needed: Attempting Port "
1604 "Recovery...\n");
1605 lpfc_offline_prep(phba, mbx_action);
1606 lpfc_offline(phba);
1607 /* release interrupt for possible resource change */
1608 lpfc_sli4_disable_intr(phba);
1609 lpfc_sli_brdrestart(phba);
1610 /* request and enable interrupt */
1611 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
1612 if (intr_mode == LPFC_INTR_ERROR) {
1613 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1614 "3175 Failed to enable interrupt\n");
1615 return -EIO;
1616 }
1617 phba->intr_mode = intr_mode;
1618 rc = lpfc_online(phba);
1619 if (rc == 0)
1620 lpfc_unblock_mgmt_io(phba);
1621
James Smart618a5232012-06-12 13:54:36 -04001622 return rc;
1623}
1624
1625/**
James Smartda0436e2009-05-22 14:51:39 -04001626 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1627 * @phba: pointer to lpfc hba data structure.
1628 *
1629 * This routine is invoked to handle the SLI4 HBA hardware error attention
1630 * conditions.
1631 **/
1632static void
1633lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1634{
1635 struct lpfc_vport *vport = phba->pport;
1636 uint32_t event_data;
1637 struct Scsi_Host *shost;
James Smart2fcee4b2010-12-15 17:57:46 -05001638 uint32_t if_type;
James Smart2e90f4b2011-12-13 13:22:37 -05001639 struct lpfc_register portstat_reg = {0};
1640 uint32_t reg_err1, reg_err2;
1641 uint32_t uerrlo_reg, uemasklo_reg;
James Smart65791f12016-07-06 12:35:56 -07001642 uint32_t smphr_port_status = 0, pci_rd_rc1, pci_rd_rc2;
James Smarte10b2022014-02-20 09:57:43 -05001643 bool en_rn_msg = true;
James Smart946727d2015-04-07 15:07:09 -04001644 struct temp_event temp_event_data;
James Smart65791f12016-07-06 12:35:56 -07001645 struct lpfc_register portsmphr_reg;
1646 int rc, i;
James Smartda0436e2009-05-22 14:51:39 -04001647
1648 /* If the pci channel is offline, ignore possible errors, since
1649 * we cannot communicate with the pci card anyway.
1650 */
1651 if (pci_channel_offline(phba->pcidev))
1652 return;
James Smartda0436e2009-05-22 14:51:39 -04001653
James Smart65791f12016-07-06 12:35:56 -07001654 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
James Smart2fcee4b2010-12-15 17:57:46 -05001655 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1656 switch (if_type) {
1657 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart2e90f4b2011-12-13 13:22:37 -05001658 pci_rd_rc1 = lpfc_readl(
1659 phba->sli4_hba.u.if_type0.UERRLOregaddr,
1660 &uerrlo_reg);
1661 pci_rd_rc2 = lpfc_readl(
1662 phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
1663 &uemasklo_reg);
1664 /* consider PCI bus read error as pci_channel_offline */
1665 if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
1666 return;
James Smart65791f12016-07-06 12:35:56 -07001667 if (!(phba->hba_flag & HBA_RECOVERABLE_UE)) {
1668 lpfc_sli4_offline_eratt(phba);
1669 return;
1670 }
1671 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1672 "7623 Checking UE recoverable");
1673
1674 for (i = 0; i < phba->sli4_hba.ue_to_sr / 1000; i++) {
1675 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1676 &portsmphr_reg.word0))
1677 continue;
1678
1679 smphr_port_status = bf_get(lpfc_port_smphr_port_status,
1680 &portsmphr_reg);
1681 if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1682 LPFC_PORT_SEM_UE_RECOVERABLE)
1683 break;
1684 /*Sleep for 1Sec, before checking SEMAPHORE */
1685 msleep(1000);
1686 }
1687
1688 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1689 "4827 smphr_port_status x%x : Waited %dSec",
1690 smphr_port_status, i);
1691
1692 /* Recoverable UE, reset the HBA device */
1693 if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1694 LPFC_PORT_SEM_UE_RECOVERABLE) {
1695 for (i = 0; i < 20; i++) {
1696 msleep(1000);
1697 if (!lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1698 &portsmphr_reg.word0) &&
1699 (LPFC_POST_STAGE_PORT_READY ==
1700 bf_get(lpfc_port_smphr_port_status,
1701 &portsmphr_reg))) {
1702 rc = lpfc_sli4_port_sta_fn_reset(phba,
1703 LPFC_MBX_NO_WAIT, en_rn_msg);
1704 if (rc == 0)
1705 return;
1706 lpfc_printf_log(phba,
1707 KERN_ERR, LOG_INIT,
1708 "4215 Failed to recover UE");
1709 break;
1710 }
1711 }
1712 }
1713 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1714 "7624 Firmware not ready: Failing UE recovery,"
1715 " waited %dSec", i);
James Smart2fcee4b2010-12-15 17:57:46 -05001716 lpfc_sli4_offline_eratt(phba);
1717 break;
James Smart946727d2015-04-07 15:07:09 -04001718
James Smart2fcee4b2010-12-15 17:57:46 -05001719 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart2e90f4b2011-12-13 13:22:37 -05001720 pci_rd_rc1 = lpfc_readl(
1721 phba->sli4_hba.u.if_type2.STATUSregaddr,
1722 &portstat_reg.word0);
1723 /* consider PCI bus read error as pci_channel_offline */
James Smart6b5151f2012-01-18 16:24:06 -05001724 if (pci_rd_rc1 == -EIO) {
1725 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1726 "3151 PCI bus read access failure: x%x\n",
1727 readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
James Smart2e90f4b2011-12-13 13:22:37 -05001728 return;
James Smart6b5151f2012-01-18 16:24:06 -05001729 }
James Smart2e90f4b2011-12-13 13:22:37 -05001730 reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
1731 reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
James Smart2fcee4b2010-12-15 17:57:46 -05001732 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
James Smart2fcee4b2010-12-15 17:57:46 -05001733 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1734 "2889 Port Overtemperature event, "
James Smart946727d2015-04-07 15:07:09 -04001735 "taking port offline Data: x%x x%x\n",
1736 reg_err1, reg_err2);
1737
James Smart310429e2016-07-06 12:35:54 -07001738 phba->sfp_alarm |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
James Smart946727d2015-04-07 15:07:09 -04001739 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1740 temp_event_data.event_code = LPFC_CRIT_TEMP;
1741 temp_event_data.data = 0xFFFFFFFF;
1742
1743 shost = lpfc_shost_from_vport(phba->pport);
1744 fc_host_post_vendor_event(shost, fc_get_event_number(),
1745 sizeof(temp_event_data),
1746 (char *)&temp_event_data,
1747 SCSI_NL_VID_TYPE_PCI
1748 | PCI_VENDOR_ID_EMULEX);
1749
James Smart2fcee4b2010-12-15 17:57:46 -05001750 spin_lock_irq(&phba->hbalock);
1751 phba->over_temp_state = HBA_OVER_TEMP;
1752 spin_unlock_irq(&phba->hbalock);
1753 lpfc_sli4_offline_eratt(phba);
James Smart946727d2015-04-07 15:07:09 -04001754 return;
James Smart2fcee4b2010-12-15 17:57:46 -05001755 }
James Smart2e90f4b2011-12-13 13:22:37 -05001756 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smarte10b2022014-02-20 09:57:43 -05001757 reg_err2 == SLIPORT_ERR2_REG_FW_RESTART) {
James Smart2e90f4b2011-12-13 13:22:37 -05001758 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte10b2022014-02-20 09:57:43 -05001759 "3143 Port Down: Firmware Update "
1760 "Detected\n");
1761 en_rn_msg = false;
1762 } else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smart2e90f4b2011-12-13 13:22:37 -05001763 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1764 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1765 "3144 Port Down: Debug Dump\n");
1766 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1767 reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
1768 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1769 "3145 Port Down: Provisioning\n");
James Smart618a5232012-06-12 13:54:36 -04001770
James Smart946727d2015-04-07 15:07:09 -04001771 /* If resets are disabled then leave the HBA alone and return */
1772 if (!phba->cfg_enable_hba_reset)
1773 return;
1774
James Smart618a5232012-06-12 13:54:36 -04001775 /* Check port status register for function reset */
James Smarte10b2022014-02-20 09:57:43 -05001776 rc = lpfc_sli4_port_sta_fn_reset(phba, LPFC_MBX_NO_WAIT,
1777 en_rn_msg);
James Smart618a5232012-06-12 13:54:36 -04001778 if (rc == 0) {
1779 /* don't report event on forced debug dump */
1780 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1781 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1782 return;
1783 else
1784 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001785 }
James Smart618a5232012-06-12 13:54:36 -04001786 /* fall through for not able to recover */
James Smart6b5151f2012-01-18 16:24:06 -05001787 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1788 "3152 Unrecoverable error, bring the port "
1789 "offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001790 lpfc_sli4_offline_eratt(phba);
1791 break;
1792 case LPFC_SLI_INTF_IF_TYPE_1:
1793 default:
1794 break;
1795 }
James Smart2e90f4b2011-12-13 13:22:37 -05001796 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1797 "3123 Report dump event to upper layer\n");
1798 /* Send an internal error event to mgmt application */
1799 lpfc_board_errevt_to_mgmt(phba);
1800
1801 event_data = FC_REG_DUMP_EVENT;
1802 shost = lpfc_shost_from_vport(vport);
1803 fc_host_post_vendor_event(shost, fc_get_event_number(),
1804 sizeof(event_data), (char *) &event_data,
1805 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
James Smartda0436e2009-05-22 14:51:39 -04001806}
1807
1808/**
1809 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1810 * @phba: pointer to lpfc HBA data structure.
1811 *
1812 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1813 * routine from the API jump table function pointer from the lpfc_hba struct.
1814 *
1815 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001816 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001817 * Any other value - error.
1818 **/
1819void
1820lpfc_handle_eratt(struct lpfc_hba *phba)
1821{
1822 (*phba->lpfc_handle_eratt)(phba);
1823}
1824
1825/**
James Smart3621a712009-04-06 18:47:14 -04001826 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04001827 * @phba: pointer to lpfc hba data structure.
1828 *
1829 * This routine is invoked from the worker thread to handle a HBA host
1830 * attention link event.
1831 **/
dea31012005-04-17 16:05:31 -05001832void
James Smart2e0fef82007-06-17 19:56:36 -05001833lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001834{
James Smart2e0fef82007-06-17 19:56:36 -05001835 struct lpfc_vport *vport = phba->pport;
1836 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001837 LPFC_MBOXQ_t *pmb;
1838 volatile uint32_t control;
1839 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05001840 int rc = 0;
dea31012005-04-17 16:05:31 -05001841
1842 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001843 if (!pmb) {
1844 rc = 1;
dea31012005-04-17 16:05:31 -05001845 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05001846 }
dea31012005-04-17 16:05:31 -05001847
1848 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001849 if (!mp) {
1850 rc = 2;
dea31012005-04-17 16:05:31 -05001851 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05001852 }
dea31012005-04-17 16:05:31 -05001853
1854 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05001855 if (!mp->virt) {
1856 rc = 3;
dea31012005-04-17 16:05:31 -05001857 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05001858 }
dea31012005-04-17 16:05:31 -05001859
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05001860 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04001861 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05001862
1863 psli->slistat.link_event++;
James Smart76a95d72010-11-20 23:11:48 -05001864 lpfc_read_topology(phba, pmb, mp);
1865 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smart2e0fef82007-06-17 19:56:36 -05001866 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04001867 /* Block ELS IOCBs until we have processed this mbox command */
1868 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04001869 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05001870 if (rc == MBX_NOT_FINISHED) {
1871 rc = 4;
James Smart14691152006-12-02 13:34:28 -05001872 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05001873 }
dea31012005-04-17 16:05:31 -05001874
1875 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05001876 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001877 writel(HA_LATT, phba->HAregaddr);
1878 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001879 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001880
1881 return;
1882
James Smart14691152006-12-02 13:34:28 -05001883lpfc_handle_latt_free_mbuf:
James Smart0d2b6b82008-06-14 22:52:47 -04001884 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05001885 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001886lpfc_handle_latt_free_mp:
1887 kfree(mp);
1888lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04001889 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001890lpfc_handle_latt_err_exit:
1891 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001892 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001893 psli->sli_flag |= LPFC_PROCESS_LA;
1894 control = readl(phba->HCregaddr);
1895 control |= HC_LAINT_ENA;
1896 writel(control, phba->HCregaddr);
1897 readl(phba->HCregaddr); /* flush */
1898
1899 /* Clear Link Attention in HA REG */
1900 writel(HA_LATT, phba->HAregaddr);
1901 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001902 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001903 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001904 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001905
James Smart09372822008-01-11 01:52:54 -05001906 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1907 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05001908
1909 return;
1910}
1911
James Smarte59058c2008-08-24 21:49:00 -04001912/**
James Smart3621a712009-04-06 18:47:14 -04001913 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04001914 * @phba: pointer to lpfc hba data structure.
1915 * @vpd: pointer to the vital product data.
1916 * @len: length of the vital product data in bytes.
1917 *
1918 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1919 * an array of characters. In this routine, the ModelName, ProgramType, and
1920 * ModelDesc, etc. fields of the phba data structure will be populated.
1921 *
1922 * Return codes
1923 * 0 - pointer to the VPD passed in is NULL
1924 * 1 - success
1925 **/
James Smart3772a992009-05-22 14:50:54 -04001926int
James Smart2e0fef82007-06-17 19:56:36 -05001927lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05001928{
1929 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05001930 int Length;
dea31012005-04-17 16:05:31 -05001931 int i, j;
1932 int finished = 0;
1933 int index = 0;
1934
1935 if (!vpd)
1936 return 0;
1937
1938 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05001939 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001940 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05001941 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1942 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001943 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05001944 switch (vpd[index]) {
1945 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001946 case 0x91:
dea31012005-04-17 16:05:31 -05001947 index += 1;
1948 lenlo = vpd[index];
1949 index += 1;
1950 lenhi = vpd[index];
1951 index += 1;
1952 i = ((((unsigned short)lenhi) << 8) + lenlo);
1953 index += i;
1954 break;
1955 case 0x90:
1956 index += 1;
1957 lenlo = vpd[index];
1958 index += 1;
1959 lenhi = vpd[index];
1960 index += 1;
1961 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001962 if (Length > len - index)
1963 Length = len - index;
dea31012005-04-17 16:05:31 -05001964 while (Length > 0) {
1965 /* Look for Serial Number */
1966 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1967 index += 2;
1968 i = vpd[index];
1969 index += 1;
1970 j = 0;
1971 Length -= (3+i);
1972 while(i--) {
1973 phba->SerialNumber[j++] = vpd[index++];
1974 if (j == 31)
1975 break;
1976 }
1977 phba->SerialNumber[j] = 0;
1978 continue;
1979 }
1980 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1981 phba->vpd_flag |= VPD_MODEL_DESC;
1982 index += 2;
1983 i = vpd[index];
1984 index += 1;
1985 j = 0;
1986 Length -= (3+i);
1987 while(i--) {
1988 phba->ModelDesc[j++] = vpd[index++];
1989 if (j == 255)
1990 break;
1991 }
1992 phba->ModelDesc[j] = 0;
1993 continue;
1994 }
1995 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1996 phba->vpd_flag |= VPD_MODEL_NAME;
1997 index += 2;
1998 i = vpd[index];
1999 index += 1;
2000 j = 0;
2001 Length -= (3+i);
2002 while(i--) {
2003 phba->ModelName[j++] = vpd[index++];
2004 if (j == 79)
2005 break;
2006 }
2007 phba->ModelName[j] = 0;
2008 continue;
2009 }
2010 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
2011 phba->vpd_flag |= VPD_PROGRAM_TYPE;
2012 index += 2;
2013 i = vpd[index];
2014 index += 1;
2015 j = 0;
2016 Length -= (3+i);
2017 while(i--) {
2018 phba->ProgramType[j++] = vpd[index++];
2019 if (j == 255)
2020 break;
2021 }
2022 phba->ProgramType[j] = 0;
2023 continue;
2024 }
2025 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
2026 phba->vpd_flag |= VPD_PORT;
2027 index += 2;
2028 i = vpd[index];
2029 index += 1;
2030 j = 0;
2031 Length -= (3+i);
2032 while(i--) {
James Smartcd1c8302011-10-10 21:33:25 -04002033 if ((phba->sli_rev == LPFC_SLI_REV4) &&
2034 (phba->sli4_hba.pport_name_sta ==
2035 LPFC_SLI4_PPNAME_GET)) {
2036 j++;
2037 index++;
2038 } else
2039 phba->Port[j++] = vpd[index++];
2040 if (j == 19)
2041 break;
dea31012005-04-17 16:05:31 -05002042 }
James Smartcd1c8302011-10-10 21:33:25 -04002043 if ((phba->sli_rev != LPFC_SLI_REV4) ||
2044 (phba->sli4_hba.pport_name_sta ==
2045 LPFC_SLI4_PPNAME_NON))
2046 phba->Port[j] = 0;
dea31012005-04-17 16:05:31 -05002047 continue;
2048 }
2049 else {
2050 index += 2;
2051 i = vpd[index];
2052 index += 1;
2053 index += i;
2054 Length -= (3 + i);
2055 }
2056 }
2057 finished = 0;
2058 break;
2059 case 0x78:
2060 finished = 1;
2061 break;
2062 default:
2063 index ++;
2064 break;
2065 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002066 }
dea31012005-04-17 16:05:31 -05002067
2068 return(1);
2069}
2070
James Smarte59058c2008-08-24 21:49:00 -04002071/**
James Smart3621a712009-04-06 18:47:14 -04002072 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04002073 * @phba: pointer to lpfc hba data structure.
2074 * @mdp: pointer to the data structure to hold the derived model name.
2075 * @descp: pointer to the data structure to hold the derived description.
2076 *
2077 * This routine retrieves HBA's description based on its registered PCI device
2078 * ID. The @descp passed into this function points to an array of 256 chars. It
2079 * shall be returned with the model name, maximum speed, and the host bus type.
2080 * The @mdp passed into this function points to an array of 80 chars. When the
2081 * function returns, the @mdp will be filled with the model name.
2082 **/
dea31012005-04-17 16:05:31 -05002083static void
James Smart2e0fef82007-06-17 19:56:36 -05002084lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05002085{
2086 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05002087 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002088 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04002089 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04002090 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002091 struct {
James Smarta747c9c2009-11-18 15:41:10 -05002092 char *name;
2093 char *bus;
2094 char *function;
2095 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002096
2097 if (mdp && mdp[0] != '\0'
2098 && descp && descp[0] != '\0')
2099 return;
2100
James Smartd38dd522015-08-31 16:48:17 -04002101 if (phba->lmt & LMT_32Gb)
2102 max_speed = 32;
2103 else if (phba->lmt & LMT_16Gb)
James Smartc0c11512011-05-24 11:41:34 -04002104 max_speed = 16;
2105 else if (phba->lmt & LMT_10Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002106 max_speed = 10;
2107 else if (phba->lmt & LMT_8Gb)
2108 max_speed = 8;
2109 else if (phba->lmt & LMT_4Gb)
2110 max_speed = 4;
2111 else if (phba->lmt & LMT_2Gb)
2112 max_speed = 2;
James Smart4169d862012-09-29 11:32:09 -04002113 else if (phba->lmt & LMT_1Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002114 max_speed = 1;
James Smart4169d862012-09-29 11:32:09 -04002115 else
2116 max_speed = 0;
dea31012005-04-17 16:05:31 -05002117
2118 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05002119
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002120 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002121 case PCI_DEVICE_ID_FIREFLY:
James Smart12222f42014-05-21 08:05:19 -04002122 m = (typeof(m)){"LP6000", "PCI",
2123 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002124 break;
dea31012005-04-17 16:05:31 -05002125 case PCI_DEVICE_ID_SUPERFLY:
2126 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smart12222f42014-05-21 08:05:19 -04002127 m = (typeof(m)){"LP7000", "PCI", ""};
dea31012005-04-17 16:05:31 -05002128 else
James Smart12222f42014-05-21 08:05:19 -04002129 m = (typeof(m)){"LP7000E", "PCI", ""};
2130 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
dea31012005-04-17 16:05:31 -05002131 break;
2132 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002133 m = (typeof(m)){"LP8000", "PCI",
James Smart12222f42014-05-21 08:05:19 -04002134 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002135 break;
2136 case PCI_DEVICE_ID_CENTAUR:
2137 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smart12222f42014-05-21 08:05:19 -04002138 m = (typeof(m)){"LP9002", "PCI", ""};
dea31012005-04-17 16:05:31 -05002139 else
James Smart12222f42014-05-21 08:05:19 -04002140 m = (typeof(m)){"LP9000", "PCI", ""};
2141 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
dea31012005-04-17 16:05:31 -05002142 break;
2143 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002144 m = (typeof(m)){"LP952", "PCI",
James Smart12222f42014-05-21 08:05:19 -04002145 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002146 break;
2147 case PCI_DEVICE_ID_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05002148 m = (typeof(m)){"LP9802", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002149 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002150 break;
2151 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05002152 m = (typeof(m)){"LP10000", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002153 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002154 break;
2155 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05002156 m = (typeof(m)){"LPX1000", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002157 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002158 break;
2159 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002160 m = (typeof(m)){"LP982", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002161 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002162 break;
2163 case PCI_DEVICE_ID_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002164 m = (typeof(m)){"LP1050", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002165 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002166 break;
2167 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05002168 m = (typeof(m)){"LP11000", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002169 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002170 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002171 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002172 m = (typeof(m)){"LP11000-SP", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002173 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002174 break;
2175 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002176 m = (typeof(m)){"LP11002-SP", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002177 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002178 break;
2179 case PCI_DEVICE_ID_NEPTUNE:
James Smart12222f42014-05-21 08:05:19 -04002180 m = (typeof(m)){"LPe1000", "PCIe",
2181 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002182 break;
2183 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smart12222f42014-05-21 08:05:19 -04002184 m = (typeof(m)){"LPe1000-SP", "PCIe",
2185 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002186 break;
2187 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smart12222f42014-05-21 08:05:19 -04002188 m = (typeof(m)){"LPe1002-SP", "PCIe",
2189 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002190 break;
dea31012005-04-17 16:05:31 -05002191 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05002192 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002193 break;
2194 case PCI_DEVICE_ID_BSMB:
James Smart12222f42014-05-21 08:05:19 -04002195 m = (typeof(m)){"LP111", "PCI-X2",
2196 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002197 break;
2198 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05002199 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002200 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002201 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002202 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002203 break;
2204 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002205 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04002206 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002207 break;
dea31012005-04-17 16:05:31 -05002208 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05002209 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002210 break;
2211 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05002212 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002213 break;
2214 case PCI_DEVICE_ID_LP101:
James Smart12222f42014-05-21 08:05:19 -04002215 m = (typeof(m)){"LP101", "PCI-X",
2216 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002217 break;
2218 case PCI_DEVICE_ID_LP10000S:
James Smart12222f42014-05-21 08:05:19 -04002219 m = (typeof(m)){"LP10000-S", "PCI",
2220 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002221 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002222 case PCI_DEVICE_ID_LP11000S:
James Smart12222f42014-05-21 08:05:19 -04002223 m = (typeof(m)){"LP11000-S", "PCI-X2",
2224 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05002225 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002226 case PCI_DEVICE_ID_LPE11000S:
James Smart12222f42014-05-21 08:05:19 -04002227 m = (typeof(m)){"LPe11000-S", "PCIe",
2228 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002229 break;
James Smartb87eab32007-04-25 09:53:28 -04002230 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05002231 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002232 break;
2233 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05002234 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002235 break;
2236 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05002237 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002238 break;
2239 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002240 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002241 break;
2242 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002243 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002244 break;
2245 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05002246 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002247 break;
James Smart84774a42008-08-24 21:50:06 -04002248 case PCI_DEVICE_ID_HORNET:
James Smart12222f42014-05-21 08:05:19 -04002249 m = (typeof(m)){"LP21000", "PCIe",
2250 "Obsolete, Unsupported FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002251 GE = 1;
2252 break;
2253 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05002254 m = (typeof(m)){"LPev12000", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002255 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002256 break;
2257 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05002258 m = (typeof(m)){"LPev12000", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002259 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002260 break;
2261 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05002262 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002263 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002264 break;
James Smartda0436e2009-05-22 14:51:39 -04002265 case PCI_DEVICE_ID_TIGERSHARK:
2266 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002267 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04002268 break;
James Smarta747c9c2009-11-18 15:41:10 -05002269 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04002270 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002271 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
2272 break;
2273 case PCI_DEVICE_ID_FALCON:
2274 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
2275 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04002276 break;
James Smart98fc5dd2010-06-07 15:24:29 -04002277 case PCI_DEVICE_ID_BALIUS:
2278 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
James Smart12222f42014-05-21 08:05:19 -04002279 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart98fc5dd2010-06-07 15:24:29 -04002280 break;
James Smart085c6472010-11-20 23:11:37 -05002281 case PCI_DEVICE_ID_LANCER_FC:
James Smartc0c11512011-05-24 11:41:34 -04002282 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
James Smart085c6472010-11-20 23:11:37 -05002283 break;
James Smart12222f42014-05-21 08:05:19 -04002284 case PCI_DEVICE_ID_LANCER_FC_VF:
2285 m = (typeof(m)){"LPe16000", "PCIe",
2286 "Obsolete, Unsupported Fibre Channel Adapter"};
2287 break;
James Smart085c6472010-11-20 23:11:37 -05002288 case PCI_DEVICE_ID_LANCER_FCOE:
2289 oneConnect = 1;
James Smart079b5c92011-08-21 21:48:49 -04002290 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
James Smart085c6472010-11-20 23:11:37 -05002291 break;
James Smart12222f42014-05-21 08:05:19 -04002292 case PCI_DEVICE_ID_LANCER_FCOE_VF:
2293 oneConnect = 1;
2294 m = (typeof(m)){"OCe15100", "PCIe",
2295 "Obsolete, Unsupported FCoE"};
2296 break;
James Smartd38dd522015-08-31 16:48:17 -04002297 case PCI_DEVICE_ID_LANCER_G6_FC:
2298 m = (typeof(m)){"LPe32000", "PCIe", "Fibre Channel Adapter"};
2299 break;
James Smartf8cafd32012-08-03 12:42:51 -04002300 case PCI_DEVICE_ID_SKYHAWK:
2301 case PCI_DEVICE_ID_SKYHAWK_VF:
2302 oneConnect = 1;
2303 m = (typeof(m)){"OCe14000", "PCIe", "FCoE"};
2304 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002305 default:
James Smarta747c9c2009-11-18 15:41:10 -05002306 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002307 break;
dea31012005-04-17 16:05:31 -05002308 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002309
2310 if (mdp && mdp[0] == '\0')
2311 snprintf(mdp, 79,"%s", m.name);
James Smartc0c11512011-05-24 11:41:34 -04002312 /*
2313 * oneConnect hba requires special processing, they are all initiators
James Smartda0436e2009-05-22 14:51:39 -04002314 * and we put the port number on the end
2315 */
2316 if (descp && descp[0] == '\0') {
2317 if (oneConnect)
2318 snprintf(descp, 255,
James Smart4169d862012-09-29 11:32:09 -04002319 "Emulex OneConnect %s, %s Initiator %s",
James Smarta747c9c2009-11-18 15:41:10 -05002320 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04002321 phba->Port);
James Smart4169d862012-09-29 11:32:09 -04002322 else if (max_speed == 0)
2323 snprintf(descp, 255,
Sebastian Herbszt290237d2015-08-31 16:48:08 -04002324 "Emulex %s %s %s",
James Smart4169d862012-09-29 11:32:09 -04002325 m.name, m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002326 else
2327 snprintf(descp, 255,
2328 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05002329 m.name, max_speed, (GE) ? "GE" : "Gb",
2330 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002331 }
dea31012005-04-17 16:05:31 -05002332}
2333
James Smarte59058c2008-08-24 21:49:00 -04002334/**
James Smart3621a712009-04-06 18:47:14 -04002335 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04002336 * @phba: pointer to lpfc hba data structure.
2337 * @pring: pointer to a IOCB ring.
2338 * @cnt: the number of IOCBs to be posted to the IOCB ring.
2339 *
2340 * This routine posts a given number of IOCBs with the associated DMA buffer
2341 * descriptors specified by the cnt argument to the given IOCB ring.
2342 *
2343 * Return codes
2344 * The number of IOCBs NOT able to be posted to the IOCB ring.
2345 **/
dea31012005-04-17 16:05:31 -05002346int
James Smart495a7142008-06-14 22:52:59 -04002347lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05002348{
2349 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002350 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05002351 struct lpfc_dmabuf *mp1, *mp2;
2352
2353 cnt += pring->missbufcnt;
2354
2355 /* While there are buffers to post */
2356 while (cnt > 0) {
2357 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002358 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002359 if (iocb == NULL) {
2360 pring->missbufcnt = cnt;
2361 return cnt;
2362 }
dea31012005-04-17 16:05:31 -05002363 icmd = &iocb->iocb;
2364
2365 /* 2 buffers can be posted per command */
2366 /* Allocate buffer to post */
2367 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2368 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04002369 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2370 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002371 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002372 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002373 pring->missbufcnt = cnt;
2374 return cnt;
2375 }
2376
2377 INIT_LIST_HEAD(&mp1->list);
2378 /* Allocate buffer to post */
2379 if (cnt > 1) {
2380 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2381 if (mp2)
2382 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2383 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04002384 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002385 kfree(mp2);
dea31012005-04-17 16:05:31 -05002386 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2387 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002388 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002389 pring->missbufcnt = cnt;
2390 return cnt;
2391 }
2392
2393 INIT_LIST_HEAD(&mp2->list);
2394 } else {
2395 mp2 = NULL;
2396 }
2397
2398 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2399 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2400 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2401 icmd->ulpBdeCount = 1;
2402 cnt--;
2403 if (mp2) {
2404 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2405 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2406 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2407 cnt--;
2408 icmd->ulpBdeCount = 2;
2409 }
2410
2411 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2412 icmd->ulpLe = 1;
2413
James Smart3772a992009-05-22 14:50:54 -04002414 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2415 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05002416 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2417 kfree(mp1);
2418 cnt++;
2419 if (mp2) {
2420 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2421 kfree(mp2);
2422 cnt++;
2423 }
James Bottomley604a3e32005-10-29 10:28:33 -05002424 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002425 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05002426 return cnt;
2427 }
dea31012005-04-17 16:05:31 -05002428 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05002429 if (mp2)
dea31012005-04-17 16:05:31 -05002430 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05002431 }
2432 pring->missbufcnt = 0;
2433 return 0;
2434}
2435
James Smarte59058c2008-08-24 21:49:00 -04002436/**
James Smart3621a712009-04-06 18:47:14 -04002437 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04002438 * @phba: pointer to lpfc hba data structure.
2439 *
2440 * This routine posts initial receive IOCB buffers to the ELS ring. The
2441 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
2442 * set to 64 IOCBs.
2443 *
2444 * Return codes
2445 * 0 - success (currently always success)
2446 **/
dea31012005-04-17 16:05:31 -05002447static int
James Smart2e0fef82007-06-17 19:56:36 -05002448lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002449{
2450 struct lpfc_sli *psli = &phba->sli;
2451
2452 /* Ring 0, ELS / CT buffers */
James Smart495a7142008-06-14 22:52:59 -04002453 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05002454 /* Ring 2 - FCP no buffers needed */
2455
2456 return 0;
2457}
2458
2459#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2460
James Smarte59058c2008-08-24 21:49:00 -04002461/**
James Smart3621a712009-04-06 18:47:14 -04002462 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04002463 * @HashResultPointer: pointer to an array as hash table.
2464 *
2465 * This routine sets up the initial values to the array of hash table entries
2466 * for the LC HBAs.
2467 **/
dea31012005-04-17 16:05:31 -05002468static void
2469lpfc_sha_init(uint32_t * HashResultPointer)
2470{
2471 HashResultPointer[0] = 0x67452301;
2472 HashResultPointer[1] = 0xEFCDAB89;
2473 HashResultPointer[2] = 0x98BADCFE;
2474 HashResultPointer[3] = 0x10325476;
2475 HashResultPointer[4] = 0xC3D2E1F0;
2476}
2477
James Smarte59058c2008-08-24 21:49:00 -04002478/**
James Smart3621a712009-04-06 18:47:14 -04002479 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04002480 * @HashResultPointer: pointer to an initial/result hash table.
2481 * @HashWorkingPointer: pointer to an working hash table.
2482 *
2483 * This routine iterates an initial hash table pointed by @HashResultPointer
2484 * with the values from the working hash table pointeed by @HashWorkingPointer.
2485 * The results are putting back to the initial hash table, returned through
2486 * the @HashResultPointer as the result hash table.
2487 **/
dea31012005-04-17 16:05:31 -05002488static void
2489lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2490{
2491 int t;
2492 uint32_t TEMP;
2493 uint32_t A, B, C, D, E;
2494 t = 16;
2495 do {
2496 HashWorkingPointer[t] =
2497 S(1,
2498 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2499 8] ^
2500 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2501 } while (++t <= 79);
2502 t = 0;
2503 A = HashResultPointer[0];
2504 B = HashResultPointer[1];
2505 C = HashResultPointer[2];
2506 D = HashResultPointer[3];
2507 E = HashResultPointer[4];
2508
2509 do {
2510 if (t < 20) {
2511 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2512 } else if (t < 40) {
2513 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2514 } else if (t < 60) {
2515 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2516 } else {
2517 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2518 }
2519 TEMP += S(5, A) + E + HashWorkingPointer[t];
2520 E = D;
2521 D = C;
2522 C = S(30, B);
2523 B = A;
2524 A = TEMP;
2525 } while (++t <= 79);
2526
2527 HashResultPointer[0] += A;
2528 HashResultPointer[1] += B;
2529 HashResultPointer[2] += C;
2530 HashResultPointer[3] += D;
2531 HashResultPointer[4] += E;
2532
2533}
2534
James Smarte59058c2008-08-24 21:49:00 -04002535/**
James Smart3621a712009-04-06 18:47:14 -04002536 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04002537 * @RandomChallenge: pointer to the entry of host challenge random number array.
2538 * @HashWorking: pointer to the entry of the working hash array.
2539 *
2540 * This routine calculates the working hash array referred by @HashWorking
2541 * from the challenge random numbers associated with the host, referred by
2542 * @RandomChallenge. The result is put into the entry of the working hash
2543 * array and returned by reference through @HashWorking.
2544 **/
dea31012005-04-17 16:05:31 -05002545static void
2546lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2547{
2548 *HashWorking = (*RandomChallenge ^ *HashWorking);
2549}
2550
James Smarte59058c2008-08-24 21:49:00 -04002551/**
James Smart3621a712009-04-06 18:47:14 -04002552 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04002553 * @phba: pointer to lpfc hba data structure.
2554 * @hbainit: pointer to an array of unsigned 32-bit integers.
2555 *
2556 * This routine performs the special handling for LC HBA initialization.
2557 **/
dea31012005-04-17 16:05:31 -05002558void
2559lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2560{
2561 int t;
2562 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05002563 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05002564
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002565 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002566 if (!HashWorking)
2567 return;
2568
dea31012005-04-17 16:05:31 -05002569 HashWorking[0] = HashWorking[78] = *pwwnn++;
2570 HashWorking[1] = HashWorking[79] = *pwwnn;
2571
2572 for (t = 0; t < 7; t++)
2573 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2574
2575 lpfc_sha_init(hbainit);
2576 lpfc_sha_iterate(hbainit, HashWorking);
2577 kfree(HashWorking);
2578}
2579
James Smarte59058c2008-08-24 21:49:00 -04002580/**
James Smart3621a712009-04-06 18:47:14 -04002581 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04002582 * @vport: pointer to a virtual N_Port data structure.
2583 *
2584 * This routine performs the necessary cleanups before deleting the @vport.
2585 * It invokes the discovery state machine to perform necessary state
2586 * transitions and to release the ndlps associated with the @vport. Note,
2587 * the physical port is treated as @vport 0.
2588 **/
James Smart87af33f2007-10-27 13:37:43 -04002589void
James Smart2e0fef82007-06-17 19:56:36 -05002590lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002591{
James Smart87af33f2007-10-27 13:37:43 -04002592 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002593 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04002594 int i = 0;
dea31012005-04-17 16:05:31 -05002595
James Smart87af33f2007-10-27 13:37:43 -04002596 if (phba->link_state > LPFC_LINK_DOWN)
2597 lpfc_port_link_failure(vport);
2598
2599 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002600 if (!NLP_CHK_NODE_ACT(ndlp)) {
2601 ndlp = lpfc_enable_node(vport, ndlp,
2602 NLP_STE_UNUSED_NODE);
2603 if (!ndlp)
2604 continue;
2605 spin_lock_irq(&phba->ndlp_lock);
2606 NLP_SET_FREE_REQ(ndlp);
2607 spin_unlock_irq(&phba->ndlp_lock);
2608 /* Trigger the release of the ndlp memory */
2609 lpfc_nlp_put(ndlp);
2610 continue;
2611 }
2612 spin_lock_irq(&phba->ndlp_lock);
2613 if (NLP_CHK_FREE_REQ(ndlp)) {
2614 /* The ndlp should not be in memory free mode already */
2615 spin_unlock_irq(&phba->ndlp_lock);
2616 continue;
2617 } else
2618 /* Indicate request for freeing ndlp memory */
2619 NLP_SET_FREE_REQ(ndlp);
2620 spin_unlock_irq(&phba->ndlp_lock);
2621
James Smart58da1ff2008-04-07 10:15:56 -04002622 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2623 ndlp->nlp_DID == Fabric_DID) {
2624 /* Just free up ndlp with Fabric_DID for vports */
2625 lpfc_nlp_put(ndlp);
2626 continue;
2627 }
2628
James Smarteff4a012012-01-18 16:25:25 -05002629 /* take care of nodes in unused state before the state
2630 * machine taking action.
2631 */
2632 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
2633 lpfc_nlp_put(ndlp);
2634 continue;
2635 }
2636
James Smart87af33f2007-10-27 13:37:43 -04002637 if (ndlp->nlp_type & NLP_FABRIC)
2638 lpfc_disc_state_machine(vport, ndlp, NULL,
2639 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002640
James Smart87af33f2007-10-27 13:37:43 -04002641 lpfc_disc_state_machine(vport, ndlp, NULL,
2642 NLP_EVT_DEVICE_RM);
2643 }
2644
James Smarta8adb832007-10-27 13:37:53 -04002645 /* At this point, ALL ndlp's should be gone
2646 * because of the previous NLP_EVT_DEVICE_RM.
2647 * Lets wait for this to happen, if needed.
2648 */
James Smart87af33f2007-10-27 13:37:43 -04002649 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002650 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002651 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002652 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002653 list_for_each_entry_safe(ndlp, next_ndlp,
2654 &vport->fc_nodes, nlp_listp) {
2655 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2656 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002657 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002658 "usgmap:x%x refcnt:%d\n",
2659 ndlp->nlp_DID, (void *)ndlp,
2660 ndlp->nlp_usg_map,
2661 atomic_read(
2662 &ndlp->kref.refcount));
2663 }
James Smarta8adb832007-10-27 13:37:53 -04002664 break;
James Smart87af33f2007-10-27 13:37:43 -04002665 }
James Smarta8adb832007-10-27 13:37:53 -04002666
2667 /* Wait for any activity on ndlps to settle */
2668 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002669 }
James Smart1151e3e2011-02-16 12:39:35 -05002670 lpfc_cleanup_vports_rrqs(vport, NULL);
dea31012005-04-17 16:05:31 -05002671}
2672
James Smarte59058c2008-08-24 21:49:00 -04002673/**
James Smart3621a712009-04-06 18:47:14 -04002674 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002675 * @vport: pointer to a virtual N_Port data structure.
2676 *
2677 * This routine stops all the timers associated with a @vport. This function
2678 * is invoked before disabling or deleting a @vport. Note that the physical
2679 * port is treated as @vport 0.
2680 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002681void
2682lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002683{
James Smart92d7f7b2007-06-17 19:56:38 -05002684 del_timer_sync(&vport->els_tmofunc);
James Smart92494142011-02-16 12:39:44 -05002685 del_timer_sync(&vport->delayed_disc_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002686 lpfc_can_disctmo(vport);
2687 return;
dea31012005-04-17 16:05:31 -05002688}
2689
James Smarte59058c2008-08-24 21:49:00 -04002690/**
James Smartecfd03c2010-02-12 14:41:27 -05002691 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2692 * @phba: pointer to lpfc hba data structure.
2693 *
2694 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2695 * caller of this routine should already hold the host lock.
2696 **/
2697void
2698__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2699{
James Smart5ac6b302010-10-22 11:05:36 -04002700 /* Clear pending FCF rediscovery wait flag */
2701 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2702
James Smartecfd03c2010-02-12 14:41:27 -05002703 /* Now, try to stop the timer */
2704 del_timer(&phba->fcf.redisc_wait);
2705}
2706
2707/**
2708 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2709 * @phba: pointer to lpfc hba data structure.
2710 *
2711 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2712 * checks whether the FCF rediscovery wait timer is pending with the host
2713 * lock held before proceeding with disabling the timer and clearing the
2714 * wait timer pendig flag.
2715 **/
2716void
2717lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2718{
2719 spin_lock_irq(&phba->hbalock);
2720 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2721 /* FCF rediscovery timer already fired or stopped */
2722 spin_unlock_irq(&phba->hbalock);
2723 return;
2724 }
2725 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart5ac6b302010-10-22 11:05:36 -04002726 /* Clear failover in progress flags */
2727 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
James Smartecfd03c2010-02-12 14:41:27 -05002728 spin_unlock_irq(&phba->hbalock);
2729}
2730
2731/**
James Smart3772a992009-05-22 14:50:54 -04002732 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002733 * @phba: pointer to lpfc hba data structure.
2734 *
2735 * This routine stops all the timers associated with a HBA. This function is
2736 * invoked before either putting a HBA offline or unloading the driver.
2737 **/
James Smart3772a992009-05-22 14:50:54 -04002738void
2739lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002740{
James Smart51ef4c22007-08-02 11:10:31 -04002741 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05002742 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002743 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002744 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002745 del_timer_sync(&phba->hb_tmofunc);
James Smart1151e3e2011-02-16 12:39:35 -05002746 if (phba->sli_rev == LPFC_SLI_REV4) {
2747 del_timer_sync(&phba->rrq_tmr);
2748 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2749 }
James Smart3772a992009-05-22 14:50:54 -04002750 phba->hb_outstanding = 0;
2751
2752 switch (phba->pci_dev_grp) {
2753 case LPFC_PCI_DEV_LP:
2754 /* Stop any LightPulse device specific driver timers */
2755 del_timer_sync(&phba->fcp_poll_timer);
2756 break;
2757 case LPFC_PCI_DEV_OC:
2758 /* Stop any OneConnect device sepcific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05002759 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04002760 break;
2761 default:
2762 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2763 "0297 Invalid device group (x%x)\n",
2764 phba->pci_dev_grp);
2765 break;
2766 }
James Smart2e0fef82007-06-17 19:56:36 -05002767 return;
dea31012005-04-17 16:05:31 -05002768}
2769
James Smarte59058c2008-08-24 21:49:00 -04002770/**
James Smart3621a712009-04-06 18:47:14 -04002771 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002772 * @phba: pointer to lpfc hba data structure.
2773 *
2774 * This routine marks a HBA's management interface as blocked. Once the HBA's
2775 * management interface is marked as blocked, all the user space access to
2776 * the HBA, whether they are from sysfs interface or libdfc interface will
2777 * all be blocked. The HBA is set to block the management interface when the
2778 * driver prepares the HBA interface for online or offline.
2779 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002780static void
James Smart618a5232012-06-12 13:54:36 -04002781lpfc_block_mgmt_io(struct lpfc_hba *phba, int mbx_action)
Adrian Bunka6ababd2007-11-05 18:07:33 +01002782{
2783 unsigned long iflag;
James Smart6e7288d2010-06-07 15:23:35 -04002784 uint8_t actcmd = MBX_HEARTBEAT;
2785 unsigned long timeout;
2786
Adrian Bunka6ababd2007-11-05 18:07:33 +01002787 spin_lock_irqsave(&phba->hbalock, iflag);
2788 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
James Smart618a5232012-06-12 13:54:36 -04002789 spin_unlock_irqrestore(&phba->hbalock, iflag);
2790 if (mbx_action == LPFC_MBX_NO_WAIT)
2791 return;
2792 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
2793 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002794 if (phba->sli.mbox_active) {
James Smart6e7288d2010-06-07 15:23:35 -04002795 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
James Smarta183a152011-10-10 21:32:43 -04002796 /* Determine how long we might wait for the active mailbox
2797 * command to be gracefully completed by firmware.
2798 */
2799 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
2800 phba->sli.mbox_active) * 1000) + jiffies;
2801 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002802 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002803
James Smart6e7288d2010-06-07 15:23:35 -04002804 /* Wait for the outstnading mailbox command to complete */
2805 while (phba->sli.mbox_active) {
2806 /* Check active mailbox complete status every 2ms */
2807 msleep(2);
2808 if (time_after(jiffies, timeout)) {
2809 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2810 "2813 Mgmt IO is Blocked %x "
2811 "- mbox cmd %x still active\n",
2812 phba->sli.sli_flag, actcmd);
2813 break;
2814 }
2815 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002816}
2817
James Smarte59058c2008-08-24 21:49:00 -04002818/**
James Smart6b5151f2012-01-18 16:24:06 -05002819 * lpfc_sli4_node_prep - Assign RPIs for active nodes.
2820 * @phba: pointer to lpfc hba data structure.
2821 *
2822 * Allocate RPIs for all active remote nodes. This is needed whenever
2823 * an SLI4 adapter is reset and the driver is not unloading. Its purpose
2824 * is to fixup the temporary rpi assignments.
2825 **/
2826void
2827lpfc_sli4_node_prep(struct lpfc_hba *phba)
2828{
2829 struct lpfc_nodelist *ndlp, *next_ndlp;
2830 struct lpfc_vport **vports;
2831 int i;
2832
2833 if (phba->sli_rev != LPFC_SLI_REV4)
2834 return;
2835
2836 vports = lpfc_create_vport_work_array(phba);
2837 if (vports != NULL) {
2838 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2839 if (vports[i]->load_flag & FC_UNLOADING)
2840 continue;
2841
2842 list_for_each_entry_safe(ndlp, next_ndlp,
2843 &vports[i]->fc_nodes,
2844 nlp_listp) {
James Smartbe6bb942015-04-07 15:07:22 -04002845 if (NLP_CHK_NODE_ACT(ndlp)) {
James Smart6b5151f2012-01-18 16:24:06 -05002846 ndlp->nlp_rpi =
2847 lpfc_sli4_alloc_rpi(phba);
James Smartbe6bb942015-04-07 15:07:22 -04002848 lpfc_printf_vlog(ndlp->vport, KERN_INFO,
2849 LOG_NODE,
2850 "0009 rpi:%x DID:%x "
2851 "flg:%x map:%x %p\n",
2852 ndlp->nlp_rpi,
2853 ndlp->nlp_DID,
2854 ndlp->nlp_flag,
2855 ndlp->nlp_usg_map,
2856 ndlp);
2857 }
James Smart6b5151f2012-01-18 16:24:06 -05002858 }
2859 }
2860 }
2861 lpfc_destroy_vport_work_array(phba, vports);
2862}
2863
2864/**
James Smart3621a712009-04-06 18:47:14 -04002865 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04002866 * @phba: pointer to lpfc hba data structure.
2867 *
2868 * This routine initializes the HBA and brings a HBA online. During this
2869 * process, the management interface is blocked to prevent user space access
2870 * to the HBA interfering with the driver initialization.
2871 *
2872 * Return codes
2873 * 0 - successful
2874 * 1 - failed
2875 **/
dea31012005-04-17 16:05:31 -05002876int
James Smart2e0fef82007-06-17 19:56:36 -05002877lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002878{
Julia Lawall372bd282008-12-16 16:15:08 +01002879 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04002880 struct lpfc_vport **vports;
2881 int i;
James Smart16a3a202013-04-17 20:14:38 -04002882 bool vpis_cleared = false;
James Smart2e0fef82007-06-17 19:56:36 -05002883
dea31012005-04-17 16:05:31 -05002884 if (!phba)
2885 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01002886 vport = phba->pport;
dea31012005-04-17 16:05:31 -05002887
James Smart2e0fef82007-06-17 19:56:36 -05002888 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05002889 return 0;
2890
James Smarted957682007-06-17 19:56:37 -05002891 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002892 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05002893
James Smart618a5232012-06-12 13:54:36 -04002894 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05002895
James Smart46fa3112007-04-25 09:51:45 -04002896 if (!lpfc_sli_queue_setup(phba)) {
2897 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002898 return 1;
James Smart46fa3112007-04-25 09:51:45 -04002899 }
2900
James Smartda0436e2009-05-22 14:51:39 -04002901 if (phba->sli_rev == LPFC_SLI_REV4) {
2902 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2903 lpfc_unblock_mgmt_io(phba);
2904 return 1;
2905 }
James Smart16a3a202013-04-17 20:14:38 -04002906 spin_lock_irq(&phba->hbalock);
2907 if (!phba->sli4_hba.max_cfg_param.vpi_used)
2908 vpis_cleared = true;
2909 spin_unlock_irq(&phba->hbalock);
James Smartda0436e2009-05-22 14:51:39 -04002910 } else {
2911 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2912 lpfc_unblock_mgmt_io(phba);
2913 return 1;
2914 }
James Smart46fa3112007-04-25 09:51:45 -04002915 }
dea31012005-04-17 16:05:31 -05002916
James Smart549e55c2007-08-02 11:09:51 -04002917 vports = lpfc_create_vport_work_array(phba);
Arnd Bergmannaeb66412016-03-14 15:29:44 +01002918 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04002919 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002920 struct Scsi_Host *shost;
2921 shost = lpfc_shost_from_vport(vports[i]);
2922 spin_lock_irq(shost->host_lock);
2923 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2924 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2925 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart16a3a202013-04-17 20:14:38 -04002926 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart1c6834a2009-07-19 10:01:26 -04002927 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart16a3a202013-04-17 20:14:38 -04002928 if ((vpis_cleared) &&
2929 (vports[i]->port_type !=
2930 LPFC_PHYSICAL_PORT))
2931 vports[i]->vpi = 0;
2932 }
James Smart549e55c2007-08-02 11:09:51 -04002933 spin_unlock_irq(shost->host_lock);
2934 }
Arnd Bergmannaeb66412016-03-14 15:29:44 +01002935 }
2936 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002937
James Smart46fa3112007-04-25 09:51:45 -04002938 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002939 return 0;
2940}
2941
James Smarte59058c2008-08-24 21:49:00 -04002942/**
James Smart3621a712009-04-06 18:47:14 -04002943 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04002944 * @phba: pointer to lpfc hba data structure.
2945 *
2946 * This routine marks a HBA's management interface as not blocked. Once the
2947 * HBA's management interface is marked as not blocked, all the user space
2948 * access to the HBA, whether they are from sysfs interface or libdfc
2949 * interface will be allowed. The HBA is set to block the management interface
2950 * when the driver prepares the HBA interface for online or offline and then
2951 * set to unblock the management interface afterwards.
2952 **/
James Smart46fa3112007-04-25 09:51:45 -04002953void
James Smart46fa3112007-04-25 09:51:45 -04002954lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2955{
2956 unsigned long iflag;
2957
James Smart2e0fef82007-06-17 19:56:36 -05002958 spin_lock_irqsave(&phba->hbalock, iflag);
2959 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2960 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04002961}
2962
James Smarte59058c2008-08-24 21:49:00 -04002963/**
James Smart3621a712009-04-06 18:47:14 -04002964 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04002965 * @phba: pointer to lpfc hba data structure.
2966 *
2967 * This routine is invoked to prepare a HBA to be brought offline. It performs
2968 * unregistration login to all the nodes on all vports and flushes the mailbox
2969 * queue to make it ready to be brought offline.
2970 **/
James Smart46fa3112007-04-25 09:51:45 -04002971void
James Smart618a5232012-06-12 13:54:36 -04002972lpfc_offline_prep(struct lpfc_hba *phba, int mbx_action)
James Smart46fa3112007-04-25 09:51:45 -04002973{
James Smart2e0fef82007-06-17 19:56:36 -05002974 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04002975 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04002976 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05002977 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04002978 int i;
dea31012005-04-17 16:05:31 -05002979
James Smart2e0fef82007-06-17 19:56:36 -05002980 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002981 return;
dea31012005-04-17 16:05:31 -05002982
James Smart618a5232012-06-12 13:54:36 -04002983 lpfc_block_mgmt_io(phba, mbx_action);
dea31012005-04-17 16:05:31 -05002984
2985 lpfc_linkdown(phba);
2986
James Smart87af33f2007-10-27 13:37:43 -04002987 /* Issue an unreg_login to all nodes on all vports */
2988 vports = lpfc_create_vport_work_array(phba);
2989 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04002990 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04002991 if (vports[i]->load_flag & FC_UNLOADING)
2992 continue;
James Smart72100cc2010-02-12 14:43:01 -05002993 shost = lpfc_shost_from_vport(vports[i]);
2994 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002995 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002996 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2997 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05002998 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002999
James Smart87af33f2007-10-27 13:37:43 -04003000 shost = lpfc_shost_from_vport(vports[i]);
3001 list_for_each_entry_safe(ndlp, next_ndlp,
3002 &vports[i]->fc_nodes,
3003 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05003004 if (!NLP_CHK_NODE_ACT(ndlp))
3005 continue;
James Smart87af33f2007-10-27 13:37:43 -04003006 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
3007 continue;
3008 if (ndlp->nlp_type & NLP_FABRIC) {
3009 lpfc_disc_state_machine(vports[i], ndlp,
3010 NULL, NLP_EVT_DEVICE_RECOVERY);
3011 lpfc_disc_state_machine(vports[i], ndlp,
3012 NULL, NLP_EVT_DEVICE_RM);
3013 }
3014 spin_lock_irq(shost->host_lock);
3015 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart401ee0c2012-03-01 22:35:34 -05003016 spin_unlock_irq(shost->host_lock);
James Smart6b5151f2012-01-18 16:24:06 -05003017 /*
3018 * Whenever an SLI4 port goes offline, free the
James Smart401ee0c2012-03-01 22:35:34 -05003019 * RPI. Get a new RPI when the adapter port
3020 * comes back online.
James Smart6b5151f2012-01-18 16:24:06 -05003021 */
James Smartbe6bb942015-04-07 15:07:22 -04003022 if (phba->sli_rev == LPFC_SLI_REV4) {
3023 lpfc_printf_vlog(ndlp->vport,
3024 KERN_INFO, LOG_NODE,
3025 "0011 lpfc_offline: "
3026 "ndlp:x%p did %x "
3027 "usgmap:x%x rpi:%x\n",
3028 ndlp, ndlp->nlp_DID,
3029 ndlp->nlp_usg_map,
3030 ndlp->nlp_rpi);
3031
James Smart6b5151f2012-01-18 16:24:06 -05003032 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smartbe6bb942015-04-07 15:07:22 -04003033 }
James Smart87af33f2007-10-27 13:37:43 -04003034 lpfc_unreg_rpi(vports[i], ndlp);
3035 }
3036 }
3037 }
James Smart09372822008-01-11 01:52:54 -05003038 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05003039
James Smart618a5232012-06-12 13:54:36 -04003040 lpfc_sli_mbox_sys_shutdown(phba, mbx_action);
James Smart46fa3112007-04-25 09:51:45 -04003041}
3042
James Smarte59058c2008-08-24 21:49:00 -04003043/**
James Smart3621a712009-04-06 18:47:14 -04003044 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04003045 * @phba: pointer to lpfc hba data structure.
3046 *
3047 * This routine actually brings a HBA offline. It stops all the timers
3048 * associated with the HBA, brings down the SLI layer, and eventually
3049 * marks the HBA as in offline state for the upper layer protocol.
3050 **/
James Smart46fa3112007-04-25 09:51:45 -04003051void
James Smart2e0fef82007-06-17 19:56:36 -05003052lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04003053{
James Smart549e55c2007-08-02 11:09:51 -04003054 struct Scsi_Host *shost;
3055 struct lpfc_vport **vports;
3056 int i;
James Smart46fa3112007-04-25 09:51:45 -04003057
James Smart549e55c2007-08-02 11:09:51 -04003058 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04003059 return;
James Smart688a8862006-07-06 15:49:56 -04003060
James Smartda0436e2009-05-22 14:51:39 -04003061 /* stop port and all timers associated with this hba */
3062 lpfc_stop_port(phba);
James Smart51ef4c22007-08-02 11:10:31 -04003063 vports = lpfc_create_vport_work_array(phba);
3064 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04003065 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04003066 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05003067 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05003068 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003069 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05003070 /* Bring down the SLI Layer and cleanup. The HBA is offline
3071 now. */
3072 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003073 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04003074 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003075 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04003076 vports = lpfc_create_vport_work_array(phba);
3077 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04003078 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04003079 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04003080 spin_lock_irq(shost->host_lock);
3081 vports[i]->work_port_events = 0;
3082 vports[i]->fc_flag |= FC_OFFLINE_MODE;
3083 spin_unlock_irq(shost->host_lock);
3084 }
James Smart09372822008-01-11 01:52:54 -05003085 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05003086}
3087
James Smarte59058c2008-08-24 21:49:00 -04003088/**
James Smart3621a712009-04-06 18:47:14 -04003089 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04003090 * @phba: pointer to lpfc hba data structure.
3091 *
3092 * This routine is to free all the SCSI buffers and IOCBs from the driver
3093 * list back to kernel. It is called from lpfc_pci_remove_one to free
3094 * the internal resources before the device is removed from the system.
James Smarte59058c2008-08-24 21:49:00 -04003095 **/
James Smart8a9d2e82012-05-09 21:16:12 -04003096static void
James Smart2e0fef82007-06-17 19:56:36 -05003097lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003098{
3099 struct lpfc_scsi_buf *sb, *sb_next;
3100 struct lpfc_iocbq *io, *io_next;
3101
James Smart2e0fef82007-06-17 19:56:36 -05003102 spin_lock_irq(&phba->hbalock);
James Smarta40fc5f2013-04-17 20:17:40 -04003103
dea31012005-04-17 16:05:31 -05003104 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smarta40fc5f2013-04-17 20:17:40 -04003105
3106 spin_lock(&phba->scsi_buf_list_put_lock);
3107 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_put,
3108 list) {
dea31012005-04-17 16:05:31 -05003109 list_del(&sb->list);
3110 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05003111 sb->dma_handle);
dea31012005-04-17 16:05:31 -05003112 kfree(sb);
3113 phba->total_scsi_bufs--;
3114 }
James Smarta40fc5f2013-04-17 20:17:40 -04003115 spin_unlock(&phba->scsi_buf_list_put_lock);
3116
3117 spin_lock(&phba->scsi_buf_list_get_lock);
3118 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_get,
3119 list) {
3120 list_del(&sb->list);
3121 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
3122 sb->dma_handle);
3123 kfree(sb);
3124 phba->total_scsi_bufs--;
3125 }
3126 spin_unlock(&phba->scsi_buf_list_get_lock);
dea31012005-04-17 16:05:31 -05003127
3128 /* Release all the lpfc_iocbq entries maintained by this host. */
3129 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
3130 list_del(&io->list);
3131 kfree(io);
3132 phba->total_iocbq_bufs--;
3133 }
James Smart6d368e52011-05-24 11:44:12 -04003134
James Smart2e0fef82007-06-17 19:56:36 -05003135 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003136}
3137
3138/**
3139 * lpfc_sli4_xri_sgl_update - update xri-sgl sizing and mapping
3140 * @phba: pointer to lpfc hba data structure.
3141 *
3142 * This routine first calculates the sizes of the current els and allocated
3143 * scsi sgl lists, and then goes through all sgls to updates the physical
3144 * XRIs assigned due to port function reset. During port initialization, the
3145 * current els and allocated scsi sgl lists are 0s.
3146 *
3147 * Return codes
3148 * 0 - successful (for now, it always returns 0)
3149 **/
3150int
3151lpfc_sli4_xri_sgl_update(struct lpfc_hba *phba)
3152{
3153 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
3154 struct lpfc_scsi_buf *psb = NULL, *psb_next = NULL;
3155 uint16_t i, lxri, xri_cnt, els_xri_cnt, scsi_xri_cnt;
3156 LIST_HEAD(els_sgl_list);
3157 LIST_HEAD(scsi_sgl_list);
3158 int rc;
James Smartdafe8ce2014-09-03 12:56:40 -04003159 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart8a9d2e82012-05-09 21:16:12 -04003160
3161 /*
3162 * update on pci function's els xri-sgl list
3163 */
3164 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3165 if (els_xri_cnt > phba->sli4_hba.els_xri_cnt) {
3166 /* els xri-sgl expanded */
3167 xri_cnt = els_xri_cnt - phba->sli4_hba.els_xri_cnt;
3168 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3169 "3157 ELS xri-sgl count increased from "
3170 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3171 els_xri_cnt);
3172 /* allocate the additional els sgls */
3173 for (i = 0; i < xri_cnt; i++) {
3174 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3175 GFP_KERNEL);
3176 if (sglq_entry == NULL) {
3177 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3178 "2562 Failure to allocate an "
3179 "ELS sgl entry:%d\n", i);
3180 rc = -ENOMEM;
3181 goto out_free_mem;
3182 }
3183 sglq_entry->buff_type = GEN_BUFF_TYPE;
3184 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0,
3185 &sglq_entry->phys);
3186 if (sglq_entry->virt == NULL) {
3187 kfree(sglq_entry);
3188 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3189 "2563 Failure to allocate an "
3190 "ELS mbuf:%d\n", i);
3191 rc = -ENOMEM;
3192 goto out_free_mem;
3193 }
3194 sglq_entry->sgl = sglq_entry->virt;
3195 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
3196 sglq_entry->state = SGL_FREED;
3197 list_add_tail(&sglq_entry->list, &els_sgl_list);
3198 }
James Smart38c20672013-03-01 16:37:44 -05003199 spin_lock_irq(&phba->hbalock);
James Smartdafe8ce2014-09-03 12:56:40 -04003200 spin_lock(&pring->ring_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003201 list_splice_init(&els_sgl_list, &phba->sli4_hba.lpfc_sgl_list);
James Smartdafe8ce2014-09-03 12:56:40 -04003202 spin_unlock(&pring->ring_lock);
James Smart38c20672013-03-01 16:37:44 -05003203 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003204 } else if (els_xri_cnt < phba->sli4_hba.els_xri_cnt) {
3205 /* els xri-sgl shrinked */
3206 xri_cnt = phba->sli4_hba.els_xri_cnt - els_xri_cnt;
3207 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3208 "3158 ELS xri-sgl count decreased from "
3209 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3210 els_xri_cnt);
3211 spin_lock_irq(&phba->hbalock);
James Smartdafe8ce2014-09-03 12:56:40 -04003212 spin_lock(&pring->ring_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003213 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &els_sgl_list);
James Smartdafe8ce2014-09-03 12:56:40 -04003214 spin_unlock(&pring->ring_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003215 spin_unlock_irq(&phba->hbalock);
3216 /* release extra els sgls from list */
3217 for (i = 0; i < xri_cnt; i++) {
3218 list_remove_head(&els_sgl_list,
3219 sglq_entry, struct lpfc_sglq, list);
3220 if (sglq_entry) {
3221 lpfc_mbuf_free(phba, sglq_entry->virt,
3222 sglq_entry->phys);
3223 kfree(sglq_entry);
3224 }
3225 }
3226 spin_lock_irq(&phba->hbalock);
James Smartdafe8ce2014-09-03 12:56:40 -04003227 spin_lock(&pring->ring_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003228 list_splice_init(&els_sgl_list, &phba->sli4_hba.lpfc_sgl_list);
James Smartdafe8ce2014-09-03 12:56:40 -04003229 spin_unlock(&pring->ring_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003230 spin_unlock_irq(&phba->hbalock);
3231 } else
3232 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3233 "3163 ELS xri-sgl count unchanged: %d\n",
3234 els_xri_cnt);
3235 phba->sli4_hba.els_xri_cnt = els_xri_cnt;
3236
3237 /* update xris to els sgls on the list */
3238 sglq_entry = NULL;
3239 sglq_entry_next = NULL;
3240 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
3241 &phba->sli4_hba.lpfc_sgl_list, list) {
3242 lxri = lpfc_sli4_next_xritag(phba);
3243 if (lxri == NO_XRI) {
3244 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3245 "2400 Failed to allocate xri for "
3246 "ELS sgl\n");
3247 rc = -ENOMEM;
3248 goto out_free_mem;
3249 }
3250 sglq_entry->sli4_lxritag = lxri;
3251 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3252 }
3253
3254 /*
3255 * update on pci function's allocated scsi xri-sgl list
3256 */
3257 phba->total_scsi_bufs = 0;
3258
3259 /* maximum number of xris available for scsi buffers */
3260 phba->sli4_hba.scsi_xri_max = phba->sli4_hba.max_cfg_param.max_xri -
3261 els_xri_cnt;
3262
3263 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3264 "2401 Current allocated SCSI xri-sgl count:%d, "
3265 "maximum SCSI xri count:%d\n",
3266 phba->sli4_hba.scsi_xri_cnt,
3267 phba->sli4_hba.scsi_xri_max);
3268
James Smarta40fc5f2013-04-17 20:17:40 -04003269 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart164cecd2013-09-06 12:22:38 -04003270 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003271 list_splice_init(&phba->lpfc_scsi_buf_list_get, &scsi_sgl_list);
3272 list_splice(&phba->lpfc_scsi_buf_list_put, &scsi_sgl_list);
James Smart164cecd2013-09-06 12:22:38 -04003273 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003274 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003275
3276 if (phba->sli4_hba.scsi_xri_cnt > phba->sli4_hba.scsi_xri_max) {
3277 /* max scsi xri shrinked below the allocated scsi buffers */
3278 scsi_xri_cnt = phba->sli4_hba.scsi_xri_cnt -
3279 phba->sli4_hba.scsi_xri_max;
3280 /* release the extra allocated scsi buffers */
3281 for (i = 0; i < scsi_xri_cnt; i++) {
3282 list_remove_head(&scsi_sgl_list, psb,
3283 struct lpfc_scsi_buf, list);
James Smarta2fc4aef2014-09-03 12:57:55 -04003284 if (psb) {
3285 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
3286 psb->data, psb->dma_handle);
3287 kfree(psb);
3288 }
James Smart8a9d2e82012-05-09 21:16:12 -04003289 }
James Smarta40fc5f2013-04-17 20:17:40 -04003290 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003291 phba->sli4_hba.scsi_xri_cnt -= scsi_xri_cnt;
James Smarta40fc5f2013-04-17 20:17:40 -04003292 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003293 }
3294
3295 /* update xris associated to remaining allocated scsi buffers */
3296 psb = NULL;
3297 psb_next = NULL;
3298 list_for_each_entry_safe(psb, psb_next, &scsi_sgl_list, list) {
3299 lxri = lpfc_sli4_next_xritag(phba);
3300 if (lxri == NO_XRI) {
3301 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3302 "2560 Failed to allocate xri for "
3303 "scsi buffer\n");
3304 rc = -ENOMEM;
3305 goto out_free_mem;
3306 }
3307 psb->cur_iocbq.sli4_lxritag = lxri;
3308 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3309 }
James Smarta40fc5f2013-04-17 20:17:40 -04003310 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart164cecd2013-09-06 12:22:38 -04003311 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003312 list_splice_init(&scsi_sgl_list, &phba->lpfc_scsi_buf_list_get);
3313 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
James Smart164cecd2013-09-06 12:22:38 -04003314 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003315 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003316
dea31012005-04-17 16:05:31 -05003317 return 0;
James Smart8a9d2e82012-05-09 21:16:12 -04003318
3319out_free_mem:
3320 lpfc_free_els_sgl_list(phba);
3321 lpfc_scsi_free(phba);
3322 return rc;
dea31012005-04-17 16:05:31 -05003323}
3324
James Smarte59058c2008-08-24 21:49:00 -04003325/**
James Smart3621a712009-04-06 18:47:14 -04003326 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04003327 * @phba: pointer to lpfc hba data structure.
3328 * @instance: a unique integer ID to this FC port.
3329 * @dev: pointer to the device data structure.
3330 *
3331 * This routine creates a FC port for the upper layer protocol. The FC port
3332 * can be created on top of either a physical port or a virtual port provided
3333 * by the HBA. This routine also allocates a SCSI host data structure (shost)
3334 * and associates the FC port created before adding the shost into the SCSI
3335 * layer.
3336 *
3337 * Return codes
3338 * @vport - pointer to the virtual N_Port data structure.
3339 * NULL - port create failed.
3340 **/
James Smart2e0fef82007-06-17 19:56:36 -05003341struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04003342lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04003343{
James Smart2e0fef82007-06-17 19:56:36 -05003344 struct lpfc_vport *vport;
3345 struct Scsi_Host *shost;
3346 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04003347
James Smartea4142f2015-04-07 15:07:13 -04003348 if (dev != &phba->pcidev->dev) {
James Smart3de2a652007-08-02 11:09:59 -04003349 shost = scsi_host_alloc(&lpfc_vport_template,
3350 sizeof(struct lpfc_vport));
James Smartea4142f2015-04-07 15:07:13 -04003351 } else {
3352 if (phba->sli_rev == LPFC_SLI_REV4)
3353 shost = scsi_host_alloc(&lpfc_template,
James Smart3de2a652007-08-02 11:09:59 -04003354 sizeof(struct lpfc_vport));
James Smartea4142f2015-04-07 15:07:13 -04003355 else
3356 shost = scsi_host_alloc(&lpfc_template_s3,
3357 sizeof(struct lpfc_vport));
3358 }
James Smart2e0fef82007-06-17 19:56:36 -05003359 if (!shost)
3360 goto out;
James Smart47a86172007-04-25 09:53:22 -04003361
James Smart2e0fef82007-06-17 19:56:36 -05003362 vport = (struct lpfc_vport *) shost->hostdata;
3363 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05003364 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05003365 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05003366 vport->fc_rscn_flush = 0;
James Smart47a86172007-04-25 09:53:22 -04003367
James Smart3de2a652007-08-02 11:09:59 -04003368 lpfc_get_vport_cfgparam(vport);
James Smart2e0fef82007-06-17 19:56:36 -05003369 shost->unique_id = instance;
3370 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04003371 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05003372 shost->this_id = -1;
3373 shost->max_cmd_len = 16;
James Smart8b0dff12015-05-22 10:42:38 -04003374 shost->nr_hw_queues = phba->cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04003375 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart28baac72010-02-12 14:42:03 -05003376 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04003377 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smartda0436e2009-05-22 14:51:39 -04003378 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
3379 }
James Smart81301a92008-12-04 22:39:46 -05003380
James Smart47a86172007-04-25 09:53:22 -04003381 /*
James Smart2e0fef82007-06-17 19:56:36 -05003382 * Set initial can_queue value since 0 is no longer supported and
3383 * scsi_add_host will fail. This will be adjusted later based on the
3384 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04003385 */
James Smart2e0fef82007-06-17 19:56:36 -05003386 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04003387 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05003388 shost->transportt = lpfc_vport_transport_template;
3389 vport->port_type = LPFC_NPIV_PORT;
3390 } else {
3391 shost->transportt = lpfc_transport_template;
3392 vport->port_type = LPFC_PHYSICAL_PORT;
3393 }
James Smart47a86172007-04-25 09:53:22 -04003394
James Smart2e0fef82007-06-17 19:56:36 -05003395 /* Initialize all internally managed lists. */
3396 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04003397 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05003398 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04003399
James Smart2e0fef82007-06-17 19:56:36 -05003400 init_timer(&vport->fc_disctmo);
3401 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05003402 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04003403
James Smart2e0fef82007-06-17 19:56:36 -05003404 init_timer(&vport->els_tmofunc);
3405 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05003406 vport->els_tmofunc.data = (unsigned long)vport;
James Smart92494142011-02-16 12:39:44 -05003407
3408 init_timer(&vport->delayed_disc_tmo);
3409 vport->delayed_disc_tmo.function = lpfc_delayed_disc_tmo;
3410 vport->delayed_disc_tmo.data = (unsigned long)vport;
3411
James Bottomleyd139b9b2009-11-05 13:33:12 -06003412 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05003413 if (error)
3414 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04003415
James Smart549e55c2007-08-02 11:09:51 -04003416 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003417 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04003418 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003419 return vport;
James Smart47a86172007-04-25 09:53:22 -04003420
James Smart2e0fef82007-06-17 19:56:36 -05003421out_put_shost:
3422 scsi_host_put(shost);
3423out:
3424 return NULL;
James Smart47a86172007-04-25 09:53:22 -04003425}
3426
James Smarte59058c2008-08-24 21:49:00 -04003427/**
James Smart3621a712009-04-06 18:47:14 -04003428 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04003429 * @vport: pointer to an lpfc virtual N_Port data structure.
3430 *
3431 * This routine destroys a FC port from the upper layer protocol. All the
3432 * resources associated with the port are released.
3433 **/
James Smart2e0fef82007-06-17 19:56:36 -05003434void
3435destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04003436{
James Smart92d7f7b2007-06-17 19:56:38 -05003437 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3438 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003439
James Smart858c9f62007-06-17 19:56:39 -05003440 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05003441 fc_remove_host(shost);
3442 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04003443
James Smart92d7f7b2007-06-17 19:56:38 -05003444 spin_lock_irq(&phba->hbalock);
3445 list_del_init(&vport->listentry);
3446 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04003447
James Smart92d7f7b2007-06-17 19:56:38 -05003448 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04003449 return;
James Smart47a86172007-04-25 09:53:22 -04003450}
3451
James Smarte59058c2008-08-24 21:49:00 -04003452/**
James Smart3621a712009-04-06 18:47:14 -04003453 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04003454 *
3455 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
3456 * uses the kernel idr facility to perform the task.
3457 *
3458 * Return codes:
3459 * instance - a unique integer ID allocated as the new instance.
3460 * -1 - lpfc get instance failed.
3461 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003462int
3463lpfc_get_instance(void)
3464{
Tejun Heoab516032013-02-27 17:04:44 -08003465 int ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003466
Tejun Heoab516032013-02-27 17:04:44 -08003467 ret = idr_alloc(&lpfc_hba_index, NULL, 0, 0, GFP_KERNEL);
3468 return ret < 0 ? -1 : ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003469}
3470
James Smarte59058c2008-08-24 21:49:00 -04003471/**
James Smart3621a712009-04-06 18:47:14 -04003472 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04003473 * @shost: pointer to SCSI host data structure.
3474 * @time: elapsed time of the scan in jiffies.
3475 *
3476 * This routine is called by the SCSI layer with a SCSI host to determine
3477 * whether the scan host is finished.
3478 *
3479 * Note: there is no scan_start function as adapter initialization will have
3480 * asynchronously kicked off the link initialization.
3481 *
3482 * Return codes
3483 * 0 - SCSI host scan is not over yet.
3484 * 1 - SCSI host scan is over.
3485 **/
James Smart47a86172007-04-25 09:53:22 -04003486int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
3487{
James Smart2e0fef82007-06-17 19:56:36 -05003488 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3489 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05003490 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04003491
James Smart858c9f62007-06-17 19:56:39 -05003492 spin_lock_irq(shost->host_lock);
3493
James Smart51ef4c22007-08-02 11:10:31 -04003494 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05003495 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003496 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05003497 }
James Smart256ec0d2013-04-17 20:14:58 -04003498 if (time >= msecs_to_jiffies(30 * 1000)) {
James Smart2e0fef82007-06-17 19:56:36 -05003499 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003500 "0461 Scanning longer than 30 "
3501 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003502 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003503 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003504 }
James Smart256ec0d2013-04-17 20:14:58 -04003505 if (time >= msecs_to_jiffies(15 * 1000) &&
3506 phba->link_state <= LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -05003507 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003508 "0465 Link down longer than 15 "
3509 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003510 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05003511 goto finished;
James Smart47a86172007-04-25 09:53:22 -04003512 }
3513
James Smart2e0fef82007-06-17 19:56:36 -05003514 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05003515 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003516 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05003517 goto finished;
James Smart256ec0d2013-04-17 20:14:58 -04003518 if (vport->fc_map_cnt == 0 && time < msecs_to_jiffies(2 * 1000))
James Smart858c9f62007-06-17 19:56:39 -05003519 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003520 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05003521 goto finished;
3522
3523 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003524
3525finished:
James Smart858c9f62007-06-17 19:56:39 -05003526 spin_unlock_irq(shost->host_lock);
3527 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05003528}
3529
James Smarte59058c2008-08-24 21:49:00 -04003530/**
James Smart3621a712009-04-06 18:47:14 -04003531 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04003532 * @shost: pointer to SCSI host data structure.
3533 *
3534 * This routine initializes a given SCSI host attributes on a FC port. The
3535 * SCSI host can be either on top of a physical port or a virtual port.
3536 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003537void lpfc_host_attrib_init(struct Scsi_Host *shost)
3538{
3539 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3540 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003541 /*
James Smart2e0fef82007-06-17 19:56:36 -05003542 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04003543 */
3544
James Smart2e0fef82007-06-17 19:56:36 -05003545 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
3546 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04003547 fc_host_supported_classes(shost) = FC_COS_CLASS3;
3548
3549 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003550 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003551 fc_host_supported_fc4s(shost)[2] = 1;
3552 fc_host_supported_fc4s(shost)[7] = 1;
3553
James Smart92d7f7b2007-06-17 19:56:38 -05003554 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
3555 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04003556
3557 fc_host_supported_speeds(shost) = 0;
James Smartd38dd522015-08-31 16:48:17 -04003558 if (phba->lmt & LMT_32Gb)
3559 fc_host_supported_speeds(shost) |= FC_PORTSPEED_32GBIT;
James Smart88a2cfb2011-07-22 18:36:33 -04003560 if (phba->lmt & LMT_16Gb)
3561 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
James Smart47a86172007-04-25 09:53:22 -04003562 if (phba->lmt & LMT_10Gb)
3563 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04003564 if (phba->lmt & LMT_8Gb)
3565 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04003566 if (phba->lmt & LMT_4Gb)
3567 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
3568 if (phba->lmt & LMT_2Gb)
3569 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
3570 if (phba->lmt & LMT_1Gb)
3571 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
3572
3573 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05003574 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
3575 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04003576
Mike Christie0af5d702010-09-15 16:52:31 -05003577 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
3578
James Smart47a86172007-04-25 09:53:22 -04003579 /* This value is also unchanging */
3580 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003581 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003582 fc_host_active_fc4s(shost)[2] = 1;
3583 fc_host_active_fc4s(shost)[7] = 1;
3584
James Smart92d7f7b2007-06-17 19:56:38 -05003585 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04003586 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04003587 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04003588 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04003589}
dea31012005-04-17 16:05:31 -05003590
James Smarte59058c2008-08-24 21:49:00 -04003591/**
James Smartda0436e2009-05-22 14:51:39 -04003592 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04003593 * @phba: pointer to lpfc hba data structure.
3594 *
James Smartda0436e2009-05-22 14:51:39 -04003595 * This routine is invoked to stop an SLI3 device port, it stops the device
3596 * from generating interrupts and stops the device driver's timers for the
3597 * device.
James Smarte59058c2008-08-24 21:49:00 -04003598 **/
James Smartdb2378e2008-02-08 18:49:51 -05003599static void
James Smartda0436e2009-05-22 14:51:39 -04003600lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05003601{
James Smartda0436e2009-05-22 14:51:39 -04003602 /* Clear all interrupt enable conditions */
3603 writel(0, phba->HCregaddr);
3604 readl(phba->HCregaddr); /* flush */
3605 /* Clear all pending interrupts */
3606 writel(0xffffffff, phba->HAregaddr);
3607 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04003608
James Smartda0436e2009-05-22 14:51:39 -04003609 /* Reset some HBA SLI setup states */
3610 lpfc_stop_hba_timers(phba);
3611 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05003612}
3613
James Smarte59058c2008-08-24 21:49:00 -04003614/**
James Smartda0436e2009-05-22 14:51:39 -04003615 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05003616 * @phba: pointer to lpfc hba data structure.
3617 *
James Smartda0436e2009-05-22 14:51:39 -04003618 * This routine is invoked to stop an SLI4 device port, it stops the device
3619 * from generating interrupts and stops the device driver's timers for the
3620 * device.
3621 **/
3622static void
3623lpfc_stop_port_s4(struct lpfc_hba *phba)
3624{
3625 /* Reset some HBA SLI4 setup states */
3626 lpfc_stop_hba_timers(phba);
3627 phba->pport->work_port_events = 0;
3628 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04003629}
3630
3631/**
3632 * lpfc_stop_port - Wrapper function for stopping hba port
3633 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05003634 *
James Smartda0436e2009-05-22 14:51:39 -04003635 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
3636 * the API jump table function pointer from the lpfc_hba struct.
3637 **/
3638void
3639lpfc_stop_port(struct lpfc_hba *phba)
3640{
3641 phba->lpfc_stop_port(phba);
3642}
3643
3644/**
James Smartecfd03c2010-02-12 14:41:27 -05003645 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
3646 * @phba: Pointer to hba for which this call is being executed.
3647 *
3648 * This routine starts the timer waiting for the FCF rediscovery to complete.
3649 **/
3650void
3651lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
3652{
3653 unsigned long fcf_redisc_wait_tmo =
3654 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
3655 /* Start fcf rediscovery wait period timer */
3656 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
3657 spin_lock_irq(&phba->hbalock);
3658 /* Allow action to new fcf asynchronous event */
3659 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
3660 /* Mark the FCF rediscovery pending state */
3661 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
3662 spin_unlock_irq(&phba->hbalock);
3663}
3664
3665/**
3666 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
3667 * @ptr: Map to lpfc_hba data structure pointer.
3668 *
3669 * This routine is invoked when waiting for FCF table rediscover has been
3670 * timed out. If new FCF record(s) has (have) been discovered during the
3671 * wait period, a new FCF event shall be added to the FCOE async event
3672 * list, and then worker thread shall be waked up for processing from the
3673 * worker thread context.
3674 **/
Rashika Kheriae399b222014-09-03 12:55:28 -04003675static void
James Smartecfd03c2010-02-12 14:41:27 -05003676lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
3677{
3678 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
3679
3680 /* Don't send FCF rediscovery event if timer cancelled */
3681 spin_lock_irq(&phba->hbalock);
3682 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
3683 spin_unlock_irq(&phba->hbalock);
3684 return;
3685 }
3686 /* Clear FCF rediscovery timer pending flag */
3687 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
3688 /* FCF rediscovery event to worker thread */
3689 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
3690 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003691 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04003692 "2776 FCF rediscover quiescent timer expired\n");
James Smartecfd03c2010-02-12 14:41:27 -05003693 /* wake up worker thread */
3694 lpfc_worker_wake_up(phba);
3695}
3696
3697/**
James Smartda0436e2009-05-22 14:51:39 -04003698 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
3699 * @phba: pointer to lpfc hba data structure.
3700 * @acqe_link: pointer to the async link completion queue entry.
3701 *
3702 * This routine is to parse the SLI4 link-attention link fault code and
3703 * translate it into the base driver's read link attention mailbox command
3704 * status.
3705 *
3706 * Return: Link-attention status in terms of base driver's coding.
3707 **/
3708static uint16_t
3709lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
3710 struct lpfc_acqe_link *acqe_link)
3711{
3712 uint16_t latt_fault;
3713
3714 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
3715 case LPFC_ASYNC_LINK_FAULT_NONE:
3716 case LPFC_ASYNC_LINK_FAULT_LOCAL:
3717 case LPFC_ASYNC_LINK_FAULT_REMOTE:
3718 latt_fault = 0;
3719 break;
3720 default:
3721 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3722 "0398 Invalid link fault code: x%x\n",
3723 bf_get(lpfc_acqe_link_fault, acqe_link));
3724 latt_fault = MBXERR_ERROR;
3725 break;
3726 }
3727 return latt_fault;
3728}
3729
3730/**
3731 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
3732 * @phba: pointer to lpfc hba data structure.
3733 * @acqe_link: pointer to the async link completion queue entry.
3734 *
3735 * This routine is to parse the SLI4 link attention type and translate it
3736 * into the base driver's link attention type coding.
3737 *
3738 * Return: Link attention type in terms of base driver's coding.
3739 **/
3740static uint8_t
3741lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
3742 struct lpfc_acqe_link *acqe_link)
3743{
3744 uint8_t att_type;
3745
3746 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
3747 case LPFC_ASYNC_LINK_STATUS_DOWN:
3748 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
James Smart76a95d72010-11-20 23:11:48 -05003749 att_type = LPFC_ATT_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04003750 break;
3751 case LPFC_ASYNC_LINK_STATUS_UP:
3752 /* Ignore physical link up events - wait for logical link up */
James Smart76a95d72010-11-20 23:11:48 -05003753 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003754 break;
3755 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
James Smart76a95d72010-11-20 23:11:48 -05003756 att_type = LPFC_ATT_LINK_UP;
James Smartda0436e2009-05-22 14:51:39 -04003757 break;
3758 default:
3759 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3760 "0399 Invalid link attention type: x%x\n",
3761 bf_get(lpfc_acqe_link_status, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003762 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003763 break;
3764 }
3765 return att_type;
3766}
3767
3768/**
James Smart8b68cd52012-09-29 11:32:37 -04003769 * lpfc_sli_port_speed_get - Get sli3 link speed code to link speed
3770 * @phba: pointer to lpfc hba data structure.
3771 *
3772 * This routine is to get an SLI3 FC port's link speed in Mbps.
3773 *
3774 * Return: link speed in terms of Mbps.
3775 **/
3776uint32_t
3777lpfc_sli_port_speed_get(struct lpfc_hba *phba)
3778{
3779 uint32_t link_speed;
3780
3781 if (!lpfc_is_link_up(phba))
3782 return 0;
3783
James Smarta085e872015-12-16 18:12:02 -05003784 if (phba->sli_rev <= LPFC_SLI_REV3) {
3785 switch (phba->fc_linkspeed) {
3786 case LPFC_LINK_SPEED_1GHZ:
3787 link_speed = 1000;
3788 break;
3789 case LPFC_LINK_SPEED_2GHZ:
3790 link_speed = 2000;
3791 break;
3792 case LPFC_LINK_SPEED_4GHZ:
3793 link_speed = 4000;
3794 break;
3795 case LPFC_LINK_SPEED_8GHZ:
3796 link_speed = 8000;
3797 break;
3798 case LPFC_LINK_SPEED_10GHZ:
3799 link_speed = 10000;
3800 break;
3801 case LPFC_LINK_SPEED_16GHZ:
3802 link_speed = 16000;
3803 break;
3804 default:
3805 link_speed = 0;
3806 }
3807 } else {
3808 if (phba->sli4_hba.link_state.logical_speed)
3809 link_speed =
3810 phba->sli4_hba.link_state.logical_speed;
3811 else
3812 link_speed = phba->sli4_hba.link_state.speed;
James Smart8b68cd52012-09-29 11:32:37 -04003813 }
3814 return link_speed;
3815}
3816
3817/**
3818 * lpfc_sli4_port_speed_parse - Parse async evt link speed code to link speed
3819 * @phba: pointer to lpfc hba data structure.
3820 * @evt_code: asynchronous event code.
3821 * @speed_code: asynchronous event link speed code.
3822 *
3823 * This routine is to parse the giving SLI4 async event link speed code into
3824 * value of Mbps for the link speed.
3825 *
3826 * Return: link speed in terms of Mbps.
3827 **/
3828static uint32_t
3829lpfc_sli4_port_speed_parse(struct lpfc_hba *phba, uint32_t evt_code,
3830 uint8_t speed_code)
3831{
3832 uint32_t port_speed;
3833
3834 switch (evt_code) {
3835 case LPFC_TRAILER_CODE_LINK:
3836 switch (speed_code) {
James Smart26d830e2015-04-07 15:07:17 -04003837 case LPFC_ASYNC_LINK_SPEED_ZERO:
James Smart8b68cd52012-09-29 11:32:37 -04003838 port_speed = 0;
3839 break;
James Smart26d830e2015-04-07 15:07:17 -04003840 case LPFC_ASYNC_LINK_SPEED_10MBPS:
James Smart8b68cd52012-09-29 11:32:37 -04003841 port_speed = 10;
3842 break;
James Smart26d830e2015-04-07 15:07:17 -04003843 case LPFC_ASYNC_LINK_SPEED_100MBPS:
James Smart8b68cd52012-09-29 11:32:37 -04003844 port_speed = 100;
3845 break;
James Smart26d830e2015-04-07 15:07:17 -04003846 case LPFC_ASYNC_LINK_SPEED_1GBPS:
James Smart8b68cd52012-09-29 11:32:37 -04003847 port_speed = 1000;
3848 break;
James Smart26d830e2015-04-07 15:07:17 -04003849 case LPFC_ASYNC_LINK_SPEED_10GBPS:
James Smart8b68cd52012-09-29 11:32:37 -04003850 port_speed = 10000;
3851 break;
James Smart26d830e2015-04-07 15:07:17 -04003852 case LPFC_ASYNC_LINK_SPEED_20GBPS:
3853 port_speed = 20000;
3854 break;
3855 case LPFC_ASYNC_LINK_SPEED_25GBPS:
3856 port_speed = 25000;
3857 break;
3858 case LPFC_ASYNC_LINK_SPEED_40GBPS:
3859 port_speed = 40000;
3860 break;
James Smart8b68cd52012-09-29 11:32:37 -04003861 default:
3862 port_speed = 0;
3863 }
3864 break;
3865 case LPFC_TRAILER_CODE_FC:
3866 switch (speed_code) {
James Smart26d830e2015-04-07 15:07:17 -04003867 case LPFC_FC_LA_SPEED_UNKNOWN:
James Smart8b68cd52012-09-29 11:32:37 -04003868 port_speed = 0;
3869 break;
James Smart26d830e2015-04-07 15:07:17 -04003870 case LPFC_FC_LA_SPEED_1G:
James Smart8b68cd52012-09-29 11:32:37 -04003871 port_speed = 1000;
3872 break;
James Smart26d830e2015-04-07 15:07:17 -04003873 case LPFC_FC_LA_SPEED_2G:
James Smart8b68cd52012-09-29 11:32:37 -04003874 port_speed = 2000;
3875 break;
James Smart26d830e2015-04-07 15:07:17 -04003876 case LPFC_FC_LA_SPEED_4G:
James Smart8b68cd52012-09-29 11:32:37 -04003877 port_speed = 4000;
3878 break;
James Smart26d830e2015-04-07 15:07:17 -04003879 case LPFC_FC_LA_SPEED_8G:
James Smart8b68cd52012-09-29 11:32:37 -04003880 port_speed = 8000;
3881 break;
James Smart26d830e2015-04-07 15:07:17 -04003882 case LPFC_FC_LA_SPEED_10G:
James Smart8b68cd52012-09-29 11:32:37 -04003883 port_speed = 10000;
3884 break;
James Smart26d830e2015-04-07 15:07:17 -04003885 case LPFC_FC_LA_SPEED_16G:
James Smart8b68cd52012-09-29 11:32:37 -04003886 port_speed = 16000;
3887 break;
James Smartd38dd522015-08-31 16:48:17 -04003888 case LPFC_FC_LA_SPEED_32G:
3889 port_speed = 32000;
3890 break;
James Smart8b68cd52012-09-29 11:32:37 -04003891 default:
3892 port_speed = 0;
3893 }
3894 break;
3895 default:
3896 port_speed = 0;
3897 }
3898 return port_speed;
3899}
3900
3901/**
James Smart70f3c072010-12-15 17:57:33 -05003902 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
James Smartda0436e2009-05-22 14:51:39 -04003903 * @phba: pointer to lpfc hba data structure.
3904 * @acqe_link: pointer to the async link completion queue entry.
3905 *
James Smart70f3c072010-12-15 17:57:33 -05003906 * This routine is to handle the SLI4 asynchronous FCoE link event.
James Smartda0436e2009-05-22 14:51:39 -04003907 **/
3908static void
3909lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3910 struct lpfc_acqe_link *acqe_link)
3911{
3912 struct lpfc_dmabuf *mp;
3913 LPFC_MBOXQ_t *pmb;
3914 MAILBOX_t *mb;
James Smart76a95d72010-11-20 23:11:48 -05003915 struct lpfc_mbx_read_top *la;
James Smartda0436e2009-05-22 14:51:39 -04003916 uint8_t att_type;
James Smart76a95d72010-11-20 23:11:48 -05003917 int rc;
James Smartda0436e2009-05-22 14:51:39 -04003918
3919 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05003920 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
James Smartda0436e2009-05-22 14:51:39 -04003921 return;
James Smart32b97932009-07-19 10:01:21 -04003922 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003923 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3924 if (!pmb) {
3925 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3926 "0395 The mboxq allocation failed\n");
3927 return;
3928 }
3929 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3930 if (!mp) {
3931 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3932 "0396 The lpfc_dmabuf allocation failed\n");
3933 goto out_free_pmb;
3934 }
3935 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3936 if (!mp->virt) {
3937 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3938 "0397 The mbuf allocation failed\n");
3939 goto out_free_dmabuf;
3940 }
3941
3942 /* Cleanup any outstanding ELS commands */
3943 lpfc_els_flush_all_cmd(phba);
3944
3945 /* Block ELS IOCBs until we have done process link event */
3946 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3947
3948 /* Update link event statistics */
3949 phba->sli.slistat.link_event++;
3950
James Smart76a95d72010-11-20 23:11:48 -05003951 /* Create lpfc_handle_latt mailbox command from link ACQE */
3952 lpfc_read_topology(phba, pmb, mp);
3953 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smartda0436e2009-05-22 14:51:39 -04003954 pmb->vport = phba->pport;
3955
James Smartda0436e2009-05-22 14:51:39 -04003956 /* Keep the link status for extra SLI4 state machine reference */
3957 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04003958 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_LINK,
3959 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smartda0436e2009-05-22 14:51:39 -04003960 phba->sli4_hba.link_state.duplex =
3961 bf_get(lpfc_acqe_link_duplex, acqe_link);
3962 phba->sli4_hba.link_state.status =
3963 bf_get(lpfc_acqe_link_status, acqe_link);
James Smart70f3c072010-12-15 17:57:33 -05003964 phba->sli4_hba.link_state.type =
3965 bf_get(lpfc_acqe_link_type, acqe_link);
3966 phba->sli4_hba.link_state.number =
3967 bf_get(lpfc_acqe_link_number, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04003968 phba->sli4_hba.link_state.fault =
3969 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05003970 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04003971 bf_get(lpfc_acqe_logical_link_speed, acqe_link) * 10;
3972
James Smart70f3c072010-12-15 17:57:33 -05003973 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartc31098c2011-04-16 11:03:33 -04003974 "2900 Async FC/FCoE Link event - Speed:%dGBit "
3975 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
3976 "Logical speed:%dMbps Fault:%d\n",
James Smart70f3c072010-12-15 17:57:33 -05003977 phba->sli4_hba.link_state.speed,
3978 phba->sli4_hba.link_state.topology,
3979 phba->sli4_hba.link_state.status,
3980 phba->sli4_hba.link_state.type,
3981 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04003982 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05003983 phba->sli4_hba.link_state.fault);
James Smart76a95d72010-11-20 23:11:48 -05003984 /*
3985 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
3986 * topology info. Note: Optional for non FC-AL ports.
3987 */
3988 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3989 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3990 if (rc == MBX_NOT_FINISHED)
3991 goto out_free_dmabuf;
3992 return;
3993 }
3994 /*
3995 * For FCoE Mode: fill in all the topology information we need and call
3996 * the READ_TOPOLOGY completion routine to continue without actually
3997 * sending the READ_TOPOLOGY mailbox command to the port.
3998 */
3999 /* Parse and translate status field */
4000 mb = &pmb->u.mb;
4001 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
4002
4003 /* Parse and translate link attention fields */
4004 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
4005 la->eventTag = acqe_link->event_tag;
4006 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
4007 bf_set(lpfc_mbx_read_top_link_spd, la,
James Smarta085e872015-12-16 18:12:02 -05004008 (bf_get(lpfc_acqe_link_speed, acqe_link)));
James Smart76a95d72010-11-20 23:11:48 -05004009
4010 /* Fake the the following irrelvant fields */
4011 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
4012 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
4013 bf_set(lpfc_mbx_read_top_il, la, 0);
4014 bf_set(lpfc_mbx_read_top_pb, la, 0);
4015 bf_set(lpfc_mbx_read_top_fa, la, 0);
4016 bf_set(lpfc_mbx_read_top_mm, la, 0);
James Smartda0436e2009-05-22 14:51:39 -04004017
4018 /* Invoke the lpfc_handle_latt mailbox command callback function */
James Smart76a95d72010-11-20 23:11:48 -05004019 lpfc_mbx_cmpl_read_topology(phba, pmb);
James Smartda0436e2009-05-22 14:51:39 -04004020
4021 return;
4022
4023out_free_dmabuf:
4024 kfree(mp);
4025out_free_pmb:
4026 mempool_free(pmb, phba->mbox_mem_pool);
4027}
4028
4029/**
James Smart70f3c072010-12-15 17:57:33 -05004030 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
4031 * @phba: pointer to lpfc hba data structure.
4032 * @acqe_fc: pointer to the async fc completion queue entry.
4033 *
4034 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
4035 * that the event was received and then issue a read_topology mailbox command so
4036 * that the rest of the driver will treat it the same as SLI3.
4037 **/
4038static void
4039lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
4040{
4041 struct lpfc_dmabuf *mp;
4042 LPFC_MBOXQ_t *pmb;
James Smart7bdedb32016-07-06 12:36:00 -07004043 MAILBOX_t *mb;
4044 struct lpfc_mbx_read_top *la;
James Smart70f3c072010-12-15 17:57:33 -05004045 int rc;
4046
4047 if (bf_get(lpfc_trailer_type, acqe_fc) !=
4048 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
4049 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4050 "2895 Non FC link Event detected.(%d)\n",
4051 bf_get(lpfc_trailer_type, acqe_fc));
4052 return;
4053 }
4054 /* Keep the link status for extra SLI4 state machine reference */
4055 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04004056 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
4057 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
James Smart70f3c072010-12-15 17:57:33 -05004058 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
4059 phba->sli4_hba.link_state.topology =
4060 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
4061 phba->sli4_hba.link_state.status =
4062 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
4063 phba->sli4_hba.link_state.type =
4064 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
4065 phba->sli4_hba.link_state.number =
4066 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
4067 phba->sli4_hba.link_state.fault =
4068 bf_get(lpfc_acqe_link_fault, acqe_fc);
4069 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04004070 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
James Smart70f3c072010-12-15 17:57:33 -05004071 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4072 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
4073 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
4074 "%dMbps Fault:%d\n",
4075 phba->sli4_hba.link_state.speed,
4076 phba->sli4_hba.link_state.topology,
4077 phba->sli4_hba.link_state.status,
4078 phba->sli4_hba.link_state.type,
4079 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04004080 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05004081 phba->sli4_hba.link_state.fault);
4082 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4083 if (!pmb) {
4084 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4085 "2897 The mboxq allocation failed\n");
4086 return;
4087 }
4088 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4089 if (!mp) {
4090 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4091 "2898 The lpfc_dmabuf allocation failed\n");
4092 goto out_free_pmb;
4093 }
4094 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
4095 if (!mp->virt) {
4096 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4097 "2899 The mbuf allocation failed\n");
4098 goto out_free_dmabuf;
4099 }
4100
4101 /* Cleanup any outstanding ELS commands */
4102 lpfc_els_flush_all_cmd(phba);
4103
4104 /* Block ELS IOCBs until we have done process link event */
4105 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
4106
4107 /* Update link event statistics */
4108 phba->sli.slistat.link_event++;
4109
4110 /* Create lpfc_handle_latt mailbox command from link ACQE */
4111 lpfc_read_topology(phba, pmb, mp);
4112 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
4113 pmb->vport = phba->pport;
4114
James Smart7bdedb32016-07-06 12:36:00 -07004115 if (phba->sli4_hba.link_state.status != LPFC_FC_LA_TYPE_LINK_UP) {
4116 /* Parse and translate status field */
4117 mb = &pmb->u.mb;
4118 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba,
4119 (void *)acqe_fc);
4120
4121 /* Parse and translate link attention fields */
4122 la = (struct lpfc_mbx_read_top *)&pmb->u.mb.un.varReadTop;
4123 la->eventTag = acqe_fc->event_tag;
4124 bf_set(lpfc_mbx_read_top_att_type, la,
4125 LPFC_FC_LA_TYPE_LINK_DOWN);
4126
4127 /* Invoke the mailbox command callback function */
4128 lpfc_mbx_cmpl_read_topology(phba, pmb);
4129
4130 return;
4131 }
4132
James Smart70f3c072010-12-15 17:57:33 -05004133 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4134 if (rc == MBX_NOT_FINISHED)
4135 goto out_free_dmabuf;
4136 return;
4137
4138out_free_dmabuf:
4139 kfree(mp);
4140out_free_pmb:
4141 mempool_free(pmb, phba->mbox_mem_pool);
4142}
4143
4144/**
4145 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
4146 * @phba: pointer to lpfc hba data structure.
4147 * @acqe_fc: pointer to the async SLI completion queue entry.
4148 *
4149 * This routine is to handle the SLI4 asynchronous SLI events.
4150 **/
4151static void
4152lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
4153{
James Smart4b8bae02012-06-12 13:55:07 -04004154 char port_name;
James Smart8c1312e2012-10-31 14:45:09 -04004155 char message[128];
James Smart4b8bae02012-06-12 13:55:07 -04004156 uint8_t status;
James Smart946727d2015-04-07 15:07:09 -04004157 uint8_t evt_type;
James Smart448193b2015-12-16 18:12:05 -05004158 uint8_t operational = 0;
James Smart946727d2015-04-07 15:07:09 -04004159 struct temp_event temp_event_data;
James Smart4b8bae02012-06-12 13:55:07 -04004160 struct lpfc_acqe_misconfigured_event *misconfigured;
James Smart946727d2015-04-07 15:07:09 -04004161 struct Scsi_Host *shost;
James Smart4b8bae02012-06-12 13:55:07 -04004162
James Smart946727d2015-04-07 15:07:09 -04004163 evt_type = bf_get(lpfc_trailer_type, acqe_sli);
4164
James Smart448193b2015-12-16 18:12:05 -05004165 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4166 "2901 Async SLI event - Event Data1:x%08x Event Data2:"
4167 "x%08x SLI Event Type:%d\n",
4168 acqe_sli->event_data1, acqe_sli->event_data2,
4169 evt_type);
James Smart4b8bae02012-06-12 13:55:07 -04004170
4171 port_name = phba->Port[0];
4172 if (port_name == 0x00)
4173 port_name = '?'; /* get port name is empty */
4174
James Smart946727d2015-04-07 15:07:09 -04004175 switch (evt_type) {
4176 case LPFC_SLI_EVENT_TYPE_OVER_TEMP:
4177 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
4178 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
4179 temp_event_data.data = (uint32_t)acqe_sli->event_data1;
4180
4181 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4182 "3190 Over Temperature:%d Celsius- Port Name %c\n",
4183 acqe_sli->event_data1, port_name);
4184
James Smart310429e2016-07-06 12:35:54 -07004185 phba->sfp_warning |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
James Smart946727d2015-04-07 15:07:09 -04004186 shost = lpfc_shost_from_vport(phba->pport);
4187 fc_host_post_vendor_event(shost, fc_get_event_number(),
4188 sizeof(temp_event_data),
4189 (char *)&temp_event_data,
4190 SCSI_NL_VID_TYPE_PCI
4191 | PCI_VENDOR_ID_EMULEX);
4192 break;
4193 case LPFC_SLI_EVENT_TYPE_NORM_TEMP:
4194 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
4195 temp_event_data.event_code = LPFC_NORMAL_TEMP;
4196 temp_event_data.data = (uint32_t)acqe_sli->event_data1;
4197
4198 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4199 "3191 Normal Temperature:%d Celsius - Port Name %c\n",
4200 acqe_sli->event_data1, port_name);
4201
4202 shost = lpfc_shost_from_vport(phba->pport);
4203 fc_host_post_vendor_event(shost, fc_get_event_number(),
4204 sizeof(temp_event_data),
4205 (char *)&temp_event_data,
4206 SCSI_NL_VID_TYPE_PCI
4207 | PCI_VENDOR_ID_EMULEX);
4208 break;
4209 case LPFC_SLI_EVENT_TYPE_MISCONFIGURED:
4210 misconfigured = (struct lpfc_acqe_misconfigured_event *)
James Smart4b8bae02012-06-12 13:55:07 -04004211 &acqe_sli->event_data1;
4212
James Smart946727d2015-04-07 15:07:09 -04004213 /* fetch the status for this port */
4214 switch (phba->sli4_hba.lnk_info.lnk_no) {
4215 case LPFC_LINK_NUMBER_0:
James Smart448193b2015-12-16 18:12:05 -05004216 status = bf_get(lpfc_sli_misconfigured_port0_state,
4217 &misconfigured->theEvent);
4218 operational = bf_get(lpfc_sli_misconfigured_port0_op,
James Smart4b8bae02012-06-12 13:55:07 -04004219 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04004220 break;
4221 case LPFC_LINK_NUMBER_1:
James Smart448193b2015-12-16 18:12:05 -05004222 status = bf_get(lpfc_sli_misconfigured_port1_state,
4223 &misconfigured->theEvent);
4224 operational = bf_get(lpfc_sli_misconfigured_port1_op,
James Smart4b8bae02012-06-12 13:55:07 -04004225 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04004226 break;
4227 case LPFC_LINK_NUMBER_2:
James Smart448193b2015-12-16 18:12:05 -05004228 status = bf_get(lpfc_sli_misconfigured_port2_state,
4229 &misconfigured->theEvent);
4230 operational = bf_get(lpfc_sli_misconfigured_port2_op,
James Smart4b8bae02012-06-12 13:55:07 -04004231 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04004232 break;
4233 case LPFC_LINK_NUMBER_3:
James Smart448193b2015-12-16 18:12:05 -05004234 status = bf_get(lpfc_sli_misconfigured_port3_state,
4235 &misconfigured->theEvent);
4236 operational = bf_get(lpfc_sli_misconfigured_port3_op,
James Smart4b8bae02012-06-12 13:55:07 -04004237 &misconfigured->theEvent);
James Smart946727d2015-04-07 15:07:09 -04004238 break;
4239 default:
James Smart448193b2015-12-16 18:12:05 -05004240 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4241 "3296 "
4242 "LPFC_SLI_EVENT_TYPE_MISCONFIGURED "
4243 "event: Invalid link %d",
4244 phba->sli4_hba.lnk_info.lnk_no);
4245 return;
James Smart946727d2015-04-07 15:07:09 -04004246 }
James Smart4b8bae02012-06-12 13:55:07 -04004247
James Smart448193b2015-12-16 18:12:05 -05004248 /* Skip if optic state unchanged */
4249 if (phba->sli4_hba.lnk_info.optic_state == status)
4250 return;
4251
James Smart946727d2015-04-07 15:07:09 -04004252 switch (status) {
4253 case LPFC_SLI_EVENT_STATUS_VALID:
James Smart448193b2015-12-16 18:12:05 -05004254 sprintf(message, "Physical Link is functional");
4255 break;
James Smart946727d2015-04-07 15:07:09 -04004256 case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
4257 sprintf(message, "Optics faulted/incorrectly "
4258 "installed/not installed - Reseat optics, "
4259 "if issue not resolved, replace.");
4260 break;
4261 case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
4262 sprintf(message,
4263 "Optics of two types installed - Remove one "
4264 "optic or install matching pair of optics.");
4265 break;
4266 case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
4267 sprintf(message, "Incompatible optics - Replace with "
James Smart292098b2012-09-29 11:31:41 -04004268 "compatible optics for card to function.");
James Smart946727d2015-04-07 15:07:09 -04004269 break;
James Smart448193b2015-12-16 18:12:05 -05004270 case LPFC_SLI_EVENT_STATUS_UNQUALIFIED:
4271 sprintf(message, "Unqualified optics - Replace with "
4272 "Avago optics for Warranty and Technical "
4273 "Support - Link is%s operational",
4274 (operational) ? "" : " not");
4275 break;
4276 case LPFC_SLI_EVENT_STATUS_UNCERTIFIED:
4277 sprintf(message, "Uncertified optics - Replace with "
4278 "Avago-certified optics to enable link "
4279 "operation - Link is%s operational",
4280 (operational) ? "" : " not");
4281 break;
James Smart946727d2015-04-07 15:07:09 -04004282 default:
4283 /* firmware is reporting a status we don't know about */
4284 sprintf(message, "Unknown event status x%02x", status);
4285 break;
4286 }
James Smart448193b2015-12-16 18:12:05 -05004287 phba->sli4_hba.lnk_info.optic_state = status;
James Smart946727d2015-04-07 15:07:09 -04004288 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart448193b2015-12-16 18:12:05 -05004289 "3176 Port Name %c %s\n", port_name, message);
James Smart946727d2015-04-07 15:07:09 -04004290 break;
4291 case LPFC_SLI_EVENT_TYPE_REMOTE_DPORT:
4292 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4293 "3192 Remote DPort Test Initiated - "
4294 "Event Data1:x%08x Event Data2: x%08x\n",
4295 acqe_sli->event_data1, acqe_sli->event_data2);
James Smart4b8bae02012-06-12 13:55:07 -04004296 break;
4297 default:
James Smart946727d2015-04-07 15:07:09 -04004298 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4299 "3193 Async SLI event - Event Data1:x%08x Event Data2:"
4300 "x%08x SLI Event Type:%d\n",
4301 acqe_sli->event_data1, acqe_sli->event_data2,
4302 evt_type);
James Smart4b8bae02012-06-12 13:55:07 -04004303 break;
4304 }
James Smart70f3c072010-12-15 17:57:33 -05004305}
4306
4307/**
James Smartfc2b9892010-02-26 14:15:29 -05004308 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
4309 * @vport: pointer to vport data structure.
4310 *
4311 * This routine is to perform Clear Virtual Link (CVL) on a vport in
4312 * response to a CVL event.
4313 *
4314 * Return the pointer to the ndlp with the vport if successful, otherwise
4315 * return NULL.
4316 **/
4317static struct lpfc_nodelist *
4318lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
4319{
4320 struct lpfc_nodelist *ndlp;
4321 struct Scsi_Host *shost;
4322 struct lpfc_hba *phba;
4323
4324 if (!vport)
4325 return NULL;
James Smartfc2b9892010-02-26 14:15:29 -05004326 phba = vport->phba;
4327 if (!phba)
4328 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04004329 ndlp = lpfc_findnode_did(vport, Fabric_DID);
4330 if (!ndlp) {
4331 /* Cannot find existing Fabric ndlp, so allocate a new one */
4332 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
4333 if (!ndlp)
4334 return 0;
4335 lpfc_nlp_init(vport, ndlp, Fabric_DID);
4336 /* Set the node type */
4337 ndlp->nlp_type |= NLP_FABRIC;
4338 /* Put ndlp onto node list */
4339 lpfc_enqueue_node(vport, ndlp);
4340 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
4341 /* re-setup ndlp without removing from node list */
4342 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
4343 if (!ndlp)
4344 return 0;
4345 }
James Smart63e801c2010-11-20 23:14:19 -05004346 if ((phba->pport->port_state < LPFC_FLOGI) &&
4347 (phba->pport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05004348 return NULL;
4349 /* If virtual link is not yet instantiated ignore CVL */
James Smart63e801c2010-11-20 23:14:19 -05004350 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
4351 && (vport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05004352 return NULL;
4353 shost = lpfc_shost_from_vport(vport);
4354 if (!shost)
4355 return NULL;
4356 lpfc_linkdown_port(vport);
4357 lpfc_cleanup_pending_mbox(vport);
4358 spin_lock_irq(shost->host_lock);
4359 vport->fc_flag |= FC_VPORT_CVL_RCVD;
4360 spin_unlock_irq(shost->host_lock);
4361
4362 return ndlp;
4363}
4364
4365/**
4366 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
4367 * @vport: pointer to lpfc hba data structure.
4368 *
4369 * This routine is to perform Clear Virtual Link (CVL) on all vports in
4370 * response to a FCF dead event.
4371 **/
4372static void
4373lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
4374{
4375 struct lpfc_vport **vports;
4376 int i;
4377
4378 vports = lpfc_create_vport_work_array(phba);
4379 if (vports)
4380 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
4381 lpfc_sli4_perform_vport_cvl(vports[i]);
4382 lpfc_destroy_vport_work_array(phba, vports);
4383}
4384
4385/**
James Smart76a95d72010-11-20 23:11:48 -05004386 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
James Smartda0436e2009-05-22 14:51:39 -04004387 * @phba: pointer to lpfc hba data structure.
4388 * @acqe_link: pointer to the async fcoe completion queue entry.
4389 *
4390 * This routine is to handle the SLI4 asynchronous fcoe event.
4391 **/
4392static void
James Smart76a95d72010-11-20 23:11:48 -05004393lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
James Smart70f3c072010-12-15 17:57:33 -05004394 struct lpfc_acqe_fip *acqe_fip)
James Smartda0436e2009-05-22 14:51:39 -04004395{
James Smart70f3c072010-12-15 17:57:33 -05004396 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004397 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04004398 struct lpfc_vport *vport;
4399 struct lpfc_nodelist *ndlp;
4400 struct Scsi_Host *shost;
James Smart695a8142010-01-26 23:08:03 -05004401 int active_vlink_present;
4402 struct lpfc_vport **vports;
4403 int i;
James Smartda0436e2009-05-22 14:51:39 -04004404
James Smart70f3c072010-12-15 17:57:33 -05004405 phba->fc_eventTag = acqe_fip->event_tag;
4406 phba->fcoe_eventtag = acqe_fip->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004407 switch (event_type) {
James Smart70f3c072010-12-15 17:57:33 -05004408 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
4409 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
4410 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
James Smart999d8132010-03-15 11:24:56 -04004411 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4412 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004413 "2546 New FCF event, evt_tag:x%x, "
4414 "index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004415 acqe_fip->event_tag,
4416 acqe_fip->index);
James Smart999d8132010-03-15 11:24:56 -04004417 else
4418 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
4419 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004420 "2788 FCF param modified event, "
4421 "evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004422 acqe_fip->event_tag,
4423 acqe_fip->index);
James Smart38b92ef2010-08-04 16:11:39 -04004424 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004425 /*
4426 * During period of FCF discovery, read the FCF
4427 * table record indexed by the event to update
James Smarta93ff372010-10-22 11:06:08 -04004428 * FCF roundrobin failover eligible FCF bmask.
James Smart0c9ab6f2010-02-26 14:15:57 -05004429 */
4430 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4431 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004432 "2779 Read FCF (x%x) for updating "
4433 "roundrobin FCF failover bmask\n",
James Smart70f3c072010-12-15 17:57:33 -05004434 acqe_fip->index);
4435 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05004436 }
James Smart38b92ef2010-08-04 16:11:39 -04004437
4438 /* If the FCF discovery is in progress, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04004439 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04004440 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart38b92ef2010-08-04 16:11:39 -04004441 spin_unlock_irq(&phba->hbalock);
4442 break;
4443 }
4444 /* If fast FCF failover rescan event is pending, do nothing */
4445 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
4446 spin_unlock_irq(&phba->hbalock);
4447 break;
4448 }
4449
James Smartc2b97122013-05-31 17:05:36 -04004450 /* If the FCF has been in discovered state, do nothing. */
4451 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
James Smart3804dc82010-07-14 15:31:37 -04004452 spin_unlock_irq(&phba->hbalock);
4453 break;
4454 }
4455 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04004456
James Smart0c9ab6f2010-02-26 14:15:57 -05004457 /* Otherwise, scan the entire FCF table and re-discover SAN */
4458 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004459 "2770 Start FCF table scan per async FCF "
4460 "event, evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004461 acqe_fip->event_tag, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05004462 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
4463 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04004464 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05004465 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4466 "2547 Issue FCF scan read FCF mailbox "
James Smarta93ff372010-10-22 11:06:08 -04004467 "command failed (x%x)\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004468 break;
4469
James Smart70f3c072010-12-15 17:57:33 -05004470 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
James Smartda0436e2009-05-22 14:51:39 -04004471 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04004472 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004473 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
4474 acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04004475 break;
4476
James Smart70f3c072010-12-15 17:57:33 -05004477 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
James Smart80c17842012-03-01 22:35:45 -05004478 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05004479 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004480 "2549 FCF (x%x) disconnected from network, "
James Smart70f3c072010-12-15 17:57:33 -05004481 "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart38b92ef2010-08-04 16:11:39 -04004482 /*
4483 * If we are in the middle of FCF failover process, clear
4484 * the corresponding FCF bit in the roundrobin bitmap.
James Smartda0436e2009-05-22 14:51:39 -04004485 */
James Smartfc2b9892010-02-26 14:15:29 -05004486 spin_lock_irq(&phba->hbalock);
James Smarta1cadfe2016-07-06 12:36:02 -07004487 if ((phba->fcf.fcf_flag & FCF_DISCOVERY) &&
4488 (phba->fcf.current_rec.fcf_indx != acqe_fip->index)) {
James Smartfc2b9892010-02-26 14:15:29 -05004489 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004490 /* Update FLOGI FCF failover eligible FCF bmask */
James Smart70f3c072010-12-15 17:57:33 -05004491 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004492 break;
4493 }
James Smart38b92ef2010-08-04 16:11:39 -04004494 spin_unlock_irq(&phba->hbalock);
4495
4496 /* If the event is not for currently used fcf do nothing */
James Smart70f3c072010-12-15 17:57:33 -05004497 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
James Smart38b92ef2010-08-04 16:11:39 -04004498 break;
4499
4500 /*
4501 * Otherwise, request the port to rediscover the entire FCF
4502 * table for a fast recovery from case that the current FCF
4503 * is no longer valid as we are not in the middle of FCF
4504 * failover process already.
4505 */
James Smartc2b97122013-05-31 17:05:36 -04004506 spin_lock_irq(&phba->hbalock);
4507 /* Mark the fast failover process in progress */
4508 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
4509 spin_unlock_irq(&phba->hbalock);
4510
4511 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
4512 "2771 Start FCF fast failover process due to "
4513 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
4514 "\n", acqe_fip->event_tag, acqe_fip->index);
4515 rc = lpfc_sli4_redisc_fcf_table(phba);
4516 if (rc) {
4517 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4518 LOG_DISCOVERY,
4519 "2772 Issue FCF rediscover mabilbox "
4520 "command failed, fail through to FCF "
4521 "dead event\n");
4522 spin_lock_irq(&phba->hbalock);
4523 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
4524 spin_unlock_irq(&phba->hbalock);
4525 /*
4526 * Last resort will fail over by treating this
4527 * as a link down to FCF registration.
4528 */
4529 lpfc_sli4_fcf_dead_failthrough(phba);
4530 } else {
4531 /* Reset FCF roundrobin bmask for new discovery */
4532 lpfc_sli4_clear_fcf_rr_bmask(phba);
4533 /*
4534 * Handling fast FCF failover to a DEAD FCF event is
4535 * considered equalivant to receiving CVL to all vports.
4536 */
4537 lpfc_sli4_perform_all_vport_cvl(phba);
4538 }
James Smartda0436e2009-05-22 14:51:39 -04004539 break;
James Smart70f3c072010-12-15 17:57:33 -05004540 case LPFC_FIP_EVENT_TYPE_CVL:
James Smart80c17842012-03-01 22:35:45 -05004541 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05004542 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6669f9b2009-10-02 15:16:45 -04004543 "2718 Clear Virtual Link Received for VPI 0x%x"
James Smart70f3c072010-12-15 17:57:33 -05004544 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart6d368e52011-05-24 11:44:12 -04004545
James Smart6669f9b2009-10-02 15:16:45 -04004546 vport = lpfc_find_vport_by_vpid(phba,
James Smart5248a742011-07-22 18:37:06 -04004547 acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004548 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04004549 if (!ndlp)
4550 break;
James Smart695a8142010-01-26 23:08:03 -05004551 active_vlink_present = 0;
4552
4553 vports = lpfc_create_vport_work_array(phba);
4554 if (vports) {
4555 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
4556 i++) {
4557 if ((!(vports[i]->fc_flag &
4558 FC_VPORT_CVL_RCVD)) &&
4559 (vports[i]->port_state > LPFC_FDISC)) {
4560 active_vlink_present = 1;
4561 break;
4562 }
4563 }
4564 lpfc_destroy_vport_work_array(phba, vports);
4565 }
4566
James Smartcc823552015-05-21 13:55:26 -04004567 /*
4568 * Don't re-instantiate if vport is marked for deletion.
4569 * If we are here first then vport_delete is going to wait
4570 * for discovery to complete.
4571 */
4572 if (!(vport->load_flag & FC_UNLOADING) &&
4573 active_vlink_present) {
James Smart695a8142010-01-26 23:08:03 -05004574 /*
4575 * If there are other active VLinks present,
4576 * re-instantiate the Vlink using FDISC.
4577 */
James Smart256ec0d2013-04-17 20:14:58 -04004578 mod_timer(&ndlp->nlp_delayfunc,
4579 jiffies + msecs_to_jiffies(1000));
James Smartfc2b9892010-02-26 14:15:29 -05004580 shost = lpfc_shost_from_vport(vport);
James Smart6669f9b2009-10-02 15:16:45 -04004581 spin_lock_irq(shost->host_lock);
4582 ndlp->nlp_flag |= NLP_DELAY_TMO;
4583 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05004584 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
4585 vport->port_state = LPFC_FDISC;
4586 } else {
James Smartecfd03c2010-02-12 14:41:27 -05004587 /*
4588 * Otherwise, we request port to rediscover
4589 * the entire FCF table for a fast recovery
4590 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05004591 * is no longer valid if we are not already
4592 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05004593 */
James Smartfc2b9892010-02-26 14:15:29 -05004594 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004595 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05004596 spin_unlock_irq(&phba->hbalock);
4597 break;
4598 }
4599 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05004600 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004601 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004602 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4603 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004604 "2773 Start FCF failover per CVL, "
James Smart70f3c072010-12-15 17:57:33 -05004605 "evt_tag:x%x\n", acqe_fip->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05004606 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05004607 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004608 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4609 LOG_DISCOVERY,
4610 "2774 Issue FCF rediscover "
4611 "mabilbox command failed, "
4612 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05004613 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004614 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004615 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05004616 /*
4617 * Last resort will be re-try on the
4618 * the current registered FCF entry.
4619 */
4620 lpfc_retry_pport_discovery(phba);
James Smart38b92ef2010-08-04 16:11:39 -04004621 } else
4622 /*
4623 * Reset FCF roundrobin bmask for new
4624 * discovery.
4625 */
James Smart7d791df2011-07-22 18:37:52 -04004626 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart6669f9b2009-10-02 15:16:45 -04004627 }
4628 break;
James Smartda0436e2009-05-22 14:51:39 -04004629 default:
4630 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4631 "0288 Unknown FCoE event type 0x%x event tag "
James Smart70f3c072010-12-15 17:57:33 -05004632 "0x%x\n", event_type, acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04004633 break;
4634 }
4635}
4636
4637/**
4638 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
4639 * @phba: pointer to lpfc hba data structure.
4640 * @acqe_link: pointer to the async dcbx completion queue entry.
4641 *
4642 * This routine is to handle the SLI4 asynchronous dcbx event.
4643 **/
4644static void
4645lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
4646 struct lpfc_acqe_dcbx *acqe_dcbx)
4647{
James Smart4d9ab992009-10-02 15:16:39 -04004648 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004649 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4650 "0290 The SLI4 DCBX asynchronous event is not "
4651 "handled yet\n");
4652}
4653
4654/**
James Smartb19a0612010-04-06 14:48:51 -04004655 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
4656 * @phba: pointer to lpfc hba data structure.
4657 * @acqe_link: pointer to the async grp5 completion queue entry.
4658 *
4659 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
4660 * is an asynchronous notified of a logical link speed change. The Port
4661 * reports the logical link speed in units of 10Mbps.
4662 **/
4663static void
4664lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
4665 struct lpfc_acqe_grp5 *acqe_grp5)
4666{
4667 uint16_t prev_ll_spd;
4668
4669 phba->fc_eventTag = acqe_grp5->event_tag;
4670 phba->fcoe_eventtag = acqe_grp5->event_tag;
4671 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
4672 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04004673 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5)) * 10;
James Smartb19a0612010-04-06 14:48:51 -04004674 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4675 "2789 GRP5 Async Event: Updating logical link speed "
James Smart8b68cd52012-09-29 11:32:37 -04004676 "from %dMbps to %dMbps\n", prev_ll_spd,
4677 phba->sli4_hba.link_state.logical_speed);
James Smartb19a0612010-04-06 14:48:51 -04004678}
4679
4680/**
James Smartda0436e2009-05-22 14:51:39 -04004681 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
4682 * @phba: pointer to lpfc hba data structure.
4683 *
4684 * This routine is invoked by the worker thread to process all the pending
4685 * SLI4 asynchronous events.
4686 **/
4687void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
4688{
4689 struct lpfc_cq_event *cq_event;
4690
4691 /* First, declare the async event has been handled */
4692 spin_lock_irq(&phba->hbalock);
4693 phba->hba_flag &= ~ASYNC_EVENT;
4694 spin_unlock_irq(&phba->hbalock);
4695 /* Now, handle all the async events */
4696 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
4697 /* Get the first event from the head of the event queue */
4698 spin_lock_irq(&phba->hbalock);
4699 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
4700 cq_event, struct lpfc_cq_event, list);
4701 spin_unlock_irq(&phba->hbalock);
4702 /* Process the asynchronous event */
4703 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
4704 case LPFC_TRAILER_CODE_LINK:
4705 lpfc_sli4_async_link_evt(phba,
4706 &cq_event->cqe.acqe_link);
4707 break;
4708 case LPFC_TRAILER_CODE_FCOE:
James Smart70f3c072010-12-15 17:57:33 -05004709 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004710 break;
4711 case LPFC_TRAILER_CODE_DCBX:
4712 lpfc_sli4_async_dcbx_evt(phba,
4713 &cq_event->cqe.acqe_dcbx);
4714 break;
James Smartb19a0612010-04-06 14:48:51 -04004715 case LPFC_TRAILER_CODE_GRP5:
4716 lpfc_sli4_async_grp5_evt(phba,
4717 &cq_event->cqe.acqe_grp5);
4718 break;
James Smart70f3c072010-12-15 17:57:33 -05004719 case LPFC_TRAILER_CODE_FC:
4720 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
4721 break;
4722 case LPFC_TRAILER_CODE_SLI:
4723 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
4724 break;
James Smartda0436e2009-05-22 14:51:39 -04004725 default:
4726 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4727 "1804 Invalid asynchrous event code: "
4728 "x%x\n", bf_get(lpfc_trailer_code,
4729 &cq_event->cqe.mcqe_cmpl));
4730 break;
4731 }
4732 /* Free the completion event processed to the free pool */
4733 lpfc_sli4_cq_event_release(phba, cq_event);
4734 }
4735}
4736
4737/**
James Smartecfd03c2010-02-12 14:41:27 -05004738 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
4739 * @phba: pointer to lpfc hba data structure.
4740 *
4741 * This routine is invoked by the worker thread to process FCF table
4742 * rediscovery pending completion event.
4743 **/
4744void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
4745{
4746 int rc;
4747
4748 spin_lock_irq(&phba->hbalock);
4749 /* Clear FCF rediscovery timeout event */
4750 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
4751 /* Clear driver fast failover FCF record flag */
4752 phba->fcf.failover_rec.flag = 0;
4753 /* Set state for FCF fast failover */
4754 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
4755 spin_unlock_irq(&phba->hbalock);
4756
4757 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05004758 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004759 "2777 Start post-quiescent FCF table scan\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05004760 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05004761 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05004762 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4763 "2747 Issue FCF scan read FCF mailbox "
4764 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05004765}
4766
4767/**
James Smartda0436e2009-05-22 14:51:39 -04004768 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
4769 * @phba: pointer to lpfc hba data structure.
4770 * @dev_grp: The HBA PCI-Device group number.
4771 *
4772 * This routine is invoked to set up the per HBA PCI-Device group function
4773 * API jump table entries.
4774 *
4775 * Return: 0 if success, otherwise -ENODEV
4776 **/
4777int
4778lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05004779{
4780 int rc;
4781
James Smartda0436e2009-05-22 14:51:39 -04004782 /* Set up lpfc PCI-device group */
4783 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05004784
James Smartda0436e2009-05-22 14:51:39 -04004785 /* The LPFC_PCI_DEV_OC uses SLI4 */
4786 if (dev_grp == LPFC_PCI_DEV_OC)
4787 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05004788
James Smartda0436e2009-05-22 14:51:39 -04004789 /* Set up device INIT API function jump table */
4790 rc = lpfc_init_api_table_setup(phba, dev_grp);
4791 if (rc)
4792 return -ENODEV;
4793 /* Set up SCSI API function jump table */
4794 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
4795 if (rc)
4796 return -ENODEV;
4797 /* Set up SLI API function jump table */
4798 rc = lpfc_sli_api_table_setup(phba, dev_grp);
4799 if (rc)
4800 return -ENODEV;
4801 /* Set up MBOX API function jump table */
4802 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
4803 if (rc)
4804 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05004805
James Smartda0436e2009-05-22 14:51:39 -04004806 return 0;
James Smart5b75da22008-12-04 22:39:35 -05004807}
4808
4809/**
James Smart3621a712009-04-06 18:47:14 -04004810 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05004811 * @phba: pointer to lpfc hba data structure.
4812 * @intr_mode: active interrupt mode adopted.
4813 *
4814 * This routine it invoked to log the currently used active interrupt mode
4815 * to the device.
James Smart3772a992009-05-22 14:50:54 -04004816 **/
4817static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05004818{
4819 switch (intr_mode) {
4820 case 0:
4821 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4822 "0470 Enable INTx interrupt mode.\n");
4823 break;
4824 case 1:
4825 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4826 "0481 Enabled MSI interrupt mode.\n");
4827 break;
4828 case 2:
4829 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4830 "0480 Enabled MSI-X interrupt mode.\n");
4831 break;
4832 default:
4833 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4834 "0482 Illegal interrupt mode.\n");
4835 break;
4836 }
4837 return;
4838}
4839
James Smart5b75da22008-12-04 22:39:35 -05004840/**
James Smart3772a992009-05-22 14:50:54 -04004841 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05004842 * @phba: pointer to lpfc hba data structure.
4843 *
James Smart3772a992009-05-22 14:50:54 -04004844 * This routine is invoked to enable the PCI device that is common to all
4845 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05004846 *
4847 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004848 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004849 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05004850 **/
James Smart3772a992009-05-22 14:50:54 -04004851static int
4852lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05004853{
James Smart3772a992009-05-22 14:50:54 -04004854 struct pci_dev *pdev;
James Smart079b5c92011-08-21 21:48:49 -04004855 int bars = 0;
James Smart5b75da22008-12-04 22:39:35 -05004856
James Smart3772a992009-05-22 14:50:54 -04004857 /* Obtain PCI device reference */
4858 if (!phba->pcidev)
4859 goto out_error;
4860 else
4861 pdev = phba->pcidev;
4862 /* Select PCI BARs */
4863 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4864 /* Enable PCI device */
4865 if (pci_enable_device_mem(pdev))
4866 goto out_error;
4867 /* Request PCI resource for the device */
4868 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
4869 goto out_disable_device;
4870 /* Set up device as PCI master and save state for EEH */
4871 pci_set_master(pdev);
4872 pci_try_set_mwi(pdev);
4873 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05004874
James Smart05580562011-05-24 11:40:48 -04004875 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
Jon Mason453193e2013-09-11 15:38:13 -07004876 if (pci_is_pcie(pdev))
James Smart05580562011-05-24 11:40:48 -04004877 pdev->needs_freset = 1;
4878
James Smart3772a992009-05-22 14:50:54 -04004879 return 0;
James Smart5b75da22008-12-04 22:39:35 -05004880
James Smart3772a992009-05-22 14:50:54 -04004881out_disable_device:
4882 pci_disable_device(pdev);
4883out_error:
James Smart079b5c92011-08-21 21:48:49 -04004884 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4885 "1401 Failed to enable pci device, bars:x%x\n", bars);
James Smart3772a992009-05-22 14:50:54 -04004886 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05004887}
4888
4889/**
James Smart3772a992009-05-22 14:50:54 -04004890 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05004891 * @phba: pointer to lpfc hba data structure.
4892 *
James Smart3772a992009-05-22 14:50:54 -04004893 * This routine is invoked to disable the PCI device that is common to all
4894 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05004895 **/
4896static void
James Smart3772a992009-05-22 14:50:54 -04004897lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05004898{
James Smart3772a992009-05-22 14:50:54 -04004899 struct pci_dev *pdev;
4900 int bars;
James Smart5b75da22008-12-04 22:39:35 -05004901
James Smart3772a992009-05-22 14:50:54 -04004902 /* Obtain PCI device reference */
4903 if (!phba->pcidev)
4904 return;
4905 else
4906 pdev = phba->pcidev;
4907 /* Select PCI BARs */
4908 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4909 /* Release PCI resource and disable PCI device */
4910 pci_release_selected_regions(pdev, bars);
4911 pci_disable_device(pdev);
James Smart5b75da22008-12-04 22:39:35 -05004912
4913 return;
4914}
4915
4916/**
James Smart3772a992009-05-22 14:50:54 -04004917 * lpfc_reset_hba - Reset a hba
4918 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04004919 *
James Smart3772a992009-05-22 14:50:54 -04004920 * This routine is invoked to reset a hba device. It brings the HBA
4921 * offline, performs a board restart, and then brings the board back
4922 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
4923 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04004924 **/
James Smart3772a992009-05-22 14:50:54 -04004925void
4926lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004927{
James Smart3772a992009-05-22 14:50:54 -04004928 /* If resets are disabled then set error state and return. */
4929 if (!phba->cfg_enable_hba_reset) {
4930 phba->link_state = LPFC_HBA_ERROR;
4931 return;
4932 }
James Smartee620212014-04-04 13:51:53 -04004933 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
4934 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
4935 else
4936 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart3772a992009-05-22 14:50:54 -04004937 lpfc_offline(phba);
4938 lpfc_sli_brdrestart(phba);
4939 lpfc_online(phba);
4940 lpfc_unblock_mgmt_io(phba);
4941}
dea31012005-04-17 16:05:31 -05004942
James Smart3772a992009-05-22 14:50:54 -04004943/**
James Smart0a96e972011-07-22 18:37:28 -04004944 * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
4945 * @phba: pointer to lpfc hba data structure.
4946 *
4947 * This function enables the PCI SR-IOV virtual functions to a physical
4948 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4949 * enable the number of virtual functions to the physical function. As
4950 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4951 * API call does not considered as an error condition for most of the device.
4952 **/
4953uint16_t
4954lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
4955{
4956 struct pci_dev *pdev = phba->pcidev;
4957 uint16_t nr_virtfn;
4958 int pos;
4959
James Smart0a96e972011-07-22 18:37:28 -04004960 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
4961 if (pos == 0)
4962 return 0;
4963
4964 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
4965 return nr_virtfn;
4966}
4967
4968/**
James Smart912e3ac2011-05-24 11:42:11 -04004969 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
4970 * @phba: pointer to lpfc hba data structure.
4971 * @nr_vfn: number of virtual functions to be enabled.
4972 *
4973 * This function enables the PCI SR-IOV virtual functions to a physical
4974 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4975 * enable the number of virtual functions to the physical function. As
4976 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4977 * API call does not considered as an error condition for most of the device.
4978 **/
4979int
4980lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
4981{
4982 struct pci_dev *pdev = phba->pcidev;
James Smart0a96e972011-07-22 18:37:28 -04004983 uint16_t max_nr_vfn;
James Smart912e3ac2011-05-24 11:42:11 -04004984 int rc;
4985
James Smart0a96e972011-07-22 18:37:28 -04004986 max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
4987 if (nr_vfn > max_nr_vfn) {
4988 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4989 "3057 Requested vfs (%d) greater than "
4990 "supported vfs (%d)", nr_vfn, max_nr_vfn);
4991 return -EINVAL;
4992 }
4993
James Smart912e3ac2011-05-24 11:42:11 -04004994 rc = pci_enable_sriov(pdev, nr_vfn);
4995 if (rc) {
4996 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4997 "2806 Failed to enable sriov on this device "
4998 "with vfn number nr_vf:%d, rc:%d\n",
4999 nr_vfn, rc);
5000 } else
5001 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5002 "2807 Successful enable sriov on this device "
5003 "with vfn number nr_vf:%d\n", nr_vfn);
5004 return rc;
5005}
5006
5007/**
James Smart3772a992009-05-22 14:50:54 -04005008 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
5009 * @phba: pointer to lpfc hba data structure.
5010 *
5011 * This routine is invoked to set up the driver internal resources specific to
5012 * support the SLI-3 HBA device it attached to.
5013 *
5014 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005015 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005016 * other values - error
5017 **/
5018static int
5019lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
5020{
5021 struct lpfc_sli *psli;
James Smart912e3ac2011-05-24 11:42:11 -04005022 int rc;
dea31012005-04-17 16:05:31 -05005023
James Smart3772a992009-05-22 14:50:54 -04005024 /*
5025 * Initialize timers used by driver
5026 */
dea31012005-04-17 16:05:31 -05005027
James Smart3772a992009-05-22 14:50:54 -04005028 /* Heartbeat timer */
5029 init_timer(&phba->hb_tmofunc);
5030 phba->hb_tmofunc.function = lpfc_hb_timeout;
5031 phba->hb_tmofunc.data = (unsigned long)phba;
5032
5033 psli = &phba->sli;
5034 /* MBOX heartbeat timer */
5035 init_timer(&psli->mbox_tmo);
5036 psli->mbox_tmo.function = lpfc_mbox_timeout;
5037 psli->mbox_tmo.data = (unsigned long) phba;
5038 /* FCP polling mode timer */
5039 init_timer(&phba->fcp_poll_timer);
5040 phba->fcp_poll_timer.function = lpfc_poll_timeout;
5041 phba->fcp_poll_timer.data = (unsigned long) phba;
5042 /* Fabric block timer */
5043 init_timer(&phba->fabric_block_timer);
5044 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
5045 phba->fabric_block_timer.data = (unsigned long) phba;
5046 /* EA polling mode timer */
5047 init_timer(&phba->eratt_poll);
5048 phba->eratt_poll.function = lpfc_poll_eratt;
5049 phba->eratt_poll.data = (unsigned long) phba;
5050
5051 /* Host attention work mask setup */
5052 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
5053 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
5054
5055 /* Get all the module params for configuring this host */
5056 lpfc_get_cfgparam(phba);
James Smart49198b32010-04-06 15:04:33 -04005057 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
5058 phba->menlo_flag |= HBA_MENLO_SUPPORT;
5059 /* check for menlo minimum sg count */
5060 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
5061 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
5062 }
5063
James Smart2a76a282012-08-03 12:35:54 -04005064 if (!phba->sli.ring)
Firo Yangc6cb9b42015-08-31 16:48:09 -04005065 phba->sli.ring = kzalloc(LPFC_SLI3_MAX_RING *
James Smart2a76a282012-08-03 12:35:54 -04005066 sizeof(struct lpfc_sli_ring), GFP_KERNEL);
5067 if (!phba->sli.ring)
5068 return -ENOMEM;
5069
James Smart3772a992009-05-22 14:50:54 -04005070 /*
James Smart96f70772013-04-17 20:16:15 -04005071 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smart3772a992009-05-22 14:50:54 -04005072 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smart3772a992009-05-22 14:50:54 -04005073 */
James Smart3772a992009-05-22 14:50:54 -04005074
James Smart96f70772013-04-17 20:16:15 -04005075 /* Initialize the host templates the configured values. */
James Smart3772a992009-05-22 14:50:54 -04005076 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
Bodo Stroesser5f406fa2015-08-31 16:48:10 -04005077 lpfc_template_s3.sg_tablesize = phba->cfg_sg_seg_cnt;
James Smart3772a992009-05-22 14:50:54 -04005078
James Smart96f70772013-04-17 20:16:15 -04005079 /* There are going to be 2 reserved BDEs: 1 FCP cmnd + 1 FCP rsp */
5080 if (phba->cfg_enable_bg) {
5081 /*
5082 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
5083 * the FCP rsp, and a BDE for each. Sice we have no control
5084 * over how many protection data segments the SCSI Layer
5085 * will hand us (ie: there could be one for every block
5086 * in the IO), we just allocate enough BDEs to accomidate
5087 * our max amount and we need to limit lpfc_sg_seg_cnt to
5088 * minimize the risk of running out.
5089 */
5090 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5091 sizeof(struct fcp_rsp) +
5092 (LPFC_MAX_SG_SEG_CNT * sizeof(struct ulp_bde64));
5093
5094 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SEG_CNT_DIF)
5095 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT_DIF;
5096
5097 /* Total BDEs in BPL for scsi_sg_list and scsi_sg_prot_list */
5098 phba->cfg_total_seg_cnt = LPFC_MAX_SG_SEG_CNT;
5099 } else {
5100 /*
5101 * The scsi_buf for a regular I/O will hold the FCP cmnd,
5102 * the FCP rsp, a BDE for each, and a BDE for up to
5103 * cfg_sg_seg_cnt data segments.
5104 */
5105 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5106 sizeof(struct fcp_rsp) +
5107 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
5108
5109 /* Total BDEs in BPL for scsi_sg_list */
5110 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
5111 }
5112
5113 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
5114 "9088 sg_tablesize:%d dmabuf_size:%d total_bde:%d\n",
5115 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
5116 phba->cfg_total_seg_cnt);
5117
James Smart3772a992009-05-22 14:50:54 -04005118 phba->max_vpi = LPFC_MAX_VPI;
5119 /* This will be set to correct value after config_port mbox */
5120 phba->max_vports = 0;
5121
5122 /*
5123 * Initialize the SLI Layer to run with lpfc HBAs.
5124 */
5125 lpfc_sli_setup(phba);
5126 lpfc_sli_queue_setup(phba);
5127
5128 /* Allocate device driver memory */
5129 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
5130 return -ENOMEM;
5131
James Smart912e3ac2011-05-24 11:42:11 -04005132 /*
5133 * Enable sr-iov virtual functions if supported and configured
5134 * through the module parameter.
5135 */
5136 if (phba->cfg_sriov_nr_virtfn > 0) {
5137 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
5138 phba->cfg_sriov_nr_virtfn);
5139 if (rc) {
5140 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5141 "2808 Requested number of SR-IOV "
5142 "virtual functions (%d) is not "
5143 "supported\n",
5144 phba->cfg_sriov_nr_virtfn);
5145 phba->cfg_sriov_nr_virtfn = 0;
5146 }
5147 }
5148
James Smart3772a992009-05-22 14:50:54 -04005149 return 0;
5150}
5151
5152/**
5153 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
5154 * @phba: pointer to lpfc hba data structure.
5155 *
5156 * This routine is invoked to unset the driver internal resources set up
5157 * specific for supporting the SLI-3 HBA device it attached to.
5158 **/
5159static void
5160lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
5161{
5162 /* Free device driver memory allocated */
5163 lpfc_mem_free_all(phba);
5164
5165 return;
5166}
5167
5168/**
James Smartda0436e2009-05-22 14:51:39 -04005169 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
5170 * @phba: pointer to lpfc hba data structure.
5171 *
5172 * This routine is invoked to set up the driver internal resources specific to
5173 * support the SLI-4 HBA device it attached to.
5174 *
5175 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005176 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005177 * other values - error
5178 **/
5179static int
5180lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
5181{
James Smart7bb03bb2013-04-17 20:19:16 -04005182 struct lpfc_vector_map_info *cpup;
James Smartda0436e2009-05-22 14:51:39 -04005183 struct lpfc_sli *psli;
James Smart28baac72010-02-12 14:42:03 -05005184 LPFC_MBOXQ_t *mboxq;
James Smart96f70772013-04-17 20:16:15 -04005185 int rc, i, hbq_count, max_buf_size;
James Smart28baac72010-02-12 14:42:03 -05005186 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
5187 struct lpfc_mqe *mqe;
James Smart09294d42013-04-17 20:16:05 -04005188 int longs;
James Smart1ba981f2014-02-20 09:56:45 -05005189 int fof_vectors = 0;
James Smartda0436e2009-05-22 14:51:39 -04005190
James Smart716d3bc2013-09-06 12:18:28 -04005191 /* Get all the module params for configuring this host */
5192 lpfc_get_cfgparam(phba);
5193
James Smartda0436e2009-05-22 14:51:39 -04005194 /* Before proceed, wait for POST done and device ready */
5195 rc = lpfc_sli4_post_status_check(phba);
5196 if (rc)
5197 return -ENODEV;
5198
5199 /*
5200 * Initialize timers used by driver
5201 */
5202
5203 /* Heartbeat timer */
5204 init_timer(&phba->hb_tmofunc);
5205 phba->hb_tmofunc.function = lpfc_hb_timeout;
5206 phba->hb_tmofunc.data = (unsigned long)phba;
James Smart19ca7602010-11-20 23:11:55 -05005207 init_timer(&phba->rrq_tmr);
5208 phba->rrq_tmr.function = lpfc_rrq_timeout;
5209 phba->rrq_tmr.data = (unsigned long)phba;
James Smartda0436e2009-05-22 14:51:39 -04005210
5211 psli = &phba->sli;
5212 /* MBOX heartbeat timer */
5213 init_timer(&psli->mbox_tmo);
5214 psli->mbox_tmo.function = lpfc_mbox_timeout;
5215 psli->mbox_tmo.data = (unsigned long) phba;
5216 /* Fabric block timer */
5217 init_timer(&phba->fabric_block_timer);
5218 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
5219 phba->fabric_block_timer.data = (unsigned long) phba;
5220 /* EA polling mode timer */
5221 init_timer(&phba->eratt_poll);
5222 phba->eratt_poll.function = lpfc_poll_eratt;
5223 phba->eratt_poll.data = (unsigned long) phba;
James Smartecfd03c2010-02-12 14:41:27 -05005224 /* FCF rediscover timer */
5225 init_timer(&phba->fcf.redisc_wait);
5226 phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
5227 phba->fcf.redisc_wait.data = (unsigned long)phba;
5228
James Smartda0436e2009-05-22 14:51:39 -04005229 /*
James Smart7ad20aa2011-05-24 11:44:28 -04005230 * Control structure for handling external multi-buffer mailbox
5231 * command pass-through.
5232 */
5233 memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
5234 sizeof(struct lpfc_mbox_ext_buf_ctx));
5235 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
5236
James Smartda0436e2009-05-22 14:51:39 -04005237 phba->max_vpi = LPFC_MAX_VPI;
James Smart67d12732012-08-03 12:36:13 -04005238
James Smartda0436e2009-05-22 14:51:39 -04005239 /* This will be set to correct value after the read_config mbox */
5240 phba->max_vports = 0;
5241
5242 /* Program the default value of vlan_id and fc_map */
5243 phba->valid_vlan = 0;
5244 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5245 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5246 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5247
5248 /*
James Smart2a76a282012-08-03 12:35:54 -04005249 * For SLI4, instead of using ring 0 (LPFC_FCP_RING) for FCP commands
5250 * we will associate a new ring, for each FCP fastpath EQ/CQ/WQ tuple.
5251 */
5252 if (!phba->sli.ring)
5253 phba->sli.ring = kzalloc(
James Smart67d12732012-08-03 12:36:13 -04005254 (LPFC_SLI3_MAX_RING + phba->cfg_fcp_io_channel) *
James Smart2a76a282012-08-03 12:35:54 -04005255 sizeof(struct lpfc_sli_ring), GFP_KERNEL);
5256 if (!phba->sli.ring)
5257 return -ENOMEM;
James Smart085c6472010-11-20 23:11:37 -05005258
James Smart09294d42013-04-17 20:16:05 -04005259 /*
5260 * It doesn't matter what family our adapter is in, we are
5261 * limited to 2 Pages, 512 SGEs, for our SGL.
5262 * There are going to be 2 reserved SGEs: 1 FCP cmnd + 1 FCP rsp
5263 */
5264 max_buf_size = (2 * SLI4_PAGE_SIZE);
5265 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SGL_SEG_CNT - 2)
5266 phba->cfg_sg_seg_cnt = LPFC_MAX_SGL_SEG_CNT - 2;
James Smart09294d42013-04-17 20:16:05 -04005267
James Smartda0436e2009-05-22 14:51:39 -04005268 /*
James Smart96f70772013-04-17 20:16:15 -04005269 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smartda0436e2009-05-22 14:51:39 -04005270 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smart28baac72010-02-12 14:42:03 -05005271 */
James Smart085c6472010-11-20 23:11:37 -05005272
James Smart96f70772013-04-17 20:16:15 -04005273 if (phba->cfg_enable_bg) {
5274 /*
5275 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
5276 * the FCP rsp, and a SGE for each. Sice we have no control
5277 * over how many protection data segments the SCSI Layer
5278 * will hand us (ie: there could be one for every block
5279 * in the IO), we just allocate enough SGEs to accomidate
5280 * our max amount and we need to limit lpfc_sg_seg_cnt to
5281 * minimize the risk of running out.
5282 */
5283 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5284 sizeof(struct fcp_rsp) + max_buf_size;
5285
5286 /* Total SGEs for scsi_sg_list and scsi_sg_prot_list */
5287 phba->cfg_total_seg_cnt = LPFC_MAX_SGL_SEG_CNT;
5288
5289 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SLI4_SEG_CNT_DIF)
5290 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SLI4_SEG_CNT_DIF;
5291 } else {
5292 /*
5293 * The scsi_buf for a regular I/O will hold the FCP cmnd,
5294 * the FCP rsp, a SGE for each, and a SGE for up to
5295 * cfg_sg_seg_cnt data segments.
5296 */
5297 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5298 sizeof(struct fcp_rsp) +
5299 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct sli4_sge));
5300
5301 /* Total SGEs for scsi_sg_list */
5302 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
5303 /*
5304 * NOTE: if (phba->cfg_sg_seg_cnt + 2) <= 256 we only need
5305 * to post 1 page for the SGL.
5306 */
James Smart085c6472010-11-20 23:11:37 -05005307 }
James Smartacd68592012-01-18 16:25:09 -05005308
James Smart96f70772013-04-17 20:16:15 -04005309 /* Initialize the host templates with the updated values. */
5310 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5311 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5312
5313 if (phba->cfg_sg_dma_buf_size <= LPFC_MIN_SG_SLI4_BUF_SZ)
5314 phba->cfg_sg_dma_buf_size = LPFC_MIN_SG_SLI4_BUF_SZ;
5315 else
5316 phba->cfg_sg_dma_buf_size =
5317 SLI4_PAGE_ALIGN(phba->cfg_sg_dma_buf_size);
5318
5319 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
5320 "9087 sg_tablesize:%d dmabuf_size:%d total_sge:%d\n",
5321 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
5322 phba->cfg_total_seg_cnt);
James Smartda0436e2009-05-22 14:51:39 -04005323
5324 /* Initialize buffer queue management fields */
5325 hbq_count = lpfc_sli_hbq_count();
5326 for (i = 0; i < hbq_count; ++i)
5327 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
5328 INIT_LIST_HEAD(&phba->rb_pend_list);
5329 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
5330 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
5331
5332 /*
5333 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
5334 */
5335 /* Initialize the Abort scsi buffer list used by driver */
5336 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
5337 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
5338 /* This abort list used by worker thread */
5339 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
5340
5341 /*
James Smart6d368e52011-05-24 11:44:12 -04005342 * Initialize driver internal slow-path work queues
James Smartda0436e2009-05-22 14:51:39 -04005343 */
5344
5345 /* Driver internel slow-path CQ Event pool */
5346 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
5347 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04005348 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04005349 /* Asynchronous event CQ Event work queue list */
5350 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
5351 /* Fast-path XRI aborted CQ Event work queue list */
5352 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
5353 /* Slow-path XRI aborted CQ Event work queue list */
5354 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
5355 /* Receive queue CQ Event work queue list */
5356 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
5357
James Smart6d368e52011-05-24 11:44:12 -04005358 /* Initialize extent block lists. */
5359 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
5360 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
5361 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
5362 INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
5363
James Smart448193b2015-12-16 18:12:05 -05005364 /* initialize optic_state to 0xFF */
5365 phba->sli4_hba.lnk_info.optic_state = 0xff;
5366
James Smartda0436e2009-05-22 14:51:39 -04005367 /* Initialize the driver internal SLI layer lists. */
5368 lpfc_sli_setup(phba);
5369 lpfc_sli_queue_setup(phba);
5370
5371 /* Allocate device driver memory */
5372 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
5373 if (rc)
5374 return -ENOMEM;
5375
James Smart2fcee4b2010-12-15 17:57:46 -05005376 /* IF Type 2 ports get initialized now. */
5377 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
5378 LPFC_SLI_INTF_IF_TYPE_2) {
5379 rc = lpfc_pci_function_reset(phba);
5380 if (unlikely(rc))
5381 return -ENODEV;
James Smart946727d2015-04-07 15:07:09 -04005382 phba->temp_sensor_support = 1;
James Smart2fcee4b2010-12-15 17:57:46 -05005383 }
5384
James Smartda0436e2009-05-22 14:51:39 -04005385 /* Create the bootstrap mailbox command */
5386 rc = lpfc_create_bootstrap_mbox(phba);
5387 if (unlikely(rc))
5388 goto out_free_mem;
5389
5390 /* Set up the host's endian order with the device. */
5391 rc = lpfc_setup_endian_order(phba);
5392 if (unlikely(rc))
5393 goto out_free_bsmbx;
5394
5395 /* Set up the hba's configuration parameters. */
5396 rc = lpfc_sli4_read_config(phba);
5397 if (unlikely(rc))
5398 goto out_free_bsmbx;
James Smartcff261f2013-12-17 20:29:47 -05005399 rc = lpfc_mem_alloc_active_rrq_pool_s4(phba);
5400 if (unlikely(rc))
5401 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04005402
James Smart2fcee4b2010-12-15 17:57:46 -05005403 /* IF Type 0 ports get initialized now. */
5404 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
5405 LPFC_SLI_INTF_IF_TYPE_0) {
5406 rc = lpfc_pci_function_reset(phba);
5407 if (unlikely(rc))
5408 goto out_free_bsmbx;
5409 }
James Smartda0436e2009-05-22 14:51:39 -04005410
James Smartcb5172e2010-03-15 11:25:07 -04005411 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
5412 GFP_KERNEL);
5413 if (!mboxq) {
5414 rc = -ENOMEM;
5415 goto out_free_bsmbx;
5416 }
5417
James Smartfedd3b72011-02-16 12:39:24 -05005418 /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
James Smartcb5172e2010-03-15 11:25:07 -04005419 lpfc_supported_pages(mboxq);
5420 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smartfedd3b72011-02-16 12:39:24 -05005421 if (!rc) {
5422 mqe = &mboxq->u.mqe;
5423 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
5424 LPFC_MAX_SUPPORTED_PAGES);
5425 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
5426 switch (pn_page[i]) {
5427 case LPFC_SLI4_PARAMETERS:
5428 phba->sli4_hba.pc_sli4_params.supported = 1;
5429 break;
5430 default:
5431 break;
5432 }
5433 }
5434 /* Read the port's SLI4 Parameters capabilities if supported. */
5435 if (phba->sli4_hba.pc_sli4_params.supported)
5436 rc = lpfc_pc_sli4_params_get(phba, mboxq);
5437 if (rc) {
5438 mempool_free(mboxq, phba->mbox_mem_pool);
5439 rc = -EIO;
5440 goto out_free_bsmbx;
James Smartcb5172e2010-03-15 11:25:07 -04005441 }
5442 }
James Smart65791f12016-07-06 12:35:56 -07005443
James Smartfedd3b72011-02-16 12:39:24 -05005444 /*
5445 * Get sli4 parameters that override parameters from Port capabilities.
James Smart6d368e52011-05-24 11:44:12 -04005446 * If this call fails, it isn't critical unless the SLI4 parameters come
5447 * back in conflict.
James Smartfedd3b72011-02-16 12:39:24 -05005448 */
James Smart6d368e52011-05-24 11:44:12 -04005449 rc = lpfc_get_sli4_parameters(phba, mboxq);
5450 if (rc) {
5451 if (phba->sli4_hba.extents_in_use &&
5452 phba->sli4_hba.rpi_hdrs_in_use) {
5453 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5454 "2999 Unsupported SLI4 Parameters "
5455 "Extents and RPI headers enabled.\n");
5456 goto out_free_bsmbx;
5457 }
5458 }
James Smartcb5172e2010-03-15 11:25:07 -04005459 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1ba981f2014-02-20 09:56:45 -05005460
5461 /* Verify OAS is supported */
5462 lpfc_sli4_oas_verify(phba);
5463 if (phba->cfg_fof)
5464 fof_vectors = 1;
5465
James Smart5350d872011-10-10 21:33:49 -04005466 /* Verify all the SLI4 queues */
5467 rc = lpfc_sli4_queue_verify(phba);
James Smartda0436e2009-05-22 14:51:39 -04005468 if (rc)
5469 goto out_free_bsmbx;
5470
5471 /* Create driver internal CQE event pool */
5472 rc = lpfc_sli4_cq_event_pool_create(phba);
5473 if (rc)
James Smart5350d872011-10-10 21:33:49 -04005474 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04005475
James Smart8a9d2e82012-05-09 21:16:12 -04005476 /* Initialize sgl lists per host */
5477 lpfc_init_sgl_list(phba);
5478
5479 /* Allocate and initialize active sgl array */
James Smartda0436e2009-05-22 14:51:39 -04005480 rc = lpfc_init_active_sgl_array(phba);
5481 if (rc) {
5482 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5483 "1430 Failed to initialize sgl list.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04005484 goto out_destroy_cq_event_pool;
James Smartda0436e2009-05-22 14:51:39 -04005485 }
James Smartda0436e2009-05-22 14:51:39 -04005486 rc = lpfc_sli4_init_rpi_hdrs(phba);
5487 if (rc) {
5488 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5489 "1432 Failed to initialize rpi headers.\n");
5490 goto out_free_active_sgl;
5491 }
5492
James Smarta93ff372010-10-22 11:06:08 -04005493 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
James Smart0c9ab6f2010-02-26 14:15:57 -05005494 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
5495 phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
5496 GFP_KERNEL);
5497 if (!phba->fcf.fcf_rr_bmask) {
5498 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5499 "2759 Failed allocate memory for FCF round "
5500 "robin failover bmask\n");
James Smart05580562011-05-24 11:40:48 -04005501 rc = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -05005502 goto out_remove_rpi_hdrs;
5503 }
5504
James Smart67d12732012-08-03 12:36:13 -04005505 phba->sli4_hba.fcp_eq_hdl =
5506 kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
James Smart1ba981f2014-02-20 09:56:45 -05005507 (fof_vectors + phba->cfg_fcp_io_channel)),
5508 GFP_KERNEL);
James Smart67d12732012-08-03 12:36:13 -04005509 if (!phba->sli4_hba.fcp_eq_hdl) {
5510 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5511 "2572 Failed allocate memory for "
5512 "fast-path per-EQ handle array\n");
5513 rc = -ENOMEM;
5514 goto out_free_fcf_rr_bmask;
James Smartda0436e2009-05-22 14:51:39 -04005515 }
5516
5517 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
James Smart1ba981f2014-02-20 09:56:45 -05005518 (fof_vectors +
5519 phba->cfg_fcp_io_channel)), GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005520 if (!phba->sli4_hba.msix_entries) {
5521 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5522 "2573 Failed allocate memory for msi-x "
5523 "interrupt vector entries\n");
James Smart05580562011-05-24 11:40:48 -04005524 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04005525 goto out_free_fcp_eq_hdl;
5526 }
5527
James Smart7bb03bb2013-04-17 20:19:16 -04005528 phba->sli4_hba.cpu_map = kzalloc((sizeof(struct lpfc_vector_map_info) *
5529 phba->sli4_hba.num_present_cpu),
5530 GFP_KERNEL);
5531 if (!phba->sli4_hba.cpu_map) {
5532 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5533 "3327 Failed allocate memory for msi-x "
5534 "interrupt vector mapping\n");
5535 rc = -ENOMEM;
5536 goto out_free_msix;
5537 }
James Smartb246de12013-05-31 17:03:07 -04005538 if (lpfc_used_cpu == NULL) {
5539 lpfc_used_cpu = kzalloc((sizeof(uint16_t) * lpfc_present_cpu),
5540 GFP_KERNEL);
5541 if (!lpfc_used_cpu) {
5542 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5543 "3335 Failed allocate memory for msi-x "
5544 "interrupt vector mapping\n");
5545 kfree(phba->sli4_hba.cpu_map);
5546 rc = -ENOMEM;
5547 goto out_free_msix;
5548 }
5549 for (i = 0; i < lpfc_present_cpu; i++)
5550 lpfc_used_cpu[i] = LPFC_VECTOR_MAP_EMPTY;
5551 }
5552
James Smart7bb03bb2013-04-17 20:19:16 -04005553 /* Initialize io channels for round robin */
5554 cpup = phba->sli4_hba.cpu_map;
5555 rc = 0;
5556 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
5557 cpup->channel_id = rc;
5558 rc++;
5559 if (rc >= phba->cfg_fcp_io_channel)
5560 rc = 0;
5561 }
5562
James Smart912e3ac2011-05-24 11:42:11 -04005563 /*
5564 * Enable sr-iov virtual functions if supported and configured
5565 * through the module parameter.
5566 */
5567 if (phba->cfg_sriov_nr_virtfn > 0) {
5568 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
5569 phba->cfg_sriov_nr_virtfn);
5570 if (rc) {
5571 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5572 "3020 Requested number of SR-IOV "
5573 "virtual functions (%d) is not "
5574 "supported\n",
5575 phba->cfg_sriov_nr_virtfn);
5576 phba->cfg_sriov_nr_virtfn = 0;
5577 }
5578 }
5579
James Smart5248a742011-07-22 18:37:06 -04005580 return 0;
James Smartda0436e2009-05-22 14:51:39 -04005581
James Smart7bb03bb2013-04-17 20:19:16 -04005582out_free_msix:
5583 kfree(phba->sli4_hba.msix_entries);
James Smartda0436e2009-05-22 14:51:39 -04005584out_free_fcp_eq_hdl:
5585 kfree(phba->sli4_hba.fcp_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05005586out_free_fcf_rr_bmask:
5587 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04005588out_remove_rpi_hdrs:
5589 lpfc_sli4_remove_rpi_hdrs(phba);
5590out_free_active_sgl:
5591 lpfc_free_active_sgl(phba);
James Smartda0436e2009-05-22 14:51:39 -04005592out_destroy_cq_event_pool:
5593 lpfc_sli4_cq_event_pool_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04005594out_free_bsmbx:
5595 lpfc_destroy_bootstrap_mbox(phba);
5596out_free_mem:
5597 lpfc_mem_free(phba);
5598 return rc;
5599}
5600
5601/**
5602 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
5603 * @phba: pointer to lpfc hba data structure.
5604 *
5605 * This routine is invoked to unset the driver internal resources set up
5606 * specific for supporting the SLI-4 HBA device it attached to.
5607 **/
5608static void
5609lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
5610{
5611 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
5612
James Smart7bb03bb2013-04-17 20:19:16 -04005613 /* Free memory allocated for msi-x interrupt vector to CPU mapping */
5614 kfree(phba->sli4_hba.cpu_map);
5615 phba->sli4_hba.num_present_cpu = 0;
5616 phba->sli4_hba.num_online_cpu = 0;
James Smart76fd07a2014-02-20 09:57:18 -05005617 phba->sli4_hba.curr_disp_cpu = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04005618
James Smartda0436e2009-05-22 14:51:39 -04005619 /* Free memory allocated for msi-x interrupt vector entries */
5620 kfree(phba->sli4_hba.msix_entries);
5621
5622 /* Free memory allocated for fast-path work queue handles */
5623 kfree(phba->sli4_hba.fcp_eq_hdl);
5624
5625 /* Free the allocated rpi headers. */
5626 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04005627 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04005628
James Smart0c9ab6f2010-02-26 14:15:57 -05005629 /* Free eligible FCF index bmask */
5630 kfree(phba->fcf.fcf_rr_bmask);
5631
James Smartda0436e2009-05-22 14:51:39 -04005632 /* Free the ELS sgl list */
5633 lpfc_free_active_sgl(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04005634 lpfc_free_els_sgl_list(phba);
James Smartda0436e2009-05-22 14:51:39 -04005635
James Smartda0436e2009-05-22 14:51:39 -04005636 /* Free the completion queue EQ event pool */
5637 lpfc_sli4_cq_event_release_all(phba);
5638 lpfc_sli4_cq_event_pool_destroy(phba);
5639
James Smart6d368e52011-05-24 11:44:12 -04005640 /* Release resource identifiers. */
5641 lpfc_sli4_dealloc_resource_identifiers(phba);
5642
James Smartda0436e2009-05-22 14:51:39 -04005643 /* Free the bsmbx region. */
5644 lpfc_destroy_bootstrap_mbox(phba);
5645
5646 /* Free the SLI Layer memory with SLI4 HBAs */
5647 lpfc_mem_free_all(phba);
5648
5649 /* Free the current connect table */
5650 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04005651 &phba->fcf_conn_rec_list, list) {
5652 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04005653 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04005654 }
James Smartda0436e2009-05-22 14:51:39 -04005655
5656 return;
5657}
5658
5659/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005660 * lpfc_init_api_table_setup - Set up init api function jump table
James Smart3772a992009-05-22 14:50:54 -04005661 * @phba: The hba struct for which this call is being executed.
5662 * @dev_grp: The HBA PCI-Device group number.
5663 *
5664 * This routine sets up the device INIT interface API function jump table
5665 * in @phba struct.
5666 *
5667 * Returns: 0 - success, -ENODEV - failure.
5668 **/
5669int
5670lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5671{
James Smart84d1b002010-02-12 14:42:33 -05005672 phba->lpfc_hba_init_link = lpfc_hba_init_link;
5673 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart7f860592011-03-11 16:05:52 -05005674 phba->lpfc_selective_reset = lpfc_selective_reset;
James Smart3772a992009-05-22 14:50:54 -04005675 switch (dev_grp) {
5676 case LPFC_PCI_DEV_LP:
5677 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
5678 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
5679 phba->lpfc_stop_port = lpfc_stop_port_s3;
5680 break;
James Smartda0436e2009-05-22 14:51:39 -04005681 case LPFC_PCI_DEV_OC:
5682 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
5683 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
5684 phba->lpfc_stop_port = lpfc_stop_port_s4;
5685 break;
James Smart3772a992009-05-22 14:50:54 -04005686 default:
5687 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5688 "1431 Invalid HBA PCI-device group: 0x%x\n",
5689 dev_grp);
5690 return -ENODEV;
5691 break;
5692 }
5693 return 0;
5694}
5695
5696/**
5697 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
5698 * @phba: pointer to lpfc hba data structure.
5699 *
5700 * This routine is invoked to set up the driver internal resources before the
5701 * device specific resource setup to support the HBA device it attached to.
5702 *
5703 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005704 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005705 * other values - error
5706 **/
5707static int
5708lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
5709{
5710 /*
5711 * Driver resources common to all SLI revisions
5712 */
James Smartea2151b2008-09-07 11:52:10 -04005713 atomic_set(&phba->fast_event_count, 0);
James Smart2e0fef82007-06-17 19:56:36 -05005714 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005715
James Smarte47c9092008-02-08 18:49:26 -05005716 /* Initialize ndlp management spinlock */
5717 spin_lock_init(&phba->ndlp_lock);
5718
James Smart2e0fef82007-06-17 19:56:36 -05005719 INIT_LIST_HEAD(&phba->port_list);
James Smart3772a992009-05-22 14:50:54 -04005720 INIT_LIST_HEAD(&phba->work_list);
James Smart84774a42008-08-24 21:50:06 -04005721 init_waitqueue_head(&phba->wait_4_mlo_m_q);
dea31012005-04-17 16:05:31 -05005722
James Smart3772a992009-05-22 14:50:54 -04005723 /* Initialize the wait queue head for the kernel thread */
5724 init_waitqueue_head(&phba->work_waitq);
James Smart858c9f62007-06-17 19:56:39 -05005725
James Smart3772a992009-05-22 14:50:54 -04005726 /* Initialize the scsi buffer list used by driver for scsi IO */
James Smarta40fc5f2013-04-17 20:17:40 -04005727 spin_lock_init(&phba->scsi_buf_list_get_lock);
5728 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_get);
5729 spin_lock_init(&phba->scsi_buf_list_put_lock);
5730 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
dea31012005-04-17 16:05:31 -05005731
James Smart3772a992009-05-22 14:50:54 -04005732 /* Initialize the fabric iocb list */
5733 INIT_LIST_HEAD(&phba->fabric_iocb_list);
dea31012005-04-17 16:05:31 -05005734
James Smart3772a992009-05-22 14:50:54 -04005735 /* Initialize list to save ELS buffers */
5736 INIT_LIST_HEAD(&phba->elsbuf);
dea31012005-04-17 16:05:31 -05005737
James Smart3772a992009-05-22 14:50:54 -04005738 /* Initialize FCF connection rec list */
5739 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
dea31012005-04-17 16:05:31 -05005740
James Smart1ba981f2014-02-20 09:56:45 -05005741 /* Initialize OAS configuration list */
5742 spin_lock_init(&phba->devicelock);
5743 INIT_LIST_HEAD(&phba->luns);
5744
James Smart3772a992009-05-22 14:50:54 -04005745 return 0;
5746}
dea31012005-04-17 16:05:31 -05005747
James Smart3772a992009-05-22 14:50:54 -04005748/**
5749 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
5750 * @phba: pointer to lpfc hba data structure.
5751 *
5752 * This routine is invoked to set up the driver internal resources after the
5753 * device specific resource setup to support the HBA device it attached to.
5754 *
5755 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005756 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005757 * other values - error
5758 **/
5759static int
5760lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
5761{
5762 int error;
5763
5764 /* Startup the kernel thread for this host adapter. */
5765 phba->worker_thread = kthread_run(lpfc_do_work, phba,
5766 "lpfc_worker_%d", phba->brd_no);
5767 if (IS_ERR(phba->worker_thread)) {
5768 error = PTR_ERR(phba->worker_thread);
5769 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005770 }
5771
James Smart3772a992009-05-22 14:50:54 -04005772 return 0;
5773}
5774
5775/**
5776 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
5777 * @phba: pointer to lpfc hba data structure.
5778 *
5779 * This routine is invoked to unset the driver internal resources set up after
5780 * the device specific resource setup for supporting the HBA device it
5781 * attached to.
5782 **/
5783static void
5784lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
5785{
5786 /* Stop kernel worker thread */
5787 kthread_stop(phba->worker_thread);
5788}
5789
5790/**
5791 * lpfc_free_iocb_list - Free iocb list.
5792 * @phba: pointer to lpfc hba data structure.
5793 *
5794 * This routine is invoked to free the driver's IOCB list and memory.
5795 **/
5796static void
5797lpfc_free_iocb_list(struct lpfc_hba *phba)
5798{
5799 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
5800
5801 spin_lock_irq(&phba->hbalock);
5802 list_for_each_entry_safe(iocbq_entry, iocbq_next,
5803 &phba->lpfc_iocb_list, list) {
5804 list_del(&iocbq_entry->list);
5805 kfree(iocbq_entry);
5806 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005807 }
James Smart3772a992009-05-22 14:50:54 -04005808 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005809
James Smart3772a992009-05-22 14:50:54 -04005810 return;
5811}
dea31012005-04-17 16:05:31 -05005812
James Smart3772a992009-05-22 14:50:54 -04005813/**
5814 * lpfc_init_iocb_list - Allocate and initialize iocb list.
5815 * @phba: pointer to lpfc hba data structure.
5816 *
5817 * This routine is invoked to allocate and initizlize the driver's IOCB
5818 * list and set up the IOCB tag array accordingly.
5819 *
5820 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005821 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005822 * other values - error
5823 **/
5824static int
5825lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
5826{
5827 struct lpfc_iocbq *iocbq_entry = NULL;
5828 uint16_t iotag;
5829 int i;
dea31012005-04-17 16:05:31 -05005830
5831 /* Initialize and populate the iocb list per host. */
5832 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04005833 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07005834 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05005835 if (iocbq_entry == NULL) {
5836 printk(KERN_ERR "%s: only allocated %d iocbs of "
5837 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07005838 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05005839 goto out_free_iocbq;
5840 }
5841
James Bottomley604a3e32005-10-29 10:28:33 -05005842 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
5843 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04005844 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05005845 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04005846 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05005847 goto out_free_iocbq;
5848 }
James Smart6d368e52011-05-24 11:44:12 -04005849 iocbq_entry->sli4_lxritag = NO_XRI;
James Smart3772a992009-05-22 14:50:54 -04005850 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05005851
5852 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005853 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
5854 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05005855 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005856 }
5857
James Smart3772a992009-05-22 14:50:54 -04005858 return 0;
5859
5860out_free_iocbq:
5861 lpfc_free_iocb_list(phba);
5862
5863 return -ENOMEM;
5864}
5865
5866/**
James Smart8a9d2e82012-05-09 21:16:12 -04005867 * lpfc_free_sgl_list - Free a given sgl list.
James Smartda0436e2009-05-22 14:51:39 -04005868 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -04005869 * @sglq_list: pointer to the head of sgl list.
James Smartda0436e2009-05-22 14:51:39 -04005870 *
James Smart8a9d2e82012-05-09 21:16:12 -04005871 * This routine is invoked to free a give sgl list and memory.
James Smartda0436e2009-05-22 14:51:39 -04005872 **/
James Smart8a9d2e82012-05-09 21:16:12 -04005873void
5874lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
James Smartda0436e2009-05-22 14:51:39 -04005875{
5876 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart8a9d2e82012-05-09 21:16:12 -04005877
5878 list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
5879 list_del(&sglq_entry->list);
5880 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
5881 kfree(sglq_entry);
5882 }
5883}
5884
5885/**
5886 * lpfc_free_els_sgl_list - Free els sgl list.
5887 * @phba: pointer to lpfc hba data structure.
5888 *
5889 * This routine is invoked to free the driver's els sgl list and memory.
5890 **/
5891static void
5892lpfc_free_els_sgl_list(struct lpfc_hba *phba)
5893{
James Smartda0436e2009-05-22 14:51:39 -04005894 LIST_HEAD(sglq_list);
James Smartdafe8ce2014-09-03 12:56:40 -04005895 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smartda0436e2009-05-22 14:51:39 -04005896
James Smart8a9d2e82012-05-09 21:16:12 -04005897 /* Retrieve all els sgls from driver list */
James Smartda0436e2009-05-22 14:51:39 -04005898 spin_lock_irq(&phba->hbalock);
James Smartdafe8ce2014-09-03 12:56:40 -04005899 spin_lock(&pring->ring_lock);
James Smartda0436e2009-05-22 14:51:39 -04005900 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
James Smartdafe8ce2014-09-03 12:56:40 -04005901 spin_unlock(&pring->ring_lock);
James Smartda0436e2009-05-22 14:51:39 -04005902 spin_unlock_irq(&phba->hbalock);
5903
James Smart8a9d2e82012-05-09 21:16:12 -04005904 /* Now free the sgl list */
5905 lpfc_free_sgl_list(phba, &sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04005906}
5907
5908/**
5909 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
5910 * @phba: pointer to lpfc hba data structure.
5911 *
5912 * This routine is invoked to allocate the driver's active sgl memory.
5913 * This array will hold the sglq_entry's for active IOs.
5914 **/
5915static int
5916lpfc_init_active_sgl_array(struct lpfc_hba *phba)
5917{
5918 int size;
5919 size = sizeof(struct lpfc_sglq *);
5920 size *= phba->sli4_hba.max_cfg_param.max_xri;
5921
5922 phba->sli4_hba.lpfc_sglq_active_list =
5923 kzalloc(size, GFP_KERNEL);
5924 if (!phba->sli4_hba.lpfc_sglq_active_list)
5925 return -ENOMEM;
5926 return 0;
5927}
5928
5929/**
5930 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
5931 * @phba: pointer to lpfc hba data structure.
5932 *
5933 * This routine is invoked to walk through the array of active sglq entries
5934 * and free all of the resources.
5935 * This is just a place holder for now.
5936 **/
5937static void
5938lpfc_free_active_sgl(struct lpfc_hba *phba)
5939{
5940 kfree(phba->sli4_hba.lpfc_sglq_active_list);
5941}
5942
5943/**
5944 * lpfc_init_sgl_list - Allocate and initialize sgl list.
5945 * @phba: pointer to lpfc hba data structure.
5946 *
5947 * This routine is invoked to allocate and initizlize the driver's sgl
5948 * list and set up the sgl xritag tag array accordingly.
5949 *
James Smartda0436e2009-05-22 14:51:39 -04005950 **/
James Smart8a9d2e82012-05-09 21:16:12 -04005951static void
James Smartda0436e2009-05-22 14:51:39 -04005952lpfc_init_sgl_list(struct lpfc_hba *phba)
5953{
James Smartda0436e2009-05-22 14:51:39 -04005954 /* Initialize and populate the sglq list per host/VF. */
5955 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
5956 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
5957
James Smart8a9d2e82012-05-09 21:16:12 -04005958 /* els xri-sgl book keeping */
5959 phba->sli4_hba.els_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04005960
James Smart8a9d2e82012-05-09 21:16:12 -04005961 /* scsi xri-buffer book keeping */
James Smartda0436e2009-05-22 14:51:39 -04005962 phba->sli4_hba.scsi_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04005963}
5964
5965/**
5966 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
5967 * @phba: pointer to lpfc hba data structure.
5968 *
5969 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -04005970 * port for those SLI4 ports that do not support extents. This routine
James Smartda0436e2009-05-22 14:51:39 -04005971 * posts a PAGE_SIZE memory region to the port to hold up to
James Smart88a2cfb2011-07-22 18:36:33 -04005972 * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
5973 * and should be called only when interrupts are disabled.
James Smartda0436e2009-05-22 14:51:39 -04005974 *
5975 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005976 * 0 - successful
James Smart88a2cfb2011-07-22 18:36:33 -04005977 * -ERROR - otherwise.
James Smartda0436e2009-05-22 14:51:39 -04005978 **/
5979int
5980lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
5981{
5982 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04005983 struct lpfc_rpi_hdr *rpi_hdr;
5984
5985 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
James Smartff78d8f2011-12-13 13:21:35 -05005986 if (!phba->sli4_hba.rpi_hdrs_in_use)
James Smart6d368e52011-05-24 11:44:12 -04005987 return rc;
James Smart6d368e52011-05-24 11:44:12 -04005988 if (phba->sli4_hba.extents_in_use)
5989 return -EIO;
James Smartda0436e2009-05-22 14:51:39 -04005990
5991 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
5992 if (!rpi_hdr) {
5993 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5994 "0391 Error during rpi post operation\n");
5995 lpfc_sli4_remove_rpis(phba);
5996 rc = -ENODEV;
5997 }
5998
5999 return rc;
6000}
6001
6002/**
6003 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
6004 * @phba: pointer to lpfc hba data structure.
6005 *
6006 * This routine is invoked to allocate a single 4KB memory region to
6007 * support rpis and stores them in the phba. This single region
6008 * provides support for up to 64 rpis. The region is used globally
6009 * by the device.
6010 *
6011 * Returns:
6012 * A valid rpi hdr on success.
6013 * A NULL pointer on any failure.
6014 **/
6015struct lpfc_rpi_hdr *
6016lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
6017{
6018 uint16_t rpi_limit, curr_rpi_range;
6019 struct lpfc_dmabuf *dmabuf;
6020 struct lpfc_rpi_hdr *rpi_hdr;
James Smart9589b0622011-04-16 11:03:17 -04006021 uint32_t rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04006022
James Smart6d368e52011-05-24 11:44:12 -04006023 /*
6024 * If the SLI4 port supports extents, posting the rpi header isn't
6025 * required. Set the expected maximum count and let the actual value
6026 * get set when extents are fully allocated.
6027 */
6028 if (!phba->sli4_hba.rpi_hdrs_in_use)
6029 return NULL;
6030 if (phba->sli4_hba.extents_in_use)
6031 return NULL;
6032
6033 /* The limit on the logical index is just the max_rpi count. */
James Smartda0436e2009-05-22 14:51:39 -04006034 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
James Smart6d368e52011-05-24 11:44:12 -04006035 phba->sli4_hba.max_cfg_param.max_rpi - 1;
James Smartda0436e2009-05-22 14:51:39 -04006036
6037 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04006038 /*
6039 * Establish the starting RPI in this header block. The starting
6040 * rpi is normalized to a zero base because the physical rpi is
6041 * port based.
6042 */
James Smart97f2ecf2012-03-01 22:35:23 -05006043 curr_rpi_range = phba->sli4_hba.next_rpi;
James Smartda0436e2009-05-22 14:51:39 -04006044 spin_unlock_irq(&phba->hbalock);
6045
6046 /*
6047 * The port has a limited number of rpis. The increment here
6048 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
6049 * and to allow the full max_rpi range per port.
6050 */
6051 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
James Smart9589b0622011-04-16 11:03:17 -04006052 rpi_count = rpi_limit - curr_rpi_range;
6053 else
6054 rpi_count = LPFC_RPI_HDR_COUNT;
James Smartda0436e2009-05-22 14:51:39 -04006055
James Smart6d368e52011-05-24 11:44:12 -04006056 if (!rpi_count)
6057 return NULL;
James Smartda0436e2009-05-22 14:51:39 -04006058 /*
6059 * First allocate the protocol header region for the port. The
6060 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
6061 */
6062 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
6063 if (!dmabuf)
6064 return NULL;
6065
Joe Perches1aee3832014-09-03 12:56:12 -04006066 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
6067 LPFC_HDR_TEMPLATE_SIZE,
6068 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04006069 if (!dmabuf->virt) {
6070 rpi_hdr = NULL;
6071 goto err_free_dmabuf;
6072 }
6073
James Smartda0436e2009-05-22 14:51:39 -04006074 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
6075 rpi_hdr = NULL;
6076 goto err_free_coherent;
6077 }
6078
6079 /* Save the rpi header data for cleanup later. */
6080 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
6081 if (!rpi_hdr)
6082 goto err_free_coherent;
6083
6084 rpi_hdr->dmabuf = dmabuf;
6085 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
6086 rpi_hdr->page_count = 1;
6087 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04006088
6089 /* The rpi_hdr stores the logical index only. */
6090 rpi_hdr->start_rpi = curr_rpi_range;
James Smartda0436e2009-05-22 14:51:39 -04006091 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
6092
6093 /*
James Smart6d368e52011-05-24 11:44:12 -04006094 * The next_rpi stores the next logical module-64 rpi value used
6095 * to post physical rpis in subsequent rpi postings.
James Smartda0436e2009-05-22 14:51:39 -04006096 */
James Smart9589b0622011-04-16 11:03:17 -04006097 phba->sli4_hba.next_rpi += rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04006098 spin_unlock_irq(&phba->hbalock);
6099 return rpi_hdr;
6100
6101 err_free_coherent:
6102 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
6103 dmabuf->virt, dmabuf->phys);
6104 err_free_dmabuf:
6105 kfree(dmabuf);
6106 return NULL;
6107}
6108
6109/**
6110 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
6111 * @phba: pointer to lpfc hba data structure.
6112 *
6113 * This routine is invoked to remove all memory resources allocated
James Smart6d368e52011-05-24 11:44:12 -04006114 * to support rpis for SLI4 ports not supporting extents. This routine
6115 * presumes the caller has released all rpis consumed by fabric or port
6116 * logins and is prepared to have the header pages removed.
James Smartda0436e2009-05-22 14:51:39 -04006117 **/
6118void
6119lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
6120{
6121 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
6122
James Smart6d368e52011-05-24 11:44:12 -04006123 if (!phba->sli4_hba.rpi_hdrs_in_use)
6124 goto exit;
6125
James Smartda0436e2009-05-22 14:51:39 -04006126 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
6127 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
6128 list_del(&rpi_hdr->list);
6129 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
6130 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
6131 kfree(rpi_hdr->dmabuf);
6132 kfree(rpi_hdr);
6133 }
James Smart6d368e52011-05-24 11:44:12 -04006134 exit:
6135 /* There are no rpis available to the port now. */
6136 phba->sli4_hba.next_rpi = 0;
James Smartda0436e2009-05-22 14:51:39 -04006137}
6138
6139/**
James Smart3772a992009-05-22 14:50:54 -04006140 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
6141 * @pdev: pointer to pci device data structure.
6142 *
6143 * This routine is invoked to allocate the driver hba data structure for an
6144 * HBA device. If the allocation is successful, the phba reference to the
6145 * PCI device data structure is set.
6146 *
6147 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006148 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04006149 * NULL - error
6150 **/
6151static struct lpfc_hba *
6152lpfc_hba_alloc(struct pci_dev *pdev)
6153{
6154 struct lpfc_hba *phba;
6155
6156 /* Allocate memory for HBA structure */
6157 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
6158 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02006159 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04006160 return NULL;
6161 }
6162
6163 /* Set reference to PCI device in HBA structure */
6164 phba->pcidev = pdev;
6165
6166 /* Assign an unused board number */
6167 phba->brd_no = lpfc_get_instance();
6168 if (phba->brd_no < 0) {
6169 kfree(phba);
6170 return NULL;
6171 }
James Smart65791f12016-07-06 12:35:56 -07006172 phba->eratt_poll_interval = LPFC_ERATT_POLL_INTERVAL;
James Smart3772a992009-05-22 14:50:54 -04006173
James Smart4fede782010-01-26 23:08:55 -05006174 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04006175 INIT_LIST_HEAD(&phba->ct_ev_waiters);
6176
James Smart3772a992009-05-22 14:50:54 -04006177 return phba;
6178}
6179
6180/**
6181 * lpfc_hba_free - Free driver hba data structure with a device.
6182 * @phba: pointer to lpfc hba data structure.
6183 *
6184 * This routine is invoked to free the driver hba data structure with an
6185 * HBA device.
6186 **/
6187static void
6188lpfc_hba_free(struct lpfc_hba *phba)
6189{
6190 /* Release the driver assigned board number */
6191 idr_remove(&lpfc_hba_index, phba->brd_no);
6192
James Smart2a76a282012-08-03 12:35:54 -04006193 /* Free memory allocated with sli rings */
6194 kfree(phba->sli.ring);
6195 phba->sli.ring = NULL;
6196
James Smart3772a992009-05-22 14:50:54 -04006197 kfree(phba);
6198 return;
6199}
6200
6201/**
6202 * lpfc_create_shost - Create hba physical port with associated scsi host.
6203 * @phba: pointer to lpfc hba data structure.
6204 *
6205 * This routine is invoked to create HBA physical port and associate a SCSI
6206 * host with it.
6207 *
6208 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006209 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006210 * other values - error
6211 **/
6212static int
6213lpfc_create_shost(struct lpfc_hba *phba)
6214{
6215 struct lpfc_vport *vport;
6216 struct Scsi_Host *shost;
6217
6218 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05006219 phba->fc_edtov = FF_DEF_EDTOV;
6220 phba->fc_ratov = FF_DEF_RATOV;
6221 phba->fc_altov = FF_DEF_ALTOV;
6222 phba->fc_arbtov = FF_DEF_ARBTOV;
6223
James Smartd7c47992010-06-08 18:31:54 -04006224 atomic_set(&phba->sdev_cnt, 0);
James Smart3de2a652007-08-02 11:09:59 -04006225 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05006226 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04006227 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05006228
6229 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05006230 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05006231 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04006232 /* Put reference to SCSI host to driver's device private data */
6233 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05006234
James Smart4258e982015-12-16 18:11:58 -05006235 /*
6236 * At this point we are fully registered with PSA. In addition,
6237 * any initial discovery should be completed.
6238 */
6239 vport->load_flag |= FC_ALLOW_FDMI;
James Smart8663cbb2016-03-31 14:12:33 -07006240 if (phba->cfg_enable_SmartSAN ||
6241 (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
James Smart4258e982015-12-16 18:11:58 -05006242
6243 /* Setup appropriate attribute masks */
6244 vport->fdmi_hba_mask = LPFC_FDMI2_HBA_ATTR;
James Smart8663cbb2016-03-31 14:12:33 -07006245 if (phba->cfg_enable_SmartSAN)
James Smart4258e982015-12-16 18:11:58 -05006246 vport->fdmi_port_mask = LPFC_FDMI2_SMART_ATTR;
6247 else
6248 vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
6249 }
James Smart3772a992009-05-22 14:50:54 -04006250 return 0;
6251}
dea31012005-04-17 16:05:31 -05006252
James Smart3772a992009-05-22 14:50:54 -04006253/**
6254 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
6255 * @phba: pointer to lpfc hba data structure.
6256 *
6257 * This routine is invoked to destroy HBA physical port and the associated
6258 * SCSI host.
6259 **/
6260static void
6261lpfc_destroy_shost(struct lpfc_hba *phba)
6262{
6263 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04006264
James Smart3772a992009-05-22 14:50:54 -04006265 /* Destroy physical port that associated with the SCSI host */
6266 destroy_port(vport);
6267
6268 return;
6269}
6270
6271/**
6272 * lpfc_setup_bg - Setup Block guard structures and debug areas.
6273 * @phba: pointer to lpfc hba data structure.
6274 * @shost: the shost to be used to detect Block guard settings.
6275 *
6276 * This routine sets up the local Block guard protocol settings for @shost.
6277 * This routine also allocates memory for debugging bg buffers.
6278 **/
6279static void
6280lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
6281{
James Smartbbeb79b2012-06-12 13:54:27 -04006282 uint32_t old_mask;
6283 uint32_t old_guard;
6284
James Smart3772a992009-05-22 14:50:54 -04006285 int pagecnt = 10;
6286 if (lpfc_prot_mask && lpfc_prot_guard) {
6287 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6288 "1478 Registering BlockGuard with the "
6289 "SCSI layer\n");
James Smartbbeb79b2012-06-12 13:54:27 -04006290
6291 old_mask = lpfc_prot_mask;
6292 old_guard = lpfc_prot_guard;
6293
6294 /* Only allow supported values */
6295 lpfc_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
6296 SHOST_DIX_TYPE0_PROTECTION |
6297 SHOST_DIX_TYPE1_PROTECTION);
6298 lpfc_prot_guard &= (SHOST_DIX_GUARD_IP | SHOST_DIX_GUARD_CRC);
6299
6300 /* DIF Type 1 protection for profiles AST1/C1 is end to end */
6301 if (lpfc_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
6302 lpfc_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
6303
6304 if (lpfc_prot_mask && lpfc_prot_guard) {
6305 if ((old_mask != lpfc_prot_mask) ||
6306 (old_guard != lpfc_prot_guard))
6307 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6308 "1475 Registering BlockGuard with the "
6309 "SCSI layer: mask %d guard %d\n",
6310 lpfc_prot_mask, lpfc_prot_guard);
6311
6312 scsi_host_set_prot(shost, lpfc_prot_mask);
6313 scsi_host_set_guard(shost, lpfc_prot_guard);
6314 } else
6315 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6316 "1479 Not Registering BlockGuard with the SCSI "
6317 "layer, Bad protection parameters: %d %d\n",
6318 old_mask, old_guard);
James Smart98c9ea52007-10-27 13:37:33 -04006319 }
James Smartbbeb79b2012-06-12 13:54:27 -04006320
James Smart81301a92008-12-04 22:39:46 -05006321 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05006322 while (pagecnt) {
6323 spin_lock_init(&_dump_buf_lock);
6324 _dump_buf_data =
6325 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
6326 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04006327 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6328 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04006329 "_dump_buf_data at 0x%p\n",
6330 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05006331 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04006332 memset(_dump_buf_data, 0,
6333 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05006334 break;
James Smart3772a992009-05-22 14:50:54 -04006335 } else
James Smart81301a92008-12-04 22:39:46 -05006336 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05006337 }
James Smart81301a92008-12-04 22:39:46 -05006338 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04006339 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6340 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04006341 "memory for hexdump\n");
6342 } else
James Smart6a9c52c2009-10-02 15:16:51 -04006343 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6344 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05006345 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05006346 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05006347 while (pagecnt) {
6348 _dump_buf_dif =
6349 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
6350 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04006351 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6352 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04006353 "_dump_buf_dif at 0x%p\n",
6354 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05006355 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04006356 memset(_dump_buf_dif, 0,
6357 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05006358 break;
James Smart3772a992009-05-22 14:50:54 -04006359 } else
James Smart81301a92008-12-04 22:39:46 -05006360 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05006361 }
James Smart81301a92008-12-04 22:39:46 -05006362 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04006363 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6364 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04006365 "memory for hexdump\n");
6366 } else
James Smart6a9c52c2009-10-02 15:16:51 -04006367 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6368 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04006369 _dump_buf_dif);
6370}
6371
6372/**
6373 * lpfc_post_init_setup - Perform necessary device post initialization setup.
6374 * @phba: pointer to lpfc hba data structure.
6375 *
6376 * This routine is invoked to perform all the necessary post initialization
6377 * setup for the device.
6378 **/
6379static void
6380lpfc_post_init_setup(struct lpfc_hba *phba)
6381{
6382 struct Scsi_Host *shost;
6383 struct lpfc_adapter_event_header adapter_event;
6384
6385 /* Get the default values for Model Name and Description */
6386 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
6387
6388 /*
6389 * hba setup may have changed the hba_queue_depth so we need to
6390 * adjust the value of can_queue.
6391 */
6392 shost = pci_get_drvdata(phba->pcidev);
6393 shost->can_queue = phba->cfg_hba_queue_depth - 10;
6394 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
6395 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05006396
6397 lpfc_host_attrib_init(shost);
6398
James Smart2e0fef82007-06-17 19:56:36 -05006399 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
6400 spin_lock_irq(shost->host_lock);
6401 lpfc_poll_start_timer(phba);
6402 spin_unlock_irq(shost->host_lock);
6403 }
James Smart8f6d98d2006-08-01 07:34:00 -04006404
James Smart93996272008-08-24 21:50:30 -04006405 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6406 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04006407 /* Send board arrival event to upper layer */
6408 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
6409 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
6410 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04006411 sizeof(adapter_event),
6412 (char *) &adapter_event,
6413 LPFC_NL_VENDOR_ID);
6414 return;
6415}
6416
6417/**
6418 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
6419 * @phba: pointer to lpfc hba data structure.
6420 *
6421 * This routine is invoked to set up the PCI device memory space for device
6422 * with SLI-3 interface spec.
6423 *
6424 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006425 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006426 * other values - error
6427 **/
6428static int
6429lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
6430{
6431 struct pci_dev *pdev;
6432 unsigned long bar0map_len, bar2map_len;
6433 int i, hbq_count;
6434 void *ptr;
6435 int error = -ENODEV;
6436
6437 /* Obtain PCI device reference */
6438 if (!phba->pcidev)
6439 return error;
6440 else
6441 pdev = phba->pcidev;
6442
6443 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05006444 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
6445 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
6446 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
6447 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smart3772a992009-05-22 14:50:54 -04006448 return error;
Michael Reed8e685972009-09-18 12:02:05 -05006449 }
6450 }
James Smart3772a992009-05-22 14:50:54 -04006451
6452 /* Get the bus address of Bar0 and Bar2 and the number of bytes
6453 * required by each mapping.
6454 */
6455 phba->pci_bar0_map = pci_resource_start(pdev, 0);
6456 bar0map_len = pci_resource_len(pdev, 0);
6457
6458 phba->pci_bar2_map = pci_resource_start(pdev, 2);
6459 bar2map_len = pci_resource_len(pdev, 2);
6460
6461 /* Map HBA SLIM to a kernel virtual address. */
6462 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
6463 if (!phba->slim_memmap_p) {
6464 dev_printk(KERN_ERR, &pdev->dev,
6465 "ioremap failed for SLIM memory.\n");
6466 goto out;
6467 }
6468
6469 /* Map HBA Control Registers to a kernel virtual address. */
6470 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
6471 if (!phba->ctrl_regs_memmap_p) {
6472 dev_printk(KERN_ERR, &pdev->dev,
6473 "ioremap failed for HBA control registers.\n");
6474 goto out_iounmap_slim;
6475 }
6476
6477 /* Allocate memory for SLI-2 structures */
Joe Perches1aee3832014-09-03 12:56:12 -04006478 phba->slim2p.virt = dma_zalloc_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6479 &phba->slim2p.phys, GFP_KERNEL);
James Smart3772a992009-05-22 14:50:54 -04006480 if (!phba->slim2p.virt)
6481 goto out_iounmap;
6482
James Smart3772a992009-05-22 14:50:54 -04006483 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04006484 phba->mbox_ext = (phba->slim2p.virt +
6485 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04006486 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
6487 phba->IOCBs = (phba->slim2p.virt +
6488 offsetof(struct lpfc_sli2_slim, IOCBs));
6489
6490 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
6491 lpfc_sli_hbq_size(),
6492 &phba->hbqslimp.phys,
6493 GFP_KERNEL);
6494 if (!phba->hbqslimp.virt)
6495 goto out_free_slim;
6496
6497 hbq_count = lpfc_sli_hbq_count();
6498 ptr = phba->hbqslimp.virt;
6499 for (i = 0; i < hbq_count; ++i) {
6500 phba->hbqs[i].hbq_virt = ptr;
6501 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
6502 ptr += (lpfc_hbq_defs[i]->entry_count *
6503 sizeof(struct lpfc_hbq_entry));
6504 }
6505 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
6506 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
6507
6508 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
6509
6510 INIT_LIST_HEAD(&phba->rb_pend_list);
6511
6512 phba->MBslimaddr = phba->slim_memmap_p;
6513 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
6514 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
6515 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
6516 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04006517
dea31012005-04-17 16:05:31 -05006518 return 0;
6519
dea31012005-04-17 16:05:31 -05006520out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04006521 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6522 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05006523out_iounmap:
6524 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05006525out_iounmap_slim:
dea31012005-04-17 16:05:31 -05006526 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05006527out:
6528 return error;
6529}
6530
James Smarte59058c2008-08-24 21:49:00 -04006531/**
James Smart3772a992009-05-22 14:50:54 -04006532 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
6533 * @phba: pointer to lpfc hba data structure.
6534 *
6535 * This routine is invoked to unset the PCI device memory space for device
6536 * with SLI-3 interface spec.
6537 **/
6538static void
6539lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
6540{
6541 struct pci_dev *pdev;
6542
6543 /* Obtain PCI device reference */
6544 if (!phba->pcidev)
6545 return;
6546 else
6547 pdev = phba->pcidev;
6548
6549 /* Free coherent DMA memory allocated */
6550 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
6551 phba->hbqslimp.virt, phba->hbqslimp.phys);
6552 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6553 phba->slim2p.virt, phba->slim2p.phys);
6554
6555 /* I/O memory unmap */
6556 iounmap(phba->ctrl_regs_memmap_p);
6557 iounmap(phba->slim_memmap_p);
6558
6559 return;
6560}
6561
6562/**
James Smartda0436e2009-05-22 14:51:39 -04006563 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
6564 * @phba: pointer to lpfc hba data structure.
6565 *
6566 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
6567 * done and check status.
6568 *
6569 * Return 0 if successful, otherwise -ENODEV.
6570 **/
6571int
6572lpfc_sli4_post_status_check(struct lpfc_hba *phba)
6573{
James Smart2fcee4b2010-12-15 17:57:46 -05006574 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
6575 struct lpfc_register reg_data;
6576 int i, port_error = 0;
6577 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04006578
James Smart9940b972011-03-11 16:06:12 -05006579 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
6580 memset(&reg_data, 0, sizeof(reg_data));
James Smart2fcee4b2010-12-15 17:57:46 -05006581 if (!phba->sli4_hba.PSMPHRregaddr)
James Smartda0436e2009-05-22 14:51:39 -04006582 return -ENODEV;
6583
James Smartda0436e2009-05-22 14:51:39 -04006584 /* Wait up to 30 seconds for the SLI Port POST done and ready */
6585 for (i = 0; i < 3000; i++) {
James Smart9940b972011-03-11 16:06:12 -05006586 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
6587 &portsmphr_reg.word0) ||
6588 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
James Smart2fcee4b2010-12-15 17:57:46 -05006589 /* Port has a fatal POST error, break out */
James Smartda0436e2009-05-22 14:51:39 -04006590 port_error = -ENODEV;
6591 break;
6592 }
James Smart2fcee4b2010-12-15 17:57:46 -05006593 if (LPFC_POST_STAGE_PORT_READY ==
6594 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
James Smartda0436e2009-05-22 14:51:39 -04006595 break;
James Smartda0436e2009-05-22 14:51:39 -04006596 msleep(10);
6597 }
6598
James Smart2fcee4b2010-12-15 17:57:46 -05006599 /*
6600 * If there was a port error during POST, then don't proceed with
6601 * other register reads as the data may not be valid. Just exit.
6602 */
6603 if (port_error) {
James Smartda0436e2009-05-22 14:51:39 -04006604 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05006605 "1408 Port Failed POST - portsmphr=0x%x, "
6606 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
6607 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
6608 portsmphr_reg.word0,
6609 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
6610 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
6611 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
6612 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
6613 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
6614 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
6615 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
6616 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
6617 } else {
James Smart28baac72010-02-12 14:42:03 -05006618 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05006619 "2534 Device Info: SLIFamily=0x%x, "
6620 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
6621 "SLIHint_2=0x%x, FT=0x%x\n",
James Smart28baac72010-02-12 14:42:03 -05006622 bf_get(lpfc_sli_intf_sli_family,
6623 &phba->sli4_hba.sli_intf),
6624 bf_get(lpfc_sli_intf_slirev,
6625 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05006626 bf_get(lpfc_sli_intf_if_type,
James Smart28baac72010-02-12 14:42:03 -05006627 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05006628 bf_get(lpfc_sli_intf_sli_hint1,
6629 &phba->sli4_hba.sli_intf),
6630 bf_get(lpfc_sli_intf_sli_hint2,
6631 &phba->sli4_hba.sli_intf),
6632 bf_get(lpfc_sli_intf_func_type,
James Smart28baac72010-02-12 14:42:03 -05006633 &phba->sli4_hba.sli_intf));
James Smart2fcee4b2010-12-15 17:57:46 -05006634 /*
6635 * Check for other Port errors during the initialization
6636 * process. Fail the load if the port did not come up
6637 * correctly.
6638 */
6639 if_type = bf_get(lpfc_sli_intf_if_type,
6640 &phba->sli4_hba.sli_intf);
6641 switch (if_type) {
6642 case LPFC_SLI_INTF_IF_TYPE_0:
6643 phba->sli4_hba.ue_mask_lo =
6644 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
6645 phba->sli4_hba.ue_mask_hi =
6646 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
6647 uerrlo_reg.word0 =
6648 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
6649 uerrhi_reg.word0 =
6650 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
6651 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
6652 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
6653 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6654 "1422 Unrecoverable Error "
6655 "Detected during POST "
6656 "uerr_lo_reg=0x%x, "
6657 "uerr_hi_reg=0x%x, "
6658 "ue_mask_lo_reg=0x%x, "
6659 "ue_mask_hi_reg=0x%x\n",
6660 uerrlo_reg.word0,
6661 uerrhi_reg.word0,
6662 phba->sli4_hba.ue_mask_lo,
6663 phba->sli4_hba.ue_mask_hi);
6664 port_error = -ENODEV;
6665 }
6666 break;
6667 case LPFC_SLI_INTF_IF_TYPE_2:
6668 /* Final checks. The port status should be clean. */
James Smart9940b972011-03-11 16:06:12 -05006669 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
6670 &reg_data.word0) ||
James Smart05580562011-05-24 11:40:48 -04006671 (bf_get(lpfc_sliport_status_err, &reg_data) &&
6672 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
James Smart2fcee4b2010-12-15 17:57:46 -05006673 phba->work_status[0] =
6674 readl(phba->sli4_hba.u.if_type2.
6675 ERR1regaddr);
6676 phba->work_status[1] =
6677 readl(phba->sli4_hba.u.if_type2.
6678 ERR2regaddr);
6679 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05006680 "2888 Unrecoverable port error "
6681 "following POST: port status reg "
6682 "0x%x, port_smphr reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05006683 "error 1=0x%x, error 2=0x%x\n",
6684 reg_data.word0,
6685 portsmphr_reg.word0,
6686 phba->work_status[0],
6687 phba->work_status[1]);
6688 port_error = -ENODEV;
6689 }
6690 break;
6691 case LPFC_SLI_INTF_IF_TYPE_1:
6692 default:
6693 break;
6694 }
James Smart28baac72010-02-12 14:42:03 -05006695 }
James Smartda0436e2009-05-22 14:51:39 -04006696 return port_error;
6697}
6698
6699/**
6700 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
6701 * @phba: pointer to lpfc hba data structure.
James Smart2fcee4b2010-12-15 17:57:46 -05006702 * @if_type: The SLI4 interface type getting configured.
James Smartda0436e2009-05-22 14:51:39 -04006703 *
6704 * This routine is invoked to set up SLI4 BAR0 PCI config space register
6705 * memory map.
6706 **/
6707static void
James Smart2fcee4b2010-12-15 17:57:46 -05006708lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04006709{
James Smart2fcee4b2010-12-15 17:57:46 -05006710 switch (if_type) {
6711 case LPFC_SLI_INTF_IF_TYPE_0:
6712 phba->sli4_hba.u.if_type0.UERRLOregaddr =
6713 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
6714 phba->sli4_hba.u.if_type0.UERRHIregaddr =
6715 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
6716 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
6717 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
6718 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
6719 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
6720 phba->sli4_hba.SLIINTFregaddr =
6721 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
6722 break;
6723 case LPFC_SLI_INTF_IF_TYPE_2:
6724 phba->sli4_hba.u.if_type2.ERR1regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006725 phba->sli4_hba.conf_regs_memmap_p +
6726 LPFC_CTL_PORT_ER1_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006727 phba->sli4_hba.u.if_type2.ERR2regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006728 phba->sli4_hba.conf_regs_memmap_p +
6729 LPFC_CTL_PORT_ER2_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006730 phba->sli4_hba.u.if_type2.CTRLregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006731 phba->sli4_hba.conf_regs_memmap_p +
6732 LPFC_CTL_PORT_CTL_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006733 phba->sli4_hba.u.if_type2.STATUSregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006734 phba->sli4_hba.conf_regs_memmap_p +
6735 LPFC_CTL_PORT_STA_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006736 phba->sli4_hba.SLIINTFregaddr =
6737 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
6738 phba->sli4_hba.PSMPHRregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006739 phba->sli4_hba.conf_regs_memmap_p +
6740 LPFC_CTL_PORT_SEM_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006741 phba->sli4_hba.RQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05006742 phba->sli4_hba.conf_regs_memmap_p +
6743 LPFC_ULP0_RQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05006744 phba->sli4_hba.WQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05006745 phba->sli4_hba.conf_regs_memmap_p +
6746 LPFC_ULP0_WQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05006747 phba->sli4_hba.EQCQDBregaddr =
6748 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
6749 phba->sli4_hba.MQDBregaddr =
6750 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
6751 phba->sli4_hba.BMBXregaddr =
6752 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
6753 break;
6754 case LPFC_SLI_INTF_IF_TYPE_1:
6755 default:
6756 dev_printk(KERN_ERR, &phba->pcidev->dev,
6757 "FATAL - unsupported SLI4 interface type - %d\n",
6758 if_type);
6759 break;
6760 }
James Smartda0436e2009-05-22 14:51:39 -04006761}
6762
6763/**
6764 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
6765 * @phba: pointer to lpfc hba data structure.
6766 *
6767 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
6768 * memory map.
6769 **/
6770static void
6771lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
6772{
James Smart2fcee4b2010-12-15 17:57:46 -05006773 phba->sli4_hba.PSMPHRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
6774 LPFC_SLIPORT_IF0_SMPHR;
James Smartda0436e2009-05-22 14:51:39 -04006775 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006776 LPFC_HST_ISR0;
James Smartda0436e2009-05-22 14:51:39 -04006777 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006778 LPFC_HST_IMR0;
James Smartda0436e2009-05-22 14:51:39 -04006779 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006780 LPFC_HST_ISCR0;
James Smartda0436e2009-05-22 14:51:39 -04006781}
6782
6783/**
6784 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
6785 * @phba: pointer to lpfc hba data structure.
6786 * @vf: virtual function number
6787 *
6788 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
6789 * based on the given viftual function number, @vf.
6790 *
6791 * Return 0 if successful, otherwise -ENODEV.
6792 **/
6793static int
6794lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
6795{
6796 if (vf > LPFC_VIR_FUNC_MAX)
6797 return -ENODEV;
6798
6799 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05006800 vf * LPFC_VFR_PAGE_SIZE +
6801 LPFC_ULP0_RQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04006802 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05006803 vf * LPFC_VFR_PAGE_SIZE +
6804 LPFC_ULP0_WQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04006805 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6806 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
6807 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6808 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
6809 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6810 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
6811 return 0;
6812}
6813
6814/**
6815 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
6816 * @phba: pointer to lpfc hba data structure.
6817 *
6818 * This routine is invoked to create the bootstrap mailbox
6819 * region consistent with the SLI-4 interface spec. This
6820 * routine allocates all memory necessary to communicate
6821 * mailbox commands to the port and sets up all alignment
6822 * needs. No locks are expected to be held when calling
6823 * this routine.
6824 *
6825 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006826 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04006827 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04006828 **/
6829static int
6830lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
6831{
6832 uint32_t bmbx_size;
6833 struct lpfc_dmabuf *dmabuf;
6834 struct dma_address *dma_address;
6835 uint32_t pa_addr;
6836 uint64_t phys_addr;
6837
6838 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
6839 if (!dmabuf)
6840 return -ENOMEM;
6841
6842 /*
6843 * The bootstrap mailbox region is comprised of 2 parts
6844 * plus an alignment restriction of 16 bytes.
6845 */
6846 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
Joe Perches1aee3832014-09-03 12:56:12 -04006847 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, bmbx_size,
6848 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04006849 if (!dmabuf->virt) {
6850 kfree(dmabuf);
6851 return -ENOMEM;
6852 }
James Smartda0436e2009-05-22 14:51:39 -04006853
6854 /*
6855 * Initialize the bootstrap mailbox pointers now so that the register
6856 * operations are simple later. The mailbox dma address is required
6857 * to be 16-byte aligned. Also align the virtual memory as each
6858 * maibox is copied into the bmbx mailbox region before issuing the
6859 * command to the port.
6860 */
6861 phba->sli4_hba.bmbx.dmabuf = dmabuf;
6862 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
6863
6864 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
6865 LPFC_ALIGN_16_BYTE);
6866 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
6867 LPFC_ALIGN_16_BYTE);
6868
6869 /*
6870 * Set the high and low physical addresses now. The SLI4 alignment
6871 * requirement is 16 bytes and the mailbox is posted to the port
6872 * as two 30-bit addresses. The other data is a bit marking whether
6873 * the 30-bit address is the high or low address.
6874 * Upcast bmbx aphys to 64bits so shift instruction compiles
6875 * clean on 32 bit machines.
6876 */
6877 dma_address = &phba->sli4_hba.bmbx.dma_address;
6878 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
6879 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
6880 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
6881 LPFC_BMBX_BIT1_ADDR_HI);
6882
6883 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
6884 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
6885 LPFC_BMBX_BIT1_ADDR_LO);
6886 return 0;
6887}
6888
6889/**
6890 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
6891 * @phba: pointer to lpfc hba data structure.
6892 *
6893 * This routine is invoked to teardown the bootstrap mailbox
6894 * region and release all host resources. This routine requires
6895 * the caller to ensure all mailbox commands recovered, no
6896 * additional mailbox comands are sent, and interrupts are disabled
6897 * before calling this routine.
6898 *
6899 **/
6900static void
6901lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
6902{
6903 dma_free_coherent(&phba->pcidev->dev,
6904 phba->sli4_hba.bmbx.bmbx_size,
6905 phba->sli4_hba.bmbx.dmabuf->virt,
6906 phba->sli4_hba.bmbx.dmabuf->phys);
6907
6908 kfree(phba->sli4_hba.bmbx.dmabuf);
6909 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
6910}
6911
6912/**
6913 * lpfc_sli4_read_config - Get the config parameters.
6914 * @phba: pointer to lpfc hba data structure.
6915 *
6916 * This routine is invoked to read the configuration parameters from the HBA.
6917 * The configuration parameters are used to set the base and maximum values
6918 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
6919 * allocation for the port.
6920 *
6921 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006922 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006923 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006924 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006925 **/
James Smartff78d8f2011-12-13 13:21:35 -05006926int
James Smartda0436e2009-05-22 14:51:39 -04006927lpfc_sli4_read_config(struct lpfc_hba *phba)
6928{
6929 LPFC_MBOXQ_t *pmb;
6930 struct lpfc_mbx_read_config *rd_config;
James Smart912e3ac2011-05-24 11:42:11 -04006931 union lpfc_sli4_cfg_shdr *shdr;
6932 uint32_t shdr_status, shdr_add_status;
6933 struct lpfc_mbx_get_func_cfg *get_func_cfg;
6934 struct lpfc_rsrc_desc_fcfcoe *desc;
James Smart8aa134a2012-08-14 14:25:29 -04006935 char *pdesc_0;
James Smart8aa134a2012-08-14 14:25:29 -04006936 int length, i, rc = 0, rc2;
James Smartda0436e2009-05-22 14:51:39 -04006937
6938 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6939 if (!pmb) {
6940 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6941 "2011 Unable to allocate memory for issuing "
6942 "SLI_CONFIG_SPECIAL mailbox command\n");
6943 return -ENOMEM;
6944 }
6945
6946 lpfc_read_config(phba, pmb);
6947
6948 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6949 if (rc != MBX_SUCCESS) {
6950 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6951 "2012 Mailbox failed , mbxCmd x%x "
6952 "READ_CONFIG, mbxStatus x%x\n",
6953 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6954 bf_get(lpfc_mqe_status, &pmb->u.mqe));
6955 rc = -EIO;
6956 } else {
6957 rd_config = &pmb->u.mqe.un.rd_config;
James Smartff78d8f2011-12-13 13:21:35 -05006958 if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
6959 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
6960 phba->sli4_hba.lnk_info.lnk_tp =
6961 bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
6962 phba->sli4_hba.lnk_info.lnk_no =
6963 bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
6964 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6965 "3081 lnk_type:%d, lnk_numb:%d\n",
6966 phba->sli4_hba.lnk_info.lnk_tp,
6967 phba->sli4_hba.lnk_info.lnk_no);
6968 } else
6969 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6970 "3082 Mailbox (x%x) returned ldv:x0\n",
6971 bf_get(lpfc_mqe_command, &pmb->u.mqe));
James Smart6d368e52011-05-24 11:44:12 -04006972 phba->sli4_hba.extents_in_use =
6973 bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04006974 phba->sli4_hba.max_cfg_param.max_xri =
6975 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
6976 phba->sli4_hba.max_cfg_param.xri_base =
6977 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
6978 phba->sli4_hba.max_cfg_param.max_vpi =
6979 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
6980 phba->sli4_hba.max_cfg_param.vpi_base =
6981 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
6982 phba->sli4_hba.max_cfg_param.max_rpi =
6983 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
6984 phba->sli4_hba.max_cfg_param.rpi_base =
6985 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
6986 phba->sli4_hba.max_cfg_param.max_vfi =
6987 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
6988 phba->sli4_hba.max_cfg_param.vfi_base =
6989 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
6990 phba->sli4_hba.max_cfg_param.max_fcfi =
6991 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04006992 phba->sli4_hba.max_cfg_param.max_eq =
6993 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
6994 phba->sli4_hba.max_cfg_param.max_rq =
6995 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
6996 phba->sli4_hba.max_cfg_param.max_wq =
6997 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
6998 phba->sli4_hba.max_cfg_param.max_cq =
6999 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
7000 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
7001 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
7002 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
7003 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
James Smart5ffc2662009-11-18 15:39:44 -05007004 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
7005 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04007006 phba->max_vports = phba->max_vpi;
7007 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart6d368e52011-05-24 11:44:12 -04007008 "2003 cfg params Extents? %d "
7009 "XRI(B:%d M:%d), "
James Smartda0436e2009-05-22 14:51:39 -04007010 "VPI(B:%d M:%d) "
7011 "VFI(B:%d M:%d) "
7012 "RPI(B:%d M:%d) "
James Smart6d368e52011-05-24 11:44:12 -04007013 "FCFI(Count:%d)\n",
7014 phba->sli4_hba.extents_in_use,
James Smartda0436e2009-05-22 14:51:39 -04007015 phba->sli4_hba.max_cfg_param.xri_base,
7016 phba->sli4_hba.max_cfg_param.max_xri,
7017 phba->sli4_hba.max_cfg_param.vpi_base,
7018 phba->sli4_hba.max_cfg_param.max_vpi,
7019 phba->sli4_hba.max_cfg_param.vfi_base,
7020 phba->sli4_hba.max_cfg_param.max_vfi,
7021 phba->sli4_hba.max_cfg_param.rpi_base,
7022 phba->sli4_hba.max_cfg_param.max_rpi,
James Smartda0436e2009-05-22 14:51:39 -04007023 phba->sli4_hba.max_cfg_param.max_fcfi);
7024 }
James Smart912e3ac2011-05-24 11:42:11 -04007025
7026 if (rc)
7027 goto read_cfg_out;
James Smartda0436e2009-05-22 14:51:39 -04007028
7029 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -04007030 length = phba->sli4_hba.max_cfg_param.max_xri -
7031 lpfc_sli4_get_els_iocb_cnt(phba);
7032 if (phba->cfg_hba_queue_depth > length) {
7033 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7034 "3361 HBA queue depth changed from %d to %d\n",
7035 phba->cfg_hba_queue_depth, length);
7036 phba->cfg_hba_queue_depth = length;
7037 }
James Smart912e3ac2011-05-24 11:42:11 -04007038
7039 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
7040 LPFC_SLI_INTF_IF_TYPE_2)
7041 goto read_cfg_out;
7042
7043 /* get the pf# and vf# for SLI4 if_type 2 port */
7044 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
7045 sizeof(struct lpfc_sli4_cfg_mhdr));
7046 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
7047 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
7048 length, LPFC_SLI4_MBX_EMBED);
7049
James Smart8aa134a2012-08-14 14:25:29 -04007050 rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart912e3ac2011-05-24 11:42:11 -04007051 shdr = (union lpfc_sli4_cfg_shdr *)
7052 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
7053 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7054 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart8aa134a2012-08-14 14:25:29 -04007055 if (rc2 || shdr_status || shdr_add_status) {
James Smart912e3ac2011-05-24 11:42:11 -04007056 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7057 "3026 Mailbox failed , mbxCmd x%x "
7058 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
7059 bf_get(lpfc_mqe_command, &pmb->u.mqe),
7060 bf_get(lpfc_mqe_status, &pmb->u.mqe));
James Smart912e3ac2011-05-24 11:42:11 -04007061 goto read_cfg_out;
7062 }
7063
7064 /* search for fc_fcoe resrouce descriptor */
7065 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
James Smart912e3ac2011-05-24 11:42:11 -04007066
James Smart8aa134a2012-08-14 14:25:29 -04007067 pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
7068 desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
7069 length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
7070 if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
7071 length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
7072 else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
7073 goto read_cfg_out;
7074
James Smart912e3ac2011-05-24 11:42:11 -04007075 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
James Smart8aa134a2012-08-14 14:25:29 -04007076 desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
James Smart912e3ac2011-05-24 11:42:11 -04007077 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
James Smart8aa134a2012-08-14 14:25:29 -04007078 bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
James Smart912e3ac2011-05-24 11:42:11 -04007079 phba->sli4_hba.iov.pf_number =
7080 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
7081 phba->sli4_hba.iov.vf_number =
7082 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
7083 break;
7084 }
7085 }
7086
7087 if (i < LPFC_RSRC_DESC_MAX_NUM)
7088 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7089 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
7090 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
7091 phba->sli4_hba.iov.vf_number);
James Smart8aa134a2012-08-14 14:25:29 -04007092 else
James Smart912e3ac2011-05-24 11:42:11 -04007093 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7094 "3028 GET_FUNCTION_CONFIG: failed to find "
7095 "Resrouce Descriptor:x%x\n",
7096 LPFC_RSRC_DESC_TYPE_FCFCOE);
James Smart912e3ac2011-05-24 11:42:11 -04007097
7098read_cfg_out:
7099 mempool_free(pmb, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04007100 return rc;
7101}
7102
7103/**
James Smart2fcee4b2010-12-15 17:57:46 -05007104 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
James Smartda0436e2009-05-22 14:51:39 -04007105 * @phba: pointer to lpfc hba data structure.
7106 *
James Smart2fcee4b2010-12-15 17:57:46 -05007107 * This routine is invoked to setup the port-side endian order when
7108 * the port if_type is 0. This routine has no function for other
7109 * if_types.
James Smartda0436e2009-05-22 14:51:39 -04007110 *
7111 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007112 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007113 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007114 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007115 **/
7116static int
7117lpfc_setup_endian_order(struct lpfc_hba *phba)
7118{
7119 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05007120 uint32_t if_type, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04007121 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
7122 HOST_ENDIAN_HIGH_WORD1};
7123
James Smart2fcee4b2010-12-15 17:57:46 -05007124 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7125 switch (if_type) {
7126 case LPFC_SLI_INTF_IF_TYPE_0:
7127 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
7128 GFP_KERNEL);
7129 if (!mboxq) {
7130 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7131 "0492 Unable to allocate memory for "
7132 "issuing SLI_CONFIG_SPECIAL mailbox "
7133 "command\n");
7134 return -ENOMEM;
7135 }
James Smartda0436e2009-05-22 14:51:39 -04007136
James Smart2fcee4b2010-12-15 17:57:46 -05007137 /*
7138 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
7139 * two words to contain special data values and no other data.
7140 */
7141 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
7142 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
7143 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7144 if (rc != MBX_SUCCESS) {
7145 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7146 "0493 SLI_CONFIG_SPECIAL mailbox "
7147 "failed with status x%x\n",
7148 rc);
7149 rc = -EIO;
7150 }
7151 mempool_free(mboxq, phba->mbox_mem_pool);
7152 break;
7153 case LPFC_SLI_INTF_IF_TYPE_2:
7154 case LPFC_SLI_INTF_IF_TYPE_1:
7155 default:
7156 break;
James Smartda0436e2009-05-22 14:51:39 -04007157 }
James Smartda0436e2009-05-22 14:51:39 -04007158 return rc;
7159}
7160
7161/**
James Smart5350d872011-10-10 21:33:49 -04007162 * lpfc_sli4_queue_verify - Verify and update EQ and CQ counts
James Smartda0436e2009-05-22 14:51:39 -04007163 * @phba: pointer to lpfc hba data structure.
7164 *
James Smart5350d872011-10-10 21:33:49 -04007165 * This routine is invoked to check the user settable queue counts for EQs and
7166 * CQs. after this routine is called the counts will be set to valid values that
7167 * adhere to the constraints of the system's interrupt vectors and the port's
7168 * queue resources.
James Smartda0436e2009-05-22 14:51:39 -04007169 *
7170 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007171 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007172 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04007173 **/
7174static int
James Smart5350d872011-10-10 21:33:49 -04007175lpfc_sli4_queue_verify(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04007176{
James Smart67d12732012-08-03 12:36:13 -04007177 int cfg_fcp_io_channel;
James Smart90695ee2012-08-14 14:25:36 -04007178 uint32_t cpu;
7179 uint32_t i = 0;
James Smart1ba981f2014-02-20 09:56:45 -05007180 int fof_vectors = phba->cfg_fof ? 1 : 0;
James Smartda0436e2009-05-22 14:51:39 -04007181
7182 /*
James Smart67d12732012-08-03 12:36:13 -04007183 * Sanity check for configured queue parameters against the run-time
James Smartda0436e2009-05-22 14:51:39 -04007184 * device parameters
7185 */
7186
James Smart67d12732012-08-03 12:36:13 -04007187 /* Sanity check on HBA EQ parameters */
7188 cfg_fcp_io_channel = phba->cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04007189
James Smart7bb03bb2013-04-17 20:19:16 -04007190 /* It doesn't make sense to have more io channels then online CPUs */
7191 for_each_present_cpu(cpu) {
7192 if (cpu_online(cpu))
7193 i++;
James Smart90695ee2012-08-14 14:25:36 -04007194 }
James Smart7bb03bb2013-04-17 20:19:16 -04007195 phba->sli4_hba.num_online_cpu = i;
James Smartb246de12013-05-31 17:03:07 -04007196 phba->sli4_hba.num_present_cpu = lpfc_present_cpu;
James Smart76fd07a2014-02-20 09:57:18 -05007197 phba->sli4_hba.curr_disp_cpu = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04007198
James Smart90695ee2012-08-14 14:25:36 -04007199 if (i < cfg_fcp_io_channel) {
James Smart82c3e9b2012-09-29 11:29:50 -04007200 lpfc_printf_log(phba,
7201 KERN_ERR, LOG_INIT,
James Smart90695ee2012-08-14 14:25:36 -04007202 "3188 Reducing IO channels to match number of "
James Smart7bb03bb2013-04-17 20:19:16 -04007203 "online CPUs: from %d to %d\n",
7204 cfg_fcp_io_channel, i);
James Smart90695ee2012-08-14 14:25:36 -04007205 cfg_fcp_io_channel = i;
7206 }
7207
James Smart1ba981f2014-02-20 09:56:45 -05007208 if (cfg_fcp_io_channel + fof_vectors >
James Smart67d12732012-08-03 12:36:13 -04007209 phba->sli4_hba.max_cfg_param.max_eq) {
James Smart82c3e9b2012-09-29 11:29:50 -04007210 if (phba->sli4_hba.max_cfg_param.max_eq <
7211 LPFC_FCP_IO_CHAN_MIN) {
James Smartda0436e2009-05-22 14:51:39 -04007212 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7213 "2574 Not enough EQs (%d) from the "
7214 "pci function for supporting FCP "
7215 "EQs (%d)\n",
7216 phba->sli4_hba.max_cfg_param.max_eq,
James Smart67d12732012-08-03 12:36:13 -04007217 phba->cfg_fcp_io_channel);
James Smartda0436e2009-05-22 14:51:39 -04007218 goto out_error;
7219 }
James Smart82c3e9b2012-09-29 11:29:50 -04007220 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7221 "2575 Reducing IO channels to match number of "
7222 "available EQs: from %d to %d\n",
7223 cfg_fcp_io_channel,
7224 phba->sli4_hba.max_cfg_param.max_eq);
James Smart1ba981f2014-02-20 09:56:45 -05007225 cfg_fcp_io_channel = phba->sli4_hba.max_cfg_param.max_eq -
7226 fof_vectors;
James Smartda0436e2009-05-22 14:51:39 -04007227 }
James Smart67d12732012-08-03 12:36:13 -04007228
James Smartda0436e2009-05-22 14:51:39 -04007229 /* The actual number of FCP event queues adopted */
James Smart67d12732012-08-03 12:36:13 -04007230 phba->cfg_fcp_io_channel = cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04007231
James Smartda0436e2009-05-22 14:51:39 -04007232 /* Get EQ depth from module parameter, fake the default for now */
7233 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
7234 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
7235
James Smart5350d872011-10-10 21:33:49 -04007236 /* Get CQ depth from module parameter, fake the default for now */
7237 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
7238 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
7239
7240 return 0;
7241out_error:
7242 return -ENOMEM;
7243}
7244
7245/**
7246 * lpfc_sli4_queue_create - Create all the SLI4 queues
7247 * @phba: pointer to lpfc hba data structure.
7248 *
7249 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
7250 * operation. For each SLI4 queue type, the parameters such as queue entry
7251 * count (queue depth) shall be taken from the module parameter. For now,
7252 * we just use some constant number as place holder.
7253 *
7254 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07007255 * 0 - successful
James Smart5350d872011-10-10 21:33:49 -04007256 * -ENOMEM - No availble memory
7257 * -EIO - The mailbox failed to complete successfully.
7258 **/
7259int
7260lpfc_sli4_queue_create(struct lpfc_hba *phba)
7261{
7262 struct lpfc_queue *qdesc;
James Smart67d12732012-08-03 12:36:13 -04007263 int idx;
James Smart5350d872011-10-10 21:33:49 -04007264
7265 /*
James Smart67d12732012-08-03 12:36:13 -04007266 * Create HBA Record arrays.
James Smart5350d872011-10-10 21:33:49 -04007267 */
James Smart67d12732012-08-03 12:36:13 -04007268 if (!phba->cfg_fcp_io_channel)
7269 return -ERANGE;
James Smart5350d872011-10-10 21:33:49 -04007270
James Smart67d12732012-08-03 12:36:13 -04007271 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
7272 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
7273 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
7274 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
7275 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
7276 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
7277
7278 phba->sli4_hba.hba_eq = kzalloc((sizeof(struct lpfc_queue *) *
7279 phba->cfg_fcp_io_channel), GFP_KERNEL);
7280 if (!phba->sli4_hba.hba_eq) {
James Smartda0436e2009-05-22 14:51:39 -04007281 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04007282 "2576 Failed allocate memory for "
7283 "fast-path EQ record array\n");
James Smartda0436e2009-05-22 14:51:39 -04007284 goto out_error;
7285 }
James Smart67d12732012-08-03 12:36:13 -04007286
7287 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
7288 phba->cfg_fcp_io_channel), GFP_KERNEL);
7289 if (!phba->sli4_hba.fcp_cq) {
7290 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7291 "2577 Failed allocate memory for fast-path "
7292 "CQ record array\n");
7293 goto out_error;
7294 }
7295
7296 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
7297 phba->cfg_fcp_io_channel), GFP_KERNEL);
7298 if (!phba->sli4_hba.fcp_wq) {
7299 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7300 "2578 Failed allocate memory for fast-path "
7301 "WQ record array\n");
7302 goto out_error;
7303 }
James Smartda0436e2009-05-22 14:51:39 -04007304
James Smart5350d872011-10-10 21:33:49 -04007305 /*
James Smart67d12732012-08-03 12:36:13 -04007306 * Since the first EQ can have multiple CQs associated with it,
7307 * this array is used to quickly see if we have a FCP fast-path
7308 * CQ match.
James Smart5350d872011-10-10 21:33:49 -04007309 */
James Smart67d12732012-08-03 12:36:13 -04007310 phba->sli4_hba.fcp_cq_map = kzalloc((sizeof(uint16_t) *
7311 phba->cfg_fcp_io_channel), GFP_KERNEL);
7312 if (!phba->sli4_hba.fcp_cq_map) {
7313 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7314 "2545 Failed allocate memory for fast-path "
7315 "CQ map\n");
7316 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007317 }
James Smart67d12732012-08-03 12:36:13 -04007318
7319 /*
7320 * Create HBA Event Queues (EQs). The cfg_fcp_io_channel specifies
7321 * how many EQs to create.
7322 */
7323 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7324
7325 /* Create EQs */
James Smartda0436e2009-05-22 14:51:39 -04007326 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
7327 phba->sli4_hba.eq_ecount);
7328 if (!qdesc) {
7329 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04007330 "0497 Failed allocate EQ (%d)\n", idx);
7331 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007332 }
James Smart67d12732012-08-03 12:36:13 -04007333 phba->sli4_hba.hba_eq[idx] = qdesc;
7334
7335 /* Create Fast Path FCP CQs */
7336 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7337 phba->sli4_hba.cq_ecount);
7338 if (!qdesc) {
7339 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7340 "0499 Failed allocate fast-path FCP "
7341 "CQ (%d)\n", idx);
7342 goto out_error;
7343 }
7344 phba->sli4_hba.fcp_cq[idx] = qdesc;
7345
7346 /* Create Fast Path FCP WQs */
James Smartb5c53952016-03-31 14:12:30 -07007347 if (phba->fcp_embed_io) {
7348 qdesc = lpfc_sli4_queue_alloc(phba,
7349 LPFC_WQE128_SIZE,
7350 LPFC_WQE128_DEF_COUNT);
7351 } else {
7352 qdesc = lpfc_sli4_queue_alloc(phba,
7353 phba->sli4_hba.wq_esize,
7354 phba->sli4_hba.wq_ecount);
7355 }
James Smart67d12732012-08-03 12:36:13 -04007356 if (!qdesc) {
7357 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7358 "0503 Failed allocate fast-path FCP "
7359 "WQ (%d)\n", idx);
7360 goto out_error;
7361 }
7362 phba->sli4_hba.fcp_wq[idx] = qdesc;
James Smartda0436e2009-05-22 14:51:39 -04007363 }
7364
James Smart67d12732012-08-03 12:36:13 -04007365
James Smartda0436e2009-05-22 14:51:39 -04007366 /*
James Smart67d12732012-08-03 12:36:13 -04007367 * Create Slow Path Completion Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04007368 */
7369
James Smartda0436e2009-05-22 14:51:39 -04007370 /* Create slow-path Mailbox Command Complete Queue */
7371 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7372 phba->sli4_hba.cq_ecount);
7373 if (!qdesc) {
7374 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7375 "0500 Failed allocate slow-path mailbox CQ\n");
James Smart67d12732012-08-03 12:36:13 -04007376 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007377 }
7378 phba->sli4_hba.mbx_cq = qdesc;
7379
7380 /* Create slow-path ELS Complete Queue */
7381 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7382 phba->sli4_hba.cq_ecount);
7383 if (!qdesc) {
7384 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7385 "0501 Failed allocate slow-path ELS CQ\n");
James Smart67d12732012-08-03 12:36:13 -04007386 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007387 }
7388 phba->sli4_hba.els_cq = qdesc;
7389
James Smartda0436e2009-05-22 14:51:39 -04007390
James Smart5350d872011-10-10 21:33:49 -04007391 /*
James Smart67d12732012-08-03 12:36:13 -04007392 * Create Slow Path Work Queues (WQs)
James Smart5350d872011-10-10 21:33:49 -04007393 */
James Smartda0436e2009-05-22 14:51:39 -04007394
7395 /* Create Mailbox Command Queue */
James Smartda0436e2009-05-22 14:51:39 -04007396
7397 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
7398 phba->sli4_hba.mq_ecount);
7399 if (!qdesc) {
7400 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7401 "0505 Failed allocate slow-path MQ\n");
James Smart67d12732012-08-03 12:36:13 -04007402 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007403 }
7404 phba->sli4_hba.mbx_wq = qdesc;
7405
7406 /*
James Smart67d12732012-08-03 12:36:13 -04007407 * Create ELS Work Queues
James Smartda0436e2009-05-22 14:51:39 -04007408 */
James Smartda0436e2009-05-22 14:51:39 -04007409
7410 /* Create slow-path ELS Work Queue */
7411 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
7412 phba->sli4_hba.wq_ecount);
7413 if (!qdesc) {
7414 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7415 "0504 Failed allocate slow-path ELS WQ\n");
James Smart67d12732012-08-03 12:36:13 -04007416 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007417 }
7418 phba->sli4_hba.els_wq = qdesc;
7419
James Smartda0436e2009-05-22 14:51:39 -04007420 /*
7421 * Create Receive Queue (RQ)
7422 */
James Smartda0436e2009-05-22 14:51:39 -04007423
7424 /* Create Receive Queue for header */
7425 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
7426 phba->sli4_hba.rq_ecount);
7427 if (!qdesc) {
7428 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7429 "0506 Failed allocate receive HRQ\n");
James Smart67d12732012-08-03 12:36:13 -04007430 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007431 }
7432 phba->sli4_hba.hdr_rq = qdesc;
7433
7434 /* Create Receive Queue for data */
7435 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
7436 phba->sli4_hba.rq_ecount);
7437 if (!qdesc) {
7438 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7439 "0507 Failed allocate receive DRQ\n");
James Smart67d12732012-08-03 12:36:13 -04007440 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007441 }
7442 phba->sli4_hba.dat_rq = qdesc;
7443
James Smart1ba981f2014-02-20 09:56:45 -05007444 /* Create the Queues needed for Flash Optimized Fabric operations */
7445 if (phba->cfg_fof)
7446 lpfc_fof_queue_create(phba);
James Smartda0436e2009-05-22 14:51:39 -04007447 return 0;
7448
James Smartda0436e2009-05-22 14:51:39 -04007449out_error:
James Smart67d12732012-08-03 12:36:13 -04007450 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007451 return -ENOMEM;
7452}
7453
7454/**
7455 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
7456 * @phba: pointer to lpfc hba data structure.
7457 *
7458 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
7459 * operation.
7460 *
7461 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007462 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007463 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007464 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007465 **/
James Smart5350d872011-10-10 21:33:49 -04007466void
James Smartda0436e2009-05-22 14:51:39 -04007467lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
7468{
James Smart67d12732012-08-03 12:36:13 -04007469 int idx;
7470
James Smart1ba981f2014-02-20 09:56:45 -05007471 if (phba->cfg_fof)
7472 lpfc_fof_queue_destroy(phba);
7473
James Smart67d12732012-08-03 12:36:13 -04007474 if (phba->sli4_hba.hba_eq != NULL) {
7475 /* Release HBA event queue */
7476 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7477 if (phba->sli4_hba.hba_eq[idx] != NULL) {
7478 lpfc_sli4_queue_free(
7479 phba->sli4_hba.hba_eq[idx]);
7480 phba->sli4_hba.hba_eq[idx] = NULL;
7481 }
7482 }
7483 kfree(phba->sli4_hba.hba_eq);
7484 phba->sli4_hba.hba_eq = NULL;
7485 }
7486
7487 if (phba->sli4_hba.fcp_cq != NULL) {
7488 /* Release FCP completion queue */
7489 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7490 if (phba->sli4_hba.fcp_cq[idx] != NULL) {
7491 lpfc_sli4_queue_free(
7492 phba->sli4_hba.fcp_cq[idx]);
7493 phba->sli4_hba.fcp_cq[idx] = NULL;
7494 }
7495 }
7496 kfree(phba->sli4_hba.fcp_cq);
7497 phba->sli4_hba.fcp_cq = NULL;
7498 }
7499
7500 if (phba->sli4_hba.fcp_wq != NULL) {
7501 /* Release FCP work queue */
7502 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7503 if (phba->sli4_hba.fcp_wq[idx] != NULL) {
7504 lpfc_sli4_queue_free(
7505 phba->sli4_hba.fcp_wq[idx]);
7506 phba->sli4_hba.fcp_wq[idx] = NULL;
7507 }
7508 }
7509 kfree(phba->sli4_hba.fcp_wq);
7510 phba->sli4_hba.fcp_wq = NULL;
7511 }
7512
7513 /* Release FCP CQ mapping array */
7514 if (phba->sli4_hba.fcp_cq_map != NULL) {
7515 kfree(phba->sli4_hba.fcp_cq_map);
7516 phba->sli4_hba.fcp_cq_map = NULL;
7517 }
James Smartda0436e2009-05-22 14:51:39 -04007518
7519 /* Release mailbox command work queue */
James Smart67d12732012-08-03 12:36:13 -04007520 if (phba->sli4_hba.mbx_wq != NULL) {
7521 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
7522 phba->sli4_hba.mbx_wq = NULL;
7523 }
James Smartda0436e2009-05-22 14:51:39 -04007524
7525 /* Release ELS work queue */
James Smart67d12732012-08-03 12:36:13 -04007526 if (phba->sli4_hba.els_wq != NULL) {
7527 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
7528 phba->sli4_hba.els_wq = NULL;
7529 }
James Smartda0436e2009-05-22 14:51:39 -04007530
7531 /* Release unsolicited receive queue */
James Smart67d12732012-08-03 12:36:13 -04007532 if (phba->sli4_hba.hdr_rq != NULL) {
7533 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
7534 phba->sli4_hba.hdr_rq = NULL;
7535 }
7536 if (phba->sli4_hba.dat_rq != NULL) {
7537 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
7538 phba->sli4_hba.dat_rq = NULL;
7539 }
James Smartda0436e2009-05-22 14:51:39 -04007540
James Smartda0436e2009-05-22 14:51:39 -04007541 /* Release ELS complete queue */
James Smart67d12732012-08-03 12:36:13 -04007542 if (phba->sli4_hba.els_cq != NULL) {
7543 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
7544 phba->sli4_hba.els_cq = NULL;
7545 }
James Smartda0436e2009-05-22 14:51:39 -04007546
7547 /* Release mailbox command complete queue */
James Smart67d12732012-08-03 12:36:13 -04007548 if (phba->sli4_hba.mbx_cq != NULL) {
7549 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
7550 phba->sli4_hba.mbx_cq = NULL;
7551 }
James Smartda0436e2009-05-22 14:51:39 -04007552
7553 return;
7554}
7555
7556/**
7557 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
7558 * @phba: pointer to lpfc hba data structure.
7559 *
7560 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
7561 * operation.
7562 *
7563 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007564 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007565 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007566 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007567 **/
7568int
7569lpfc_sli4_queue_setup(struct lpfc_hba *phba)
7570{
James Smart2a76a282012-08-03 12:35:54 -04007571 struct lpfc_sli *psli = &phba->sli;
7572 struct lpfc_sli_ring *pring;
James Smartda0436e2009-05-22 14:51:39 -04007573 int rc = -ENOMEM;
7574 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
7575 int fcp_cq_index = 0;
James Smart962bc512013-01-03 15:44:00 -05007576 uint32_t shdr_status, shdr_add_status;
7577 union lpfc_sli4_cfg_shdr *shdr;
7578 LPFC_MBOXQ_t *mboxq;
7579 uint32_t length;
7580
7581 /* Check for dual-ULP support */
7582 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7583 if (!mboxq) {
7584 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7585 "3249 Unable to allocate memory for "
7586 "QUERY_FW_CFG mailbox command\n");
7587 return -ENOMEM;
7588 }
7589 length = (sizeof(struct lpfc_mbx_query_fw_config) -
7590 sizeof(struct lpfc_sli4_cfg_mhdr));
7591 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7592 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
7593 length, LPFC_SLI4_MBX_EMBED);
7594
7595 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7596
7597 shdr = (union lpfc_sli4_cfg_shdr *)
7598 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7599 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7600 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7601 if (shdr_status || shdr_add_status || rc) {
7602 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7603 "3250 QUERY_FW_CFG mailbox failed with status "
7604 "x%x add_status x%x, mbx status x%x\n",
7605 shdr_status, shdr_add_status, rc);
7606 if (rc != MBX_TIMEOUT)
7607 mempool_free(mboxq, phba->mbox_mem_pool);
7608 rc = -ENXIO;
7609 goto out_error;
7610 }
7611
7612 phba->sli4_hba.fw_func_mode =
7613 mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
7614 phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
7615 phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
James Smart8b017a32015-05-21 13:55:18 -04007616 phba->sli4_hba.physical_port =
7617 mboxq->u.mqe.un.query_fw_cfg.rsp.physical_port;
James Smart962bc512013-01-03 15:44:00 -05007618 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7619 "3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
7620 "ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
7621 phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
7622
7623 if (rc != MBX_TIMEOUT)
7624 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04007625
7626 /*
James Smart67d12732012-08-03 12:36:13 -04007627 * Set up HBA Event Queues (EQs)
James Smartda0436e2009-05-22 14:51:39 -04007628 */
7629
James Smart67d12732012-08-03 12:36:13 -04007630 /* Set up HBA event queue */
7631 if (phba->cfg_fcp_io_channel && !phba->sli4_hba.hba_eq) {
James Smart2e90f4b2011-12-13 13:22:37 -05007632 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7633 "3147 Fast-path EQs not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007634 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007635 goto out_error;
James Smart2e90f4b2011-12-13 13:22:37 -05007636 }
James Smart67d12732012-08-03 12:36:13 -04007637 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
7638 if (!phba->sli4_hba.hba_eq[fcp_eqidx]) {
James Smartda0436e2009-05-22 14:51:39 -04007639 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7640 "0522 Fast-path EQ (%d) not "
7641 "allocated\n", fcp_eqidx);
James Smart1b511972011-12-13 13:23:09 -05007642 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007643 goto out_destroy_hba_eq;
James Smartda0436e2009-05-22 14:51:39 -04007644 }
James Smart67d12732012-08-03 12:36:13 -04007645 rc = lpfc_eq_create(phba, phba->sli4_hba.hba_eq[fcp_eqidx],
James Smartbf8dae82012-08-03 12:36:24 -04007646 (phba->cfg_fcp_imax / phba->cfg_fcp_io_channel));
James Smartda0436e2009-05-22 14:51:39 -04007647 if (rc) {
7648 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7649 "0523 Failed setup of fast-path EQ "
James Smarta2fc4aef2014-09-03 12:57:55 -04007650 "(%d), rc = 0x%x\n", fcp_eqidx,
7651 (uint32_t)rc);
James Smart67d12732012-08-03 12:36:13 -04007652 goto out_destroy_hba_eq;
James Smartda0436e2009-05-22 14:51:39 -04007653 }
7654 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04007655 "2584 HBA EQ setup: "
James Smartda0436e2009-05-22 14:51:39 -04007656 "queue[%d]-id=%d\n", fcp_eqidx,
James Smart67d12732012-08-03 12:36:13 -04007657 phba->sli4_hba.hba_eq[fcp_eqidx]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007658 }
7659
James Smart67d12732012-08-03 12:36:13 -04007660 /* Set up fast-path FCP Response Complete Queue */
7661 if (!phba->sli4_hba.fcp_cq) {
7662 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7663 "3148 Fast-path FCP CQ array not "
7664 "allocated\n");
7665 rc = -ENOMEM;
7666 goto out_destroy_hba_eq;
7667 }
7668
7669 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_io_channel; fcp_cqidx++) {
7670 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
7671 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7672 "0526 Fast-path FCP CQ (%d) not "
7673 "allocated\n", fcp_cqidx);
7674 rc = -ENOMEM;
7675 goto out_destroy_fcp_cq;
7676 }
7677 rc = lpfc_cq_create(phba, phba->sli4_hba.fcp_cq[fcp_cqidx],
7678 phba->sli4_hba.hba_eq[fcp_cqidx], LPFC_WCQ, LPFC_FCP);
7679 if (rc) {
7680 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7681 "0527 Failed setup of fast-path FCP "
James Smarta2fc4aef2014-09-03 12:57:55 -04007682 "CQ (%d), rc = 0x%x\n", fcp_cqidx,
7683 (uint32_t)rc);
James Smart67d12732012-08-03 12:36:13 -04007684 goto out_destroy_fcp_cq;
7685 }
7686
7687 /* Setup fcp_cq_map for fast lookup */
7688 phba->sli4_hba.fcp_cq_map[fcp_cqidx] =
7689 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id;
7690
7691 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7692 "2588 FCP CQ setup: cq[%d]-id=%d, "
7693 "parent seq[%d]-id=%d\n",
7694 fcp_cqidx,
7695 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
7696 fcp_cqidx,
7697 phba->sli4_hba.hba_eq[fcp_cqidx]->queue_id);
7698 }
7699
7700 /* Set up fast-path FCP Work Queue */
7701 if (!phba->sli4_hba.fcp_wq) {
7702 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7703 "3149 Fast-path FCP WQ array not "
7704 "allocated\n");
7705 rc = -ENOMEM;
7706 goto out_destroy_fcp_cq;
7707 }
7708
7709 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_io_channel; fcp_wqidx++) {
7710 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
7711 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7712 "0534 Fast-path FCP WQ (%d) not "
7713 "allocated\n", fcp_wqidx);
7714 rc = -ENOMEM;
7715 goto out_destroy_fcp_wq;
7716 }
7717 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
7718 phba->sli4_hba.fcp_cq[fcp_wqidx],
7719 LPFC_FCP);
7720 if (rc) {
7721 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7722 "0535 Failed setup of fast-path FCP "
James Smarta2fc4aef2014-09-03 12:57:55 -04007723 "WQ (%d), rc = 0x%x\n", fcp_wqidx,
7724 (uint32_t)rc);
James Smart67d12732012-08-03 12:36:13 -04007725 goto out_destroy_fcp_wq;
7726 }
7727
7728 /* Bind this WQ to the next FCP ring */
7729 pring = &psli->ring[MAX_SLI3_CONFIGURED_RINGS + fcp_wqidx];
7730 pring->sli.sli4.wqp = (void *)phba->sli4_hba.fcp_wq[fcp_wqidx];
7731 phba->sli4_hba.fcp_cq[fcp_wqidx]->pring = pring;
7732
7733 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7734 "2591 FCP WQ setup: wq[%d]-id=%d, "
7735 "parent cq[%d]-id=%d\n",
7736 fcp_wqidx,
7737 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
7738 fcp_cq_index,
7739 phba->sli4_hba.fcp_cq[fcp_wqidx]->queue_id);
7740 }
James Smartda0436e2009-05-22 14:51:39 -04007741 /*
7742 * Set up Complete Queues (CQs)
7743 */
7744
7745 /* Set up slow-path MBOX Complete Queue as the first CQ */
7746 if (!phba->sli4_hba.mbx_cq) {
7747 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7748 "0528 Mailbox CQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007749 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007750 goto out_destroy_fcp_wq;
James Smartda0436e2009-05-22 14:51:39 -04007751 }
James Smart67d12732012-08-03 12:36:13 -04007752 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq,
7753 phba->sli4_hba.hba_eq[0], LPFC_MCQ, LPFC_MBOX);
James Smartda0436e2009-05-22 14:51:39 -04007754 if (rc) {
7755 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7756 "0529 Failed setup of slow-path mailbox CQ: "
James Smarta2fc4aef2014-09-03 12:57:55 -04007757 "rc = 0x%x\n", (uint32_t)rc);
James Smart67d12732012-08-03 12:36:13 -04007758 goto out_destroy_fcp_wq;
James Smartda0436e2009-05-22 14:51:39 -04007759 }
7760 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7761 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
7762 phba->sli4_hba.mbx_cq->queue_id,
James Smart67d12732012-08-03 12:36:13 -04007763 phba->sli4_hba.hba_eq[0]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007764
7765 /* Set up slow-path ELS Complete Queue */
7766 if (!phba->sli4_hba.els_cq) {
7767 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7768 "0530 ELS CQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007769 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04007770 goto out_destroy_mbx_cq;
7771 }
James Smart67d12732012-08-03 12:36:13 -04007772 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq,
7773 phba->sli4_hba.hba_eq[0], LPFC_WCQ, LPFC_ELS);
James Smartda0436e2009-05-22 14:51:39 -04007774 if (rc) {
7775 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7776 "0531 Failed setup of slow-path ELS CQ: "
James Smarta2fc4aef2014-09-03 12:57:55 -04007777 "rc = 0x%x\n", (uint32_t)rc);
James Smartda0436e2009-05-22 14:51:39 -04007778 goto out_destroy_mbx_cq;
7779 }
7780 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7781 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
7782 phba->sli4_hba.els_cq->queue_id,
James Smart67d12732012-08-03 12:36:13 -04007783 phba->sli4_hba.hba_eq[0]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007784
7785 /*
7786 * Set up all the Work Queues (WQs)
7787 */
7788
7789 /* Set up Mailbox Command Queue */
7790 if (!phba->sli4_hba.mbx_wq) {
7791 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7792 "0538 Slow-path MQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007793 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007794 goto out_destroy_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04007795 }
7796 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
7797 phba->sli4_hba.mbx_cq, LPFC_MBOX);
7798 if (rc) {
7799 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7800 "0539 Failed setup of slow-path MQ: "
7801 "rc = 0x%x\n", rc);
James Smart67d12732012-08-03 12:36:13 -04007802 goto out_destroy_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04007803 }
7804 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7805 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
7806 phba->sli4_hba.mbx_wq->queue_id,
7807 phba->sli4_hba.mbx_cq->queue_id);
7808
7809 /* Set up slow-path ELS Work Queue */
7810 if (!phba->sli4_hba.els_wq) {
7811 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7812 "0536 Slow-path ELS WQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007813 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04007814 goto out_destroy_mbx_wq;
7815 }
7816 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
7817 phba->sli4_hba.els_cq, LPFC_ELS);
7818 if (rc) {
7819 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7820 "0537 Failed setup of slow-path ELS WQ: "
James Smarta2fc4aef2014-09-03 12:57:55 -04007821 "rc = 0x%x\n", (uint32_t)rc);
James Smartda0436e2009-05-22 14:51:39 -04007822 goto out_destroy_mbx_wq;
7823 }
James Smart2a76a282012-08-03 12:35:54 -04007824
7825 /* Bind this WQ to the ELS ring */
7826 pring = &psli->ring[LPFC_ELS_RING];
7827 pring->sli.sli4.wqp = (void *)phba->sli4_hba.els_wq;
7828 phba->sli4_hba.els_cq->pring = pring;
7829
James Smartda0436e2009-05-22 14:51:39 -04007830 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7831 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
7832 phba->sli4_hba.els_wq->queue_id,
7833 phba->sli4_hba.els_cq->queue_id);
7834
James Smartda0436e2009-05-22 14:51:39 -04007835 /*
7836 * Create Receive Queue (RQ)
7837 */
7838 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
7839 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7840 "0540 Receive Queue not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007841 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007842 goto out_destroy_els_wq;
James Smartda0436e2009-05-22 14:51:39 -04007843 }
James Smart73d91e52011-10-10 21:32:10 -04007844
7845 lpfc_rq_adjust_repost(phba, phba->sli4_hba.hdr_rq, LPFC_ELS_HBQ);
7846 lpfc_rq_adjust_repost(phba, phba->sli4_hba.dat_rq, LPFC_ELS_HBQ);
7847
James Smartda0436e2009-05-22 14:51:39 -04007848 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04007849 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04007850 if (rc) {
7851 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7852 "0541 Failed setup of Receive Queue: "
James Smarta2fc4aef2014-09-03 12:57:55 -04007853 "rc = 0x%x\n", (uint32_t)rc);
James Smartda0436e2009-05-22 14:51:39 -04007854 goto out_destroy_fcp_wq;
7855 }
James Smart73d91e52011-10-10 21:32:10 -04007856
James Smartda0436e2009-05-22 14:51:39 -04007857 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7858 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
7859 "parent cq-id=%d\n",
7860 phba->sli4_hba.hdr_rq->queue_id,
7861 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04007862 phba->sli4_hba.els_cq->queue_id);
James Smart1ba981f2014-02-20 09:56:45 -05007863
7864 if (phba->cfg_fof) {
7865 rc = lpfc_fof_queue_setup(phba);
7866 if (rc) {
7867 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7868 "0549 Failed setup of FOF Queues: "
7869 "rc = 0x%x\n", rc);
7870 goto out_destroy_els_rq;
7871 }
7872 }
James Smart2c9c5a02015-04-07 15:07:15 -04007873
7874 /*
7875 * Configure EQ delay multipier for interrupt coalescing using
7876 * MODIFY_EQ_DELAY for all EQs created, LPFC_MAX_EQ_DELAY at a time.
7877 */
7878 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel;
7879 fcp_eqidx += LPFC_MAX_EQ_DELAY)
7880 lpfc_modify_fcp_eq_delay(phba, fcp_eqidx);
James Smartda0436e2009-05-22 14:51:39 -04007881 return 0;
7882
James Smart1ba981f2014-02-20 09:56:45 -05007883out_destroy_els_rq:
7884 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
James Smart2e90f4b2011-12-13 13:22:37 -05007885out_destroy_els_wq:
James Smartda0436e2009-05-22 14:51:39 -04007886 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7887out_destroy_mbx_wq:
7888 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
James Smart2e90f4b2011-12-13 13:22:37 -05007889out_destroy_els_cq:
James Smartda0436e2009-05-22 14:51:39 -04007890 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
7891out_destroy_mbx_cq:
7892 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
James Smart67d12732012-08-03 12:36:13 -04007893out_destroy_fcp_wq:
7894 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
7895 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
7896out_destroy_fcp_cq:
7897 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
7898 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
7899out_destroy_hba_eq:
James Smartda0436e2009-05-22 14:51:39 -04007900 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
James Smart67d12732012-08-03 12:36:13 -04007901 lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[fcp_eqidx]);
James Smartda0436e2009-05-22 14:51:39 -04007902out_error:
7903 return rc;
7904}
7905
7906/**
7907 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
7908 * @phba: pointer to lpfc hba data structure.
7909 *
7910 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
7911 * operation.
7912 *
7913 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007914 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007915 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007916 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007917 **/
7918void
7919lpfc_sli4_queue_unset(struct lpfc_hba *phba)
7920{
7921 int fcp_qidx;
7922
James Smart1ba981f2014-02-20 09:56:45 -05007923 /* Unset the queues created for Flash Optimized Fabric operations */
7924 if (phba->cfg_fof)
7925 lpfc_fof_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007926 /* Unset mailbox command work queue */
7927 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
7928 /* Unset ELS work queue */
7929 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7930 /* Unset unsolicited receive queue */
7931 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
7932 /* Unset FCP work queue */
James Smart67d12732012-08-03 12:36:13 -04007933 if (phba->sli4_hba.fcp_wq) {
7934 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
7935 fcp_qidx++)
7936 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
7937 }
James Smartda0436e2009-05-22 14:51:39 -04007938 /* Unset mailbox command complete queue */
7939 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
7940 /* Unset ELS complete queue */
7941 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
James Smartda0436e2009-05-22 14:51:39 -04007942 /* Unset FCP response complete queue */
James Smart2e90f4b2011-12-13 13:22:37 -05007943 if (phba->sli4_hba.fcp_cq) {
James Smart67d12732012-08-03 12:36:13 -04007944 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
7945 fcp_qidx++)
James Smart2e90f4b2011-12-13 13:22:37 -05007946 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05007947 }
James Smartda0436e2009-05-22 14:51:39 -04007948 /* Unset fast-path event queue */
James Smart67d12732012-08-03 12:36:13 -04007949 if (phba->sli4_hba.hba_eq) {
7950 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
James Smart2e90f4b2011-12-13 13:22:37 -05007951 fcp_qidx++)
James Smart67d12732012-08-03 12:36:13 -04007952 lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[fcp_qidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05007953 }
James Smartda0436e2009-05-22 14:51:39 -04007954}
7955
7956/**
7957 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
7958 * @phba: pointer to lpfc hba data structure.
7959 *
7960 * This routine is invoked to allocate and set up a pool of completion queue
7961 * events. The body of the completion queue event is a completion queue entry
7962 * CQE. For now, this pool is used for the interrupt service routine to queue
7963 * the following HBA completion queue events for the worker thread to process:
7964 * - Mailbox asynchronous events
7965 * - Receive queue completion unsolicited events
7966 * Later, this can be used for all the slow-path events.
7967 *
7968 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007969 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007970 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04007971 **/
7972static int
7973lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
7974{
7975 struct lpfc_cq_event *cq_event;
7976 int i;
7977
7978 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
7979 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
7980 if (!cq_event)
7981 goto out_pool_create_fail;
7982 list_add_tail(&cq_event->list,
7983 &phba->sli4_hba.sp_cqe_event_pool);
7984 }
7985 return 0;
7986
7987out_pool_create_fail:
7988 lpfc_sli4_cq_event_pool_destroy(phba);
7989 return -ENOMEM;
7990}
7991
7992/**
7993 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
7994 * @phba: pointer to lpfc hba data structure.
7995 *
7996 * This routine is invoked to free the pool of completion queue events at
7997 * driver unload time. Note that, it is the responsibility of the driver
7998 * cleanup routine to free all the outstanding completion-queue events
7999 * allocated from this pool back into the pool before invoking this routine
8000 * to destroy the pool.
8001 **/
8002static void
8003lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
8004{
8005 struct lpfc_cq_event *cq_event, *next_cq_event;
8006
8007 list_for_each_entry_safe(cq_event, next_cq_event,
8008 &phba->sli4_hba.sp_cqe_event_pool, list) {
8009 list_del(&cq_event->list);
8010 kfree(cq_event);
8011 }
8012}
8013
8014/**
8015 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
8016 * @phba: pointer to lpfc hba data structure.
8017 *
8018 * This routine is the lock free version of the API invoked to allocate a
8019 * completion-queue event from the free pool.
8020 *
8021 * Return: Pointer to the newly allocated completion-queue event if successful
8022 * NULL otherwise.
8023 **/
8024struct lpfc_cq_event *
8025__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
8026{
8027 struct lpfc_cq_event *cq_event = NULL;
8028
8029 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
8030 struct lpfc_cq_event, list);
8031 return cq_event;
8032}
8033
8034/**
8035 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
8036 * @phba: pointer to lpfc hba data structure.
8037 *
8038 * This routine is the lock version of the API invoked to allocate a
8039 * completion-queue event from the free pool.
8040 *
8041 * Return: Pointer to the newly allocated completion-queue event if successful
8042 * NULL otherwise.
8043 **/
8044struct lpfc_cq_event *
8045lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
8046{
8047 struct lpfc_cq_event *cq_event;
8048 unsigned long iflags;
8049
8050 spin_lock_irqsave(&phba->hbalock, iflags);
8051 cq_event = __lpfc_sli4_cq_event_alloc(phba);
8052 spin_unlock_irqrestore(&phba->hbalock, iflags);
8053 return cq_event;
8054}
8055
8056/**
8057 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
8058 * @phba: pointer to lpfc hba data structure.
8059 * @cq_event: pointer to the completion queue event to be freed.
8060 *
8061 * This routine is the lock free version of the API invoked to release a
8062 * completion-queue event back into the free pool.
8063 **/
8064void
8065__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
8066 struct lpfc_cq_event *cq_event)
8067{
8068 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
8069}
8070
8071/**
8072 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
8073 * @phba: pointer to lpfc hba data structure.
8074 * @cq_event: pointer to the completion queue event to be freed.
8075 *
8076 * This routine is the lock version of the API invoked to release a
8077 * completion-queue event back into the free pool.
8078 **/
8079void
8080lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
8081 struct lpfc_cq_event *cq_event)
8082{
8083 unsigned long iflags;
8084 spin_lock_irqsave(&phba->hbalock, iflags);
8085 __lpfc_sli4_cq_event_release(phba, cq_event);
8086 spin_unlock_irqrestore(&phba->hbalock, iflags);
8087}
8088
8089/**
8090 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
8091 * @phba: pointer to lpfc hba data structure.
8092 *
8093 * This routine is to free all the pending completion-queue events to the
8094 * back into the free pool for device reset.
8095 **/
8096static void
8097lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
8098{
8099 LIST_HEAD(cqelist);
8100 struct lpfc_cq_event *cqe;
8101 unsigned long iflags;
8102
8103 /* Retrieve all the pending WCQEs from pending WCQE lists */
8104 spin_lock_irqsave(&phba->hbalock, iflags);
8105 /* Pending FCP XRI abort events */
8106 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
8107 &cqelist);
8108 /* Pending ELS XRI abort events */
8109 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
8110 &cqelist);
8111 /* Pending asynnc events */
8112 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
8113 &cqelist);
8114 spin_unlock_irqrestore(&phba->hbalock, iflags);
8115
8116 while (!list_empty(&cqelist)) {
8117 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
8118 lpfc_sli4_cq_event_release(phba, cqe);
8119 }
8120}
8121
8122/**
8123 * lpfc_pci_function_reset - Reset pci function.
8124 * @phba: pointer to lpfc hba data structure.
8125 *
8126 * This routine is invoked to request a PCI function reset. It will destroys
8127 * all resources assigned to the PCI function which originates this request.
8128 *
8129 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008130 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008131 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04008132 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04008133 **/
8134int
8135lpfc_pci_function_reset(struct lpfc_hba *phba)
8136{
8137 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05008138 uint32_t rc = 0, if_type;
James Smartda0436e2009-05-22 14:51:39 -04008139 uint32_t shdr_status, shdr_add_status;
James Smart2f6fa2c2014-09-03 12:57:08 -04008140 uint32_t rdy_chk;
8141 uint32_t port_reset = 0;
James Smartda0436e2009-05-22 14:51:39 -04008142 union lpfc_sli4_cfg_shdr *shdr;
James Smart2fcee4b2010-12-15 17:57:46 -05008143 struct lpfc_register reg_data;
James Smart2b81f942012-03-01 22:37:18 -05008144 uint16_t devid;
James Smartda0436e2009-05-22 14:51:39 -04008145
James Smart2fcee4b2010-12-15 17:57:46 -05008146 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
8147 switch (if_type) {
8148 case LPFC_SLI_INTF_IF_TYPE_0:
8149 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
8150 GFP_KERNEL);
8151 if (!mboxq) {
8152 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8153 "0494 Unable to allocate memory for "
8154 "issuing SLI_FUNCTION_RESET mailbox "
8155 "command\n");
8156 return -ENOMEM;
8157 }
8158
8159 /* Setup PCI function reset mailbox-ioctl command */
8160 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
8161 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
8162 LPFC_SLI4_MBX_EMBED);
8163 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8164 shdr = (union lpfc_sli4_cfg_shdr *)
8165 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
8166 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
8167 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
8168 &shdr->response);
8169 if (rc != MBX_TIMEOUT)
8170 mempool_free(mboxq, phba->mbox_mem_pool);
8171 if (shdr_status || shdr_add_status || rc) {
8172 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8173 "0495 SLI_FUNCTION_RESET mailbox "
8174 "failed with status x%x add_status x%x,"
8175 " mbx status x%x\n",
8176 shdr_status, shdr_add_status, rc);
8177 rc = -ENXIO;
8178 }
8179 break;
8180 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart2f6fa2c2014-09-03 12:57:08 -04008181wait:
8182 /*
8183 * Poll the Port Status Register and wait for RDY for
8184 * up to 30 seconds. If the port doesn't respond, treat
8185 * it as an error.
8186 */
James Smart77d093f2015-04-07 15:07:08 -04008187 for (rdy_chk = 0; rdy_chk < 1500; rdy_chk++) {
James Smart2f6fa2c2014-09-03 12:57:08 -04008188 if (lpfc_readl(phba->sli4_hba.u.if_type2.
8189 STATUSregaddr, &reg_data.word0)) {
8190 rc = -ENODEV;
8191 goto out;
8192 }
8193 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
8194 break;
8195 msleep(20);
8196 }
8197
8198 if (!bf_get(lpfc_sliport_status_rdy, &reg_data)) {
8199 phba->work_status[0] = readl(
8200 phba->sli4_hba.u.if_type2.ERR1regaddr);
8201 phba->work_status[1] = readl(
8202 phba->sli4_hba.u.if_type2.ERR2regaddr);
8203 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8204 "2890 Port not ready, port status reg "
8205 "0x%x error 1=0x%x, error 2=0x%x\n",
8206 reg_data.word0,
8207 phba->work_status[0],
8208 phba->work_status[1]);
8209 rc = -ENODEV;
8210 goto out;
8211 }
8212
8213 if (!port_reset) {
8214 /*
8215 * Reset the port now
8216 */
James Smart2fcee4b2010-12-15 17:57:46 -05008217 reg_data.word0 = 0;
8218 bf_set(lpfc_sliport_ctrl_end, &reg_data,
8219 LPFC_SLIPORT_LITTLE_ENDIAN);
8220 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
8221 LPFC_SLIPORT_INIT_PORT);
8222 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
8223 CTRLregaddr);
James Smart8fcb8ac2012-03-01 22:35:58 -05008224 /* flush */
James Smart2b81f942012-03-01 22:37:18 -05008225 pci_read_config_word(phba->pcidev,
8226 PCI_DEVICE_ID, &devid);
James Smart2fcee4b2010-12-15 17:57:46 -05008227
James Smart2f6fa2c2014-09-03 12:57:08 -04008228 port_reset = 1;
8229 msleep(20);
8230 goto wait;
8231 } else if (bf_get(lpfc_sliport_status_rn, &reg_data)) {
8232 rc = -ENODEV;
8233 goto out;
James Smart2fcee4b2010-12-15 17:57:46 -05008234 }
8235 break;
James Smart2f6fa2c2014-09-03 12:57:08 -04008236
James Smart2fcee4b2010-12-15 17:57:46 -05008237 case LPFC_SLI_INTF_IF_TYPE_1:
8238 default:
8239 break;
James Smartda0436e2009-05-22 14:51:39 -04008240 }
8241
James Smart73d91e52011-10-10 21:32:10 -04008242out:
James Smart2fcee4b2010-12-15 17:57:46 -05008243 /* Catch the not-ready port failure after a port reset. */
James Smart2f6fa2c2014-09-03 12:57:08 -04008244 if (rc) {
James Smart229adb02013-04-17 20:16:51 -04008245 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8246 "3317 HBA not functional: IP Reset Failed "
James Smart2f6fa2c2014-09-03 12:57:08 -04008247 "try: echo fw_reset > board_mode\n");
James Smart2fcee4b2010-12-15 17:57:46 -05008248 rc = -ENODEV;
James Smart229adb02013-04-17 20:16:51 -04008249 }
James Smart2fcee4b2010-12-15 17:57:46 -05008250
James Smartda0436e2009-05-22 14:51:39 -04008251 return rc;
8252}
8253
8254/**
James Smartda0436e2009-05-22 14:51:39 -04008255 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
8256 * @phba: pointer to lpfc hba data structure.
8257 *
8258 * This routine is invoked to set up the PCI device memory space for device
8259 * with SLI-4 interface spec.
8260 *
8261 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008262 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008263 * other values - error
8264 **/
8265static int
8266lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
8267{
8268 struct pci_dev *pdev;
8269 unsigned long bar0map_len, bar1map_len, bar2map_len;
8270 int error = -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -05008271 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04008272
8273 /* Obtain PCI device reference */
8274 if (!phba->pcidev)
8275 return error;
8276 else
8277 pdev = phba->pcidev;
8278
8279 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05008280 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
8281 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
8282 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
8283 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smartda0436e2009-05-22 14:51:39 -04008284 return error;
Michael Reed8e685972009-09-18 12:02:05 -05008285 }
8286 }
James Smartda0436e2009-05-22 14:51:39 -04008287
James Smart2fcee4b2010-12-15 17:57:46 -05008288 /*
8289 * The BARs and register set definitions and offset locations are
8290 * dependent on the if_type.
8291 */
8292 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
8293 &phba->sli4_hba.sli_intf.word0)) {
8294 return error;
8295 }
8296
8297 /* There is no SLI3 failback for SLI4 devices. */
8298 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
8299 LPFC_SLI_INTF_VALID) {
8300 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8301 "2894 SLI_INTF reg contents invalid "
8302 "sli_intf reg 0x%x\n",
8303 phba->sli4_hba.sli_intf.word0);
8304 return error;
8305 }
8306
8307 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
8308 /*
8309 * Get the bus address of SLI4 device Bar regions and the
8310 * number of bytes required by each mapping. The mapping of the
8311 * particular PCI BARs regions is dependent on the type of
8312 * SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -04008313 */
James Smartf5ca6f22013-09-06 12:21:09 -04008314 if (pci_resource_start(pdev, PCI_64BIT_BAR0)) {
8315 phba->pci_bar0_map = pci_resource_start(pdev, PCI_64BIT_BAR0);
8316 bar0map_len = pci_resource_len(pdev, PCI_64BIT_BAR0);
James Smart2fcee4b2010-12-15 17:57:46 -05008317
8318 /*
8319 * Map SLI4 PCI Config Space Register base to a kernel virtual
8320 * addr
8321 */
8322 phba->sli4_hba.conf_regs_memmap_p =
8323 ioremap(phba->pci_bar0_map, bar0map_len);
8324 if (!phba->sli4_hba.conf_regs_memmap_p) {
8325 dev_printk(KERN_ERR, &pdev->dev,
8326 "ioremap failed for SLI4 PCI config "
8327 "registers.\n");
8328 goto out;
8329 }
James Smartf5ca6f22013-09-06 12:21:09 -04008330 phba->pci_bar0_memmap_p = phba->sli4_hba.conf_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05008331 /* Set up BAR0 PCI config space register memory map */
8332 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smart1dfb5a42010-02-12 14:40:50 -05008333 } else {
8334 phba->pci_bar0_map = pci_resource_start(pdev, 1);
8335 bar0map_len = pci_resource_len(pdev, 1);
James Smart2fcee4b2010-12-15 17:57:46 -05008336 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8337 dev_printk(KERN_ERR, &pdev->dev,
8338 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
8339 goto out;
8340 }
8341 phba->sli4_hba.conf_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04008342 ioremap(phba->pci_bar0_map, bar0map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05008343 if (!phba->sli4_hba.conf_regs_memmap_p) {
8344 dev_printk(KERN_ERR, &pdev->dev,
8345 "ioremap failed for SLI4 PCI config "
8346 "registers.\n");
8347 goto out;
8348 }
8349 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smartda0436e2009-05-22 14:51:39 -04008350 }
8351
James Smartc31098c2011-04-16 11:03:33 -04008352 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
James Smartf5ca6f22013-09-06 12:21:09 -04008353 (pci_resource_start(pdev, PCI_64BIT_BAR2))) {
James Smart2fcee4b2010-12-15 17:57:46 -05008354 /*
8355 * Map SLI4 if type 0 HBA Control Register base to a kernel
8356 * virtual address and setup the registers.
8357 */
James Smartf5ca6f22013-09-06 12:21:09 -04008358 phba->pci_bar1_map = pci_resource_start(pdev, PCI_64BIT_BAR2);
8359 bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
James Smart2fcee4b2010-12-15 17:57:46 -05008360 phba->sli4_hba.ctrl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04008361 ioremap(phba->pci_bar1_map, bar1map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05008362 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
8363 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04008364 "ioremap failed for SLI4 HBA control registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05008365 goto out_iounmap_conf;
8366 }
James Smartf5ca6f22013-09-06 12:21:09 -04008367 phba->pci_bar2_memmap_p = phba->sli4_hba.ctrl_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05008368 lpfc_sli4_bar1_register_memmap(phba);
James Smartda0436e2009-05-22 14:51:39 -04008369 }
8370
James Smartc31098c2011-04-16 11:03:33 -04008371 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
James Smartf5ca6f22013-09-06 12:21:09 -04008372 (pci_resource_start(pdev, PCI_64BIT_BAR4))) {
James Smart2fcee4b2010-12-15 17:57:46 -05008373 /*
8374 * Map SLI4 if type 0 HBA Doorbell Register base to a kernel
8375 * virtual address and setup the registers.
8376 */
James Smartf5ca6f22013-09-06 12:21:09 -04008377 phba->pci_bar2_map = pci_resource_start(pdev, PCI_64BIT_BAR4);
8378 bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
James Smart2fcee4b2010-12-15 17:57:46 -05008379 phba->sli4_hba.drbl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04008380 ioremap(phba->pci_bar2_map, bar2map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05008381 if (!phba->sli4_hba.drbl_regs_memmap_p) {
8382 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04008383 "ioremap failed for SLI4 HBA doorbell registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05008384 goto out_iounmap_ctrl;
8385 }
James Smartf5ca6f22013-09-06 12:21:09 -04008386 phba->pci_bar4_memmap_p = phba->sli4_hba.drbl_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05008387 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
8388 if (error)
8389 goto out_iounmap_all;
James Smartda0436e2009-05-22 14:51:39 -04008390 }
8391
James Smartda0436e2009-05-22 14:51:39 -04008392 return 0;
8393
8394out_iounmap_all:
8395 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
8396out_iounmap_ctrl:
8397 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
8398out_iounmap_conf:
8399 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8400out:
8401 return error;
8402}
8403
8404/**
8405 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
8406 * @phba: pointer to lpfc hba data structure.
8407 *
8408 * This routine is invoked to unset the PCI device memory space for device
8409 * with SLI-4 interface spec.
8410 **/
8411static void
8412lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
8413{
James Smart2e90f4b2011-12-13 13:22:37 -05008414 uint32_t if_type;
8415 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smartda0436e2009-05-22 14:51:39 -04008416
James Smart2e90f4b2011-12-13 13:22:37 -05008417 switch (if_type) {
8418 case LPFC_SLI_INTF_IF_TYPE_0:
8419 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
8420 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
8421 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8422 break;
8423 case LPFC_SLI_INTF_IF_TYPE_2:
8424 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8425 break;
8426 case LPFC_SLI_INTF_IF_TYPE_1:
8427 default:
8428 dev_printk(KERN_ERR, &phba->pcidev->dev,
8429 "FATAL - unsupported SLI4 interface type - %d\n",
8430 if_type);
8431 break;
8432 }
James Smartda0436e2009-05-22 14:51:39 -04008433}
8434
8435/**
James Smart3772a992009-05-22 14:50:54 -04008436 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
8437 * @phba: pointer to lpfc hba data structure.
8438 *
8439 * This routine is invoked to enable the MSI-X interrupt vectors to device
Alexander Gordeev4f871e12014-09-03 12:56:29 -04008440 * with SLI-3 interface specs. The kernel function pci_enable_msix_exact()
8441 * is called to enable the MSI-X vectors. Note that pci_enable_msix_exact(),
8442 * once invoked, enables either all or nothing, depending on the current
James Smart3772a992009-05-22 14:50:54 -04008443 * availability of PCI vector resources. The device driver is responsible
8444 * for calling the individual request_irq() to register each MSI-X vector
8445 * with a interrupt handler, which is done in this function. Note that
8446 * later when device is unloading, the driver should always call free_irq()
8447 * on all MSI-X vectors it has done request_irq() on before calling
8448 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
8449 * will be left with MSI-X enabled and leaks its vectors.
8450 *
8451 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008452 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008453 * other values - error
8454 **/
8455static int
8456lpfc_sli_enable_msix(struct lpfc_hba *phba)
8457{
8458 int rc, i;
8459 LPFC_MBOXQ_t *pmb;
8460
8461 /* Set up MSI-X multi-message vectors */
8462 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8463 phba->msix_entries[i].entry = i;
8464
8465 /* Configure MSI-X capability structure */
Alexander Gordeev4f871e12014-09-03 12:56:29 -04008466 rc = pci_enable_msix_exact(phba->pcidev, phba->msix_entries,
8467 LPFC_MSIX_VECTORS);
James Smart3772a992009-05-22 14:50:54 -04008468 if (rc) {
8469 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8470 "0420 PCI enable MSI-X failed (%d)\n", rc);
Alexander Gordeev029165a2014-07-16 20:05:15 +02008471 goto vec_fail_out;
James Smart3772a992009-05-22 14:50:54 -04008472 }
8473 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8474 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8475 "0477 MSI-X entry[%d]: vector=x%x "
8476 "message=%d\n", i,
8477 phba->msix_entries[i].vector,
8478 phba->msix_entries[i].entry);
8479 /*
8480 * Assign MSI-X vectors to interrupt handlers
8481 */
8482
8483 /* vector-0 is associated to slow-path handler */
8484 rc = request_irq(phba->msix_entries[0].vector,
James Smarted243d32015-05-21 13:55:25 -04008485 &lpfc_sli_sp_intr_handler, 0,
James Smart3772a992009-05-22 14:50:54 -04008486 LPFC_SP_DRIVER_HANDLER_NAME, phba);
8487 if (rc) {
8488 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8489 "0421 MSI-X slow-path request_irq failed "
8490 "(%d)\n", rc);
8491 goto msi_fail_out;
8492 }
8493
8494 /* vector-1 is associated to fast-path handler */
8495 rc = request_irq(phba->msix_entries[1].vector,
James Smarted243d32015-05-21 13:55:25 -04008496 &lpfc_sli_fp_intr_handler, 0,
James Smart3772a992009-05-22 14:50:54 -04008497 LPFC_FP_DRIVER_HANDLER_NAME, phba);
8498
8499 if (rc) {
8500 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8501 "0429 MSI-X fast-path request_irq failed "
8502 "(%d)\n", rc);
8503 goto irq_fail_out;
8504 }
8505
8506 /*
8507 * Configure HBA MSI-X attention conditions to messages
8508 */
8509 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8510
8511 if (!pmb) {
8512 rc = -ENOMEM;
8513 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8514 "0474 Unable to allocate memory for issuing "
8515 "MBOX_CONFIG_MSI command\n");
8516 goto mem_fail_out;
8517 }
8518 rc = lpfc_config_msi(phba, pmb);
8519 if (rc)
8520 goto mbx_fail_out;
8521 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
8522 if (rc != MBX_SUCCESS) {
8523 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
8524 "0351 Config MSI mailbox command failed, "
8525 "mbxCmd x%x, mbxStatus x%x\n",
8526 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
8527 goto mbx_fail_out;
8528 }
8529
8530 /* Free memory allocated for mailbox command */
8531 mempool_free(pmb, phba->mbox_mem_pool);
8532 return rc;
8533
8534mbx_fail_out:
8535 /* Free memory allocated for mailbox command */
8536 mempool_free(pmb, phba->mbox_mem_pool);
8537
8538mem_fail_out:
8539 /* free the irq already requested */
8540 free_irq(phba->msix_entries[1].vector, phba);
8541
8542irq_fail_out:
8543 /* free the irq already requested */
8544 free_irq(phba->msix_entries[0].vector, phba);
8545
8546msi_fail_out:
8547 /* Unconfigure MSI-X capability structure */
8548 pci_disable_msix(phba->pcidev);
Alexander Gordeev029165a2014-07-16 20:05:15 +02008549
8550vec_fail_out:
James Smart3772a992009-05-22 14:50:54 -04008551 return rc;
8552}
8553
8554/**
8555 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
8556 * @phba: pointer to lpfc hba data structure.
8557 *
8558 * This routine is invoked to release the MSI-X vectors and then disable the
8559 * MSI-X interrupt mode to device with SLI-3 interface spec.
8560 **/
8561static void
8562lpfc_sli_disable_msix(struct lpfc_hba *phba)
8563{
8564 int i;
8565
8566 /* Free up MSI-X multi-message vectors */
8567 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8568 free_irq(phba->msix_entries[i].vector, phba);
8569 /* Disable MSI-X */
8570 pci_disable_msix(phba->pcidev);
8571
8572 return;
8573}
8574
8575/**
8576 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
8577 * @phba: pointer to lpfc hba data structure.
8578 *
8579 * This routine is invoked to enable the MSI interrupt mode to device with
8580 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
8581 * enable the MSI vector. The device driver is responsible for calling the
8582 * request_irq() to register MSI vector with a interrupt the handler, which
8583 * is done in this function.
8584 *
8585 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008586 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008587 * other values - error
8588 */
8589static int
8590lpfc_sli_enable_msi(struct lpfc_hba *phba)
8591{
8592 int rc;
8593
8594 rc = pci_enable_msi(phba->pcidev);
8595 if (!rc)
8596 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8597 "0462 PCI enable MSI mode success.\n");
8598 else {
8599 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8600 "0471 PCI enable MSI mode failed (%d)\n", rc);
8601 return rc;
8602 }
8603
8604 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
James Smarted243d32015-05-21 13:55:25 -04008605 0, LPFC_DRIVER_NAME, phba);
James Smart3772a992009-05-22 14:50:54 -04008606 if (rc) {
8607 pci_disable_msi(phba->pcidev);
8608 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8609 "0478 MSI request_irq failed (%d)\n", rc);
8610 }
8611 return rc;
8612}
8613
8614/**
8615 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
8616 * @phba: pointer to lpfc hba data structure.
8617 *
8618 * This routine is invoked to disable the MSI interrupt mode to device with
8619 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
8620 * done request_irq() on before calling pci_disable_msi(). Failure to do so
8621 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
8622 * its vector.
8623 */
8624static void
8625lpfc_sli_disable_msi(struct lpfc_hba *phba)
8626{
8627 free_irq(phba->pcidev->irq, phba);
8628 pci_disable_msi(phba->pcidev);
8629 return;
8630}
8631
8632/**
8633 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
8634 * @phba: pointer to lpfc hba data structure.
8635 *
8636 * This routine is invoked to enable device interrupt and associate driver's
8637 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
8638 * spec. Depends on the interrupt mode configured to the driver, the driver
8639 * will try to fallback from the configured interrupt mode to an interrupt
8640 * mode which is supported by the platform, kernel, and device in the order
8641 * of:
8642 * MSI-X -> MSI -> IRQ.
8643 *
8644 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008645 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008646 * other values - error
8647 **/
8648static uint32_t
8649lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
8650{
8651 uint32_t intr_mode = LPFC_INTR_ERROR;
8652 int retval;
8653
8654 if (cfg_mode == 2) {
8655 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
8656 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
8657 if (!retval) {
8658 /* Now, try to enable MSI-X interrupt mode */
8659 retval = lpfc_sli_enable_msix(phba);
8660 if (!retval) {
8661 /* Indicate initialization to MSI-X mode */
8662 phba->intr_type = MSIX;
8663 intr_mode = 2;
8664 }
8665 }
8666 }
8667
8668 /* Fallback to MSI if MSI-X initialization failed */
8669 if (cfg_mode >= 1 && phba->intr_type == NONE) {
8670 retval = lpfc_sli_enable_msi(phba);
8671 if (!retval) {
8672 /* Indicate initialization to MSI mode */
8673 phba->intr_type = MSI;
8674 intr_mode = 1;
8675 }
8676 }
8677
8678 /* Fallback to INTx if both MSI-X/MSI initalization failed */
8679 if (phba->intr_type == NONE) {
8680 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
8681 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8682 if (!retval) {
8683 /* Indicate initialization to INTx mode */
8684 phba->intr_type = INTx;
8685 intr_mode = 0;
8686 }
8687 }
8688 return intr_mode;
8689}
8690
8691/**
8692 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
8693 * @phba: pointer to lpfc hba data structure.
8694 *
8695 * This routine is invoked to disable device interrupt and disassociate the
8696 * driver's interrupt handler(s) from interrupt vector(s) to device with
8697 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
8698 * release the interrupt vector(s) for the message signaled interrupt.
8699 **/
8700static void
8701lpfc_sli_disable_intr(struct lpfc_hba *phba)
8702{
8703 /* Disable the currently initialized interrupt mode */
8704 if (phba->intr_type == MSIX)
8705 lpfc_sli_disable_msix(phba);
8706 else if (phba->intr_type == MSI)
8707 lpfc_sli_disable_msi(phba);
8708 else if (phba->intr_type == INTx)
8709 free_irq(phba->pcidev->irq, phba);
8710
8711 /* Reset interrupt management states */
8712 phba->intr_type = NONE;
8713 phba->sli.slistat.sli_intr = 0;
8714
8715 return;
8716}
8717
8718/**
James Smart7bb03bb2013-04-17 20:19:16 -04008719 * lpfc_find_next_cpu - Find next available CPU that matches the phys_id
8720 * @phba: pointer to lpfc hba data structure.
8721 *
8722 * Find next available CPU to use for IRQ to CPU affinity.
8723 */
8724static int
8725lpfc_find_next_cpu(struct lpfc_hba *phba, uint32_t phys_id)
8726{
8727 struct lpfc_vector_map_info *cpup;
8728 int cpu;
8729
8730 cpup = phba->sli4_hba.cpu_map;
8731 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8732 /* CPU must be online */
8733 if (cpu_online(cpu)) {
8734 if ((cpup->irq == LPFC_VECTOR_MAP_EMPTY) &&
8735 (lpfc_used_cpu[cpu] == LPFC_VECTOR_MAP_EMPTY) &&
8736 (cpup->phys_id == phys_id)) {
8737 return cpu;
8738 }
8739 }
8740 cpup++;
8741 }
8742
8743 /*
8744 * If we get here, we have used ALL CPUs for the specific
8745 * phys_id. Now we need to clear out lpfc_used_cpu and start
8746 * reusing CPUs.
8747 */
8748
8749 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8750 if (lpfc_used_cpu[cpu] == phys_id)
8751 lpfc_used_cpu[cpu] = LPFC_VECTOR_MAP_EMPTY;
8752 }
8753
8754 cpup = phba->sli4_hba.cpu_map;
8755 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8756 /* CPU must be online */
8757 if (cpu_online(cpu)) {
8758 if ((cpup->irq == LPFC_VECTOR_MAP_EMPTY) &&
8759 (cpup->phys_id == phys_id)) {
8760 return cpu;
8761 }
8762 }
8763 cpup++;
8764 }
8765 return LPFC_VECTOR_MAP_EMPTY;
8766}
8767
8768/**
8769 * lpfc_sli4_set_affinity - Set affinity for HBA IRQ vectors
8770 * @phba: pointer to lpfc hba data structure.
8771 * @vectors: number of HBA vectors
8772 *
8773 * Affinitize MSIX IRQ vectors to CPUs. Try to equally spread vector
8774 * affinization across multple physical CPUs (numa nodes).
8775 * In addition, this routine will assign an IO channel for each CPU
8776 * to use when issuing I/Os.
8777 */
8778static int
8779lpfc_sli4_set_affinity(struct lpfc_hba *phba, int vectors)
8780{
8781 int i, idx, saved_chann, used_chann, cpu, phys_id;
James Smartec2087a2013-09-06 12:20:36 -04008782 int max_phys_id, min_phys_id;
8783 int num_io_channel, first_cpu, chan;
James Smart7bb03bb2013-04-17 20:19:16 -04008784 struct lpfc_vector_map_info *cpup;
8785#ifdef CONFIG_X86
8786 struct cpuinfo_x86 *cpuinfo;
8787#endif
James Smart7bb03bb2013-04-17 20:19:16 -04008788 uint8_t chann[LPFC_FCP_IO_CHAN_MAX+1];
8789
8790 /* If there is no mapping, just return */
8791 if (!phba->cfg_fcp_cpu_map)
8792 return 1;
8793
8794 /* Init cpu_map array */
8795 memset(phba->sli4_hba.cpu_map, 0xff,
8796 (sizeof(struct lpfc_vector_map_info) *
8797 phba->sli4_hba.num_present_cpu));
8798
8799 max_phys_id = 0;
James Smartec2087a2013-09-06 12:20:36 -04008800 min_phys_id = 0xff;
James Smart7bb03bb2013-04-17 20:19:16 -04008801 phys_id = 0;
8802 num_io_channel = 0;
8803 first_cpu = LPFC_VECTOR_MAP_EMPTY;
8804
8805 /* Update CPU map with physical id and core id of each CPU */
8806 cpup = phba->sli4_hba.cpu_map;
8807 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8808#ifdef CONFIG_X86
8809 cpuinfo = &cpu_data(cpu);
8810 cpup->phys_id = cpuinfo->phys_proc_id;
8811 cpup->core_id = cpuinfo->cpu_core_id;
8812#else
8813 /* No distinction between CPUs for other platforms */
8814 cpup->phys_id = 0;
8815 cpup->core_id = 0;
8816#endif
8817
8818 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8819 "3328 CPU physid %d coreid %d\n",
8820 cpup->phys_id, cpup->core_id);
8821
8822 if (cpup->phys_id > max_phys_id)
8823 max_phys_id = cpup->phys_id;
James Smartec2087a2013-09-06 12:20:36 -04008824 if (cpup->phys_id < min_phys_id)
8825 min_phys_id = cpup->phys_id;
James Smart7bb03bb2013-04-17 20:19:16 -04008826 cpup++;
8827 }
8828
James Smartec2087a2013-09-06 12:20:36 -04008829 phys_id = min_phys_id;
James Smart7bb03bb2013-04-17 20:19:16 -04008830 /* Now associate the HBA vectors with specific CPUs */
8831 for (idx = 0; idx < vectors; idx++) {
8832 cpup = phba->sli4_hba.cpu_map;
8833 cpu = lpfc_find_next_cpu(phba, phys_id);
8834 if (cpu == LPFC_VECTOR_MAP_EMPTY) {
8835
8836 /* Try for all phys_id's */
8837 for (i = 1; i < max_phys_id; i++) {
8838 phys_id++;
8839 if (phys_id > max_phys_id)
James Smartec2087a2013-09-06 12:20:36 -04008840 phys_id = min_phys_id;
James Smart7bb03bb2013-04-17 20:19:16 -04008841 cpu = lpfc_find_next_cpu(phba, phys_id);
8842 if (cpu == LPFC_VECTOR_MAP_EMPTY)
8843 continue;
8844 goto found;
8845 }
8846
James Smartec2087a2013-09-06 12:20:36 -04008847 /* Use round robin for scheduling */
8848 phba->cfg_fcp_io_sched = LPFC_FCP_SCHED_ROUND_ROBIN;
8849 chan = 0;
8850 cpup = phba->sli4_hba.cpu_map;
8851 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
8852 cpup->channel_id = chan;
8853 cpup++;
8854 chan++;
8855 if (chan >= phba->cfg_fcp_io_channel)
8856 chan = 0;
8857 }
8858
James Smart7bb03bb2013-04-17 20:19:16 -04008859 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8860 "3329 Cannot set affinity:"
8861 "Error mapping vector %d (%d)\n",
8862 idx, vectors);
8863 return 0;
8864 }
8865found:
8866 cpup += cpu;
8867 if (phba->cfg_fcp_cpu_map == LPFC_DRIVER_CPU_MAP)
8868 lpfc_used_cpu[cpu] = phys_id;
8869
8870 /* Associate vector with selected CPU */
8871 cpup->irq = phba->sli4_hba.msix_entries[idx].vector;
8872
8873 /* Associate IO channel with selected CPU */
8874 cpup->channel_id = idx;
8875 num_io_channel++;
8876
8877 if (first_cpu == LPFC_VECTOR_MAP_EMPTY)
8878 first_cpu = cpu;
8879
8880 /* Now affinitize to the selected CPU */
James Smart7bb03bb2013-04-17 20:19:16 -04008881 i = irq_set_affinity_hint(phba->sli4_hba.msix_entries[idx].
Ian Mitchellc0365c02015-08-31 16:48:12 -04008882 vector, get_cpu_mask(cpu));
James Smart7bb03bb2013-04-17 20:19:16 -04008883
8884 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8885 "3330 Set Affinity: CPU %d channel %d "
8886 "irq %d (%x)\n",
8887 cpu, cpup->channel_id,
8888 phba->sli4_hba.msix_entries[idx].vector, i);
8889
8890 /* Spread vector mapping across multple physical CPU nodes */
8891 phys_id++;
8892 if (phys_id > max_phys_id)
James Smartec2087a2013-09-06 12:20:36 -04008893 phys_id = min_phys_id;
James Smart7bb03bb2013-04-17 20:19:16 -04008894 }
8895
8896 /*
8897 * Finally fill in the IO channel for any remaining CPUs.
8898 * At this point, all IO channels have been assigned to a specific
8899 * MSIx vector, mapped to a specific CPU.
8900 * Base the remaining IO channel assigned, to IO channels already
8901 * assigned to other CPUs on the same phys_id.
8902 */
James Smartec2087a2013-09-06 12:20:36 -04008903 for (i = min_phys_id; i <= max_phys_id; i++) {
James Smart7bb03bb2013-04-17 20:19:16 -04008904 /*
8905 * If there are no io channels already mapped to
8906 * this phys_id, just round robin thru the io_channels.
8907 * Setup chann[] for round robin.
8908 */
8909 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++)
8910 chann[idx] = idx;
8911
8912 saved_chann = 0;
8913 used_chann = 0;
8914
8915 /*
8916 * First build a list of IO channels already assigned
8917 * to this phys_id before reassigning the same IO
8918 * channels to the remaining CPUs.
8919 */
8920 cpup = phba->sli4_hba.cpu_map;
8921 cpu = first_cpu;
8922 cpup += cpu;
8923 for (idx = 0; idx < phba->sli4_hba.num_present_cpu;
8924 idx++) {
8925 if (cpup->phys_id == i) {
8926 /*
8927 * Save any IO channels that are
8928 * already mapped to this phys_id.
8929 */
8930 if (cpup->irq != LPFC_VECTOR_MAP_EMPTY) {
James Smart6690e0d2015-12-16 18:11:56 -05008931 if (saved_chann <=
8932 LPFC_FCP_IO_CHAN_MAX) {
8933 chann[saved_chann] =
8934 cpup->channel_id;
8935 saved_chann++;
8936 }
James Smart7bb03bb2013-04-17 20:19:16 -04008937 goto out;
8938 }
8939
8940 /* See if we are using round-robin */
8941 if (saved_chann == 0)
8942 saved_chann =
8943 phba->cfg_fcp_io_channel;
8944
8945 /* Associate next IO channel with CPU */
8946 cpup->channel_id = chann[used_chann];
8947 num_io_channel++;
8948 used_chann++;
8949 if (used_chann == saved_chann)
8950 used_chann = 0;
8951
8952 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8953 "3331 Set IO_CHANN "
8954 "CPU %d channel %d\n",
8955 idx, cpup->channel_id);
8956 }
8957out:
8958 cpu++;
8959 if (cpu >= phba->sli4_hba.num_present_cpu) {
8960 cpup = phba->sli4_hba.cpu_map;
8961 cpu = 0;
8962 } else {
8963 cpup++;
8964 }
8965 }
8966 }
8967
8968 if (phba->sli4_hba.num_online_cpu != phba->sli4_hba.num_present_cpu) {
8969 cpup = phba->sli4_hba.cpu_map;
8970 for (idx = 0; idx < phba->sli4_hba.num_present_cpu; idx++) {
8971 if (cpup->channel_id == LPFC_VECTOR_MAP_EMPTY) {
8972 cpup->channel_id = 0;
8973 num_io_channel++;
8974
8975 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8976 "3332 Assign IO_CHANN "
8977 "CPU %d channel %d\n",
8978 idx, cpup->channel_id);
8979 }
8980 cpup++;
8981 }
8982 }
8983
8984 /* Sanity check */
8985 if (num_io_channel != phba->sli4_hba.num_present_cpu)
8986 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8987 "3333 Set affinity mismatch:"
James Smartec2087a2013-09-06 12:20:36 -04008988 "%d chann != %d cpus: %d vectors\n",
James Smart7bb03bb2013-04-17 20:19:16 -04008989 num_io_channel, phba->sli4_hba.num_present_cpu,
8990 vectors);
8991
James Smartec2087a2013-09-06 12:20:36 -04008992 /* Enable using cpu affinity for scheduling */
James Smart7bb03bb2013-04-17 20:19:16 -04008993 phba->cfg_fcp_io_sched = LPFC_FCP_SCHED_BY_CPU;
8994 return 1;
8995}
8996
8997
8998/**
James Smartda0436e2009-05-22 14:51:39 -04008999 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
9000 * @phba: pointer to lpfc hba data structure.
9001 *
9002 * This routine is invoked to enable the MSI-X interrupt vectors to device
Alexander Gordeev4f871e12014-09-03 12:56:29 -04009003 * with SLI-4 interface spec. The kernel function pci_enable_msix_range()
9004 * is called to enable the MSI-X vectors. The device driver is responsible
9005 * for calling the individual request_irq() to register each MSI-X vector
9006 * with a interrupt handler, which is done in this function. Note that
9007 * later when device is unloading, the driver should always call free_irq()
9008 * on all MSI-X vectors it has done request_irq() on before calling
9009 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
9010 * will be left with MSI-X enabled and leaks its vectors.
James Smartda0436e2009-05-22 14:51:39 -04009011 *
9012 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009013 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04009014 * other values - error
9015 **/
9016static int
9017lpfc_sli4_enable_msix(struct lpfc_hba *phba)
9018{
James Smart75baf692010-06-08 18:31:21 -04009019 int vectors, rc, index;
James Smartda0436e2009-05-22 14:51:39 -04009020
9021 /* Set up MSI-X multi-message vectors */
James Smart82c3e9b2012-09-29 11:29:50 -04009022 for (index = 0; index < phba->cfg_fcp_io_channel; index++)
James Smartda0436e2009-05-22 14:51:39 -04009023 phba->sli4_hba.msix_entries[index].entry = index;
9024
9025 /* Configure MSI-X capability structure */
James Smart82c3e9b2012-09-29 11:29:50 -04009026 vectors = phba->cfg_fcp_io_channel;
James Smart1ba981f2014-02-20 09:56:45 -05009027 if (phba->cfg_fof) {
9028 phba->sli4_hba.msix_entries[index].entry = index;
9029 vectors++;
9030 }
Alexander Gordeev4f871e12014-09-03 12:56:29 -04009031 rc = pci_enable_msix_range(phba->pcidev, phba->sli4_hba.msix_entries,
9032 2, vectors);
9033 if (rc < 0) {
James Smartda0436e2009-05-22 14:51:39 -04009034 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9035 "0484 PCI enable MSI-X failed (%d)\n", rc);
Alexander Gordeev029165a2014-07-16 20:05:15 +02009036 goto vec_fail_out;
James Smartda0436e2009-05-22 14:51:39 -04009037 }
Alexander Gordeev4f871e12014-09-03 12:56:29 -04009038 vectors = rc;
James Smart75baf692010-06-08 18:31:21 -04009039
James Smartda0436e2009-05-22 14:51:39 -04009040 /* Log MSI-X vector assignment */
James Smart75baf692010-06-08 18:31:21 -04009041 for (index = 0; index < vectors; index++)
James Smartda0436e2009-05-22 14:51:39 -04009042 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9043 "0489 MSI-X entry[%d]: vector=x%x "
9044 "message=%d\n", index,
9045 phba->sli4_hba.msix_entries[index].vector,
9046 phba->sli4_hba.msix_entries[index].entry);
James Smart67d12732012-08-03 12:36:13 -04009047
James Smart7bb03bb2013-04-17 20:19:16 -04009048 /* Assign MSI-X vectors to interrupt handlers */
James Smart67d12732012-08-03 12:36:13 -04009049 for (index = 0; index < vectors; index++) {
James Smart4305f182012-08-03 12:36:33 -04009050 memset(&phba->sli4_hba.handler_name[index], 0, 16);
James Smarta2fc4aef2014-09-03 12:57:55 -04009051 snprintf((char *)&phba->sli4_hba.handler_name[index],
9052 LPFC_SLI4_HANDLER_NAME_SZ,
James Smart4305f182012-08-03 12:36:33 -04009053 LPFC_DRIVER_HANDLER_NAME"%d", index);
James Smartda0436e2009-05-22 14:51:39 -04009054
James Smart67d12732012-08-03 12:36:13 -04009055 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
9056 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
James Smartba20c852012-08-03 12:36:52 -04009057 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].fcp_eq_in_use, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009058 if (phba->cfg_fof && (index == (vectors - 1)))
9059 rc = request_irq(
9060 phba->sli4_hba.msix_entries[index].vector,
James Smarted243d32015-05-21 13:55:25 -04009061 &lpfc_sli4_fof_intr_handler, 0,
James Smart1ba981f2014-02-20 09:56:45 -05009062 (char *)&phba->sli4_hba.handler_name[index],
9063 &phba->sli4_hba.fcp_eq_hdl[index]);
9064 else
9065 rc = request_irq(
9066 phba->sli4_hba.msix_entries[index].vector,
James Smarted243d32015-05-21 13:55:25 -04009067 &lpfc_sli4_hba_intr_handler, 0,
James Smart4305f182012-08-03 12:36:33 -04009068 (char *)&phba->sli4_hba.handler_name[index],
James Smart67d12732012-08-03 12:36:13 -04009069 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -04009070 if (rc) {
9071 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9072 "0486 MSI-X fast-path (%d) "
9073 "request_irq failed (%d)\n", index, rc);
9074 goto cfg_fail_out;
9075 }
9076 }
9077
James Smart1ba981f2014-02-20 09:56:45 -05009078 if (phba->cfg_fof)
9079 vectors--;
9080
James Smart82c3e9b2012-09-29 11:29:50 -04009081 if (vectors != phba->cfg_fcp_io_channel) {
9082 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9083 "3238 Reducing IO channels to match number of "
9084 "MSI-X vectors, requested %d got %d\n",
9085 phba->cfg_fcp_io_channel, vectors);
9086 phba->cfg_fcp_io_channel = vectors;
9087 }
James Smart7bb03bb2013-04-17 20:19:16 -04009088
James Smart8b0dff12015-05-22 10:42:38 -04009089 if (!shost_use_blk_mq(lpfc_shost_from_vport(phba->pport)))
9090 lpfc_sli4_set_affinity(phba, vectors);
James Smartda0436e2009-05-22 14:51:39 -04009091 return rc;
9092
9093cfg_fail_out:
9094 /* free the irq already requested */
James Smartacbd8612013-09-06 12:21:19 -04009095 for (--index; index >= 0; index--) {
9096 irq_set_affinity_hint(phba->sli4_hba.msix_entries[index].
9097 vector, NULL);
James Smart67d12732012-08-03 12:36:13 -04009098 free_irq(phba->sli4_hba.msix_entries[index].vector,
9099 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smartacbd8612013-09-06 12:21:19 -04009100 }
James Smartda0436e2009-05-22 14:51:39 -04009101
James Smartda0436e2009-05-22 14:51:39 -04009102 /* Unconfigure MSI-X capability structure */
9103 pci_disable_msix(phba->pcidev);
Alexander Gordeev029165a2014-07-16 20:05:15 +02009104
9105vec_fail_out:
James Smartda0436e2009-05-22 14:51:39 -04009106 return rc;
9107}
9108
9109/**
9110 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
9111 * @phba: pointer to lpfc hba data structure.
9112 *
9113 * This routine is invoked to release the MSI-X vectors and then disable the
9114 * MSI-X interrupt mode to device with SLI-4 interface spec.
9115 **/
9116static void
9117lpfc_sli4_disable_msix(struct lpfc_hba *phba)
9118{
9119 int index;
9120
9121 /* Free up MSI-X multi-message vectors */
James Smartacbd8612013-09-06 12:21:19 -04009122 for (index = 0; index < phba->cfg_fcp_io_channel; index++) {
9123 irq_set_affinity_hint(phba->sli4_hba.msix_entries[index].
9124 vector, NULL);
James Smartda0436e2009-05-22 14:51:39 -04009125 free_irq(phba->sli4_hba.msix_entries[index].vector,
James Smart67d12732012-08-03 12:36:13 -04009126 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smartacbd8612013-09-06 12:21:19 -04009127 }
James Smart1ba981f2014-02-20 09:56:45 -05009128 if (phba->cfg_fof) {
9129 free_irq(phba->sli4_hba.msix_entries[index].vector,
9130 &phba->sli4_hba.fcp_eq_hdl[index]);
9131 }
James Smartda0436e2009-05-22 14:51:39 -04009132 /* Disable MSI-X */
9133 pci_disable_msix(phba->pcidev);
9134
9135 return;
9136}
9137
9138/**
9139 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
9140 * @phba: pointer to lpfc hba data structure.
9141 *
9142 * This routine is invoked to enable the MSI interrupt mode to device with
9143 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
9144 * to enable the MSI vector. The device driver is responsible for calling
9145 * the request_irq() to register MSI vector with a interrupt the handler,
9146 * which is done in this function.
9147 *
9148 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009149 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04009150 * other values - error
9151 **/
9152static int
9153lpfc_sli4_enable_msi(struct lpfc_hba *phba)
9154{
9155 int rc, index;
9156
9157 rc = pci_enable_msi(phba->pcidev);
9158 if (!rc)
9159 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9160 "0487 PCI enable MSI mode success.\n");
9161 else {
9162 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9163 "0488 PCI enable MSI mode failed (%d)\n", rc);
9164 return rc;
9165 }
9166
9167 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
James Smarted243d32015-05-21 13:55:25 -04009168 0, LPFC_DRIVER_NAME, phba);
James Smartda0436e2009-05-22 14:51:39 -04009169 if (rc) {
9170 pci_disable_msi(phba->pcidev);
9171 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9172 "0490 MSI request_irq failed (%d)\n", rc);
James Smart75baf692010-06-08 18:31:21 -04009173 return rc;
James Smartda0436e2009-05-22 14:51:39 -04009174 }
9175
James Smart67d12732012-08-03 12:36:13 -04009176 for (index = 0; index < phba->cfg_fcp_io_channel; index++) {
James Smartda0436e2009-05-22 14:51:39 -04009177 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
9178 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
9179 }
9180
James Smart1ba981f2014-02-20 09:56:45 -05009181 if (phba->cfg_fof) {
9182 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
9183 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
9184 }
James Smart75baf692010-06-08 18:31:21 -04009185 return 0;
James Smartda0436e2009-05-22 14:51:39 -04009186}
9187
9188/**
9189 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
9190 * @phba: pointer to lpfc hba data structure.
9191 *
9192 * This routine is invoked to disable the MSI interrupt mode to device with
9193 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
9194 * done request_irq() on before calling pci_disable_msi(). Failure to do so
9195 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
9196 * its vector.
9197 **/
9198static void
9199lpfc_sli4_disable_msi(struct lpfc_hba *phba)
9200{
9201 free_irq(phba->pcidev->irq, phba);
9202 pci_disable_msi(phba->pcidev);
9203 return;
9204}
9205
9206/**
9207 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
9208 * @phba: pointer to lpfc hba data structure.
9209 *
9210 * This routine is invoked to enable device interrupt and associate driver's
9211 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
9212 * interface spec. Depends on the interrupt mode configured to the driver,
9213 * the driver will try to fallback from the configured interrupt mode to an
9214 * interrupt mode which is supported by the platform, kernel, and device in
9215 * the order of:
9216 * MSI-X -> MSI -> IRQ.
9217 *
9218 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02009219 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04009220 * other values - error
9221 **/
9222static uint32_t
9223lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
9224{
9225 uint32_t intr_mode = LPFC_INTR_ERROR;
9226 int retval, index;
9227
9228 if (cfg_mode == 2) {
9229 /* Preparation before conf_msi mbox cmd */
9230 retval = 0;
9231 if (!retval) {
9232 /* Now, try to enable MSI-X interrupt mode */
9233 retval = lpfc_sli4_enable_msix(phba);
9234 if (!retval) {
9235 /* Indicate initialization to MSI-X mode */
9236 phba->intr_type = MSIX;
9237 intr_mode = 2;
9238 }
9239 }
9240 }
9241
9242 /* Fallback to MSI if MSI-X initialization failed */
9243 if (cfg_mode >= 1 && phba->intr_type == NONE) {
9244 retval = lpfc_sli4_enable_msi(phba);
9245 if (!retval) {
9246 /* Indicate initialization to MSI mode */
9247 phba->intr_type = MSI;
9248 intr_mode = 1;
9249 }
9250 }
9251
9252 /* Fallback to INTx if both MSI-X/MSI initalization failed */
9253 if (phba->intr_type == NONE) {
9254 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
9255 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
9256 if (!retval) {
9257 /* Indicate initialization to INTx mode */
9258 phba->intr_type = INTx;
9259 intr_mode = 0;
James Smart67d12732012-08-03 12:36:13 -04009260 for (index = 0; index < phba->cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04009261 index++) {
9262 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
9263 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
James Smartba20c852012-08-03 12:36:52 -04009264 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].
9265 fcp_eq_in_use, 1);
James Smartda0436e2009-05-22 14:51:39 -04009266 }
James Smart1ba981f2014-02-20 09:56:45 -05009267 if (phba->cfg_fof) {
9268 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
9269 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
9270 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].
9271 fcp_eq_in_use, 1);
9272 }
James Smartda0436e2009-05-22 14:51:39 -04009273 }
9274 }
9275 return intr_mode;
9276}
9277
9278/**
9279 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
9280 * @phba: pointer to lpfc hba data structure.
9281 *
9282 * This routine is invoked to disable device interrupt and disassociate
9283 * the driver's interrupt handler(s) from interrupt vector(s) to device
9284 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
9285 * will release the interrupt vector(s) for the message signaled interrupt.
9286 **/
9287static void
9288lpfc_sli4_disable_intr(struct lpfc_hba *phba)
9289{
9290 /* Disable the currently initialized interrupt mode */
9291 if (phba->intr_type == MSIX)
9292 lpfc_sli4_disable_msix(phba);
9293 else if (phba->intr_type == MSI)
9294 lpfc_sli4_disable_msi(phba);
9295 else if (phba->intr_type == INTx)
9296 free_irq(phba->pcidev->irq, phba);
9297
9298 /* Reset interrupt management states */
9299 phba->intr_type = NONE;
9300 phba->sli.slistat.sli_intr = 0;
9301
9302 return;
9303}
9304
9305/**
James Smart3772a992009-05-22 14:50:54 -04009306 * lpfc_unset_hba - Unset SLI3 hba device initialization
9307 * @phba: pointer to lpfc hba data structure.
9308 *
9309 * This routine is invoked to unset the HBA device initialization steps to
9310 * a device with SLI-3 interface spec.
9311 **/
9312static void
9313lpfc_unset_hba(struct lpfc_hba *phba)
9314{
9315 struct lpfc_vport *vport = phba->pport;
9316 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
9317
9318 spin_lock_irq(shost->host_lock);
9319 vport->load_flag |= FC_UNLOADING;
9320 spin_unlock_irq(shost->host_lock);
9321
James Smart72859902012-01-18 16:25:38 -05009322 kfree(phba->vpi_bmask);
9323 kfree(phba->vpi_ids);
9324
James Smart3772a992009-05-22 14:50:54 -04009325 lpfc_stop_hba_timers(phba);
9326
9327 phba->pport->work_port_events = 0;
9328
9329 lpfc_sli_hba_down(phba);
9330
9331 lpfc_sli_brdrestart(phba);
9332
9333 lpfc_sli_disable_intr(phba);
9334
9335 return;
9336}
9337
9338/**
James Smart5af5eee2010-10-22 11:06:38 -04009339 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
9340 * @phba: Pointer to HBA context object.
9341 *
9342 * This function is called in the SLI4 code path to wait for completion
9343 * of device's XRIs exchange busy. It will check the XRI exchange busy
9344 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
9345 * that, it will check the XRI exchange busy on outstanding FCP and ELS
9346 * I/Os every 30 seconds, log error message, and wait forever. Only when
9347 * all XRI exchange busy complete, the driver unload shall proceed with
9348 * invoking the function reset ioctl mailbox command to the CNA and the
9349 * the rest of the driver unload resource release.
9350 **/
9351static void
9352lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
9353{
9354 int wait_time = 0;
9355 int fcp_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
9356 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
9357
9358 while (!fcp_xri_cmpl || !els_xri_cmpl) {
9359 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
9360 if (!fcp_xri_cmpl)
9361 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9362 "2877 FCP XRI exchange busy "
9363 "wait time: %d seconds.\n",
9364 wait_time/1000);
9365 if (!els_xri_cmpl)
9366 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9367 "2878 ELS XRI exchange busy "
9368 "wait time: %d seconds.\n",
9369 wait_time/1000);
9370 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
9371 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
9372 } else {
9373 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
9374 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
9375 }
9376 fcp_xri_cmpl =
9377 list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
9378 els_xri_cmpl =
9379 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
9380 }
9381}
9382
9383/**
James Smartda0436e2009-05-22 14:51:39 -04009384 * lpfc_sli4_hba_unset - Unset the fcoe hba
9385 * @phba: Pointer to HBA context object.
9386 *
9387 * This function is called in the SLI4 code path to reset the HBA's FCoE
9388 * function. The caller is not required to hold any lock. This routine
9389 * issues PCI function reset mailbox command to reset the FCoE function.
9390 * At the end of the function, it calls lpfc_hba_down_post function to
9391 * free any pending commands.
9392 **/
9393static void
9394lpfc_sli4_hba_unset(struct lpfc_hba *phba)
9395{
9396 int wait_cnt = 0;
9397 LPFC_MBOXQ_t *mboxq;
James Smart912e3ac2011-05-24 11:42:11 -04009398 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -04009399
9400 lpfc_stop_hba_timers(phba);
9401 phba->sli4_hba.intr_enable = 0;
9402
9403 /*
9404 * Gracefully wait out the potential current outstanding asynchronous
9405 * mailbox command.
9406 */
9407
9408 /* First, block any pending async mailbox command from posted */
9409 spin_lock_irq(&phba->hbalock);
9410 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
9411 spin_unlock_irq(&phba->hbalock);
9412 /* Now, trying to wait it out if we can */
9413 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9414 msleep(10);
9415 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
9416 break;
9417 }
9418 /* Forcefully release the outstanding mailbox command if timed out */
9419 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9420 spin_lock_irq(&phba->hbalock);
9421 mboxq = phba->sli.mbox_active;
9422 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
9423 __lpfc_mbox_cmpl_put(phba, mboxq);
9424 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9425 phba->sli.mbox_active = NULL;
9426 spin_unlock_irq(&phba->hbalock);
9427 }
9428
James Smart5af5eee2010-10-22 11:06:38 -04009429 /* Abort all iocbs associated with the hba */
9430 lpfc_sli_hba_iocb_abort(phba);
9431
9432 /* Wait for completion of device XRI exchange busy */
9433 lpfc_sli4_xri_exchange_busy_wait(phba);
9434
James Smartda0436e2009-05-22 14:51:39 -04009435 /* Disable PCI subsystem interrupt */
9436 lpfc_sli4_disable_intr(phba);
9437
James Smart912e3ac2011-05-24 11:42:11 -04009438 /* Disable SR-IOV if enabled */
9439 if (phba->cfg_sriov_nr_virtfn)
9440 pci_disable_sriov(pdev);
9441
James Smartda0436e2009-05-22 14:51:39 -04009442 /* Stop kthread signal shall trigger work_done one more time */
9443 kthread_stop(phba->worker_thread);
9444
James Smart3677a3a2010-09-29 11:19:14 -04009445 /* Reset SLI4 HBA FCoE function */
9446 lpfc_pci_function_reset(phba);
James Smart5350d872011-10-10 21:33:49 -04009447 lpfc_sli4_queue_destroy(phba);
James Smart3677a3a2010-09-29 11:19:14 -04009448
James Smartda0436e2009-05-22 14:51:39 -04009449 /* Stop the SLI4 device port */
9450 phba->pport->work_port_events = 0;
9451}
9452
James Smart28baac72010-02-12 14:42:03 -05009453 /**
9454 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
9455 * @phba: Pointer to HBA context object.
9456 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
9457 *
9458 * This function is called in the SLI4 code path to read the port's
9459 * sli4 capabilities.
9460 *
9461 * This function may be be called from any context that can block-wait
9462 * for the completion. The expectation is that this routine is called
9463 * typically from probe_one or from the online routine.
9464 **/
9465int
9466lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9467{
9468 int rc;
9469 struct lpfc_mqe *mqe;
9470 struct lpfc_pc_sli4_params *sli4_params;
9471 uint32_t mbox_tmo;
9472
9473 rc = 0;
9474 mqe = &mboxq->u.mqe;
9475
9476 /* Read the port's SLI4 Parameters port capabilities */
James Smartfedd3b72011-02-16 12:39:24 -05009477 lpfc_pc_sli4_params(mboxq);
James Smart28baac72010-02-12 14:42:03 -05009478 if (!phba->sli4_hba.intr_enable)
9479 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9480 else {
James Smarta183a152011-10-10 21:32:43 -04009481 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart28baac72010-02-12 14:42:03 -05009482 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
9483 }
9484
9485 if (unlikely(rc))
9486 return 1;
9487
9488 sli4_params = &phba->sli4_hba.pc_sli4_params;
9489 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
9490 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
9491 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
9492 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
9493 &mqe->un.sli4_params);
9494 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
9495 &mqe->un.sli4_params);
9496 sli4_params->proto_types = mqe->un.sli4_params.word3;
9497 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
9498 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
9499 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
9500 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
9501 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
9502 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
9503 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
9504 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
9505 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
9506 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
9507 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
9508 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
9509 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
9510 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
9511 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
9512 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
9513 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
9514 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
9515 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
9516 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
James Smart05580562011-05-24 11:40:48 -04009517
9518 /* Make sure that sge_supp_len can be handled by the driver */
9519 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
9520 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
9521
James Smart28baac72010-02-12 14:42:03 -05009522 return rc;
9523}
9524
James Smartda0436e2009-05-22 14:51:39 -04009525/**
James Smartfedd3b72011-02-16 12:39:24 -05009526 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
9527 * @phba: Pointer to HBA context object.
9528 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
9529 *
9530 * This function is called in the SLI4 code path to read the port's
9531 * sli4 capabilities.
9532 *
9533 * This function may be be called from any context that can block-wait
9534 * for the completion. The expectation is that this routine is called
9535 * typically from probe_one or from the online routine.
9536 **/
9537int
9538lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9539{
9540 int rc;
9541 struct lpfc_mqe *mqe = &mboxq->u.mqe;
9542 struct lpfc_pc_sli4_params *sli4_params;
James Smarta183a152011-10-10 21:32:43 -04009543 uint32_t mbox_tmo;
James Smartfedd3b72011-02-16 12:39:24 -05009544 int length;
9545 struct lpfc_sli4_parameters *mbx_sli4_parameters;
9546
James Smart6d368e52011-05-24 11:44:12 -04009547 /*
9548 * By default, the driver assumes the SLI4 port requires RPI
9549 * header postings. The SLI4_PARAM response will correct this
9550 * assumption.
9551 */
9552 phba->sli4_hba.rpi_hdrs_in_use = 1;
9553
James Smartfedd3b72011-02-16 12:39:24 -05009554 /* Read the port's SLI4 Config Parameters */
9555 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
9556 sizeof(struct lpfc_sli4_cfg_mhdr));
9557 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
9558 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
9559 length, LPFC_SLI4_MBX_EMBED);
9560 if (!phba->sli4_hba.intr_enable)
9561 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -04009562 else {
9563 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
9564 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
9565 }
James Smartfedd3b72011-02-16 12:39:24 -05009566 if (unlikely(rc))
9567 return rc;
9568 sli4_params = &phba->sli4_hba.pc_sli4_params;
9569 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
9570 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
9571 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
9572 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
9573 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
9574 mbx_sli4_parameters);
9575 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
9576 mbx_sli4_parameters);
9577 if (bf_get(cfg_phwq, mbx_sli4_parameters))
9578 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
9579 else
9580 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
9581 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
9582 sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
James Smart1ba981f2014-02-20 09:56:45 -05009583 sli4_params->oas_supported = bf_get(cfg_oas, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -05009584 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
9585 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
9586 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
9587 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
James Smart0c651872013-07-15 18:33:23 -04009588 sli4_params->wqsize = bf_get(cfg_wqsize, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -05009589 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
9590 mbx_sli4_parameters);
9591 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
9592 mbx_sli4_parameters);
James Smart6d368e52011-05-24 11:44:12 -04009593 phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
9594 phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
James Smart05580562011-05-24 11:40:48 -04009595
9596 /* Make sure that sge_supp_len can be handled by the driver */
9597 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
9598 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
9599
James Smartb5c53952016-03-31 14:12:30 -07009600 /*
9601 * Issue IOs with CDB embedded in WQE to minimized the number
9602 * of DMAs the firmware has to do. Setting this to 1 also forces
9603 * the driver to use 128 bytes WQEs for FCP IOs.
9604 */
9605 if (bf_get(cfg_ext_embed_cb, mbx_sli4_parameters))
9606 phba->fcp_embed_io = 1;
9607 else
9608 phba->fcp_embed_io = 0;
James Smart7bdedb32016-07-06 12:36:00 -07009609
9610 /*
9611 * Check if the SLI port supports MDS Diagnostics
9612 */
9613 if (bf_get(cfg_mds_diags, mbx_sli4_parameters))
9614 phba->mds_diags_support = 1;
9615 else
9616 phba->mds_diags_support = 0;
James Smartfedd3b72011-02-16 12:39:24 -05009617 return 0;
9618}
9619
9620/**
James Smart3772a992009-05-22 14:50:54 -04009621 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
9622 * @pdev: pointer to PCI device
9623 * @pid: pointer to PCI device identifier
9624 *
9625 * This routine is to be called to attach a device with SLI-3 interface spec
9626 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
9627 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
9628 * information of the device and driver to see if the driver state that it can
9629 * support this kind of device. If the match is successful, the driver core
9630 * invokes this routine. If this routine determines it can claim the HBA, it
9631 * does all the initialization that it needs to do to handle the HBA properly.
9632 *
9633 * Return code
9634 * 0 - driver can claim the device
9635 * negative value - driver can not claim the device
9636 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009637static int
James Smart3772a992009-05-22 14:50:54 -04009638lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
9639{
9640 struct lpfc_hba *phba;
9641 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04009642 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -04009643 int error;
9644 uint32_t cfg_mode, intr_mode;
9645
9646 /* Allocate memory for HBA structure */
9647 phba = lpfc_hba_alloc(pdev);
9648 if (!phba)
9649 return -ENOMEM;
9650
9651 /* Perform generic PCI device enabling operation */
9652 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -04009653 if (error)
James Smart3772a992009-05-22 14:50:54 -04009654 goto out_free_phba;
James Smart3772a992009-05-22 14:50:54 -04009655
9656 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
9657 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
9658 if (error)
9659 goto out_disable_pci_dev;
9660
9661 /* Set up SLI-3 specific device PCI memory space */
9662 error = lpfc_sli_pci_mem_setup(phba);
9663 if (error) {
9664 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9665 "1402 Failed to set up pci memory space.\n");
9666 goto out_disable_pci_dev;
9667 }
9668
9669 /* Set up phase-1 common device driver resources */
9670 error = lpfc_setup_driver_resource_phase1(phba);
9671 if (error) {
9672 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9673 "1403 Failed to set up driver resource.\n");
9674 goto out_unset_pci_mem_s3;
9675 }
9676
9677 /* Set up SLI-3 specific device driver resources */
9678 error = lpfc_sli_driver_resource_setup(phba);
9679 if (error) {
9680 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9681 "1404 Failed to set up driver resource.\n");
9682 goto out_unset_pci_mem_s3;
9683 }
9684
9685 /* Initialize and populate the iocb list per host */
9686 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
9687 if (error) {
9688 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9689 "1405 Failed to initialize iocb list.\n");
9690 goto out_unset_driver_resource_s3;
9691 }
9692
9693 /* Set up common device driver resources */
9694 error = lpfc_setup_driver_resource_phase2(phba);
9695 if (error) {
9696 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9697 "1406 Failed to set up driver resource.\n");
9698 goto out_free_iocb_list;
9699 }
9700
James Smart079b5c92011-08-21 21:48:49 -04009701 /* Get the default values for Model Name and Description */
9702 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
9703
James Smart3772a992009-05-22 14:50:54 -04009704 /* Create SCSI host to the physical port */
9705 error = lpfc_create_shost(phba);
9706 if (error) {
9707 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9708 "1407 Failed to create scsi host.\n");
9709 goto out_unset_driver_resource;
9710 }
9711
9712 /* Configure sysfs attributes */
9713 vport = phba->pport;
9714 error = lpfc_alloc_sysfs_attr(vport);
9715 if (error) {
9716 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9717 "1476 Failed to allocate sysfs attr\n");
9718 goto out_destroy_shost;
9719 }
9720
James Smart6669f9b2009-10-02 15:16:45 -04009721 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -04009722 /* Now, trying to enable interrupt and bring up the device */
9723 cfg_mode = phba->cfg_use_msi;
9724 while (true) {
9725 /* Put device to a known state before enabling interrupt */
9726 lpfc_stop_port(phba);
9727 /* Configure and enable interrupt */
9728 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
9729 if (intr_mode == LPFC_INTR_ERROR) {
9730 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9731 "0431 Failed to enable interrupt.\n");
9732 error = -ENODEV;
9733 goto out_free_sysfs_attr;
9734 }
9735 /* SLI-3 HBA setup */
9736 if (lpfc_sli_hba_setup(phba)) {
9737 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9738 "1477 Failed to set up hba\n");
9739 error = -ENODEV;
9740 goto out_remove_device;
9741 }
9742
9743 /* Wait 50ms for the interrupts of previous mailbox commands */
9744 msleep(50);
9745 /* Check active interrupts on message signaled interrupts */
9746 if (intr_mode == 0 ||
9747 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
9748 /* Log the current active interrupt mode */
9749 phba->intr_mode = intr_mode;
9750 lpfc_log_intr_mode(phba, intr_mode);
9751 break;
9752 } else {
9753 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9754 "0447 Configure interrupt mode (%d) "
9755 "failed active interrupt test.\n",
9756 intr_mode);
9757 /* Disable the current interrupt mode */
9758 lpfc_sli_disable_intr(phba);
9759 /* Try next level of interrupt mode */
9760 cfg_mode = --intr_mode;
9761 }
9762 }
9763
9764 /* Perform post initialization setup */
9765 lpfc_post_init_setup(phba);
9766
9767 /* Check if there are static vports to be created. */
9768 lpfc_create_static_vport(phba);
9769
9770 return 0;
9771
9772out_remove_device:
9773 lpfc_unset_hba(phba);
9774out_free_sysfs_attr:
9775 lpfc_free_sysfs_attr(vport);
9776out_destroy_shost:
9777 lpfc_destroy_shost(phba);
9778out_unset_driver_resource:
9779 lpfc_unset_driver_resource_phase2(phba);
9780out_free_iocb_list:
9781 lpfc_free_iocb_list(phba);
9782out_unset_driver_resource_s3:
9783 lpfc_sli_driver_resource_unset(phba);
9784out_unset_pci_mem_s3:
9785 lpfc_sli_pci_mem_unset(phba);
9786out_disable_pci_dev:
9787 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04009788 if (shost)
9789 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -04009790out_free_phba:
9791 lpfc_hba_free(phba);
9792 return error;
9793}
9794
9795/**
9796 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -04009797 * @pdev: pointer to PCI device
9798 *
James Smart3772a992009-05-22 14:50:54 -04009799 * This routine is to be called to disattach a device with SLI-3 interface
9800 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
9801 * removed from PCI bus, it performs all the necessary cleanup for the HBA
9802 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -04009803 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009804static void
James Smart3772a992009-05-22 14:50:54 -04009805lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -05009806{
James Smart2e0fef82007-06-17 19:56:36 -05009807 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9808 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -05009809 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -05009810 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -05009811 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05009812 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
9813
James Smart549e55c2007-08-02 11:09:51 -04009814 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04009815 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04009816 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009817
James Smart858c9f62007-06-17 19:56:39 -05009818 lpfc_free_sysfs_attr(vport);
9819
James Smarteada2722008-12-04 22:39:13 -05009820 /* Release all the vports against this physical port */
9821 vports = lpfc_create_vport_work_array(phba);
9822 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -04009823 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
9824 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
9825 continue;
James Smarteada2722008-12-04 22:39:13 -05009826 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -04009827 }
James Smarteada2722008-12-04 22:39:13 -05009828 lpfc_destroy_vport_work_array(phba, vports);
9829
9830 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -05009831 fc_remove_host(shost);
9832 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04009833 lpfc_cleanup(vport);
9834
James Smart2e0fef82007-06-17 19:56:36 -05009835 /*
9836 * Bring down the SLI Layer. This step disable all interrupts,
9837 * clears the rings, discards all mailbox commands, and resets
9838 * the HBA.
9839 */
James Smarta257bf92009-04-06 18:48:10 -04009840
Justin P. Mattock48e34d02010-12-30 15:07:58 -08009841 /* HBA interrupt will be disabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -05009842 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -04009843 /* Stop kthread signal shall trigger work_done one more time */
9844 kthread_stop(phba->worker_thread);
9845 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -05009846 lpfc_sli_brdrestart(phba);
9847
James Smart72859902012-01-18 16:25:38 -05009848 kfree(phba->vpi_bmask);
9849 kfree(phba->vpi_ids);
9850
James Smart3772a992009-05-22 14:50:54 -04009851 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05009852 spin_lock_irq(&phba->hbalock);
9853 list_del_init(&vport->listentry);
9854 spin_unlock_irq(&phba->hbalock);
9855
James Smart858c9f62007-06-17 19:56:39 -05009856 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05009857
James Smart912e3ac2011-05-24 11:42:11 -04009858 /* Disable SR-IOV if enabled */
9859 if (phba->cfg_sriov_nr_virtfn)
9860 pci_disable_sriov(pdev);
9861
James Smart5b75da22008-12-04 22:39:35 -05009862 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009863 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -05009864
James Smart858c9f62007-06-17 19:56:39 -05009865 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05009866
9867 /*
9868 * Call scsi_free before mem_free since scsi bufs are released to their
9869 * corresponding pools here.
9870 */
9871 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -04009872 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -05009873
James Smart34b02dc2008-08-24 21:49:55 -04009874 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
9875 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -05009876
James Smart2e0fef82007-06-17 19:56:36 -05009877 /* Free resources associated with SLI2 interface */
9878 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -04009879 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -05009880
9881 /* unmap adapter SLIM and Control Registers */
9882 iounmap(phba->ctrl_regs_memmap_p);
9883 iounmap(phba->slim_memmap_p);
9884
James Smart3772a992009-05-22 14:50:54 -04009885 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -05009886
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05009887 pci_release_selected_regions(pdev, bars);
James Smart2e0fef82007-06-17 19:56:36 -05009888 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05009889}
9890
Linas Vepstas8d63f372007-02-14 14:28:36 -06009891/**
James Smart3772a992009-05-22 14:50:54 -04009892 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05009893 * @pdev: pointer to PCI device
9894 * @msg: power management message
9895 *
James Smart3772a992009-05-22 14:50:54 -04009896 * This routine is to be called from the kernel's PCI subsystem to support
9897 * system Power Management (PM) to device with SLI-3 interface spec. When
9898 * PM invokes this method, it quiesces the device by stopping the driver's
9899 * worker thread for the device, turning off device's interrupt and DMA,
9900 * and bring the device offline. Note that as the driver implements the
9901 * minimum PM requirements to a power-aware driver's PM support for the
9902 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
9903 * to the suspend() method call will be treated as SUSPEND and the driver will
9904 * fully reinitialize its device during resume() method call, the driver will
9905 * set device to PCI_D3hot state in PCI config space instead of setting it
9906 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -05009907 *
9908 * Return code
James Smart3772a992009-05-22 14:50:54 -04009909 * 0 - driver suspended the device
9910 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05009911 **/
9912static int
James Smart3772a992009-05-22 14:50:54 -04009913lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -05009914{
9915 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9916 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9917
9918 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9919 "0473 PCI device Power Management suspend.\n");
9920
9921 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -04009922 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart3a55b532008-12-04 22:38:54 -05009923 lpfc_offline(phba);
9924 kthread_stop(phba->worker_thread);
9925
9926 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -04009927 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -05009928
9929 /* Save device state to PCI config space */
9930 pci_save_state(pdev);
9931 pci_set_power_state(pdev, PCI_D3hot);
9932
9933 return 0;
9934}
9935
9936/**
James Smart3772a992009-05-22 14:50:54 -04009937 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05009938 * @pdev: pointer to PCI device
9939 *
James Smart3772a992009-05-22 14:50:54 -04009940 * This routine is to be called from the kernel's PCI subsystem to support
9941 * system Power Management (PM) to device with SLI-3 interface spec. When PM
9942 * invokes this method, it restores the device's PCI config space state and
9943 * fully reinitializes the device and brings it online. Note that as the
9944 * driver implements the minimum PM requirements to a power-aware driver's
9945 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
9946 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
9947 * driver will fully reinitialize its device during resume() method call,
9948 * the device will be set to PCI_D0 directly in PCI config space before
9949 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -05009950 *
9951 * Return code
James Smart3772a992009-05-22 14:50:54 -04009952 * 0 - driver suspended the device
9953 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05009954 **/
9955static int
James Smart3772a992009-05-22 14:50:54 -04009956lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -05009957{
9958 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9959 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -05009960 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05009961 int error;
9962
9963 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9964 "0452 PCI device Power Management resume.\n");
9965
9966 /* Restore device state from PCI config space */
9967 pci_set_power_state(pdev, PCI_D0);
9968 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -04009969
James Smart1dfb5a42010-02-12 14:40:50 -05009970 /*
9971 * As the new kernel behavior of pci_restore_state() API call clears
9972 * device saved_state flag, need to save the restored state again.
9973 */
9974 pci_save_state(pdev);
9975
James Smart3a55b532008-12-04 22:38:54 -05009976 if (pdev->is_busmaster)
9977 pci_set_master(pdev);
9978
9979 /* Startup the kernel thread for this host adapter. */
9980 phba->worker_thread = kthread_run(lpfc_do_work, phba,
9981 "lpfc_worker_%d", phba->brd_no);
9982 if (IS_ERR(phba->worker_thread)) {
9983 error = PTR_ERR(phba->worker_thread);
9984 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9985 "0434 PM resume failed to start worker "
9986 "thread: error=x%x.\n", error);
9987 return error;
9988 }
9989
James Smart5b75da22008-12-04 22:39:35 -05009990 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009991 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05009992 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -05009993 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -05009994 "0430 PM resume Failed to enable interrupt\n");
9995 return -EIO;
9996 } else
9997 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05009998
9999 /* Restart HBA and bring it online */
10000 lpfc_sli_brdrestart(phba);
10001 lpfc_online(phba);
10002
James Smart5b75da22008-12-04 22:39:35 -050010003 /* Log the current active interrupt mode */
10004 lpfc_log_intr_mode(phba, phba->intr_mode);
10005
James Smart3a55b532008-12-04 22:38:54 -050010006 return 0;
10007}
10008
10009/**
James Smart891478a2009-11-18 15:40:23 -050010010 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
10011 * @phba: pointer to lpfc hba data structure.
10012 *
10013 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -040010014 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -050010015 **/
10016static void
10017lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
10018{
10019 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10020 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -040010021
10022 /*
10023 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
10024 * and let the SCSI mid-layer to retry them to recover.
10025 */
James Smartdb55fba2014-04-04 13:52:02 -040010026 lpfc_sli_abort_fcp_rings(phba);
James Smart891478a2009-11-18 15:40:23 -050010027}
10028
10029/**
James Smart0d878412009-10-02 15:16:56 -040010030 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
10031 * @phba: pointer to lpfc hba data structure.
10032 *
10033 * This routine is called to prepare the SLI3 device for PCI slot reset. It
10034 * disables the device interrupt and pci device, and aborts the internal FCP
10035 * pending I/Os.
10036 **/
10037static void
10038lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
10039{
James Smart0d878412009-10-02 15:16:56 -040010040 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -050010041 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -040010042
James Smart75baf692010-06-08 18:31:21 -040010043 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040010044 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040010045
James Smarte2af0d22010-03-15 11:25:32 -040010046 /* Block all SCSI devices' I/Os on the host */
10047 lpfc_scsi_dev_block(phba);
10048
James Smartea714f32013-04-17 20:18:47 -040010049 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
10050 lpfc_sli_flush_fcp_rings(phba);
10051
James Smarte2af0d22010-03-15 11:25:32 -040010052 /* stop all timers */
10053 lpfc_stop_hba_timers(phba);
10054
James Smart0d878412009-10-02 15:16:56 -040010055 /* Disable interrupt and pci device */
10056 lpfc_sli_disable_intr(phba);
10057 pci_disable_device(phba->pcidev);
James Smart0d878412009-10-02 15:16:56 -040010058}
10059
10060/**
10061 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
10062 * @phba: pointer to lpfc hba data structure.
10063 *
10064 * This routine is called to prepare the SLI3 device for PCI slot permanently
10065 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
10066 * pending I/Os.
10067 **/
10068static void
James Smart75baf692010-06-08 18:31:21 -040010069lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
James Smart0d878412009-10-02 15:16:56 -040010070{
10071 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -050010072 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -040010073 /* Block all SCSI devices' I/Os on the host */
10074 lpfc_scsi_dev_block(phba);
10075
10076 /* stop all timers */
10077 lpfc_stop_hba_timers(phba);
10078
James Smart0d878412009-10-02 15:16:56 -040010079 /* Clean up all driver's outstanding SCSI I/Os */
10080 lpfc_sli_flush_fcp_rings(phba);
10081}
10082
10083/**
James Smart3772a992009-05-22 14:50:54 -040010084 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -040010085 * @pdev: pointer to PCI device.
10086 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -060010087 *
James Smart3772a992009-05-22 14:50:54 -040010088 * This routine is called from the PCI subsystem for I/O error handling to
10089 * device with SLI-3 interface spec. This function is called by the PCI
10090 * subsystem after a PCI bus error affecting this device has been detected.
10091 * When this function is invoked, it will need to stop all the I/Os and
10092 * interrupt(s) to the device. Once that is done, it will return
10093 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
10094 * as desired.
James Smarte59058c2008-08-24 21:49:00 -040010095 *
10096 * Return codes
James Smart0d878412009-10-02 15:16:56 -040010097 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -040010098 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10099 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -040010100 **/
James Smart3772a992009-05-22 14:50:54 -040010101static pci_ers_result_t
10102lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -060010103{
James Smart51ef4c22007-08-02 11:10:31 -040010104 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10105 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060010106
James Smart0d878412009-10-02 15:16:56 -040010107 switch (state) {
10108 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -050010109 /* Non-fatal error, prepare for recovery */
10110 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -040010111 return PCI_ERS_RESULT_CAN_RECOVER;
10112 case pci_channel_io_frozen:
10113 /* Fatal error, prepare for slot reset */
10114 lpfc_sli_prep_dev_for_reset(phba);
10115 return PCI_ERS_RESULT_NEED_RESET;
10116 case pci_channel_io_perm_failure:
10117 /* Permanent failure, prepare for device down */
James Smart75baf692010-06-08 18:31:21 -040010118 lpfc_sli_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -060010119 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -040010120 default:
10121 /* Unknown state, prepare and request slot reset */
10122 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10123 "0472 Unknown PCI error state: x%x\n", state);
10124 lpfc_sli_prep_dev_for_reset(phba);
10125 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -040010126 }
Linas Vepstas8d63f372007-02-14 14:28:36 -060010127}
10128
10129/**
James Smart3772a992009-05-22 14:50:54 -040010130 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -040010131 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -060010132 *
James Smart3772a992009-05-22 14:50:54 -040010133 * This routine is called from the PCI subsystem for error handling to
10134 * device with SLI-3 interface spec. This is called after PCI bus has been
10135 * reset to restart the PCI card from scratch, as if from a cold-boot.
10136 * During the PCI subsystem error recovery, after driver returns
10137 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
10138 * recovery and then call this routine before calling the .resume method
10139 * to recover the device. This function will initialize the HBA device,
10140 * enable the interrupt, but it will just put the HBA to offline state
10141 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -040010142 *
10143 * Return codes
James Smart3772a992009-05-22 14:50:54 -040010144 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10145 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -060010146 */
James Smart3772a992009-05-22 14:50:54 -040010147static pci_ers_result_t
10148lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -060010149{
James Smart51ef4c22007-08-02 11:10:31 -040010150 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10151 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060010152 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -050010153 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -060010154
10155 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +110010156 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -060010157 printk(KERN_ERR "lpfc: Cannot re-enable "
10158 "PCI device after reset.\n");
10159 return PCI_ERS_RESULT_DISCONNECT;
10160 }
10161
James Smart97207482008-12-04 22:39:19 -050010162 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050010163
10164 /*
10165 * As the new kernel behavior of pci_restore_state() API call clears
10166 * device saved_state flag, need to save the restored state again.
10167 */
10168 pci_save_state(pdev);
10169
James Smart97207482008-12-04 22:39:19 -050010170 if (pdev->is_busmaster)
10171 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -060010172
James Smart92d7f7b2007-06-17 19:56:38 -050010173 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010174 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -050010175 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -060010176
James Smart5b75da22008-12-04 22:39:35 -050010177 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -040010178 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -050010179 if (intr_mode == LPFC_INTR_ERROR) {
10180 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10181 "0427 Cannot re-enable interrupt after "
10182 "slot reset.\n");
10183 return PCI_ERS_RESULT_DISCONNECT;
10184 } else
10185 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -060010186
James Smart75baf692010-06-08 18:31:21 -040010187 /* Take device offline, it will perform cleanup */
James Smart618a5232012-06-12 13:54:36 -040010188 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
Linas Vepstas8d63f372007-02-14 14:28:36 -060010189 lpfc_offline(phba);
10190 lpfc_sli_brdrestart(phba);
10191
James Smart5b75da22008-12-04 22:39:35 -050010192 /* Log the current active interrupt mode */
10193 lpfc_log_intr_mode(phba, phba->intr_mode);
10194
Linas Vepstas8d63f372007-02-14 14:28:36 -060010195 return PCI_ERS_RESULT_RECOVERED;
10196}
10197
10198/**
James Smart3772a992009-05-22 14:50:54 -040010199 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -040010200 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -060010201 *
James Smart3772a992009-05-22 14:50:54 -040010202 * This routine is called from the PCI subsystem for error handling to device
10203 * with SLI-3 interface spec. It is called when kernel error recovery tells
10204 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
10205 * error recovery. After this call, traffic can start to flow from this device
10206 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -060010207 */
James Smart3772a992009-05-22 14:50:54 -040010208static void
10209lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -060010210{
James Smart51ef4c22007-08-02 11:10:31 -040010211 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10212 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -060010213
James Smarte2af0d22010-03-15 11:25:32 -040010214 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -040010215 lpfc_online(phba);
James Smart0d878412009-10-02 15:16:56 -040010216
10217 /* Clean up Advanced Error Reporting (AER) if needed */
10218 if (phba->hba_flag & HBA_AER_ENABLED)
10219 pci_cleanup_aer_uncorrect_error_status(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -060010220}
10221
James Smart3772a992009-05-22 14:50:54 -040010222/**
James Smartda0436e2009-05-22 14:51:39 -040010223 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
10224 * @phba: pointer to lpfc hba data structure.
10225 *
10226 * returns the number of ELS/CT IOCBs to reserve
10227 **/
10228int
10229lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
10230{
10231 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
10232
James Smartf1126682009-06-10 17:22:44 -040010233 if (phba->sli_rev == LPFC_SLI_REV4) {
10234 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -040010235 return 10;
James Smartf1126682009-06-10 17:22:44 -040010236 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -040010237 return 25;
James Smartf1126682009-06-10 17:22:44 -040010238 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -040010239 return 50;
James Smartf1126682009-06-10 17:22:44 -040010240 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -040010241 return 100;
James Smart8a9d2e82012-05-09 21:16:12 -040010242 else if (max_xri <= 1536)
James Smart6a9c52c2009-10-02 15:16:51 -040010243 return 150;
James Smart8a9d2e82012-05-09 21:16:12 -040010244 else if (max_xri <= 2048)
10245 return 200;
10246 else
10247 return 250;
James Smartf1126682009-06-10 17:22:44 -040010248 } else
10249 return 0;
James Smartda0436e2009-05-22 14:51:39 -040010250}
10251
10252/**
James Smart52d52442011-05-24 11:42:45 -040010253 * lpfc_write_firmware - attempt to write a firmware image to the port
James Smart52d52442011-05-24 11:42:45 -040010254 * @fw: pointer to firmware image returned from request_firmware.
James Smartce396282012-09-29 11:30:56 -040010255 * @phba: pointer to lpfc hba data structure.
James Smart52d52442011-05-24 11:42:45 -040010256 *
James Smart52d52442011-05-24 11:42:45 -040010257 **/
James Smartce396282012-09-29 11:30:56 -040010258static void
10259lpfc_write_firmware(const struct firmware *fw, void *context)
James Smart52d52442011-05-24 11:42:45 -040010260{
James Smartce396282012-09-29 11:30:56 -040010261 struct lpfc_hba *phba = (struct lpfc_hba *)context;
James Smart6b5151f2012-01-18 16:24:06 -050010262 char fwrev[FW_REV_STR_SIZE];
James Smartce396282012-09-29 11:30:56 -040010263 struct lpfc_grp_hdr *image;
James Smart52d52442011-05-24 11:42:45 -040010264 struct list_head dma_buffer_list;
10265 int i, rc = 0;
10266 struct lpfc_dmabuf *dmabuf, *next;
10267 uint32_t offset = 0, temp_offset = 0;
10268
James Smartc71ab862012-10-31 14:44:33 -040010269 /* It can be null in no-wait mode, sanity check */
James Smartce396282012-09-29 11:30:56 -040010270 if (!fw) {
10271 rc = -ENXIO;
10272 goto out;
10273 }
10274 image = (struct lpfc_grp_hdr *)fw->data;
10275
James Smart52d52442011-05-24 11:42:45 -040010276 INIT_LIST_HEAD(&dma_buffer_list);
James Smart079b5c92011-08-21 21:48:49 -040010277 if ((be32_to_cpu(image->magic_number) != LPFC_GROUP_OJECT_MAGIC_NUM) ||
10278 (bf_get_be32(lpfc_grp_hdr_file_type, image) !=
10279 LPFC_FILE_TYPE_GROUP) ||
10280 (bf_get_be32(lpfc_grp_hdr_id, image) != LPFC_FILE_ID_GROUP) ||
10281 (be32_to_cpu(image->size) != fw->size)) {
James Smart52d52442011-05-24 11:42:45 -040010282 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10283 "3022 Invalid FW image found. "
James Smart079b5c92011-08-21 21:48:49 -040010284 "Magic:%x Type:%x ID:%x\n",
10285 be32_to_cpu(image->magic_number),
10286 bf_get_be32(lpfc_grp_hdr_file_type, image),
10287 bf_get_be32(lpfc_grp_hdr_id, image));
James Smartce396282012-09-29 11:30:56 -040010288 rc = -EINVAL;
10289 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010290 }
10291 lpfc_decode_firmware_rev(phba, fwrev, 1);
James Smart88a2cfb2011-07-22 18:36:33 -040010292 if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
James Smart52d52442011-05-24 11:42:45 -040010293 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartce396282012-09-29 11:30:56 -040010294 "3023 Updating Firmware, Current Version:%s "
James Smart52d52442011-05-24 11:42:45 -040010295 "New Version:%s\n",
James Smart88a2cfb2011-07-22 18:36:33 -040010296 fwrev, image->revision);
James Smart52d52442011-05-24 11:42:45 -040010297 for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
10298 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
10299 GFP_KERNEL);
10300 if (!dmabuf) {
10301 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040010302 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010303 }
10304 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
10305 SLI4_PAGE_SIZE,
10306 &dmabuf->phys,
10307 GFP_KERNEL);
10308 if (!dmabuf->virt) {
10309 kfree(dmabuf);
10310 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040010311 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010312 }
10313 list_add_tail(&dmabuf->list, &dma_buffer_list);
10314 }
10315 while (offset < fw->size) {
10316 temp_offset = offset;
10317 list_for_each_entry(dmabuf, &dma_buffer_list, list) {
James Smart079b5c92011-08-21 21:48:49 -040010318 if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
James Smart52d52442011-05-24 11:42:45 -040010319 memcpy(dmabuf->virt,
10320 fw->data + temp_offset,
James Smart079b5c92011-08-21 21:48:49 -040010321 fw->size - temp_offset);
10322 temp_offset = fw->size;
James Smart52d52442011-05-24 11:42:45 -040010323 break;
10324 }
James Smart52d52442011-05-24 11:42:45 -040010325 memcpy(dmabuf->virt, fw->data + temp_offset,
10326 SLI4_PAGE_SIZE);
James Smart88a2cfb2011-07-22 18:36:33 -040010327 temp_offset += SLI4_PAGE_SIZE;
James Smart52d52442011-05-24 11:42:45 -040010328 }
10329 rc = lpfc_wr_object(phba, &dma_buffer_list,
10330 (fw->size - offset), &offset);
James Smartce396282012-09-29 11:30:56 -040010331 if (rc)
10332 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010333 }
10334 rc = offset;
10335 }
James Smartce396282012-09-29 11:30:56 -040010336
10337release_out:
James Smart52d52442011-05-24 11:42:45 -040010338 list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
10339 list_del(&dmabuf->list);
10340 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
10341 dmabuf->virt, dmabuf->phys);
10342 kfree(dmabuf);
10343 }
James Smartce396282012-09-29 11:30:56 -040010344 release_firmware(fw);
10345out:
10346 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartc71ab862012-10-31 14:44:33 -040010347 "3024 Firmware update done: %d.\n", rc);
James Smartce396282012-09-29 11:30:56 -040010348 return;
James Smart52d52442011-05-24 11:42:45 -040010349}
10350
10351/**
James Smartc71ab862012-10-31 14:44:33 -040010352 * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
10353 * @phba: pointer to lpfc hba data structure.
10354 *
10355 * This routine is called to perform Linux generic firmware upgrade on device
10356 * that supports such feature.
10357 **/
10358int
10359lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
10360{
10361 uint8_t file_name[ELX_MODEL_NAME_SIZE];
10362 int ret;
10363 const struct firmware *fw;
10364
10365 /* Only supported on SLI4 interface type 2 for now */
10366 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
10367 LPFC_SLI_INTF_IF_TYPE_2)
10368 return -EPERM;
10369
10370 snprintf(file_name, ELX_MODEL_NAME_SIZE, "%s.grp", phba->ModelName);
10371
10372 if (fw_upgrade == INT_FW_UPGRADE) {
10373 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
10374 file_name, &phba->pcidev->dev,
10375 GFP_KERNEL, (void *)phba,
10376 lpfc_write_firmware);
10377 } else if (fw_upgrade == RUN_FW_UPGRADE) {
10378 ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
10379 if (!ret)
10380 lpfc_write_firmware(fw, (void *)phba);
10381 } else {
10382 ret = -EINVAL;
10383 }
10384
10385 return ret;
10386}
10387
10388/**
James Smartda0436e2009-05-22 14:51:39 -040010389 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
10390 * @pdev: pointer to PCI device
10391 * @pid: pointer to PCI device identifier
10392 *
10393 * This routine is called from the kernel's PCI subsystem to device with
10394 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
10395 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
10396 * information of the device and driver to see if the driver state that it
10397 * can support this kind of device. If the match is successful, the driver
10398 * core invokes this routine. If this routine determines it can claim the HBA,
10399 * it does all the initialization that it needs to do to handle the HBA
10400 * properly.
10401 *
10402 * Return code
10403 * 0 - driver can claim the device
10404 * negative value - driver can not claim the device
10405 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010406static int
James Smartda0436e2009-05-22 14:51:39 -040010407lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
10408{
10409 struct lpfc_hba *phba;
10410 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -040010411 struct Scsi_Host *shost = NULL;
Sebastian Herbsztdb6f1c22015-08-31 16:48:14 -040010412 int error;
James Smartda0436e2009-05-22 14:51:39 -040010413 uint32_t cfg_mode, intr_mode;
James Smart67d12732012-08-03 12:36:13 -040010414 int adjusted_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -040010415
10416 /* Allocate memory for HBA structure */
10417 phba = lpfc_hba_alloc(pdev);
10418 if (!phba)
10419 return -ENOMEM;
10420
10421 /* Perform generic PCI device enabling operation */
10422 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -040010423 if (error)
James Smartda0436e2009-05-22 14:51:39 -040010424 goto out_free_phba;
James Smartda0436e2009-05-22 14:51:39 -040010425
10426 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
10427 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
10428 if (error)
10429 goto out_disable_pci_dev;
10430
10431 /* Set up SLI-4 specific device PCI memory space */
10432 error = lpfc_sli4_pci_mem_setup(phba);
10433 if (error) {
10434 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10435 "1410 Failed to set up pci memory space.\n");
10436 goto out_disable_pci_dev;
10437 }
10438
10439 /* Set up phase-1 common device driver resources */
10440 error = lpfc_setup_driver_resource_phase1(phba);
10441 if (error) {
10442 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10443 "1411 Failed to set up driver resource.\n");
10444 goto out_unset_pci_mem_s4;
10445 }
10446
10447 /* Set up SLI-4 Specific device driver resources */
10448 error = lpfc_sli4_driver_resource_setup(phba);
10449 if (error) {
10450 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10451 "1412 Failed to set up driver resource.\n");
10452 goto out_unset_pci_mem_s4;
10453 }
10454
10455 /* Initialize and populate the iocb list per host */
James Smart2a9bf3d2010-06-07 15:24:45 -040010456
10457 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10458 "2821 initialize iocb list %d.\n",
10459 phba->cfg_iocb_cnt*1024);
10460 error = lpfc_init_iocb_list(phba, phba->cfg_iocb_cnt*1024);
10461
James Smartda0436e2009-05-22 14:51:39 -040010462 if (error) {
10463 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10464 "1413 Failed to initialize iocb list.\n");
10465 goto out_unset_driver_resource_s4;
10466 }
10467
James Smart19ca7602010-11-20 23:11:55 -050010468 INIT_LIST_HEAD(&phba->active_rrq_list);
James Smart7d791df2011-07-22 18:37:52 -040010469 INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
James Smart19ca7602010-11-20 23:11:55 -050010470
James Smartda0436e2009-05-22 14:51:39 -040010471 /* Set up common device driver resources */
10472 error = lpfc_setup_driver_resource_phase2(phba);
10473 if (error) {
10474 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10475 "1414 Failed to set up driver resource.\n");
10476 goto out_free_iocb_list;
10477 }
10478
James Smart079b5c92011-08-21 21:48:49 -040010479 /* Get the default values for Model Name and Description */
10480 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
10481
James Smartda0436e2009-05-22 14:51:39 -040010482 /* Create SCSI host to the physical port */
10483 error = lpfc_create_shost(phba);
10484 if (error) {
10485 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10486 "1415 Failed to create scsi host.\n");
10487 goto out_unset_driver_resource;
10488 }
10489
10490 /* Configure sysfs attributes */
10491 vport = phba->pport;
10492 error = lpfc_alloc_sysfs_attr(vport);
10493 if (error) {
10494 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10495 "1416 Failed to allocate sysfs attr\n");
10496 goto out_destroy_shost;
10497 }
10498
James Smart6669f9b2009-10-02 15:16:45 -040010499 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -040010500 /* Now, trying to enable interrupt and bring up the device */
10501 cfg_mode = phba->cfg_use_msi;
James Smartda0436e2009-05-22 14:51:39 -040010502
James Smart7b15db32013-01-03 15:43:29 -050010503 /* Put device to a known state before enabling interrupt */
10504 lpfc_stop_port(phba);
10505 /* Configure and enable interrupt */
10506 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
10507 if (intr_mode == LPFC_INTR_ERROR) {
10508 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10509 "0426 Failed to enable interrupt.\n");
10510 error = -ENODEV;
10511 goto out_free_sysfs_attr;
James Smartda0436e2009-05-22 14:51:39 -040010512 }
James Smart7b15db32013-01-03 15:43:29 -050010513 /* Default to single EQ for non-MSI-X */
10514 if (phba->intr_type != MSIX)
10515 adjusted_fcp_io_channel = 1;
10516 else
10517 adjusted_fcp_io_channel = phba->cfg_fcp_io_channel;
10518 phba->cfg_fcp_io_channel = adjusted_fcp_io_channel;
10519 /* Set up SLI-4 HBA */
10520 if (lpfc_sli4_hba_setup(phba)) {
10521 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10522 "1421 Failed to set up hba\n");
10523 error = -ENODEV;
10524 goto out_disable_intr;
10525 }
10526
10527 /* Log the current active interrupt mode */
10528 phba->intr_mode = intr_mode;
10529 lpfc_log_intr_mode(phba, intr_mode);
James Smartda0436e2009-05-22 14:51:39 -040010530
10531 /* Perform post initialization setup */
10532 lpfc_post_init_setup(phba);
10533
James Smartc71ab862012-10-31 14:44:33 -040010534 /* check for firmware upgrade or downgrade */
10535 if (phba->cfg_request_firmware_upgrade)
Sebastian Herbsztdb6f1c22015-08-31 16:48:14 -040010536 lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
James Smart52d52442011-05-24 11:42:45 -040010537
James Smart1c6834a2009-07-19 10:01:26 -040010538 /* Check if there are static vports to be created. */
10539 lpfc_create_static_vport(phba);
James Smartda0436e2009-05-22 14:51:39 -040010540 return 0;
10541
10542out_disable_intr:
10543 lpfc_sli4_disable_intr(phba);
10544out_free_sysfs_attr:
10545 lpfc_free_sysfs_attr(vport);
10546out_destroy_shost:
10547 lpfc_destroy_shost(phba);
10548out_unset_driver_resource:
10549 lpfc_unset_driver_resource_phase2(phba);
10550out_free_iocb_list:
10551 lpfc_free_iocb_list(phba);
10552out_unset_driver_resource_s4:
10553 lpfc_sli4_driver_resource_unset(phba);
10554out_unset_pci_mem_s4:
10555 lpfc_sli4_pci_mem_unset(phba);
10556out_disable_pci_dev:
10557 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040010558 if (shost)
10559 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -040010560out_free_phba:
10561 lpfc_hba_free(phba);
10562 return error;
10563}
10564
10565/**
10566 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
10567 * @pdev: pointer to PCI device
10568 *
10569 * This routine is called from the kernel's PCI subsystem to device with
10570 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
10571 * removed from PCI bus, it performs all the necessary cleanup for the HBA
10572 * device to be removed from the PCI subsystem properly.
10573 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010574static void
James Smartda0436e2009-05-22 14:51:39 -040010575lpfc_pci_remove_one_s4(struct pci_dev *pdev)
10576{
10577 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10578 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
10579 struct lpfc_vport **vports;
10580 struct lpfc_hba *phba = vport->phba;
10581 int i;
10582
10583 /* Mark the device unloading flag */
10584 spin_lock_irq(&phba->hbalock);
10585 vport->load_flag |= FC_UNLOADING;
10586 spin_unlock_irq(&phba->hbalock);
10587
10588 /* Free the HBA sysfs attributes */
10589 lpfc_free_sysfs_attr(vport);
10590
10591 /* Release all the vports against this physical port */
10592 vports = lpfc_create_vport_work_array(phba);
10593 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040010594 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
10595 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
10596 continue;
James Smartda0436e2009-05-22 14:51:39 -040010597 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040010598 }
James Smartda0436e2009-05-22 14:51:39 -040010599 lpfc_destroy_vport_work_array(phba, vports);
10600
10601 /* Remove FC host and then SCSI host with the physical port */
10602 fc_remove_host(shost);
10603 scsi_remove_host(shost);
10604
10605 /* Perform cleanup on the physical port */
10606 lpfc_cleanup(vport);
10607
10608 /*
10609 * Bring down the SLI Layer. This step disables all interrupts,
10610 * clears the rings, discards all mailbox commands, and resets
10611 * the HBA FCoE function.
10612 */
10613 lpfc_debugfs_terminate(vport);
10614 lpfc_sli4_hba_unset(phba);
10615
10616 spin_lock_irq(&phba->hbalock);
10617 list_del_init(&vport->listentry);
10618 spin_unlock_irq(&phba->hbalock);
10619
James Smart3677a3a2010-09-29 11:19:14 -040010620 /* Perform scsi free before driver resource_unset since scsi
James Smartda0436e2009-05-22 14:51:39 -040010621 * buffers are released to their corresponding pools here.
10622 */
10623 lpfc_scsi_free(phba);
James Smart67d12732012-08-03 12:36:13 -040010624
James Smartda0436e2009-05-22 14:51:39 -040010625 lpfc_sli4_driver_resource_unset(phba);
10626
10627 /* Unmap adapter Control and Doorbell registers */
10628 lpfc_sli4_pci_mem_unset(phba);
10629
10630 /* Release PCI resources and disable device's PCI function */
10631 scsi_host_put(shost);
10632 lpfc_disable_pci_dev(phba);
10633
10634 /* Finally, free the driver's device data structure */
10635 lpfc_hba_free(phba);
10636
10637 return;
10638}
10639
10640/**
10641 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
10642 * @pdev: pointer to PCI device
10643 * @msg: power management message
10644 *
10645 * This routine is called from the kernel's PCI subsystem to support system
10646 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
10647 * this method, it quiesces the device by stopping the driver's worker
10648 * thread for the device, turning off device's interrupt and DMA, and bring
10649 * the device offline. Note that as the driver implements the minimum PM
10650 * requirements to a power-aware driver's PM support for suspend/resume -- all
10651 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
10652 * method call will be treated as SUSPEND and the driver will fully
10653 * reinitialize its device during resume() method call, the driver will set
10654 * device to PCI_D3hot state in PCI config space instead of setting it
10655 * according to the @msg provided by the PM.
10656 *
10657 * Return code
10658 * 0 - driver suspended the device
10659 * Error otherwise
10660 **/
10661static int
10662lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
10663{
10664 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10665 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10666
10667 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart75baf692010-06-08 18:31:21 -040010668 "2843 PCI device Power Management suspend.\n");
James Smartda0436e2009-05-22 14:51:39 -040010669
10670 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040010671 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smartda0436e2009-05-22 14:51:39 -040010672 lpfc_offline(phba);
10673 kthread_stop(phba->worker_thread);
10674
10675 /* Disable interrupt from device */
10676 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040010677 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -040010678
10679 /* Save device state to PCI config space */
10680 pci_save_state(pdev);
10681 pci_set_power_state(pdev, PCI_D3hot);
10682
10683 return 0;
10684}
10685
10686/**
10687 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
10688 * @pdev: pointer to PCI device
10689 *
10690 * This routine is called from the kernel's PCI subsystem to support system
10691 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
10692 * this method, it restores the device's PCI config space state and fully
10693 * reinitializes the device and brings it online. Note that as the driver
10694 * implements the minimum PM requirements to a power-aware driver's PM for
10695 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
10696 * to the suspend() method call will be treated as SUSPEND and the driver
10697 * will fully reinitialize its device during resume() method call, the device
10698 * will be set to PCI_D0 directly in PCI config space before restoring the
10699 * state.
10700 *
10701 * Return code
10702 * 0 - driver suspended the device
10703 * Error otherwise
10704 **/
10705static int
10706lpfc_pci_resume_one_s4(struct pci_dev *pdev)
10707{
10708 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10709 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10710 uint32_t intr_mode;
10711 int error;
10712
10713 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10714 "0292 PCI device Power Management resume.\n");
10715
10716 /* Restore device state from PCI config space */
10717 pci_set_power_state(pdev, PCI_D0);
10718 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050010719
10720 /*
10721 * As the new kernel behavior of pci_restore_state() API call clears
10722 * device saved_state flag, need to save the restored state again.
10723 */
10724 pci_save_state(pdev);
10725
James Smartda0436e2009-05-22 14:51:39 -040010726 if (pdev->is_busmaster)
10727 pci_set_master(pdev);
10728
10729 /* Startup the kernel thread for this host adapter. */
10730 phba->worker_thread = kthread_run(lpfc_do_work, phba,
10731 "lpfc_worker_%d", phba->brd_no);
10732 if (IS_ERR(phba->worker_thread)) {
10733 error = PTR_ERR(phba->worker_thread);
10734 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10735 "0293 PM resume failed to start worker "
10736 "thread: error=x%x.\n", error);
10737 return error;
10738 }
10739
10740 /* Configure and enable interrupt */
10741 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
10742 if (intr_mode == LPFC_INTR_ERROR) {
10743 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10744 "0294 PM resume Failed to enable interrupt\n");
10745 return -EIO;
10746 } else
10747 phba->intr_mode = intr_mode;
10748
10749 /* Restart HBA and bring it online */
10750 lpfc_sli_brdrestart(phba);
10751 lpfc_online(phba);
10752
10753 /* Log the current active interrupt mode */
10754 lpfc_log_intr_mode(phba, phba->intr_mode);
10755
10756 return 0;
10757}
10758
10759/**
James Smart75baf692010-06-08 18:31:21 -040010760 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
10761 * @phba: pointer to lpfc hba data structure.
10762 *
10763 * This routine is called to prepare the SLI4 device for PCI slot recover. It
10764 * aborts all the outstanding SCSI I/Os to the pci device.
10765 **/
10766static void
10767lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
10768{
James Smart75baf692010-06-08 18:31:21 -040010769 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10770 "2828 PCI channel I/O abort preparing for recovery\n");
10771 /*
10772 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
10773 * and let the SCSI mid-layer to retry them to recover.
10774 */
James Smartdb55fba2014-04-04 13:52:02 -040010775 lpfc_sli_abort_fcp_rings(phba);
James Smart75baf692010-06-08 18:31:21 -040010776}
10777
10778/**
10779 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
10780 * @phba: pointer to lpfc hba data structure.
10781 *
10782 * This routine is called to prepare the SLI4 device for PCI slot reset. It
10783 * disables the device interrupt and pci device, and aborts the internal FCP
10784 * pending I/Os.
10785 **/
10786static void
10787lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
10788{
10789 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10790 "2826 PCI channel disable preparing for reset\n");
10791
10792 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040010793 lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
James Smart75baf692010-06-08 18:31:21 -040010794
10795 /* Block all SCSI devices' I/Os on the host */
10796 lpfc_scsi_dev_block(phba);
10797
James Smartea714f32013-04-17 20:18:47 -040010798 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
10799 lpfc_sli_flush_fcp_rings(phba);
10800
James Smart75baf692010-06-08 18:31:21 -040010801 /* stop all timers */
10802 lpfc_stop_hba_timers(phba);
10803
10804 /* Disable interrupt and pci device */
10805 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040010806 lpfc_sli4_queue_destroy(phba);
James Smart75baf692010-06-08 18:31:21 -040010807 pci_disable_device(phba->pcidev);
James Smart75baf692010-06-08 18:31:21 -040010808}
10809
10810/**
10811 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
10812 * @phba: pointer to lpfc hba data structure.
10813 *
10814 * This routine is called to prepare the SLI4 device for PCI slot permanently
10815 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
10816 * pending I/Os.
10817 **/
10818static void
10819lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
10820{
10821 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10822 "2827 PCI channel permanent disable for failure\n");
10823
10824 /* Block all SCSI devices' I/Os on the host */
10825 lpfc_scsi_dev_block(phba);
10826
10827 /* stop all timers */
10828 lpfc_stop_hba_timers(phba);
10829
10830 /* Clean up all driver's outstanding SCSI I/Os */
10831 lpfc_sli_flush_fcp_rings(phba);
10832}
10833
10834/**
James Smartda0436e2009-05-22 14:51:39 -040010835 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
10836 * @pdev: pointer to PCI device.
10837 * @state: the current PCI connection state.
10838 *
10839 * This routine is called from the PCI subsystem for error handling to device
10840 * with SLI-4 interface spec. This function is called by the PCI subsystem
10841 * after a PCI bus error affecting this device has been detected. When this
10842 * function is invoked, it will need to stop all the I/Os and interrupt(s)
10843 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
10844 * for the PCI subsystem to perform proper recovery as desired.
10845 *
10846 * Return codes
10847 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10848 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10849 **/
10850static pci_ers_result_t
10851lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
10852{
James Smart75baf692010-06-08 18:31:21 -040010853 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10854 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10855
10856 switch (state) {
10857 case pci_channel_io_normal:
10858 /* Non-fatal error, prepare for recovery */
10859 lpfc_sli4_prep_dev_for_recover(phba);
10860 return PCI_ERS_RESULT_CAN_RECOVER;
10861 case pci_channel_io_frozen:
10862 /* Fatal error, prepare for slot reset */
10863 lpfc_sli4_prep_dev_for_reset(phba);
10864 return PCI_ERS_RESULT_NEED_RESET;
10865 case pci_channel_io_perm_failure:
10866 /* Permanent failure, prepare for device down */
10867 lpfc_sli4_prep_dev_for_perm_failure(phba);
10868 return PCI_ERS_RESULT_DISCONNECT;
10869 default:
10870 /* Unknown state, prepare and request slot reset */
10871 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10872 "2825 Unknown PCI error state: x%x\n", state);
10873 lpfc_sli4_prep_dev_for_reset(phba);
10874 return PCI_ERS_RESULT_NEED_RESET;
10875 }
James Smartda0436e2009-05-22 14:51:39 -040010876}
10877
10878/**
10879 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
10880 * @pdev: pointer to PCI device.
10881 *
10882 * This routine is called from the PCI subsystem for error handling to device
10883 * with SLI-4 interface spec. It is called after PCI bus has been reset to
10884 * restart the PCI card from scratch, as if from a cold-boot. During the
10885 * PCI subsystem error recovery, after the driver returns
10886 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
10887 * recovery and then call this routine before calling the .resume method to
10888 * recover the device. This function will initialize the HBA device, enable
10889 * the interrupt, but it will just put the HBA to offline state without
10890 * passing any I/O traffic.
10891 *
10892 * Return codes
10893 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10894 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10895 */
10896static pci_ers_result_t
10897lpfc_io_slot_reset_s4(struct pci_dev *pdev)
10898{
James Smart75baf692010-06-08 18:31:21 -040010899 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10900 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10901 struct lpfc_sli *psli = &phba->sli;
10902 uint32_t intr_mode;
10903
10904 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
10905 if (pci_enable_device_mem(pdev)) {
10906 printk(KERN_ERR "lpfc: Cannot re-enable "
10907 "PCI device after reset.\n");
10908 return PCI_ERS_RESULT_DISCONNECT;
10909 }
10910
10911 pci_restore_state(pdev);
James Smart0a96e972011-07-22 18:37:28 -040010912
10913 /*
10914 * As the new kernel behavior of pci_restore_state() API call clears
10915 * device saved_state flag, need to save the restored state again.
10916 */
10917 pci_save_state(pdev);
10918
James Smart75baf692010-06-08 18:31:21 -040010919 if (pdev->is_busmaster)
10920 pci_set_master(pdev);
10921
10922 spin_lock_irq(&phba->hbalock);
10923 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
10924 spin_unlock_irq(&phba->hbalock);
10925
10926 /* Configure and enable interrupt */
10927 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
10928 if (intr_mode == LPFC_INTR_ERROR) {
10929 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10930 "2824 Cannot re-enable interrupt after "
10931 "slot reset.\n");
10932 return PCI_ERS_RESULT_DISCONNECT;
10933 } else
10934 phba->intr_mode = intr_mode;
10935
10936 /* Log the current active interrupt mode */
10937 lpfc_log_intr_mode(phba, phba->intr_mode);
10938
James Smartda0436e2009-05-22 14:51:39 -040010939 return PCI_ERS_RESULT_RECOVERED;
10940}
10941
10942/**
10943 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
10944 * @pdev: pointer to PCI device
10945 *
10946 * This routine is called from the PCI subsystem for error handling to device
10947 * with SLI-4 interface spec. It is called when kernel error recovery tells
10948 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
10949 * error recovery. After this call, traffic can start to flow from this device
10950 * again.
10951 **/
10952static void
10953lpfc_io_resume_s4(struct pci_dev *pdev)
10954{
James Smart75baf692010-06-08 18:31:21 -040010955 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10956 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10957
10958 /*
10959 * In case of slot reset, as function reset is performed through
10960 * mailbox command which needs DMA to be enabled, this operation
10961 * has to be moved to the io resume phase. Taking device offline
10962 * will perform the necessary cleanup.
10963 */
10964 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
10965 /* Perform device reset */
James Smart618a5232012-06-12 13:54:36 -040010966 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040010967 lpfc_offline(phba);
10968 lpfc_sli_brdrestart(phba);
10969 /* Bring the device back online */
10970 lpfc_online(phba);
10971 }
10972
10973 /* Clean up Advanced Error Reporting (AER) if needed */
10974 if (phba->hba_flag & HBA_AER_ENABLED)
10975 pci_cleanup_aer_uncorrect_error_status(pdev);
James Smartda0436e2009-05-22 14:51:39 -040010976}
10977
10978/**
James Smart3772a992009-05-22 14:50:54 -040010979 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
10980 * @pdev: pointer to PCI device
10981 * @pid: pointer to PCI device identifier
10982 *
10983 * This routine is to be registered to the kernel's PCI subsystem. When an
10984 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
10985 * at PCI device-specific information of the device and driver to see if the
10986 * driver state that it can support this kind of device. If the match is
10987 * successful, the driver core invokes this routine. This routine dispatches
10988 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
10989 * do all the initialization that it needs to do to handle the HBA device
10990 * properly.
10991 *
10992 * Return code
10993 * 0 - driver can claim the device
10994 * negative value - driver can not claim the device
10995 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010996static int
James Smart3772a992009-05-22 14:50:54 -040010997lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
10998{
10999 int rc;
James Smart8fa38512009-07-19 10:01:03 -040011000 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -040011001
James Smart28baac72010-02-12 14:42:03 -050011002 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -040011003 return -ENODEV;
11004
James Smart8fa38512009-07-19 10:01:03 -040011005 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -050011006 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -040011007 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040011008 else
James Smart3772a992009-05-22 14:50:54 -040011009 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040011010
James Smart3772a992009-05-22 14:50:54 -040011011 return rc;
11012}
11013
11014/**
11015 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
11016 * @pdev: pointer to PCI device
11017 *
11018 * This routine is to be registered to the kernel's PCI subsystem. When an
11019 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
11020 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
11021 * remove routine, which will perform all the necessary cleanup for the
11022 * device to be removed from the PCI subsystem properly.
11023 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011024static void
James Smart3772a992009-05-22 14:50:54 -040011025lpfc_pci_remove_one(struct pci_dev *pdev)
11026{
11027 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11028 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11029
11030 switch (phba->pci_dev_grp) {
11031 case LPFC_PCI_DEV_LP:
11032 lpfc_pci_remove_one_s3(pdev);
11033 break;
James Smartda0436e2009-05-22 14:51:39 -040011034 case LPFC_PCI_DEV_OC:
11035 lpfc_pci_remove_one_s4(pdev);
11036 break;
James Smart3772a992009-05-22 14:50:54 -040011037 default:
11038 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11039 "1424 Invalid PCI device group: 0x%x\n",
11040 phba->pci_dev_grp);
11041 break;
11042 }
11043 return;
11044}
11045
11046/**
11047 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
11048 * @pdev: pointer to PCI device
11049 * @msg: power management message
11050 *
11051 * This routine is to be registered to the kernel's PCI subsystem to support
11052 * system Power Management (PM). When PM invokes this method, it dispatches
11053 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
11054 * suspend the device.
11055 *
11056 * Return code
11057 * 0 - driver suspended the device
11058 * Error otherwise
11059 **/
11060static int
11061lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
11062{
11063 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11064 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11065 int rc = -ENODEV;
11066
11067 switch (phba->pci_dev_grp) {
11068 case LPFC_PCI_DEV_LP:
11069 rc = lpfc_pci_suspend_one_s3(pdev, msg);
11070 break;
James Smartda0436e2009-05-22 14:51:39 -040011071 case LPFC_PCI_DEV_OC:
11072 rc = lpfc_pci_suspend_one_s4(pdev, msg);
11073 break;
James Smart3772a992009-05-22 14:50:54 -040011074 default:
11075 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11076 "1425 Invalid PCI device group: 0x%x\n",
11077 phba->pci_dev_grp);
11078 break;
11079 }
11080 return rc;
11081}
11082
11083/**
11084 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
11085 * @pdev: pointer to PCI device
11086 *
11087 * This routine is to be registered to the kernel's PCI subsystem to support
11088 * system Power Management (PM). When PM invokes this method, it dispatches
11089 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
11090 * resume the device.
11091 *
11092 * Return code
11093 * 0 - driver suspended the device
11094 * Error otherwise
11095 **/
11096static int
11097lpfc_pci_resume_one(struct pci_dev *pdev)
11098{
11099 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11100 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11101 int rc = -ENODEV;
11102
11103 switch (phba->pci_dev_grp) {
11104 case LPFC_PCI_DEV_LP:
11105 rc = lpfc_pci_resume_one_s3(pdev);
11106 break;
James Smartda0436e2009-05-22 14:51:39 -040011107 case LPFC_PCI_DEV_OC:
11108 rc = lpfc_pci_resume_one_s4(pdev);
11109 break;
James Smart3772a992009-05-22 14:50:54 -040011110 default:
11111 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11112 "1426 Invalid PCI device group: 0x%x\n",
11113 phba->pci_dev_grp);
11114 break;
11115 }
11116 return rc;
11117}
11118
11119/**
11120 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
11121 * @pdev: pointer to PCI device.
11122 * @state: the current PCI connection state.
11123 *
11124 * This routine is registered to the PCI subsystem for error handling. This
11125 * function is called by the PCI subsystem after a PCI bus error affecting
11126 * this device has been detected. When this routine is invoked, it dispatches
11127 * the action to the proper SLI-3 or SLI-4 device error detected handling
11128 * routine, which will perform the proper error detected operation.
11129 *
11130 * Return codes
11131 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
11132 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11133 **/
11134static pci_ers_result_t
11135lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
11136{
11137 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11138 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11139 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
11140
11141 switch (phba->pci_dev_grp) {
11142 case LPFC_PCI_DEV_LP:
11143 rc = lpfc_io_error_detected_s3(pdev, state);
11144 break;
James Smartda0436e2009-05-22 14:51:39 -040011145 case LPFC_PCI_DEV_OC:
11146 rc = lpfc_io_error_detected_s4(pdev, state);
11147 break;
James Smart3772a992009-05-22 14:50:54 -040011148 default:
11149 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11150 "1427 Invalid PCI device group: 0x%x\n",
11151 phba->pci_dev_grp);
11152 break;
11153 }
11154 return rc;
11155}
11156
11157/**
11158 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
11159 * @pdev: pointer to PCI device.
11160 *
11161 * This routine is registered to the PCI subsystem for error handling. This
11162 * function is called after PCI bus has been reset to restart the PCI card
11163 * from scratch, as if from a cold-boot. When this routine is invoked, it
11164 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
11165 * routine, which will perform the proper device reset.
11166 *
11167 * Return codes
11168 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
11169 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11170 **/
11171static pci_ers_result_t
11172lpfc_io_slot_reset(struct pci_dev *pdev)
11173{
11174 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11175 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11176 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
11177
11178 switch (phba->pci_dev_grp) {
11179 case LPFC_PCI_DEV_LP:
11180 rc = lpfc_io_slot_reset_s3(pdev);
11181 break;
James Smartda0436e2009-05-22 14:51:39 -040011182 case LPFC_PCI_DEV_OC:
11183 rc = lpfc_io_slot_reset_s4(pdev);
11184 break;
James Smart3772a992009-05-22 14:50:54 -040011185 default:
11186 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11187 "1428 Invalid PCI device group: 0x%x\n",
11188 phba->pci_dev_grp);
11189 break;
11190 }
11191 return rc;
11192}
11193
11194/**
11195 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
11196 * @pdev: pointer to PCI device
11197 *
11198 * This routine is registered to the PCI subsystem for error handling. It
11199 * is called when kernel error recovery tells the lpfc driver that it is
11200 * OK to resume normal PCI operation after PCI bus error recovery. When
11201 * this routine is invoked, it dispatches the action to the proper SLI-3
11202 * or SLI-4 device io_resume routine, which will resume the device operation.
11203 **/
11204static void
11205lpfc_io_resume(struct pci_dev *pdev)
11206{
11207 struct Scsi_Host *shost = pci_get_drvdata(pdev);
11208 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11209
11210 switch (phba->pci_dev_grp) {
11211 case LPFC_PCI_DEV_LP:
11212 lpfc_io_resume_s3(pdev);
11213 break;
James Smartda0436e2009-05-22 14:51:39 -040011214 case LPFC_PCI_DEV_OC:
11215 lpfc_io_resume_s4(pdev);
11216 break;
James Smart3772a992009-05-22 14:50:54 -040011217 default:
11218 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11219 "1429 Invalid PCI device group: 0x%x\n",
11220 phba->pci_dev_grp);
11221 break;
11222 }
11223 return;
11224}
11225
James Smart1ba981f2014-02-20 09:56:45 -050011226/**
11227 * lpfc_sli4_oas_verify - Verify OAS is supported by this adapter
11228 * @phba: pointer to lpfc hba data structure.
11229 *
11230 * This routine checks to see if OAS is supported for this adapter. If
11231 * supported, the configure Flash Optimized Fabric flag is set. Otherwise,
11232 * the enable oas flag is cleared and the pool created for OAS device data
11233 * is destroyed.
11234 *
11235 **/
11236void
11237lpfc_sli4_oas_verify(struct lpfc_hba *phba)
11238{
11239
11240 if (!phba->cfg_EnableXLane)
11241 return;
11242
11243 if (phba->sli4_hba.pc_sli4_params.oas_supported) {
11244 phba->cfg_fof = 1;
11245 } else {
James Smartf38fa0b2014-04-04 13:52:21 -040011246 phba->cfg_fof = 0;
James Smart1ba981f2014-02-20 09:56:45 -050011247 if (phba->device_data_mem_pool)
11248 mempool_destroy(phba->device_data_mem_pool);
11249 phba->device_data_mem_pool = NULL;
11250 }
11251
11252 return;
11253}
11254
11255/**
11256 * lpfc_fof_queue_setup - Set up all the fof queues
11257 * @phba: pointer to lpfc hba data structure.
11258 *
11259 * This routine is invoked to set up all the fof queues for the FC HBA
11260 * operation.
11261 *
11262 * Return codes
11263 * 0 - successful
11264 * -ENOMEM - No available memory
11265 **/
11266int
11267lpfc_fof_queue_setup(struct lpfc_hba *phba)
11268{
11269 struct lpfc_sli *psli = &phba->sli;
11270 int rc;
11271
11272 rc = lpfc_eq_create(phba, phba->sli4_hba.fof_eq, LPFC_MAX_IMAX);
11273 if (rc)
11274 return -ENOMEM;
11275
James Smartf38fa0b2014-04-04 13:52:21 -040011276 if (phba->cfg_fof) {
James Smart1ba981f2014-02-20 09:56:45 -050011277
11278 rc = lpfc_cq_create(phba, phba->sli4_hba.oas_cq,
11279 phba->sli4_hba.fof_eq, LPFC_WCQ, LPFC_FCP);
11280 if (rc)
11281 goto out_oas_cq;
11282
11283 rc = lpfc_wq_create(phba, phba->sli4_hba.oas_wq,
11284 phba->sli4_hba.oas_cq, LPFC_FCP);
11285 if (rc)
11286 goto out_oas_wq;
11287
11288 phba->sli4_hba.oas_cq->pring = &psli->ring[LPFC_FCP_OAS_RING];
11289 phba->sli4_hba.oas_ring = &psli->ring[LPFC_FCP_OAS_RING];
11290 }
11291
11292 return 0;
11293
11294out_oas_wq:
James Smartf38fa0b2014-04-04 13:52:21 -040011295 lpfc_cq_destroy(phba, phba->sli4_hba.oas_cq);
James Smart1ba981f2014-02-20 09:56:45 -050011296out_oas_cq:
11297 lpfc_eq_destroy(phba, phba->sli4_hba.fof_eq);
11298 return rc;
11299
11300}
11301
11302/**
11303 * lpfc_fof_queue_create - Create all the fof queues
11304 * @phba: pointer to lpfc hba data structure.
11305 *
11306 * This routine is invoked to allocate all the fof queues for the FC HBA
11307 * operation. For each SLI4 queue type, the parameters such as queue entry
11308 * count (queue depth) shall be taken from the module parameter. For now,
11309 * we just use some constant number as place holder.
11310 *
11311 * Return codes
11312 * 0 - successful
11313 * -ENOMEM - No availble memory
11314 * -EIO - The mailbox failed to complete successfully.
11315 **/
11316int
11317lpfc_fof_queue_create(struct lpfc_hba *phba)
11318{
11319 struct lpfc_queue *qdesc;
11320
11321 /* Create FOF EQ */
11322 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
11323 phba->sli4_hba.eq_ecount);
11324 if (!qdesc)
11325 goto out_error;
11326
11327 phba->sli4_hba.fof_eq = qdesc;
11328
James Smartf38fa0b2014-04-04 13:52:21 -040011329 if (phba->cfg_fof) {
James Smart1ba981f2014-02-20 09:56:45 -050011330
11331 /* Create OAS CQ */
11332 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
11333 phba->sli4_hba.cq_ecount);
11334 if (!qdesc)
11335 goto out_error;
11336
11337 phba->sli4_hba.oas_cq = qdesc;
11338
11339 /* Create OAS WQ */
11340 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
11341 phba->sli4_hba.wq_ecount);
11342 if (!qdesc)
11343 goto out_error;
11344
11345 phba->sli4_hba.oas_wq = qdesc;
11346
11347 }
11348 return 0;
11349
11350out_error:
11351 lpfc_fof_queue_destroy(phba);
11352 return -ENOMEM;
11353}
11354
11355/**
11356 * lpfc_fof_queue_destroy - Destroy all the fof queues
11357 * @phba: pointer to lpfc hba data structure.
11358 *
11359 * This routine is invoked to release all the SLI4 queues with the FC HBA
11360 * operation.
11361 *
11362 * Return codes
11363 * 0 - successful
11364 **/
11365int
11366lpfc_fof_queue_destroy(struct lpfc_hba *phba)
11367{
11368 /* Release FOF Event queue */
11369 if (phba->sli4_hba.fof_eq != NULL) {
11370 lpfc_sli4_queue_free(phba->sli4_hba.fof_eq);
11371 phba->sli4_hba.fof_eq = NULL;
11372 }
11373
11374 /* Release OAS Completion queue */
11375 if (phba->sli4_hba.oas_cq != NULL) {
11376 lpfc_sli4_queue_free(phba->sli4_hba.oas_cq);
11377 phba->sli4_hba.oas_cq = NULL;
11378 }
11379
11380 /* Release OAS Work queue */
11381 if (phba->sli4_hba.oas_wq != NULL) {
11382 lpfc_sli4_queue_free(phba->sli4_hba.oas_wq);
11383 phba->sli4_hba.oas_wq = NULL;
11384 }
11385 return 0;
11386}
11387
dea31012005-04-17 16:05:31 -050011388static struct pci_device_id lpfc_id_table[] = {
11389 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
11390 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -040011391 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
11392 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011393 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
11394 PCI_ANY_ID, PCI_ANY_ID, },
11395 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
11396 PCI_ANY_ID, PCI_ANY_ID, },
11397 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
11398 PCI_ANY_ID, PCI_ANY_ID, },
11399 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
11400 PCI_ANY_ID, PCI_ANY_ID, },
11401 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
11402 PCI_ANY_ID, PCI_ANY_ID, },
11403 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
11404 PCI_ANY_ID, PCI_ANY_ID, },
11405 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
11406 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050011407 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
11408 PCI_ANY_ID, PCI_ANY_ID, },
11409 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
11410 PCI_ANY_ID, PCI_ANY_ID, },
11411 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
11412 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011413 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
11414 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050011415 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
11416 PCI_ANY_ID, PCI_ANY_ID, },
11417 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
11418 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011419 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
11420 PCI_ANY_ID, PCI_ANY_ID, },
11421 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
11422 PCI_ANY_ID, PCI_ANY_ID, },
11423 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
11424 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -040011425 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
11426 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050011427 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
11428 PCI_ANY_ID, PCI_ANY_ID, },
11429 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
11430 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011431 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
11432 PCI_ANY_ID, PCI_ANY_ID, },
11433 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
11434 PCI_ANY_ID, PCI_ANY_ID, },
11435 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
11436 PCI_ANY_ID, PCI_ANY_ID, },
11437 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
11438 PCI_ANY_ID, PCI_ANY_ID, },
11439 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
11440 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050011441 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
11442 PCI_ANY_ID, PCI_ANY_ID, },
11443 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
11444 PCI_ANY_ID, PCI_ANY_ID, },
James Smartb87eab32007-04-25 09:53:28 -040011445 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
11446 PCI_ANY_ID, PCI_ANY_ID, },
11447 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
11448 PCI_ANY_ID, PCI_ANY_ID, },
11449 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
11450 PCI_ANY_ID, PCI_ANY_ID, },
11451 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
11452 PCI_ANY_ID, PCI_ANY_ID, },
11453 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
11454 PCI_ANY_ID, PCI_ANY_ID, },
11455 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
11456 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -040011457 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
11458 PCI_ANY_ID, PCI_ANY_ID, },
11459 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
11460 PCI_ANY_ID, PCI_ANY_ID, },
11461 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
11462 PCI_ANY_ID, PCI_ANY_ID, },
James Smart3772a992009-05-22 14:50:54 -040011463 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
11464 PCI_ANY_ID, PCI_ANY_ID, },
James Smarta747c9c2009-11-18 15:41:10 -050011465 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TOMCAT,
11466 PCI_ANY_ID, PCI_ANY_ID, },
11467 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FALCON,
James Smart6669f9b2009-10-02 15:16:45 -040011468 PCI_ANY_ID, PCI_ANY_ID, },
James Smart98fc5dd2010-06-07 15:24:29 -040011469 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BALIUS,
11470 PCI_ANY_ID, PCI_ANY_ID, },
James Smart085c6472010-11-20 23:11:37 -050011471 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC,
11472 PCI_ANY_ID, PCI_ANY_ID, },
11473 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE,
11474 PCI_ANY_ID, PCI_ANY_ID, },
James Smartc0c11512011-05-24 11:41:34 -040011475 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC_VF,
11476 PCI_ANY_ID, PCI_ANY_ID, },
11477 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE_VF,
11478 PCI_ANY_ID, PCI_ANY_ID, },
James Smartd38dd522015-08-31 16:48:17 -040011479 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_G6_FC,
11480 PCI_ANY_ID, PCI_ANY_ID, },
James Smartf8cafd32012-08-03 12:42:51 -040011481 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SKYHAWK,
11482 PCI_ANY_ID, PCI_ANY_ID, },
11483 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SKYHAWK_VF,
11484 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011485 { 0 }
11486};
11487
11488MODULE_DEVICE_TABLE(pci, lpfc_id_table);
11489
Stephen Hemmingera55b2d22012-09-07 09:33:16 -070011490static const struct pci_error_handlers lpfc_err_handler = {
Linas Vepstas8d63f372007-02-14 14:28:36 -060011491 .error_detected = lpfc_io_error_detected,
11492 .slot_reset = lpfc_io_slot_reset,
11493 .resume = lpfc_io_resume,
11494};
11495
dea31012005-04-17 16:05:31 -050011496static struct pci_driver lpfc_driver = {
11497 .name = LPFC_DRIVER_NAME,
11498 .id_table = lpfc_id_table,
11499 .probe = lpfc_pci_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011500 .remove = lpfc_pci_remove_one,
James Smart3a55b532008-12-04 22:38:54 -050011501 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -040011502 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -050011503 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -050011504};
11505
James Smart3ef6d242012-01-18 16:23:48 -050011506static const struct file_operations lpfc_mgmt_fop = {
Al Viro858feac2013-04-14 22:39:37 -040011507 .owner = THIS_MODULE,
James Smart3ef6d242012-01-18 16:23:48 -050011508};
11509
11510static struct miscdevice lpfc_mgmt_dev = {
11511 .minor = MISC_DYNAMIC_MINOR,
11512 .name = "lpfcmgmt",
11513 .fops = &lpfc_mgmt_fop,
11514};
11515
James Smarte59058c2008-08-24 21:49:00 -040011516/**
James Smart3621a712009-04-06 18:47:14 -040011517 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -040011518 *
11519 * This routine is to be invoked when the lpfc module is loaded into the
11520 * kernel. The special kernel macro module_init() is used to indicate the
11521 * role of this routine to the kernel as lpfc module entry point.
11522 *
11523 * Return codes
11524 * 0 - successful
11525 * -ENOMEM - FC attach transport failed
11526 * all others - failed
11527 */
dea31012005-04-17 16:05:31 -050011528static int __init
11529lpfc_init(void)
11530{
James Smart7bb03bb2013-04-17 20:19:16 -040011531 int cpu;
dea31012005-04-17 16:05:31 -050011532 int error = 0;
11533
11534 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040011535 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -050011536
James Smart3ef6d242012-01-18 16:23:48 -050011537 error = misc_register(&lpfc_mgmt_dev);
11538 if (error)
11539 printk(KERN_ERR "Could not register lpfcmgmt device, "
11540 "misc_register returned with status %d", error);
11541
James Smart7ee5d432007-10-27 13:37:17 -040011542 if (lpfc_enable_npiv) {
11543 lpfc_transport_functions.vport_create = lpfc_vport_create;
11544 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
11545 }
dea31012005-04-17 16:05:31 -050011546 lpfc_transport_template =
11547 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -040011548 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -050011549 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -040011550 if (lpfc_enable_npiv) {
James Smart7ee5d432007-10-27 13:37:17 -040011551 lpfc_vport_transport_template =
James Smart98c9ea52007-10-27 13:37:33 -040011552 fc_attach_transport(&lpfc_vport_transport_functions);
11553 if (lpfc_vport_transport_template == NULL) {
11554 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040011555 return -ENOMEM;
James Smart98c9ea52007-10-27 13:37:33 -040011556 }
James Smart7ee5d432007-10-27 13:37:17 -040011557 }
James Smart7bb03bb2013-04-17 20:19:16 -040011558
11559 /* Initialize in case vector mapping is needed */
James Smartb246de12013-05-31 17:03:07 -040011560 lpfc_used_cpu = NULL;
11561 lpfc_present_cpu = 0;
11562 for_each_present_cpu(cpu)
11563 lpfc_present_cpu++;
James Smart7bb03bb2013-04-17 20:19:16 -040011564
dea31012005-04-17 16:05:31 -050011565 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -050011566 if (error) {
dea31012005-04-17 16:05:31 -050011567 fc_release_transport(lpfc_transport_template);
James Smartd7c255b2008-08-24 21:50:00 -040011568 if (lpfc_enable_npiv)
11569 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -050011570 }
dea31012005-04-17 16:05:31 -050011571
11572 return error;
11573}
11574
James Smarte59058c2008-08-24 21:49:00 -040011575/**
James Smart3621a712009-04-06 18:47:14 -040011576 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -040011577 *
11578 * This routine is invoked when the lpfc module is removed from the kernel.
11579 * The special kernel macro module_exit() is used to indicate the role of
11580 * this routine to the kernel as lpfc module exit point.
11581 */
dea31012005-04-17 16:05:31 -050011582static void __exit
11583lpfc_exit(void)
11584{
James Smart3ef6d242012-01-18 16:23:48 -050011585 misc_deregister(&lpfc_mgmt_dev);
dea31012005-04-17 16:05:31 -050011586 pci_unregister_driver(&lpfc_driver);
11587 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040011588 if (lpfc_enable_npiv)
11589 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -050011590 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040011591 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
11592 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050011593 (1L << _dump_buf_data_order), _dump_buf_data);
11594 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
11595 }
11596
11597 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -040011598 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
11599 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050011600 (1L << _dump_buf_dif_order), _dump_buf_dif);
11601 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
11602 }
James Smartb246de12013-05-31 17:03:07 -040011603 kfree(lpfc_used_cpu);
Johannes Thumshirn79739672015-08-31 16:48:11 -040011604 idr_destroy(&lpfc_hba_index);
dea31012005-04-17 16:05:31 -050011605}
11606
11607module_init(lpfc_init);
11608module_exit(lpfc_exit);
11609MODULE_LICENSE("GPL");
11610MODULE_DESCRIPTION(LPFC_MODULE_DESC);
11611MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
11612MODULE_VERSION("0:" LPFC_DRIVER_VERSION);